


import copy
import inspect
import unittest

from transformers import AutoBackbone
from transformers.configuration_utils import PretrainedConfig
from transformers.testing_utils import require_timm, require_torch, torch_device
from transformers.utils.import_utils import is_torch_available

from ...test_backbone_common import BackboneTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor


if is_torch_available():
 import torch

 from transformers import TimmBackbone, TimmBackboneConfig

from ...test_pipeline_mixin import PipelineTesterMixin


class 		_SCREAMING_SNAKE_CASE     :


     def __init__(  self							,					A_							,					A_=None							,					A_=None							,					A_=None							,					A_="resnet50"							,					A_=3							,					A_=32							,					A_=3							,					A_=True							,					A_=True							,					):
       _UpperCAmelCase		:							Dict							=     parent
       _UpperCAmelCase		:							str							=     out_indices if out_indices is not None else [4]
       _UpperCAmelCase		:							Any							=     stage_names
       _UpperCAmelCase		:							str							=     out_features
       _UpperCAmelCase		:							Optional[int]							=     backbone
       _UpperCAmelCase		:							Dict							=     batch_size
       _UpperCAmelCase		:							int							=     image_size
       _UpperCAmelCase		:							Optional[int]							=     num_channels
       _UpperCAmelCase		:							Optional[int]							=     use_pretrained_backbone
       _UpperCAmelCase		:							Any							=     is_training


     def        __snake_case(  self    ):
       _UpperCAmelCase		:							Optional[Any]							=     floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )
       _UpperCAmelCase		:							Optional[int]							=     self.get_config()

       return config, pixel_values


     def        __snake_case(  self    ):
       return TimmBackboneConfig(
           image_size=self.image_size							,					num_channels=self.num_channels							,					out_features=self.out_features							,					out_indices=self.out_indices							,					stage_names=self.stage_names							,					use_pretrained_backbone=self.use_pretrained_backbone							,					backbone=self.backbone							,					)


     def        __snake_case(  self							,					A_							,					A_    ):
       _UpperCAmelCase		:							List[str]							=     TimmBackbone(config=A_    )
       model.to(A_    )
       model.eval()
       with torch.no_grad():
         _UpperCAmelCase		:							Union[str, Any]							=     model(A_    )
       self.parent.assertEqual(
           result.feature_map[-1].shape							,					(self.batch_size, model.channels[-1], 14, 14)							,					)




     def        __snake_case(  self    ):
       _UpperCAmelCase		:							Dict							=     self.prepare_config_and_inputs()
       _UpperCAmelCase,_UpperCAmelCase		:							int							=     config_and_inputs
       _UpperCAmelCase		:							Dict							=     {"""pixel_values""": pixel_values}
       return config, inputs_dict


