

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				:			List[Any]   ,   a				:			List[Any]   ,   a				:			List[Any]=13   ,   a				:			Union[str, Any]=7   ,   a				:			Optional[int]=True   ,   a				:			Optional[int]=True   ,   a				:			int=True   ,   a				:			Any=True   ,   a				:			Dict=99   ,   a				:			Tuple=32   ,   a				:			Optional[int]=5   ,   a				:			List[Any]=4   ,   a				:			Optional[int]=4   ,   a				:			List[str]="gelu"   ,   a				:			Optional[int]=0.0   ,   a				:			int=0.1   ,   a				:			List[Any]=True   ,   a				:			Union[str, Any]=512   ,   a				:			Tuple=16   ,   a				:			Union[str, Any]=2   ,   a				:			List[str]=0.02   ,   a				:			Any=3   ,   a				:			int=4   ,   a				:			int=None   ,   )				->							Tuple:

				"""simple docstring"""

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

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   int					  =							ids_tensor([self.batch_size, self.seq_length]   ,   self.vocab_size						)

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

				SCREAMING_SNAKE_CASE :   Optional[int]					  =							None
				if self.use_labels:
					SCREAMING_SNAKE_CASE :   Dict					  =							ids_tensor([self.batch_size, self.seq_length]   ,   self.num_labels						)

				SCREAMING_SNAKE_CASE :   List[Any]					  =							self.get_config()

				return config, input_ids, input_mask, token_labels
			def 				__UpperCamelCase			(      self				:			str						)				->							List[Any]:

				"""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=a   ,   initializer_range=self.initializer_range   ,   )
			def 				__UpperCamelCase			(      self				:			Union[str, Any]						)				->							Any:

				"""simple docstring"""

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

				SCREAMING_SNAKE_CASE :   Optional[int]					  =							True

				return config, input_ids, input_mask, token_labels
			def 				__UpperCamelCase			(      self				:			List[str]   ,   a				:			Union[str, Any]   ,   a				:			Optional[Any]   ,   a				:			int						)				->							Optional[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   List[str]					  =							GPTNeoXJapaneseModel(config=a						)
				model.to(a						)
				model.eval()
				SCREAMING_SNAKE_CASE :   str					  =							model(a   ,   attention_mask=a						)
				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							model(a						)
				self.parent.assertEqual(result.last_hidden_state.shape   ,   (self.batch_size, self.seq_length, self.hidden_size)						)
			def 				__UpperCamelCase			(      self				:			Tuple   ,   a				:			Optional[int]   ,   a				:			List[str]   ,   a				:			str						)				->							List[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   int					  =							True
				SCREAMING_SNAKE_CASE :   Dict					  =							GPTNeoXJapaneseModel(a						)
				model.to(a						)
				model.eval()
				SCREAMING_SNAKE_CASE :   List[Any]					  =							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				:			Optional[Any]   ,   a				:			Tuple   ,   a				:			Union[str, Any]   ,   a				:			Union[str, Any]   ,   a				:			List[Any]						)				->							Tuple:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   int					  =							GPTNeoXJapaneseForCausalLM(config=a						)
				model.to(a						)
				model.eval()
				SCREAMING_SNAKE_CASE :   Optional[int]					  =							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[int]   ,   a				:			Tuple   ,   a				:			Tuple   ,   a				:			Dict						)				->							Optional[int]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Optional[Any]					  =							True
				SCREAMING_SNAKE_CASE :   str					  =							GPTNeoXJapaneseForCausalLM(config=a						)
				model.to(a						)
				model.eval()

				# first forward pass
				SCREAMING_SNAKE_CASE :   List[str]					  =							model(a   ,   attention_mask=a   ,   use_cache=a						)
				SCREAMING_SNAKE_CASE :   Any					  =							outputs.past_key_values

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

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

				SCREAMING_SNAKE_CASE :   List[Any]					  =							model(a   ,   attention_mask=a   ,   output_hidden_states=a						)
				SCREAMING_SNAKE_CASE :   Any					  =							output_from_no_past["hidden_states"][0]
				SCREAMING_SNAKE_CASE :   Dict					  =							model(
				    a   ,   attention_mask=a   ,   past_key_values=a   ,   output_hidden_states=a   ,   )["hidden_states"][0]

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






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

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Any					  =							self.prepare_config_and_inputs()
				SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE :   Optional[int]					  =							config_and_inputs
				SCREAMING_SNAKE_CASE :   Any					  =							{"input_ids": input_ids, "attention_mask": input_mask}
				return config, inputs_dict






@require_torch
class _UpperCamelCase				(				__A				,						__A				,						unittest.TestCase ):
			'''simple docstring'''







			lowerCamelCase__							     =(GPTNeoXJapaneseModel, GPTNeoXJapaneseForCausalLM) if is_torch_available() else ()
			lowerCamelCase__							     =(GPTNeoXJapaneseForCausalLM,) if is_torch_available() else ()
			lowerCamelCase__							     =(
			    {'feature-extraction': GPTNeoXJapaneseModel, 'text-generation': GPTNeoXJapaneseForCausalLM}
			    if is_torch_available()
			    else {}
			)
			lowerCamelCase__							     =False
			lowerCamelCase__							     =False
			lowerCamelCase__							     =False
			lowerCamelCase__							     =False
			def 				__UpperCamelCase			(      self				:			List[Any]						)				->							Any:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							GPTNeoXJapaneseModelTester(self						)
				SCREAMING_SNAKE_CASE :   List[str]					  =							ConfigTester(self   ,   config_class=a   ,   hidden_size=37						)
			def 				__UpperCamelCase			(      self				:			str						)				->							Optional[Any]:

				"""simple docstring"""

				self.config_tester.run_common_tests()
			def 				__UpperCamelCase			(      self				:			Optional[Any]						)				->							int:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE :   str					  =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_model(a   ,   a   ,   a						)
			def 				__UpperCamelCase			(      self				:			List[Any]						)				->							Dict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE :   str					  =							self.model_tester.prepare_config_and_inputs_for_decoder()
				self.model_tester.create_and_check_model_as_decoder(a   ,   a   ,   a						)
			def 				__UpperCamelCase			(      self				:			str						)				->							Optional[Any]:

				"""simple docstring"""

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

				SCREAMING_SNAKE_CASE :   Dict					  =							None

				self.model_tester.create_and_check_model_as_decoder(a   ,   a   ,   a						)
			def 				__UpperCamelCase			(      self				:			Optional[Any]						)				->							str:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE  ,SCREAMING_SNAKE_CASE :   Union[str, Any]					  =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_decoder_model_past_large_inputs(a   ,   a   ,   a						)
			def 				__UpperCamelCase			(      self				:			str						)				->							List[Any]:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   List[Any]					  =							self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_causal_lm(*a						)






			@slow
			def 				__UpperCamelCase			(      self				:			Optional[Any]						)				->							Dict:

				"""simple docstring"""

				SCREAMING_SNAKE_CASE :   List[Any]					  =							"abeja/gpt-neox-japanese-2.7b"

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

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

				SCREAMING_SNAKE_CASE :   List[str]					  =							GPTNeoXJapaneseTokenizer.from_pretrained(a						)
				SCREAMING_SNAKE_CASE :   Tuple					  =							GPTNeoXJapaneseForCausalLM.from_pretrained(a						)

				SCREAMING_SNAKE_CASE :   Optional[Any]					  =							[]
				for prompt in prompts:
					SCREAMING_SNAKE_CASE :   str					  =							tokenizer(a   ,   return_tensors="pt"						).input_ids
					SCREAMING_SNAKE_CASE :   Any					  =							model.generate(a   ,   max_length=50						)
					SCREAMING_SNAKE_CASE :   Tuple					  =							tokenizer.batch_decode(a   ,   skip_special_tokens=a						)
					predicted_outputs += generated_string
				self.assertListEqual(a   ,   a						)


from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


a_      =     {
    'configuration_swinv2': ['SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP', 'Swinv2Config'],
}


try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			a_      =     [
			    'SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST',
			    'Swinv2ForImageClassification',
			    'Swinv2ForMaskedImageModeling',
			    'Swinv2Model',
			    'Swinv2PreTrainedModel',
			]


if TYPE_CHECKING:
			from .configuration_swinva import SWINV2_PRETRAINED_CONFIG_ARCHIVE_MAP, SwinvaConfig

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_swinva import (
						    SWINV2_PRETRAINED_MODEL_ARCHIVE_LIST,
						    SwinvaForImageClassification,
						    SwinvaForMaskedImageModeling,
						    SwinvaModel,
						    SwinvaPreTrainedModel,
						)


else:
			import sys

			a_      =     _LazyModule(__name__, globals()['__file__'], _import_structure, module_spec=__spec__)