



import itertools
import random
import unittest

import numpy as np

from transformers import WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST, WavaVecaConfig, WavaVecaFeatureExtractor
from transformers.testing_utils import require_torch, slow

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


__snake_case						:Optional[Any]						=					random.Random()





def 					__snake_case     (      _UpperCAmelCase       ,			_UpperCAmelCase=1.0       ,			_UpperCAmelCase=None       ,			_UpperCAmelCase=None    ):
    if rng is None:
        __a							=    global_rng

    __a							=    []
    for batch_idx in range(shape[0]    ):
        values.append([]    )
        for _ in range(shape[1]    ):
            values[-1].append(rng.random() * scale    )

    return values

class    _A	(     unittest.TestCase   ):



       def __init__(		self							:					str					,		__SCREAMING_SNAKE_CASE							:					List[Any]					,		__SCREAMING_SNAKE_CASE							:					str=7					,		__SCREAMING_SNAKE_CASE							:					Any=400					,		__SCREAMING_SNAKE_CASE							:					int=2_000					,		__SCREAMING_SNAKE_CASE							:					Optional[int]=1					,		__SCREAMING_SNAKE_CASE							:					List[str]=0.0					,		__SCREAMING_SNAKE_CASE							:					Union[str, Any]=16_000					,		__SCREAMING_SNAKE_CASE							:					List[str]=True					,		__SCREAMING_SNAKE_CASE							:					Dict=True					,		):


           '''simple docstring'''

           __a							=    parent
           __a							=    batch_size
           __a							=    min_seq_length
           __a							=    max_seq_length
           __a							=    (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
           __a							=    feature_size
           __a							=    padding_value
           __a							=    sampling_rate
           __a							=    return_attention_mask
           __a							=    do_normalize



       def 		_lowerCamelCase      (		self							:					List[Any]):


           '''simple docstring'''

           return {
               "feature_size": self.feature_size,
               "padding_value": self.padding_value,
               "sampling_rate": self.sampling_rate,
               "return_attention_mask": self.return_attention_mask,
               "do_normalize": self.do_normalize,
           }





       def 		_lowerCamelCase      (		self							:					Optional[Any]					,		__SCREAMING_SNAKE_CASE							:					List[Any]=False					,		__SCREAMING_SNAKE_CASE							:					List[Any]=False):


           '''simple docstring'''

           def _flatten(__SCREAMING_SNAKE_CASE							:					int):
               return list(itertools.chain(*__SCREAMING_SNAKE_CASE))

           if equal_length:
               __a							=    floats_list((self.batch_size, self.max_seq_length))
           else:
               # make sure that inputs increase in size
               __a							=    [
                   _flatten(floats_list((x, self.feature_size)))
                   for x in range(self.min_seq_length					,		self.max_seq_length					,		self.seq_length_diff)
               ]

           if numpify:
               __a							=    [np.asarray(__SCREAMING_SNAKE_CASE) for x in speech_inputs]

           return speech_inputs


class    _A	(     __UpperCAmelCase				,unittest.TestCase   ):
       UpperCamelCase__  :   Optional[int]						 =						WavaVecaFeatureExtractor



       def 		_lowerCamelCase      (		self							:					Optional[int]):


           '''simple docstring'''

           __a							=    WavaVecaFeatureExtractionTester(self)



       def 		_lowerCamelCase      (		self							:					str					,		__SCREAMING_SNAKE_CASE							:					Optional[Any]):


           '''simple docstring'''

           self.assertTrue(np.all(np.mean(__SCREAMING_SNAKE_CASE					,		axis=0) < 1E-3))
           self.assertTrue(np.all(np.abs(np.var(__SCREAMING_SNAKE_CASE					,		axis=0) - 1) < 1E-3))



       def 		_lowerCamelCase      (		self							:					Optional[int]):


           '''simple docstring'''

           __a							=    self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
           # create three inputs of length 800, 1000, and 1200
           __a							=    [floats_list((1, x))[0] for x in range(800					,		1_400					,		200)]
           __a							=    [np.asarray(__SCREAMING_SNAKE_CASE) for speech_input in speech_inputs]

           # Test not batched input
           __a							=    feat_extract(speech_inputs[0]					,		return_tensors='''np''').input_values
           __a							=    feat_extract(np_speech_inputs[0]					,		return_tensors='''np''').input_values
           self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE					,		__SCREAMING_SNAKE_CASE					,		atol=1E-3))

           # Test batched
           __a							=    feat_extract(__SCREAMING_SNAKE_CASE					,		return_tensors='''np''').input_values
           __a							=    feat_extract(__SCREAMING_SNAKE_CASE					,		return_tensors='''np''').input_values
           for enc_seq_a, enc_seq_a in zip(__SCREAMING_SNAKE_CASE					,		__SCREAMING_SNAKE_CASE):
               self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE					,		__SCREAMING_SNAKE_CASE					,		atol=1E-3))

           # Test 2-D numpy arrays are batched.
           __a							=    [floats_list((1, x))[0] for x in (800, 800, 800)]
           __a							=    np.asarray(__SCREAMING_SNAKE_CASE)
           __a							=    feat_extract(__SCREAMING_SNAKE_CASE					,		return_tensors='''np''').input_values
           __a							=    feat_extract(__SCREAMING_SNAKE_CASE					,		return_tensors='''np''').input_values
           for enc_seq_a, enc_seq_a in zip(__SCREAMING_SNAKE_CASE					,		__SCREAMING_SNAKE_CASE):
               self.assertTrue(np.allclose(__SCREAMING_SNAKE_CASE					,		__SCREAMING_SNAKE_CASE					,		atol=1E-3))



       def 		_lowerCamelCase      (		self							:					Dict):


           '''simple docstring'''

           __a							=    self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
           __a							=    [floats_list((1, x))[0] for x in range(800					,		1_400					,		200)]

           __a							=    ['''longest''', '''max_length''', '''do_not_pad''']
           __a							=    [None, 1_600, None]
           for max_length, padding in zip(__SCREAMING_SNAKE_CASE					,		__SCREAMING_SNAKE_CASE):
               __a							=    feat_extract(__SCREAMING_SNAKE_CASE					,		padding=__SCREAMING_SNAKE_CASE					,		max_length=__SCREAMING_SNAKE_CASE					,		return_tensors='''np''')
               __a							=    processed.input_values

               self._check_zero_mean_unit_variance(input_values[0][:800])
               self.assertTrue(input_values[0][800:].sum() < 1E-6)
               self._check_zero_mean_unit_variance(input_values[1][:1_000])
               self.assertTrue(input_values[0][1_000:].sum() < 1E-6)
               self._check_zero_mean_unit_variance(input_values[2][:1_200])



       def 		_lowerCamelCase      (		self							:					Dict):


           '''simple docstring'''

           __a							=    self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
           __a							=    range(800					,		1_400					,		200)
           __a							=    [floats_list((1, x))[0] for x in lengths]

           __a							=    ['''longest''', '''max_length''', '''do_not_pad''']
           __a							=    [None, 1_600, None]

           for max_length, padding in zip(__SCREAMING_SNAKE_CASE					,		__SCREAMING_SNAKE_CASE):
               __a							=    feat_extract(__SCREAMING_SNAKE_CASE					,		max_length=__SCREAMING_SNAKE_CASE					,		padding=__SCREAMING_SNAKE_CASE)
               __a							=    processed.input_values

               self._check_zero_mean_unit_variance(input_values[0][:800])
               self._check_zero_mean_unit_variance(input_values[1][:1_000])
               self._check_zero_mean_unit_variance(input_values[2][:1_200])



       def 		_lowerCamelCase      (		self							:					int):


           '''simple docstring'''

           __a							=    self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
           __a							=    [floats_list((1, x))[0] for x in range(800					,		1_400					,		200)]
           __a							=    feat_extract(
               __SCREAMING_SNAKE_CASE					,		truncation=__SCREAMING_SNAKE_CASE					,		max_length=1_000					,		padding='''max_length'''					,		return_tensors='''np''')
           __a							=    processed.input_values

           self._check_zero_mean_unit_variance(input_values[0, :800])
           self._check_zero_mean_unit_variance(input_values[1])
           self._check_zero_mean_unit_variance(input_values[2])



       def 		_lowerCamelCase      (		self							:					str):


           '''simple docstring'''

           __a							=    self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
           __a							=    [floats_list((1, x))[0] for x in range(800					,		1_400					,		200)]
           __a							=    feat_extract(
               __SCREAMING_SNAKE_CASE					,		truncation=__SCREAMING_SNAKE_CASE					,		max_length=1_000					,		padding='''longest'''					,		return_tensors='''np''')
           __a							=    processed.input_values

           self._check_zero_mean_unit_variance(input_values[0, :800])
           self._check_zero_mean_unit_variance(input_values[1, :1_000])
           self._check_zero_mean_unit_variance(input_values[2])

           # make sure that if max_length < longest -> then pad to max_length
           self.assertTrue(input_values.shape == (3, 1_000))

           __a							=    [floats_list((1, x))[0] for x in range(800					,		1_400					,		200)]
           __a							=    feat_extract(
               __SCREAMING_SNAKE_CASE					,		truncation=__SCREAMING_SNAKE_CASE					,		max_length=2_000					,		padding='''longest'''					,		return_tensors='''np''')
           __a							=    processed.input_values

           self._check_zero_mean_unit_variance(input_values[0, :800])
           self._check_zero_mean_unit_variance(input_values[1, :1_000])
           self._check_zero_mean_unit_variance(input_values[2])

           # make sure that if max_length > longest -> then pad to longest
           self.assertTrue(input_values.shape == (3, 1_200))



       @require_torch
       def 		_lowerCamelCase      (		self							:					str):


           '''simple docstring'''

           import torch

           __a							=    self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict())
           __a							=    np.random.rand(100).astype(np.floataa)
           __a							=    np_speech_inputs.tolist()

           for inputs in [py_speech_inputs, np_speech_inputs]:
               __a							=    feature_extractor.pad([{'''input_values''': inputs}]					,		return_tensors='''np''')
               self.assertTrue(np_processed.input_values.dtype == np.floataa)
               __a							=    feature_extractor.pad([{'''input_values''': inputs}]					,		return_tensors='''pt''')
               self.assertTrue(pt_processed.input_values.dtype == torch.floataa)





       @slow
       @require_torch
       def 		_lowerCamelCase      (		self							:					Union[str, Any]):


           '''simple docstring'''

           for model_id in WAV_2_VEC_2_PRETRAINED_MODEL_ARCHIVE_LIST:
               __a							=    WavaVecaConfig.from_pretrained(__SCREAMING_SNAKE_CASE)
               __a							=    WavaVecaFeatureExtractor.from_pretrained(__SCREAMING_SNAKE_CASE)

               # only "layer" feature extraction norm should make use of
               # attention_mask
               self.assertEqual(feat_extract.return_attention_mask					,		config.feat_extract_norm == '''layer''')










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


