




'''simple docstring'''



import inspect
import unittest
import warnings
from math import ceil, floor

from transformers import LevitConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch

      from transformers import (
          MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
          MODEL_MAPPING,
          LevitForImageClassification,
          LevitForImageClassificationWithTeacher,
          LevitModel,
      )
      from transformers.models.levit.modeling_levit import LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
      from PIL import Image

      from transformers import LevitImageProcessor






class 							_a				(			SCREAMING_SNAKE_CASE ):





       '''simple docstring'''






       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      int    					=						self.config_class(**self.inputs_dict  )
             self.parent.assertTrue(hasattr(A,							'hidden_sizes'  )  )
             self.parent.assertTrue(hasattr(A,							'num_attention_heads'  )  )






class 							_a				:





       '''simple docstring'''






       def __init__(      self,							A,							A=13,							A=64,							A=3,							A=3,							A=2,							A=1,							A=16,							A=[128, 256, 384],							A=[4, 6, 8],							A=[2, 3, 4],							A=[16, 16, 16],							A=0,							A=[2, 2, 2],							A=[2, 2, 2],							A=0.02,							A=True,							A=True,							A=2,							):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      List[str]    					=						parent
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						batch_size
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						image_size
             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						num_channels
             SCREAMING_SNAKE_CASE   :      List[Any]    					=						kernel_size
             SCREAMING_SNAKE_CASE   :      Tuple    					=						stride
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						padding
             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						hidden_sizes
             SCREAMING_SNAKE_CASE   :      List[Any]    					=						num_attention_heads
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						depths
             SCREAMING_SNAKE_CASE   :      Tuple    					=						key_dim
             SCREAMING_SNAKE_CASE   :      List[Any]    					=						drop_path_rate
             SCREAMING_SNAKE_CASE   :      Any    					=						patch_size
             SCREAMING_SNAKE_CASE   :      Any    					=						attention_ratio
             SCREAMING_SNAKE_CASE   :      List[str]    					=						mlp_ratio
             SCREAMING_SNAKE_CASE   :      int    					=						initializer_range
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						[
                 ['Subsample', key_dim[0], hidden_sizes[0] // key_dim[0], 4, 2, 2],
                 ['Subsample', key_dim[0], hidden_sizes[1] // key_dim[0], 4, 2, 2],
             ]
             SCREAMING_SNAKE_CASE   :      str    					=						is_training
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						use_labels
             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						num_labels
             SCREAMING_SNAKE_CASE   :      List[Any]    					=						initializer_range







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

             SCREAMING_SNAKE_CASE   :      Dict    					=						None
             if self.use_labels:
                   SCREAMING_SNAKE_CASE   :      List[str]    					=						ids_tensor([self.batch_size],							self.num_labels  )

             SCREAMING_SNAKE_CASE   :      str    					=						self.get_config()
             return config, pixel_values, labels







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             return LevitConfig(
                 image_size=self.image_size,							num_channels=self.num_channels,							kernel_size=self.kernel_size,							stride=self.stride,							padding=self.padding,							patch_size=self.patch_size,							hidden_sizes=self.hidden_sizes,							num_attention_heads=self.num_attention_heads,							depths=self.depths,							key_dim=self.key_dim,							drop_path_rate=self.drop_path_rate,							mlp_ratio=self.mlp_ratio,							attention_ratio=self.attention_ratio,							initializer_range=self.initializer_range,							down_ops=self.down_ops,							)







       def        UpperCamelCase_							(      self,							A,							A,							A  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Tuple    					=						LevitModel(config=A  )
             model.to(A  )
             model.eval()
             SCREAMING_SNAKE_CASE   :      List[str]    					=						model(A  )
             SCREAMING_SNAKE_CASE   :      int    					=						(self.image_size, self.image_size)
             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						image_size[0], image_size[1]
             for _ in range(4  ):
                   SCREAMING_SNAKE_CASE   :      List[Any]    					=						floor(((height + 2 * self.padding - self.kernel_size) / self.stride) + 1  )
                   SCREAMING_SNAKE_CASE   :      int    					=						floor(((width + 2 * self.padding - self.kernel_size) / self.stride) + 1  )
             self.parent.assertEqual(
                 result.last_hidden_state.shape,							(self.batch_size, ceil(height / 4  ) * ceil(width / 4  ), self.hidden_sizes[-1]),							)







       def        UpperCamelCase_							(      self,							A,							A,							A  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      int    					=						self.num_labels
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						LevitForImageClassification(A  )
             model.to(A  )
             model.eval()
             SCREAMING_SNAKE_CASE   :      Tuple    					=						model(A,							labels=A  )
             self.parent.assertEqual(result.logits.shape,							(self.batch_size, self.num_labels)  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						self.prepare_config_and_inputs()
             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      str    					=						config_and_inputs
             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						{'pixel_values': pixel_values}
             return config, inputs_dict






@require_torch
class 							_a				(			SCREAMING_SNAKE_CASE ,      SCREAMING_SNAKE_CASE ,      unittest.TestCase ):





       '''simple docstring'''






       A						:				Dict        =						(
           (LevitModel, LevitForImageClassification, LevitForImageClassificationWithTeacher)
           if is_torch_available()
           else ()
       )
       A						:				Optional[int]        =						(
           {
               '''feature-extraction''': LevitModel,
               '''image-classification''': (LevitForImageClassification, LevitForImageClassificationWithTeacher),
           }
           if is_torch_available()
           else {}
       )

       A						:				Optional[Any]        =						False
       A						:				List[Any]        =						False
       A						:				str        =						False
       A						:				Dict        =						False
       A						:				Dict        =						False







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      List[Any]    					=						LevitModelTester(self  )
             SCREAMING_SNAKE_CASE   :      Dict    					=						ConfigTester(self,							config_class=A,							has_text_modality=A,							hidden_size=37  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             self.create_and_test_config_common_properties()
             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        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             return







       @unittest.skip(reason='Levit does not use inputs_embeds'  )
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             pass







       @unittest.skip(reason='Levit does not support input and output embeddings'  )
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             pass







       @unittest.skip(reason='Levit does not output attentions'  )
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             pass







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Tuple    					=						self.model_tester.prepare_config_and_inputs_for_common()

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

                   SCREAMING_SNAKE_CASE   :      List[Any]    					=						['pixel_values']
                   self.assertListEqual(arg_names[:1],							A  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             def check_hidden_states_output(A,							A,							A  ):
                   SCREAMING_SNAKE_CASE   :      Any    					=						model_class(A  )
                   model.to(A  )
                   model.eval()

                   with torch.no_grad():
                         SCREAMING_SNAKE_CASE   :      Tuple    					=						model(**self._prepare_for_class(A,							A  )  )

                   SCREAMING_SNAKE_CASE   :      Optional[int]    					=						outputs.hidden_states

                   SCREAMING_SNAKE_CASE   :      Dict    					=						len(self.model_tester.depths  ) + 1
                   self.assertEqual(len(A  ),							A  )

                   SCREAMING_SNAKE_CASE   :      List[str]    					=						(self.model_tester.image_size, self.model_tester.image_size)
                   SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Any    					=						image_size[0], image_size[1]
                   for _ in range(4  ):
                         SCREAMING_SNAKE_CASE   :      List[str]    					=						floor(
                             (
                                 (height + 2 * self.model_tester.padding - self.model_tester.kernel_size)
                                 / self.model_tester.stride
                             )
                             + 1  )
                         SCREAMING_SNAKE_CASE   :      str    					=						floor(
                             (
                                 (width + 2 * self.model_tester.padding - self.model_tester.kernel_size)
                                 / self.model_tester.stride
                             )
                             + 1  )
                   # verify the first hidden states (first block)
                   self.assertListEqual(
                       list(hidden_states[0].shape[-2:]  ),							[
                           height * width,
                           self.model_tester.hidden_sizes[0],
                       ],							)

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

             for model_class in self.all_model_classes:
                   SCREAMING_SNAKE_CASE   :      Tuple    					=						True
                   check_hidden_states_output(A,							A,							A  )

                   # check that output_hidden_states also work using config
                   del inputs_dict["output_hidden_states"]
                   SCREAMING_SNAKE_CASE   :      Tuple    					=						True

                   check_hidden_states_output(A,							A,							A  )







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







             '''simple docstring'''







             pass







       def        UpperCamelCase_							(      self,							A,							A,							A=False  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Optional[int]    					=						super()._prepare_for_class(A,							A,							return_labels=A  )

             if return_labels:
                   if model_class.__name__ == "LevitForImageClassificationWithTeacher":
                         del inputs_dict["labels"]

             return inputs_dict







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      List[Any]    					=						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_model(*A  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						self.model_tester.prepare_config_and_inputs()
             self.model_tester.create_and_check_for_image_classification(*A  )







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             if not self.model_tester.is_training:
                   return

             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Optional[int]    					=						self.model_tester.prepare_config_and_inputs_for_common()
             SCREAMING_SNAKE_CASE   :      Dict    					=						True

             for model_class in self.all_model_classes:
                   # LevitForImageClassificationWithTeacher supports inference-only
                   if (
                       model_class in get_values(A  )
                       or model_class.__name__ == "LevitForImageClassificationWithTeacher"
                   ):
                         continue
                   SCREAMING_SNAKE_CASE   :      Any    					=						model_class(A  )
                   model.to(A  )
                   model.train()
                   SCREAMING_SNAKE_CASE   :      Optional[int]    					=						self._prepare_for_class(A,							A,							return_labels=A  )
                   SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						model(**A  ).loss
                   loss.backward()







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE					,    SCREAMING_SNAKE_CASE   :      Tuple    					=						self.model_tester.prepare_config_and_inputs_for_common()
             if not self.model_tester.is_training:
                   return

             SCREAMING_SNAKE_CASE   :      Tuple    					=						False
             SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						True

             for model_class in self.all_model_classes:
                   if model_class in get_values(A  ) or not model_class.supports_gradient_checkpointing:
                         continue
                   # LevitForImageClassificationWithTeacher supports inference-only
                   if model_class.__name__ == "LevitForImageClassificationWithTeacher":
                         continue
                   SCREAMING_SNAKE_CASE   :      Union[str, Any]    					=						model_class(A  )
                   model.gradient_checkpointing_enable()
                   model.to(A  )
                   model.train()
                   SCREAMING_SNAKE_CASE   :      Tuple    					=						self._prepare_for_class(A,							A,							return_labels=A  )
                   SCREAMING_SNAKE_CASE   :      Any    					=						model(**A  ).loss
                   loss.backward()







       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







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

             SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						[
                 {'title': 'multi_label_classification', 'num_labels': 2, 'dtype': torch.float},
                 {'title': 'single_label_classification', 'num_labels': 1, 'dtype': torch.long},
                 {'title': 'regression', 'num_labels': 1, 'dtype': torch.float},
             ]

             for model_class in self.all_model_classes:
                   if (
                       model_class
                       not in [
                           *get_values(A  ),
                       ]
                       or model_class.__name__ == "LevitForImageClassificationWithTeacher"
                   ):
                         continue

                   for problem_type in problem_types:
                         with self.subTest(msg=F"Testing {model_class} with {problem_type['title']}"  ):
                               SCREAMING_SNAKE_CASE   :      List[str]    					=						problem_type['title']
                               SCREAMING_SNAKE_CASE   :      Tuple    					=						problem_type['num_labels']

                               SCREAMING_SNAKE_CASE   :      Tuple    					=						model_class(A  )
                               model.to(A  )
                               model.train()

                               SCREAMING_SNAKE_CASE   :      Tuple    					=						self._prepare_for_class(A,							A,							return_labels=A  )

                               if problem_type["num_labels"] > 1:
                                     SCREAMING_SNAKE_CASE   :      List[Any]    					=						inputs['labels'].unsqueeze(1  ).repeat(1,							problem_type['num_labels']  )

                               SCREAMING_SNAKE_CASE   :      Optional[int]    					=						inputs['labels'].to(problem_type['dtype']  )

                               # This tests that we do not trigger the warning form PyTorch "Using a target size that is different
                               # to the input size. This will likely lead to incorrect results due to broadcasting. Please ensure
                               # they have the same size." which is a symptom something in wrong for the regression problem.
                               # See https://github.com/huggingface/transformers/issues/11780
                               with warnings.catch_warnings(record=A  ) as warning_list:
                                     SCREAMING_SNAKE_CASE   :      Optional[Any]    					=						model(**A  ).loss
                               for w in warning_list:
                                     if "Using a target size that is different to the input size" in str(w.message  ):
                                           raise ValueError(
                                               F"Something is going wrong in the regression problem: intercepted {w.message}"  )

                               loss.backward()







       @slow
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             for model_name in LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
                   SCREAMING_SNAKE_CASE   :      List[str]    					=						LevitModel.from_pretrained(A  )
                   self.assertIsNotNone(A  )




def   lowercase__(			):

      """simple docstring"""






      SCREAMING_SNAKE_CASE   :      Optional[int]    					=						Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'    )
      return image






@require_torch
@require_vision
class 							_a				(			unittest.TestCase ):





       '''simple docstring'''






       @cached_property
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             return LevitImageProcessor.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]  )







       @slow
       def        UpperCamelCase_							(      self  ):







             '''simple docstring'''







             SCREAMING_SNAKE_CASE   :      str    					=						LevitForImageClassificationWithTeacher.from_pretrained(LEVIT_PRETRAINED_MODEL_ARCHIVE_LIST[0]  ).to(
                 A  )

             SCREAMING_SNAKE_CASE   :      List[Any]    					=						self.default_image_processor
             SCREAMING_SNAKE_CASE   :      str    					=						prepare_img()
             SCREAMING_SNAKE_CASE   :      int    					=						image_processor(images=A,							return_tensors='pt'  ).to(A  )

             # forward pass
             with torch.no_grad():
                   SCREAMING_SNAKE_CASE   :      List[str]    					=						model(**A  )

             # verify the logits
             SCREAMING_SNAKE_CASE   :      Any    					=						torch.Size((1, 1_000)  )
             self.assertEqual(outputs.logits.shape,							A  )

             SCREAMING_SNAKE_CASE   :      List[str]    					=						torch.tensor([1.04_48, -0.37_45, -1.83_17]  ).to(A  )

             self.assertTrue(torch.allclose(outputs.logits[0, :3],							A,							atol=1E-4  )  )







'''simple docstring'''





import gc
import unittest

from transformers import CTRLConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...generation.test_utils import GenerationTesterMixin
from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import (
     CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
     CTRLForSequenceClassification,
     CTRLLMHeadModel,
     CTRLModel,
 )


class 					snake_case			:



       def __init__(					self  ,UpperCAmelCase_  ,UpperCAmelCase_=14  ,UpperCAmelCase_=7  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=True  ,UpperCAmelCase_=99  ,UpperCAmelCase_=32  ,UpperCAmelCase_=5  ,UpperCAmelCase_=4  ,UpperCAmelCase_=37  ,UpperCAmelCase_="gelu"  ,UpperCAmelCase_=0.1  ,UpperCAmelCase_=0.1  ,UpperCAmelCase_=512  ,UpperCAmelCase_=16  ,UpperCAmelCase_=2  ,UpperCAmelCase_=0.02  ,UpperCAmelCase_=3  ,UpperCAmelCase_=4  ,UpperCAmelCase_=None  ,)	->				Dict:
           lowercase__									=    parent
           lowercase__									=    batch_size
           lowercase__									=    seq_length
           lowercase__									=    is_training
           lowercase__									=    use_token_type_ids
           lowercase__									=    use_input_mask
           lowercase__									=    use_labels
           lowercase__									=    use_mc_token_ids
           lowercase__									=    vocab_size
           lowercase__									=    hidden_size
           lowercase__									=    num_hidden_layers
           lowercase__									=    num_attention_heads
           lowercase__									=    intermediate_size
           lowercase__									=    hidden_act
           lowercase__									=    hidden_dropout_prob
           lowercase__									=    attention_probs_dropout_prob
           lowercase__									=    max_position_embeddings
           lowercase__									=    type_vocab_size
           lowercase__									=    type_sequence_label_size
           lowercase__									=    initializer_range
           lowercase__									=    num_labels
           lowercase__									=    num_choices
           lowercase__									=    scope
           lowercase__									=    self.vocab_size - 1



       def 				_a   (					self   )	->				Any:
           lowercase__									=    ids_tensor([self.batch_size, self.seq_length]  ,self.vocab_size   )

           lowercase__									=    None
           if self.use_input_mask:
               lowercase__									=    random_attention_mask([self.batch_size, self.seq_length]   )

           lowercase__									=    None
           if self.use_token_type_ids:
               lowercase__									=    ids_tensor([self.batch_size, self.seq_length]  ,self.type_vocab_size   )

           lowercase__									=    None
           if self.use_mc_token_ids:
               lowercase__									=    ids_tensor([self.batch_size, self.num_choices]  ,self.seq_length   )

           lowercase__									=    None
           lowercase__									=    None
           lowercase__									=    None
           if self.use_labels:
               lowercase__									=    ids_tensor([self.batch_size]  ,self.type_sequence_label_size   )
               lowercase__									=    ids_tensor([self.batch_size, self.seq_length]  ,self.num_labels   )
               lowercase__									=    ids_tensor([self.batch_size]  ,self.num_choices   )

           lowercase__									=    self.get_config()

           lowercase__									=    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   )	->				Optional[int]:
           return 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  ,)



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,*UpperCAmelCase_   )	->				List[Any]:
           lowercase__									=    CTRLModel(config=UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()

           model(UpperCAmelCase_  ,token_type_ids=UpperCAmelCase_  ,head_mask=UpperCAmelCase_   )
           model(UpperCAmelCase_  ,token_type_ids=UpperCAmelCase_   )
           lowercase__									=    model(UpperCAmelCase_   )
           self.parent.assertEqual(result.last_hidden_state.shape  ,(self.batch_size, self.seq_length, self.hidden_size)   )
           self.parent.assertEqual(len(result.past_key_values   )  ,config.n_layer   )



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,*UpperCAmelCase_   )	->				Optional[int]:
           lowercase__									=    CTRLLMHeadModel(UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()

           lowercase__									=    model(UpperCAmelCase_  ,token_type_ids=UpperCAmelCase_  ,labels=UpperCAmelCase_   )
           self.parent.assertEqual(result.loss.shape  ,()   )
           self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.seq_length, self.vocab_size)   )



       def 				_a   (					self   )	->				Union[str, Any]:
           lowercase__									=    self.prepare_config_and_inputs()

           (
               (
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							(
               lowercase__
           )							,							
           )									=    config_and_inputs

           lowercase__									=    {"input_ids": input_ids, "token_type_ids": token_type_ids, "head_mask": head_mask}

           return config, inputs_dict







       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,*UpperCAmelCase_   )	->				Optional[int]:
           lowercase__									=    self.num_labels
           lowercase__									=    CTRLForSequenceClassification(UpperCAmelCase_   )
           model.to(UpperCAmelCase_   )
           model.eval()
           lowercase__									=    ids_tensor([self.batch_size]  ,self.type_sequence_label_size   )
           lowercase__									=    model(UpperCAmelCase_  ,token_type_ids=UpperCAmelCase_  ,labels=UpperCAmelCase_   )
           self.parent.assertEqual(result.logits.shape  ,(self.batch_size, self.num_labels)   )





@require_torch
class 					snake_case			(UpperCamelCase		,   UpperCamelCase		,   UpperCamelCase		,   unittest.TestCase ):
       lowerCAmelCase__	:Union[str, Any]     =       (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
       lowerCAmelCase__	:Dict     =       (CTRLLMHeadModel,) if is_torch_available() else ()
       lowerCAmelCase__	:int     =       (
           {
               "feature-extraction": CTRLModel,
               "text-classification": CTRLForSequenceClassification,
               "text-generation": CTRLLMHeadModel,
               "zero-shot": CTRLForSequenceClassification,
           }
           if is_torch_available()
           else {}
       )
       lowerCAmelCase__	:str     =       True
       lowerCAmelCase__	:Tuple     =       False
       lowerCAmelCase__	:List[Any]     =       False



       def 				_a   (					self  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_  ,UpperCAmelCase_   )	->				Tuple:
           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   )	->				str:
           lowercase__									=    CTRLModelTester(self   )
           lowercase__									=    ConfigTester(self  ,config_class=UpperCAmelCase_  ,n_embd=37   )



       def 				_a   (					self   )	->				List[Any]:
           super().tearDown()
           # clean-up as much as possible GPU memory occupied by PyTorch
           gc.collect()
           torch.cuda.empty_cache()



       def 				_a   (					self   )	->				Tuple:
           self.config_tester.run_common_tests()



       def 				_a   (					self   )	->				Any:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_ctrl_model(*UpperCAmelCase_   )



       def 				_a   (					self   )	->				Optional[Any]:
           lowercase__									=    self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_lm_head_model(*UpperCAmelCase_   )



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



       @slow
       def 				_a   (					self   )	->				List[str]:
           for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               lowercase__									=    CTRLModel.from_pretrained(UpperCAmelCase_   )
               self.assertIsNotNone(UpperCAmelCase_   )







       @unittest.skip("The model doesn't support left padding"   )  # and it's not used enough to be worth fixing :)
       def 				_a   (					self   )	->				Tuple:
           pass





@require_torch
class 					snake_case			(unittest.TestCase ):



       def 				_a   (					self   )	->				int:
           super().tearDown()
           # clean-up as much as possible GPU memory occupied by PyTorch
           gc.collect()
           torch.cuda.empty_cache()







       @slow
       def 				_a   (					self   )	->				int:
           lowercase__									=    CTRLLMHeadModel.from_pretrained("ctrl"   )
           model.to(UpperCAmelCase_   )
           lowercase__									=    torch.tensor(
               [[11_859, 0, 1_611, 8]]  ,dtype=torch.long  ,device=UpperCAmelCase_   )  # Legal the president is
           lowercase__									=    [
               11_859,
               0,
               1_611,
               8,
               5,
               150,
               26_449,
               2,
               19,
               348,
               469,
               3,
               2_595,
               48,
               20_740,
               246_533,
               246_533,
               19,
               30,
               5,
           ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

           lowercase__									=    model.generate(UpperCAmelCase_  ,do_sample=UpperCAmelCase_   )
           self.assertListEqual(output_ids[0].tolist()  ,UpperCAmelCase_   )




