






"""simple docstring"""




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


	'''simple docstring'''





	def __init__(     self      ,      _A      ,      _A=1_3      ,      _A=7      ,      _A=True      ,      _A=True      ,      _A=True      ,      _A=True      ,      _A=9_9      ,      _A=3_2      ,      _A=5      ,      _A=4      ,      _A=4      ,      _A="gelu"      ,      _A=0.0      ,      _A=0.1      ,      _A=True      ,      _A=5_1_2      ,      _A=1_6      ,      _A=2      ,      _A=0.02      ,      _A=3      ,      _A=4      ,      _A=None      ,      ):



		'''simple docstring'''

		_SCREAMING_SNAKE_CASE						=parent
		_SCREAMING_SNAKE_CASE						=batch_size
		_SCREAMING_SNAKE_CASE						=seq_length
		_SCREAMING_SNAKE_CASE						=is_training
		_SCREAMING_SNAKE_CASE						=use_input_mask
		_SCREAMING_SNAKE_CASE						=use_token_type_ids
		_SCREAMING_SNAKE_CASE						=use_labels
		_SCREAMING_SNAKE_CASE						=vocab_size
		_SCREAMING_SNAKE_CASE						=hidden_size
		_SCREAMING_SNAKE_CASE						=num_hidden_layers
		_SCREAMING_SNAKE_CASE						=num_attention_heads
		_SCREAMING_SNAKE_CASE						=intermediate_multiple_size
		_SCREAMING_SNAKE_CASE						=hidden_act
		_SCREAMING_SNAKE_CASE						=hidden_dropout
		_SCREAMING_SNAKE_CASE						=attention_dropout
		_SCREAMING_SNAKE_CASE						=weight_tying
		_SCREAMING_SNAKE_CASE						=max_position_embeddings
		_SCREAMING_SNAKE_CASE						=type_vocab_size
		_SCREAMING_SNAKE_CASE						=type_sequence_label_size
		_SCREAMING_SNAKE_CASE						=initializer_range
		_SCREAMING_SNAKE_CASE						=num_labels
		_SCREAMING_SNAKE_CASE						=num_choices
		_SCREAMING_SNAKE_CASE						=scope


	def 				UpperCamelCase_  (     self					):



		'''simple docstring'''

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

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

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

		_SCREAMING_SNAKE_CASE						=self.get_config()

		return config, input_ids, input_mask, token_labels


	def 				UpperCamelCase_  (     self					):



		'''simple docstring'''

		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=UpperCAmelCase_      ,      initializer_range=self.initializer_range      ,      )


	def 				UpperCamelCase_  (     self					):



		'''simple docstring'''

		_SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE						=self.prepare_config_and_inputs()

		_SCREAMING_SNAKE_CASE						=True

		return config, input_ids, input_mask, token_labels


	def 				UpperCamelCase_  (     self      ,      _A      ,      _A      ,      _A					):



		'''simple docstring'''

		_SCREAMING_SNAKE_CASE						=GPTNeoXJapaneseModel(config=UpperCAmelCase_					)
		model.to(UpperCAmelCase_					)
		model.eval()
		_SCREAMING_SNAKE_CASE						=model(UpperCAmelCase_      ,      attention_mask=UpperCAmelCase_					)
		_SCREAMING_SNAKE_CASE						=model(UpperCAmelCase_					)
		self.parent.assertEqual(result.last_hidden_state.shape      ,      (self.batch_size, self.seq_length, self.hidden_size)					)


	def 				UpperCamelCase_  (     self      ,      _A      ,      _A      ,      _A					):



		'''simple docstring'''

		_SCREAMING_SNAKE_CASE						=True
		_SCREAMING_SNAKE_CASE						=GPTNeoXJapaneseModel(UpperCAmelCase_					)
		model.to(UpperCAmelCase_					)
		model.eval()
		_SCREAMING_SNAKE_CASE						=model(UpperCAmelCase_      ,      attention_mask=UpperCAmelCase_					)
		self.parent.assertEqual(result.last_hidden_state.shape      ,      (self.batch_size, self.seq_length, self.hidden_size)					)


	def 				UpperCamelCase_  (     self      ,      _A      ,      _A      ,      _A      ,      _A					):



		'''simple docstring'''

		_SCREAMING_SNAKE_CASE						=GPTNeoXJapaneseForCausalLM(config=UpperCAmelCase_					)
		model.to(UpperCAmelCase_					)
		model.eval()
		_SCREAMING_SNAKE_CASE						=model(UpperCAmelCase_      ,      attention_mask=UpperCAmelCase_      ,      labels=UpperCAmelCase_					)
		self.parent.assertEqual(result.logits.shape      ,      (self.batch_size, self.seq_length, self.vocab_size)					)


	def 				UpperCamelCase_  (     self      ,      _A      ,      _A      ,      _A					):



		'''simple docstring'''

		_SCREAMING_SNAKE_CASE						=True
		_SCREAMING_SNAKE_CASE						=GPTNeoXJapaneseForCausalLM(config=UpperCAmelCase_					)
		model.to(UpperCAmelCase_					)
		model.eval()

		# first forward pass
		_SCREAMING_SNAKE_CASE						=model(UpperCAmelCase_      ,      attention_mask=UpperCAmelCase_      ,      use_cache=UpperCAmelCase_					)
		_SCREAMING_SNAKE_CASE						=outputs.past_key_values

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

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

		_SCREAMING_SNAKE_CASE						=model(UpperCAmelCase_      ,      attention_mask=UpperCAmelCase_      ,      output_hidden_states=UpperCAmelCase_					)
		_SCREAMING_SNAKE_CASE						=output_from_no_past['''hidden_states'''][0]
		_SCREAMING_SNAKE_CASE						=model(
		    UpperCAmelCase_      ,      attention_mask=UpperCAmelCase_      ,      past_key_values=UpperCAmelCase_      ,      output_hidden_states=UpperCAmelCase_      ,      )['''hidden_states'''][0]

		# select random slice
		_SCREAMING_SNAKE_CASE						=ids_tensor((1,)      ,      output_from_past.shape[-1]					).item()
		_SCREAMING_SNAKE_CASE						=output_from_no_past[:, -3:, random_slice_idx].detach()
		_SCREAMING_SNAKE_CASE						=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(UpperCAmelCase_      ,      UpperCAmelCase_      ,      atol=1E-3					)					)







	def 				UpperCamelCase_  (     self					):



		'''simple docstring'''

		_SCREAMING_SNAKE_CASE						=self.prepare_config_and_inputs()
		_SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE						=config_and_inputs
		_SCREAMING_SNAKE_CASE						={'''input_ids''': input_ids, '''attention_mask''': input_mask}
		return config, inputs_dict




