


'''simple docstring'''




from ... import PretrainedConfig


__snake_case    						={
    """sijunhe/nezha-cn-base""": """https://huggingface.co/sijunhe/nezha-cn-base/resolve/main/config.json""",
}

class 		UpperCAmelCase_      (  __lowercase		):
     lowerCamelCase				:	Dict		=     NEZHA_PRETRAINED_CONFIG_ARCHIVE_MAP
     lowerCamelCase				:	Optional[Any]		=     '''nezha'''





     def __init__( self       :      List[str]    ,      UpperCAmelCase__       :      str=2_1_1_2_8    ,      UpperCAmelCase__       :      Optional[int]=7_6_8    ,      UpperCAmelCase__       :      Dict=1_2    ,      UpperCAmelCase__       :      Any=1_2    ,      UpperCAmelCase__       :      Optional[Any]=3_0_7_2    ,      UpperCAmelCase__       :      Optional[Any]="gelu"    ,      UpperCAmelCase__       :      Tuple=0.1    ,      UpperCAmelCase__       :      Optional[Any]=0.1    ,      UpperCAmelCase__       :      int=5_1_2    ,      UpperCAmelCase__       :      Optional[int]=6_4    ,      UpperCAmelCase__       :      Any=2    ,      UpperCAmelCase__       :      Tuple=0.02    ,      UpperCAmelCase__       :      List[Any]=1E-12    ,      UpperCAmelCase__       :      Dict=0.1    ,      UpperCAmelCase__       :      int=0    ,      UpperCAmelCase__       :      Optional[int]=2    ,      UpperCAmelCase__       :      Union[str, Any]=3    ,      UpperCAmelCase__       :      Union[str, Any]=True    ,      **UpperCAmelCase__       :      Dict    ,      )				->  Optional[Any]:
        super().__init__(pad_token_id=UpperCAmelCase__    ,      bos_token_id=UpperCAmelCase__    ,      eos_token_id=UpperCAmelCase__    ,      **UpperCAmelCase__    )

        lowerCAmelCase     =			vocab_size
        lowerCAmelCase     =			hidden_size
        lowerCAmelCase     =			num_hidden_layers
        lowerCAmelCase     =			num_attention_heads
        lowerCAmelCase     =			hidden_act
        lowerCAmelCase     =			intermediate_size
        lowerCAmelCase     =			hidden_dropout_prob
        lowerCAmelCase     =			attention_probs_dropout_prob
        lowerCAmelCase     =			max_position_embeddings
        lowerCAmelCase     =			max_relative_position
        lowerCAmelCase     =			type_vocab_size
        lowerCAmelCase     =			initializer_range
        lowerCAmelCase     =			layer_norm_eps
        lowerCAmelCase     =			classifier_dropout
        lowerCAmelCase     =			use_cache









'''simple docstring'''




import itertools
import random
import unittest

import numpy as np

from transformers import ASTFeatureExtractor
from transformers.testing_utils import require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


__snake_case    						=random.Random()

if is_torch_available():
       import torch





def 	a_					( lowerCamelCase	:     Dict	, lowerCamelCase	:     Dict=1.0	, lowerCamelCase	:     List[Any]=None	, lowerCamelCase	:     Union[str, Any]=None				):
   if rng is None:
      lowerCAmelCase     =			global_rng

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

   return values

