





import unittest

from transformers import GPTNeoXJapaneseConfig, is_torch_available
from transformers.models.gpt_neox_japanese.tokenization_gpt_neox_japanese import GPTNeoXJapaneseTokenizer
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch

      from transformers import GPTNeoXJapaneseForCausalLM, GPTNeoXJapaneseModel





class 				A_				:







       """simple docstring"""





       def __init__(    self	:	Optional[int]						,__A	:	Dict						,__A	:	List[str]=13						,__A	:	str=7						,__A	:	Union[str, Any]=True						,__A	:	Union[str, Any]=True						,__A	:	Optional[int]=True						,__A	:	Union[str, Any]=True						,__A	:	Optional[int]=99						,__A	:	Optional[int]=32						,__A	:	int=5						,__A	:	Dict=4						,__A	:	List[Any]=4						,__A	:	Optional[int]="gelu"						,__A	:	Optional[int]=0.0						,__A	:	Tuple=0.1						,__A	:	Dict=True						,__A	:	int=512						,__A	:	Optional[Any]=16						,__A	:	Any=2						,__A	:	Optional[int]=0.02						,__A	:	List[str]=3						,__A	:	Tuple=4						,__A	:	str=None						,)						->					Optional[int]:
         _lowercase							= parent
         _lowercase							= batch_size
         _lowercase							= seq_length
         _lowercase							= is_training
         _lowercase							= use_input_mask
         _lowercase							= use_token_type_ids
         _lowercase							= use_labels
         _lowercase							= vocab_size
         _lowercase							= hidden_size
         _lowercase							= num_hidden_layers
         _lowercase							= num_attention_heads
         _lowercase							= intermediate_multiple_size
         _lowercase							= hidden_act
         _lowercase							= hidden_dropout
         _lowercase							= attention_dropout
         _lowercase							= weight_tying
         _lowercase							= max_position_embeddings
         _lowercase							= type_vocab_size
         _lowercase							= type_sequence_label_size
         _lowercase							= initializer_range
         _lowercase							= num_labels
         _lowercase							= num_choices
         _lowercase							= scope







       def    __UpperCAmelCase (    self	:	str  )						->					str:
         _lowercase							= ids_tensor([self.batch_size, self.seq_length]						,self.vocab_size  )

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

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

         _lowercase							= self.get_config()

         return config, input_ids, input_mask, token_labels







       def    __UpperCAmelCase (    self	:	str  )						->					Optional[Any]:
         return GPTNeoXJapaneseConfig(
             vocab_size=self.vocab_size						,hidden_size=self.hidden_size						,num_hidden_layers=self.num_hidden_layers						,num_attention_heads=self.num_attention_heads						,intermediate_multiple_size=self.intermediate_multiple_size						,hidden_act=self.hidden_act						,hidden_dropout=self.hidden_dropout						,attention_dropout=self.attention_dropout						,weight_tying=self.weight_tying						,max_position_embeddings=self.max_position_embeddings						,type_vocab_size=self.type_vocab_size						,is_decoder=__A						,initializer_range=self.initializer_range						,)







       def    __UpperCAmelCase (    self	:	Any  )						->					List[Any]:
         _lowercase						,		_lowercase						,		_lowercase						,		_lowercase							= self.prepare_config_and_inputs()

         _lowercase							= True

         return config, input_ids, input_mask, token_labels







       def    __UpperCAmelCase (    self	:	Optional[int]						,__A	:	Optional[int]						,__A	:	Any						,__A	:	str  )						->					int:
         _lowercase							= GPTNeoXJapaneseModel(config=__A  )
         model.to(__A  )
         model.eval()
         _lowercase							= model(__A						,attention_mask=__A  )
         _lowercase							= model(__A  )
         self.parent.assertEqual(result.last_hidden_state.shape						,(self.batch_size, self.seq_length, self.hidden_size)  )







       def    __UpperCAmelCase (    self	:	str						,__A	:	Optional[Any]						,__A	:	Tuple						,__A	:	Any  )						->					List[Any]:
         _lowercase							= True
         _lowercase							= GPTNeoXJapaneseModel(__A  )
         model.to(__A  )
         model.eval()
         _lowercase							= model(__A						,attention_mask=__A  )
         self.parent.assertEqual(result.last_hidden_state.shape						,(self.batch_size, self.seq_length, self.hidden_size)  )







       def    __UpperCAmelCase (    self	:	int						,__A	:	Union[str, Any]						,__A	:	Optional[int]						,__A	:	int						,__A	:	Any  )						->					Optional[Any]:
         _lowercase							= GPTNeoXJapaneseForCausalLM(config=__A  )
         model.to(__A  )
         model.eval()
         _lowercase							= model(__A						,attention_mask=__A						,labels=__A  )
         self.parent.assertEqual(result.logits.shape						,(self.batch_size, self.seq_length, self.vocab_size)  )







       def    __UpperCAmelCase (    self	:	Optional[Any]						,__A	:	Any						,__A	:	Tuple						,__A	:	Optional[int]  )						->					int:
         _lowercase							= True
         _lowercase							= GPTNeoXJapaneseForCausalLM(config=__A  )
         model.to(__A  )
         model.eval()

         # first forward pass
         _lowercase							= model(__A						,attention_mask=__A						,use_cache=__A  )
         _lowercase							= outputs.past_key_values

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

         # append to next input_ids and
         _lowercase							= torch.cat([input_ids, next_tokens]						,dim=-1  )
         _lowercase							= torch.cat([input_mask, next_mask]						,dim=-1  )

         _lowercase							= model(__A						,attention_mask=__A						,output_hidden_states=__A  )
         _lowercase							= output_from_no_past['hidden_states'][0]
         _lowercase							= model(
             __A						,attention_mask=__A						,past_key_values=__A						,output_hidden_states=__A						,)['hidden_states'][0]

         # select random slice
         _lowercase							= ids_tensor((1,)						,output_from_past.shape[-1]  ).item()
         _lowercase							= output_from_no_past[:, -3:, random_slice_idx].detach()
         _lowercase							= output_from_past[:, :, random_slice_idx].detach()

         self.parent.assertTrue(output_from_past_slice.shape[1] == next_tokens.shape[1]  )

         # test that outputs are equal for slice
         self.parent.assertTrue(torch.allclose(__A						,__A						,atol=1e-3  )  )







       def    __UpperCAmelCase (    self	:	Dict  )						->					Optional[int]:
         _lowercase							= self.prepare_config_and_inputs()
         _lowercase						,		_lowercase						,		_lowercase						,		_lowercase							= config_and_inputs
         _lowercase							= {'input_ids': input_ids, 'attention_mask': input_mask}
         return config, inputs_dict





