

'''simple docstring'''





from copy import deepcopy
from unittest.case import TestCase

import pytest

from datasets.arrow_dataset import Dataset
from datasets.features import Audio, ClassLabel, Features, Image, Sequence, Value
from datasets.info import DatasetInfo
from datasets.tasks import (
    AudioClassification,
    AutomaticSpeechRecognition,
    ImageClassification,
    LanguageModeling,
    QuestionAnsweringExtractive,
    Summarization,
    TextClassification,
    task_template_from_dict,
)
from datasets.utils.py_utils import asdict


lowerCAmelCase							:						Dict     			=		{
    """id""": """5733be284776f41900661182""",
    """title""": """University_of_Notre_Dame""",
    """context""": """Architecturally, the school has a Catholic character. Atop the Main Building\'s gold dome is a golden statue of the Virgin Mary. Immediately in front of the Main Building and facing it, is a copper statue of Christ with arms upraised with the legend \"Venite Ad Me Omnes\". Next to the Main Building is the Basilica of the Sacred Heart. Immediately behind the basilica is the Grotto, a Marian place of prayer and reflection. It is a replica of the grotto at Lourdes, France where the Virgin Mary reputedly appeared to Saint Bernadette Soubirous in 1858. At the end of the main drive (and in a direct line that connects through 3 statues and the Gold Dome), is a simple, modern stone statue of Mary.""",
    """question""": """To whom did the Virgin Mary allegedly appear in 1858 in Lourdes France?""",
    """answers""": {"""text""": ["""Saint Bernadette Soubirous"""], """answer_start""": [515]},
}







@pytest.mark.parametrize(
    "task_cls"       ,			[
        AudioClassification,
        AutomaticSpeechRecognition,
        ImageClassification,
        LanguageModeling,
        QuestionAnsweringExtractive,
        Summarization,
        TextClassification,
    ]       ,			)
def     A_   (					_UpperCAmelCase					):
						SCREAMING_SNAKE_CASE_:				Dict							= task_cls()
						SCREAMING_SNAKE_CASE_:				Optional[Any]							= asdict(_UpperCAmelCase					)
						SCREAMING_SNAKE_CASE_:				Optional[Any]							= task_template_from_dict(_UpperCAmelCase					)
						assert task == reloaded



class 						__lowercase       :




			"""simple docstring"""







			def  _SCREAMING_SNAKE_CASE    (				self						:  int):
									SCREAMING_SNAKE_CASE_:				Optional[int]							= LanguageModeling(text_column="input_text")
									assert {"input_text": "text"} == task.column_mapping




			def  _SCREAMING_SNAKE_CASE    (				self						:  int):
									SCREAMING_SNAKE_CASE_:				List[Any]							= Features({"text": Value("string")})
									SCREAMING_SNAKE_CASE_:				List[str]							= {"text_column": "input_text"}
									SCREAMING_SNAKE_CASE_:				Union[str, Any]							= LanguageModeling.from_dict(lowerCAmelCase__)
									assert "language-modeling" == task.task
									assert input_schema == task.input_schema







