





"""simple docstring"""





import unittest

from transformers import (
    MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
    TextaTextGenerationPipeline,
    pipeline,
)
from transformers.testing_utils import is_pipeline_test, require_tf, require_torch
from transformers.utils import is_torch_available

from .test_pipelines_common import ANY


if is_torch_available():
						import torch





@is_pipeline_test
class        _lowerCamelCase      (       unittest.TestCase ):
						UpperCAmelCase_ =			MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
						UpperCAmelCase_ =			TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING


						def     snake_case_   (self   ,							__a   ,							__a   ,							__a		) ->   int:
								UpperCamelCase   =	TextaTextGenerationPipeline(model=__a   ,							tokenizer=__a		)
								return generator, ["Something to write", "Something else"]


						def     snake_case_   (self   ,							__a   ,							__a		) ->   Union[str, Any]:
								UpperCamelCase   =	generator("Something there"		)
								self.assertEqual(__a   ,							[{"generated_text": ANY(__a		)}]		)
								# These are encoder decoder, they don't just append to incoming string
								self.assertFalse(outputs[0]["generated_text"].startswith("Something there"		)		)

								UpperCamelCase   =	generator(["This is great !", "Something else"]   ,							num_return_sequences=2   ,							do_sample=__a		)
								self.assertEqual(
								    __a   ,							[
								        [{"generated_text": ANY(__a		)}, {"generated_text": ANY(__a		)}],
								        [{"generated_text": ANY(__a		)}, {"generated_text": ANY(__a		)}],
								    ]   ,							)

								UpperCamelCase   =	generator(
								    ["This is great !", "Something else"]   ,							num_return_sequences=2   ,							batch_size=2   ,							do_sample=__a		)
								self.assertEqual(
								    __a   ,							[
								        [{"generated_text": ANY(__a		)}, {"generated_text": ANY(__a		)}],
								        [{"generated_text": ANY(__a		)}, {"generated_text": ANY(__a		)}],
								    ]   ,							)

								with self.assertRaises(__a		):
										generator(4		)


						@require_torch
						def     snake_case_   (self		) ->   Optional[int]:
								UpperCamelCase   =	pipeline("text2text-generation"   ,							model="patrickvonplaten/t5-tiny-random"   ,							framework="pt"		)
								# do_sample=False necessary for reproducibility
								UpperCamelCase   =	generator("Something there"   ,							do_sample=__a		)
								self.assertEqual(__a   ,							[{"generated_text": ""}]		)

								UpperCamelCase   =	3
								UpperCamelCase   =	generator(
								    "Something there"   ,							num_return_sequences=__a   ,							num_beams=__a   ,							)
								UpperCamelCase   =	[
								    {"generated_text": "Beide Beide Beide Beide Beide Beide Beide Beide Beide"},
								    {"generated_text": "Beide Beide Beide Beide Beide Beide Beide Beide"},
								    {"generated_text": ""},
								]
								self.assertEqual(__a   ,							__a		)

								UpperCamelCase   =	generator("This is a test"   ,							do_sample=__a   ,							num_return_sequences=2   ,							return_tensors=__a		)
								self.assertEqual(
								    __a   ,							[
								        {"generated_token_ids": ANY(torch.Tensor		)},
								        {"generated_token_ids": ANY(torch.Tensor		)},
								    ]   ,							)
								UpperCamelCase   =	generator.model.config.eos_token_id
								UpperCamelCase   =	"<pad>"
								UpperCamelCase   =	generator(
								    ["This is a test", "This is a second test"]   ,							do_sample=__a   ,							num_return_sequences=2   ,							batch_size=2   ,							return_tensors=__a   ,							)
								self.assertEqual(
								    __a   ,							[
								        [
								            {"generated_token_ids": ANY(torch.Tensor		)},
								            {"generated_token_ids": ANY(torch.Tensor		)},
								        ],
								        [
								            {"generated_token_ids": ANY(torch.Tensor		)},
								            {"generated_token_ids": ANY(torch.Tensor		)},
								        ],
								    ]   ,							)




						@require_tf
						def     snake_case_   (self		) ->   Dict:
								UpperCamelCase   =	pipeline("text2text-generation"   ,							model="patrickvonplaten/t5-tiny-random"   ,							framework="tf"		)
								# do_sample=False necessary for reproducibility
								UpperCamelCase   =	generator("Something there"   ,							do_sample=__a		)
								self.assertEqual(__a   ,							[{"generated_text": ""}]		)













"""simple docstring"""





import copy

from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ..auto.configuration_auto import CONFIG_MAPPING


lowerCAmelCase__     			=			logging.get_logger(__name__)





class        _lowerCamelCase      (       _lowercase ):
						UpperCAmelCase_ =			"upernet"


						def __init__(self   ,							__a=None   ,							__a=5_12   ,							__a=0.02   ,							__a=[1, 2, 3, 6]   ,							__a=True   ,							__a=0.4   ,							__a=3_84   ,							__a=2_56   ,							__a=1   ,							__a=False   ,							__a=2_55   ,							**__a   ,							) ->   Tuple:
								super().__init__(**__a		)

								if backbone_config is None:
										logger.info("`backbone_config` is `None`. Initializing the config with the default `ResNet` backbone."		)
										UpperCamelCase   =	CONFIG_MAPPING["resnet"](out_features=["stage1", "stage2", "stage3", "stage4"]		)
								elif isinstance(__a   ,							__a		):
										UpperCamelCase   =	backbone_config.get("model_type"		)
										UpperCamelCase   =	CONFIG_MAPPING[backbone_model_type]
										UpperCamelCase   =	config_class.from_dict(__a		)

								UpperCamelCase   =	backbone_config
								UpperCamelCase   =	hidden_size
								UpperCamelCase   =	initializer_range
								UpperCamelCase   =	pool_scales
								UpperCamelCase   =	use_auxiliary_head
								UpperCamelCase   =	auxiliary_loss_weight
								UpperCamelCase   =	auxiliary_in_channels
								UpperCamelCase   =	auxiliary_channels
								UpperCamelCase   =	auxiliary_num_convs
								UpperCamelCase   =	auxiliary_concat_input
								UpperCamelCase   =	loss_ignore_index




						def     snake_case_   (self		) ->   Optional[int]:
								UpperCamelCase   =	copy.deepcopy(self.__dict__		)
								UpperCamelCase   =	self.backbone_config.to_dict()
								UpperCamelCase   =	self.__class__.model_type
								return output