@require_torch
class 				A_				(      UpperCAmelCase					,	UpperCAmelCase					,	unittest.TestCase	):







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : Optional[Any]      =  (GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
       SCREAMING_SNAKE_CASE_      : Any      =  (GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
       SCREAMING_SNAKE_CASE_      : Any      =  (
           {'''feature-extraction''': GPTNeoXJapaneseModel, '''text-generation''': GPTNeoXJapaneseForCausalLM}
           if is_torch_available()
           else {}
       )
       SCREAMING_SNAKE_CASE_      : Tuple      =  False
       SCREAMING_SNAKE_CASE_      : int      =  False
       SCREAMING_SNAKE_CASE_      : Tuple      =  False
       SCREAMING_SNAKE_CASE_      : List[Any]      =  False







       def    __UpperCAmelCase (    self	:	List[Any]  )						->					Any:
         _lowercase							= GPTNeoXJapaneseModelTester(self  )
         _lowercase							= ConfigTester(self						,config_class=__A						,hidden_size=37  )







       def    __UpperCAmelCase (    self	:	Optional[Any]  )						->					Union[str, Any]:
         self.config_tester.run_common_tests()







       def    __UpperCAmelCase (    self	:	Union[str, Any]  )						->					Union[str, Any]:
         _lowercase						,		_lowercase						,		_lowercase						,		_lowercase							= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(__A						,__A						,__A  )







       def    __UpperCAmelCase (    self	:	List[str]  )						->					List[Any]:
         _lowercase						,		_lowercase						,		_lowercase						,		_lowercase							= self.model_tester.prepare_config_and_inputs_for_decoder()
         self.model_tester.create_and_check_model_as_decoder(__A						,__A						,__A  )







       def    __UpperCAmelCase (    self	:	Dict  )						->					Any:
         # This regression test was failing with PyTorch < 1.3
         _lowercase						,		_lowercase						,		_lowercase						,		_lowercase							= self.model_tester.prepare_config_and_inputs_for_decoder()

         _lowercase							= None

         self.model_tester.create_and_check_model_as_decoder(__A						,__A						,__A  )







       def    __UpperCAmelCase (    self	:	str  )						->					Tuple:
         _lowercase						,		_lowercase						,		_lowercase						,		_lowercase							= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_decoder_model_past_large_inputs(__A						,__A						,__A  )







       def    __UpperCAmelCase (    self	:	int  )						->					Tuple:
         _lowercase							= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_causal_lm(*__A  )







       @slow
       def    __UpperCAmelCase (    self	:	List[str]  )						->					str:
         _lowercase							= 'abeja/gpt-neox-japanese-2.7b'

         _lowercase							= ['データサイエンティストとは、', '100年後に必要とされる会社は、', 'フルリモートの環境で働くために必要なことは、', '国境の長いトンネルを抜けると', '美味しい日本食といえば、']

         _lowercase							= [
             'データサイエンティストとは、データを分析し、ビジネスに役立つ知見を導き出す専門家のことです。',
             '100年後に必要とされる会社は、「人」が中心の会社です。',
             'フルリモートの環境で働くために必要なことは、「自分の時間をコントロールする」ことです。',
             '国境の長いトンネルを抜けると、そこは雪国だった。',
             '美味しい日本食といえば、やっぱりお寿司ですよね。',
         ]

         _lowercase							= GPTNeoXJapaneseTokenizer.from_pretrained(__A  )
         _lowercase							= GPTNeoXJapaneseForCausalLM.from_pretrained(__A  )

         _lowercase							= []
         for prompt in prompts:
           _lowercase							= tokenizer(__A						,return_tensors='pt'  ).input_ids
           _lowercase							= model.generate(__A						,max_length=50  )
           _lowercase							= tokenizer.batch_decode(__A						,skip_special_tokens=__A  )
           predicted_outputs += generated_string
         self.assertListEqual(__A						,__A  )






import inspect
import unittest

from transformers import RegNetConfig, is_flax_available
from transformers.testing_utils import require_flax, slow
from transformers.utils import cached_property, is_vision_available

from ...test_configuration_common import ConfigTester
from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor


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

      from transformers.models.regnet.modeling_flax_regnet import FlaxRegNetForImageClassification, FlaxRegNetModel

if is_vision_available():
      from PIL import Image

      from transformers import AutoImageProcessor





class 				A_				(      unittest.TestCase	):







       """simple docstring"""





       def __init__(    self	:	List[str]						,__A	:	str						,__A	:	Dict=3						,__A	:	str=32						,__A	:	int=3						,__A	:	Union[str, Any]=10						,__A	:	Optional[Any]=[10, 20, 30, 40]						,__A	:	List[str]=[1, 1, 2, 1]						,__A	:	Optional[int]=True						,__A	:	Optional[Any]=True						,__A	:	int="relu"						,__A	:	Union[str, Any]=3						,__A	:	List[Any]=None						,)						->					int:
         _lowercase							= parent
         _lowercase							= batch_size
         _lowercase							= image_size
         _lowercase							= num_channels
         _lowercase							= embeddings_size
         _lowercase							= hidden_sizes
         _lowercase							= depths
         _lowercase							= is_training
         _lowercase							= use_labels
         _lowercase							= hidden_act
         _lowercase							= num_labels
         _lowercase							= scope
         _lowercase							= len(__A  )







       def    __UpperCAmelCase (    self	:	Optional[Any]  )						->					Any:
         _lowercase							= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]  )

         _lowercase							= self.get_config()

         return config, pixel_values







       def    __UpperCAmelCase (    self	:	Any  )						->					int:
         return RegNetConfig(
             num_channels=self.num_channels						,embeddings_size=self.embeddings_size						,hidden_sizes=self.hidden_sizes						,depths=self.depths						,hidden_act=self.hidden_act						,num_labels=self.num_labels						,image_size=self.image_size						,)







       def    __UpperCAmelCase (    self	:	Tuple						,__A	:	Any						,__A	:	Union[str, Any]  )						->					List[Any]:
         _lowercase							= FlaxRegNetModel(config=__A  )
         _lowercase							= model(__A  )

         # Output shape (b, c, h, w)
         self.parent.assertEqual(
             result.last_hidden_state.shape						,(self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)						,)







       def    __UpperCAmelCase (    self	:	List[Any]						,__A	:	Optional[int]						,__A	:	Dict  )						->					Any:
         _lowercase							= self.num_labels
         _lowercase							= FlaxRegNetForImageClassification(config=__A  )
         _lowercase							= model(__A  )
         self.parent.assertEqual(result.logits.shape						,(self.batch_size, self.num_labels)  )







       def    __UpperCAmelCase (    self	:	Tuple  )						->					Any:
         _lowercase							= self.prepare_config_and_inputs()
         _lowercase						,		_lowercase							= config_and_inputs
         _lowercase							= {'pixel_values': pixel_values}
         return config, inputs_dict





