


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




					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=512							,							_lowerCamelCase=16							,							_lowerCamelCase=2							,							_lowerCamelCase=0.02							,							_lowerCamelCase=3							,							_lowerCamelCase=4							,							_lowerCamelCase=None							,							):
								UpperCAmelCase__  :					Optional[Any]        =     parent
								UpperCAmelCase__  :					Union[str, Any]        =     batch_size
								UpperCAmelCase__  :					str        =     seq_length
								UpperCAmelCase__  :					List[Any]        =     is_training
								UpperCAmelCase__  :					Optional[Any]        =     use_token_type_ids
								UpperCAmelCase__  :					Union[str, Any]        =     use_input_mask
								UpperCAmelCase__  :					Union[str, Any]        =     use_labels
								UpperCAmelCase__  :					str        =     use_mc_token_ids
								UpperCAmelCase__  :					Tuple        =     vocab_size
								UpperCAmelCase__  :					Optional[int]        =     hidden_size
								UpperCAmelCase__  :					Union[str, Any]        =     num_hidden_layers
								UpperCAmelCase__  :					Tuple        =     num_attention_heads
								UpperCAmelCase__  :					Dict        =     intermediate_size
								UpperCAmelCase__  :					Any        =     hidden_act
								UpperCAmelCase__  :					Tuple        =     hidden_dropout_prob
								UpperCAmelCase__  :					List[str]        =     attention_probs_dropout_prob
								UpperCAmelCase__  :					List[Any]        =     max_position_embeddings
								UpperCAmelCase__  :					Union[str, Any]        =     type_vocab_size
								UpperCAmelCase__  :					Optional[int]        =     type_sequence_label_size
								UpperCAmelCase__  :					List[Any]        =     initializer_range
								UpperCAmelCase__  :					Optional[Any]        =     num_labels
								UpperCAmelCase__  :					Optional[int]        =     num_choices
								UpperCAmelCase__  :					List[str]        =     scope
								UpperCAmelCase__  :					Any        =     self.vocab_size - 1




					def 				snake_case__						(      self):
								UpperCAmelCase__  :					str        =     ids_tensor([self.batch_size, self.seq_length]							,							self.vocab_size)

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

								UpperCAmelCase__  :					List[str]        =     None
								if self.use_token_type_ids:
											UpperCAmelCase__  :					int        =     ids_tensor([self.batch_size, self.seq_length]							,							self.type_vocab_size)

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

								UpperCAmelCase__  :					Optional[int]        =     None
								UpperCAmelCase__  :					List[Any]        =     None
								UpperCAmelCase__  :					Any        =     None
								if self.use_labels:
											UpperCAmelCase__  :					List[Any]        =     ids_tensor([self.batch_size]							,							self.type_sequence_label_size)
											UpperCAmelCase__  :					Union[str, Any]        =     ids_tensor([self.batch_size, self.seq_length]							,							self.num_labels)
											UpperCAmelCase__  :					str        =     ids_tensor([self.batch_size]							,							self.num_choices)

								UpperCAmelCase__  :					int        =     self.get_config()

								UpperCAmelCase__  :					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 				snake_case__						(      self):
								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							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							*_lowerCamelCase):
								UpperCAmelCase__  :					Dict        =     CTRLModel(config=_lowerCamelCase)
								model.to(_lowerCamelCase)
								model.eval()

								model(_lowerCamelCase							,							token_type_ids=_lowerCamelCase							,							head_mask=_lowerCamelCase)
								model(_lowerCamelCase							,							token_type_ids=_lowerCamelCase)
								UpperCAmelCase__  :					List[str]        =     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 				snake_case__						(      self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							*_lowerCamelCase):
								UpperCAmelCase__  :					Dict        =     CTRLLMHeadModel(_lowerCamelCase)
								model.to(_lowerCamelCase)
								model.eval()

								UpperCAmelCase__  :					Optional[Any]        =     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 				snake_case__						(      self):
								UpperCAmelCase__  :					Optional[Any]        =     self.prepare_config_and_inputs()

								(
								    (
								    UpperCAmelCase__
								)   ,       (
								    UpperCAmelCase__
								)   ,       (
								    UpperCAmelCase__
								)   ,       (
								    UpperCAmelCase__
								)   ,       (
								    UpperCAmelCase__
								)   ,       (
								    UpperCAmelCase__
								)   ,       (
								    UpperCAmelCase__
								)   ,       (
								    UpperCAmelCase__
								)   ,       (
								    UpperCAmelCase__
								)   ,       
								)  :					Any        =     config_and_inputs

								UpperCAmelCase__  :					str        =     {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """head_mask""": head_mask}

								return config, inputs_dict




					def 				snake_case__						(      self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							*_lowerCamelCase):
								UpperCAmelCase__  :					str        =     self.num_labels
								UpperCAmelCase__  :					int        =     CTRLForSequenceClassification(_lowerCamelCase)
								model.to(_lowerCamelCase)
								model.eval()
								UpperCAmelCase__  :					Tuple        =     ids_tensor([self.batch_size]							,							self.type_sequence_label_size)
								UpperCAmelCase__  :					str        =     model(_lowerCamelCase							,							token_type_ids=_lowerCamelCase							,							labels=_lowerCamelCase)
								self.parent.assertEqual(result.logits.shape							,							(self.batch_size, self.num_labels))


