




"""simple docstring"""

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







				def __init__(							self				:   Optional[int] ,       _lowerCamelCase				:   int ,       ):



					"""simple docstring"""

					A_					:			Union[str, Any]						=						parent
					A_					:			Tuple						=						13
					A_					:			int						=						7
					A_					:			Any						=						True
					A_					:			Tuple						=						True
					A_					:			int						=						True
					A_					:			List[Any]						=						True
					A_					:			int						=						True
					A_					:			List[Any]						=						99
					A_					:			Dict						=						32
					A_					:			Any						=						2
					A_					:			Optional[int]						=						4
					A_					:			Dict						=						37
					A_					:			Union[str, Any]						=						'''gelu'''
					A_					:			Tuple						=						0.1
					A_					:			List[Any]						=						0.1
					A_					:			List[str]						=						5_12
					A_					:			Tuple						=						16
					A_					:			Tuple						=						2
					A_					:			str						=						0.02
					A_					:			Any						=						3
					A_					:			Tuple						=						4
					A_					:			Tuple						=						None
					A_					:			Dict						=						self.vocab_size - 1







				def    a_					(							self				:   Optional[int]    ):



					"""simple docstring"""

					A_					:			Any						=						ids_tensor([self.batch_size, self.seq_length] ,       self.vocab_size    )

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

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

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

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

					A_					:			Tuple						=						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 ,       )

					A_					:			List[str]						=						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				:   Union[str, Any] ,       _lowerCamelCase				:   List[Any] ,       _lowerCamelCase				:   Dict ,       _lowerCamelCase				:   Optional[Any] ,       _lowerCamelCase				:   str ,       _lowerCamelCase				:   Dict ,       *_lowerCamelCase				:   Union[str, Any]    ):



					"""simple docstring"""

					A_					:			Any						=						TFOpenAIGPTModel(config=__snake_case    )
					A_					:			Union[str, Any]						=						{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
					A_					:			List[Any]						=						model(__snake_case    )

					A_					:			int						=						[input_ids, input_mask]
					A_					:			Dict						=						model(__snake_case    )

					A_					:			Tuple						=						model(__snake_case    )

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







				def    a_					(							self				:   Optional[Any] ,       _lowerCamelCase				:   str ,       _lowerCamelCase				:   str ,       _lowerCamelCase				:   Union[str, Any] ,       _lowerCamelCase				:   Optional[int] ,       _lowerCamelCase				:   Tuple ,       *_lowerCamelCase				:   List[Any]    ):



					"""simple docstring"""

					A_					:			Optional[Any]						=						TFOpenAIGPTLMHeadModel(config=__snake_case    )
					A_					:			Dict						=						{'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
					A_					:			Any						=						model(__snake_case    )
					self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.seq_length, self.vocab_size)    )







				def    a_					(							self				:   Union[str, Any] ,       _lowerCamelCase				:   Union[str, Any] ,       _lowerCamelCase				:   str ,       _lowerCamelCase				:   str ,       _lowerCamelCase				:   Union[str, Any] ,       _lowerCamelCase				:   str ,       _lowerCamelCase				:   List[Any] ,       *_lowerCamelCase				:   Optional[Any]    ):



					"""simple docstring"""

					A_					:			List[str]						=						TFOpenAIGPTDoubleHeadsModel(config=__snake_case    )

					A_					:			str						=						tf.tile(tf.expand_dims(__snake_case ,       1    ) ,       (1, self.num_choices, 1)    )
					A_					:			Any						=						tf.tile(tf.expand_dims(__snake_case ,       1    ) ,       (1, self.num_choices, 1)    )
					A_					:			List[str]						=						tf.tile(tf.expand_dims(__snake_case ,       1    ) ,       (1, self.num_choices, 1)    )

					A_					:			Optional[Any]						=						{
					    '''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,
					}
					A_					:			int						=						model(__snake_case    )
					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    a_					(							self				:   str ,       _lowerCamelCase				:   Dict ,       _lowerCamelCase				:   Dict ,       _lowerCamelCase				:   Optional[Any] ,       _lowerCamelCase				:   Tuple ,       _lowerCamelCase				:   Any ,       *_lowerCamelCase				:   Optional[int]    ):



					"""simple docstring"""

					A_					:			Dict						=						self.num_labels
					A_					:			Optional[int]						=						ids_tensor([self.batch_size] ,       self.type_sequence_label_size    )
					A_					:			Optional[Any]						=						{
					    '''input_ids''': input_ids,
					    '''attention_mask''': input_mask,
					    '''token_type_ids''': token_type_ids,
					    '''labels''': sequence_labels,
					}
					A_					:			Optional[int]						=						TFOpenAIGPTForSequenceClassification(__snake_case    )
					A_					:			Tuple						=						model(__snake_case    )
					self.parent.assertEqual(result.logits.shape ,       (self.batch_size, self.num_labels)    )







				def    a_					(							self				:   int    ):



					"""simple docstring"""

					A_					:			List[str]						=						self.prepare_config_and_inputs()

					(
					    A_		
					)					:			int						=						config_and_inputs

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

@require_tf
class     lowercase (      A__       ,		A__       ,		unittest.TestCase):
				__lowerCAmelCase      :      Dict					    =				(
				    (TFOpenAIGPTModel, TFOpenAIGPTLMHeadModel, TFOpenAIGPTDoubleHeadsModel, TFOpenAIGPTForSequenceClassification)
				    if is_tf_available()
				    else ()
				)
				__lowerCAmelCase      :      Optional[Any]					    =				(
				    (TFOpenAIGPTLMHeadModel,) if is_tf_available() else ()
				)  # TODO (PVP): Add Double HeadsModel when generate() function is changed accordingly
				__lowerCAmelCase      :      Any					    =				(
				    {
				        """feature-extraction""": TFOpenAIGPTModel,
				        """text-classification""": TFOpenAIGPTForSequenceClassification,
				        """text-generation""": TFOpenAIGPTLMHeadModel,
				        """zero-shot""": TFOpenAIGPTForSequenceClassification,
				    }
				    if is_tf_available()
				    else {}
				)
				__lowerCAmelCase      :      Dict					    =				False
				__lowerCAmelCase      :      Union[str, Any]					    =				False







				def    a_					(							self				:   Any ,       _lowerCamelCase				:   int ,       _lowerCamelCase				:   Tuple ,       _lowerCamelCase				:   Optional[int] ,       _lowerCamelCase				:   List[Any] ,       _lowerCamelCase				:   List[Any]    ):



					"""simple docstring"""

					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    a_					(							self				:   Tuple    ):



					"""simple docstring"""

					A_					:			Tuple						=						TFOpenAIGPTModelTester(self    )
					A_					:			str						=						ConfigTester(self ,       config_class=__snake_case ,       n_embd=37    )







				def    a_					(							self				:   Optional[int]    ):



					"""simple docstring"""

					self.config_tester.run_common_tests()







				def    a_					(							self				:   Dict    ):



					"""simple docstring"""

					A_					:			Optional[Any]						=						self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_model(*__snake_case    )







				def    a_					(							self				:   Optional[Any]    ):



					"""simple docstring"""

					A_					:			Union[str, Any]						=						self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_lm_head(*__snake_case    )







				def    a_					(							self				:   Dict    ):



					"""simple docstring"""

					A_					:			Tuple						=						self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_double_head(*__snake_case    )







				def    a_					(							self				:   Optional[int]    ):



					"""simple docstring"""

					A_					:			Tuple						=						self.model_tester.prepare_config_and_inputs()
					self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*__snake_case    )







				@slow
				def    a_					(							self				:   str    ):



					"""simple docstring"""

					for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
						A_					:			Optional[int]						=						TFOpenAIGPTModel.from_pretrained(__snake_case    )
						self.assertIsNotNone(__snake_case    )

@require_tf
class     lowercase (      unittest.TestCase):







				@slow
				def    a_					(							self				:   Optional[Any]    ):



					"""simple docstring"""

					A_					:			Optional[int]						=						TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''    )
					A_					:			Dict						=						tf.convert_to_tensor([[4_81, 47_35, 5_44]] ,       dtype=tf.intaa    )  # the president is
					A_					:			Union[str, Any]						=						[
					    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

					A_					:			List[Any]						=						model.generate(__snake_case ,       do_sample=__snake_case    )
					self.assertListEqual(output_ids[0].numpy().tolist() ,       __snake_case    )











"""simple docstring"""

from .chunk_utils import chunk_layer
from .data_transforms import make_atomaa_masks
from .feats import atomaa_to_atomaa, frames_and_literature_positions_to_atomaa_pos, torsion_angles_to_frames
from .loss import compute_predicted_aligned_error, compute_tm
from .protein import Protein as OFProtein
from .protein import to_pdb
from .rigid_utils import Rigid, Rotation
from .tensor_utils import dict_multimap, flatten_final_dims, permute_final_dims





