






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_						:


						'''simple docstring'''




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






						def       _lowerCAmelCase       (	self				) ->				List[str]:
								snake_case_				:							int						 =       ids_tensor([self.batch_size, self.seq_length]		,				self.vocab_size				)

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

								snake_case_				:							List[Any]						 =       None
								if self.use_token_type_ids:
										snake_case_				:							Optional[int]						 =       ids_tensor([self.batch_size, self.seq_length]		,				self.type_vocab_size				)

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

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

								snake_case_				:							Tuple						 =       self.get_config()

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

								model(_SCREAMING_SNAKE_CASE		,				token_type_ids=_SCREAMING_SNAKE_CASE		,				head_mask=_SCREAMING_SNAKE_CASE				)
								model(_SCREAMING_SNAKE_CASE		,				token_type_ids=_SCREAMING_SNAKE_CASE				)
								snake_case_				:							Any						 =       model(_SCREAMING_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       _lowerCAmelCase       (	self		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				*_SCREAMING_SNAKE_CASE				) ->				Optional[int]:
								snake_case_				:							Optional[Any]						 =       CTRLLMHeadModel(_SCREAMING_SNAKE_CASE				)
								model.to(_SCREAMING_SNAKE_CASE				)
								model.eval()

								snake_case_				:							List[Any]						 =       model(_SCREAMING_SNAKE_CASE		,				token_type_ids=_SCREAMING_SNAKE_CASE		,				labels=_SCREAMING_SNAKE_CASE				)
								self.parent.assertEqual(result.loss.shape		,				()				)
								self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.seq_length, self.vocab_size)				)






						def       _lowerCAmelCase       (	self				) ->				int:
								snake_case_				:							Union[str, Any]						 =       self.prepare_config_and_inputs()

								(
								    snake_case_
								)				:							List[Any]						 =       config_and_inputs

								snake_case_				:							Tuple						 =       {"""input_ids""": input_ids, """token_type_ids""": token_type_ids, """head_mask""": head_mask}

								return config, inputs_dict






						def       _lowerCAmelCase       (	self		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				*_SCREAMING_SNAKE_CASE				) ->				List[str]:
								snake_case_				:							Any						 =       self.num_labels
								snake_case_				:							str						 =       CTRLForSequenceClassification(_SCREAMING_SNAKE_CASE				)
								model.to(_SCREAMING_SNAKE_CASE				)
								model.eval()
								snake_case_				:							int						 =       ids_tensor([self.batch_size]		,				self.type_sequence_label_size				)
								snake_case_				:							Optional[int]						 =       model(_SCREAMING_SNAKE_CASE		,				token_type_ids=_SCREAMING_SNAKE_CASE		,				labels=_SCREAMING_SNAKE_CASE				)
								self.parent.assertEqual(result.logits.shape		,				(self.batch_size, self.num_labels)				)




@require_torch
class 		UpperCAmelCase_						(	A_					,  A_					,  A_					,  unittest.TestCase					):


						'''simple docstring'''




						A     :   str						=					(CTRLModel, CTRLLMHeadModel, CTRLForSequenceClassification) if is_torch_available() else ()
						A     :   str						=					(CTRLLMHeadModel,) if is_torch_available() else ()
						A     :   Tuple						=					(
						    {
						        '''feature-extraction''': CTRLModel,
						        '''text-classification''': CTRLForSequenceClassification,
						        '''text-generation''': CTRLLMHeadModel,
						        '''zero-shot''': CTRLForSequenceClassification,
						    }
						    if is_torch_available()
						    else {}
						)
						A     :   List[str]						=					True
						A     :   Dict						=					False
						A     :   Optional[int]						=					False






						def       _lowerCAmelCase       (	self		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE		,				_SCREAMING_SNAKE_CASE				) ->				str:
								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				) ->				Any:
								snake_case_				:							Tuple						 =       CTRLModelTester(self				)
								snake_case_				:							Union[str, Any]						 =       ConfigTester(self		,				config_class=_SCREAMING_SNAKE_CASE		,				n_embd=37				)






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






						def       _lowerCAmelCase       (	self				) ->				int:
								self.config_tester.run_common_tests()






						def       _lowerCAmelCase       (	self				) ->				Dict:
								snake_case_				:							Union[str, Any]						 =       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_ctrl_model(*_SCREAMING_SNAKE_CASE				)






						def       _lowerCAmelCase       (	self				) ->				Union[str, Any]:
								snake_case_				:							Optional[Any]						 =       self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_lm_head_model(*_SCREAMING_SNAKE_CASE				)






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






						@slow
						def       _lowerCAmelCase       (	self				) ->				Optional[int]:
								for model_name in CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
										snake_case_				:							List[str]						 =       CTRLModel.from_pretrained(_SCREAMING_SNAKE_CASE				)
										self.assertIsNotNone(_SCREAMING_SNAKE_CASE				)






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




@require_torch
class 		UpperCAmelCase_						(	unittest.TestCase					):


						'''simple docstring'''




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






						@slow
						def       _lowerCAmelCase       (	self				) ->				Dict:
								snake_case_				:							Dict						 =       CTRLLMHeadModel.from_pretrained("ctrl"				)
								model.to(_SCREAMING_SNAKE_CASE				)
								snake_case_				:							Optional[int]						 =       torch.tensor(
								    [[1_1859, 0, 1611, 8]]		,				dtype=torch.long		,				device=_SCREAMING_SNAKE_CASE				)  # Legal the president is
								snake_case_				:							Optional[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

								snake_case_				:							Union[str, Any]						 =       model.generate(_SCREAMING_SNAKE_CASE		,				do_sample=_SCREAMING_SNAKE_CASE				)
								self.assertListEqual(output_ids[0].tolist()		,				_SCREAMING_SNAKE_CASE				)








from typing import TYPE_CHECKING

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


lowercase  :							int			  =    {
    '''configuration_audio_spectrogram_transformer''': [
        '''AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''ASTConfig''',
    ]
}

try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							lowercase  :							Optional[Any]			  =    [
							    '''AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST''',
							    '''ASTForAudioClassification''',
							    '''ASTModel''',
							    '''ASTPreTrainedModel''',
							]

try:
							if not is_speech_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							lowercase  :							Optional[int]			  =    ['''ASTFeatureExtractor''']

if TYPE_CHECKING:
							from .configuration_audio_spectrogram_transformer import (
							    AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP,
							    ASTConfig,
							)

							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_audio_spectrogram_transformer import (
														    AUDIO_SPECTROGRAM_TRANSFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
														    ASTForAudioClassification,
														    ASTModel,
														    ASTPreTrainedModel,
														)

							try:
														if not is_speech_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .feature_extraction_audio_spectrogram_transformer import ASTFeatureExtractor


else:
							import sys

							lowercase  :							Optional[int]			  =    _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