__snake_case						:Dict						=					logging.get_logger(__name__)

__snake_case						:List[Any]						=					{
    '''tanreinama/GPTSAN-2.8B-spout_is_uniform''': (
        '''https://huggingface.co/tanreinama/GPTSAN-2.8B-spout_is_uniform/resolve/main/config.json'''
    ),
}

class    _A	(     __UpperCAmelCase   ):
       UpperCamelCase__  :   List[str]						 =						'''gptsan-japanese'''
       UpperCamelCase__  :   Dict						 =						[
           '''past_key_values''',
       ]
       UpperCamelCase__  :   Dict						 =						{
           '''hidden_size''': '''d_model''',
           '''num_attention_heads''': '''num_heads''',
           '''num_hidden_layers''': '''num_layers''',
       }



       def __init__(		self							:					List[str]					,		__SCREAMING_SNAKE_CASE							:					Optional[Any]=36_000					,		__SCREAMING_SNAKE_CASE							:					Tuple=1_280					,		__SCREAMING_SNAKE_CASE							:					List[Any]=1_024					,		__SCREAMING_SNAKE_CASE							:					List[Any]=8_192					,		__SCREAMING_SNAKE_CASE							:					str=4_096					,		__SCREAMING_SNAKE_CASE							:					Any=128					,		__SCREAMING_SNAKE_CASE							:					int=10					,		__SCREAMING_SNAKE_CASE							:					Optional[int]=0					,		__SCREAMING_SNAKE_CASE							:					Optional[Any]=16					,		__SCREAMING_SNAKE_CASE							:					List[Any]=16					,		__SCREAMING_SNAKE_CASE							:					Optional[Any]=128					,		__SCREAMING_SNAKE_CASE							:					Tuple=0.0					,		__SCREAMING_SNAKE_CASE							:					List[Any]=1E-5					,		__SCREAMING_SNAKE_CASE							:					str=False					,		__SCREAMING_SNAKE_CASE							:					Optional[int]=0.0					,		__SCREAMING_SNAKE_CASE							:					List[str]="float32"					,		__SCREAMING_SNAKE_CASE							:					List[str]=False					,		__SCREAMING_SNAKE_CASE							:					Any=False					,		__SCREAMING_SNAKE_CASE							:					Any=False					,		__SCREAMING_SNAKE_CASE							:					int=0.0_02					,		__SCREAMING_SNAKE_CASE							:					Any=False					,		__SCREAMING_SNAKE_CASE							:					Tuple=True					,		__SCREAMING_SNAKE_CASE							:					int=35_998					,		__SCREAMING_SNAKE_CASE							:					Optional[int]=35_995					,		__SCREAMING_SNAKE_CASE							:					List[str]=35_999					,		**__SCREAMING_SNAKE_CASE							:					List[str]					,		):


           '''simple docstring'''

           __a							=    vocab_size
           __a							=    max_position_embeddings
           __a							=    d_model
           __a							=    d_ff
           __a							=    d_ext
           __a							=    d_spout
           __a							=    num_switch_layers
           __a							=    num_ext_layers
           __a							=    num_switch_layers + num_ext_layers
           __a							=    num_heads
           __a							=    num_experts
           __a							=    expert_capacity
           __a							=    dropout_rate
           __a							=    layer_norm_epsilon
           __a							=    router_bias
           __a							=    router_jitter_noise
           __a							=    router_dtype
           __a							=    router_ignore_padding_tokens
           __a							=    output_hidden_states
           __a							=    output_attentions
           __a							=    initializer_factor
           __a							=    output_router_logits
           __a							=    use_cache

           super().__init__(
               separator_token_id=__SCREAMING_SNAKE_CASE					,		pad_token_id=__SCREAMING_SNAKE_CASE					,		eos_token_id=__SCREAMING_SNAKE_CASE					,		**__SCREAMING_SNAKE_CASE					,		)





