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


 def __init__(    self  : Dict			,							a__  : List[str]			,							a__  : Any=14			,							a__  : List[Any]=7			,							a__  : List[str]=True			,							a__  : Tuple=True			,							a__  : str=True			,							a__  : Optional[Any]=True			,							a__  : Tuple=True			,							a__  : Any=99			,							a__  : int=32			,							a__  : Optional[Any]=5			,							a__  : Any=4			,							a__  : List[Any]=37			,							a__  : Tuple="gelu"			,							a__  : Dict=0.1			,							a__  : int=0.1			,							a__  : Any=512			,							a__  : str=16			,							a__  : Union[str, Any]=2			,							a__  : Optional[int]=0.02			,							a__  : Tuple=3			,							a__  : Union[str, Any]=4			,							a__  : Any=None			,							):
  '''simple docstring'''

  lowerCAmelCase__     :   Optional[Any]     		=	parent
  lowerCAmelCase__     :   List[str]     		=	batch_size
  lowerCAmelCase__     :   Dict     		=	seq_length
  lowerCAmelCase__     :   List[Any]     		=	is_training
  lowerCAmelCase__     :   Optional[Any]     		=	use_token_type_ids
  lowerCAmelCase__     :   Any     		=	use_input_mask
  lowerCAmelCase__     :   Optional[int]     		=	use_labels
  lowerCAmelCase__     :   int     		=	use_mc_token_ids
  lowerCAmelCase__     :   Dict     		=	vocab_size
  lowerCAmelCase__     :   str     		=	hidden_size
  lowerCAmelCase__     :   List[str]     		=	num_hidden_layers
  lowerCAmelCase__     :   Any     		=	num_attention_heads
  lowerCAmelCase__     :   str     		=	intermediate_size
  lowerCAmelCase__     :   List[str]     		=	hidden_act
  lowerCAmelCase__     :   str     		=	hidden_dropout_prob
  lowerCAmelCase__     :   Optional[Any]     		=	attention_probs_dropout_prob
  lowerCAmelCase__     :   Any     		=	max_position_embeddings
  lowerCAmelCase__     :   str     		=	type_vocab_size
  lowerCAmelCase__     :   Optional[int]     		=	type_sequence_label_size
  lowerCAmelCase__     :   List[Any]     		=	initializer_range
  lowerCAmelCase__     :   Optional[int]     		=	num_labels
  lowerCAmelCase__     :   str     		=	num_choices
  lowerCAmelCase__     :   str     		=	scope
  lowerCAmelCase__     :   Optional[int]     		=	self.vocab_size - 1


 def    _A	(    self  : List[str]    ):
  '''simple docstring'''

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

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

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

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

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

  lowerCAmelCase__     :   Dict     		=	self.get_config()

  lowerCAmelCase__     :   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    _A	(    self  : Union[str, 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    _A	(    self  : Tuple			,							a__  : Optional[int]			,							a__  : Any			,							a__  : int			,							a__  : Union[str, Any]			,							a__  : Dict			,							*a__  : List[str]    ):
  '''simple docstring'''

  lowerCAmelCase__     :   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__    )
  lowerCAmelCase__     :   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    _A	(    self  : List[str]			,							a__  : Tuple			,							a__  : Any			,							a__  : Any			,							a__  : Optional[int]			,							a__  : str			,							*a__  : str    ):
  '''simple docstring'''

  lowerCAmelCase__     :   List[str]     		=	CTRLLMHeadModel(a__    )
  model.to(a__    )
  model.eval()

  lowerCAmelCase__     :   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    _A	(    self  : List[Any]    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Any     		=	self.prepare_config_and_inputs()

  (
      (
      lowerCAmelCase__
  )		,     (
      lowerCAmelCase__
  )		,     (
      lowerCAmelCase__
  )		,     (
      lowerCAmelCase__
  )		,     (
      lowerCAmelCase__
  )		,     (
      lowerCAmelCase__
  )		,     (
      lowerCAmelCase__
  )		,     (
      lowerCAmelCase__
  )		,     (
      lowerCAmelCase__
  )		,     
  )     :   str     		=	config_and_inputs

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

  return config, inputs_dict


 def    _A	(    self  : Optional[int]			,							a__  : str			,							a__  : List[Any]			,							a__  : Dict			,							a__  : Any			,							*a__  : List[Any]    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Dict     		=	self.num_labels
  lowerCAmelCase__     :   int     		=	CTRLForSequenceClassification(a__    )
  model.to(a__    )
  model.eval()
  lowerCAmelCase__     :   int     		=	ids_tensor([self.batch_size]			,							self.type_sequence_label_size    )
  lowerCAmelCase__     :   Any     		=	model(a__			,							token_type_ids=a__			,							labels=a__    )
  self.parent.assertEqual(result.logits.shape			,							(self.batch_size, self.num_labels)    )




@require_torch
class    lowerCAmelCase       (     UpperCamelCase_    ,  UpperCamelCase_    ,  UpperCamelCase_    ,  unittest.TestCase  ):
 A_				:				str	=			(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
 A_				:				Optional[Any]	=			(CTRLLMHeadModel,) if is_torch_available() else ()
 A_				:				int	=			(
     {
         """feature-extraction""": CTRLModel,
         """text-classification""": CTRLForSequenceClassification,
         """text-generation""": CTRLLMHeadModel,
         """zero-shot""": CTRLForSequenceClassification,
     }
     if is_torch_available()
     else {}
 )
 A_				:				str	=			True
 A_				:				Optional[int]	=			False
 A_				:				int	=			False


 def    _A	(    self  : Tuple			,							a__  : Any			,							a__  : Union[str, Any]			,							a__  : str			,							a__  : List[str]			,							a__  : Optional[Any]    ):
  '''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    _A	(    self  : Dict    ):
  '''simple docstring'''

  lowerCAmelCase__     :   str     		=	CTRLModelTester(self    )
  lowerCAmelCase__     :   str     		=	ConfigTester(self			,							config_class=a__			,							n_embd=37    )


 def    _A	(    self  : List[str]    ):
  '''simple docstring'''

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


 def    _A	(    self  : int    ):
  '''simple docstring'''

  self.config_tester.run_common_tests()


 def    _A	(    self  : Union[str, Any]    ):
  '''simple docstring'''

  lowerCAmelCase__     :   int     		=	self.model_tester.prepare_config_and_inputs()
  self.model_tester.create_and_check_ctrl_model(*a__    )


 def    _A	(    self  : Dict    ):
  '''simple docstring'''

  lowerCAmelCase__     :   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    _A	(    self  : List[Any]    ):
  '''simple docstring'''

  pass


 @slow
 def    _A	(    self  : Tuple    ):
  '''simple docstring'''

  for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
   lowerCAmelCase__     :   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    _A	(    self  : Optional[int]    ):
  '''simple docstring'''

  pass




@require_torch
class    lowerCAmelCase       (     unittest.TestCase  ):


 def    _A	(    self  : Optional[int]    ):
  '''simple docstring'''

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


 @slow
 def    _A	(    self  : Union[str, Any]    ):
  '''simple docstring'''

  lowerCAmelCase__     :   Dict     		=	CTRLLMHeadModel.from_pretrained("ctrl"    )
  model.to(a__    )
  lowerCAmelCase__     :   Union[str, Any]     		=	torch.tensor(
      [[1_1859, 0, 1611, 8]]			,							dtype=torch.long			,							device=a__    )  # Legal the president is
  lowerCAmelCase__     :   Optional[Any]     		=	[
      1_1859,
      0,
      1611,
      8,
      5,
      150,
      2_6449,
      2,
      19,
      348,
      469,
      3,
      2595,
      48,
      2_0740,
      24_6533,
      24_6533,
      19,
      30,
      5,
  ]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

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







'''simple docstring'''

import warnings
from functools import wraps
from typing import Callable


def        UpperCAmelCase_ (  lowerCamelCase_			):







 """simple docstring"""


 @wraps(lowerCamelCase_			)
 def _inner_fn(*lowerCamelCase_      ,			**lowerCamelCase_			):
  warnings.warn(
      (f'''\'{fn.__name__}\' is experimental and might be subject to breaking changes in the future.''')      ,			lowerCamelCase_      ,			)
  return fn(*lowerCamelCase_      ,			**lowerCamelCase_			)

 return _inner_fn