@require_torch
@require_timm
class 		_SCREAMING_SNAKE_CASE     (						A							,	A							,	A							,	unittest.TestCase     ):
     __SCREAMING_SNAKE_CASE     					=    (TimmBackbone,) if is_torch_available() else ()
     __SCREAMING_SNAKE_CASE     					=    {'''feature-extraction''': TimmBackbone} if is_torch_available() else {}
     __SCREAMING_SNAKE_CASE     					=    False
     __SCREAMING_SNAKE_CASE     					=    False
     __SCREAMING_SNAKE_CASE     					=    False
     __SCREAMING_SNAKE_CASE     					=    False


     def        __snake_case(  self    ):
       _UpperCAmelCase		:							int							=     TimmBackboneModelTester(self    )
       _UpperCAmelCase		:							Optional[Any]							=     ConfigTester(self							,					config_class=A_							,					has_text_modality=A_    )


     def        __snake_case(  self    ):
       self.config_tester.create_and_test_config_to_json_string()
       self.config_tester.create_and_test_config_to_json_file()
       self.config_tester.create_and_test_config_from_and_save_pretrained()
       self.config_tester.create_and_test_config_with_num_labels()
       self.config_tester.check_config_can_be_init_without_params()
       self.config_tester.check_config_arguments_init()


     def        __snake_case(  self    ):
       _UpperCAmelCase		:							Optional[Any]							=     """resnet18"""
       _UpperCAmelCase		:							List[Any]							=     """microsoft/resnet-18"""

       _UpperCAmelCase		:							Dict							=     AutoBackbone.from_pretrained(A_							,					use_timm_backbone=A_    )
       _UpperCAmelCase		:							List[Any]							=     AutoBackbone.from_pretrained(A_    )

       self.assertEqual(len(timm_model.out_features    )							,					len(transformers_model.out_features    )    )
       self.assertEqual(len(timm_model.stage_names    )							,					len(transformers_model.stage_names    )    )
       self.assertEqual(timm_model.channels							,					transformers_model.channels    )
       # Out indices are set to the last layer by default. For timm models, we don't know
       # the number of layers in advance, so we set it to (-1,), whereas for transformers
       # models, we set it to [len(stage_names) - 1] (kept for backward compatibility).
       self.assertEqual(timm_model.out_indices							,					(-1,)    )
       self.assertEqual(transformers_model.out_indices							,					[len(timm_model.stage_names    ) - 1]    )

       _UpperCAmelCase		:							List[Any]							=     AutoBackbone.from_pretrained(A_							,					use_timm_backbone=A_							,					out_indices=[1, 2, 3]    )
       _UpperCAmelCase		:							List[Any]							=     AutoBackbone.from_pretrained(A_							,					out_indices=[1, 2, 3]    )

       self.assertEqual(timm_model.out_indices							,					transformers_model.out_indices    )
       self.assertEqual(len(timm_model.out_features    )							,					len(transformers_model.out_features    )    )
       self.assertEqual(timm_model.channels							,					transformers_model.channels    )


     @unittest.skip("""TimmBackbone doesn't support feed forward chunking"""    )
     def        __snake_case(  self    ):
       pass


     @unittest.skip("""TimmBackbone doesn't have num_hidden_layers attribute"""    )
     def        __snake_case(  self    ):
       pass


     @unittest.skip("""TimmBackbone initialization is managed on the timm side"""    )
     def        __snake_case(  self    ):
       pass


     @unittest.skip("""TimmBackbone models doesn't have inputs_embeds"""    )
     def        __snake_case(  self    ):
       pass


     @unittest.skip("""TimmBackbone models doesn't have inputs_embeds"""    )
     def        __snake_case(  self    ):
       pass


     @unittest.skip("""TimmBackbone model cannot be created without specifying a backbone checkpoint"""    )
     def        __snake_case(  self    ):
       pass


     @unittest.skip("""Only checkpoints on timm can be loaded into TimmBackbone"""    )
     def        __snake_case(  self    ):
       pass


     @unittest.skip("""model weights aren't tied in TimmBackbone."""    )
     def        __snake_case(  self    ):
       pass


     @unittest.skip("""model weights aren't tied in TimmBackbone."""    )
     def        __snake_case(  self    ):
       pass


     @unittest.skip("""Only checkpoints on timm can be loaded into TimmBackbone"""    )
     def        __snake_case(  self    ):
       pass


     @unittest.skip("""Only checkpoints on timm can be loaded into TimmBackbone"""    )
     def        __snake_case(  self    ):
       pass


     @unittest.skip("""TimmBackbone doesn't have hidden size info in its configuration."""    )
     def        __snake_case(  self    ):
       pass


     @unittest.skip("""TimmBackbone doesn't support output_attentions."""    )
     def        __snake_case(  self    ):
       pass


     @unittest.skip("""Safetensors is not supported by timm."""    )
     def        __snake_case(  self    ):
       pass


     @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""    )
     def        __snake_case(  self    ):
       pass


     def        __snake_case(  self    ):
       _UpperCAmelCase,_UpperCAmelCase		:							List[Any]							=     self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
         _UpperCAmelCase		:							List[Any]							=     model_class(A_    )
         _UpperCAmelCase		:							str							=     inspect.signature(model.forward    )
         # signature.parameters is an OrderedDict => so arg_names order is deterministic
         _UpperCAmelCase		:							Optional[int]							=     [*signature.parameters.keys()]

         _UpperCAmelCase		:							str							=     ["""pixel_values"""]
         self.assertListEqual(arg_names[:1]							,					A_    )


     def        __snake_case(  self    ):
       _UpperCAmelCase,_UpperCAmelCase		:							Optional[Any]							=     self.model_tester.prepare_config_and_inputs_for_common()
       _UpperCAmelCase		:							Union[str, Any]							=     True
       _UpperCAmelCase		:							Tuple							=     self.has_attentions

       # no need to test all models as different heads yield the same functionality
       _UpperCAmelCase		:							Tuple							=     self.all_model_classes[0]
       _UpperCAmelCase		:							List[str]							=     model_class(A_    )
       model.to(A_    )

       _UpperCAmelCase		:							Optional[Any]							=     self._prepare_for_class(A_							,					A_    )
       _UpperCAmelCase		:							Union[str, Any]							=     model(**A_    )
       _UpperCAmelCase		:							Tuple							=     outputs[0][-1]

       # Encoder-/Decoder-only models
       _UpperCAmelCase		:							int							=     outputs.hidden_states[0]
       hidden_states.retain_grad()

       if self.has_attentions:
         _UpperCAmelCase		:							str							=     outputs.attentions[0]
         attentions.retain_grad()

       output.flatten()[0].backward(retain_graph=A_    )

       self.assertIsNotNone(hidden_states.grad    )

       if self.has_attentions:
         self.assertIsNotNone(attentions.grad    )




     def        __snake_case(  self    ):
       _UpperCAmelCase,_UpperCAmelCase		:							Optional[int]							=     self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
         _UpperCAmelCase		:							Dict							=     model_class(A_    )
         model.to(A_    )
         model.eval()
         _UpperCAmelCase		:							Any							=     model(**A_    )

         self.assertEqual(len(result.feature_maps    )							,					len(config.out_indices    )    )
         self.assertEqual(len(model.channels    )							,					len(config.out_indices    )    )

         # Check output of last stage is taken if out_features=None, out_indices=None
         _UpperCAmelCase		:							int							=     copy.deepcopy(A_    )
         _UpperCAmelCase		:							Optional[Any]							=     None
         _UpperCAmelCase		:							str							=     model_class(A_    )
         model.to(A_    )
         model.eval()
         _UpperCAmelCase		:							Dict							=     model(**A_    )

         self.assertEqual(len(result.feature_maps    )							,					1    )
         self.assertEqual(len(model.channels    )							,					1    )

         # Check backbone can be initialized with fresh weights
         _UpperCAmelCase		:							Optional[int]							=     copy.deepcopy(A_    )
         _UpperCAmelCase		:							Tuple							=     False
         _UpperCAmelCase		:							List[str]							=     model_class(A_    )
         model.to(A_    )
         model.eval()
         _UpperCAmelCase		:							int							=     model(**A_    )









