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







		"""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		,						) ->     Tuple:
								_lowerCAmelCase										=parent
								_lowerCAmelCase										=batch_size
								_lowerCAmelCase										=seq_length
								_lowerCAmelCase										=is_training
								_lowerCAmelCase										=use_token_type_ids
								_lowerCAmelCase										=use_input_mask
								_lowerCAmelCase										=use_labels
								_lowerCAmelCase										=use_mc_token_ids
								_lowerCAmelCase										=vocab_size
								_lowerCAmelCase										=hidden_size
								_lowerCAmelCase										=num_hidden_layers
								_lowerCAmelCase										=num_attention_heads
								_lowerCAmelCase										=intermediate_size
								_lowerCAmelCase										=hidden_act
								_lowerCAmelCase										=hidden_dropout_prob
								_lowerCAmelCase										=attention_probs_dropout_prob
								_lowerCAmelCase										=max_position_embeddings
								_lowerCAmelCase										=type_vocab_size
								_lowerCAmelCase										=type_sequence_label_size
								_lowerCAmelCase										=initializer_range
								_lowerCAmelCase										=num_labels
								_lowerCAmelCase										=num_choices
								_lowerCAmelCase										=scope
								_lowerCAmelCase										=self.vocab_size - 1






		def 		UpperCamelCase__       (   self				) ->     Dict:
								_lowerCAmelCase										=ids_tensor([self.batch_size, self.seq_length]		,						self.vocab_size				)

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

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

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

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

								_lowerCAmelCase										=self.get_config()

								_lowerCAmelCase										=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				) ->     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 		UpperCamelCase__       (   self		,						__A		,						__A		,						__A		,						__A		,						__A		,						*__A				) ->     Union[str, Any]:
								_lowerCAmelCase										=CTRLModel(config=lowercase_				)
								model.to(lowercase_				)
								model.eval()

								model(lowercase_		,						token_type_ids=lowercase_		,						head_mask=lowercase_				)
								model(lowercase_		,						token_type_ids=lowercase_				)
								_lowerCAmelCase										=model(lowercase_				)
								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				) ->     Tuple:
								_lowerCAmelCase										=CTRLLMHeadModel(lowercase_				)
								model.to(lowercase_				)
								model.eval()

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






		def 		UpperCamelCase__       (   self				) ->     Optional[int]:
								_lowerCAmelCase										=self.prepare_config_and_inputs()

								(
								    _lowerCAmelCase 
								)										=config_and_inputs

								_lowerCAmelCase										={"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				) ->     Union[str, Any]:
								_lowerCAmelCase										=self.num_labels
								_lowerCAmelCase										=CTRLForSequenceClassification(lowercase_				)
								model.to(lowercase_				)
								model.eval()
								_lowerCAmelCase										=ids_tensor([self.batch_size]		,						self.type_sequence_label_size				)
								_lowerCAmelCase										=model(lowercase_		,						token_type_ids=lowercase_		,						labels=lowercase_				)
								self.parent.assertEqual(result.logits.shape		,						(self.batch_size, self.num_labels)				)

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







		"""simple docstring"""
		lowercase   :      Union[str, Any]       =							(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
		lowercase   :      List[str]       =							(CTRLLMHeadModel,) if is_torch_available() else ()
		lowercase   :      Optional[int]       =							(
		    {
		        'feature-extraction': CTRLModel,
		        'text-classification': CTRLForSequenceClassification,
		        'text-generation': CTRLLMHeadModel,
		        'zero-shot': CTRLForSequenceClassification,
		    }
		    if is_torch_available()
		    else {}
		)
		lowercase   :      str       =							True
		lowercase   :      Any       =							False
		lowercase   :      List[str]       =							False






		def 		UpperCamelCase__       (   self		,						__A		,						__A		,						__A		,						__A		,						__A				) ->     Optional[Any]:
								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				) ->     Optional[Any]:
								_lowerCAmelCase										=CTRLModelTester(self				)
								_lowerCAmelCase										=ConfigTester(self		,						config_class=lowercase_		,						n_embd=37				)






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






		def 		UpperCamelCase__       (   self				) ->     Optional[Any]:
								self.config_tester.run_common_tests()






		def 		UpperCamelCase__       (   self				) ->     int:
								_lowerCAmelCase										=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_ctrl_model(*lowercase_				)






		def 		UpperCamelCase__       (   self				) ->     Optional[int]:
								_lowerCAmelCase										=self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_lm_head_model(*lowercase_				)






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






		@slow
		def 		UpperCamelCase__       (   self				) ->     int:
								for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														_lowerCAmelCase										=CTRLModel.from_pretrained(lowercase_				)
														self.assertIsNotNone(lowercase_				)






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

@require_torch
class   SCREAMING_SNAKE_CASE			(						unittest.TestCase):







		"""simple docstring"""






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






		@slow
		def 		UpperCamelCase__       (   self				) ->     str:
								_lowerCAmelCase										=CTRLLMHeadModel.from_pretrained('ctrl'				)
								model.to(lowercase_				)
								_lowerCAmelCase										=torch.tensor(
								    [[1_1859, 0, 1611, 8]]		,						dtype=torch.long		,						device=lowercase_				)  # Legal the president is
								_lowerCAmelCase										=[
								    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										=model.generate(lowercase_		,						do_sample=lowercase_				)
								self.assertListEqual(output_ids[0].tolist()		,						lowercase_				)






'''simple docstring'''

from . import __version__

# Backward compatibility imports, to make sure all those objects can be found in file_utils
from .utils import (
    CLOUDFRONT_DISTRIB_PREFIX,
    CONFIG_NAME,
    DISABLE_TELEMETRY,
    DUMMY_INPUTS,
    DUMMY_MASK,
    ENV_VARS_TRUE_AND_AUTO_VALUES,
    ENV_VARS_TRUE_VALUES,
    FEATURE_EXTRACTOR_NAME,
    FLAX_WEIGHTS_NAME,
    HF_MODULES_CACHE,
    HUGGINGFACE_CO_PREFIX,
    HUGGINGFACE_CO_RESOLVE_ENDPOINT,
    MODEL_CARD_NAME,
    MULTIPLE_CHOICE_DUMMY_INPUTS,
    PYTORCH_PRETRAINED_BERT_CACHE,
    PYTORCH_TRANSFORMERS_CACHE,
    S3_BUCKET_PREFIX,
    SENTENCEPIECE_UNDERLINE,
    SPIECE_UNDERLINE,
    TF2_WEIGHTS_NAME,
    TF_WEIGHTS_NAME,
    TORCH_FX_REQUIRED_VERSION,
    TRANSFORMERS_CACHE,
    TRANSFORMERS_DYNAMIC_MODULE_NAME,
    USE_JAX,
    USE_TF,
    USE_TORCH,
    WEIGHTS_INDEX_NAME,
    WEIGHTS_NAME,
    ContextManagers,
    DummyObject,
    EntryNotFoundError,
    ExplicitEnum,
    ModelOutput,
    PaddingStrategy,
    PushToHubMixin,
    RepositoryNotFoundError,
    RevisionNotFoundError,
    TensorType,
    _LazyModule,
    add_code_sample_docstrings,
    add_end_docstrings,
    add_start_docstrings,
    add_start_docstrings_to_model_forward,
    cached_property,
    copy_func,
    default_cache_path,
    define_sagemaker_information,
    get_cached_models,
    get_file_from_repo,
    get_full_repo_name,
    get_torch_version,
    has_file,
    http_user_agent,
    is_apex_available,
    is_bsa_available,
    is_coloredlogs_available,
    is_datasets_available,
    is_detectrona_available,
    is_faiss_available,
    is_flax_available,
    is_ftfy_available,
    is_in_notebook,
    is_ipex_available,
    is_librosa_available,
    is_offline_mode,
    is_onnx_available,
    is_pandas_available,
    is_phonemizer_available,
    is_protobuf_available,
    is_psutil_available,
    is_pyanvml_available,
    is_pyctcdecode_available,
    is_pytesseract_available,
    is_pytorch_quantization_available,
    is_rjieba_available,
    is_sagemaker_dp_enabled,
    is_sagemaker_mp_enabled,
    is_scipy_available,
    is_sentencepiece_available,
    is_seqio_available,
    is_sklearn_available,
    is_soundfile_availble,
    is_spacy_available,
    is_speech_available,
    is_tensor,
    is_tensorflow_probability_available,
    is_tfaonnx_available,
    is_tf_available,
    is_timm_available,
    is_tokenizers_available,
    is_torch_available,
    is_torch_bfaa_available,
    is_torch_cuda_available,
    is_torch_fx_available,
    is_torch_fx_proxy,
    is_torch_mps_available,
    is_torch_tfaa_available,
    is_torch_tpu_available,
    is_torchaudio_available,
    is_training_run_on_sagemaker,
    is_vision_available,
    replace_return_docstrings,
    requires_backends,
    to_numpy,
    to_py_obj,
    torch_only_method,
)