class 						__lowercase       (				UpperCAmelCase_			):




			"""simple docstring"""







			def  _SCREAMING_SNAKE_CASE    (				self						:  Dict):
									SCREAMING_SNAKE_CASE_:				Optional[Any]							= sorted(["pos", "neg"])



			def  _SCREAMING_SNAKE_CASE    (				self						:  int):
									SCREAMING_SNAKE_CASE_:				Dict							= TextClassification(text_column="input_text"     ,		label_column="input_label")
									self.assertDictEqual({"input_text": "text", "input_label": "labels"}     ,		task.column_mapping)



			def  _SCREAMING_SNAKE_CASE    (				self						:  str):
									SCREAMING_SNAKE_CASE_:				int							= Features({"text": Value("string")})
									# Labels are cast to tuple during `TextClassification.__post_init__`, so we do the same here
									SCREAMING_SNAKE_CASE_:				Tuple							= Features({"labels": ClassLabel})
									SCREAMING_SNAKE_CASE_:				str							= {"text_column": "input_text", "label_column": "input_labels"}
									SCREAMING_SNAKE_CASE_:				Union[str, Any]							= TextClassification.from_dict(lowerCAmelCase__)
									self.assertEqual("text-classification"     ,		task.task)
									self.assertEqual(lowerCAmelCase__     ,		task.input_schema)
									self.assertEqual(lowerCAmelCase__     ,		task.label_schema)




			def  _SCREAMING_SNAKE_CASE    (				self						:  Any):
									SCREAMING_SNAKE_CASE_:				Optional[int]							= TextClassification(text_column="input_text"     ,		label_column="input_label")
									self.assertEqual(task.label_schema["labels"]     ,		lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_:				Optional[int]							= task.align_with_features(Features({"input_label": ClassLabel(names=self.labels)}))
									self.assertEqual(task.label_schema["labels"]     ,		ClassLabel(names=self.labels))







class 						__lowercase       (				UpperCAmelCase_			):




			"""simple docstring"""







			def  _SCREAMING_SNAKE_CASE    (				self						:  Optional[int]):
									SCREAMING_SNAKE_CASE_:				List[Any]							= QuestionAnsweringExtractive(
									    context_column="input_context"     ,		question_column="input_question"     ,		answers_column="input_answers")
									self.assertDictEqual(
									    {"input_context": "context", "input_question": "question", "input_answers": "answers"}     ,		task.column_mapping)




			def  _SCREAMING_SNAKE_CASE    (				self						:  int):
									SCREAMING_SNAKE_CASE_:				Any							= Features({"question": Value("string"), "context": Value("string")})
									SCREAMING_SNAKE_CASE_:				Optional[int]							= Features(
									    {
									        "answers": Sequence(
									            {
									                "text": Value("string"),
									                "answer_start": Value("int32"),
									            })
									    })
									SCREAMING_SNAKE_CASE_:				Optional[int]							= {
									    "context_column": "input_input_context",
									    "question_column": "input_question",
									    "answers_column": "input_answers",
									}
									SCREAMING_SNAKE_CASE_:				str							= QuestionAnsweringExtractive.from_dict(lowerCAmelCase__)
									self.assertEqual("question-answering-extractive"     ,		task.task)
									self.assertEqual(lowerCAmelCase__     ,		task.input_schema)
									self.assertEqual(lowerCAmelCase__     ,		task.label_schema)







class 						__lowercase       (				UpperCAmelCase_			):




			"""simple docstring"""







			def  _SCREAMING_SNAKE_CASE    (				self						:  Any):
									SCREAMING_SNAKE_CASE_:				Optional[int]							= Summarization(text_column="input_text"     ,		summary_column="input_summary")
									self.assertDictEqual({"input_text": "text", "input_summary": "summary"}     ,		task.column_mapping)




			def  _SCREAMING_SNAKE_CASE    (				self						:  Optional[Any]):
									SCREAMING_SNAKE_CASE_:				Optional[int]							= Features({"text": Value("string")})
									SCREAMING_SNAKE_CASE_:				Any							= Features({"summary": Value("string")})
									SCREAMING_SNAKE_CASE_:				Dict							= {"text_column": "input_text", "summary_column": "input_summary"}
									SCREAMING_SNAKE_CASE_:				List[Any]							= Summarization.from_dict(lowerCAmelCase__)
									self.assertEqual("summarization"     ,		task.task)
									self.assertEqual(lowerCAmelCase__     ,		task.input_schema)
									self.assertEqual(lowerCAmelCase__     ,		task.label_schema)







class 						__lowercase       (				UpperCAmelCase_			):




			"""simple docstring"""







			def  _SCREAMING_SNAKE_CASE    (				self						:  Optional[Any]):
									SCREAMING_SNAKE_CASE_:				List[str]							= AutomaticSpeechRecognition(audio_column="input_audio"     ,		transcription_column="input_transcription")
									self.assertDictEqual({"input_audio": "audio", "input_transcription": "transcription"}     ,		task.column_mapping)




			def  _SCREAMING_SNAKE_CASE    (				self						:  Tuple):
									SCREAMING_SNAKE_CASE_:				str							= Features({"audio": Audio()})
									SCREAMING_SNAKE_CASE_:				Optional[int]							= Features({"transcription": Value("string")})
									SCREAMING_SNAKE_CASE_:				List[str]							= {
									    "audio_column": "input_audio",
									    "transcription_column": "input_transcription",
									}
									SCREAMING_SNAKE_CASE_:				List[str]							= AutomaticSpeechRecognition.from_dict(lowerCAmelCase__)
									self.assertEqual("automatic-speech-recognition"     ,		task.task)
									self.assertEqual(lowerCAmelCase__     ,		task.input_schema)
									self.assertEqual(lowerCAmelCase__     ,		task.label_schema)







class 						__lowercase       (				UpperCAmelCase_			):




			"""simple docstring"""







			def  _SCREAMING_SNAKE_CASE    (				self						:  Optional[Any]):
									SCREAMING_SNAKE_CASE_:				List[str]							= sorted(["pos", "neg"])



			def  _SCREAMING_SNAKE_CASE    (				self						:  List[Any]):
									SCREAMING_SNAKE_CASE_:				Dict							= AudioClassification(audio_column="input_audio"     ,		label_column="input_label")
									self.assertDictEqual({"input_audio": "audio", "input_label": "labels"}     ,		task.column_mapping)



			def  _SCREAMING_SNAKE_CASE    (				self						:  str):
									SCREAMING_SNAKE_CASE_:				Tuple							= Features({"audio": Audio()})
									SCREAMING_SNAKE_CASE_:				Union[str, Any]							= Features({"labels": ClassLabel})
									SCREAMING_SNAKE_CASE_:				Tuple							= {
									    "audio_column": "input_image",
									    "label_column": "input_label",
									}
									SCREAMING_SNAKE_CASE_:				Union[str, Any]							= AudioClassification.from_dict(lowerCAmelCase__)
									self.assertEqual("audio-classification"     ,		task.task)
									self.assertEqual(lowerCAmelCase__     ,		task.input_schema)
									self.assertEqual(lowerCAmelCase__     ,		task.label_schema)




			def  _SCREAMING_SNAKE_CASE    (				self						:  Tuple):
									SCREAMING_SNAKE_CASE_:				List[Any]							= AudioClassification(audio_column="input_audio"     ,		label_column="input_label")
									self.assertEqual(task.label_schema["labels"]     ,		lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_:				Tuple							= task.align_with_features(Features({"input_label": ClassLabel(names=self.labels)}))
									self.assertEqual(task.label_schema["labels"]     ,		ClassLabel(names=self.labels))







class 						__lowercase       (				UpperCAmelCase_			):




			"""simple docstring"""







			def  _SCREAMING_SNAKE_CASE    (				self						:  List[Any]):
									SCREAMING_SNAKE_CASE_:				List[str]							= sorted(["pos", "neg"])



			def  _SCREAMING_SNAKE_CASE    (				self						:  Optional[int]):
									SCREAMING_SNAKE_CASE_:				Union[str, Any]							= ImageClassification(image_column="input_image"     ,		label_column="input_label")
									self.assertDictEqual({"input_image": "image", "input_label": "labels"}     ,		task.column_mapping)



			def  _SCREAMING_SNAKE_CASE    (				self						:  Dict):
									SCREAMING_SNAKE_CASE_:				str							= Features({"image": Image()})
									SCREAMING_SNAKE_CASE_:				List[Any]							= Features({"labels": ClassLabel})
									SCREAMING_SNAKE_CASE_:				Dict							= {
									    "image_column": "input_image",
									    "label_column": "input_label",
									}
									SCREAMING_SNAKE_CASE_:				List[str]							= ImageClassification.from_dict(lowerCAmelCase__)
									self.assertEqual("image-classification"     ,		task.task)
									self.assertEqual(lowerCAmelCase__     ,		task.input_schema)
									self.assertEqual(lowerCAmelCase__     ,		task.label_schema)




			def  _SCREAMING_SNAKE_CASE    (				self						:  Any):
									SCREAMING_SNAKE_CASE_:				Optional[int]							= ImageClassification(image_column="input_image"     ,		label_column="input_label")
									self.assertEqual(task.label_schema["labels"]     ,		lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_:				str							= task.align_with_features(Features({"input_label": ClassLabel(names=self.labels)}))
									self.assertEqual(task.label_schema["labels"]     ,		ClassLabel(names=self.labels))







class 						__lowercase       (				UpperCAmelCase_			):




			"""simple docstring"""







			def  _SCREAMING_SNAKE_CASE    (				self						:  str):
									SCREAMING_SNAKE_CASE_:				int							= DatasetInfo(task_templates=QuestionAnsweringExtractive())
									SCREAMING_SNAKE_CASE_:				Any							= Dataset.from_dict({k: [v] for k, v in SAMPLE_QUESTION_ANSWERING_EXTRACTIVE.items()}     ,		info=lowerCAmelCase__)
									assert isinstance(dataset.info.task_templates     ,		lowerCAmelCase__)
									assert len(dataset.info.task_templates) == 1

									def keep_task(lowerCAmelCase__						:  Optional[int]):
															return x

									def dont_keep_task(lowerCAmelCase__						:  Dict):
															SCREAMING_SNAKE_CASE_:				int							= deepcopy(lowerCAmelCase__)
															SCREAMING_SNAKE_CASE_:				Optional[int]							= 0
															return out

									SCREAMING_SNAKE_CASE_:				Optional[int]							= dataset.map(lowerCAmelCase__)
									assert mapped_dataset.info.task_templates == dataset.info.task_templates
									# reload from cache
									SCREAMING_SNAKE_CASE_:				Tuple							= dataset.map(lowerCAmelCase__)
									assert mapped_dataset.info.task_templates == dataset.info.task_templates

									SCREAMING_SNAKE_CASE_:				str							= dataset.map(lowerCAmelCase__)
									assert mapped_dataset.info.task_templates == []
									# reload from cache
									SCREAMING_SNAKE_CASE_:				List[str]							= dataset.map(lowerCAmelCase__)
									assert mapped_dataset.info.task_templates == []




			def  _SCREAMING_SNAKE_CASE    (				self						:  List[str]):
									SCREAMING_SNAKE_CASE_:				str							= Features({"text": Value("string"), "label": ClassLabel(names=("pos", "neg"))})
									SCREAMING_SNAKE_CASE_:				Optional[int]							= TextClassification(text_column="text"     ,		label_column="label")
									SCREAMING_SNAKE_CASE_:				str							= DatasetInfo(features=lowerCAmelCase__     ,		task_templates=lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_:				Tuple							= Dataset.from_dict({"text": ["A sentence."], "label": ["pos"]}     ,		info=lowerCAmelCase__)

									def process(lowerCAmelCase__						:  List[str]):
															return example

									SCREAMING_SNAKE_CASE_:				List[str]							= dataset.remove_columns("label")
									SCREAMING_SNAKE_CASE_:				Optional[Any]							= modified_dataset.map(lowerCAmelCase__)
									assert mapped_dataset.info.task_templates == []









import tempfile
import unittest

import numpy as np

import transformers
from transformers import GPTaConfig, GPTaTokenizer, is_flax_available, is_torch_available
from transformers.testing_utils import is_pt_flax_cross_test, require_flax, slow

from ...generation.test_flax_utils import FlaxGenerationTesterMixin
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
						import jax
						import jax.numpy as jnp

						from transformers.modeling_flax_pytorch_utils import (
						    convert_pytorch_state_dict_to_flax,
						    load_flax_weights_in_pytorch_model,
						)
						from transformers.models.gpta.modeling_flax_gpta import FlaxGPTaLMHeadModel, FlaxGPTaModel

if is_torch_available():
						import torch



class 						__lowercase       :




			"""simple docstring"""







			def __init__(				self						:  Optional[Any]     ,		lowerCAmelCase__						:  Any     ,		lowerCAmelCase__						:  Optional[Any]=14     ,		lowerCAmelCase__						:  List[Any]=7     ,		lowerCAmelCase__						:  List[str]=True     ,		lowerCAmelCase__						:  int=True     ,		lowerCAmelCase__						:  str=False     ,		lowerCAmelCase__						:  List[str]=True     ,		lowerCAmelCase__						:  int=99     ,		lowerCAmelCase__						:  List[str]=32     ,		lowerCAmelCase__						:  Dict=5     ,		lowerCAmelCase__						:  Union[str, Any]=4     ,		lowerCAmelCase__						:  List[Any]=37     ,		lowerCAmelCase__						:  int="gelu"     ,		lowerCAmelCase__						:  List[Any]=0.1     ,		lowerCAmelCase__						:  Optional[int]=0.1     ,		lowerCAmelCase__						:  Optional[int]=512     ,		lowerCAmelCase__						:  Any=0.02     ,		):
									SCREAMING_SNAKE_CASE_:				Tuple							= parent
									SCREAMING_SNAKE_CASE_:				Optional[Any]							= batch_size
									SCREAMING_SNAKE_CASE_:				Tuple							= seq_length
									SCREAMING_SNAKE_CASE_:				Dict							= is_training
									SCREAMING_SNAKE_CASE_:				Dict							= use_input_mask
									SCREAMING_SNAKE_CASE_:				Any							= use_token_type_ids
									SCREAMING_SNAKE_CASE_:				Dict							= use_labels
									SCREAMING_SNAKE_CASE_:				List[str]							= vocab_size
									SCREAMING_SNAKE_CASE_:				str							= hidden_size
									SCREAMING_SNAKE_CASE_:				List[Any]							= num_hidden_layers
									SCREAMING_SNAKE_CASE_:				Optional[int]							= num_attention_heads
									SCREAMING_SNAKE_CASE_:				Any							= intermediate_size
									SCREAMING_SNAKE_CASE_:				Union[str, Any]							= hidden_act
									SCREAMING_SNAKE_CASE_:				List[Any]							= hidden_dropout_prob
									SCREAMING_SNAKE_CASE_:				Dict							= attention_probs_dropout_prob
									SCREAMING_SNAKE_CASE_:				Dict							= max_position_embeddings
									SCREAMING_SNAKE_CASE_:				List[str]							= initializer_range
									SCREAMING_SNAKE_CASE_:				Any							= None
									SCREAMING_SNAKE_CASE_:				int							= vocab_size - 1
									SCREAMING_SNAKE_CASE_:				List[Any]							= vocab_size - 1
									SCREAMING_SNAKE_CASE_:				Any							= vocab_size - 1



			def  _SCREAMING_SNAKE_CASE    (				self						:  Optional[Any]):
									SCREAMING_SNAKE_CASE_:				Union[str, Any]							= ids_tensor([self.batch_size, self.seq_length]     ,		self.vocab_size)

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

									SCREAMING_SNAKE_CASE_:				Any							= GPTaConfig(
									    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     ,		use_cache=lowerCAmelCase__     ,		bos_token_id=self.bos_token_id     ,		eos_token_id=self.eos_token_id     ,		pad_token_id=self.pad_token_id     ,		)

									return (config, input_ids, input_mask)



			def  _SCREAMING_SNAKE_CASE    (				self						:  int):
									SCREAMING_SNAKE_CASE_:				int							= self.prepare_config_and_inputs()
									SCREAMING_SNAKE_CASE_	,					SCREAMING_SNAKE_CASE_	,					SCREAMING_SNAKE_CASE_:				Tuple							= config_and_inputs
									SCREAMING_SNAKE_CASE_:				Optional[Any]							= {"input_ids": input_ids, "attention_mask": attention_mask}
									return config, inputs_dict



			def  _SCREAMING_SNAKE_CASE    (				self						:  List[Any]):
									SCREAMING_SNAKE_CASE_	,					SCREAMING_SNAKE_CASE_	,					SCREAMING_SNAKE_CASE_:				str							= self.prepare_config_and_inputs()

									SCREAMING_SNAKE_CASE_:				List[Any]							= floats_tensor([self.batch_size, self.seq_length, self.hidden_size])
									SCREAMING_SNAKE_CASE_:				List[str]							= ids_tensor([self.batch_size, self.seq_length]     ,		vocab_size=2)

									return (
									    config,
									    input_ids,
									    attention_mask,
									    encoder_hidden_states,
									    encoder_attention_mask,
									)



			def  _SCREAMING_SNAKE_CASE    (				self						:  List[str]     ,		lowerCAmelCase__						:  Optional[int]     ,		lowerCAmelCase__						:  List[str]     ,		lowerCAmelCase__						:  Optional[int]     ,		lowerCAmelCase__						:  Union[str, Any]):
									SCREAMING_SNAKE_CASE_:				Dict							= 20
									SCREAMING_SNAKE_CASE_:				Optional[int]							= model_class_name(lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_:				Union[str, Any]							= model.init_cache(input_ids.shape[0]     ,		lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_:				Optional[int]							= jnp.ones((input_ids.shape[0], max_decoder_length)     ,		dtype="i4")

									SCREAMING_SNAKE_CASE_:				Any							= jnp.broadcast_to(
									    jnp.arange(input_ids.shape[-1] - 1)[None, :]     ,		(input_ids.shape[0], input_ids.shape[-1] - 1))
									SCREAMING_SNAKE_CASE_:				Optional[Any]							= model(
									    input_ids[:, :-1]     ,		attention_mask=lowerCAmelCase__     ,		past_key_values=lowerCAmelCase__     ,		position_ids=lowerCAmelCase__     ,		)

									SCREAMING_SNAKE_CASE_:				str							= jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]     ,		dtype="i4")
									SCREAMING_SNAKE_CASE_:				int							= model(
									    input_ids[:, -1:]     ,		attention_mask=lowerCAmelCase__     ,		past_key_values=outputs_cache.past_key_values     ,		position_ids=lowerCAmelCase__     ,		)

									SCREAMING_SNAKE_CASE_:				Tuple							= model(lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_:				str							= np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])))
									self.parent.assertTrue(diff < 1E-3     ,		msg=F"Max diff is {diff}")




			def  _SCREAMING_SNAKE_CASE    (				self						:  List[str]     ,		lowerCAmelCase__						:  Optional[Any]     ,		lowerCAmelCase__						:  str     ,		lowerCAmelCase__						:  Optional[int]     ,		lowerCAmelCase__						:  Any):
									SCREAMING_SNAKE_CASE_:				Any							= 20
									SCREAMING_SNAKE_CASE_:				Union[str, Any]							= model_class_name(lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_:				List[Any]							= jnp.concatenate(
									    [attention_mask, jnp.zeros((attention_mask.shape[0], max_decoder_length - attention_mask.shape[1]))]     ,		axis=-1     ,		)

									SCREAMING_SNAKE_CASE_:				List[Any]							= model.init_cache(input_ids.shape[0]     ,		lowerCAmelCase__)
									SCREAMING_SNAKE_CASE_:				Any							= jnp.broadcast_to(
									    jnp.arange(input_ids.shape[-1] - 1)[None, :]     ,		(input_ids.shape[0], input_ids.shape[-1] - 1))

									SCREAMING_SNAKE_CASE_:				Union[str, Any]							= model(
									    input_ids[:, :-1]     ,		attention_mask=lowerCAmelCase__     ,		past_key_values=lowerCAmelCase__     ,		position_ids=lowerCAmelCase__     ,		)
									SCREAMING_SNAKE_CASE_:				int							= jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]     ,		dtype="i4")
									SCREAMING_SNAKE_CASE_:				Optional[Any]							= model(
									    input_ids[:, -1:]     ,		past_key_values=outputs_cache.past_key_values     ,		attention_mask=lowerCAmelCase__     ,		position_ids=lowerCAmelCase__     ,		)

									SCREAMING_SNAKE_CASE_:				Any							= model(lowerCAmelCase__     ,		attention_mask=lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_:				List[Any]							= np.max(np.abs((outputs_cache_next[0][:, -1, :5] - outputs[0][:, -1, :5])))
									self.parent.assertTrue(diff < 1E-3     ,		msg=F"Max diff is {diff}")







@require_flax
class 						__lowercase       (				UpperCAmelCase_	, UpperCAmelCase_	, unittest.TestCase			):




			"""simple docstring"""







			_UpperCAmelCase     :							str    							=  (FlaxGPTaModel, FlaxGPTaLMHeadModel) if is_flax_available() else ()
			_UpperCAmelCase     :							Optional[Any]    							=  (FlaxGPTaLMHeadModel,) if is_flax_available() else ()



			def  _SCREAMING_SNAKE_CASE    (				self						:  str):
									SCREAMING_SNAKE_CASE_:				Tuple							= FlaxGPTaModelTester(self)



			def  _SCREAMING_SNAKE_CASE    (				self						:  Union[str, Any]):
									for model_class_name in self.all_model_classes:
															SCREAMING_SNAKE_CASE_	,					SCREAMING_SNAKE_CASE_	,					SCREAMING_SNAKE_CASE_:				List[str]							= self.model_tester.prepare_config_and_inputs()
															self.model_tester.check_use_cache_forward(lowerCAmelCase__     ,		lowerCAmelCase__     ,		lowerCAmelCase__     ,		lowerCAmelCase__)



			def  _SCREAMING_SNAKE_CASE    (				self						:  Optional[Any]):
									for model_class_name in self.all_model_classes:
															SCREAMING_SNAKE_CASE_	,					SCREAMING_SNAKE_CASE_	,					SCREAMING_SNAKE_CASE_:				Optional[int]							= self.model_tester.prepare_config_and_inputs()
															self.model_tester.check_use_cache_forward_with_attn_mask(
															    lowerCAmelCase__     ,		lowerCAmelCase__     ,		lowerCAmelCase__     ,		lowerCAmelCase__)



			@slow
			def  _SCREAMING_SNAKE_CASE    (				self						:  Dict):
									SCREAMING_SNAKE_CASE_:				Optional[Any]							= GPTaTokenizer.from_pretrained("gpt2"     ,		pad_token="</s>"     ,		padding_side="left")
									SCREAMING_SNAKE_CASE_:				Any							= tokenizer(["Hello this is a long string", "Hey"]     ,		return_tensors="np"     ,		padding=lowerCAmelCase__     ,		truncation=lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_:				str							= FlaxGPTaLMHeadModel.from_pretrained("gpt2")
									SCREAMING_SNAKE_CASE_:				Dict							= False
									SCREAMING_SNAKE_CASE_:				Dict							= model.config.eos_token_id

									SCREAMING_SNAKE_CASE_:				str							= jax.jit(model.generate)

									SCREAMING_SNAKE_CASE_:				Union[str, Any]							= jit_generate(inputs["input_ids"]     ,		attention_mask=inputs["attention_mask"]).sequences

									SCREAMING_SNAKE_CASE_:				Dict							= tokenizer.batch_decode(lowerCAmelCase__     ,		skip_special_tokens=lowerCAmelCase__)

									SCREAMING_SNAKE_CASE_:				Any							= [
									    "Hello this is a long string of words. I'm going to start with the first one.\n",
									    "Hey, I'm not sure if I'm going to be able to do",
									]

									self.assertListEqual(lowerCAmelCase__     ,		lowerCAmelCase__)



			@is_pt_flax_cross_test
			def  _SCREAMING_SNAKE_CASE    (				self						:  int):
									SCREAMING_SNAKE_CASE_	,					SCREAMING_SNAKE_CASE_:				int							= self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
															with self.subTest(model_class.__name__):
																					# prepare inputs
																					SCREAMING_SNAKE_CASE_:				List[str]							= self._prepare_for_class(lowerCAmelCase__     ,		lowerCAmelCase__)
																					SCREAMING_SNAKE_CASE_:				List[str]							= {k: torch.tensor(v.tolist()) for k, v in prepared_inputs_dict.items()}

																					# load corresponding PyTorch class
																					SCREAMING_SNAKE_CASE_:				List[str]							= model_class.__name__[4:]  # Skip the "Flax" at the beginning
																					SCREAMING_SNAKE_CASE_:				str							= getattr(lowerCAmelCase__     ,		lowerCAmelCase__)

																					SCREAMING_SNAKE_CASE_	,					SCREAMING_SNAKE_CASE_:				List[str]							= pt_inputs["input_ids"].shape
																					SCREAMING_SNAKE_CASE_:				List[str]							= np.random.randint(0     ,		seq_length - 1     ,		size=(batch_size,))
																					for batch_idx, start_index in enumerate(lowerCAmelCase__):
																											SCREAMING_SNAKE_CASE_:				Any							= 0
																											SCREAMING_SNAKE_CASE_:				int							= 1
																											SCREAMING_SNAKE_CASE_:				List[Any]							= 0
																											SCREAMING_SNAKE_CASE_:				str							= 1
																					SCREAMING_SNAKE_CASE_:				List[Any]							= pt_model_class(lowerCAmelCase__).eval()
																					SCREAMING_SNAKE_CASE_:				Tuple							= model_class(lowerCAmelCase__     ,		dtype=jnp.floataa)

																					SCREAMING_SNAKE_CASE_:				Dict							= convert_pytorch_state_dict_to_flax(pt_model.state_dict()     ,		lowerCAmelCase__)
																					SCREAMING_SNAKE_CASE_:				List[str]							= fx_state

																					with torch.no_grad():
																											SCREAMING_SNAKE_CASE_:				Tuple							= pt_model(**lowerCAmelCase__).to_tuple()

																					SCREAMING_SNAKE_CASE_:				Dict							= fx_model(**lowerCAmelCase__).to_tuple()
																					self.assertEqual(len(lowerCAmelCase__)     ,		len(lowerCAmelCase__)     ,		"Output lengths differ between Flax and PyTorch")
																					for fx_output, pt_output in zip(lowerCAmelCase__     ,		lowerCAmelCase__):
																											self.assert_almost_equals(fx_output[:, -1]     ,		pt_output[:, -1].numpy()     ,		4E-2)

																					with tempfile.TemporaryDirectory() as tmpdirname:
																											pt_model.save_pretrained(lowerCAmelCase__)
																											SCREAMING_SNAKE_CASE_:				List[str]							= model_class.from_pretrained(lowerCAmelCase__     ,		from_pt=lowerCAmelCase__)

																					SCREAMING_SNAKE_CASE_:				int							= fx_model_loaded(**lowerCAmelCase__).to_tuple()
																					self.assertEqual(
																					    len(lowerCAmelCase__)     ,		len(lowerCAmelCase__)     ,		"Output lengths differ between Flax and PyTorch")
																					for fx_output_loaded, pt_output in zip(lowerCAmelCase__     ,		lowerCAmelCase__):
																											self.assert_almost_equals(fx_output_loaded[:, -1]     ,		pt_output[:, -1].numpy()     ,		4E-2)



			@is_pt_flax_cross_test
			def  _SCREAMING_SNAKE_CASE    (				self						:  int):
									SCREAMING_SNAKE_CASE_	,					SCREAMING_SNAKE_CASE_:				Optional[int]							= self.model_tester.prepare_config_and_inputs_for_common()
									for model_class in self.all_model_classes:
															with self.subTest(model_class.__name__):
																					# prepare inputs
																					SCREAMING_SNAKE_CASE_:				str							= self._prepare_for_class(lowerCAmelCase__     ,		lowerCAmelCase__)
																					SCREAMING_SNAKE_CASE_:				Optional[Any]							= {k: torch.tensor(v.tolist()) for k, v in prepared_inputs_dict.items()}

																					# load corresponding PyTorch class
																					SCREAMING_SNAKE_CASE_:				Optional[int]							= model_class.__name__[4:]  # Skip the "Flax" at the beginning
																					SCREAMING_SNAKE_CASE_:				List[str]							= getattr(lowerCAmelCase__     ,		lowerCAmelCase__)

																					SCREAMING_SNAKE_CASE_:				Any							= pt_model_class(lowerCAmelCase__).eval()
																					SCREAMING_SNAKE_CASE_:				Optional[int]							= model_class(lowerCAmelCase__     ,		dtype=jnp.floataa)

																					SCREAMING_SNAKE_CASE_:				Optional[int]							= load_flax_weights_in_pytorch_model(lowerCAmelCase__     ,		fx_model.params)
																					SCREAMING_SNAKE_CASE_	,					SCREAMING_SNAKE_CASE_:				Optional[Any]							= pt_inputs["input_ids"].shape
																					SCREAMING_SNAKE_CASE_:				Any							= np.random.randint(0     ,		seq_length - 1     ,		size=(batch_size,))
																					for batch_idx, start_index in enumerate(lowerCAmelCase__):
																											SCREAMING_SNAKE_CASE_:				Any							= 0
																											SCREAMING_SNAKE_CASE_:				List[Any]							= 1
																											SCREAMING_SNAKE_CASE_:				Union[str, Any]							= 0
																											SCREAMING_SNAKE_CASE_:				Optional[Any]							= 1

																					# make sure weights are tied in PyTorch
																					pt_model.tie_weights()

																					with torch.no_grad():
																											SCREAMING_SNAKE_CASE_:				List[Any]							= pt_model(**lowerCAmelCase__).to_tuple()

																					SCREAMING_SNAKE_CASE_:				Union[str, Any]							= fx_model(**lowerCAmelCase__).to_tuple()
																					self.assertEqual(len(lowerCAmelCase__)     ,		len(lowerCAmelCase__)     ,		"Output lengths differ between Flax and PyTorch")
																					for fx_output, pt_output in zip(lowerCAmelCase__     ,		lowerCAmelCase__):
																											self.assert_almost_equals(fx_output[:, -1]     ,		pt_output[:, -1].numpy()     ,		4E-2)

																					with tempfile.TemporaryDirectory() as tmpdirname:
																											fx_model.save_pretrained(lowerCAmelCase__)
																											SCREAMING_SNAKE_CASE_:				List[Any]							= pt_model_class.from_pretrained(lowerCAmelCase__     ,		from_flax=lowerCAmelCase__)

																					with torch.no_grad():
																											SCREAMING_SNAKE_CASE_:				Union[str, Any]							= pt_model_loaded(**lowerCAmelCase__).to_tuple()

																					self.assertEqual(
																					    len(lowerCAmelCase__)     ,		len(lowerCAmelCase__)     ,		"Output lengths differ between Flax and PyTorch")
																					for fx_output, pt_output in zip(lowerCAmelCase__     ,		lowerCAmelCase__):
																											self.assert_almost_equals(fx_output[:, -1]     ,		pt_output[:, -1].numpy()     ,		4E-2)




			@slow
			def  _SCREAMING_SNAKE_CASE    (				self						:  Optional[int]):
									for model_class_name in self.all_model_classes:
															SCREAMING_SNAKE_CASE_:				List[str]							= model_class_name.from_pretrained("gpt2"     ,		from_pt=lowerCAmelCase__)
															SCREAMING_SNAKE_CASE_:				int							= model(np.ones((1, 1)))
															self.assertIsNotNone(lowerCAmelCase__)