from __future__ import annotations

import unittest

from transformers import BlenderbotConfig, BlenderbotTokenizer, is_tf_available
from transformers.testing_utils import require_tf, require_tokenizers, slow
from transformers.utils import cached_property

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
 import tensorflow as tf

 from transformers import TFAutoModelForSeqaSeqLM, TFBlenderbotForConditionalGeneration, TFBlenderbotModel


@require_tf
class 		_SCREAMING_SNAKE_CASE     :
     __SCREAMING_SNAKE_CASE     					=    BlenderbotConfig
     __SCREAMING_SNAKE_CASE     					=    {}
     __SCREAMING_SNAKE_CASE     					=    '''gelu'''


     def __init__(  self							,					A_							,					A_=13							,					A_=7							,					A_=True							,					A_=False							,					A_=99							,					A_=32							,					A_=2							,					A_=4							,					A_=37							,					A_=0.1							,					A_=0.1							,					A_=20							,					A_=2							,					A_=1							,					A_=0							,					):
       _UpperCAmelCase		:							List[str]							=     parent
       _UpperCAmelCase		:							Union[str, Any]							=     batch_size
       _UpperCAmelCase		:							Optional[Any]							=     seq_length
       _UpperCAmelCase		:							List[Any]							=     is_training
       _UpperCAmelCase		:							Union[str, Any]							=     use_labels
       _UpperCAmelCase		:							Tuple							=     vocab_size
       _UpperCAmelCase		:							Optional[int]							=     hidden_size
       _UpperCAmelCase		:							int							=     num_hidden_layers
       _UpperCAmelCase		:							List[Any]							=     num_attention_heads
       _UpperCAmelCase		:							List[Any]							=     intermediate_size

       _UpperCAmelCase		:							Union[str, Any]							=     hidden_dropout_prob
       _UpperCAmelCase		:							str							=     attention_probs_dropout_prob
       _UpperCAmelCase		:							Optional[int]							=     max_position_embeddings
       _UpperCAmelCase		:							Optional[Any]							=     eos_token_id
       _UpperCAmelCase		:							Optional[Any]							=     pad_token_id
       _UpperCAmelCase		:							Tuple							=     bos_token_id


     def        __snake_case(  self    ):
       _UpperCAmelCase		:							Optional[Any]							=     ids_tensor([self.batch_size, self.seq_length - 1]							,					self.vocab_size    )
       _UpperCAmelCase		:							Tuple							=     tf.expand_dims(tf.constant([self.eos_token_id] * self.batch_size    )							,					1    )
       _UpperCAmelCase		:							List[Any]							=     tf.concat([input_ids, eos_tensor]							,					axis=1    )

       _UpperCAmelCase		:							int							=     ids_tensor([self.batch_size, self.seq_length]							,					self.vocab_size    )

       _UpperCAmelCase		:							List[Any]							=     self.config_cls(
           vocab_size=self.vocab_size							,					d_model=self.hidden_size							,					encoder_layers=self.num_hidden_layers							,					decoder_layers=self.num_hidden_layers							,					encoder_attention_heads=self.num_attention_heads							,					decoder_attention_heads=self.num_attention_heads							,					encoder_ffn_dim=self.intermediate_size							,					decoder_ffn_dim=self.intermediate_size							,					dropout=self.hidden_dropout_prob							,					attention_dropout=self.attention_probs_dropout_prob							,					max_position_embeddings=self.max_position_embeddings							,					eos_token_ids=[2]							,					bos_token_id=self.bos_token_id							,					pad_token_id=self.pad_token_id							,					decoder_start_token_id=self.pad_token_id							,					**self.config_updates							,					)
       _UpperCAmelCase		:							Dict							=     prepare_blenderbot_inputs_dict(A_							,					A_							,					A_    )
       return config, inputs_dict




     def        __snake_case(  self							,					A_							,					A_    ):
       _UpperCAmelCase		:							Union[str, Any]							=     TFBlenderbotModel(config=A_    ).get_decoder()
       _UpperCAmelCase		:							List[Any]							=     inputs_dict["""input_ids"""]

       _UpperCAmelCase		:							Any							=     input_ids[:1, :]
       _UpperCAmelCase		:							List[str]							=     inputs_dict["""attention_mask"""][:1, :]
       _UpperCAmelCase		:							Tuple							=     inputs_dict["""head_mask"""]
       _UpperCAmelCase		:							Union[str, Any]							=     1

       # first forward pass
       _UpperCAmelCase		:							List[str]							=     model(A_							,					attention_mask=A_							,					head_mask=A_							,					use_cache=A_    )

       _UpperCAmelCase,_UpperCAmelCase		:							List[str]							=     outputs.to_tuple()

       # create hypothetical next token and extent to next_input_ids
       _UpperCAmelCase		:							List[Any]							=     ids_tensor((self.batch_size, 3)							,					config.vocab_size    )
       _UpperCAmelCase		:							List[str]							=     tf.cast(ids_tensor((self.batch_size, 3)							,					2    )							,					tf.inta    )

       # append to next input_ids and
       _UpperCAmelCase		:							Dict							=     tf.concat([input_ids, next_tokens]							,					axis=-1    )
       _UpperCAmelCase		:							Optional[Any]							=     tf.concat([attention_mask, next_attn_mask]							,					axis=-1    )

       _UpperCAmelCase		:							Tuple							=     model(A_							,					attention_mask=A_    )[0]
       _UpperCAmelCase		:							int							=     model(A_							,					attention_mask=A_							,					past_key_values=A_    )[0]

       self.parent.assertEqual(next_tokens.shape[1]							,					output_from_past.shape[1]    )

       # select random slice
       _UpperCAmelCase		:							Tuple							=     int(ids_tensor((1,)							,					output_from_past.shape[-1]    )    )
       _UpperCAmelCase		:							str							=     output_from_no_past[:, -3:, random_slice_idx]
       _UpperCAmelCase		:							str							=     output_from_past[:, :, random_slice_idx]

       # test that outputs are equal for slice
       tf.debugging.assert_near(A_							,					A_							,					rtol=1e-3    )