@require_torch
class 		_snake_case					(     a__       ,   a__       ,   a__       ,   unittest.TestCase	):
					lowerCAmelCase      :Tuple   =					(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
					lowerCAmelCase      :Any   =					(CTRLLMHeadModel,) if is_torch_available() else ()
					lowerCAmelCase      :Union[str, Any]   =					(
					    {
					        '''feature-extraction''': CTRLModel,
					        '''text-classification''': CTRLForSequenceClassification,
					        '''text-generation''': CTRLLMHeadModel,
					        '''zero-shot''': CTRLForSequenceClassification,
					    }
					    if is_torch_available()
					    else {}
					)
					lowerCAmelCase      :List[Any]   =					True
					lowerCAmelCase      :Optional[Any]   =					False
					lowerCAmelCase      :Tuple   =					False




					def 				snake_case__						(      self							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase							,							_lowerCamelCase):
								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):
								UpperCAmelCase__  :					Union[str, Any]        =     CTRLModelTester(self)
								UpperCAmelCase__  :					int        =     ConfigTester(self							,							config_class=_lowerCamelCase							,							n_embd=37)




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




					def 				snake_case__						(      self):
								self.config_tester.run_common_tests()




					def 				snake_case__						(      self):
								UpperCAmelCase__  :					Optional[Any]        =     self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_ctrl_model(*_lowerCamelCase)




					def 				snake_case__						(      self):
								UpperCAmelCase__  :					Any        =     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 				snake_case__						(      self):
								pass




					@slow
					def 				snake_case__						(      self):
								for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											UpperCAmelCase__  :					Any        =     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 				snake_case__						(      self):
								pass


@require_torch
class 		_snake_case					(     unittest.TestCase	):




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




					@slow
					def 				snake_case__						(      self):
								UpperCAmelCase__  :					Tuple        =     CTRLLMHeadModel.from_pretrained("""ctrl""")
								model.to(_lowerCamelCase)
								UpperCAmelCase__  :					Any        =     torch.tensor(
								    [[1_1859, 0, 1611, 8]]							,							dtype=torch.long							,							device=_lowerCamelCase)  # Legal the president is
								UpperCAmelCase__  :					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

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



'''simple docstring'''



from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__A	=logging.get_logger(__name__)

__A	={
    'xlm-mlm-en-2048': 'https://huggingface.co/xlm-mlm-en-2048/resolve/main/config.json',
    'xlm-mlm-ende-1024': 'https://huggingface.co/xlm-mlm-ende-1024/resolve/main/config.json',
    'xlm-mlm-enfr-1024': 'https://huggingface.co/xlm-mlm-enfr-1024/resolve/main/config.json',
    'xlm-mlm-enro-1024': 'https://huggingface.co/xlm-mlm-enro-1024/resolve/main/config.json',
    'xlm-mlm-tlm-xnli15-1024': 'https://huggingface.co/xlm-mlm-tlm-xnli15-1024/resolve/main/config.json',
    'xlm-mlm-xnli15-1024': 'https://huggingface.co/xlm-mlm-xnli15-1024/resolve/main/config.json',
    'xlm-clm-enfr-1024': 'https://huggingface.co/xlm-clm-enfr-1024/resolve/main/config.json',
    'xlm-clm-ende-1024': 'https://huggingface.co/xlm-clm-ende-1024/resolve/main/config.json',
    'xlm-mlm-17-1280': 'https://huggingface.co/xlm-mlm-17-1280/resolve/main/config.json',
    'xlm-mlm-100-1280': 'https://huggingface.co/xlm-mlm-100-1280/resolve/main/config.json',
}