@require_flax
class 				A_				(      UpperCAmelCase					,	unittest.TestCase	):







       """simple docstring"""





       SCREAMING_SNAKE_CASE_      : Tuple      =  (FlaxRegNetModel, FlaxRegNetForImageClassification) if is_flax_available() else ()

       SCREAMING_SNAKE_CASE_      : Union[str, Any]      =  False
       SCREAMING_SNAKE_CASE_      : Any      =  False
       SCREAMING_SNAKE_CASE_      : Union[str, Any]      =  False







       def    __UpperCAmelCase (    self	:	Dict  )						->					None:
         _lowercase							= FlaxRegNetModelTester(self  )
         _lowercase							= ConfigTester(self						,config_class=__A						,has_text_modality=__A  )







       def    __UpperCAmelCase (    self	:	List[str]  )						->					Union[str, Any]:
         self.create_and_test_config_common_properties()
         self.config_tester.create_and_test_config_to_json_string()
         self.config_tester.create_and_test_config_to_json_file()
         self.config_tester.create_and_test_config_from_and_save_pretrained()
         self.config_tester.create_and_test_config_with_num_labels()
         self.config_tester.check_config_can_be_init_without_params()
         self.config_tester.check_config_arguments_init()







       def    __UpperCAmelCase (    self	:	Optional[int]  )						->					Any:
         return







       def    __UpperCAmelCase (    self	:	Dict  )						->					int:
         _lowercase							= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(*__A  )







       def    __UpperCAmelCase (    self	:	Optional[Any]  )						->					Any:
         _lowercase							= self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_image_classification(*__A  )







       @unittest.skip(reason='RegNet does not use inputs_embeds'  )
       def    __UpperCAmelCase (    self	:	List[str]  )						->					Optional[int]:
         pass







       @unittest.skip(reason='RegNet does not support input and output embeddings'  )
       def    __UpperCAmelCase (    self	:	Tuple  )						->					Any:
         pass







       def    __UpperCAmelCase (    self	:	Dict  )						->					List[str]:
         _lowercase						,		_lowercase							= self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
           _lowercase							= model_class(__A  )
           _lowercase							= inspect.signature(model.__call__  )
           # signature.parameters is an OrderedDict => so arg_names order is deterministic
           _lowercase							= [*signature.parameters.keys()]

           _lowercase							= ['pixel_values']
           self.assertListEqual(arg_names[:1]						,__A  )







       def    __UpperCAmelCase (    self	:	Union[str, Any]  )						->					List[str]:
         def check_hidden_states_output(__A	:	Optional[Any]						,__A	:	Optional[int]						,__A	:	str  ):
           _lowercase							= model_class(__A  )

           _lowercase							= model(**self._prepare_for_class(__A						,__A  )  )

           _lowercase							= outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

           _lowercase							= self.model_tester.num_stages
           self.assertEqual(len(__A  )						,expected_num_stages + 1  )

         _lowercase						,		_lowercase							= self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
           _lowercase							= True
           check_hidden_states_output(__A						,__A						,__A  )

           # check that output_hidden_states also work using config
           del inputs_dict["output_hidden_states"]
           _lowercase							= True

           check_hidden_states_output(__A						,__A						,__A  )







       def    __UpperCAmelCase (    self	:	Optional[Any]  )						->					int:
         _lowercase						,		_lowercase							= self.model_tester.prepare_config_and_inputs_for_common()

         for model_class in self.all_model_classes:
           with self.subTest(model_class.__name__  ):
             _lowercase							= self._prepare_for_class(__A						,__A  )
             _lowercase							= model_class(__A  )

             @jax.jit
             def model_jitted(__A	:	Any						,**__A	:	Union[str, Any]  ):
               return model(pixel_values=__A						,**__A  )

             with self.subTest('JIT Enabled'  ):
               _lowercase							= model_jitted(**__A  ).to_tuple()

             with self.subTest('JIT Disabled'  ):
               with jax.disable_jit():
                 _lowercase							= model_jitted(**__A  ).to_tuple()

             self.assertEqual(len(__A  )						,len(__A  )  )
             for jitted_output, output in zip(__A						,__A  ):
               self.assertEqual(jitted_output.shape						,output.shape  )

def 							SCREAMING_SNAKE_CASE__       (  )      ->						str:
  _lowercase							= Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'     )
  return image







@require_flax
class 				A_				(      unittest.TestCase	):







       """simple docstring"""





       @cached_property
       def    __UpperCAmelCase (    self	:	str  )						->					Any:
         return AutoImageProcessor.from_pretrained('facebook/regnet-y-040'  ) if is_vision_available() else None







       @slow
       def    __UpperCAmelCase (    self	:	Optional[Any]  )						->					Any:
         _lowercase							= FlaxRegNetForImageClassification.from_pretrained('facebook/regnet-y-040'  )

         _lowercase							= self.default_image_processor
         _lowercase							= prepare_img()
         _lowercase							= image_processor(images=__A						,return_tensors='np'  )

         _lowercase							= model(**__A  )

         # verify the logits
         _lowercase							= (1, 1000)
         self.assertEqual(outputs.logits.shape						,__A  )

         _lowercase							= jnp.array([-0.4180, -1.5051, -3.4836]  )

         self.assertTrue(jnp.allclose(outputs.logits[0, :3]						,__A						,atol=1e-4  )  )