



"""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 			__snake_case			:




  def __init__(					self	:		Dict		,   __lowerCAmelCase	:		str		,   __lowerCAmelCase	:		Tuple=1_3		,   __lowerCAmelCase	:		Optional[int]=7		,   __lowerCAmelCase	:		Union[str, Any]=True		,   __lowerCAmelCase	:		List[str]=True		,   __lowerCAmelCase	:		Tuple=True		,   __lowerCAmelCase	:		Tuple=True		,   __lowerCAmelCase	:		Optional[Any]=9_9		,   __lowerCAmelCase	:		str=3_2		,   __lowerCAmelCase	:		Dict=5		,   __lowerCAmelCase	:		int=4		,   __lowerCAmelCase	:		Tuple=4		,   __lowerCAmelCase	:		List[str]="gelu"		,   __lowerCAmelCase	:		Dict=0.0		,   __lowerCAmelCase	:		Union[str, Any]=0.1		,   __lowerCAmelCase	:		str=True		,   __lowerCAmelCase	:		str=5_1_2		,   __lowerCAmelCase	:		str=1_6		,   __lowerCAmelCase	:		List[str]=2		,   __lowerCAmelCase	:		Optional[Any]=0.02		,   __lowerCAmelCase	:		int=3		,   __lowerCAmelCase	:		int=4		,   __lowerCAmelCase	:		Union[str, Any]=None		,   ):






         """simple docstring"""






         _lowerCamelCase		:							int            =					parent
         _lowerCamelCase		:							Tuple            =					batch_size
         _lowerCamelCase		:							Any            =					seq_length
         _lowerCamelCase		:							List[Any]            =					is_training
         _lowerCamelCase		:							Union[str, Any]            =					use_input_mask
         _lowerCamelCase		:							Any            =					use_token_type_ids
         _lowerCamelCase		:							Tuple            =					use_labels
         _lowerCamelCase		:							Union[str, Any]            =					vocab_size
         _lowerCamelCase		:							str            =					hidden_size
         _lowerCamelCase		:							Optional[Any]            =					num_hidden_layers
         _lowerCamelCase		:							Tuple            =					num_attention_heads
         _lowerCamelCase		:							Dict            =					intermediate_multiple_size
         _lowerCamelCase		:							str            =					hidden_act
         _lowerCamelCase		:							List[Any]            =					hidden_dropout
         _lowerCamelCase		:							Union[str, Any]            =					attention_dropout
         _lowerCamelCase		:							Tuple            =					weight_tying
         _lowerCamelCase		:							Any            =					max_position_embeddings
         _lowerCamelCase		:							Tuple            =					type_vocab_size
         _lowerCamelCase		:							str            =					type_sequence_label_size
         _lowerCamelCase		:							Union[str, Any]            =					initializer_range
         _lowerCamelCase		:							Dict            =					num_labels
         _lowerCamelCase		:							Union[str, Any]            =					num_choices
         _lowerCamelCase		:							int            =					scope




  def 		SCREAMING_SNAKE_CASE							(					self	:		Optional[Any]						):






         """simple docstring"""






         _lowerCamelCase		:							List[Any]            =					ids_tensor([self.batch_size, self.seq_length]		,   self.vocab_size						)

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

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

         _lowerCamelCase		:							Optional[Any]            =					self.get_config()

         return config, input_ids, input_mask, token_labels




  def 		SCREAMING_SNAKE_CASE							(					self	:		List[str]						):






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




  def 		SCREAMING_SNAKE_CASE							(					self	:		Optional[int]						):






         """simple docstring"""






         _lowerCamelCase     ,       _lowerCamelCase     ,       _lowerCamelCase     ,       _lowerCamelCase		:							Optional[Any]            =					self.prepare_config_and_inputs()

         _lowerCamelCase		:							Optional[Any]            =					True

         return config, input_ids, input_mask, token_labels




  def 		SCREAMING_SNAKE_CASE							(					self	:		List[str]		,   __lowerCAmelCase	:		Union[str, Any]		,   __lowerCAmelCase	:		Optional[Any]		,   __lowerCAmelCase	:		Any						):






         """simple docstring"""






         _lowerCamelCase		:							str            =					GPTNeoXJapaneseModel(config=__lowerCAmelCase						)
         model.to(__lowerCAmelCase						)
         model.eval()
         _lowerCamelCase		:							Tuple            =					model(__lowerCAmelCase		,   attention_mask=__lowerCAmelCase						)
         _lowerCamelCase		:							int            =					model(__lowerCAmelCase						)
         self.parent.assertEqual(result.last_hidden_state.shape		,   (self.batch_size, self.seq_length, self.hidden_size)						)




  def 		SCREAMING_SNAKE_CASE							(					self	:		List[str]		,   __lowerCAmelCase	:		str		,   __lowerCAmelCase	:		List[Any]		,   __lowerCAmelCase	:		Any						):






         """simple docstring"""






         _lowerCamelCase		:							str            =					True
         _lowerCamelCase		:							Optional[Any]            =					GPTNeoXJapaneseModel(__lowerCAmelCase						)
         model.to(__lowerCAmelCase						)
         model.eval()
         _lowerCamelCase		:							Union[str, Any]            =					model(__lowerCAmelCase		,   attention_mask=__lowerCAmelCase						)
         self.parent.assertEqual(result.last_hidden_state.shape		,   (self.batch_size, self.seq_length, self.hidden_size)						)




  def 		SCREAMING_SNAKE_CASE							(					self	:		Tuple		,   __lowerCAmelCase	:		str		,   __lowerCAmelCase	:		str		,   __lowerCAmelCase	:		List[Any]		,   __lowerCAmelCase	:		Tuple						):






         """simple docstring"""






         _lowerCamelCase		:							List[Any]            =					GPTNeoXJapaneseForCausalLM(config=__lowerCAmelCase						)
         model.to(__lowerCAmelCase						)
         model.eval()
         _lowerCamelCase		:							str            =					model(__lowerCAmelCase		,   attention_mask=__lowerCAmelCase		,   labels=__lowerCAmelCase						)
         self.parent.assertEqual(result.logits.shape		,   (self.batch_size, self.seq_length, self.vocab_size)						)




  def 		SCREAMING_SNAKE_CASE							(					self	:		Tuple		,   __lowerCAmelCase	:		Union[str, Any]		,   __lowerCAmelCase	:		Optional[Any]		,   __lowerCAmelCase	:		Any						):






         """simple docstring"""






         _lowerCamelCase		:							List[str]            =					True
         _lowerCamelCase		:							Tuple            =					GPTNeoXJapaneseForCausalLM(config=__lowerCAmelCase						)
         model.to(__lowerCAmelCase						)
         model.eval()

         # first forward pass
         _lowerCamelCase		:							Optional[Any]            =					model(__lowerCAmelCase		,   attention_mask=__lowerCAmelCase		,   use_cache=__lowerCAmelCase						)
         _lowerCamelCase		:							str            =					outputs.past_key_values

         # create hypothetical multiple next token and extent to next_input_ids
         _lowerCamelCase		:							Optional[Any]            =					ids_tensor((self.batch_size, 3)		,   config.vocab_size						)
         _lowerCamelCase		:							Any            =					ids_tensor((self.batch_size, 3)		,   vocab_size=2						)

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

         _lowerCamelCase		:							Any            =					model(__lowerCAmelCase		,   attention_mask=__lowerCAmelCase		,   output_hidden_states=__lowerCAmelCase						)
         _lowerCamelCase		:							Optional[Any]            =					output_from_no_past['''hidden_states'''][0]
         _lowerCamelCase		:							List[Any]            =					model(
             __lowerCAmelCase		,   attention_mask=__lowerCAmelCase		,   past_key_values=__lowerCAmelCase		,   output_hidden_states=__lowerCAmelCase		,   )['''hidden_states'''][0]

         # select random slice
         _lowerCamelCase		:							Optional[int]            =					ids_tensor((1,)		,   output_from_past.shape[-1]						).item()
         _lowerCamelCase		:							List[str]            =					output_from_no_past[:, -3:, random_slice_idx].detach()
         _lowerCamelCase		:							Union[str, Any]            =					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(__lowerCAmelCase		,   __lowerCAmelCase		,   atol=1E-3						)						)




  def 		SCREAMING_SNAKE_CASE							(					self	:		Tuple						):






         """simple docstring"""






         _lowerCamelCase		:							int            =					self.prepare_config_and_inputs()
         _lowerCamelCase     ,       _lowerCamelCase     ,       _lowerCamelCase     ,       _lowerCamelCase		:							Any            =					config_and_inputs
         _lowerCamelCase		:							Tuple            =					{'''input_ids''': input_ids, '''attention_mask''': input_mask}
         return config, inputs_dict






@require_torch
class 			__snake_case			(      _lowercase    ,	_lowercase    ,	unittest.TestCase):
  snake_case__							:  Union[str, Any]         =		(GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
  snake_case__							:  Optional[int]         =		(GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
  snake_case__							:  Union[str, Any]         =		(
      {"feature-extraction": GPTNeoXJapaneseModel, "text-generation": GPTNeoXJapaneseForCausalLM}
      if is_torch_available()
      else {}
  )
  snake_case__							:  str         =		False
  snake_case__							:  Any         =		False
  snake_case__							:  List[Any]         =		False
  snake_case__							:  Dict         =		False




  def 		SCREAMING_SNAKE_CASE							(					self	:		Dict						):






         """simple docstring"""






         _lowerCamelCase		:							Optional[int]            =					GPTNeoXJapaneseModelTester(self						)
         _lowerCamelCase		:							int            =					ConfigTester(self		,   config_class=__lowerCAmelCase		,   hidden_size=3_7						)




  def 		SCREAMING_SNAKE_CASE							(					self	:		str						):






         """simple docstring"""






         self.config_tester.run_common_tests()




  def 		SCREAMING_SNAKE_CASE							(					self	:		Optional[Any]						):






         """simple docstring"""






         _lowerCamelCase     ,       _lowerCamelCase     ,       _lowerCamelCase     ,       _lowerCamelCase		:							Tuple            =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_model(__lowerCAmelCase		,   __lowerCAmelCase		,   __lowerCAmelCase						)




  def 		SCREAMING_SNAKE_CASE							(					self	:		Tuple						):






         """simple docstring"""






         _lowerCamelCase     ,       _lowerCamelCase     ,       _lowerCamelCase     ,       _lowerCamelCase		:							Dict            =					self.model_tester.prepare_config_and_inputs_for_decoder()
         self.model_tester.create_and_check_model_as_decoder(__lowerCAmelCase		,   __lowerCAmelCase		,   __lowerCAmelCase						)




  def 		SCREAMING_SNAKE_CASE							(					self	:		Optional[Any]						):






         """simple docstring"""






         _lowerCamelCase     ,       _lowerCamelCase     ,       _lowerCamelCase     ,       _lowerCamelCase		:							List[str]            =					self.model_tester.prepare_config_and_inputs_for_decoder()

         _lowerCamelCase		:							Dict            =					None

         self.model_tester.create_and_check_model_as_decoder(__lowerCAmelCase		,   __lowerCAmelCase		,   __lowerCAmelCase						)




  def 		SCREAMING_SNAKE_CASE							(					self	:		Optional[Any]						):






         """simple docstring"""






         _lowerCamelCase     ,       _lowerCamelCase     ,       _lowerCamelCase     ,       _lowerCamelCase		:							str            =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_decoder_model_past_large_inputs(__lowerCAmelCase		,   __lowerCAmelCase		,   __lowerCAmelCase						)




  def 		SCREAMING_SNAKE_CASE							(					self	:		Optional[Any]						):






         """simple docstring"""






         _lowerCamelCase		:							Dict            =					self.model_tester.prepare_config_and_inputs()
         self.model_tester.create_and_check_for_causal_lm(*__lowerCAmelCase						)




  @slow
  def 		SCREAMING_SNAKE_CASE							(					self	:		str						):






         """simple docstring"""






         _lowerCamelCase		:							List[str]            =					'''abeja/gpt-neox-japanese-2.7b'''

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

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

         _lowerCamelCase		:							Optional[int]            =					GPTNeoXJapaneseTokenizer.from_pretrained(__lowerCAmelCase						)
         _lowerCamelCase		:							Union[str, Any]            =					GPTNeoXJapaneseForCausalLM.from_pretrained(__lowerCAmelCase						)

         _lowerCamelCase		:							str            =					[]
         for prompt in prompts:
                _lowerCamelCase		:							Union[str, Any]            =					tokenizer(__lowerCAmelCase		,   return_tensors='''pt'''						).input_ids
                _lowerCamelCase		:							Optional[Any]            =					model.generate(__lowerCAmelCase		,   max_length=5_0						)
                _lowerCamelCase		:							List[str]            =					tokenizer.batch_decode(__lowerCAmelCase		,   skip_special_tokens=__lowerCAmelCase						)
                predicted_outputs += generated_string
         self.assertListEqual(__lowerCAmelCase		,   __lowerCAmelCase						)









from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


UpperCAmelCase_							=				{
    """configuration_maskformer""": ["""MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP""", """MaskFormerConfig"""],
    """configuration_maskformer_swin""": ["""MaskFormerSwinConfig"""],
}

try:
							if not is_vision_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							UpperCAmelCase_							=				["""MaskFormerFeatureExtractor"""]
							UpperCAmelCase_							=				["""MaskFormerImageProcessor"""]


try:
							if not is_torch_available():
														raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
							pass
else:
							UpperCAmelCase_							=				[
							    """MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST""",
							    """MaskFormerForInstanceSegmentation""",
							    """MaskFormerModel""",
							    """MaskFormerPreTrainedModel""",
							]
							UpperCAmelCase_							=				[
							    """MaskFormerSwinBackbone""",
							    """MaskFormerSwinModel""",
							    """MaskFormerSwinPreTrainedModel""",
							]

if TYPE_CHECKING:
							from .configuration_maskformer import MASKFORMER_PRETRAINED_CONFIG_ARCHIVE_MAP, MaskFormerConfig
							from .configuration_maskformer_swin import MaskFormerSwinConfig

							try:
														if not is_vision_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .feature_extraction_maskformer import MaskFormerFeatureExtractor
														from .image_processing_maskformer import MaskFormerImageProcessor
							try:
														if not is_torch_available():
																					raise OptionalDependencyNotAvailable()
							except OptionalDependencyNotAvailable:
														pass
							else:
														from .modeling_maskformer import (
														    MASKFORMER_PRETRAINED_MODEL_ARCHIVE_LIST,
														    MaskFormerForInstanceSegmentation,
														    MaskFormerModel,
														    MaskFormerPreTrainedModel,
														)
														from .modeling_maskformer_swin import (
														    MaskFormerSwinBackbone,
														    MaskFormerSwinModel,
														    MaskFormerSwinPreTrainedModel,
														)


else:
							import sys

							UpperCAmelCase_							=				_LazyModule(__name__, globals()["""__file__"""], _import_structure)