


from __future__ import annotations

import unittest

from transformers import CTRLConfig, is_tf_available
from transformers.testing_utils import require_tf, slow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_tf_available():
						import tensorflow as tf

						from transformers.models.ctrl.modeling_tf_ctrl import (
						    TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST,
						    TFCTRLForSequenceClassification,
						    TFCTRLLMHeadModel,
						    TFCTRLModel,
						)







class 					snake_case__(				UpperCAmelCase__				):







						'''simple docstring'''




						def __init__(					self ,    __lowercase ,    )       ->     Optional[int]:
										lowerCAmelCase_			:   str     					=					parent
										lowerCAmelCase_			:   List[Any]     					=					1_3
										lowerCAmelCase_			:   List[str]     					=					7
										lowerCAmelCase_			:   str     					=					True
										lowerCAmelCase_			:   str     					=					True
										lowerCAmelCase_			:   Union[str, Any]     					=					True
										lowerCAmelCase_			:   List[str]     					=					True
										lowerCAmelCase_			:   Dict     					=					True
										lowerCAmelCase_			:   List[str]     					=					9_9
										lowerCAmelCase_			:   List[Any]     					=					3_2
										lowerCAmelCase_			:   Tuple     					=					2
										lowerCAmelCase_			:   str     					=					4
										lowerCAmelCase_			:   List[Any]     					=					3_7
										lowerCAmelCase_			:   Dict     					=					'''gelu'''
										lowerCAmelCase_			:   Union[str, Any]     					=					0.1
										lowerCAmelCase_			:   Tuple     					=					0.1
										lowerCAmelCase_			:   int     					=					5_1_2
										lowerCAmelCase_			:   Optional[int]     					=					1_6
										lowerCAmelCase_			:   Dict     					=					2
										lowerCAmelCase_			:   Union[str, Any]     					=					0.02
										lowerCAmelCase_			:   Tuple     					=					3
										lowerCAmelCase_			:   int     					=					4
										lowerCAmelCase_			:   int     					=					None
										lowerCAmelCase_			:   int     					=					self.vocab_size - 1




						def     lowercase_			(					self				)       ->     Tuple:
										lowerCAmelCase_			:   Any     					=					ids_tensor([self.batch_size, self.seq_length] ,    self.vocab_size				)

										lowerCAmelCase_			:   int     					=					None
										if self.use_input_mask:
														lowerCAmelCase_			:   Any     					=					random_attention_mask([self.batch_size, self.seq_length]				)

										lowerCAmelCase_			:   Optional[int]     					=					None
										if self.use_token_type_ids:
														lowerCAmelCase_			:   Dict     					=					ids_tensor([self.batch_size, self.seq_length] ,    self.type_vocab_size				)

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

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

										lowerCAmelCase_			:   int     					=					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 ,    )

										lowerCAmelCase_			:   Union[str, 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     lowercase_			(					self ,    __lowercase ,    __lowercase ,    __lowercase ,    __lowercase ,    __lowercase ,    *__lowercase				)       ->     Optional[Any]:
										lowerCAmelCase_			:   Tuple     					=					TFCTRLModel(config=__lowercase				)
										lowerCAmelCase_			:   Optional[Any]     					=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
										lowerCAmelCase_			:   Dict     					=					model(__lowercase				)

										lowerCAmelCase_			:   Optional[Any]     					=					[input_ids, None, input_mask]  # None is the input for 'past'
										lowerCAmelCase_			:   List[str]     					=					model(__lowercase				)

										lowerCAmelCase_			:   int     					=					model(__lowercase				)

										self.parent.assertEqual(result.last_hidden_state.shape ,    (self.batch_size, self.seq_length, self.hidden_size)				)




						def     lowercase_			(					self ,    __lowercase ,    __lowercase ,    __lowercase ,    __lowercase ,    __lowercase ,    *__lowercase				)       ->     Dict:
										lowerCAmelCase_			:   str     					=					TFCTRLLMHeadModel(config=__lowercase				)
										lowerCAmelCase_			:   Union[str, Any]     					=					{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
										lowerCAmelCase_			:   Any     					=					model(__lowercase				)
										self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.seq_length, self.vocab_size)				)




						def     lowercase_			(					self ,    __lowercase ,    __lowercase ,    __lowercase ,    __lowercase ,    __lowercase ,    *__lowercase				)       ->     List[Any]:
										lowerCAmelCase_			:   int     					=					self.num_labels
										lowerCAmelCase_			:   str     					=					ids_tensor([self.batch_size] ,    self.type_sequence_label_size				)
										lowerCAmelCase_			:   List[str]     					=					{
										    '''input_ids''': input_ids,
										    '''token_type_ids''': token_type_ids,
										    '''labels''': sequence_labels,
										}
										lowerCAmelCase_			:   List[str]     					=					TFCTRLForSequenceClassification(__lowercase				)
										lowerCAmelCase_			:   List[Any]     					=					model(__lowercase				)
										self.parent.assertEqual(result.logits.shape ,    (self.batch_size, self.num_labels)				)




						def     lowercase_			(					self				)       ->     str:
										lowerCAmelCase_			:   List[Any]     					=					self.prepare_config_and_inputs()

										(
										    (
										    lowerCAmelCase_
										)					,	(
										    lowerCAmelCase_
										)					,	(
										    lowerCAmelCase_
										)					,	(
										    lowerCAmelCase_
										)					,	(
										    lowerCAmelCase_
										)					,	(
										    lowerCAmelCase_
										)					,	(
										    lowerCAmelCase_
										)					,	(
										    lowerCAmelCase_
										)					,	(
										    lowerCAmelCase_
										)					,	
										)			:   int     					=					config_and_inputs

										lowerCAmelCase_			:   int     					=					{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': input_mask}
										return config, inputs_dict







@require_tf
class 					snake_case__(				UpperCAmelCase__,    UpperCAmelCase__,    unittest.TestCase				):







						'''simple docstring'''




						SCREAMING_SNAKE_CASE__   :	List[Any]       =				(TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
						SCREAMING_SNAKE_CASE__   :	Dict       =				(TFCTRLLMHeadModel,) if is_tf_available() else ()
						SCREAMING_SNAKE_CASE__   :	Tuple       =				(
						    {
						        """feature-extraction""": TFCTRLModel,
						        """text-classification""": TFCTRLForSequenceClassification,
						        """text-generation""": TFCTRLLMHeadModel,
						        """zero-shot""": TFCTRLForSequenceClassification,
						    }
						    if is_tf_available()
						    else {}
						)
						SCREAMING_SNAKE_CASE__   :	int       =				False
						SCREAMING_SNAKE_CASE__   :	Optional[int]       =				False




						def     lowercase_			(					self ,    __lowercase ,    __lowercase ,    __lowercase ,    __lowercase ,    __lowercase				)       ->     Optional[int]:
										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     lowercase_			(					self				)       ->     Union[str, Any]:
										lowerCAmelCase_			:   Optional[int]     					=					TFCTRLModelTester(self				)
										lowerCAmelCase_			:   Any     					=					ConfigTester(self ,    config_class=__lowercase ,    n_embd=3_7				)




						def     lowercase_			(					self				)       ->     Any:
										self.config_tester.run_common_tests()




						def     lowercase_			(					self				)       ->     str:
										lowerCAmelCase_			:   Dict     					=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_ctrl_model(*__lowercase				)




						def     lowercase_			(					self				)       ->     List[Any]:
										lowerCAmelCase_			:   Any     					=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_ctrl_lm_head(*__lowercase				)




						def     lowercase_			(					self				)       ->     Tuple:
										lowerCAmelCase_			:   Any     					=					self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_ctrl_for_sequence_classification(*__lowercase				)




						def     lowercase_			(					self				)       ->     Any:
										lowerCAmelCase_					,	lowerCAmelCase_			:   Tuple     					=					self.model_tester.prepare_config_and_inputs_for_common()
										lowerCAmelCase_			:   int     					=					[TFCTRLLMHeadModel]
										lowerCAmelCase_			:   Dict     					=					[TFCTRLForSequenceClassification]

										for model_class in self.all_model_classes:
														lowerCAmelCase_			:   Dict     					=					model_class(__lowercase				)
														model.build()  # may be needed for the get_bias() call below
														assert isinstance(model.get_input_embeddings() ,    tf.keras.layers.Layer				)

														if model_class in list_lm_models:
																		lowerCAmelCase_			:   Optional[Any]     					=					model.get_output_embeddings()
																		assert isinstance(__lowercase ,    tf.keras.layers.Layer				)
																		lowerCAmelCase_			:   str     					=					model.get_bias()
																		assert isinstance(__lowercase ,    __lowercase				)
																		for k, v in name.items():
																						assert isinstance(__lowercase ,    tf.Variable				)
														elif model_class in list_other_models_with_output_ebd:
																		lowerCAmelCase_			:   Any     					=					model.get_output_embeddings()
																		assert isinstance(__lowercase ,    tf.keras.layers.Layer				)
																		lowerCAmelCase_			:   Tuple     					=					model.get_bias()
																		assert name is None
														else:
																		lowerCAmelCase_			:   List[str]     					=					model.get_output_embeddings()
																		assert x is None
																		lowerCAmelCase_			:   Union[str, Any]     					=					model.get_bias()
																		assert name is None




						@slow
						def     lowercase_			(					self				)       ->     str:
										for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
														lowerCAmelCase_			:   Union[str, Any]     					=					TFCTRLModel.from_pretrained(__lowercase				)
														self.assertIsNotNone(__lowercase				)







@require_tf
class 					snake_case__(				unittest.TestCase				):







						'''simple docstring'''




						@slow
						def     lowercase_			(					self				)       ->     Any:
										lowerCAmelCase_			:   str     					=					TFCTRLLMHeadModel.from_pretrained('''ctrl'''				)
										lowerCAmelCase_			:   List[Any]     					=					tf.convert_to_tensor([[1_1_8_5_9, 0, 1_6_1_1, 8]] ,    dtype=tf.intaa				)  # Legal the president is
										lowerCAmelCase_			:   Optional[Any]     					=					[
										    1_1_8_5_9,
										    0,
										    1_6_1_1,
										    8,
										    5,
										    1_5_0,
										    2_6_4_4_9,
										    2,
										    1_9,
										    3_4_8,
										    4_6_9,
										    3,
										    2_5_9_5,
										    4_8,
										    2_0_7_4_0,
										    2_4_6_5_3_3,
										    2_4_6_5_3_3,
										    1_9,
										    3_0,
										    5,
										]  # Legal the president is a good guy and I don't want to lose my job. \n \n I have a

										lowerCAmelCase_			:   Dict     					=					model.generate(__lowercase ,    do_sample=__lowercase				)
										self.assertListEqual(output_ids[0].numpy().tolist() ,    __lowercase				)



import torch.nn as nn

from .imports import is_fpa_available


if is_fpa_available():
						import transformer_engine.pytorch as te



def       lowerCAmelCase			(		lowerCAmelCase_			, lowerCAmelCase_=True			, lowerCAmelCase_=True			, lowerCAmelCase_=True			)->			Union[str, Any]:
				if not is_fpa_available():
								raise ImportError('''Using `convert_model` requires transformer_engine to be installed.'''			)
				for name, module in model.named_children():
								if isinstance(lowerCAmelCase_			, nn.Linear			) and to_transformer_engine and _convert_linear:
												# Return early if the linear layer weights are not multiples of 16
												if any(p % 16 != 0 for p in module.weight.shape			):
																return
												lowerCAmelCase_			:   Optional[int]     					=					module.bias is not None
												lowerCAmelCase_			:   Any     					=					te.Linear(
												    module.in_features			, module.out_features			, bias=lowerCAmelCase_			, params_dtype=module.weight.dtype			)
												lowerCAmelCase_			:   Dict     					=					module.weight.data.clone()
												if has_bias:
																lowerCAmelCase_			:   Any     					=					module.bias.data.clone()

												setattr(lowerCAmelCase_			, lowerCAmelCase_			, lowerCAmelCase_			)
								elif isinstance(lowerCAmelCase_			, nn.LayerNorm			) and to_transformer_engine and _convert_ln:
												lowerCAmelCase_			:   List[str]     					=					te.LayerNorm(module.normalized_shape[0]			, eps=module.eps			, params_dtype=module.weight.dtype			)
												lowerCAmelCase_			:   Union[str, Any]     					=					module.weight.data.clone()
												lowerCAmelCase_			:   List[Any]     					=					module.bias.data.clone()

												setattr(lowerCAmelCase_			, lowerCAmelCase_			, lowerCAmelCase_			)
								elif isinstance(lowerCAmelCase_			, te.Linear			) and not to_transformer_engine and _convert_linear:
												lowerCAmelCase_			:   Dict     					=					module.bias is not None
												lowerCAmelCase_			:   Union[str, Any]     					=					nn.Linear(
												    module.in_features			, module.out_features			, bias=lowerCAmelCase_			, params_dtype=module.weight.dtype			)
												lowerCAmelCase_			:   Optional[Any]     					=					module.weight.data.clone()
												if has_bias:
																lowerCAmelCase_			:   Union[str, Any]     					=					module.bias.data.clone()

												setattr(lowerCAmelCase_			, lowerCAmelCase_			, lowerCAmelCase_			)
								elif isinstance(lowerCAmelCase_			, te.LayerNorm			) and not to_transformer_engine and _convert_ln:
												lowerCAmelCase_			:   Union[str, Any]     					=					nn.LayerNorm(module.normalized_shape[0]			, eps=module.eps			, params_dtype=module.weight.dtype			)
												lowerCAmelCase_			:   List[str]     					=					module.weight.data.clone()
												lowerCAmelCase_			:   List[Any]     					=					module.bias.data.clone()

												setattr(lowerCAmelCase_			, lowerCAmelCase_			, lowerCAmelCase_			)
								else:
												convert_model(
												    lowerCAmelCase_			, to_transformer_engine=lowerCAmelCase_			, _convert_linear=_convert_linear			, _convert_ln=_convert_ln			, )




def       lowerCAmelCase			(		lowerCAmelCase_			)->			str:
				if not is_fpa_available():
								raise ImportError('''Using `has_transformer_engine_layers` requires transformer_engine to be installed.'''			)
				for m in model.modules():
								if isinstance(lowerCAmelCase_			, (te.LayerNorm, te.Linear)			):
												return True
				return False