


"""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 				SCREAMING_SNAKE_CASE__    :
							"""simple docstring"""


							def __init__(						self		,					snake_case__		,					snake_case__=13		,					snake_case__=7		,					snake_case__=True		,					snake_case__=True		,					snake_case__=True		,					snake_case__=True		,					snake_case__=99		,					snake_case__=32		,					snake_case__=5		,					snake_case__=4		,					snake_case__=4		,					snake_case__="gelu"		,					snake_case__=0.0		,					snake_case__=0.1		,					snake_case__=True		,					snake_case__=512		,					snake_case__=16		,					snake_case__=2		,					snake_case__=0.02		,					snake_case__=3		,					snake_case__=4		,					snake_case__=None		,					):






									"""simple docstring"""





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

							def 	lowercase__							(						self						):






									"""simple docstring"""





									lowerCAmelCase					:					str								=							ids_tensor([self.batch_size, self.seq_length]		,					self.vocab_size						)

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

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

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

									return config, input_ids, input_mask, token_labels

							def 	lowercase__							(						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=_A		,					initializer_range=self.initializer_range		,					)

							def 	lowercase__							(						self						):






									"""simple docstring"""





									lowerCAmelCase					:					str								=							self.prepare_config_and_inputs()

									lowerCAmelCase					:					Dict								=							True

									return config, input_ids, input_mask, token_labels

							def 	lowercase__							(						self		,					snake_case__		,					snake_case__		,					snake_case__						):






									"""simple docstring"""





									lowerCAmelCase					:					Optional[int]								=							GPTNeoXJapaneseModel(config=_A						)
									model.to(_A						)
									model.eval()
									lowerCAmelCase					:					str								=							model(_A		,					attention_mask=_A						)
									lowerCAmelCase					:					List[Any]								=							model(_A						)
									self.parent.assertEqual(result.last_hidden_state.shape		,					(self.batch_size, self.seq_length, self.hidden_size)						)

							def 	lowercase__							(						self		,					snake_case__		,					snake_case__		,					snake_case__						):






									"""simple docstring"""





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

							def 	lowercase__							(						self		,					snake_case__		,					snake_case__		,					snake_case__		,					snake_case__						):






									"""simple docstring"""





									lowerCAmelCase					:					str								=							GPTNeoXJapaneseForCausalLM(config=_A						)
									model.to(_A						)
									model.eval()
									lowerCAmelCase					:					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 	lowercase__							(						self		,					snake_case__		,					snake_case__		,					snake_case__						):






									"""simple docstring"""





									lowerCAmelCase					:					List[Any]								=							True
									lowerCAmelCase					:					str								=							GPTNeoXJapaneseForCausalLM(config=_A						)
									model.to(_A						)
									model.eval()

									# first forward pass
									lowerCAmelCase					:					Tuple								=							model(_A		,					attention_mask=_A		,					use_cache=_A						)
									lowerCAmelCase					:					Optional[int]								=							outputs.past_key_values

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

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

									lowerCAmelCase					:					Optional[Any]								=							model(_A		,					attention_mask=_A		,					output_hidden_states=_A						)
									lowerCAmelCase					:					List[str]								=							output_from_no_past['hidden_states'][0]
									lowerCAmelCase					:					Union[str, Any]								=							model(
									    _A		,					attention_mask=_A		,					past_key_values=_A		,					output_hidden_states=_A		,					)['hidden_states'][0]

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






									"""simple docstring"""





									lowerCAmelCase					:					Dict								=							self.prepare_config_and_inputs()
									lowerCAmelCase					:					Dict								=							config_and_inputs
									lowerCAmelCase					:					Optional[Any]								=							{'input_ids': input_ids, 'attention_mask': input_mask}
									return config, inputs_dict


@require_torch
class 				SCREAMING_SNAKE_CASE__    (		lowercase       ,   lowercase       ,   unittest.TestCase    ):
							"""simple docstring"""


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

							def 	lowercase__							(						self						):






									"""simple docstring"""





									lowerCAmelCase					:					Optional[int]								=							GPTNeoXJapaneseModelTester(self						)
									lowerCAmelCase					:					Tuple								=							ConfigTester(self		,					config_class=_A		,					hidden_size=37						)

							def 	lowercase__							(						self						):






									"""simple docstring"""





									self.config_tester.run_common_tests()

							def 	lowercase__							(						self						):






									"""simple docstring"""





									lowerCAmelCase					:					str								=							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(_A		,					_A		,					_A						)

							def 	lowercase__							(						self						):






									"""simple docstring"""





									lowerCAmelCase					:					Dict								=							self.model_tester.prepare_config_and_inputs_for_decoder()
									self.model_tester.create_and_check_model_as_decoder(_A		,					_A		,					_A						)

							def 	lowercase__							(						self						):






									"""simple docstring"""





									lowerCAmelCase					:					Dict								=							self.model_tester.prepare_config_and_inputs_for_decoder()

									lowerCAmelCase					:					Optional[Any]								=							None

									self.model_tester.create_and_check_model_as_decoder(_A		,					_A		,					_A						)

							def 	lowercase__							(						self						):






									"""simple docstring"""





									lowerCAmelCase					:					List[str]								=							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_decoder_model_past_large_inputs(_A		,					_A		,					_A						)

							def 	lowercase__							(						self						):






									"""simple docstring"""





									lowerCAmelCase					:					str								=							self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_causal_lm(*_A						)







							@slow
							def 	lowercase__							(						self						):






									"""simple docstring"""





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

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

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

									lowerCAmelCase					:					Union[str, Any]								=							GPTNeoXJapaneseTokenizer.from_pretrained(_A						)
									lowerCAmelCase					:					Dict								=							GPTNeoXJapaneseForCausalLM.from_pretrained(_A						)

									lowerCAmelCase					:					Dict								=							[]
									for prompt in prompts:
											lowerCAmelCase					:					List[Any]								=							tokenizer(_A		,					return_tensors="pt"						).input_ids
											lowerCAmelCase					:					List[str]								=							model.generate(_A		,					max_length=50						)
											lowerCAmelCase					:					int								=							tokenizer.batch_decode(_A		,					skip_special_tokens=_A						)
											predicted_outputs += generated_string
									self.assertListEqual(_A		,					_A						)









"""simple docstring"""







from typing import List, Optional

from ...configuration_utils import PretrainedConfig
from ...utils import logging


lowerCAmelCase__				    =       logging.get_logger(__name__)

lowerCAmelCase__				    =       {
    '''huggingface/autoformer-tourism-monthly''': '''https://huggingface.co/huggingface/autoformer-tourism-monthly/resolve/main/config.json''',
}


class 				SCREAMING_SNAKE_CASE__    (		lowercase    ):
       """simple docstring"""


       a  :    Dict       ="autoformer"
       a  :    Dict       ={
           "hidden_size": "d_model",
           "num_attention_heads": "encoder_attention_heads",
           "num_hidden_layers": "encoder_layers",
       }

       def __init__(						self		,					snake_case__ = None		,					snake_case__ = None		,					snake_case__ = "student_t"		,					snake_case__ = "nll"		,					snake_case__ = 1		,					snake_case__ = [1, 2, 3, 4, 5, 6, 7]		,					snake_case__ = True		,					snake_case__ = 0		,					snake_case__ = 0		,					snake_case__ = 0		,					snake_case__ = 0		,					snake_case__ = None		,					snake_case__ = None		,					snake_case__ = 64		,					snake_case__ = 2		,					snake_case__ = 2		,					snake_case__ = 2		,					snake_case__ = 2		,					snake_case__ = 32		,					snake_case__ = 32		,					snake_case__ = "gelu"		,					snake_case__ = 0.1		,					snake_case__ = 0.1		,					snake_case__ = 0.1		,					snake_case__ = 0.1		,					snake_case__ = 0.1		,					snake_case__ = 100		,					snake_case__ = 0.02		,					snake_case__ = True		,					snake_case__=True		,					snake_case__ = 10		,					snake_case__ = 25		,					snake_case__ = 3		,					**snake_case__		,					):






         """simple docstring"""





         lowerCAmelCase					:					Any								=							prediction_length
         lowerCAmelCase					:					Dict								=							context_length if context_length is not None else prediction_length
         lowerCAmelCase					:					Tuple								=							distribution_output
         lowerCAmelCase					:					List[Any]								=							loss
         lowerCAmelCase					:					int								=							input_size
         lowerCAmelCase					:					str								=							num_time_features
         lowerCAmelCase					:					str								=							lags_sequence
         lowerCAmelCase					:					List[str]								=							scaling
         lowerCAmelCase					:					List[Any]								=							num_dynamic_real_features
         lowerCAmelCase					:					Tuple								=							num_static_real_features
         lowerCAmelCase					:					Dict								=							num_static_categorical_features
         if cardinality is not None and num_static_categorical_features > 0:
           if len(snake_case__						) != num_static_categorical_features:
             raise ValueError(
                 "The cardinality should be a list of the same length as `num_static_categorical_features`"						)
           lowerCAmelCase					:					Any								=							cardinality
         else:
           lowerCAmelCase					:					Union[str, Any]								=							[0]
         if embedding_dimension is not None and num_static_categorical_features > 0:
           if len(snake_case__						) != num_static_categorical_features:
             raise ValueError(
                 "The embedding dimension should be a list of the same length as `num_static_categorical_features`"						)
           lowerCAmelCase					:					Tuple								=							embedding_dimension
         else:
           lowerCAmelCase					:					Any								=							[min(50		,					(cat + 1) // 2						) for cat in self.cardinality]
         lowerCAmelCase					:					Any								=							num_parallel_samples

         # Transformer architecture configuration
         lowerCAmelCase					:					str								=							input_size * len(self.lags_sequence						) + self._number_of_features
         lowerCAmelCase					:					Any								=							d_model
         lowerCAmelCase					:					List[str]								=							encoder_attention_heads
         lowerCAmelCase					:					Union[str, Any]								=							decoder_attention_heads
         lowerCAmelCase					:					Optional[int]								=							encoder_ffn_dim
         lowerCAmelCase					:					Optional[Any]								=							decoder_ffn_dim
         lowerCAmelCase					:					int								=							encoder_layers
         lowerCAmelCase					:					int								=							decoder_layers

         lowerCAmelCase					:					List[Any]								=							dropout
         lowerCAmelCase					:					Optional[int]								=							attention_dropout
         lowerCAmelCase					:					Union[str, Any]								=							activation_dropout
         lowerCAmelCase					:					Optional[int]								=							encoder_layerdrop
         lowerCAmelCase					:					Dict								=							decoder_layerdrop

         lowerCAmelCase					:					Tuple								=							activation_function
         lowerCAmelCase					:					Optional[Any]								=							init_std

         lowerCAmelCase					:					List[Any]								=							use_cache

         # Autoformer
         lowerCAmelCase					:					Any								=							label_length
         lowerCAmelCase					:					Any								=							moving_average
         lowerCAmelCase					:					Optional[Any]								=							autocorrelation_factor

         super().__init__(is_encoder_decoder=snake_case__		,					**snake_case__						)







       @property
       def 	lowercase__							(						self						):






         """simple docstring"""





         return (
             sum(self.embedding_dimension						)
             + self.num_dynamic_real_features
             + self.num_time_features
             + self.num_static_real_features
             + self.input_size * 2  # the log1p(abs(loc)) and log(scale) features
         )





