
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 			lowercase__(    UpperCAmelCase							):



	"""simple docstring"""




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

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

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

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

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

					lowercase_					  = 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	,							)

					lowercase_					  = 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  :      Tuple	,							SCREAMING_SNAKE_CASE_  :      int	,							SCREAMING_SNAKE_CASE_  :      Tuple	,							SCREAMING_SNAKE_CASE_  :      int	,							SCREAMING_SNAKE_CASE_  :      Dict	,							SCREAMING_SNAKE_CASE_  :      str	,							*SCREAMING_SNAKE_CASE_  :      List[Any]	)	->					List[Any]:
					lowercase_					  = TFCTRLModel(config=SCREAMING_SNAKE_CASE_	)
					lowercase_					  = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
					lowercase_					  = model(SCREAMING_SNAKE_CASE_	)

					lowercase_					  = [input_ids, None, input_mask]  # None is the input for 'past'
					lowercase_					  = model(SCREAMING_SNAKE_CASE_	)

					lowercase_					  = model(SCREAMING_SNAKE_CASE_	)

					self.parent.assertEqual(result.last_hidden_state.shape	,							(self.batch_size, self.seq_length, self.hidden_size)	)
	def 						_lowercase  (	self  :      int	,							SCREAMING_SNAKE_CASE_  :      Optional[int]	,							SCREAMING_SNAKE_CASE_  :      Optional[Any]	,							SCREAMING_SNAKE_CASE_  :      Optional[Any]	,							SCREAMING_SNAKE_CASE_  :      List[Any]	,							SCREAMING_SNAKE_CASE_  :      Union[str, Any]	,							*SCREAMING_SNAKE_CASE_  :      Union[str, Any]	)	->					Optional[Any]:
					lowercase_					  = TFCTRLLMHeadModel(config=SCREAMING_SNAKE_CASE_	)
					lowercase_					  = {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
					lowercase_					  = model(SCREAMING_SNAKE_CASE_	)
					self.parent.assertEqual(result.logits.shape	,							(self.batch_size, self.seq_length, self.vocab_size)	)
	def 						_lowercase  (	self  :      Optional[Any]	,							SCREAMING_SNAKE_CASE_  :      int	,							SCREAMING_SNAKE_CASE_  :      Union[str, Any]	,							SCREAMING_SNAKE_CASE_  :      List[str]	,							SCREAMING_SNAKE_CASE_  :      List[Any]	,							SCREAMING_SNAKE_CASE_  :      Dict	,							*SCREAMING_SNAKE_CASE_  :      str	)	->					Any:
					lowercase_					  = self.num_labels
					lowercase_					  = ids_tensor([self.batch_size]	,							self.type_sequence_label_size	)
					lowercase_					  = {
					    '''input_ids''': input_ids,
					    '''token_type_ids''': token_type_ids,
					    '''labels''': sequence_labels,
					}
					lowercase_					  = TFCTRLForSequenceClassification(SCREAMING_SNAKE_CASE_	)
					lowercase_					  = model(SCREAMING_SNAKE_CASE_	)
					self.parent.assertEqual(result.logits.shape	,							(self.batch_size, self.num_labels)	)




	def 						_lowercase  (	self  :      Optional[Any]	)	->					Dict:
					lowercase_					  = self.prepare_config_and_inputs()

					(
					    (
					    lowercase_
					)  ,     (
					    lowercase_
					)  ,     (
					    lowercase_
					)  ,     (
					    lowercase_
					)  ,     (
					    lowercase_
					)  ,     (
					    lowercase_
					)  ,     (
					    lowercase_
					)  ,     (
					    lowercase_
					)  ,     (
					    lowercase_
					)  ,     
					)					  = config_and_inputs

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

@require_tf
class 			lowercase__(    UpperCAmelCase			,						UpperCAmelCase			,						unittest.TestCase							):



	"""simple docstring"""




	a      :Dict   					=     (TFCTRLModel, TFCTRLLMHeadModel, TFCTRLForSequenceClassification) if is_tf_available() else ()
	a      :Any   					=     (TFCTRLLMHeadModel,) if is_tf_available() else ()
	a      :Optional[Any]   					=     (
	    {
	        'feature-extraction': TFCTRLModel,
	        'text-classification': TFCTRLForSequenceClassification,
	        'text-generation': TFCTRLLMHeadModel,
	        'zero-shot': TFCTRLForSequenceClassification,
	    }
	    if is_tf_available()
	    else {}
	)
	a      :int   					=     False
	a      :Optional[Any]   					=     False
	def 						_lowercase  (	self  :      Optional[Any]	,							SCREAMING_SNAKE_CASE_  :      Any	,							SCREAMING_SNAKE_CASE_  :      str	,							SCREAMING_SNAKE_CASE_  :      int	,							SCREAMING_SNAKE_CASE_  :      Tuple	,							SCREAMING_SNAKE_CASE_  :      Any	)	->					Union[str, 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 						_lowercase  (	self  :      Optional[Any]	)	->					Union[str, Any]:
					lowercase_					  = TFCTRLModelTester(self	)
					lowercase_					  = ConfigTester(self	,							config_class=SCREAMING_SNAKE_CASE_	,							n_embd=3_7	)
	def 						_lowercase  (	self  :      Dict	)	->					List[str]:
					self.config_tester.run_common_tests()
	def 						_lowercase  (	self  :      List[Any]	)	->					str:
					lowercase_					  = self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_ctrl_model(*SCREAMING_SNAKE_CASE_	)
	def 						_lowercase  (	self  :      str	)	->					str:
					lowercase_					  = self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_ctrl_lm_head(*SCREAMING_SNAKE_CASE_	)
	def 						_lowercase  (	self  :      Union[str, Any]	)	->					Dict:
					lowercase_					  = self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_ctrl_for_sequence_classification(*SCREAMING_SNAKE_CASE_	)
	def 						_lowercase  (	self  :      int	)	->					List[Any]:
					lowercase_  ,     lowercase_					  = self.model_tester.prepare_config_and_inputs_for_common()
					lowercase_					  = [TFCTRLLMHeadModel]
					lowercase_					  = [TFCTRLForSequenceClassification]

					for model_class in self.all_model_classes:
									lowercase_					  = model_class(SCREAMING_SNAKE_CASE_	)
									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:
													lowercase_					  = model.get_output_embeddings()
													assert isinstance(SCREAMING_SNAKE_CASE_	,							tf.keras.layers.Layer	)
													lowercase_					  = model.get_bias()
													assert isinstance(SCREAMING_SNAKE_CASE_	,							SCREAMING_SNAKE_CASE_	)
													for k, v in name.items():
																	assert isinstance(SCREAMING_SNAKE_CASE_	,							tf.Variable	)
									elif model_class in list_other_models_with_output_ebd:
													lowercase_					  = model.get_output_embeddings()
													assert isinstance(SCREAMING_SNAKE_CASE_	,							tf.keras.layers.Layer	)
													lowercase_					  = model.get_bias()
													assert name is None
									else:
													lowercase_					  = model.get_output_embeddings()
													assert x is None
													lowercase_					  = model.get_bias()
													assert name is None




	@slow
	def 						_lowercase  (	self  :      Any	)	->					Optional[Any]:
					for model_name in TF_CTRL_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
									lowercase_					  = TFCTRLModel.from_pretrained(SCREAMING_SNAKE_CASE_	)
									self.assertIsNotNone(SCREAMING_SNAKE_CASE_	)

@require_tf
class 			lowercase__(    unittest.TestCase							):



	"""simple docstring"""




	@slow
	def 						_lowercase  (	self  :      Dict	)	->					Optional[int]:
					lowercase_					  = TFCTRLLMHeadModel.from_pretrained('''ctrl'''	)
					lowercase_					  = tf.convert_to_tensor([[1_1_8_5_9, 0, 1_6_1_1, 8]]	,							dtype=tf.intaa	)  # Legal the president is
					lowercase_					  = [
					    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

					lowercase_					  = model.generate(SCREAMING_SNAKE_CASE_	,							do_sample=SCREAMING_SNAKE_CASE_	)
					self.assertListEqual(output_ids[0].numpy().tolist()	,							SCREAMING_SNAKE_CASE_	)






from typing import TYPE_CHECKING

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


__a        = {
    'configuration_donut_swin': ['DONUT_SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP', 'DonutSwinConfig'],
    'processing_donut': ['DonutProcessor'],
}

try:
						if not is_torch_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__a        = [
						    'DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST',
						    'DonutSwinModel',
						    'DonutSwinPreTrainedModel',
						]

try:
						if not is_vision_available():
												raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
						pass
else:
						__a        = ['DonutFeatureExtractor']
						__a        = ['DonutImageProcessor']


if TYPE_CHECKING:
						from .configuration_donut_swin import DONUT_SWIN_PRETRAINED_CONFIG_ARCHIVE_MAP, DonutSwinConfig
						from .processing_donut import DonutProcessor

						try:
												if not is_torch_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .modeling_donut_swin import (
												    DONUT_SWIN_PRETRAINED_MODEL_ARCHIVE_LIST,
												    DonutSwinModel,
												    DonutSwinPreTrainedModel,
												)

						try:
												if not is_vision_available():
																		raise OptionalDependencyNotAvailable()
						except OptionalDependencyNotAvailable:
												pass
						else:
												from .feature_extraction_donut import DonutFeatureExtractor
												from .image_processing_donut import DonutImageProcessor

else:
						import sys

						__a        = _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)




