






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


__a   :							List[str]						= random.Random()

if is_torch_available():
  import torch



def       _SCREAMING_SNAKE_CASE    (			__lowercase :      str      ,					__lowercase :      List[Any]=1.0      ,					__lowercase :      Any=None      ,					__lowercase :      List[str]=None     )    ->   Optional[Any]:





    """simple docstring"""

    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    __lowercase   (						unittest.TestCase	):







     '''simple docstring'''




     def __init__(					self							:      Any			,						UpperCamelCase_							:      Tuple			,						UpperCamelCase_							:      str=7			,						UpperCamelCase_							:      Any=400			,						UpperCamelCase_							:      int=2_000			,						UpperCamelCase_							:      Optional[Any]=1			,						UpperCamelCase_							:      Tuple=0.0			,						UpperCamelCase_							:      Dict=16_000			,						UpperCamelCase_							:      Any=True			,						UpperCamelCase_							:      List[str]=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							:      Dict			,						UpperCamelCase_							:      Optional[int]=False			,						UpperCamelCase_							:      List[Any]=False					):


         """simple docstring"""



         def _flatten(UpperCamelCase_							:      Dict					):
             return list(itertools.chain(*UpperCamelCase_					)					)

         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(UpperCamelCase_					) for x in speech_inputs]

         return speech_inputs


@require_torch
@require_torchaudio
class    __lowercase   (						lowercase_      ,   unittest.TestCase	):







     '''simple docstring'''
     SCREAMING_SNAKE_CASE     =					ASTFeatureExtractor




     def 		lowerCAmelCase_  (					self							:      List[str]					):


         """simple docstring"""



         __A       =						ASTFeatureExtractionTester(self					)




     def 		lowerCAmelCase_  (					self							:      List[str]					):


         """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(UpperCamelCase_					) 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(UpperCamelCase_			,						UpperCamelCase_			,						atol=1e-3					)					)

         # Test batched
         __A       =						feat_extract(UpperCamelCase_			,						padding=UpperCamelCase_			,						return_tensors="""np"""					).input_values
         __A       =						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.
         __A       =						[floats_list((1, x)					)[0] for x in (800, 800, 800)]
         __A       =						np.asarray(UpperCamelCase_					)
         __A       =						feat_extract(UpperCamelCase_			,						return_tensors="""np"""					).input_values
         __A       =						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 		lowerCAmelCase_  (					self							:      List[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					)




     def 		lowerCAmelCase_  (					self							:      List[str]			,						UpperCamelCase_							:      Union[str, Any]					):


         """simple docstring"""



         from datasets import load_dataset

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

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





     @require_torch
     def 		lowerCAmelCase_  (					self							:      Dict					):


         """simple docstring"""



         __A       =						torch.tensor(
             [-0.9894, -1.2776, -0.9066, -1.2776, -0.9349, -1.2609, -1.0386, -1.2776,
              -1.1561, -1.2776, -1.2052, -1.2723, -1.2190, -1.2132, -1.2776, -1.1133,
              -1.1953, -1.1343, -1.1584, -1.2203, -1.1770, -1.2474, -1.2381, -1.1936,
              -0.9270, -0.8317, -0.8049, -0.7706, -0.7565, -0.7869]					)
         # fmt: on

         __A       =						self._load_datasamples(1					)
         __A       =						ASTFeatureExtractor()
         __A       =						feature_extractor(UpperCamelCase_			,						return_tensors="""pt"""					).input_values
         self.assertEquals(input_values.shape			,						(1, 1_024, 128)					)
         self.assertTrue(torch.allclose(input_values[0, 0, :30]			,						UpperCamelCase_			,						atol=1e-4					)					)












from typing import TYPE_CHECKING

from ...utils import (
    OptionalDependencyNotAvailable,
    _LazyModule,
    is_sentencepiece_available,
    is_tf_available,
    is_tokenizers_available,
    is_torch_available,
)


__a   :							Optional[int]						= {"configuration_xlnet": ["XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP", "XLNetConfig"]}

try:
  if not is_sentencepiece_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __a   :							Tuple						= ["XLNetTokenizer"]

try:
  if not is_tokenizers_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __a   :							List[str]						= ["XLNetTokenizerFast"]

try:
  if not is_torch_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __a   :							Any						= [
      "XLNET_PRETRAINED_MODEL_ARCHIVE_LIST",
      "XLNetForMultipleChoice",
      "XLNetForQuestionAnswering",
      "XLNetForQuestionAnsweringSimple",
      "XLNetForSequenceClassification",
      "XLNetForTokenClassification",
      "XLNetLMHeadModel",
      "XLNetModel",
      "XLNetPreTrainedModel",
      "load_tf_weights_in_xlnet",
  ]

try:
  if not is_tf_available():
    raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
  pass
else:
  __a   :							Dict						= [
      "TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST",
      "TFXLNetForMultipleChoice",
      "TFXLNetForQuestionAnsweringSimple",
      "TFXLNetForSequenceClassification",
      "TFXLNetForTokenClassification",
      "TFXLNetLMHeadModel",
      "TFXLNetMainLayer",
      "TFXLNetModel",
      "TFXLNetPreTrainedModel",
  ]


if TYPE_CHECKING:
  from .configuration_xlnet import XLNET_PRETRAINED_CONFIG_ARCHIVE_MAP, XLNetConfig

  try:
    if not is_sentencepiece_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_xlnet import XLNetTokenizer

  try:
    if not is_tokenizers_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .tokenization_xlnet_fast import XLNetTokenizerFast

  try:
    if not is_torch_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_xlnet import (
        XLNET_PRETRAINED_MODEL_ARCHIVE_LIST,
        XLNetForMultipleChoice,
        XLNetForQuestionAnswering,
        XLNetForQuestionAnsweringSimple,
        XLNetForSequenceClassification,
        XLNetForTokenClassification,
        XLNetLMHeadModel,
        XLNetModel,
        XLNetPreTrainedModel,
        load_tf_weights_in_xlnet,
    )

  try:
    if not is_tf_available():
      raise OptionalDependencyNotAvailable()
  except OptionalDependencyNotAvailable:
    pass
  else:
    from .modeling_tf_xlnet import (
        TF_XLNET_PRETRAINED_MODEL_ARCHIVE_LIST,
        TFXLNetForMultipleChoice,
        TFXLNetForQuestionAnsweringSimple,
        TFXLNetForSequenceClassification,
        TFXLNetForTokenClassification,
        TFXLNetLMHeadModel,
        TFXLNetMainLayer,
        TFXLNetModel,
        TFXLNetPreTrainedModel,
    )

else:
  import sys

  __a   :							str						= _LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)




