




from __future__ import annotations

import copy
import os
import tempfile
from importlib import import_module
from math import isnan

from transformers import is_tf_available
from transformers.models.auto import get_values
from transformers.testing_utils import _tf_gpu_memory_limit, require_tf, slow

from ..test_modeling_tf_common import ids_tensor


if is_tf_available():
		import numpy as np
		import tensorflow as tf

		from transformers import (
		    TF_MODEL_FOR_CAUSAL_LM_MAPPING,
		    TF_MODEL_FOR_IMAGE_CLASSIFICATION_MAPPING,
		    TF_MODEL_FOR_MASKED_LM_MAPPING,
		    TF_MODEL_FOR_MULTIPLE_CHOICE_MAPPING,
		    TF_MODEL_FOR_NEXT_SENTENCE_PREDICTION_MAPPING,
		    TF_MODEL_FOR_PRETRAINING_MAPPING,
		    TF_MODEL_FOR_QUESTION_ANSWERING_MAPPING,
		    TF_MODEL_FOR_SEQ_TO_SEQ_CAUSAL_LM_MAPPING,
		    TF_MODEL_FOR_SEQUENCE_CLASSIFICATION_MAPPING,
		    TF_MODEL_FOR_TOKEN_CLASSIFICATION_MAPPING,
		    TFSharedEmbeddings,
		)

		if _tf_gpu_memory_limit is not None:
				__UpperCamelCase      : str					      =   tf.config.list_physical_devices("GPU")
				for gpu in gpus:
						# Restrict TensorFlow to only allocate x GB of memory on the GPUs
						try:
								tf.config.set_logical_device_configuration(
								    gpu, [tf.config.LogicalDeviceConfiguration(memory_limit=_tf_gpu_memory_limit)]
								)
								__UpperCamelCase      : Dict					      =   tf.config.list_logical_devices("GPU")
								print("Logical GPUs", logical_gpus)
						except RuntimeError as e:
								# Virtual devices must be set before GPUs have been initialized
								print(e)

