




'''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					):
  '''simple docstring'''




  __lowercase							:				int           =				MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING
  __lowercase							:				Any           =				TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING




  def 							UpperCAmelCase_     (   self  ,__UpperCAmelCase  ,__UpperCAmelCase  ,__UpperCAmelCase				)   ->		str:
      lowerCAmelCase__			:  Optional[Any]											= TextaTextGenerationPipeline(model=__UpperCAmelCase  ,tokenizer=__UpperCAmelCase				)
      return generator, ["Something to write", "Something else"]




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

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

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

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




  @require_torch
  def 							UpperCAmelCase_     (   self				)   ->		List[Any]:
      lowerCAmelCase__			:  List[str]											= pipeline("""text2text-generation"""  ,model="""patrickvonplaten/t5-tiny-random"""  ,framework="""pt"""				)
      # do_sample=False necessary for reproducibility
      lowerCAmelCase__			:  Any											= generator("""Something there"""  ,do_sample=__UpperCAmelCase				)
      self.assertEqual(__UpperCAmelCase  ,[{"""generated_text""": """"""}]				)

      lowerCAmelCase__			:  List[Any]											= 3
      lowerCAmelCase__			:  Any											= generator(
          """Something there"""  ,num_return_sequences=__UpperCAmelCase  ,num_beams=__UpperCAmelCase  ,)
      lowerCAmelCase__			:  List[Any]											= [
          {"""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(__UpperCAmelCase  ,__UpperCAmelCase				)

      lowerCAmelCase__			:  int											= generator("""This is a test"""  ,do_sample=__UpperCAmelCase  ,num_return_sequences=2  ,return_tensors=__UpperCAmelCase				)
      self.assertEqual(
          __UpperCAmelCase  ,[
              {"""generated_token_ids""": ANY(torch.Tensor				)},
              {"""generated_token_ids""": ANY(torch.Tensor				)},
          ]  ,)
      lowerCAmelCase__			:  List[Any]											= generator.model.config.eos_token_id
      lowerCAmelCase__			:  List[Any]											= """<pad>"""
      lowerCAmelCase__			:  Any											= generator(
          ["""This is a test""", """This is a second test"""]  ,do_sample=__UpperCAmelCase  ,num_return_sequences=2  ,batch_size=2  ,return_tensors=__UpperCAmelCase  ,)
      self.assertEqual(
          __UpperCAmelCase  ,[
              [
                  {"""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 							UpperCAmelCase_     (   self				)   ->		List[str]:
      lowerCAmelCase__			:  int											= pipeline("""text2text-generation"""  ,model="""patrickvonplaten/t5-tiny-random"""  ,framework="""tf"""				)
      # do_sample=False necessary for reproducibility
      lowerCAmelCase__			:  List[str]											= generator("""Something there"""  ,do_sample=__UpperCAmelCase				)
      self.assertEqual(__UpperCAmelCase  ,[{"""generated_text""": """"""}]				)







'''simple docstring'''





from ...configuration_utils import PretrainedConfig
from ...utils import logging
from ...utils.backbone_utils import BackboneConfigMixin, get_aligned_output_features_output_indices


_lowerCAmelCase										=     logging.get_logger(__name__)

_lowerCAmelCase										=     {
    '''facebook/convnextv2-tiny-1k-224''': '''https://huggingface.co/facebook/convnextv2-tiny-1k-224/resolve/main/config.json''',
}






class 	lowerCAmelCase_(    SCREAMING_SNAKE_CASE_						,			SCREAMING_SNAKE_CASE_					):
  '''simple docstring'''




  __lowercase							:				Tuple           =				'''convnextv2'''




  def __init__(   self  ,__UpperCAmelCase=3  ,__UpperCAmelCase=4  ,__UpperCAmelCase=4  ,__UpperCAmelCase=None  ,__UpperCAmelCase=None  ,__UpperCAmelCase="gelu"  ,__UpperCAmelCase=0.0_2  ,__UpperCAmelCase=1E-12  ,__UpperCAmelCase=0.0  ,__UpperCAmelCase=224  ,__UpperCAmelCase=None  ,__UpperCAmelCase=None  ,**__UpperCAmelCase  ,)   ->		Union[str, Any]:
      super().__init__(**__UpperCAmelCase				)

      lowerCAmelCase__			:  int											= num_channels
      lowerCAmelCase__			:  List[Any]											= patch_size
      lowerCAmelCase__			:  Union[str, Any]											= num_stages
      lowerCAmelCase__			:  Tuple											= [96, 192, 384, 768] if hidden_sizes is None else hidden_sizes
      lowerCAmelCase__			:  str											= [3, 3, 9, 3] if depths is None else depths
      lowerCAmelCase__			:  Optional[Any]											= hidden_act
      lowerCAmelCase__			:  str											= initializer_range
      lowerCAmelCase__			:  List[str]											= layer_norm_eps
      lowerCAmelCase__			:  Dict											= drop_path_rate
      lowerCAmelCase__			:  int											= image_size
      lowerCAmelCase__			:  int											= ["""stem"""] + [F"""stage{idx}""" for idx in range(1  ,len(self.depths				) + 1				)]
      lowerCAmelCase__				,       lowerCAmelCase__			:  Tuple											= get_aligned_output_features_output_indices(
          out_features=__UpperCAmelCase  ,out_indices=__UpperCAmelCase  ,stage_names=self.stage_names				)


