


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




	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						,				):

						'''simple docstring'''





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




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Dict					    =  ids_tensor([self.batch_size, self.seq_length]						,				self.vocab_size							)

						__a			:       Union[str, Any]					    =  None
						if self.use_input_mask:
											__a			:       Any					    =  random_attention_mask([self.batch_size, self.seq_length]							)

						__a			:       Tuple					    =  None
						if self.use_token_type_ids:
											__a			:       List[Any]					    =  ids_tensor([self.batch_size, self.seq_length]						,				self.type_vocab_size							)

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

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

						__a			:       Optional[int]					    =  self.get_config()

						__a			:       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       __UpperCAmelCase							(   self							):

						'''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       __UpperCAmelCase							(   self						,				__a						,				__a						,				__a						,				__a						,				__a						,				*__a							):

						'''simple docstring'''





						__a			:       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							)
						__a			:       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       __UpperCAmelCase							(   self						,				__a						,				__a						,				__a						,				__a						,				__a						,				*__a							):

						'''simple docstring'''





						__a			:       Dict					    =  CTRLLMHeadModel(__a							)
						model.to(__a							)
						model.eval()

						__a			:       List[str]					    =  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       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Union[str, Any]					    =  self.prepare_config_and_inputs()

						(
						    (
						    __a
						)    ,				(
						    __a
						)    ,				(
						    __a
						)    ,				(
						    __a
						)    ,				(
						    __a
						)    ,				(
						    __a
						)    ,				(
						    __a
						)    ,				(
						    __a
						)    ,				(
						    __a
						)    ,				
						)			:       Any					    =  config_and_inputs

						__a			:       List[Any]					    =  {'input_ids': input_ids, 'token_type_ids': token_type_ids, 'head_mask': head_mask}

						return config, inputs_dict





	def       __UpperCAmelCase							(   self						,				__a						,				__a						,				__a						,				__a						,				*__a							):

						'''simple docstring'''





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





@require_torch
class 							__UpperCamelCase				(		lowerCAmelCase_		,      lowerCAmelCase_		,      lowerCAmelCase_		,      unittest.TestCase					):
	A_				=    (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
	A_				=    (CTRLLMHeadModel,) if is_torch_available() else ()
	A_				=    (
	    {
	        "feature-extraction": CTRLModel,
	        "text-classification": CTRLForSequenceClassification,
	        "text-generation": CTRLLMHeadModel,
	        "zero-shot": CTRLForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	A_				=    True
	A_				=    False
	A_				=    False




	def       __UpperCAmelCase							(   self						,				__a						,				__a						,				__a						,				__a						,				__a							):

						'''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       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       Optional[int]					    =  CTRLModelTester(self							)
						__a			:       str					    =  ConfigTester(self						,				config_class=__a						,				n_embd=37							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





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




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						self.config_tester.run_common_tests()




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       str					    =  self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_ctrl_model(*__a							)




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       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       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						pass




	@slow
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__a			:       Dict					    =  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       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						pass





@require_torch
class 							__UpperCamelCase				(		unittest.TestCase					):




	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





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





	@slow
	def       __UpperCAmelCase							(   self							):

						'''simple docstring'''





						__a			:       int					    =  CTRLLMHeadModel.from_pretrained('ctrl'							)
						model.to(__a							)
						__a			:       Union[str, Any]					    =  torch.tensor(
						    [[1_1859, 0, 1611, 8]]						,				dtype=torch.long						,				device=__a							)  # Legal the president is
						__a			:       List[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

						__a			:       List[str]					    =  model.generate(__a						,				do_sample=__a							)
						self.assertListEqual(output_ids[0].tolist()						,				__a							)







'''simple docstring'''



import importlib
import os
import sys


# This is required to make the module import works (when the python process is running from the root of the repo)
sys.path.append('.')






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   List[Any]   ):
					__a			:       Any					    =  test_file.split(os.path.sep   )
					if components[0:2] != ["tests", "models"]:
										raise ValueError(
										    '`test_file` should start with `tests/models/` (with `/` being the OS specific path separator). Got '
										    F"""{test_file} instead."""   )
					__a			:       Tuple					    =  components[-1]
					if not test_fn.endswith('py'   ):
										raise ValueError(F"""`test_file` should be a python file. Got {test_fn} instead."""   )
					if not test_fn.startswith('test_modeling_'   ):
										raise ValueError(
										    F"""`test_file` should point to a file name of the form `test_modeling_*.py`. Got {test_fn} instead."""   )

					__a			:       List[str]					    =  components[:-1] + [test_fn.replace('.py'						,      ''   )]
					__a			:       Optional[Any]					    =  '.'.join(_SCREAMING_SNAKE_CASE   )

					return test_module_path






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   Tuple   ):
					__a			:       List[str]					    =  get_module_path(_SCREAMING_SNAKE_CASE   )
					__a			:       Dict					    =  importlib.import_module(_SCREAMING_SNAKE_CASE   )

					return test_module






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   Tuple   ):
					__a			:       List[str]					    =  []
					__a			:       List[str]					    =  get_test_module(_SCREAMING_SNAKE_CASE   )
					for attr in dir(_SCREAMING_SNAKE_CASE   ):
										if attr.endswith('ModelTester'   ):
															tester_classes.append(getattr(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE   )   )

    # sort with class names
					return sorted(_SCREAMING_SNAKE_CASE						,      key=lambda _SCREAMING_SNAKE_CASE   : x.__name__   )






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   Tuple   ):
					__a			:       Any					    =  []
					__a			:       str					    =  get_test_module(_SCREAMING_SNAKE_CASE   )
					for attr in dir(_SCREAMING_SNAKE_CASE   ):
										__a			:       int					    =  getattr(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE   )
										# (TF/Flax)ModelTesterMixin is also an attribute in specific model test module. Let's exclude them by checking
										# `all_model_classes` is not empty (which also excludes other special classes).
										__a			:       Optional[Any]					    =  getattr(_SCREAMING_SNAKE_CASE						,      'all_model_classes'						,      []   )
										if len(_SCREAMING_SNAKE_CASE   ) > 0:
															test_classes.append(_SCREAMING_SNAKE_CASE   )

    # sort with class names
					return sorted(_SCREAMING_SNAKE_CASE						,      key=lambda _SCREAMING_SNAKE_CASE   : x.__name__   )






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   int   ):
					__a			:       str					    =  get_test_classes(_SCREAMING_SNAKE_CASE   )
					__a			:       Any					    =  set()
					for test_class in test_classes:
										model_classes.update(test_class.all_model_classes   )

					# sort with class names
					return sorted(_SCREAMING_SNAKE_CASE						,      key=lambda _SCREAMING_SNAKE_CASE   : x.__name__   )






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   Optional[Any]   ):
					__a			:       Tuple					    =  test_class()
					if hasattr(_SCREAMING_SNAKE_CASE						,      'setUp'   ):
										test.setUp()

					__a			:       List[Any]					    =  None
					if hasattr(_SCREAMING_SNAKE_CASE						,      'model_tester'   ):
										# `(TF/Flax)ModelTesterMixin` has this attribute default to `None`. Let's skip this case.
										if test.model_tester is not None:
															__a			:       List[str]					    =  test.model_tester.__class__

					return model_tester






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   int						,      _SCREAMING_SNAKE_CASE      :   Optional[Any]   ):
					__a			:       str					    =  get_test_classes(_SCREAMING_SNAKE_CASE   )

					__a			:       int					    =  []
					for test_class in test_classes:
										if model_class in test_class.all_model_classes:
															target_test_classes.append(_SCREAMING_SNAKE_CASE   )

    # sort with class names
					return sorted(_SCREAMING_SNAKE_CASE						,      key=lambda _SCREAMING_SNAKE_CASE   : x.__name__   )






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   Tuple						,      _SCREAMING_SNAKE_CASE      :   List[str]   ):
					__a			:       List[Any]					    =  get_test_classes_for_model(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE   )

					__a			:       Any					    =  []
					for test_class in test_classes:
										__a			:       Any					    =  get_model_tester_from_test_class(_SCREAMING_SNAKE_CASE   )
										if tester_class is not None:
															tester_classes.append(_SCREAMING_SNAKE_CASE   )

    # sort with class names
					return sorted(_SCREAMING_SNAKE_CASE						,      key=lambda _SCREAMING_SNAKE_CASE   : x.__name__   )






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   Optional[int]   ):
					__a			:       str					    =  get_test_classes(_SCREAMING_SNAKE_CASE   )
					__a			:       int					    =  {test_class: get_model_tester_from_test_class(_SCREAMING_SNAKE_CASE   ) for test_class in test_classes}
					return test_tester_mapping






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   List[str]   ):
					__a			:       Optional[Any]					    =  get_model_classes(_SCREAMING_SNAKE_CASE   )
					__a			:       Optional[int]					    =  {
					    model_class: get_test_classes_for_model(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE   ) for model_class in model_classes
					}
					return model_test_mapping






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   List[str]   ):
					__a			:       Optional[Any]					    =  get_model_classes(_SCREAMING_SNAKE_CASE   )
					__a			:       str					    =  {
					    model_class: get_tester_classes_for_model(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE   ) for model_class in model_classes
					}
					return model_to_tester_mapping






def   lowerCamelCase							(_SCREAMING_SNAKE_CASE      :   str   ):
					if isinstance(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE   ):
										return o
					elif isinstance(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE   ):
										return o.__name__
					elif isinstance(_SCREAMING_SNAKE_CASE						,      (list, tuple)   ):
										return [to_json(_SCREAMING_SNAKE_CASE   ) for x in o]
					elif isinstance(_SCREAMING_SNAKE_CASE						,      _SCREAMING_SNAKE_CASE   ):
										return {to_json(_SCREAMING_SNAKE_CASE   ): to_json(_SCREAMING_SNAKE_CASE   ) for k, v in o.items()}
					else:
										return o