@require_tf
class 	__lowerCAmelCase			:
							UpperCamelCase__        =						None
							UpperCamelCase__        =						()
							UpperCamelCase__        =						()
							UpperCamelCase__        =						True
							UpperCamelCase__        =						True
							UpperCamelCase__        =						True
							UpperCamelCase__        =						False
							def    lowerCamelCase__							(		self		:Tuple	,   __magic_name__		:Union[str, Any]	,   __magic_name__		:Any	,   __magic_name__		:Tuple=False       ):







								'''simple docstring'''







								a		       =							copy.deepcopy(__magic_name__       )

								if model_class in get_values(__magic_name__       ):
									a		       =							{
									    k: tf.tile(tf.expand_dims(__magic_name__	,   1       )	,   (1, self.model_tester.num_choices) + (1,) * (v.ndim - 1)       )
									    if isinstance(__magic_name__	,   tf.Tensor       ) and v.ndim > 0
									    else v
									    for k, v in inputs_dict.items()
									}

								if return_labels:
									if model_class in get_values(__magic_name__       ):
										a		       =							tf.ones(self.model_tester.batch_size	,   dtype=tf.intaa       )
									elif model_class in get_values(__magic_name__       ):
										a		       =							tf.zeros(self.model_tester.batch_size	,   dtype=tf.intaa       )
										a		       =							tf.zeros(self.model_tester.batch_size	,   dtype=tf.intaa       )
									elif model_class in [
									    *get_values(__magic_name__       ),
									    *get_values(__magic_name__       ),
									]:
										a		       =							tf.zeros(self.model_tester.batch_size	,   dtype=tf.intaa       )
									elif model_class in get_values(__magic_name__       ):
										a		       =							tf.zeros(self.model_tester.batch_size	,   dtype=tf.intaa       )
									elif model_class in [
									    *get_values(__magic_name__       ),
									    *get_values(__magic_name__       ),
									    *get_values(__magic_name__       ),
									    *get_values(__magic_name__       ),
									    *get_values(__magic_name__       ),
									]:
										a		       =							tf.zeros(
										    (self.model_tester.batch_size, self.model_tester.seq_length)	,   dtype=tf.intaa       )
								return inputs_dict
							@slow
							def    lowerCamelCase__							(		self		:str       ):







								'''simple docstring'''







								a       ,				a		       =							self.model_tester.prepare_config_and_inputs_for_common()
								for model_class in self.all_model_classes[:2]:
									a		       =							self._prepare_for_class(__magic_name__	,   __magic_name__       )
									a		       =							model_class(__magic_name__       )

									@tf.function
									def run_in_graph_mode():
										return model(__magic_name__       )

									a		       =							run_in_graph_mode()
									self.assertIsNotNone(__magic_name__       )
							@slow
							def    lowerCamelCase__							(		self		:List[Any]       ):







								'''simple docstring'''







								a       ,				a		       =							self.model_tester.prepare_config_and_inputs_for_common()
								for model_class in self.all_model_classes[:2]:
									a		       =							self._prepare_for_class(__magic_name__	,   __magic_name__       )
									a		       =							model_class(__magic_name__       )

									@tf.function(experimental_compile=__magic_name__       )
									def run_in_graph_mode():
										return model(__magic_name__       )

									a		       =							run_in_graph_mode()
									self.assertIsNotNone(__magic_name__       )
							@slow
							def    lowerCamelCase__							(		self		:str       ):







								'''simple docstring'''







								a       ,				a		       =							self.model_tester.prepare_config_and_inputs_for_common()
								for model_class in self.all_model_classes[:2]:
									a		       =							model_class(__magic_name__       )
									if getattr(__magic_name__	,   """hf_compute_loss"""	,   __magic_name__       ):
										# Test that model correctly compute the loss with kwargs
										a		       =							self._prepare_for_class(inputs_dict.copy()	,   __magic_name__	,   return_labels=__magic_name__       )
										# Is there a better way to remove these decoder inputs?
										a		       =							{
										    key: val
										    for key, val in prepared_for_class.items()
										    if key not in ("""head_mask""", """decoder_head_mask""", """cross_attn_head_mask""", """decoder_input_ids""")
										}

										a		       =							{
										    """labels""",
										    """label""",
										    """label_ids""",
										    """start_positions""",
										    """start_position""",
										    """end_positions""",
										    """end_position""",
										    """next_sentence_label""",
										}
										a		       =							possible_label_cols.intersection(set(__magic_name__       )       )
										self.assertGreater(len(__magic_name__       )	,   0	,   msg="""No matching label names found!"""       )
										a		       =							{key: val for key, val in prepared_for_class.items() if key in label_names}
										a		       =							{key: val for key, val in prepared_for_class.items() if key not in label_names}
										self.assertGreater(len(__magic_name__       )	,   0       )

										# Make sure it works with XLA!
										model.compile(optimizer=tf.keras.optimizers.SGD(0.0       )	,   jit_compile=__magic_name__       )
										# Make sure the model fits without crashing regardless of where we pass the labels
										a		       =							model.fit(
										    __magic_name__	,   validation_data=__magic_name__	,   steps_per_epoch=1	,   validation_steps=1	,   shuffle=__magic_name__	,   verbose=0	,   )
										a		       =							history.history["""loss"""][0]
										self.assertTrue(not isnan(__magic_name__       )       )
										a		       =							history.history["""val_loss"""][0]
										self.assertTrue(not isnan(__magic_name__       )       )

										# Now test it with separate labels, to make sure that path works in XLA too.
										a		       =							model_class(__magic_name__       )
										model.compile(optimizer=tf.keras.optimizers.SGD(0.0       )	,   jit_compile=__magic_name__       )
										a		       =							model.fit(
										    __magic_name__	,   __magic_name__	,   validation_data=(inputs_minus_labels, labels)	,   steps_per_epoch=1	,   validation_steps=1	,   shuffle=__magic_name__	,   verbose=0	,   )

										a		       =							history.history["""loss"""][0]
										self.assertTrue(not isnan(__magic_name__       )       )
										a		       =							history.history["""val_loss"""][0]
										self.assertTrue(not isnan(__magic_name__       )       )
							@slow
							def    lowerCamelCase__							(		self		:Union[str, Any]       ):







								'''simple docstring'''







								a       ,				a		       =							self.model_tester.prepare_config_and_inputs_for_common()
								a		       =							True
								a		       =							True

								if hasattr(__magic_name__	,   """use_cache"""       ):
									a		       =							True

								a		       =							getattr(self.model_tester	,   """encoder_seq_length"""	,   self.model_tester.seq_length       )
								a		       =							getattr(self.model_tester	,   """key_length"""	,   __magic_name__       )

								for model_class in self.all_model_classes[:2]:
									a		       =							self._prepare_for_class(__magic_name__	,   __magic_name__       )
									a		       =							model_class(__magic_name__       )
									model.build()
									a		       =							len(model(__magic_name__       )       )

									for key in list(class_inputs_dict.keys()       ):
										# Remove keys not in the serving signature, as the SavedModel will not be compiled to deal with them
										if key not in model.input_signature:
											del class_inputs_dict[key]
										# Check it's a tensor, in case the inputs dict has some bools in it too
										elif isinstance(class_inputs_dict[key]	,   tf.Tensor       ) and class_inputs_dict[key].dtype.is_integer:
											a		       =							tf.cast(class_inputs_dict[key]	,   tf.intaa       )

									if set(class_inputs_dict.keys()       ) != set(model.input_signature.keys()       ):
										continue  # Some models have inputs that the preparation functions don't create, we skip those

									with tempfile.TemporaryDirectory() as tmpdirname:
										model.save_pretrained(__magic_name__	,   saved_model=__magic_name__       )
										a		       =							os.path.join(__magic_name__	,   """saved_model"""	,   """1"""       )
										a		       =							tf.keras.models.load_model(__magic_name__       )
										a		       =							model(__magic_name__       )

										if self.is_encoder_decoder:
											a		       =							outputs["""encoder_hidden_states"""]
											a		       =							outputs["""encoder_attentions"""]
										else:
											a		       =							outputs["""hidden_states"""]
											a		       =							outputs["""attentions"""]

										self.assertEqual(len(__magic_name__       )	,   __magic_name__       )

										a		       =							getattr(
										    self.model_tester	,   """expected_num_hidden_layers"""	,   self.model_tester.num_hidden_layers + 1       )

										self.assertEqual(len(__magic_name__       )	,   __magic_name__       )
										self.assertListEqual(
										    list(output_hidden_states[0].shape[-2:]       )	,   [self.model_tester.seq_length, self.model_tester.hidden_size]	,   )

										self.assertEqual(len(__magic_name__       )	,   self.model_tester.num_hidden_layers       )
										self.assertListEqual(
										    list(output_attentions[0].shape[-3:]       )	,   [self.model_tester.num_attention_heads, encoder_seq_length, encoder_key_length]	,   )
							@slow
							def    lowerCamelCase__							(		self		:Optional[Any]       ):







								'''simple docstring'''







								tf.keras.mixed_precision.set_global_policy("""mixed_float16"""       )

								# try/finally block to ensure subsequent tests run in float32
								try:
									a       ,				a		       =							self.model_tester.prepare_config_and_inputs_for_common()
									for model_class in self.all_model_classes[:2]:
										a		       =							self._prepare_for_class(__magic_name__	,   __magic_name__       )
										a		       =							model_class(__magic_name__       )
										a		       =							model(__magic_name__       )

										self.assertIsNotNone(__magic_name__       )
								finally:
									tf.keras.mixed_precision.set_global_policy("""float32"""       )
							@slow
							def    lowerCamelCase__							(		self		:List[str]       ):







								'''simple docstring'''







								a       ,				a		       =							self.model_tester.prepare_config_and_inputs_for_common()
								# head_mask and decoder_head_mask has different shapes than other input args
								if "head_mask" in inputs_dict:
									del inputs_dict["head_mask"]
								if "decoder_head_mask" in inputs_dict:
									del inputs_dict["decoder_head_mask"]
								if "cross_attn_head_mask" in inputs_dict:
									del inputs_dict["cross_attn_head_mask"]
								a		       =							{
								    module_member
								    for model_class in self.all_model_classes
								    for module in (import_module(model_class.__module__       ),)
								    for module_member_name in dir(__magic_name__       )
								    if module_member_name.endswith("""MainLayer"""       )
								    for module_member in (getattr(__magic_name__	,   __magic_name__       ),)
								    if isinstance(__magic_name__	,   __magic_name__       )
								    and tf.keras.layers.Layer in module_member.__bases__
								    and getattr(__magic_name__	,   """_keras_serializable"""	,   __magic_name__       )
								}

								for main_layer_class in tf_main_layer_classes:
									# T5MainLayer needs an embed_tokens parameter when called without the inputs_embeds parameter
									if "T5" in main_layer_class.__name__:
										# Take the same values than in TFT5ModelTester for this shared layer
										a		       =							TFSharedEmbeddings(self.model_tester.vocab_size	,   self.model_tester.hidden_size	,   name="""shared"""       )
										a		       =							False
										a		       =							main_layer_class(__magic_name__	,   embed_tokens=__magic_name__       )
									else:
										a		       =							main_layer_class(__magic_name__       )

									a		       =							{
									    name: tf.keras.Input(tensor.shape[1:]	,   dtype=tensor.dtype       ) for name, tensor in inputs_dict.items()
									}

									if hasattr(self.model_tester	,   """num_labels"""       ):
										a		       =							self.model_tester.num_labels
									else:
										a		       =							2

									a		       =							tf.data.Dataset.from_tensor_slices(
									    (inputs_dict, np.ones((self.model_tester.batch_size, self.model_tester.seq_length, num_labels, 1)       ))       ).batch(1       )

									a		       =							main_layer(__magic_name__       )[0]
									a		       =							tf.keras.layers.Dense(__magic_name__	,   activation="""softmax"""	,   name="""outputs"""       )(__magic_name__       )
									a		       =							tf.keras.models.Model(inputs=__magic_name__	,   outputs=[outputs]       )

									model.compile(loss="""binary_crossentropy"""	,   optimizer="""adam"""	,   metrics=["""binary_accuracy"""]       )
									model.fit(__magic_name__	,   epochs=1       )

									with tempfile.TemporaryDirectory() as tmpdirname:
										a		       =							os.path.join(__magic_name__	,   """keras_model.h5"""       )
										model.save(__magic_name__       )
										if "T5" in main_layer_class.__name__:
											a		       =							tf.keras.models.load_model(
											    __magic_name__	,   custom_objects={
											        main_layer_class.__name__: main_layer_class,
											        """TFSharedEmbeddings""": TFSharedEmbeddings,
											    }	,   )
										else:
											a		       =							tf.keras.models.load_model(
											    __magic_name__	,   custom_objects={main_layer_class.__name__: main_layer_class}       )
										assert isinstance(__magic_name__	,   tf.keras.Model       )
										model(__magic_name__       )
							@slow
							def    lowerCamelCase__							(		self		:int       ):







								'''simple docstring'''







								a       ,				a		       =							self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes[:2]:
									a		       =							model_class(__magic_name__       )

									a		       =							copy.deepcopy(__magic_name__       )

									if not self.is_encoder_decoder:
										a		       =							inputs["""input_ids"""]
										del inputs["input_ids"]
									else:
										a		       =							inputs["""input_ids"""]
										a		       =							inputs.get("""decoder_input_ids"""	,   __magic_name__       )
										del inputs["input_ids"]
										inputs.pop("""decoder_input_ids"""	,   __magic_name__       )

									if not self.is_encoder_decoder:
										a		       =							model.get_input_embeddings()(__magic_name__       )
									else:
										a		       =							model.get_input_embeddings()(__magic_name__       )
										a		       =							model.get_input_embeddings()(__magic_name__       )

									a		       =							self._prepare_for_class(__magic_name__	,   __magic_name__       )

									@tf.function
									def run_in_graph_mode():
										return model(__magic_name__       )

									a		       =							run_in_graph_mode()
									self.assertIsNotNone(__magic_name__       )
							def    lowerCamelCase__							(		self		:str	,   __magic_name__		:Union[str, Any]	,   __magic_name__		:Dict       ):







								'''simple docstring'''







								a		       =							[]
								if model.config.bos_token_id is not None:
									special_tokens.append(model.config.bos_token_id       )
								if model.config.pad_token_id is not None:
									special_tokens.append(model.config.pad_token_id       )
								if model.config.eos_token_id is not None:
									special_tokens.append(model.config.eos_token_id       )

								# create random bad tokens that are not special tokens
								a		       =							[]
								while len(__magic_name__       ) < num_bad_tokens:
									a		       =							tf.squeeze(ids_tensor((1, 1)	,   self.model_tester.vocab_size       )	,   0       ).numpy()[0]
									if token not in special_tokens:
										bad_tokens.append(__magic_name__       )
								return bad_tokens
							def    lowerCamelCase__							(		self		:List[str]	,   __magic_name__		:Dict       ):







								'''simple docstring'''







								for token_id in output_ids[0].numpy().tolist():
									self.assertGreaterEqual(__magic_name__	,   0       )
									self.assertLess(__magic_name__	,   self.model_tester.vocab_size       )





							def    lowerCamelCase__							(		self		:List[Any]	,   __magic_name__		:int	,   __magic_name__		:List[str]       ):







								'''simple docstring'''







								for bad_word_ids in bad_words_ids:
									# for all slices in batch
									for generated_ids_slice in generated_ids:
										# for all word idx
										for i in range(len(__magic_name__       )	,   len(__magic_name__       )       ):
											# if tokens match
											if generated_ids_slice[i - len(__magic_name__       ) : i] == bad_word_ids:
												return True
								return False