def    a__    (		snake_case__					:							Optional[Any]						,   snake_case__					:							str						,   snake_case__					:							Dict						,   snake_case__					:							str=None						,   snake_case__					:							Optional[int]=None						,   snake_case__					:							str=None						,   snake_case__					:							List[str]=None						,   snake_case__					:							List[str]=None						,   ):
  if attention_mask is None:
    _UpperCAmelCase		:							List[Any]							=     tf.cast(tf.math.not_equal(snake_case__						,   config.pad_token_id     )						,   tf.inta     )
  if decoder_attention_mask is None:
    _UpperCAmelCase		:							str							=     tf.concat(
        [
            tf.ones(decoder_input_ids[:, :1].shape						,   dtype=tf.inta     ),
            tf.cast(tf.math.not_equal(decoder_input_ids[:, 1:]						,   config.pad_token_id     )						,   tf.inta     ),
        ]						,   axis=-1						,   )
  if head_mask is None:
    _UpperCAmelCase		:							List[str]							=     tf.ones((config.encoder_layers, config.encoder_attention_heads)     )
  if decoder_head_mask is None:
    _UpperCAmelCase		:							Tuple							=     tf.ones((config.decoder_layers, config.decoder_attention_heads)     )
  if cross_attn_head_mask is None:
    _UpperCAmelCase		:							Dict							=     tf.ones((config.decoder_layers, config.decoder_attention_heads)     )
  return {
      "input_ids": input_ids,
      "decoder_input_ids": decoder_input_ids,
      "attention_mask": attention_mask,
      "decoder_attention_mask": decoder_attention_mask,
      "head_mask": head_mask,
      "decoder_head_mask": decoder_head_mask,
      "cross_attn_head_mask": cross_attn_head_mask,
  }