@require_torch
class 	__UpperCAmelCase		(				_lowerCAmelCase,  _lowerCAmelCase,  unittest.TestCase	):


	'''simple docstring'''





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


	def 				UpperCamelCase_  (     self					):



		'''simple docstring'''

		_SCREAMING_SNAKE_CASE						=GPTNeoXJapaneseModelTester(self					)
		_SCREAMING_SNAKE_CASE						=ConfigTester(self      ,      config_class=UpperCAmelCase_      ,      hidden_size=3_7					)


	def 				UpperCamelCase_  (     self					):



		'''simple docstring'''

		self.config_tester.run_common_tests()


	def 				UpperCamelCase_  (     self					):



		'''simple docstring'''

		_SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE						=self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_model(UpperCAmelCase_      ,      UpperCAmelCase_      ,      UpperCAmelCase_					)


	def 				UpperCamelCase_  (     self					):



		'''simple docstring'''

		_SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE						=self.model_tester.prepare_config_and_inputs_for_decoder()
		self.model_tester.create_and_check_model_as_decoder(UpperCAmelCase_      ,      UpperCAmelCase_      ,      UpperCAmelCase_					)


	def 				UpperCamelCase_  (     self					):



		'''simple docstring'''

		_SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE						=self.model_tester.prepare_config_and_inputs_for_decoder()

		_SCREAMING_SNAKE_CASE						=None

		self.model_tester.create_and_check_model_as_decoder(UpperCAmelCase_      ,      UpperCAmelCase_      ,      UpperCAmelCase_					)


	def 				UpperCamelCase_  (     self					):



		'''simple docstring'''

		_SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE		,     _SCREAMING_SNAKE_CASE						=self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_decoder_model_past_large_inputs(UpperCAmelCase_      ,      UpperCAmelCase_      ,      UpperCAmelCase_					)


	def 				UpperCamelCase_  (     self					):



		'''simple docstring'''

		_SCREAMING_SNAKE_CASE						=self.model_tester.prepare_config_and_inputs()
		self.model_tester.create_and_check_for_causal_lm(*UpperCAmelCase_					)







	@slow
	def 				UpperCamelCase_  (     self					):



		'''simple docstring'''

		_SCREAMING_SNAKE_CASE						='''abeja/gpt-neox-japanese-2.7b'''

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

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

		_SCREAMING_SNAKE_CASE						=GPTNeoXJapaneseTokenizer.from_pretrained(UpperCAmelCase_					)
		_SCREAMING_SNAKE_CASE						=GPTNeoXJapaneseForCausalLM.from_pretrained(UpperCAmelCase_					)

		_SCREAMING_SNAKE_CASE						=[]
		for prompt in prompts:
			_SCREAMING_SNAKE_CASE						=tokenizer(UpperCAmelCase_      ,      return_tensors='''pt'''					).input_ids
			_SCREAMING_SNAKE_CASE						=model.generate(UpperCAmelCase_      ,      max_length=5_0					)
			_SCREAMING_SNAKE_CASE						=tokenizer.batch_decode(UpperCAmelCase_      ,      skip_special_tokens=UpperCAmelCase_					)
			predicted_outputs += generated_string
		self.assertListEqual(UpperCAmelCase_      ,      UpperCAmelCase_					)