from __future__ import annotations

import unittest

from transformers import AutoTokenizer, GPTJConfig, is_tf_available
from transformers.testing_utils import require_tf, slow, tooslow

from ...test_configuration_common import ConfigTester
from ...test_modeling_tf_common import TFModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin
from ...utils.test_modeling_tf_core import TFCoreModelTesterMixin


if is_tf_available():
		import tensorflow as tf

		from transformers.models.gptj.modeling_tf_gptj import (
		    TFGPTJForCausalLM,
		    TFGPTJForQuestionAnswering,
		    TFGPTJForSequenceClassification,
		    TFGPTJModel,
		    shape_list,
		)

class 	__lowerCAmelCase			:
							def __init__(		self		:Tuple	,   __magic_name__		:str       ):







								'''simple docstring'''







								a		       =							parent
								a		       =							13
								a		       =							7
								a		       =							True
								a		       =							True
								a		       =							True
								a		       =							True
								a		       =							True
								a		       =							99
								a		       =							32
								a		       =							4
								a		       =							2
								a		       =							4
								a		       =							37
								a		       =							"""gelu"""
								a		       =							0.1
								a		       =							0.1
								a		       =							512
								a		       =							16
								a		       =							2
								a		       =							0.02
								a		       =							3
								a		       =							4
								a		       =							None
								a		       =							self.vocab_size - 1
								a		       =							self.vocab_size - 1
								a		       =							self.vocab_size - 1
							def    lowerCamelCase__							(		self		:Union[str, Any]       ):







								'''simple docstring'''







								a		       =							ids_tensor([self.batch_size, self.seq_length]	,   self.vocab_size       )

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

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

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

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

								a		       =							GPTJConfig(
								    vocab_size=self.vocab_size	,   n_embd=self.hidden_size	,   n_layer=self.num_hidden_layers	,   n_head=self.num_attention_heads	,   intermediate_size=self.intermediate_size	,   hidden_act=self.hidden_act	,   hidden_dropout_prob=self.hidden_dropout_prob	,   attention_probs_dropout_prob=self.attention_probs_dropout_prob	,   n_positions=self.max_position_embeddings	,   type_vocab_size=self.type_vocab_size	,   initializer_range=self.initializer_range	,   bos_token_id=self.bos_token_id	,   eos_token_id=self.eos_token_id	,   pad_token_id=self.pad_token_id	,   rotary_dim=self.rotary_dim	,   return_dict=__magic_name__	,   )

								a		       =							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		:List[str]	,   __magic_name__		:Any	,   __magic_name__		:Tuple	,   __magic_name__		:str	,   __magic_name__		:Any	,   __magic_name__		:Optional[Any]	,   *__magic_name__		:Optional[Any]       ):







								'''simple docstring'''







								a		       =							TFGPTJModel(config=__magic_name__       )
								a		       =							{
								    """input_ids""": input_ids,
								    """attention_mask""": input_mask,
								    """token_type_ids""": token_type_ids,
								}
								a		       =							model(__magic_name__       )

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

								a		       =							model(__magic_name__       )

								self.parent.assertEqual(result.last_hidden_state.shape	,   (self.batch_size, self.seq_length, self.hidden_size)       )
							def    lowerCamelCase__							(		self		:List[str]	,   __magic_name__		:Optional[Any]	,   __magic_name__		:List[str]	,   __magic_name__		:str	,   __magic_name__		:Tuple	,   __magic_name__		:Union[str, Any]	,   *__magic_name__		:str       ):







								'''simple docstring'''







								a		       =							TFGPTJModel(config=__magic_name__       )

								# first forward pass
								a		       =							model(__magic_name__	,   token_type_ids=__magic_name__	,   use_cache=__magic_name__       )
								a		       =							model(__magic_name__	,   token_type_ids=__magic_name__       )
								a		       =							model(__magic_name__	,   token_type_ids=__magic_name__	,   use_cache=__magic_name__       )

								self.parent.assertTrue(len(__magic_name__       ) == len(__magic_name__       )       )
								self.parent.assertTrue(len(__magic_name__       ) == len(__magic_name__       ) + 1       )

								a       ,				a		       =							outputs.to_tuple()

								# create hypothetical next token and extent to next_input_ids
								a		       =							ids_tensor((self.batch_size, 1)	,   config.vocab_size       )
								a		       =							ids_tensor([self.batch_size, 1]	,   self.type_vocab_size       )

								# append to next input_ids and token_type_ids
								a		       =							tf.concat([input_ids, next_tokens]	,   axis=-1       )
								a		       =							tf.concat([token_type_ids, next_token_types]	,   axis=-1       )

								a		       =							model(__magic_name__	,   token_type_ids=__magic_name__       )["""last_hidden_state"""]
								a		       =							model(__magic_name__	,   token_type_ids=__magic_name__	,   past_key_values=__magic_name__       )[
								    """last_hidden_state"""
								]

								# select random slice
								a		       =							int(ids_tensor((1,)	,   shape_list(__magic_name__       )[-1]       )       )
								a		       =							output_from_no_past[:, -1, random_slice_idx]
								a		       =							output_from_past[:, 0, random_slice_idx]

								# test that outputs are equal for slice
								tf.debugging.assert_near(__magic_name__	,   __magic_name__	,   rtol=1E-6       )
							def    lowerCamelCase__							(		self		:List[Any]	,   __magic_name__		:List[str]	,   __magic_name__		:Union[str, Any]	,   __magic_name__		:Optional[int]	,   __magic_name__		:str	,   __magic_name__		:str	,   *__magic_name__		:str       ):







								'''simple docstring'''







								a		       =							TFGPTJModel(config=__magic_name__       )

								# create attention mask
								a		       =							self.seq_length // 2
								a		       =							tf.ones((self.batch_size, half_seq_length)	,   dtype=tf.intaa       )
								a		       =							tf.zeros((self.batch_size, self.seq_length - half_seq_length)	,   dtype=tf.intaa       )
								a		       =							tf.concat([attn_mask_begin, attn_mask_end]	,   axis=1       )

								# first forward pass
								a       ,				a		       =							model(__magic_name__	,   attention_mask=__magic_name__       ).to_tuple()

								# create hypothetical next token and extent to next_input_ids
								a		       =							ids_tensor((self.batch_size, 1)	,   config.vocab_size       )

								# change a random masked slice from input_ids
								a		       =							ids_tensor((1,)	,   __magic_name__       ).numpy() + 1
								a		       =							ids_tensor((self.batch_size, self.seq_length)	,   config.vocab_size       )
								a		       =							tf.range(self.seq_length       ) == (self.seq_length - random_seq_idx_to_change)
								a		       =							tf.transpose(
								    tf.broadcast_to(tf.expand_dims(__magic_name__	,   -1       )	,   (self.seq_length, self.batch_size)       )       )
								a		       =							tf.where(__magic_name__	,   __magic_name__	,   __magic_name__       )

								# append to next input_ids and attn_mask
								a		       =							tf.concat([input_ids, next_tokens]	,   axis=-1       )
								a		       =							tf.concat([attn_mask, tf.ones((shape_list(__magic_name__       )[0], 1)	,   dtype=tf.intaa       )]	,   axis=1       )

								# get two different outputs
								a		       =							model(__magic_name__	,   attention_mask=__magic_name__       )["""last_hidden_state"""]
								a		       =							model(__magic_name__	,   past_key_values=__magic_name__	,   attention_mask=__magic_name__       )[
								    """last_hidden_state"""
								]

								# select random slice
								a		       =							int(ids_tensor((1,)	,   shape_list(__magic_name__       )[-1]       )       )
								a		       =							output_from_no_past[:, -1, random_slice_idx]
								a		       =							output_from_past[:, 0, random_slice_idx]

								# test that outputs are equal for slice
								tf.debugging.assert_near(__magic_name__	,   __magic_name__	,   rtol=1E-1_2       )
							def    lowerCamelCase__							(		self		:List[str]	,   __magic_name__		:List[Any]	,   __magic_name__		:List[str]	,   __magic_name__		:str	,   __magic_name__		:str	,   __magic_name__		:Optional[int]	,   *__magic_name__		:Any       ):







								'''simple docstring'''







								a		       =							TFGPTJModel(config=__magic_name__       )

								a		       =							input_ids[:1, :]
								a		       =							input_mask[:1, :]
								a		       =							token_type_ids[:1, :]
								a		       =							1

								# first forward pass
								a		       =							model(__magic_name__	,   attention_mask=__magic_name__	,   token_type_ids=__magic_name__	,   use_cache=__magic_name__       )

								a       ,				a		       =							outputs.to_tuple()

								# create hypothetical next token and extent to next_input_ids
								a		       =							ids_tensor((self.batch_size, 3)	,   config.vocab_size       )
								a		       =							ids_tensor((self.batch_size, 3)	,   2       )
								a		       =							ids_tensor((self.batch_size, 3)	,   self.type_vocab_size       )

								# append to next input_ids and token_type_ids
								a		       =							tf.concat([input_ids, next_tokens]	,   axis=-1       )
								a		       =							tf.concat([input_mask, next_attn_mask]	,   axis=-1       )
								a		       =							tf.concat([token_type_ids, next_token_types]	,   axis=-1       )

								a		       =							model(
								    __magic_name__	,   token_type_ids=__magic_name__	,   attention_mask=__magic_name__       )["""last_hidden_state"""]
								a		       =							model(
								    __magic_name__	,   token_type_ids=__magic_name__	,   attention_mask=__magic_name__	,   past_key_values=__magic_name__	,   )["""last_hidden_state"""]
								self.parent.assertTrue(output_from_past.shape[1] == next_tokens.shape[1]       )

								# select random slice
								a		       =							int(ids_tensor((1,)	,   shape_list(__magic_name__       )[-1]       )       )
								a		       =							output_from_no_past[:, -3:, random_slice_idx]
								a		       =							output_from_past[:, :, random_slice_idx]

								# test that outputs are equal for slice
								tf.debugging.assert_near(__magic_name__	,   __magic_name__	,   rtol=1E-3       )
							def    lowerCamelCase__							(		self		:str	,   __magic_name__		:Optional[int]	,   __magic_name__		:Optional[Any]	,   __magic_name__		:Optional[Any]	,   __magic_name__		:str	,   __magic_name__		:int	,   *__magic_name__		:List[Any]       ):







								'''simple docstring'''







								a		       =							TFGPTJForCausalLM(config=__magic_name__       )
								a		       =							{
								    """input_ids""": input_ids,
								    """attention_mask""": input_mask,
								    """token_type_ids""": token_type_ids,
								}
								a		       =							model(__magic_name__       )
								self.parent.assertEqual(result.logits.shape	,   (self.batch_size, self.seq_length, self.vocab_size)       )





							def    lowerCamelCase__							(		self		:int       ):







								'''simple docstring'''







								a		       =							self.prepare_config_and_inputs()

								(
								    (
								    a
								)       ,				(
								    a
								)       ,				(
								    a
								)       ,				(
								    a
								)       ,				(
								    a
								)       ,				(
								    a
								)       ,				(
								    a
								)       ,				(
								    a
								)       ,				(
								    a
								)       ,				
								)		       =							config_and_inputs

								a		       =							{
								    """input_ids""": input_ids,
								    """token_type_ids""": token_type_ids,
								    """attention_mask""": input_mask,
								}
								return config, inputs_dict







