



'''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    :  Tuple	,    __snake_case    :  int	,    __snake_case    :  List[str]=14	,    __snake_case    :  List[Any]=7	,    __snake_case    :  Optional[Any]=True	,    __snake_case    :  List[Any]=True	,    __snake_case    :  Optional[int]=True	,    __snake_case    :  Optional[Any]=True	,    __snake_case    :  List[str]=True	,    __snake_case    :  Dict=99	,    __snake_case    :  Dict=32	,    __snake_case    :  Optional[Any]=5	,    __snake_case    :  Dict=4	,    __snake_case    :  Dict=37	,    __snake_case    :  str="gelu"	,    __snake_case    :  Tuple=0.1	,    __snake_case    :  Optional[Any]=0.1	,    __snake_case    :  Optional[Any]=512	,    __snake_case    :  Optional[Any]=16	,    __snake_case    :  str=2	,    __snake_case    :  Optional[int]=0.02	,    __snake_case    :  List[Any]=3	,    __snake_case    :  Tuple=4	,    __snake_case    :  Optional[int]=None	,    )    ->	Optional[Any]:
								UpperCAmelCase				: Union[str, Any]											=	parent
								UpperCAmelCase				: Tuple											=	batch_size
								UpperCAmelCase				: str											=	seq_length
								UpperCAmelCase				: str											=	is_training
								UpperCAmelCase				: Dict											=	use_token_type_ids
								UpperCAmelCase				: str											=	use_input_mask
								UpperCAmelCase				: Optional[Any]											=	use_labels
								UpperCAmelCase				: List[str]											=	use_mc_token_ids
								UpperCAmelCase				: int											=	vocab_size
								UpperCAmelCase				: str											=	hidden_size
								UpperCAmelCase				: str											=	num_hidden_layers
								UpperCAmelCase				: Tuple											=	num_attention_heads
								UpperCAmelCase				: Optional[int]											=	intermediate_size
								UpperCAmelCase				: Union[str, Any]											=	hidden_act
								UpperCAmelCase				: Dict											=	hidden_dropout_prob
								UpperCAmelCase				: Optional[int]											=	attention_probs_dropout_prob
								UpperCAmelCase				: List[Any]											=	max_position_embeddings
								UpperCAmelCase				: Dict											=	type_vocab_size
								UpperCAmelCase				: Tuple											=	type_sequence_label_size
								UpperCAmelCase				: List[Any]											=	initializer_range
								UpperCAmelCase				: Any											=	num_labels
								UpperCAmelCase				: Union[str, Any]											=	num_choices
								UpperCAmelCase				: List[str]											=	scope
								UpperCAmelCase				: List[str]											=	self.vocab_size - 1







	def   A			(		self    :  Tuple	)    ->	Union[str, Any]:
								UpperCAmelCase				: int											=	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				: Optional[int]											=	None
								if self.use_token_type_ids:
															UpperCAmelCase				: Tuple											=	ids_tensor([self.batch_size, self.seq_length]	,    self.type_vocab_size	)

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

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

								UpperCAmelCase				: Any											=	self.get_config()

								UpperCAmelCase				: Tuple											=	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    :  Any	)    ->	List[Any]:
								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    :  Optional[int]	,    __snake_case    :  List[str]	,    __snake_case    :  str	,    __snake_case    :  Any	,    __snake_case    :  str	,    __snake_case    :  Optional[int]	,    *__snake_case    :  Union[str, Any]	)    ->	str:
								UpperCAmelCase				: List[Any]											=	CTRLModel(config=__snake_case	)
								model.to(__snake_case	)
								model.eval()

								model(__snake_case	,    token_type_ids=__snake_case	,    head_mask=__snake_case	)
								model(__snake_case	,    token_type_ids=__snake_case	)
								UpperCAmelCase				: str											=	model(__snake_case	)
								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    :  int	,    __snake_case    :  str	,    __snake_case    :  Dict	,    __snake_case    :  Optional[Any]	,    __snake_case    :  Any	,    __snake_case    :  int	,    *__snake_case    :  Union[str, Any]	)    ->	Optional[Any]:
								UpperCAmelCase				: Union[str, Any]											=	CTRLLMHeadModel(__snake_case	)
								model.to(__snake_case	)
								model.eval()

								UpperCAmelCase				: int											=	model(__snake_case	,    token_type_ids=__snake_case	,    labels=__snake_case	)
								self.parent.assertEqual(result.loss.shape	,    ()	)
								self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.seq_length, self.vocab_size)	)







	def   A			(		self    :  str	)    ->	Dict:
								UpperCAmelCase				: List[str]											=	self.prepare_config_and_inputs()

								(
								    UpperCAmelCase	
								)				: Any											=	config_and_inputs

								UpperCAmelCase				: Optional[Any]											=	{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''head_mask''': head_mask}

								return config, inputs_dict







	def   A			(		self    :  Union[str, Any]	,    __snake_case    :  Dict	,    __snake_case    :  int	,    __snake_case    :  Optional[Any]	,    __snake_case    :  Union[str, Any]	,    *__snake_case    :  List[Any]	)    ->	Dict:
								UpperCAmelCase				: List[str]											=	self.num_labels
								UpperCAmelCase				: Dict											=	CTRLForSequenceClassification(__snake_case	)
								model.to(__snake_case	)
								model.eval()
								UpperCAmelCase				: Tuple											=	ids_tensor([self.batch_size]	,    self.type_sequence_label_size	)
								UpperCAmelCase				: Tuple											=	model(__snake_case	,    token_type_ids=__snake_case	,    labels=__snake_case	)
								self.parent.assertEqual(result.logits.shape	,    (self.batch_size, self.num_labels)	)

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







	"""simple docstring"""






	lowerCamelCase__    				= (CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
	lowerCamelCase__    				= (CTRLLMHeadModel,) if is_torch_available() else ()
	lowerCamelCase__    				= (
	    {
	        """feature-extraction""": CTRLModel,
	        """text-classification""": CTRLForSequenceClassification,
	        """text-generation""": CTRLLMHeadModel,
	        """zero-shot""": CTRLForSequenceClassification,
	    }
	    if is_torch_available()
	    else {}
	)
	lowerCamelCase__    				= True
	lowerCamelCase__    				= False
	lowerCamelCase__    				= False







	def   A			(		self    :  Optional[int]	,    __snake_case    :  int	,    __snake_case    :  Optional[int]	,    __snake_case    :  Union[str, Any]	,    __snake_case    :  List[str]	,    __snake_case    :  Optional[int]	)    ->	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   A			(		self    :  int	)    ->	str:
								UpperCAmelCase				: List[str]											=	CTRLModelTester(self	)
								UpperCAmelCase				: List[str]											=	ConfigTester(self	,    config_class=__snake_case	,    n_embd=37	)







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







	def   A			(		self    :  Optional[int]	)    ->	Optional[int]:
								self.config_tester.run_common_tests()







	def   A			(		self    :  Optional[Any]	)    ->	Union[str, Any]:
								UpperCAmelCase				: Union[str, Any]											=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_ctrl_model(*__snake_case	)







	def   A			(		self    :  Tuple	)    ->	List[Any]:
								UpperCAmelCase				: Optional[int]											=	self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_lm_head_model(*__snake_case	)







	@unittest.skip('''Will be fixed soon by reducing the size of the model used for common tests.'''	)
	def   A			(		self    :  Optional[Any]	)    ->	Optional[int]:
								pass







	@slow
	def   A			(		self    :  Optional[int]	)    ->	int:
								for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
															UpperCAmelCase				: Optional[Any]											=	CTRLModel.from_pretrained(__snake_case	)
															self.assertIsNotNone(__snake_case	)







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

@require_torch
class        SCREAMING_SNAKE_CASE(							unittest.TestCase    ):







	"""simple docstring"""






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







	@slow
	def   A			(		self    :  List[Any]	)    ->	Optional[int]:
								UpperCAmelCase				: Optional[int]											=	CTRLLMHeadModel.from_pretrained('''ctrl'''	)
								model.to(__snake_case	)
								UpperCAmelCase				: List[str]											=	torch.tensor(
								    [[11859, 0, 1611, 8]]	,    dtype=torch.long	,    device=__snake_case	)  # Legal the president is
								UpperCAmelCase				: Optional[Any]											=	[
								    11859,
								    0,
								    1611,
								    8,
								    5,
								    150,
								    26449,
								    2,
								    19,
								    348,
								    469,
								    3,
								    2595,
								    48,
								    20740,
								    246533,
								    246533,
								    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				: Dict											=	model.generate(__snake_case	,    do_sample=__snake_case	)
								self.assertListEqual(output_ids[0].tolist()	,    __snake_case	)










'''simple docstring'''






import math

import tensorflow as tf
from packaging import version


def 				snake_case_  (      _lowerCAmelCase						:		List[str]						)   ->     Dict:
							UpperCAmelCase				: Tuple											=	tf.convert_to_tensor(_lowerCAmelCase						)
							UpperCAmelCase				: Dict											=	0.5 * (1.0 + tf.math.erf(x / tf.cast(tf.sqrt(2.0						)     ,   x.dtype						)						))

							return x * cdf


def 				snake_case_  (      _lowerCAmelCase						:		int						)   ->     List[Any]:
							UpperCAmelCase				: str											=	tf.convert_to_tensor(_lowerCAmelCase						)
							UpperCAmelCase				: Union[str, Any]											=	tf.cast(math.pi     ,   x.dtype						)
							UpperCAmelCase				: Tuple											=	tf.cast(0.0_4_4_7_1_5     ,   x.dtype						)
							UpperCAmelCase				: List[Any]											=	0.5 * (1.0 + tf.tanh(tf.sqrt(2.0 / pi						) * (x + coeff * tf.pow(_lowerCAmelCase     ,   3						))						))

							return x * cdf


def 				snake_case_  (      _lowerCAmelCase						:		Optional[Any]						)   ->     Any:
							UpperCAmelCase				: List[Any]											=	tf.convert_to_tensor(_lowerCAmelCase						)

							return x * tf.tanh(tf.math.softplus(_lowerCAmelCase						)						)


def 				snake_case_  (      _lowerCAmelCase						:		str						)   ->     Tuple:
							UpperCAmelCase				: Union[str, Any]											=	tf.convert_to_tensor(_lowerCAmelCase						)
							UpperCAmelCase				: Union[str, Any]											=	tf.cast(0.0_4_4_7_1_5     ,   x.dtype						)
							UpperCAmelCase				: Tuple											=	tf.cast(0.7_9_7_8_8_4_5_6_0_8     ,   x.dtype						)

							return 0.5 * x * (1.0 + tf.tanh(x * coeffa * (1.0 + coeffa * x * x)						))


def 				snake_case_  (      _lowerCAmelCase						:		Optional[Any]						)   ->     Optional[int]:
							UpperCAmelCase				: str											=	tf.convert_to_tensor(_lowerCAmelCase						)
							UpperCAmelCase				: Optional[int]											=	tf.cast(1.7_0_2     ,   x.dtype						)
							return x * tf.math.sigmoid(coeff * x						)


def 				snake_case_  (      _lowerCAmelCase						:		Any						)   ->     Any:
							return tf.clip_by_value(_gelu(_lowerCAmelCase						)     ,   -10     ,   10						)


def 				snake_case_  (      _lowerCAmelCase						:		Dict     ,   _lowerCAmelCase						:		Optional[Any]=-1						)   ->     Union[str, Any]:
							UpperCAmelCase							,	UpperCAmelCase				: Optional[Any]											=	tf.split(_lowerCAmelCase     ,   2     ,   axis=_lowerCAmelCase						)
							return a * tf.math.sigmoid(_lowerCAmelCase						)


if version.parse(tf.version.VERSION) >= version.parse("2.4"):


						def 				snake_case_  (      _lowerCAmelCase						:		Tuple						)   ->     Any:
													return tf.keras.activations.gelu(_lowerCAmelCase     ,   approximate=_lowerCAmelCase						)

						UpperCamelCase__:     List[Any]										=    tf.keras.activations.gelu




						UpperCamelCase__:     List[Any]										=    approximate_gelu_wrap
else:
						UpperCamelCase__:     List[str]										=    _gelu
						UpperCamelCase__:     Tuple										=    _gelu_new


UpperCamelCase__:     List[Any]										=    {
    "gelu": gelu,
    "gelu_10": gelu_aa,
    "gelu_fast": gelu_fast,
    "gelu_new": gelu_new,
    "glu": glu,
    "mish": mish,
    "quick_gelu": quick_gelu,
    "relu": tf.keras.activations.relu,
    "sigmoid": tf.keras.activations.sigmoid,
    "silu": tf.keras.activations.swish,
    "swish": tf.keras.activations.swish,
    "tanh": tf.keras.activations.tanh,
}




def 				snake_case_  (      _lowerCAmelCase						:		Dict						)   ->     List[Any]:
							if activation_string in ACTaFN:
														return ACTaFN[activation_string]
							else:
														raise KeyError(f"""function {activation_string} not found in ACT2FN mapping {list(ACTaFN.keys()						)}"""						)





