
'''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    ,   lowerCAmelCase_    ,   lowerCAmelCase_=14    ,   lowerCAmelCase_=7    ,   lowerCAmelCase_=True    ,   lowerCAmelCase_=True    ,   lowerCAmelCase_=True    ,   lowerCAmelCase_=True    ,   lowerCAmelCase_=True    ,   lowerCAmelCase_=99    ,   lowerCAmelCase_=32    ,   lowerCAmelCase_=5    ,   lowerCAmelCase_=4    ,   lowerCAmelCase_=37    ,   lowerCAmelCase_="gelu"    ,   lowerCAmelCase_=0.1    ,   lowerCAmelCase_=0.1    ,   lowerCAmelCase_=5_12    ,   lowerCAmelCase_=16    ,   lowerCAmelCase_=2    ,   lowerCAmelCase_=0.02    ,   lowerCAmelCase_=3    ,   lowerCAmelCase_=4    ,   lowerCAmelCase_=None    ,   ):





								"""simple docstring"""






								_snake_case   						=		parent
								_snake_case   						=		batch_size
								_snake_case   						=		seq_length
								_snake_case   						=		is_training
								_snake_case   						=		use_token_type_ids
								_snake_case   						=		use_input_mask
								_snake_case   						=		use_labels
								_snake_case   						=		use_mc_token_ids
								_snake_case   						=		vocab_size
								_snake_case   						=		hidden_size
								_snake_case   						=		num_hidden_layers
								_snake_case   						=		num_attention_heads
								_snake_case   						=		intermediate_size
								_snake_case   						=		hidden_act
								_snake_case   						=		hidden_dropout_prob
								_snake_case   						=		attention_probs_dropout_prob
								_snake_case   						=		max_position_embeddings
								_snake_case   						=		type_vocab_size
								_snake_case   						=		type_sequence_label_size
								_snake_case   						=		initializer_range
								_snake_case   						=		num_labels
								_snake_case   						=		num_choices
								_snake_case   						=		scope
								_snake_case   						=		self.vocab_size - 1


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		ids_tensor([self.batch_size, self.seq_length]    ,   self.vocab_size  )

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

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

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

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

								_snake_case   						=		self.get_config()

								_snake_case   						=		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  lowerCamelCase				(       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  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   *lowerCAmelCase_  ):





								"""simple docstring"""






								_snake_case   						=		CTRLModel(config=lowerCAmelCase_  )
								model.to(lowerCAmelCase_  )
								model.eval()

								model(lowerCAmelCase_    ,   token_type_ids=lowerCAmelCase_    ,   head_mask=lowerCAmelCase_  )
								model(lowerCAmelCase_    ,   token_type_ids=lowerCAmelCase_  )
								_snake_case   						=		model(lowerCAmelCase_  )
								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  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   *lowerCAmelCase_  ):





								"""simple docstring"""






								_snake_case   						=		CTRLLMHeadModel(lowerCAmelCase_  )
								model.to(lowerCAmelCase_  )
								model.eval()

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


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.prepare_config_and_inputs()

								(
								    (
								    _snake_case
								)						, (
								    _snake_case
								)						, (
								    _snake_case
								)						, (
								    _snake_case
								)						, (
								    _snake_case
								)						, (
								    _snake_case
								)						, (
								    _snake_case
								)						, (
								    _snake_case
								)						, (
								    _snake_case
								)						, 
								)   						=		config_and_inputs

								_snake_case   						=		{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'head_mask': head_mask}

								return config, inputs_dict




							def  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   *lowerCAmelCase_  ):





								"""simple docstring"""






								_snake_case   						=		self.num_labels
								_snake_case   						=		CTRLForSequenceClassification(lowerCAmelCase_  )
								model.to(lowerCAmelCase_  )
								model.eval()
								_snake_case   						=		ids_tensor([self.batch_size]    ,   self.type_sequence_label_size  )
								_snake_case   						=		model(lowerCAmelCase_    ,   token_type_ids=lowerCAmelCase_    ,   labels=lowerCAmelCase_  )
								self.parent.assertEqual(result.logits.shape    ,   (self.batch_size, self.num_labels)  )


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


							def  lowerCamelCase				(       self    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_    ,   lowerCAmelCase_  ):





								"""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  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		CTRLModelTester(self  )
								_snake_case   						=		ConfigTester(self    ,   config_class=lowerCAmelCase_    ,   n_embd=37  )


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






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


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								self.config_tester.run_common_tests()


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_ctrl_model(*lowerCAmelCase_  )


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_lm_head_model(*lowerCAmelCase_  )


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





								"""simple docstring"""






								pass


							@slow
							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									_snake_case   						=		CTRLModel.from_pretrained(lowerCAmelCase_  )
									self.assertIsNotNone(lowerCAmelCase_  )




							@unittest.skip('The model doesn\'t support left padding'  )  # and it's not used enough to be worth fixing :)
							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								pass


@require_torch
class 						__UpperCAmelCase      (					unittest.TestCase						):


							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






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




							@slow
							def  lowerCamelCase				(       self  ):





								"""simple docstring"""






								_snake_case   						=		CTRLLMHeadModel.from_pretrained('ctrl'  )
								model.to(lowerCAmelCase_  )
								_snake_case   						=		torch.tensor(
								    [[1_18_59, 0, 16_11, 8]]    ,   dtype=torch.long    ,   device=lowerCAmelCase_  )  # Legal the president is
								_snake_case   						=		[
								    1_18_59,
								    0,
								    16_11,
								    8,
								    5,
								    1_50,
								    2_64_49,
								    2,
								    19,
								    3_48,
								    4_69,
								    3,
								    25_95,
								    48,
								    2_07_40,
								    24_65_33,
								    24_65_33,
								    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   						=		model.generate(lowerCAmelCase_    ,   do_sample=lowerCAmelCase_  )
								self.assertListEqual(output_ids[0].tolist()    ,   lowerCAmelCase_  )



'''simple docstring'''







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_tf_available, is_torch_available


lowercase    :							Tuple      =					{
    "configuration_xlm": ["XLM_PRETRAINED_CONFIG_ARCHIVE_MAP", "XLMConfig", "XLMOnnxConfig"],
    "tokenization_xlm": ["XLMTokenizer"],
}

try:
					if not is_torch_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowercase    :							int      =					[
					    "XLM_PRETRAINED_MODEL_ARCHIVE_LIST",
					    "XLMForMultipleChoice",
					    "XLMForQuestionAnswering",
					    "XLMForQuestionAnsweringSimple",
					    "XLMForSequenceClassification",
					    "XLMForTokenClassification",
					    "XLMModel",
					    "XLMPreTrainedModel",
					    "XLMWithLMHeadModel",
					]

try:
					if not is_tf_available():
										raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
					pass
else:
					lowercase    :							Tuple      =					[
					    "TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST",
					    "TFXLMForMultipleChoice",
					    "TFXLMForQuestionAnsweringSimple",
					    "TFXLMForSequenceClassification",
					    "TFXLMForTokenClassification",
					    "TFXLMMainLayer",
					    "TFXLMModel",
					    "TFXLMPreTrainedModel",
					    "TFXLMWithLMHeadModel",
					]


if TYPE_CHECKING:
					from .configuration_xlm import XLM_PRETRAINED_CONFIG_ARCHIVE_MAP, XLMConfig, XLMOnnxConfig
					from .tokenization_xlm import XLMTokenizer

					try:
										if not is_torch_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_xlm import (
										    XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
										    XLMForMultipleChoice,
										    XLMForQuestionAnswering,
										    XLMForQuestionAnsweringSimple,
										    XLMForSequenceClassification,
										    XLMForTokenClassification,
										    XLMModel,
										    XLMPreTrainedModel,
										    XLMWithLMHeadModel,
										)

					try:
										if not is_tf_available():
															raise OptionalDependencyNotAvailable()
					except OptionalDependencyNotAvailable:
										pass
					else:
										from .modeling_tf_xlm import (
										    TF_XLM_PRETRAINED_MODEL_ARCHIVE_LIST,
										    TFXLMForMultipleChoice,
										    TFXLMForQuestionAnsweringSimple,
										    TFXLMForSequenceClassification,
										    TFXLMForTokenClassification,
										    TFXLMMainLayer,
										    TFXLMModel,
										    TFXLMPreTrainedModel,
										    TFXLMWithLMHeadModel,
										)

else:
					import sys

					lowercase    :							Union[str, Any]      =					_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)