class 		_snake_case					(     a__	):
					lowerCAmelCase      :Any   =					'''xlm'''
					lowerCAmelCase      :Any   =					{
					    '''hidden_size''': '''emb_dim''',
					    '''num_attention_heads''': '''n_heads''',
					    '''num_hidden_layers''': '''n_layers''',
					    '''n_words''': '''vocab_size''',  # For backward compatibility
					}




					def __init__(      self							,							_lowerCamelCase=3_0145							,							_lowerCamelCase=2048							,							_lowerCamelCase=12							,							_lowerCamelCase=16							,							_lowerCamelCase=0.1							,							_lowerCamelCase=0.1							,							_lowerCamelCase=True							,							_lowerCamelCase=False							,							_lowerCamelCase=False							,							_lowerCamelCase=False							,							_lowerCamelCase=1							,							_lowerCamelCase=True							,							_lowerCamelCase=512							,							_lowerCamelCase=2048**-0.5							,							_lowerCamelCase=1e-1_2							,							_lowerCamelCase=0.02							,							_lowerCamelCase=0							,							_lowerCamelCase=1							,							_lowerCamelCase=2							,							_lowerCamelCase=3							,							_lowerCamelCase=5							,							_lowerCamelCase=True							,							_lowerCamelCase="first"							,							_lowerCamelCase=True							,							_lowerCamelCase=None							,							_lowerCamelCase=True							,							_lowerCamelCase=0.1							,							_lowerCamelCase=5							,							_lowerCamelCase=5							,							_lowerCamelCase=0							,							_lowerCamelCase=0							,							_lowerCamelCase=2							,							_lowerCamelCase=0							,							**_lowerCamelCase							,							):
								UpperCAmelCase__  :					Tuple        =     vocab_size
								UpperCAmelCase__  :					Tuple        =     emb_dim
								UpperCAmelCase__  :					Optional[Any]        =     n_layers
								UpperCAmelCase__  :					List[str]        =     n_heads
								UpperCAmelCase__  :					List[str]        =     dropout
								UpperCAmelCase__  :					Optional[int]        =     attention_dropout
								UpperCAmelCase__  :					Tuple        =     gelu_activation
								UpperCAmelCase__  :					Optional[Any]        =     sinusoidal_embeddings
								UpperCAmelCase__  :					int        =     causal
								UpperCAmelCase__  :					Union[str, Any]        =     asm
								UpperCAmelCase__  :					Optional[Any]        =     n_langs
								UpperCAmelCase__  :					List[Any]        =     use_lang_emb
								UpperCAmelCase__  :					Union[str, Any]        =     layer_norm_eps
								UpperCAmelCase__  :					List[str]        =     bos_index
								UpperCAmelCase__  :					List[Any]        =     eos_index
								UpperCAmelCase__  :					int        =     pad_index
								UpperCAmelCase__  :					str        =     unk_index
								UpperCAmelCase__  :					Dict        =     mask_index
								UpperCAmelCase__  :					str        =     is_encoder
								UpperCAmelCase__  :					Dict        =     max_position_embeddings
								UpperCAmelCase__  :					Any        =     embed_init_std
								UpperCAmelCase__  :					List[Any]        =     init_std
								UpperCAmelCase__  :					List[str]        =     summary_type
								UpperCAmelCase__  :					Union[str, Any]        =     summary_use_proj
								UpperCAmelCase__  :					Any        =     summary_activation
								UpperCAmelCase__  :					List[str]        =     summary_proj_to_labels
								UpperCAmelCase__  :					Union[str, Any]        =     summary_first_dropout
								UpperCAmelCase__  :					str        =     start_n_top
								UpperCAmelCase__  :					str        =     end_n_top
								UpperCAmelCase__  :					Tuple        =     mask_token_id
								UpperCAmelCase__  :					Union[str, Any]        =     lang_id

								if "n_words" in kwargs:
											UpperCAmelCase__  :					List[str]        =     kwargs["""n_words"""]

								super().__init__(pad_token_id=_lowerCamelCase							,							bos_token_id=_lowerCamelCase							,							**_lowerCamelCase)


class 		_snake_case					(     a__	):




					@property
					def 				snake_case__						(      self):
								if self.task == "multiple-choice":
											UpperCAmelCase__  :					Any        =     {0: """batch""", 1: """choice""", 2: """sequence"""}
								else:
											UpperCAmelCase__  :					List[Any]        =     {0: """batch""", 1: """sequence"""}
								return OrderedDict(
								    [
								        ("""input_ids""", dynamic_axis),
								        ("""attention_mask""", dynamic_axis),
								        ("""token_type_ids""", dynamic_axis),
								    ])