

from __future__ import annotations

import unittest

from transformers import CTRLConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

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


if is_tf_available():
							import tensorflow as tf

							from transformers.models.ctrl.modeling_tf_ctrl import (
							    TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
							    TFCTRLForSequenceClassification,
							    TFCTRLLMHeadModel,
							    TFCTRLModel,
							)







class 						lowercase     (					_lowercase				):


		"""simple docstring"""




		def __init__(					self						,							__snake_case						,							):
			_UpperCamelCase :		Optional[Any]								=    parent
			_UpperCamelCase :		Optional[Any]								=    13
			_UpperCamelCase :		int								=    7
			_UpperCamelCase :		Union[str, Any]								=    True
			_UpperCamelCase :		Any								=    True
			_UpperCamelCase :		Optional[int]								=    True
			_UpperCamelCase :		Union[str, Any]								=    True
			_UpperCamelCase :		Optional[Any]								=    True
			_UpperCamelCase :		Optional[int]								=    99
			_UpperCamelCase :		str								=    32
			_UpperCamelCase :		Dict								=    2
			_UpperCamelCase :		Any								=    4
			_UpperCamelCase :		List[Any]								=    37
			_UpperCamelCase :		Optional[int]								=    'gelu'
			_UpperCamelCase :		Optional[Any]								=    0.1
			_UpperCamelCase :		Optional[Any]								=    0.1
			_UpperCamelCase :		List[str]								=    5_12
			_UpperCamelCase :		str								=    16
			_UpperCamelCase :		Optional[Any]								=    2
			_UpperCamelCase :		Optional[int]								=    0.0_2
			_UpperCamelCase :		int								=    3
			_UpperCamelCase :		Optional[Any]								=    4
			_UpperCamelCase :		Tuple								=    None
			_UpperCamelCase :		Union[str, Any]								=    self.vocab_size - 1


		def 		A__       (					self):
			_UpperCamelCase :		Optional[int]								=    ids_tensor([self.batch_size, self.seq_length]						,							self.vocab_size)

			_UpperCamelCase :		Dict								=    None
			if self.use_input_mask:
				_UpperCamelCase :		Tuple								=    random_attention_mask([self.batch_size, self.seq_length])

			_UpperCamelCase :		Dict								=    None
			if self.use_token_type_ids:
				_UpperCamelCase :		Optional[int]								=    ids_tensor([self.batch_size, self.seq_length]						,							self.type_vocab_size)

			_UpperCamelCase :		Optional[Any]								=    None
			if self.use_mc_token_ids:
				_UpperCamelCase :		int								=    ids_tensor([self.batch_size, self.num_choices]						,							self.seq_length)

			_UpperCamelCase :		Dict								=    None
			_UpperCamelCase :		Optional[int]								=    None
			_UpperCamelCase :		List[str]								=    None
			if self.use_labels:
				_UpperCamelCase :		Optional[Any]								=    ids_tensor([self.batch_size]						,							self.type_sequence_label_size)
				_UpperCamelCase :		str								=    ids_tensor([self.batch_size, self.seq_length]						,							self.num_labels)
				_UpperCamelCase :		Optional[Any]								=    ids_tensor([self.batch_size]						,							self.num_choices)

			_UpperCamelCase :		Tuple								=    CTRLConfig(
			    vocab_size=self.vocab_size						,							n_embd=self.hidden_size						,							n_layer=self.num_hidden_layers						,							n_head=self.num_attention_heads						,							n_positions=self.max_position_embeddings						,							pad_token_id=self.pad_token_id						,							)

			_UpperCamelCase :		str								=    ids_tensor([self.num_hidden_layers, self.num_attention_heads]						,							2)

			return (
			    config,
			    input_ids,
			    input_mask,
			    head_mask,
			    token_type_ids,
			    mc_token_ids,
			    sequence_labels,
			    token_labels,
			    choice_labels,
			)


		def 		A__       (					self						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case						,							*__snake_case):
			_UpperCamelCase :		Tuple								=    TFCTRLModel(config=__snake_case)
			_UpperCamelCase :		List[str]								=    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
			_UpperCamelCase :		List[str]								=    model(__snake_case)

			_UpperCamelCase :		Tuple								=    [input_ids, None, input_mask]  # None is the input for 'past'
			_UpperCamelCase :		Optional[Any]								=    model(__snake_case)

			_UpperCamelCase :		Optional[Any]								=    model(__snake_case)

			self.parent.assertEqual(result.last_hidden_state.shape						,							(self.batch_size, self.seq_length, self.hidden_size))


		def 		A__       (					self						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case						,							*__snake_case):
			_UpperCamelCase :		int								=    TFCTRLLMHeadModel(config=__snake_case)
			_UpperCamelCase :		Optional[int]								=    {'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
			_UpperCamelCase :		Union[str, Any]								=    model(__snake_case)
			self.parent.assertEqual(result.logits.shape						,							(self.batch_size, self.seq_length, self.vocab_size))


		def 		A__       (					self						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case						,							*__snake_case):
			_UpperCamelCase :		Optional[int]								=    self.num_labels
			_UpperCamelCase :		Any								=    ids_tensor([self.batch_size]						,							self.type_sequence_label_size)
			_UpperCamelCase :		Union[str, Any]								=    {
			    'input_ids': input_ids,
			    'token_type_ids': token_type_ids,
			    'labels': sequence_labels,
			}
			_UpperCamelCase :		Any								=    TFCTRLForSequenceClassification(__snake_case)
			_UpperCamelCase :		Any								=    model(__snake_case)
			self.parent.assertEqual(result.logits.shape						,							(self.batch_size, self.num_labels))






		def 		A__       (					self):
			_UpperCamelCase :		Dict								=    self.prepare_config_and_inputs()

			(
			    (
			    _UpperCamelCase
			)						,		(
			    _UpperCamelCase
			)						,		(
			    _UpperCamelCase
			)						,		(
			    _UpperCamelCase
			)						,		(
			    _UpperCamelCase
			)						,		(
			    _UpperCamelCase
			)						,		(
			    _UpperCamelCase
			)						,		(
			    _UpperCamelCase
			)						,		(
			    _UpperCamelCase
			)						,		
			) :		Optional[int]								=    config_and_inputs

			_UpperCamelCase :		List[str]								=    {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
			return config, inputs_dict







@require_tf
class 						lowercase     (					_lowercase     ,				_lowercase     ,				unittest.TestCase				):


		"""simple docstring"""




		a__       							=	(TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
		a__       							=	(TFCTRLLMHeadModel,) if is_tf_available() else ()
		a__       							=	(
		    {
		        "feature-extraction": TFCTRLModel,
		        "text-classification": TFCTRLForSequenceClassification,
		        "text-generation": TFCTRLLMHeadModel,
		        "zero-shot": TFCTRLForSequenceClassification,
		    }
		    if is_tf_available()
		    else {}
		)
		a__       							=	False
		a__       							=	False


		def 		A__       (					self						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case						,							__snake_case):
			if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
				# Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
				# `CTRLConfig` was never used in pipeline tests, either because of a missing checkpoint or because a tiny
				# config could not be created.
				return True

			return False


		def 		A__       (					self):
			_UpperCamelCase :		List[Any]								=    TFCTRLModelTester(self)
			_UpperCamelCase :		Any								=    ConfigTester(self						,							config_class=__snake_case						,							n_embd=37)


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


		def 		A__       (					self):
			_UpperCamelCase :		Optional[int]								=    self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_ctrl_model(*__snake_case)


		def 		A__       (					self):
			_UpperCamelCase :		Optional[int]								=    self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_ctrl_lm_head(*__snake_case)


		def 		A__       (					self):
			_UpperCamelCase :		Optional[int]								=    self.model_tester.prepare_config_and_inputs()
			self.model_tester.create_and_check_ctrl_for_sequence_classification(*__snake_case)


		def 		A__       (					self):
			_UpperCamelCase						,		_UpperCamelCase :		Optional[Any]								=    self.model_tester.prepare_config_and_inputs_for_common()
			_UpperCamelCase :		str								=    [TFCTRLLMHeadModel]
			_UpperCamelCase :		Union[str, Any]								=    [TFCTRLForSequenceClassification]

			for model_class in self.all_model_classes:
				_UpperCamelCase :		int								=    model_class(__snake_case)
				model.build()  # may be needed for the get_bias() call below
				assert isinstance(model.get_input_embeddings()						,							tf.keras.layers.Layer)

				if model_class in list_lm_models:
					_UpperCamelCase :		Tuple								=    model.get_output_embeddings()
					assert isinstance(__snake_case						,							tf.keras.layers.Layer)
					_UpperCamelCase :		Any								=    model.get_bias()
					assert isinstance(__snake_case						,							__snake_case)
					for k, v in name.items():
						assert isinstance(__snake_case						,							tf.Variable)
				elif model_class in list_other_models_with_output_ebd:
					_UpperCamelCase :		int								=    model.get_output_embeddings()
					assert isinstance(__snake_case						,							tf.keras.layers.Layer)
					_UpperCamelCase :		List[Any]								=    model.get_bias()
					assert name is None
				else:
					_UpperCamelCase :		Union[str, Any]								=    model.get_output_embeddings()
					assert x is None
					_UpperCamelCase :		Any								=    model.get_bias()
					assert name is None






		@slow
		def 		A__       (					self):
			for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
				_UpperCamelCase :		Optional[int]								=    TFCTRLModel.from_pretrained(__snake_case)
				self.assertIsNotNone(__snake_case)







@require_tf
class 						lowercase     (					unittest.TestCase				):


		"""simple docstring"""




		@slow
		def 		A__       (					self):
			_UpperCamelCase :		Optional[Any]								=    TFCTRLLMHeadModel.from_pretrained('ctrl')
			_UpperCamelCase :		Tuple								=    tf.convert_to_tensor([[1_18_59, 0, 16_11, 8]]						,							dtype=tf.intaa)  # Legal the president is
			_UpperCamelCase :		Optional[Any]								=    [
			    1_18_59,
			    0,
			    16_11,
			    8,
			    5,
			    1_50,
			    2_64_49,
			    2,
			    19,
			    3_48,
			    4_69,
			    3,
			    25_95,
			    48,
			    2_07_40,
			    24_65_33,
			    24_65_33,
			    19,
			    30,
			    5,
			]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

			_UpperCamelCase :		Any								=    model.generate(__snake_case						,							do_sample=__snake_case)
			self.assertListEqual(output_ids[0].numpy().tolist()						,							__snake_case)





'''simple docstring'''

import copy
import inspect

from transformers.testing_utils import require_torch, torch_device
from transformers.utils.backbone_utils import BackboneType




@require_torch
class    lowerCamelCase__  :
   __UpperCAmelCase           =						()
   __UpperCAmelCase           =						True



   def  _UpperCamelCase		(    self     )				->     Tuple:
        """simple docstring"""





        _UpperCamelCase  :List[str]       =self.config_class

        # test default config
        _UpperCamelCase  :List[Any]       =config_class()
        self.assertIsNotNone(lowerCAmelCase__     )
        _UpperCamelCase  :List[str]       =["""stem"""] + [f'''stage{idx}''' for idx in range(1					,      len(config.depths     ) + 1     )]
        self.assertEqual(config.stage_names					,      lowerCAmelCase__     )
        self.assertTrue(set(config.out_features     ).issubset(set(config.stage_names     )     )     )

        # Test out_features and out_indices are correctly set
        # out_features and out_indices both None
        _UpperCamelCase  :Any       =config_class(out_features=lowerCAmelCase__					,      out_indices=lowerCAmelCase__     )
        self.assertEqual(config.out_features					,      [config.stage_names[-1]]     )
        self.assertEqual(config.out_indices					,      [len(config.stage_names     ) - 1]     )

        # out_features and out_indices both set
        _UpperCamelCase  :int       =config_class(out_features=["""stem""", """stage1"""]					,      out_indices=[0, 1]     )
        self.assertEqual(config.out_features					,      ["""stem""", """stage1"""]     )
        self.assertEqual(config.out_indices					,      [0, 1]     )

        # Only out_features set
        _UpperCamelCase  :Dict       =config_class(out_features=["""stage1""", """stage3"""]     )
        self.assertEqual(config.out_features					,      ["""stage1""", """stage3"""]     )
        self.assertEqual(config.out_indices					,      [1, 3]     )

        # Only out_indices set
        _UpperCamelCase  :Tuple       =config_class(out_indices=[0, 2]     )
        self.assertEqual(config.out_features					,      [config.stage_names[0], config.stage_names[2]]     )
        self.assertEqual(config.out_indices					,      [0, 2]     )

        # Error raised when out_indices do not correspond to out_features
        with self.assertRaises(lowerCAmelCase__     ):
             _UpperCamelCase  :List[Any]       =config_class(out_features=["""stage1""", """stage2"""]					,      out_indices=[0, 2]     )



   def  _UpperCamelCase		(    self     )				->     List[Any]:
        """simple docstring"""





        _UpperCamelCase       ,      _UpperCamelCase  :Optional[Any]       =self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             _UpperCamelCase  :Union[str, Any]       =model_class(lowerCAmelCase__     )
             _UpperCamelCase  :Union[str, Any]       =inspect.signature(model.forward     )
             # signature.parameters is an OrderedDict => so arg_names order is deterministic
             _UpperCamelCase  :Tuple       =[*signature.parameters.keys()]
             _UpperCamelCase  :Any       =["""pixel_values"""]
             self.assertListEqual(arg_names[:1]					,      lowerCAmelCase__     )



   def  _UpperCamelCase		(    self     )				->     int:
        """simple docstring"""





        _UpperCamelCase       ,      _UpperCamelCase  :List[str]       =self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             _UpperCamelCase  :int       =model_class(lowerCAmelCase__     )
             self.assertEqual(len(model.channels     )					,      len(config.out_features     )     )
             _UpperCamelCase  :List[str]       =model.num_features
             _UpperCamelCase  :int       =[config.stage_names.index(lowerCAmelCase__     ) for feat in config.out_features]
             _UpperCamelCase  :List[str]       =[num_features[idx] for idx in out_indices]
             self.assertListEqual(model.channels					,      lowerCAmelCase__     )

             _UpperCamelCase  :Any       =copy.deepcopy(lowerCAmelCase__     )
             _UpperCamelCase  :List[Any]       =None
             _UpperCamelCase  :Optional[Any]       =model_class(lowerCAmelCase__     )
             self.assertEqual(len(model.channels     )					,      1     )
             self.assertListEqual(model.channels					,      [num_features[-1]]     )

             _UpperCamelCase  :Tuple       =copy.deepcopy(lowerCAmelCase__     )
             _UpperCamelCase  :int       =None
             _UpperCamelCase  :int       =model_class(lowerCAmelCase__     )
             self.assertEqual(len(model.channels     )					,      1     )
             self.assertListEqual(model.channels					,      [num_features[-1]]     )



   def  _UpperCamelCase		(    self     )				->     str:
        """simple docstring"""





        _UpperCamelCase       ,      _UpperCamelCase  :Any       =self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             _UpperCamelCase  :List[Any]       =model_class(lowerCAmelCase__     )
             model.to(lowerCAmelCase__     )
             model.eval()
             _UpperCamelCase  :str       =model(**lowerCAmelCase__     )

             self.assertEqual(len(result.feature_maps     )					,      len(config.out_features     )     )
             self.assertEqual(len(model.channels     )					,      len(config.out_features     )     )
             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  :Any       =copy.deepcopy(lowerCAmelCase__     )
             _UpperCamelCase  :List[Any]       =None
             _UpperCamelCase  :Dict       =model_class(lowerCAmelCase__     )
             model.to(lowerCAmelCase__     )
             model.eval()
             _UpperCamelCase  :str       =model(**lowerCAmelCase__     )

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

             _UpperCamelCase  :str       =copy.deepcopy(lowerCAmelCase__     )
             _UpperCamelCase  :Dict       =None
             _UpperCamelCase  :Tuple       =model_class(lowerCAmelCase__     )
             model.to(lowerCAmelCase__     )
             model.eval()
             _UpperCamelCase  :Dict       =model(**lowerCAmelCase__     )

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

             # Check backbone can be initialized with fresh weights
             _UpperCamelCase  :Any       =copy.deepcopy(lowerCAmelCase__     )
             _UpperCamelCase  :Optional[int]       =False
             _UpperCamelCase  :Any       =model_class(lowerCAmelCase__     )
             model.to(lowerCAmelCase__     )
             model.eval()
             _UpperCamelCase  :Optional[int]       =model(**lowerCAmelCase__     )



   def  _UpperCamelCase		(    self     )				->     Optional[int]:
        """simple docstring"""





        _UpperCamelCase       ,      _UpperCamelCase  :Dict       =self.model_tester.prepare_config_and_inputs_for_common()

        for backbone_class in self.all_model_classes:
             _UpperCamelCase  :Optional[int]       =backbone_class(lowerCAmelCase__     )

             self.assertTrue(hasattr(lowerCAmelCase__					,      """backbone_type"""     )     )
             self.assertTrue(hasattr(lowerCAmelCase__					,      """stage_names"""     )     )
             self.assertTrue(hasattr(lowerCAmelCase__					,      """num_features"""     )     )
             self.assertTrue(hasattr(lowerCAmelCase__					,      """out_indices"""     )     )
             self.assertTrue(hasattr(lowerCAmelCase__					,      """out_features"""     )     )
             self.assertTrue(hasattr(lowerCAmelCase__					,      """out_feature_channels"""     )     )
             self.assertTrue(hasattr(lowerCAmelCase__					,      """channels"""     )     )

             self.assertIsInstance(backbone.backbone_type					,      lowerCAmelCase__     )
             # Verify num_features has been initialized in the backbone init
             self.assertIsNotNone(backbone.num_features     )
             self.assertTrue(len(backbone.channels     ) == len(backbone.out_indices     )     )
             self.assertTrue(len(backbone.stage_names     ) == len(backbone.num_features     )     )
             self.assertTrue(len(backbone.channels     ) <= len(backbone.num_features     )     )
             self.assertTrue(len(backbone.out_feature_channels     ) == len(backbone.stage_names     )     )




   def  _UpperCamelCase		(    self     )				->     Tuple:
        """simple docstring"""





        _UpperCamelCase       ,      _UpperCamelCase  :List[Any]       =self.model_tester.prepare_config_and_inputs_for_common()
        _UpperCamelCase  :Union[str, Any]       =inputs_dict["""pixel_values"""].shape[0]

        for backbone_class in self.all_model_classes:
             _UpperCamelCase  :str       =backbone_class(lowerCAmelCase__     )
             backbone.to(lowerCAmelCase__     )
             backbone.eval()

             _UpperCamelCase  :Any       =backbone(**lowerCAmelCase__     )

             # Test default outputs and verify feature maps
             self.assertIsInstance(outputs.feature_maps					,      lowerCAmelCase__     )
             self.assertTrue(len(outputs.feature_maps     ) == len(backbone.channels     )     )
             for feature_map, n_channels in zip(outputs.feature_maps					,      backbone.channels     ):
                  self.assertTrue(feature_map.shape[:2]					,      (batch_size, n_channels)     )
             self.assertIsNone(outputs.hidden_states     )
             self.assertIsNone(outputs.attentions     )

             # Test output_hidden_states=True
             _UpperCamelCase  :Any       =backbone(**lowerCAmelCase__					,      output_hidden_states=lowerCAmelCase__     )
             self.assertIsNotNone(outputs.hidden_states     )
             self.assertTrue(len(outputs.hidden_states     )					,      len(backbone.stage_names     )     )
             for hidden_state, n_channels in zip(outputs.hidden_states					,      backbone.channels     ):
                  self.assertTrue(hidden_state.shape[:2]					,      (batch_size, n_channels)     )

             # Test output_attentions=True
             if self.has_attentions:
                  _UpperCamelCase  :int       =backbone(**lowerCAmelCase__					,      output_attentions=lowerCAmelCase__     )
                  self.assertIsNotNone(outputs.attentions     )