class 		UpperCAmelCase_      (  unittest.TestCase		):





     def __init__( self       :      List[Any]    ,      UpperCAmelCase__       :      Optional[Any]    ,      UpperCAmelCase__       :      List[str]=7    ,      UpperCAmelCase__       :      int=4_0_0    ,      UpperCAmelCase__       :      int=2_0_0_0    ,      UpperCAmelCase__       :      List[str]=1    ,      UpperCAmelCase__       :      Tuple=0.0    ,      UpperCAmelCase__       :      Tuple=1_6_0_0_0    ,      UpperCAmelCase__       :      Tuple=True    ,      UpperCAmelCase__       :      Union[str, Any]=True    ,      )				->  Any:
        lowerCAmelCase     =			parent
        lowerCAmelCase     =			batch_size
        lowerCAmelCase     =			min_seq_length
        lowerCAmelCase     =			max_seq_length
        lowerCAmelCase     =			(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
        lowerCAmelCase     =			feature_size
        lowerCAmelCase     =			padding_value
        lowerCAmelCase     =			sampling_rate
        lowerCAmelCase     =			return_attention_mask
        lowerCAmelCase     =			do_normalize





     def 							__UpperCAmelCase      ( self       :      Optional[Any]    )				->  List[str]:
        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 							__UpperCAmelCase      ( self       :      str    ,      UpperCAmelCase__       :      Union[str, Any]=False    ,      UpperCAmelCase__       :      Union[str, Any]=False    )				->  Optional[Any]:
        def _flatten(UpperCAmelCase__       :      int    ):
           return list(itertools.chain(*UpperCAmelCase__    )    )

        if equal_length:
           lowerCAmelCase     =			floats_list((self.batch_size, self.max_seq_length)    )
        else:
           # make sure that inputs increase in size
           lowerCAmelCase     =			[
               _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:
           lowerCAmelCase     =			[np.asarray(UpperCAmelCase__    ) for x in speech_inputs]

        return speech_inputs




@require_torch
@require_torchaudio
class 		UpperCAmelCase_      (  __lowercase       ,	unittest.TestCase		):
     lowerCamelCase				:	Dict		=     ASTFeatureExtractor





     def 							__UpperCAmelCase      ( self       :      str    )				->  Optional[int]:
        lowerCAmelCase     =			ASTFeatureExtractionTester(self    )





     def 							__UpperCAmelCase      ( self       :      Optional[int]    )				->  Optional[int]:
        # Tests that all call wrap to encode_plus and batch_encode_plus
        lowerCAmelCase     =			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()    )
        # create three inputs of length 800, 1000, and 1200
        lowerCAmelCase     =			[floats_list((1, x)    )[0] for x in range(8_0_0    ,      1_4_0_0    ,      2_0_0    )]
        lowerCAmelCase     =			[np.asarray(UpperCAmelCase__    ) for speech_input in speech_inputs]

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

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

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





     @require_torch
     def 							__UpperCAmelCase      ( self       :      Union[str, Any]    )				->  Optional[int]:
        import torch

        lowerCAmelCase     =			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()    )
        lowerCAmelCase     =			np.random.rand(1_0_0    ).astype(np.floataa    )
        lowerCAmelCase     =			np_speech_inputs.tolist()

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





     def 							__UpperCAmelCase      ( self       :      int    ,      UpperCAmelCase__       :      str    )				->  Tuple:
        from datasets import load_dataset

        lowerCAmelCase     =			load_dataset('hf-internal-testing/librispeech_asr_dummy'    ,      'clean'    ,      split='validation'    )
        # automatic decoding with librispeech
        lowerCAmelCase     =			ds.sort('id'    ).select(range(UpperCAmelCase__    )    )[:num_samples]['audio']

        return [x["array"] for x in speech_samples]





     @require_torch
     def 							__UpperCAmelCase      ( self       :      str    )				->  Optional[Any]:
        # fmt: off
        lowerCAmelCase     =			torch.tensor(
            [-0.9_894, -1.2_776, -0.9_066, -1.2_776, -0.9_349, -1.2_609, -1.0_386, -1.2_776,
             -1.1_561, -1.2_776, -1.2_052, -1.2_723, -1.2_190, -1.2_132, -1.2_776, -1.1_133,
             -1.1_953, -1.1_343, -1.1_584, -1.2_203, -1.1_770, -1.2_474, -1.2_381, -1.1_936,
             -0.9_270, -0.8_317, -0.8_049, -0.7_706, -0.7_565, -0.7_869]    )
        # fmt: on

        lowerCAmelCase     =			self._load_datasamples(1    )
        lowerCAmelCase     =			ASTFeatureExtractor()
        lowerCAmelCase     =			feature_extractor(UpperCAmelCase__    ,      return_tensors='pt'    ).input_values
        self.assertEquals(input_values.shape    ,      (1, 1_0_2_4, 1_2_8)    )
        self.assertTrue(torch.allclose(input_values[0, 0, :3_0]    ,      UpperCAmelCase__    ,      atol=1E-4    )    )





