




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






 def __init__(     self    ,					lowerCamelCase    ,					lowerCamelCase=14    ,					lowerCamelCase=7    ,					lowerCamelCase=True    ,					lowerCamelCase=True    ,					lowerCamelCase=False    ,					lowerCamelCase=True    ,					lowerCamelCase=99    ,					lowerCamelCase=32    ,					lowerCamelCase=5    ,					lowerCamelCase=4    ,					lowerCamelCase=37    ,					lowerCamelCase="gelu"    ,					lowerCamelCase=0.1    ,					lowerCamelCase=0.1    ,					lowerCamelCase=512    ,					lowerCamelCase=0.0_2    ,					)				->       List[str]:




      """simple docstring"""






      __magic_name__ :							Optional[int]		       =							parent
      __magic_name__ :							Dict		       =							batch_size
      __magic_name__ :							List[str]		       =							seq_length
      __magic_name__ :							List[Any]		       =							is_training
      __magic_name__ :							int		       =							use_input_mask
      __magic_name__ :							Union[str, Any]		       =							use_token_type_ids
      __magic_name__ :							Optional[Any]		       =							use_labels
      __magic_name__ :							List[Any]		       =							vocab_size
      __magic_name__ :							Any		       =							hidden_size
      __magic_name__ :							int		       =							num_hidden_layers
      __magic_name__ :							int		       =							num_attention_heads
      __magic_name__ :							Union[str, Any]		       =							intermediate_size
      __magic_name__ :							Any		       =							hidden_act
      __magic_name__ :							Tuple		       =							hidden_dropout_prob
      __magic_name__ :							int		       =							attention_probs_dropout_prob
      __magic_name__ :							Dict		       =							max_position_embeddings
      __magic_name__ :							Dict		       =							initializer_range
      __magic_name__ :							Union[str, Any]		       =							None
      __magic_name__ :							Tuple		       =							vocab_size - 1
      __magic_name__ :							Any		       =							vocab_size - 1
      __magic_name__ :							List[str]		       =							vocab_size - 1






 def 			lowercase				(     self						)				->       Optional[Any]:




      """simple docstring"""






      __magic_name__ :							Tuple		       =							ids_tensor([self.batch_size, self.seq_length]    ,					self.vocab_size						)

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

      __magic_name__ :							str		       =							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 			lowercase				(     self						)				->       str:




      """simple docstring"""






      __magic_name__ :							List[Any]		       =							self.prepare_config_and_inputs()
      __magic_name__ :							Optional[Any]		       =							config_and_inputs
      __magic_name__ :							List[str]		       =							{'''input_ids''': input_ids, '''attention_mask''': attention_mask}
      return config, inputs_dict






 def 			lowercase				(     self						)				->       str:




      """simple docstring"""






      __magic_name__ :							int		       =							self.prepare_config_and_inputs()

      __magic_name__ :							Dict		       =							floats_tensor([self.batch_size, self.seq_length, self.hidden_size]						)
      __magic_name__ :							Union[str, Any]		       =							ids_tensor([self.batch_size, self.seq_length]    ,					vocab_size=2						)

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






 def 			lowercase				(     self    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase						)				->       List[str]:




      """simple docstring"""






      __magic_name__ :							Any		       =							20
      __magic_name__ :							Any		       =							model_class_name(lowerCamelCase						)

      __magic_name__ :							Optional[int]		       =							model.init_cache(input_ids.shape[0]    ,					lowerCamelCase						)
      __magic_name__ :							Optional[Any]		       =							jnp.ones((input_ids.shape[0], max_decoder_length)    ,					dtype='''i4'''						)

      __magic_name__ :							int		       =							jnp.broadcast_to(
          jnp.arange(input_ids.shape[-1] - 1						)[None, :]    ,					(input_ids.shape[0], input_ids.shape[-1] - 1)						)
      __magic_name__ :							Tuple		       =							model(
          input_ids[:, :-1]    ,					attention_mask=lowerCamelCase    ,					past_key_values=lowerCamelCase    ,					position_ids=lowerCamelCase    ,					)

      __magic_name__ :							Dict		       =							jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]    ,					dtype='''i4'''						)
      __magic_name__ :							Tuple		       =							model(
          input_ids[:, -1:]    ,					attention_mask=lowerCamelCase    ,					past_key_values=outputs_cache.past_key_values    ,					position_ids=lowerCamelCase    ,					)

      __magic_name__ :							Union[str, Any]		       =							model(lowerCamelCase						)

      __magic_name__ :							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}'''						)






 def 			lowercase				(     self    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase						)				->       Union[str, Any]:




      """simple docstring"""






      __magic_name__ :							str		       =							20
      __magic_name__ :							Any		       =							model_class_name(lowerCamelCase						)

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

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

      __magic_name__ :							List[Any]		       =							model(
          input_ids[:, :-1]    ,					attention_mask=lowerCamelCase    ,					past_key_values=lowerCamelCase    ,					position_ids=lowerCamelCase    ,					)
      __magic_name__ :							List[Any]		       =							jnp.array(input_ids.shape[0] * [[input_ids.shape[-1] - 1]]    ,					dtype='''i4'''						)
      __magic_name__ :							List[str]		       =							model(
          input_ids[:, -1:]    ,					past_key_values=outputs_cache.past_key_values    ,					attention_mask=lowerCamelCase    ,					position_ids=lowerCamelCase    ,					)

      __magic_name__ :							List[Any]		       =							model(lowerCamelCase    ,					attention_mask=lowerCamelCase						)

      __magic_name__ :							Tuple		       =							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 		A__    (       __SCREAMING_SNAKE_CASE		,					__SCREAMING_SNAKE_CASE		,					unittest.TestCase			):
 lowerCamelCase__		:			Union[str, Any]       =(FlaxGPTaModel, FlaxGPTaLMHeadModel) if is_flax_available() else ()
 lowerCamelCase__		:			List[str]       =(FlaxGPTaLMHeadModel,) if is_flax_available() else ()






 def 			lowercase				(     self						)				->       Dict:




      """simple docstring"""






      __magic_name__ :							Any		       =							FlaxGPTaModelTester(self						)






 def 			lowercase				(     self						)				->       List[str]:




      """simple docstring"""






      for model_class_name in self.all_model_classes:
           __magic_name__ :							List[str]		       =							self.model_tester.prepare_config_and_inputs()
           self.model_tester.check_use_cache_forward(lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase						)






 def 			lowercase				(     self						)				->       Dict:




      """simple docstring"""






      for model_class_name in self.all_model_classes:
           __magic_name__ :							Dict		       =							self.model_tester.prepare_config_and_inputs()
           self.model_tester.check_use_cache_forward_with_attn_mask(
               lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase    ,					lowerCamelCase						)






 @slow
 def 			lowercase				(     self						)				->       str:




      """simple docstring"""






      __magic_name__ :							str		       =							GPTaTokenizer.from_pretrained('''gpt2'''    ,					pad_token='''</s>'''    ,					padding_side='''left'''						)
      __magic_name__ :							Optional[Any]		       =							tokenizer(['''Hello this is a long string''', '''Hey''']    ,					return_tensors='''np'''    ,					padding=lowerCamelCase    ,					truncation=lowerCamelCase						)

      __magic_name__ :							Optional[int]		       =							FlaxGPTaLMHeadModel.from_pretrained('''gpt2'''						)
      __magic_name__ :							Any		       =							False
      __magic_name__ :							Optional[Any]		       =							model.config.eos_token_id

      __magic_name__ :							List[Any]		       =							jax.jit(model.generate						)

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

      __magic_name__ :							List[Any]		       =							tokenizer.batch_decode(lowerCamelCase    ,					skip_special_tokens=lowerCamelCase						)

      __magic_name__ :							List[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 			lowercase				(     self						)				->       List[str]:




      """simple docstring"""






      __magic_name__ :							Optional[Any]		       =							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
                __magic_name__ :							str		       =							self._prepare_for_class(lowerCamelCase    ,					lowerCamelCase						)
                __magic_name__ :							Union[str, Any]		       =							{k: torch.tensor(v.tolist()						) for k, v in prepared_inputs_dict.items()}

                # load corresponding PyTorch class
                __magic_name__ :							List[Any]		       =							model_class.__name__[4:]  # Skip the "Flax" at the beginning
                __magic_name__ :							int		       =							getattr(lowerCamelCase    ,					lowerCamelCase						)

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

                __magic_name__ :							Tuple		       =							convert_pytorch_state_dict_to_flax(pt_model.state_dict()    ,					lowerCamelCase						)
                __magic_name__ :							Any		       =							fx_state

                with torch.no_grad():
                     __magic_name__ :							Optional[Any]		       =							pt_model(**lowerCamelCase						).to_tuple()

                __magic_name__ :							List[str]		       =							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						)
                     __magic_name__ :							List[Any]		       =							model_class.from_pretrained(lowerCamelCase    ,					from_pt=lowerCamelCase						)

                __magic_name__ :							Any		       =							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 			lowercase				(     self						)				->       Optional[int]:




      """simple docstring"""






      __magic_name__ :							Tuple		       =							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
                __magic_name__ :							str		       =							self._prepare_for_class(lowerCamelCase    ,					lowerCamelCase						)
                __magic_name__ :							Optional[Any]		       =							{k: torch.tensor(v.tolist()						) for k, v in prepared_inputs_dict.items()}

                # load corresponding PyTorch class
                __magic_name__ :							Optional[Any]		       =							model_class.__name__[4:]  # Skip the "Flax" at the beginning
                __magic_name__ :							str		       =							getattr(lowerCamelCase    ,					lowerCamelCase						)

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

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

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

                with torch.no_grad():
                     __magic_name__ :							Any		       =							pt_model(**lowerCamelCase						).to_tuple()

                __magic_name__ :							int		       =							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						)
                     __magic_name__ :							List[str]		       =							pt_model_class.from_pretrained(lowerCamelCase    ,					from_flax=lowerCamelCase						)

                with torch.no_grad():
                     __magic_name__ :							Optional[int]		       =							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 			lowercase				(     self						)				->       Optional[int]:




      """simple docstring"""






      for model_class_name in self.all_model_classes:
           __magic_name__ :							str		       =							model_class_name.from_pretrained('''gpt2'''    ,					from_pt=lowerCamelCase						)
           __magic_name__ :							Dict		       =							model(np.ones((1, 1)						)						)
           self.assertIsNotNone(lowerCamelCase						)









import unittest
from pathlib import Path

from transformers import is_vision_available, load_tool
from transformers.testing_utils import get_tests_dir

from .test_tools_common import ToolTesterMixin


if is_vision_available():
				from PIL import Image





class 		A__    (       unittest.TestCase		,					__SCREAMING_SNAKE_CASE			):






	def 			lowercase				(     self						)				->       Optional[int]:




						"""simple docstring"""






						__magic_name__ :							Tuple		       =							load_tool('''image-question-answering'''						)
						self.tool.setup()
						__magic_name__ :							Any		       =							load_tool('''image-question-answering'''    ,					remote=lowerCamelCase						)






	def 			lowercase				(     self						)				->       Optional[int]:




						"""simple docstring"""






						__magic_name__ :							Dict		       =							Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO'''						)						) / '''000000039769.png'''						)
						__magic_name__ :							int		       =							self.tool(lowerCamelCase    ,					'''How many cats are sleeping on the couch?'''						)
						self.assertEqual(lowerCamelCase    ,					'''2'''						)






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




						"""simple docstring"""






						__magic_name__ :							Union[str, Any]		       =							Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO'''						)						) / '''000000039769.png'''						)
						__magic_name__ :							int		       =							self.remote_tool(lowerCamelCase    ,					'''How many cats are sleeping on the couch?'''						)
						self.assertEqual(lowerCamelCase    ,					'''2'''						)






	def 			lowercase				(     self						)				->       Any:




						"""simple docstring"""






						__magic_name__ :							int		       =							Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO'''						)						) / '''000000039769.png'''						)
						__magic_name__ :							Tuple		       =							self.tool(image=lowerCamelCase    ,					question='''How many cats are sleeping on the couch?'''						)
						self.assertEqual(lowerCamelCase    ,					'''2'''						)






	def 			lowercase				(     self						)				->       Dict:




						"""simple docstring"""






						__magic_name__ :							Optional[int]		       =							Image.open(Path(get_tests_dir('''fixtures/tests_samples/COCO'''						)						) / '''000000039769.png'''						)
						__magic_name__ :							Tuple		       =							self.remote_tool(image=lowerCamelCase    ,					question='''How many cats are sleeping on the couch?'''						)
						self.assertEqual(lowerCamelCase    ,					'''2'''						)