"""simple docstring"""


import unittest

import numpy as np

from transformers.testing_utils import require_torch, require_vision
from transformers.utils import is_torch_available, is_vision_available

from ...test_image_processing_common import ImageProcessingSavingTestMixin, prepare_image_inputs


if is_torch_available():
      import torch

if is_vision_available():
      from PIL import Image

      from transformers import PoolFormerImageProcessor



class 	__a	(   unittest.TestCase						):






       def __init__(	self			:				Optional[Any]							,   UpperCAmelCase_			:				Any							,   UpperCAmelCase_			:				Optional[Any]=7							,   UpperCAmelCase_			:				List[Any]=3							,   UpperCAmelCase_			:				Optional[int]=30							,   UpperCAmelCase_			:				Any=400							,   UpperCAmelCase_			:				Any=True							,   UpperCAmelCase_			:				Dict=None							,   UpperCAmelCase_			:				Dict=0.9							,   UpperCAmelCase_			:				Tuple=None							,   UpperCAmelCase_			:				Tuple=True							,   UpperCAmelCase_			:				Optional[int]=[0.5, 0.5, 0.5]							,   UpperCAmelCase_			:				Union[str, Any]=[0.5, 0.5, 0.5]							,   )->	List[str]:







              """simple docstring"""
              UpperCamelCase       			=		size if size is not None else {"shortest_edge": 30}
              UpperCamelCase       			=		crop_size if crop_size is not None else {"height": 30, "width": 30}
              UpperCamelCase       			=		parent
              UpperCamelCase       			=		batch_size
              UpperCamelCase       			=		num_channels
              UpperCamelCase       			=		min_resolution
              UpperCamelCase       			=		max_resolution
              UpperCamelCase       			=		do_resize_and_center_crop
              UpperCamelCase       			=		size
              UpperCamelCase       			=		crop_pct
              UpperCamelCase       			=		crop_size
              UpperCamelCase       			=		do_normalize
              UpperCamelCase       			=		image_mean
              UpperCamelCase       			=		image_std






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Optional[Any]      )->	str:







              """simple docstring"""
              return {
                  "size": self.size,
                  "do_resize_and_center_crop": self.do_resize_and_center_crop,
                  "crop_pct": self.crop_pct,
                  "crop_size": self.crop_size,
                  "do_normalize": self.do_normalize,
                  "image_mean": self.image_mean,
                  "image_std": self.image_std,
              }