@require_tf
class 	__lowerCAmelCase			(			__magic_name__   , __magic_name__   , __magic_name__   , unittest.TestCase				):
							UpperCamelCase__        =						(
							    (TFGPTJForCausalLM, TFGPTJForSequenceClassification, TFGPTJForQuestionAnswering, TFGPTJModel)
							    if is_tf_available()
							    else ()
							)

							UpperCamelCase__        =						(TFGPTJForCausalLM,) if is_tf_available() else ()
							UpperCamelCase__        =						(
							    {
							        '''feature-extraction''': TFGPTJModel,
							        '''question-answering''': TFGPTJForQuestionAnswering,
							        '''text-classification''': TFGPTJForSequenceClassification,
							        '''text-generation''': TFGPTJForCausalLM,
							        '''zero-shot''': TFGPTJForSequenceClassification,
							    }
							    if is_tf_available()
							    else {}
							)
							UpperCamelCase__        =						False
							UpperCamelCase__        =						False
							UpperCamelCase__        =						False
							UpperCamelCase__        =						False
							def    lowerCamelCase__							(		self		:Union[str, Any]	,   __magic_name__		:Any	,   __magic_name__		:Optional[int]	,   __magic_name__		:int	,   __magic_name__		:List[str]	,   __magic_name__		:Union[str, Any]       ):







								'''simple docstring'''







								if (
								    pipeline_test_casse_name == "QAPipelineTests"
								    and tokenizer_name is not None
								    and not tokenizer_name.endswith("""Fast"""       )
								):
									# `QAPipelineTests` fails for a few models when the slower tokenizer are used.
									# (The slower tokenizers were never used for pipeline tests before the pipeline testing rework)
									# TODO: check (and possibly fix) the `QAPipelineTests` with slower tokenizer
									return True

								return False
							def    lowerCamelCase__							(		self		:Optional[int]       ):







								'''simple docstring'''







								a		       =							TFGPTJModelTester(self       )
								a		       =							ConfigTester(self	,   config_class=__magic_name__	,   n_embd=37       )
							def    lowerCamelCase__							(		self		:List[str]       ):







								'''simple docstring'''







								self.config_tester.run_common_tests()
							def    lowerCamelCase__							(		self		:str       ):







								'''simple docstring'''







								a		       =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_gptj_model(*__magic_name__       )
							def    lowerCamelCase__							(		self		:Optional[Any]       ):







								'''simple docstring'''







								a		       =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_gptj_model_past(*__magic_name__       )
							def    lowerCamelCase__							(		self		:Union[str, Any]       ):







								'''simple docstring'''







								a		       =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_gptj_model_attention_mask_past(*__magic_name__       )
							def    lowerCamelCase__							(		self		:Any       ):







								'''simple docstring'''







								a		       =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_gptj_model_past_large_inputs(*__magic_name__       )
							def    lowerCamelCase__							(		self		:List[str]       ):







								'''simple docstring'''







								a		       =							self.model_tester.prepare_config_and_inputs()
								self.model_tester.create_and_check_gptj_lm_head_model(*__magic_name__       )
							@slow
							@unittest.skipIf(
							    not is_tf_available() or len(tf.config.list_physical_devices("""GPU"""       )       ) > 0	,   """skip testing on GPU for now to avoid GPU OOM."""	,   )
							def    lowerCamelCase__							(		self		:str       ):







								'''simple docstring'''







								a		       =							TFGPTJModel.from_pretrained("""EleutherAI/gpt-j-6B"""	,   from_pt=__magic_name__       )
								self.assertIsNotNone(__magic_name__       )





							@unittest.skip(reason="""Currently, model embeddings are going to undergo a major refactor."""       )
							def    lowerCamelCase__							(		self		:str       ):







								'''simple docstring'''







								super().test_resize_token_embeddings()







