

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

  '''simple docstring'''





  def __init__(  self ,			a_ ,			a_=1_4 ,			a_=7 ,			a_=True ,			a_=True ,			a_=True ,			a_=True ,			a_=True ,			a_=9_9 ,			a_=3_2 ,			a_=5 ,			a_=4 ,			a_=3_7 ,			a_="gelu" ,			a_=0.1 ,			a_=0.1 ,			a_=5_1_2 ,			a_=1_6 ,			a_=2 ,			a_=0.02 ,			a_=3 ,			a_=4 ,			a_=None ,			)				->					List[Any]:


    """simple docstring"""
    UpperCAmelCase    				=						parent
    UpperCAmelCase    				=						batch_size
    UpperCAmelCase    				=						seq_length
    UpperCAmelCase    				=						is_training
    UpperCAmelCase    				=						use_token_type_ids
    UpperCAmelCase    				=						use_input_mask
    UpperCAmelCase    				=						use_labels
    UpperCAmelCase    				=						use_mc_token_ids
    UpperCAmelCase    				=						vocab_size
    UpperCAmelCase    				=						hidden_size
    UpperCAmelCase    				=						num_hidden_layers
    UpperCAmelCase    				=						num_attention_heads
    UpperCAmelCase    				=						intermediate_size
    UpperCAmelCase    				=						hidden_act
    UpperCAmelCase    				=						hidden_dropout_prob
    UpperCAmelCase    				=						attention_probs_dropout_prob
    UpperCAmelCase    				=						max_position_embeddings
    UpperCAmelCase    				=						type_vocab_size
    UpperCAmelCase    				=						type_sequence_label_size
    UpperCAmelCase    				=						initializer_range
    UpperCAmelCase    				=						num_labels
    UpperCAmelCase    				=						num_choices
    UpperCAmelCase    				=						scope
    UpperCAmelCase    				=						self.vocab_size - 1
  def     snake_case_      (  self  )				->					Tuple:


    """simple docstring"""
    UpperCAmelCase    				=						ids_tensor([self.batch_size, self.seq_length] ,			self.vocab_size  )

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

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

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

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

    UpperCAmelCase    				=						self.get_config()

    UpperCAmelCase    				=						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  )				->					Any:


    """simple docstring"""
    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_  )				->					int:


    """simple docstring"""
    UpperCAmelCase    				=						CTRLModel(config=a_  )
    model.to(a_  )
    model.eval()

    model(a_ ,			token_type_ids=a_ ,			head_mask=a_  )
    model(a_ ,			token_type_ids=a_  )
    UpperCAmelCase    				=						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_  )				->					Optional[Any]:


    """simple docstring"""
    UpperCAmelCase    				=						CTRLLMHeadModel(a_  )
    model.to(a_  )
    model.eval()

    UpperCAmelCase    				=						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  )				->					Optional[int]:


    """simple docstring"""
    UpperCAmelCase    				=						self.prepare_config_and_inputs()

    (
        (
        UpperCAmelCase
    )				,   (
        UpperCAmelCase
    )				,   (
        UpperCAmelCase
    )				,   (
        UpperCAmelCase
    )				,   (
        UpperCAmelCase
    )				,   (
        UpperCAmelCase
    )				,   (
        UpperCAmelCase
    )				,   (
        UpperCAmelCase
    )				,   (
        UpperCAmelCase
    )				,   
    )    				=						config_and_inputs

    UpperCAmelCase    				=						{'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_  )				->					Optional[int]:


    """simple docstring"""
    UpperCAmelCase    				=						self.num_labels
    UpperCAmelCase    				=						CTRLForSequenceClassification(a_  )
    model.to(a_  )
    model.eval()
    UpperCAmelCase    				=						ids_tensor([self.batch_size] ,			self.type_sequence_label_size  )
    UpperCAmelCase    				=						model(a_ ,			token_type_ids=a_ ,			labels=a_  )
    self.parent.assertEqual(result.logits.shape ,			(self.batch_size, self.num_labels)  )




@require_torch
class 							lowercase_						(		a			,							a			,							a			,							unittest.TestCase			):

  '''simple docstring'''





  __lowerCAmelCase       :			int     	= (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
  __lowerCAmelCase       :			Union[str, 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       :			Dict     	= True
  __lowerCAmelCase       :			Dict     	= False
  __lowerCAmelCase       :			Optional[int]     	= False
  def     snake_case_      (  self ,			a_ ,			a_ ,			a_ ,			a_ ,			a_  )				->					str:


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


    """simple docstring"""
    UpperCAmelCase    				=						CTRLModelTester(self  )
    UpperCAmelCase    				=						ConfigTester(self ,			config_class=a_ ,			n_embd=3_7  )
  def     snake_case_      (  self  )				->					str:


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


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


    """simple docstring"""
    UpperCAmelCase    				=						self.model_tester.prepare_config_and_inputs()
    self.model_tester.create_and_check_ctrl_model(*a_  )
  def     snake_case_      (  self  )				->					str:


    """simple docstring"""
    UpperCAmelCase    				=						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  )				->					Optional[Any]:


    """simple docstring"""
    pass
  @slow
  def     snake_case_      (  self  )				->					Optional[Any]:


    """simple docstring"""
    for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
      UpperCAmelCase    				=						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  )				->					Optional[Any]:


    """simple docstring"""
    pass




@require_torch
class 							lowercase_						(		unittest.TestCase			):

  '''simple docstring'''





  def     snake_case_      (  self  )				->					List[str]:


    """simple docstring"""
    super().tearDown()
    # clean-up as much as possible GPU memory occupied by PyTorch
    gc.collect()
    torch.cuda.empty_cache()





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


    """simple docstring"""
    UpperCAmelCase    				=						CTRLLMHeadModel.from_pretrained('ctrl'  )
    model.to(a_  )
    UpperCAmelCase    				=						torch.tensor(
        [[1_1_8_5_9, 0, 1_6_1_1, 8]] ,			dtype=torch.long ,			device=a_  )  # Legal the president is
    UpperCAmelCase    				=						[
        1_1_8_5_9,
        0,
        1_6_1_1,
        8,
        5,
        1_5_0,
        2_6_4_4_9,
        2,
        1_9,
        3_4_8,
        4_6_9,
        3,
        2_5_9_5,
        4_8,
        2_0_7_4_0,
        2_4_6_5_3_3,
        2_4_6_5_3_3,
        1_9,
        3_0,
        5,
    ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

    UpperCAmelCase    				=						model.generate(a_ ,			do_sample=a_  )
    self.assertListEqual(output_ids[0].tolist() ,			a_  )





'''simple docstring'''




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


_a					:			str							=    logging.get_logger(__name__)

_a					:			Dict							=    {
    'microsoft/cvt-13': 'https://huggingface.co/microsoft/cvt-13/resolve/main/config.json',
    # See all Cvt models at https://huggingface.co/models?filter=cvt
}



class 							lowercase_						(		a			):

  '''simple docstring'''





  __lowerCAmelCase       :			List[Any]     	= "cvt"
  def __init__(  self ,			a_=3 ,			a_=[7, 3, 3] ,			a_=[4, 2, 2] ,			a_=[2, 1, 1] ,			a_=[6_4, 1_9_2, 3_8_4] ,			a_=[1, 3, 6] ,			a_=[1, 2, 1_0] ,			a_=[4.0, 4.0, 4.0] ,			a_=[0.0, 0.0, 0.0] ,			a_=[0.0, 0.0, 0.0] ,			a_=[0.0, 0.0, 0.1] ,			a_=[True, True, True] ,			a_=[False, False, True] ,			a_=["dw_bn", "dw_bn", "dw_bn"] ,			a_=[3, 3, 3] ,			a_=[1, 1, 1] ,			a_=[2, 2, 2] ,			a_=[1, 1, 1] ,			a_=[1, 1, 1] ,			a_=0.02 ,			a_=1E-12 ,			**a_ ,			)				->					Union[str, Any]:


    """simple docstring"""
    super().__init__(**a_  )
    UpperCAmelCase    				=						num_channels
    UpperCAmelCase    				=						patch_sizes
    UpperCAmelCase    				=						patch_stride
    UpperCAmelCase    				=						patch_padding
    UpperCAmelCase    				=						embed_dim
    UpperCAmelCase    				=						num_heads
    UpperCAmelCase    				=						depth
    UpperCAmelCase    				=						mlp_ratio
    UpperCAmelCase    				=						attention_drop_rate
    UpperCAmelCase    				=						drop_rate
    UpperCAmelCase    				=						drop_path_rate
    UpperCAmelCase    				=						qkv_bias
    UpperCAmelCase    				=						cls_token
    UpperCAmelCase    				=						qkv_projection_method
    UpperCAmelCase    				=						kernel_qkv
    UpperCAmelCase    				=						padding_kv
    UpperCAmelCase    				=						stride_kv
    UpperCAmelCase    				=						padding_q
    UpperCAmelCase    				=						stride_q
    UpperCAmelCase    				=						initializer_range
    UpperCAmelCase    				=						layer_norm_eps


