




'''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 					_a :





     '''simple docstring'''



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







     def 			snake_case_      (				self							)      ->				Optional[Any]:
          snake_case			:      int        =			ids_tensor([self.batch_size, self.seq_length]							,self.vocab_size							)

          snake_case			:      Optional[Any]        =			None
          if self.use_input_mask:
               snake_case			:      Optional[Any]        =			random_attention_mask([self.batch_size, self.seq_length]							)

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

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

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

          snake_case			:      Dict        =			self.get_config()

          snake_case			:      Union[str, Any]        =			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 			snake_case_      (				self							)      ->				List[Any]:
          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 			snake_case_      (				self							,__a							,__a							,__a							,__a							,__a							,*__a							)      ->				Any:
          snake_case			:      Optional[int]        =			CTRLModel(config=a__							)
          model.to(a__							)
          model.eval()

          model(a__							,token_type_ids=a__							,head_mask=a__							)
          model(a__							,token_type_ids=a__							)
          snake_case			:      Any        =			model(a__							)
          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 			snake_case_      (				self							,__a							,__a							,__a							,__a							,__a							,*__a							)      ->				Union[str, Any]:
          snake_case			:      List[str]        =			CTRLLMHeadModel(a__							)
          model.to(a__							)
          model.eval()

          snake_case			:      Optional[Any]        =			model(a__							,token_type_ids=a__							,labels=a__							)
          self.parent.assertEqual(result.loss.shape							,()							)
          self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.seq_length, self.vocab_size)							)







     def 			snake_case_      (				self							)      ->				Tuple:
          snake_case			:      Any        =			self.prepare_config_and_inputs()

          (
              snake_case     
          )			:      str        =			config_and_inputs

          snake_case			:      Optional[Any]        =			{"input_ids": input_ids, "token_type_ids": token_type_ids, "head_mask": head_mask}

          return config, inputs_dict







     def 			snake_case_      (				self							,__a							,__a							,__a							,__a							,*__a							)      ->				Dict:
          snake_case			:      Dict        =			self.num_labels
          snake_case			:      int        =			CTRLForSequenceClassification(a__							)
          model.to(a__							)
          model.eval()
          snake_case			:      int        =			ids_tensor([self.batch_size]							,self.type_sequence_label_size							)
          snake_case			:      Any        =			model(a__							,token_type_ids=a__							,labels=a__							)
          self.parent.assertEqual(result.logits.shape							,(self.batch_size, self.num_labels)							)







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





     '''simple docstring'''



     lowerCAmelCase_  :     str          =					(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
     lowerCAmelCase_  :     Optional[Any]          =					(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_  :     Optional[int]          =					False
     lowerCAmelCase_  :     int          =					False







     def 			snake_case_      (				self							,__a							,__a							,__a							,__a							,__a							)      ->				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 			snake_case_      (				self							)      ->				List[Any]:
          snake_case			:      str        =			CTRLModelTester(self							)
          snake_case			:      str        =			ConfigTester(self							,config_class=a__							,n_embd=37							)







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







     def 			snake_case_      (				self							)      ->				Union[str, Any]:
          self.config_tester.run_common_tests()







     def 			snake_case_      (				self							)      ->				Tuple:
          snake_case			:      int        =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_ctrl_model(*a__							)







     def 			snake_case_      (				self							)      ->				List[Any]:
          snake_case			:      Optional[int]        =			self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_lm_head_model(*a__							)







     @unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""							)
     def 			snake_case_      (				self							)      ->				Union[str, Any]:
          pass







     @slow
     def 			snake_case_      (				self							)      ->				Optional[int]:

          for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
               snake_case			:      Any        =			CTRLModel.from_pretrained(a__							)
               self.assertIsNotNone(a__							)







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







@require_torch
class 					_a (unittest.TestCase	):





     '''simple docstring'''



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







     @slow
     def 			snake_case_      (				self							)      ->				List[str]:
          snake_case			:      Dict        =			CTRLLMHeadModel.from_pretrained("""ctrl"""							)
          model.to(a__							)
          snake_case			:      Union[str, Any]        =			torch.tensor(
              [[11_859, 0, 1_611, 8]]							,dtype=torch.long							,device=a__							)  # Legal the president is
          snake_case			:      Optional[Any]        =			[
              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

          snake_case			:      Dict        =			model.generate(a__							,do_sample=a__							)
          self.assertListEqual(output_ids[0].tolist()							,a__							)





'''simple docstring'''

from ....configuration_utils import PretrainedConfig
from ....utils import logging


snake_case									=    logging.get_logger(__name__)

snake_case									=    {
    """Visual-Attention-Network/van-base""": (
        """https://huggingface.co/Visual-Attention-Network/van-base/blob/main/config.json"""
    ),
}




class    lowerCAmelCase       (     UpperCamelCase_  ):
 A_				:				Tuple	=			"""van"""


 def __init__(    self  : List[Any]			,							a__  : Dict=224			,							a__  : Dict=3			,							a__  : Union[str, Any]=[7, 3, 3, 3]			,							a__  : Optional[Any]=[4, 2, 2, 2]			,							a__  : Optional[Any]=[64, 128, 320, 512]			,							a__  : List[str]=[3, 3, 12, 3]			,							a__  : Any=[8, 8, 4, 4]			,							a__  : Optional[int]="gelu"			,							a__  : List[Any]=0.02			,							a__  : Tuple=1e-6			,							a__  : List[str]=1e-2			,							a__  : List[str]=0.0			,							a__  : List[Any]=0.0			,							**a__  : Tuple			,							):
  '''simple docstring'''

  super().__init__(**a__    )
  lowerCAmelCase__     :   Any     		=	image_size
  lowerCAmelCase__     :   Tuple     		=	num_channels
  lowerCAmelCase__     :   List[Any]     		=	patch_sizes
  lowerCAmelCase__     :   Dict     		=	strides
  lowerCAmelCase__     :   List[str]     		=	hidden_sizes
  lowerCAmelCase__     :   Union[str, Any]     		=	depths
  lowerCAmelCase__     :   Tuple     		=	mlp_ratios
  lowerCAmelCase__     :   Optional[Any]     		=	hidden_act
  lowerCAmelCase__     :   Dict     		=	initializer_range
  lowerCAmelCase__     :   int     		=	layer_norm_eps
  lowerCAmelCase__     :   Optional[Any]     		=	layer_scale_init_value
  lowerCAmelCase__     :   List[str]     		=	drop_path_rate
  lowerCAmelCase__     :   Any     		=	dropout_rate





