





"""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 					_a		:






				def __init__(					self						:       Tuple,					lowerCAmelCase__						:       List[str],					)      -> Dict:




										'''simple docstring'''



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






				def 							snake_case      (					self						:       Union[str, Any]				)      -> Tuple:




										'''simple docstring'''



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

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

										_UpperCamelCase     :   int      =       None
										if self.use_token_type_ids:
																_UpperCamelCase     :   Union[str, Any]      =       ids_tensor([self.batch_size, self.seq_length],					self.type_vocab_size				)

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

										_UpperCamelCase     :   int      =       None
										_UpperCamelCase     :   Optional[Any]      =       None
										_UpperCamelCase     :   str      =       None
										if self.use_labels:
																_UpperCamelCase     :   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     :   str      =       ids_tensor([self.batch_size],					self.num_choices				)

										_UpperCamelCase     :   List[str]      =       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     :   int      =       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 							snake_case      (					self						:       Any,					lowerCAmelCase__						:       Any,					lowerCAmelCase__						:       Dict,					lowerCAmelCase__						:       Union[str, Any],					lowerCAmelCase__						:       List[Any],					lowerCAmelCase__						:       Any,					*lowerCAmelCase__						:       Dict				)      -> Any:




										'''simple docstring'''



										_UpperCamelCase     :   Dict      =       TFOpenAIGPTModel(config=lowerCAmelCase__				)
										_UpperCamelCase     :   Any      =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
										_UpperCamelCase     :   Optional[int]      =       model(lowerCAmelCase__				)

										_UpperCamelCase     :   str      =       [input_ids, input_mask]
										_UpperCamelCase     :   Union[str, Any]      =       model(lowerCAmelCase__				)

										_UpperCamelCase     :   Any      =       model(lowerCAmelCase__				)

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






				def 							snake_case      (					self						:       Any,					lowerCAmelCase__						:       List[Any],					lowerCAmelCase__						:       str,					lowerCAmelCase__						:       Dict,					lowerCAmelCase__						:       int,					lowerCAmelCase__						:       List[str],					*lowerCAmelCase__						:       List[str]				)      -> Optional[Any]:




										'''simple docstring'''



										_UpperCamelCase     :   int      =       TFOpenAIGPTLMHeadModel(config=lowerCAmelCase__				)
										_UpperCamelCase     :   Dict      =       {'''input_ids''': input_ids, '''attention_mask''': input_mask, '''token_type_ids''': token_type_ids}
										_UpperCamelCase     :   Any      =       model(lowerCAmelCase__				)
										self.parent.assertEqual(result.logits.shape,					(self.batch_size, self.seq_length, self.vocab_size)				)






				def 							snake_case      (					self						:       Tuple,					lowerCAmelCase__						:       Optional[int],					lowerCAmelCase__						:       List[Any],					lowerCAmelCase__						:       int,					lowerCAmelCase__						:       int,					lowerCAmelCase__						:       Tuple,					lowerCAmelCase__						:       Union[str, Any],					*lowerCAmelCase__						:       int				)      -> List[Any]:




										'''simple docstring'''



										_UpperCamelCase     :   Optional[int]      =       TFOpenAIGPTDoubleHeadsModel(config=lowerCAmelCase__				)

										_UpperCamelCase     :   Any      =       tf.tile(tf.expand_dims(lowerCAmelCase__,					1				),					(1, self.num_choices, 1)				)
										_UpperCamelCase     :   Union[str, Any]      =       tf.tile(tf.expand_dims(lowerCAmelCase__,					1				),					(1, self.num_choices, 1)				)
										_UpperCamelCase     :   Any      =       tf.tile(tf.expand_dims(lowerCAmelCase__,					1				),					(1, self.num_choices, 1)				)

										_UpperCamelCase     :   int      =       {
										    '''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     :   List[str]      =       model(lowerCAmelCase__				)
										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 							snake_case      (					self						:       Any,					lowerCAmelCase__						:       List[str],					lowerCAmelCase__						:       Dict,					lowerCAmelCase__						:       Optional[Any],					lowerCAmelCase__						:       List[str],					lowerCAmelCase__						:       Union[str, Any],					*lowerCAmelCase__						:       Tuple				)      -> str:




										'''simple docstring'''



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






				def 							snake_case      (					self						:       Optional[int]				)      -> List[Any]:




										'''simple docstring'''



										_UpperCamelCase     :   int      =       self.prepare_config_and_inputs()

										(
										    (
										    _UpperCamelCase
										)  ,				(
										    _UpperCamelCase
										)  ,				(
										    _UpperCamelCase
										)  ,				(
										    _UpperCamelCase
										)  ,				(
										    _UpperCamelCase
										)  ,				(
										    _UpperCamelCase
										)  ,				(
										    _UpperCamelCase
										)  ,				(
										    _UpperCamelCase
										)  ,				(
										    _UpperCamelCase
										)  ,				
										)     :   List[Any]      =       config_and_inputs

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




@require_tf
class 					_a		( _lowerCAmelCase      ,							_lowerCAmelCase      ,							unittest.TestCase							):
				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 							snake_case      (					self						:       Optional[int],					lowerCAmelCase__						:       Union[str, Any],					lowerCAmelCase__						:       Optional[Any],					lowerCAmelCase__						:       List[Any],					lowerCAmelCase__						:       Any,					lowerCAmelCase__						:       Tuple				)      -> Tuple:




										'''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 							snake_case      (					self						:       Tuple				)      -> Dict:




										'''simple docstring'''



										_UpperCamelCase     :   List[Any]      =       TFOpenAIGPTModelTester(self				)
										_UpperCamelCase     :   Any      =       ConfigTester(self,					config_class=lowerCAmelCase__,					n_embd=3_7				)






				def 							snake_case      (					self						:       int				)      -> Tuple:




										'''simple docstring'''



										self.config_tester.run_common_tests()






				def 							snake_case      (					self						:       Tuple				)      -> int:




										'''simple docstring'''



										_UpperCamelCase     :   int      =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_model(*lowerCAmelCase__				)






				def 							snake_case      (					self						:       str				)      -> Optional[int]:




										'''simple docstring'''



										_UpperCamelCase     :   int      =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_lm_head(*lowerCAmelCase__				)






				def 							snake_case      (					self						:       str				)      -> Optional[int]:




										'''simple docstring'''



										_UpperCamelCase     :   List[str]      =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_double_head(*lowerCAmelCase__				)






				def 							snake_case      (					self						:       List[str]				)      -> Union[str, Any]:




										'''simple docstring'''



										_UpperCamelCase     :   List[Any]      =       self.model_tester.prepare_config_and_inputs()
										self.model_tester.create_and_check_openai_gpt_for_sequence_classification(*lowerCAmelCase__				)






				@slow
				def 							snake_case      (					self						:       Union[str, Any]				)      -> Any:




										'''simple docstring'''



										for model_name in TF_OPENAI_GPT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
																_UpperCamelCase     :   str      =       TFOpenAIGPTModel.from_pretrained(lowerCAmelCase__				)
																self.assertIsNotNone(lowerCAmelCase__				)




@require_tf
class 					_a		( unittest.TestCase							):






				@slow
				def 							snake_case      (					self						:       Optional[Any]				)      -> List[Any]:




										'''simple docstring'''



										_UpperCamelCase     :   Any      =       TFOpenAIGPTLMHeadModel.from_pretrained('''openai-gpt'''				)
										_UpperCamelCase     :   Tuple      =       tf.convert_to_tensor([[4_8_1, 4_7_3_5, 5_4_4]],					dtype=tf.intaa				)  # the president is
										_UpperCamelCase     :   str      =       [
										    4_8_1,
										    4_7_3_5,
										    5_4_4,
										    2_4_6,
										    9_6_3,
										    8_7_0,
										    7_6_2,
										    2_3_9,
										    2_4_4,
										    4_0_4_7_7,
										    2_4_4,
										    2_4_9,
										    7_1_9,
										    8_8_1,
										    4_8_7,
										    5_4_4,
										    2_4_0,
										    2_4_4,
										    6_0_3,
										    4_8_1,
										]  # the president is a very good man. " \n " i\'m sure he is, " said the

										_UpperCamelCase     :   Any      =       model.generate(lowerCAmelCase__,					do_sample=lowerCAmelCase__				)
										self.assertListEqual(output_ids[0].numpy().tolist(),					lowerCAmelCase__				)










from typing import Any, Dict, List, Mapping

import numpy as np
import torch

from ...utils import is_cython_available, requires_backends


if is_cython_available():
				import pyximport

				pyximport.install(setup_args={"""include_dirs""": np.get_include()})
				from . import algos_graphormer  # noqa E402




def  A_(      A					,   A = 512 ):
	UpperCAmelCase_		    =  x.shape[1] if len(x.shape ) > 1 else 1
	UpperCAmelCase_		    =  1 + np.arange(0					,   feature_num * offset					,   A					,   dtype=np.intaa )
	UpperCAmelCase_		    =  x + feature_offset
	return x




def  A_(      A					,   A=True ):
	requires_backends(A					,   ["""cython"""] )

	if keep_features and "edge_attr" in item.keys():  # edge_attr
		UpperCAmelCase_		    =  np.asarray(item["""edge_attr"""]					,   dtype=np.intaa )
	else:
		UpperCAmelCase_		    =  np.ones((len(item["""edge_index"""][0] ), 1)					,   dtype=np.intaa )  # same embedding for all

	if keep_features and "node_feat" in item.keys():  # input_nodes
		UpperCAmelCase_		    =  np.asarray(item["""node_feat"""]					,   dtype=np.intaa )
	else:
		UpperCAmelCase_		    =  np.ones((item["""num_nodes"""], 1)					,   dtype=np.intaa )  # same embedding for all

	UpperCAmelCase_		    =  np.asarray(item["""edge_index"""]					,   dtype=np.intaa )

	UpperCAmelCase_		    =  convert_to_single_emb(A ) + 1
	UpperCAmelCase_		    =  item["""num_nodes"""]

	if len(edge_attr.shape ) == 1:
		UpperCAmelCase_		    =  edge_attr[:, None]
	UpperCAmelCase_		    =  np.zeros([num_nodes, num_nodes, edge_attr.shape[-1]]					,   dtype=np.intaa )
	UpperCAmelCase_		    =  convert_to_single_emb(A ) + 1

	# node adj matrix [num_nodes, num_nodes] bool
	UpperCAmelCase_		    =  np.zeros([num_nodes, num_nodes]					,   dtype=A )
	UpperCAmelCase_		    =  True

	UpperCAmelCase_    ,						UpperCAmelCase_		    =  algos_graphormer.floyd_warshall(A )
	UpperCAmelCase_		    =  np.amax(A )

	UpperCAmelCase_		    =  algos_graphormer.gen_edge_input(A					,   A					,   A )
	UpperCAmelCase_		    =  np.zeros([num_nodes + 1, num_nodes + 1]					,   dtype=np.single )  # with graph token

	# combine
	UpperCAmelCase_		    =  input_nodes + 1  # we shift all indices by one for padding
	UpperCAmelCase_		    =  attn_bias
	UpperCAmelCase_		    =  attn_edge_type
	UpperCAmelCase_		    =  shortest_path_result.astype(np.intaa ) + 1  # we shift all indices by one for padding
	UpperCAmelCase_		    =  np.sum(A					,   axis=1 ).reshape(-1 ) + 1  # we shift all indices by one for padding
	UpperCAmelCase_		    =  item["""in_degree"""]  # for undirected graph
	UpperCAmelCase_		    =  input_edges + 1  # we shift all indices by one for padding
	if "labels" not in item:
		UpperCAmelCase_		    =  item["""y"""]

	return item







class 	_UpperCamelCase	:







					'''simple docstring'''

					def __init__(    self     :	str			,       __lowercase     :	int=20			,       __lowercase     :	Union[str, Any]=False		):






						'''simple docstring'''







						if not is_cython_available():
							raise ImportError("""Graphormer preprocessing needs Cython (pyximport)"""		)

						UpperCAmelCase_		    =  spatial_pos_max
						UpperCAmelCase_		    =  on_the_fly_processing





					def __call__(    self     :	List[str]			,       __lowercase     :	List[dict]		):






						'''simple docstring'''







						if self.on_the_fly_processing:
							UpperCAmelCase_		    =  [preprocess_item(__lowercase		) for i in features]

						if not isinstance(features[0]			,       __lowercase		):
							UpperCAmelCase_		    =  [vars(__lowercase		) for f in features]
						UpperCAmelCase_		    =  {}

						UpperCAmelCase_		    =  max(len(i["""input_nodes"""]		) for i in features		)
						UpperCAmelCase_		    =  len(features[0]["""input_nodes"""][0]		)
						UpperCAmelCase_		    =  len(features[0]["""attn_edge_type"""][0][0]		)
						UpperCAmelCase_		    =  max(len(i["""input_edges"""][0][0]		) for i in features		)
						UpperCAmelCase_		    =  len(features[0]["""input_edges"""][0][0][0]		)
						UpperCAmelCase_		    =  len(__lowercase		)

						UpperCAmelCase_		    =  torch.zeros(__lowercase			,       max_node_num + 1			,       max_node_num + 1			,       dtype=torch.float		)
						UpperCAmelCase_		    =  torch.zeros(__lowercase			,       __lowercase			,       __lowercase			,       __lowercase			,       dtype=torch.long		)
						UpperCAmelCase_		    =  torch.zeros(__lowercase			,       __lowercase			,       __lowercase			,       dtype=torch.long		)
						UpperCAmelCase_		    =  torch.zeros(__lowercase			,       __lowercase			,       dtype=torch.long		)
						UpperCAmelCase_		    =  torch.zeros(__lowercase			,       __lowercase			,       __lowercase			,       dtype=torch.long		)
						UpperCAmelCase_		    =  torch.zeros(
						    __lowercase			,       __lowercase			,       __lowercase			,       __lowercase			,       __lowercase			,       dtype=torch.long		)

						for ix, f in enumerate(__lowercase		):
							for k in ["attn_bias", "attn_edge_type", "spatial_pos", "in_degree", "input_nodes", "input_edges"]:
								UpperCAmelCase_		    =  torch.tensor(f[k]		)

							if len(f["""attn_bias"""][1:, 1:][f["""spatial_pos"""] >= self.spatial_pos_max]		) > 0:
								UpperCAmelCase_		    =  float("""-inf"""		)

							UpperCAmelCase_		    =  f["""attn_bias"""]
							UpperCAmelCase_		    =  f[
							    """attn_edge_type"""
							]
							UpperCAmelCase_		    =  f["""spatial_pos"""]
							UpperCAmelCase_		    =  f["""in_degree"""]
							UpperCAmelCase_		    =  f["""input_nodes"""]
							UpperCAmelCase_		    =  f["""input_edges"""]

						UpperCAmelCase_		    =  batch["""in_degree"""]

						UpperCAmelCase_		    =  features[0]["""labels"""]
						if len(__lowercase		) == 1:  # one task
							if isinstance(sample[0]			,       __lowercase		):  # regression
								UpperCAmelCase_		    =  torch.from_numpy(np.concatenate([i["""labels"""] for i in features]		)		)
							else:  # binary classification
								UpperCAmelCase_		    =  torch.from_numpy(np.concatenate([i["""labels"""] for i in features]		)		)
						else:  # multi task classification, left to float to keep the NaNs
							UpperCAmelCase_		    =  torch.from_numpy(np.stack([i["""labels"""] for i in features]			,       axis=0		)		)

						return batch


