
from __future__ import annotations

import unittest

from transformers import OpenAIGPTConfig, 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.openai.modeling_tf_openai import (
							    TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST,
							    TFOpenAIGPTDoubleHeadsModel,
							    TFOpenAIGPTForSequenceClassification,
							    TFOpenAIGPTLMHeadModel,
							    TFOpenAIGPTModel,
							)



class 			UpperCamelCase_			:




							'''simple docstring'''

							def __init__(				self     ,						UpperCamelCase     ,						) ->	Optional[int]:
										UpperCamelCase__    :		Dict     =				parent
										UpperCamelCase__    :		int     =				13
										UpperCamelCase__    :		List[Any]     =				7
										UpperCamelCase__    :		List[str]     =				True
										UpperCamelCase__    :		List[Any]     =				True
										UpperCamelCase__    :		Union[str, Any]     =				True
										UpperCamelCase__    :		Dict     =				True
										UpperCamelCase__    :		Any     =				True
										UpperCamelCase__    :		str     =				99
										UpperCamelCase__    :		Dict     =				32
										UpperCamelCase__    :		Union[str, Any]     =				2
										UpperCamelCase__    :		Dict     =				4
										UpperCamelCase__    :		str     =				37
										UpperCamelCase__    :		List[Any]     =				'gelu'
										UpperCamelCase__    :		Dict     =				0.1
										UpperCamelCase__    :		Union[str, Any]     =				0.1
										UpperCamelCase__    :		List[str]     =				5_12
										UpperCamelCase__    :		Tuple     =				16
										UpperCamelCase__    :		Optional[Any]     =				2
										UpperCamelCase__    :		Any     =				0.02
										UpperCamelCase__    :		Any     =				3
										UpperCamelCase__    :		Any     =				4
										UpperCamelCase__    :		Optional[int]     =				None
										UpperCamelCase__    :		Union[str, Any]     =				self.vocab_size - 1
							def        lowerCAmelCase__ (				self) ->	List[Any]:
										UpperCamelCase__    :		Any     =				ids_tensor([self.batch_size, self.seq_length]     ,						self.vocab_size)

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

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

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

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

										UpperCamelCase__    :		List[Any]     =				OpenAIGPTConfig(
										    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     ,						)

										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        lowerCAmelCase__ (				self     ,						UpperCamelCase     ,						UpperCamelCase     ,						UpperCamelCase     ,						UpperCamelCase     ,						UpperCamelCase     ,						*UpperCamelCase) ->	Optional[Any]:
										UpperCamelCase__    :		Optional[int]     =				TFOpenAIGPTModel(config=UpperCamelCase)
										UpperCamelCase__    :		Optional[int]     =				{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
										UpperCamelCase__    :		Union[str, Any]     =				model(UpperCamelCase)

										UpperCamelCase__    :		Optional[Any]     =				[input_ids, input_mask]
										UpperCamelCase__    :		Optional[Any]     =				model(UpperCamelCase)

										UpperCamelCase__    :		Optional[Any]     =				model(UpperCamelCase)

										self.parent.assertEqual(result.last_hidden_state.shape     ,						(self.batch_size, self.seq_length, self.hidden_size))
							def        lowerCAmelCase__ (				self     ,						UpperCamelCase     ,						UpperCamelCase     ,						UpperCamelCase     ,						UpperCamelCase     ,						UpperCamelCase     ,						*UpperCamelCase) ->	Any:
										UpperCamelCase__    :		Optional[int]     =				TFOpenAIGPTLMHeadModel(config=UpperCamelCase)
										UpperCamelCase__    :		Any     =				{'input_ids': input_ids, 'attention_mask': input_mask, 'token_type_ids': token_type_ids}
										UpperCamelCase__    :		str     =				model(UpperCamelCase)
										self.parent.assertEqual(result.logits.shape     ,						(self.batch_size, self.seq_length, self.vocab_size))
							def        lowerCAmelCase__ (				self     ,						UpperCamelCase     ,						UpperCamelCase     ,						UpperCamelCase     ,						UpperCamelCase     ,						UpperCamelCase     ,						UpperCamelCase     ,						*UpperCamelCase) ->	Optional[int]:
										UpperCamelCase__    :		Any     =				TFOpenAIGPTDoubleHeadsModel(config=UpperCamelCase)

										UpperCamelCase__    :		Tuple     =				tf.tile(tf.expand_dims(UpperCamelCase     ,						1)     ,						(1, self.num_choices, 1))
										UpperCamelCase__    :		str     =				tf.tile(tf.expand_dims(UpperCamelCase     ,						1)     ,						(1, self.num_choices, 1))
										UpperCamelCase__    :		Union[str, Any]     =				tf.tile(tf.expand_dims(UpperCamelCase     ,						1)     ,						(1, self.num_choices, 1))

										UpperCamelCase__    :		str     =				{
										    'input_ids': multiple_choice_inputs_ids,
										    'mc_token_ids': mc_token_ids,
										    'attention_mask': multiple_choice_input_mask,
										    'token_type_ids': multiple_choice_token_type_ids,
										}
										UpperCamelCase__    :		str     =				model(UpperCamelCase)
										self.parent.assertEqual(
										    result.logits.shape     ,						(self.batch_size, self.num_choices, self.seq_length, self.vocab_size))
										self.parent.assertEqual(result.mc_logits.shape     ,						(self.batch_size, self.num_choices))
							def        lowerCAmelCase__ (				self     ,						UpperCamelCase     ,						UpperCamelCase     ,						UpperCamelCase     ,						UpperCamelCase     ,						UpperCamelCase     ,						*UpperCamelCase) ->	Dict:
										UpperCamelCase__    :		Any     =				self.num_labels
										UpperCamelCase__    :		Tuple     =				ids_tensor([self.batch_size]     ,						self.type_sequence_label_size)
										UpperCamelCase__    :		Dict     =				{
										    'input_ids': input_ids,
										    'attention_mask': input_mask,
										    'token_type_ids': token_type_ids,
										    'labels': sequence_labels,
										}
										UpperCamelCase__    :		List[Any]     =				TFOpenAIGPTForSequenceClassification(UpperCamelCase)
										UpperCamelCase__    :		List[str]     =				model(UpperCamelCase)
										self.parent.assertEqual(result.logits.shape     ,						(self.batch_size, self.num_labels))






							def        lowerCAmelCase__ (				self) ->	Tuple:
										UpperCamelCase__    :		Dict     =				self.prepare_config_and_inputs()

										(
										    (
										    UpperCamelCase__
										)   ,			(
										    UpperCamelCase__
										)   ,			(
										    UpperCamelCase__
										)   ,			(
										    UpperCamelCase__
										)   ,			(
										    UpperCamelCase__
										)   ,			(
										    UpperCamelCase__
										)   ,			(
										    UpperCamelCase__
										)   ,			(
										    UpperCamelCase__
										)   ,			(
										    UpperCamelCase__
										)   ,			
										)    :		Tuple     =				config_and_inputs

										UpperCamelCase__    :		Union[str, Any]     =				{'input_ids': input_ids, 'token_type_ids': token_type_ids, 'attention_mask': input_mask}
										return config, inputs_dict



@require_tf
class 			UpperCamelCase_			(						__UpperCamelCase	,     __UpperCamelCase	,     unittest.TestCase   ):




							'''simple docstring'''

							UpperCamelCase_					   =							(
							    (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
							    if is_tf_available()
							    else ()
							)
							UpperCamelCase_					   =							(
							    (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
							)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
							UpperCamelCase_					   =							(
							    {
							        """feature-extraction""": TFOpenAIGPTModel,
							        """text-classification""": TFOpenAIGPTForSequenceClassification,
							        """text-generation""": TFOpenAIGPTLMHeadModel,
							        """zero-shot""": TFOpenAIGPTForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							UpperCamelCase_					   =							False
							UpperCamelCase_					   =							False
							def        lowerCAmelCase__ (				self     ,						UpperCamelCase     ,						UpperCamelCase     ,						UpperCamelCase     ,						UpperCamelCase     ,						UpperCamelCase) ->	int:
										if pipeline_test_casse_name == "ZeroShotClassificationPipelineTests":
													# Get `tokenizer does not have a padding token` error for both fast/slow tokenizers.
													# `OpenAIGPTConfig` 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) ->	Dict:
										UpperCamelCase__    :		Optional[int]     =				TFOpenAIGPTModelTester(self)
										UpperCamelCase__    :		Optional[int]     =				ConfigTester(self     ,						config_class=UpperCamelCase     ,						n_embd=37)
							def        lowerCAmelCase__ (				self) ->	Tuple:
										self.config_tester.run_common_tests()
							def        lowerCAmelCase__ (				self) ->	List[str]:
										UpperCamelCase__    :		Dict     =				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_model(*UpperCamelCase)
							def        lowerCAmelCase__ (				self) ->	List[str]:
										UpperCamelCase__    :		Any     =				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_lm_head(*UpperCamelCase)
							def        lowerCAmelCase__ (				self) ->	List[Any]:
										UpperCamelCase__    :		Tuple     =				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_double_head(*UpperCamelCase)
							def        lowerCAmelCase__ (				self) ->	Union[str, Any]:
										UpperCamelCase__    :		Optional[Any]     =				self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*UpperCamelCase)






							@slow
							def        lowerCAmelCase__ (				self) ->	Dict:
										for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
													UpperCamelCase__    :		Tuple     =				TFOpenAIGPTModel.from_pretrained(UpperCamelCase)
													self.assertIsNotNone(UpperCamelCase)



@require_tf
class 			UpperCamelCase_			(						unittest.TestCase   ):




							'''simple docstring'''

							@slow
							def        lowerCAmelCase__ (				self) ->	List[str]:
										UpperCamelCase__    :		Union[str, Any]     =				TFOpenAIGPTLMHeadModel.from_pretrained('openai-gpt')
										UpperCamelCase__    :		Any     =				tf.convert_to_tensor([[4_81, 47_35, 5_44]]     ,						dtype=tf.intaa)  # the president is
										UpperCamelCase__    :		Dict     =				[
										    4_81,
										    47_35,
										    5_44,
										    2_46,
										    9_63,
										    8_70,
										    7_62,
										    2_39,
										    2_44,
										    4_04_77,
										    2_44,
										    2_49,
										    7_19,
										    8_81,
										    4_87,
										    5_44,
										    2_40,
										    2_44,
										    6_03,
										    4_81,
										]  # the president is a very good man. " \n " i\'m sure he is, " said the

										UpperCamelCase__    :		Tuple     =				model.generate(UpperCamelCase     ,						do_sample=UpperCamelCase)
										self.assertListEqual(output_ids[0].numpy().tolist()     ,						UpperCamelCase)





import enum
import inspect
from typing import Iterable, List, Optional, Tuple, Union



class 			UpperCamelCase_			(						enum.Enum   ):




							'''simple docstring'''

							UpperCamelCase_					   =							"""timm"""
							UpperCamelCase_					   =							"""transformers"""



def       _lowercase					(      __SCREAMING_SNAKE_CASE				,    __SCREAMING_SNAKE_CASE				,    __SCREAMING_SNAKE_CASE )	->     Dict:
			if stage_names is None:
						raise ValueError('Stage_names must be set for transformers backbones' )

			if out_features is not None:
						if not isinstance(__SCREAMING_SNAKE_CASE				,    (list,) ):
									raise ValueError(F"""out_features must be a list {type(__SCREAMING_SNAKE_CASE )}""" )
						if any(feat not in stage_names for feat in out_features ):
									raise ValueError(F"""out_features must be a subset of stage_names: {stage_names} got {out_features}""" )

			if out_indices is not None:
						if not isinstance(__SCREAMING_SNAKE_CASE				,    (list, tuple) ):
									raise ValueError(F"""out_indices must be a list or tuple, got {type(__SCREAMING_SNAKE_CASE )}""" )
						if any(idx >= len(__SCREAMING_SNAKE_CASE ) for idx in out_indices ):
									raise ValueError('out_indices must be valid indices for stage_names {stage_names}, got {out_indices}' )

			if out_features is not None and out_indices is not None:
						if len(__SCREAMING_SNAKE_CASE ) != len(__SCREAMING_SNAKE_CASE ):
									raise ValueError('out_features and out_indices should have the same length if both are set' )
						if out_features != [stage_names[idx] for idx in out_indices]:
									raise ValueError('out_features and out_indices should correspond to the same stages if both are set' )



def       _lowercase					(      __SCREAMING_SNAKE_CASE				,    __SCREAMING_SNAKE_CASE				,    __SCREAMING_SNAKE_CASE				,    )	->     List[Any]:
			if out_indices is None and out_features is None:
						UpperCamelCase__    :		List[Any]     =				[len(__SCREAMING_SNAKE_CASE ) - 1]
						UpperCamelCase__    :		Tuple     =				[stage_names[-1]]
			elif out_indices is None and out_features is not None:
						UpperCamelCase__    :		str     =				[stage_names.index(__SCREAMING_SNAKE_CASE ) for layer in out_features]
			elif out_features is None and out_indices is not None:
						UpperCamelCase__    :		int     =				[stage_names[idx] for idx in out_indices]
			return out_features, out_indices



def       _lowercase					(      __SCREAMING_SNAKE_CASE				,    __SCREAMING_SNAKE_CASE				,    __SCREAMING_SNAKE_CASE				,    )	->     Tuple[List[str], List[int]]:
			verify_out_features_out_indices(out_features=__SCREAMING_SNAKE_CASE				,    out_indices=__SCREAMING_SNAKE_CASE				,    stage_names=__SCREAMING_SNAKE_CASE )
			UpperCamelCase__   ,			UpperCamelCase__    :		str     =				_align_output_features_output_indices(
			    out_features=__SCREAMING_SNAKE_CASE				,    out_indices=__SCREAMING_SNAKE_CASE				,    stage_names=__SCREAMING_SNAKE_CASE )
			# Verify that the aligned out_features and out_indices are valid
			verify_out_features_out_indices(out_features=__SCREAMING_SNAKE_CASE				,    out_indices=__SCREAMING_SNAKE_CASE				,    stage_names=__SCREAMING_SNAKE_CASE )
			return output_features, output_indices



class 			UpperCamelCase_			:




							'''simple docstring'''

							UpperCamelCase_					   =							None
							def        lowerCAmelCase__ (				self     ,						UpperCamelCase) ->	None:
										if getattr(self     ,						'_backbone'     ,						UpperCamelCase) is None:
													raise ValueError('self._backbone must be set before calling _init_timm_backbone')

										# These will diagree with the defaults for the transformers models e.g. for resnet50
										# the transformer model has out_features = ['stem', 'stage1', 'stage2', 'stage3', 'stage4']
										# the timm model has out_features = ['act', 'layer1', 'layer2', 'layer3', 'layer4']
										UpperCamelCase__    :		int     =				[stage['module'] for stage in self._backbone.feature_info.info]
										UpperCamelCase__    :		str     =				[stage['num_chs'] for stage in self._backbone.feature_info.info]
										UpperCamelCase__    :		Optional[int]     =				self._backbone.feature_info.out_indices
										UpperCamelCase__    :		Optional[int]     =				self._backbone.feature_info.module_name()

										# We verify the out indices and out features are valid
										verify_out_features_out_indices(
										    out_features=UpperCamelCase     ,						out_indices=UpperCamelCase     ,						stage_names=self.stage_names)
										UpperCamelCase__   ,			UpperCamelCase__    :		Optional[int]     =				out_features, out_indices
							def        lowerCAmelCase__ (				self     ,						UpperCamelCase) ->	None:
										UpperCamelCase__    :		List[str]     =				getattr(UpperCamelCase     ,						'stage_names')
										UpperCamelCase__    :		List[str]     =				getattr(UpperCamelCase     ,						'out_features'     ,						UpperCamelCase)
										UpperCamelCase__    :		Optional[Any]     =				getattr(UpperCamelCase     ,						'out_indices'     ,						UpperCamelCase)

										UpperCamelCase__    :		Tuple     =				stage_names
										UpperCamelCase__   ,			UpperCamelCase__    :		Optional[Any]     =				get_aligned_output_features_output_indices(
										    out_features=UpperCamelCase     ,						out_indices=UpperCamelCase     ,						stage_names=UpperCamelCase)
										# Number of channels for each stage. This is set in the transformer backbone model init
										UpperCamelCase__    :		Dict     =				None
							def        lowerCAmelCase__ (				self     ,						UpperCamelCase) ->	None:
										UpperCamelCase__    :		List[Any]     =				config

										UpperCamelCase__    :		Dict     =				getattr(UpperCamelCase     ,						'use_timm_backbone'     ,						UpperCamelCase)
										UpperCamelCase__    :		List[str]     =				BackboneType.TIMM if self.use_timm_backbone else BackboneType.TRANSFORMERS

										if self.backbone_type == BackboneType.TIMM:
													self._init_timm_backbone(UpperCamelCase)
										elif self.backbone_type == BackboneType.TRANSFORMERS:
													self._init_transformers_backbone(UpperCamelCase)
										else:
													raise ValueError(F"""backbone_type {self.backbone_type} not supported.""")
							@property
							def        lowerCAmelCase__ (				self) ->	List[str]:
										return self._out_features
							@out_features.setter
							def        lowerCAmelCase__ (				self     ,						UpperCamelCase) ->	int:
										UpperCamelCase__   ,			UpperCamelCase__    :		str     =				get_aligned_output_features_output_indices(
										    out_features=UpperCamelCase     ,						out_indices=UpperCamelCase     ,						stage_names=self.stage_names)
							@property
							def        lowerCAmelCase__ (				self) ->	List[Any]:
										return self._out_indices
							@out_indices.setter
							def        lowerCAmelCase__ (				self     ,						UpperCamelCase) ->	int:
										UpperCamelCase__   ,			UpperCamelCase__    :		Optional[int]     =				get_aligned_output_features_output_indices(
										    out_features=UpperCamelCase     ,						out_indices=UpperCamelCase     ,						stage_names=self.stage_names)
							@property
							def        lowerCAmelCase__ (				self) ->	List[str]:
										# the current backbones will output the number of channels for each stage
										# even if that stage is not in the out_features list.
										return {stage: self.num_features[i] for i, stage in enumerate(self.stage_names)}
							@property
							def        lowerCAmelCase__ (				self) ->	Union[str, Any]:
										return [self.out_feature_channels[name] for name in self.out_features]
							def        lowerCAmelCase__ (				self     ,						*UpperCamelCase     ,						**UpperCamelCase) ->	str:
										UpperCamelCase__    :		List[Any]     =				dict(inspect.signature(self.forward).parameters)
										UpperCamelCase__    :		Any     =				{k: v for k, v in kwargs.items() if k in signature}
										return self(*UpperCamelCase     ,						**UpperCamelCase)
							def        lowerCAmelCase__ (				self     ,						UpperCamelCase     ,						UpperCamelCase = None     ,						UpperCamelCase = None     ,						UpperCamelCase = None     ,						) ->	Union[str, Any]:
										raise NotImplementedError('This method should be implemented by the derived class.')






							def        lowerCAmelCase__ (				self) ->	int:
										UpperCamelCase__    :		Any     =				super().to_dict()
										UpperCamelCase__    :		Optional[Any]     =				output.pop('_out_features')
										UpperCamelCase__    :		Any     =				output.pop('_out_indices')
										return output



class 			UpperCamelCase_			:




							'''simple docstring'''

							@property
							def        lowerCAmelCase__ (				self) ->	Tuple:
										return self._out_features
							@out_features.setter
							def        lowerCAmelCase__ (				self     ,						UpperCamelCase) ->	Tuple:
										UpperCamelCase__   ,			UpperCamelCase__    :		Union[str, Any]     =				get_aligned_output_features_output_indices(
										    out_features=UpperCamelCase     ,						out_indices=UpperCamelCase     ,						stage_names=self.stage_names)
							@property
							def        lowerCAmelCase__ (				self) ->	Optional[int]:
										return self._out_indices
							@out_indices.setter
							def        lowerCAmelCase__ (				self     ,						UpperCamelCase) ->	int:
										UpperCamelCase__   ,			UpperCamelCase__    :		str     =				get_aligned_output_features_output_indices(
										    out_features=UpperCamelCase     ,						out_indices=UpperCamelCase     ,						stage_names=self.stage_names)






							def        lowerCAmelCase__ (				self) ->	Tuple:
										UpperCamelCase__    :		List[Any]     =				super().to_dict()
										UpperCamelCase__    :		Optional[int]     =				output.pop('_out_features')
										UpperCamelCase__    :		Tuple     =				output.pop('_out_indices')
										return output