@require_tf
class 		_SCREAMING_SNAKE_CASE     (						A							,	A							,	unittest.TestCase     ):
     __SCREAMING_SNAKE_CASE     					=    (TFBlenderbotForConditionalGeneration, TFBlenderbotModel) if is_tf_available() else ()
     __SCREAMING_SNAKE_CASE     					=    (TFBlenderbotForConditionalGeneration,) if is_tf_available() else ()
     __SCREAMING_SNAKE_CASE     					=    (
         {
             '''conversational''': TFBlenderbotForConditionalGeneration,
             '''feature-extraction''': TFBlenderbotModel,
             '''summarization''': TFBlenderbotForConditionalGeneration,
             '''text2text-generation''': TFBlenderbotForConditionalGeneration,
             '''translation''': TFBlenderbotForConditionalGeneration,
         }
         if is_tf_available()
         else {}
     )
     __SCREAMING_SNAKE_CASE     					=    True
     __SCREAMING_SNAKE_CASE     					=    False
     __SCREAMING_SNAKE_CASE     					=    False


     def        __snake_case(  self    ):
       _UpperCAmelCase		:							Union[str, Any]							=     TFBlenderbotModelTester(self    )
       _UpperCAmelCase		:							Any							=     ConfigTester(self							,					config_class=A_    )


     def        __snake_case(  self    ):
       self.config_tester.run_common_tests()




     def        __snake_case(  self    ):
       _UpperCAmelCase		:							Tuple							=     self.model_tester.prepare_config_and_inputs_for_common()
       self.model_tester.check_decoder_model_past_large_inputs(*A_    )


@require_tokenizers
@require_tf
class 		_SCREAMING_SNAKE_CASE     (						unittest.TestCase     ):
     __SCREAMING_SNAKE_CASE     					=    ['''My friends are cool but they eat too many carbs.''']
     __SCREAMING_SNAKE_CASE     					=    '''facebook/blenderbot-400M-distill'''


     @cached_property
     def        __snake_case(  self    ):
       return BlenderbotTokenizer.from_pretrained(self.model_name    )


     @cached_property
     def        __snake_case(  self    ):
       _UpperCAmelCase		:							List[Any]							=     TFAutoModelForSeqaSeqLM.from_pretrained(self.model_name    )
       return model




     @slow
     def        __snake_case(  self    ):
       _UpperCAmelCase		:							Any							=     self.tokenizer(self.src_text							,					return_tensors="""tf"""    )
       _UpperCAmelCase		:							List[Any]							=     self.model.generate(
           model_inputs.input_ids							,					)
       _UpperCAmelCase		:							List[str]							=     self.tokenizer.batch_decode(generated_ids.numpy()							,					skip_special_tokens=A_    )[0]
       assert (
           generated_words
           == " That's unfortunate. Are they trying to lose weight or are they just trying to be healthier?"
       )