@require_tf
@tooslow
# Marked as @tooslow due to GPU OOM -- but still useful to run locally. Requires ~39GB of RAM.
class 	__lowerCAmelCase			(			unittest.TestCase				):
							def    lowerCamelCase__							(		self		:Any       ):







								'''simple docstring'''







								a		       =							TFGPTJForCausalLM.from_pretrained("""EleutherAI/gpt-j-6B"""	,   from_pt=__magic_name__       )
								a		       =							tf.convert_to_tensor([[464, 3290]]	,   dtype=tf.intaa       )  # The dog
								# fmt: off
								# The dog is a man's best friend. It is a loyal companion, and it is a friend
								a		       =							[464, 3290, 318, 257, 582, 338, 1266, 1545, 13, 632, 318, 257, 9112, 1_5185, 11, 290, 340, 318, 257, 1545]
								# fmt: on
								a		       =							model.generate(__magic_name__	,   do_sample=__magic_name__       )
								self.assertListEqual(output_ids[0].numpy().tolist()	,   __magic_name__       )
							def    lowerCamelCase__							(		self		:List[str]       ):







								'''simple docstring'''







								a		       =							AutoTokenizer.from_pretrained("""EleutherAI/gpt-j-6B"""	,   revision="""float16"""       )
								a		       =							TFGPTJForCausalLM.from_pretrained("""EleutherAI/gpt-j-6B"""	,   revision="""float16"""	,   from_pt=__magic_name__       )

								a		       =							tokenizer("""Today is a nice day and"""	,   return_tensors="""tf"""       )
								# forces the generation to happen on CPU, to avoid GPU-related quirks
								with tf.device(""":/CPU:0"""       ):
									a		       =							model.generate(**__magic_name__	,   do_sample=__magic_name__	,   seed=[42, 0]       )
								a		       =							tokenizer.decode(output_ids[0]	,   skip_special_tokens=__magic_name__       )

								a		       =							"""Today is a nice day and I’m going to go for a walk. I’"""
								self.assertEqual(__magic_name__	,   __magic_name__       )
							def    lowerCamelCase__							(		self		:Optional[Any]       ):







								'''simple docstring'''







								a		       =							TFGPTJForCausalLM.from_pretrained("""EleutherAI/gpt-j-6B"""	,   revision="""float16"""	,   from_pt=__magic_name__       )
								a		       =							AutoTokenizer.from_pretrained("""EleutherAI/gpt-j-6B"""	,   revision="""float16"""       )

								a		       =							"""left"""

								# Define PAD Token = EOS Token = 50256
								a		       =							tokenizer.eos_token
								a		       =							model.config.eos_token_id

								# use different length sentences to test batching
								a		       =							[
								    """Hello, my dog is a little""",
								    """Today, I""",
								]
								a		       =							[
								    """Hello, my dog is a little over a year old and has been diagnosed with hip dysplasia""",
								    """Today, I’m going to be talking about a topic that’""",
								]
								return model, tokenizer, sentences, expected_output_sentences
							def    lowerCamelCase__							(		self		:List[Any]       ):







								'''simple docstring'''







								a       ,				a       ,				a       ,				a		       =							self._get_beam_search_test_objects()

								a		       =							tokenizer(__magic_name__	,   return_tensors="""tf"""	,   padding=__magic_name__       )
								a		       =							model.generate(**__magic_name__	,   do_sample=__magic_name__	,   num_beams=2       )
								a		       =							tokenizer.batch_decode(__magic_name__	,   skip_special_tokens=__magic_name__       )
								self.assertListEqual(__magic_name__	,   __magic_name__       )
							def    lowerCamelCase__							(		self		:Optional[int]       ):







								'''simple docstring'''







								a       ,				a       ,				a       ,				a		       =							self._get_beam_search_test_objects()

								a		       =							tokenizer(__magic_name__	,   return_tensors="""tf"""	,   padding=__magic_name__       )
								a		       =							tokenizer(sentences[0]	,   return_tensors="""tf"""       )
								a		       =							model.generate(**__magic_name__	,   do_sample=__magic_name__	,   num_beams=2       )
								a		       =							(
								    shape_list(inputs_non_padded["""input_ids"""]       )[-1]
								    - tf.reduce_sum(tf.cast(inputs["""attention_mask"""][-1]	,   tf.intaa       )       ).numpy()
								)
								a		       =							tokenizer(sentences[1]	,   return_tensors="""tf"""       )
								a		       =							model.generate(
								    **__magic_name__	,   do_sample=__magic_name__	,   num_beams=2	,   max_length=model.config.max_length - num_paddings       )
								a		       =							tokenizer.decode(output_non_padded[0]	,   skip_special_tokens=__magic_name__       )
								a		       =							tokenizer.decode(output_padded[0]	,   skip_special_tokens=__magic_name__       )
								self.assertListEqual(__magic_name__	,   [non_padded_sentence, padded_sentence]       )





							def    lowerCamelCase__							(		self		:Optional[int]       ):







								'''simple docstring'''







								a       ,				a       ,				a       ,				a		       =							self._get_beam_search_test_objects()

								a		       =							tokenizer(__magic_name__	,   return_tensors="""tf"""	,   padding=__magic_name__       )
								a		       =							tf.function(model.generate	,   jit_compile=__magic_name__       )
								a		       =							xla_generate(**__magic_name__	,   do_sample=__magic_name__	,   num_beams=2       )
								a		       =							tokenizer.batch_decode(__magic_name__	,   skip_special_tokens=__magic_name__       )
								self.assertListEqual(__magic_name__	,   __magic_name__       )

