
"""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				,    lowerCAmelCase__				,    lowerCAmelCase__=1_3				,    lowerCAmelCase__=7				,    lowerCAmelCase__=True				,    lowerCAmelCase__=True				,    lowerCAmelCase__=True				,    lowerCAmelCase__=True				,    lowerCAmelCase__=9_9				,    lowerCAmelCase__=3_2				,    lowerCAmelCase__=5				,    lowerCAmelCase__=4				,    lowerCAmelCase__=4				,    lowerCAmelCase__="gelu"				,    lowerCAmelCase__=0.0				,    lowerCAmelCase__=0.1				,    lowerCAmelCase__=True				,    lowerCAmelCase__=5_1_2				,    lowerCAmelCase__=1_6				,    lowerCAmelCase__=2				,    lowerCAmelCase__=0.02				,    lowerCAmelCase__=3				,    lowerCAmelCase__=4				,    lowerCAmelCase__=None				,    ):
				__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       snake_case_	(	self):
				__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       snake_case_	(	self):
				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       snake_case_	(	self):
				__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       snake_case_	(	self				,    lowerCAmelCase__				,    lowerCAmelCase__				,    lowerCAmelCase__):
				__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       snake_case_	(	self				,    lowerCAmelCase__				,    lowerCAmelCase__				,    lowerCAmelCase__):
				__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       snake_case_	(	self				,    lowerCAmelCase__				,    lowerCAmelCase__				,    lowerCAmelCase__				,    lowerCAmelCase__):
				__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       snake_case_	(	self				,    lowerCAmelCase__				,    lowerCAmelCase__				,    lowerCAmelCase__):
				__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       snake_case_	(	self):
				__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 		SCREAMING_SNAKE_CASE_					(		a_	,							a_	,							unittest.TestCase					):




		"""simple docstring"""





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




		def       snake_case_	(	self):
				__SCREAMING_SNAKE_CASE				=					GPTNeoXJapaneseModelTester(self)
				__SCREAMING_SNAKE_CASE				=					ConfigTester(self				,    config_class=UpperCamelCase_				,    hidden_size=3_7)




		def       snake_case_	(	self):
				self.config_tester.run_common_tests()




		def       snake_case_	(	self):
				__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       snake_case_	(	self):
				__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       snake_case_	(	self):
				__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       snake_case_	(	self):
				__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       snake_case_	(	self):
				__SCREAMING_SNAKE_CASE				=					self.model_tester.prepare_config_and_inputs()
				self.model_tester.create_and_check_for_causal_lm(*UpperCamelCase_)




		@slow
		def       snake_case_	(	self):
				__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"""
from typing import List, Optional, Union

import numpy as np

from ...feature_extraction_sequence_utils import SequenceFeatureExtractor
from ...feature_extraction_utils import BatchFeature
from ...utils import PaddingStrategy, TensorType, logging


snake_case : Dict     		=		logging.get_logger(__name__)