@require_torch
@require_vision
class 	__a	(   _lowerCAmelCase     ,  unittest.TestCase						):
       UpperCamelCase_		:       List[Any]				=      PoolFormerImageProcessor if is_vision_available() else None






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Optional[Any]      )->	str:







              """simple docstring"""
              UpperCamelCase       			=		PoolFormerImageProcessingTester(self      )






       @property
       def 						_SCREAMING_SNAKE_CASE							(	self			:				List[Any]      )->	Optional[Any]:







              """simple docstring"""
              return self.image_processor_tester.prepare_image_processor_dict()






       def 						_SCREAMING_SNAKE_CASE							(	self			:				Dict      )->	Optional[int]:







              """simple docstring"""
              UpperCamelCase       			=		self.image_processing_class(**self.image_processor_dict      )
              self.assertTrue(hasattr(UpperCAmelCase_							,   "do_resize_and_center_crop"      )      )
              self.assertTrue(hasattr(UpperCAmelCase_							,   "size"      )      )
              self.assertTrue(hasattr(UpperCAmelCase_							,   "crop_pct"      )      )
              self.assertTrue(hasattr(UpperCAmelCase_							,   "do_normalize"      )      )
              self.assertTrue(hasattr(UpperCAmelCase_							,   "image_mean"      )      )
              self.assertTrue(hasattr(UpperCAmelCase_							,   "image_std"      )      )






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







              """simple docstring"""
              UpperCamelCase       			=		self.image_processing_class.from_dict(self.image_processor_dict      )
              self.assertEqual(image_processor.size							,   {"shortest_edge": 30}      )
              self.assertEqual(image_processor.crop_size							,   {"height": 30, "width": 30}      )

              UpperCamelCase       			=		self.image_processing_class.from_dict(self.image_processor_dict							,   size=42							,   crop_size=84      )
              self.assertEqual(image_processor.size							,   {"shortest_edge": 42}      )
              self.assertEqual(image_processor.crop_size							,   {"height": 84, "width": 84}      )






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







              """simple docstring"""
              pass






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







              """simple docstring"""
              # Initialize image_processing
              UpperCamelCase       			=		self.image_processing_class(**self.image_processor_dict      )
              # create random PIL images
              UpperCamelCase       			=		prepare_image_inputs(self.image_processor_tester							,   equal_resolution=UpperCAmelCase_      )
              for image in image_inputs:
                     self.assertIsInstance(UpperCAmelCase_							,   Image.Image      )

              # Test not batched input
              UpperCamelCase       			=		image_processing(image_inputs[0]							,   return_tensors="pt"      ).pixel_values

              self.assertEqual(
                  encoded_images.shape							,   (
                      1,
                      self.image_processor_tester.num_channels,
                      self.image_processor_tester.crop_size["height"],
                      self.image_processor_tester.crop_size["width"],
                  )							,   )

              # Test batched
              UpperCamelCase       			=		image_processing(UpperCAmelCase_							,   return_tensors="pt"      ).pixel_values
              self.assertEqual(
                  encoded_images.shape							,   (
                      self.image_processor_tester.batch_size,
                      self.image_processor_tester.num_channels,
                      self.image_processor_tester.crop_size["height"],
                      self.image_processor_tester.crop_size["width"],
                  )							,   )






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







              """simple docstring"""
              # Initialize image_processing
              UpperCamelCase       			=		self.image_processing_class(**self.image_processor_dict      )
              # create random numpy tensors
              UpperCamelCase       			=		prepare_image_inputs(self.image_processor_tester							,   equal_resolution=UpperCAmelCase_							,   numpify=UpperCAmelCase_      )
              for image in image_inputs:
                     self.assertIsInstance(UpperCAmelCase_							,   np.ndarray      )

              # Test not batched input
              UpperCamelCase       			=		image_processing(image_inputs[0]							,   return_tensors="pt"      ).pixel_values
              self.assertEqual(
                  encoded_images.shape							,   (
                      1,
                      self.image_processor_tester.num_channels,
                      self.image_processor_tester.crop_size["height"],
                      self.image_processor_tester.crop_size["width"],
                  )							,   )

              # Test batched
              UpperCamelCase       			=		image_processing(UpperCAmelCase_							,   return_tensors="pt"      ).pixel_values
              self.assertEqual(
                  encoded_images.shape							,   (
                      self.image_processor_tester.batch_size,
                      self.image_processor_tester.num_channels,
                      self.image_processor_tester.crop_size["height"],
                      self.image_processor_tester.crop_size["width"],
                  )							,   )






       def 						_SCREAMING_SNAKE_CASE							(	self			:				str      )->	str:







              """simple docstring"""
              # Initialize image_processing
              UpperCamelCase       			=		self.image_processing_class(**self.image_processor_dict      )
              # create random PyTorch tensors
              UpperCamelCase       			=		prepare_image_inputs(self.image_processor_tester							,   equal_resolution=UpperCAmelCase_							,   torchify=UpperCAmelCase_      )
              for image in image_inputs:
                     self.assertIsInstance(UpperCAmelCase_							,   torch.Tensor      )

              # Test not batched input
              UpperCamelCase       			=		image_processing(image_inputs[0]							,   return_tensors="pt"      ).pixel_values
              self.assertEqual(
                  encoded_images.shape							,   (
                      1,
                      self.image_processor_tester.num_channels,
                      self.image_processor_tester.crop_size["height"],
                      self.image_processor_tester.crop_size["width"],
                  )							,   )

              # Test batched
              UpperCamelCase       			=		image_processing(UpperCAmelCase_							,   return_tensors="pt"      ).pixel_values
              self.assertEqual(
                  encoded_images.shape							,   (
                      self.image_processor_tester.batch_size,
                      self.image_processor_tester.num_channels,
                      self.image_processor_tester.crop_size["height"],
                      self.image_processor_tester.crop_size["width"],
                  )							,   )