class        UpperCamelCase__ (				a_):







    """simple docstring"""
    __UpperCAmelCase           = ["""input_values""", """padding_mask"""]







    def __init__(		self :	Optional[int]				,    UpperCamelCase_ :	int = 1				,    UpperCamelCase_ :	int = 2_4_0_0_0				,    UpperCamelCase_ :	float = 0.0				,    UpperCamelCase_ :	float = None				,    UpperCamelCase_ :	float = None				,    **UpperCamelCase_ :	str				,    ):





           '''simple docstring'''

           super().__init__(feature_size=UpperCamelCase_				,    sampling_rate=UpperCamelCase_				,    padding_value=UpperCamelCase_				,    **UpperCamelCase_ )
           __magic_name__      				=    chunk_length_s
           __magic_name__      				=    overlap







    @property
    def 				a__			(		self :	List[str] ):





           '''simple docstring'''

           if self.chunk_length_s is None:
                  return None
           else:
                  return int(self.chunk_length_s * self.sampling_rate )







    @property
    def 				a__			(		self :	Optional[Any] ):





           '''simple docstring'''

           if self.chunk_length_s is None or self.overlap is None:
                  return None
           else:
                  return max(1				,    int((1.0 - self.overlap) * self.chunk_length ) )







    def __call__(		self :	List[Any]				,    UpperCamelCase_ :	Union[np.ndarray, List[float], List[np.ndarray], List[List[float]]]				,    UpperCamelCase_ :	Optional[Union[bool, str, PaddingStrategy]] = None				,    UpperCamelCase_ :	Optional[bool] = False				,    UpperCamelCase_ :	Optional[int] = None				,    UpperCamelCase_ :	Optional[Union[str, TensorType]] = None				,    UpperCamelCase_ :	Optional[int] = None				,    ):





           '''simple docstring'''

           if sampling_rate is not None:
                  if sampling_rate != self.sampling_rate:
                         raise ValueError(
                             f"""The model corresponding to this feature extractor: {self} was trained using a sampling rate of"""
                             f""" {self.sampling_rate}. Please make sure that the provided audio input was sampled with"""
                             f""" {self.sampling_rate} and not {sampling_rate}.""" )
           else:
                  logger.warning(
                      'It is strongly recommended to pass the `sampling_rate` argument to this function. '
                      'Failing to do so can result in silent errors that might be hard to debug.' )

           if padding and truncation:
                  raise ValueError('Both padding and truncation were set. Make sure you only set one.' )
           elif padding is None:
                  # by default let's pad the inputs
                  __magic_name__      				=    True

           __magic_name__      				=    bool(
               isinstance(UpperCamelCase_				,    (list, tuple) ) and (isinstance(raw_audio[0]				,    (np.ndarray, tuple, list) )) )

           if is_batched:
                  __magic_name__      				=    [np.asarray(UpperCamelCase_				,    dtype=np.floataa ).T for audio in raw_audio]
           elif not is_batched and not isinstance(UpperCamelCase_				,    np.ndarray ):
                  __magic_name__      				=    np.asarray(UpperCamelCase_				,    dtype=np.floataa )
           elif isinstance(UpperCamelCase_				,    np.ndarray ) and raw_audio.dtype is np.dtype(np.floataa ):
                  __magic_name__      				=    raw_audio.astype(np.floataa )

           # always return batch
           if not is_batched:
                  __magic_name__      				=    [np.asarray(UpperCamelCase_ ).T]

           # verify inputs are valid
           for idx, example in enumerate(UpperCamelCase_ ):
                  if example.ndim > 2:
                         raise ValueError(f"""Expected input shape (channels, length) but got shape {example.shape}""" )
                  if self.feature_size == 1 and example.ndim != 1:
                         raise ValueError(f"""Expected mono audio but example has {example.shape[-1]} channels""" )
                  if self.feature_size == 2 and example.shape[-1] != 2:
                         raise ValueError(f"""Expected stereo audio but example has {example.shape[-1]} channels""" )

           __magic_name__      				=    None
           __magic_name__      				=    BatchFeature({'input_values': raw_audio} )
           if self.chunk_stride is not None and self.chunk_length is not None and max_length is None:
                  if truncation:
                         __magic_name__      				=    min(array.shape[0] for array in raw_audio )
                         __magic_name__      				=    int(np.floor(max_length / self.chunk_stride ) )
                         __magic_name__      				=    (nb_step - 1) * self.chunk_stride + self.chunk_length
                  elif padding:
                         __magic_name__      				=    max(array.shape[0] for array in raw_audio )
                         __magic_name__      				=    int(np.ceil(max_length / self.chunk_stride ) )
                         __magic_name__      				=    (nb_step - 1) * self.chunk_stride + self.chunk_length
                         __magic_name__      				=    'max_length'
                  else:
                         __magic_name__      				=    input_values

        # normal padding on batch
           if padded_inputs is None:
                  __magic_name__      				=    self.pad(
                      UpperCamelCase_				,    max_length=UpperCamelCase_				,    truncation=UpperCamelCase_				,    padding=UpperCamelCase_				,    return_attention_mask=UpperCamelCase_				,    )
                  if padding:
                         __magic_name__      				=    padded_inputs.pop('attention_mask' )

           __magic_name__      				=    []
           for example in padded_inputs.pop('input_values' ):
                  if self.feature_size == 1:
                         __magic_name__      				=    example[..., None]
                  input_values.append(example.T )

           __magic_name__      				=    input_values
           if return_tensors is not None:
                  __magic_name__      				=    padded_inputs.convert_to_tensors(UpperCamelCase_ )

           return padded_inputs