import argparse
import json
import os
import sys
import tempfile
import unittest
from argparse import Namespace
from dataclasses import dataclass, field
from enum import Enum
from pathlib import Path
from typing import List, Literal, Optional

import yaml

from transformers import HfArgumentParser, TrainingArguments
from transformers.hf_argparser import make_choice_type_function, string_to_bool


# Since Python 3.10, we can use the builtin `|` operator for Union types
# See PEP 604: https://peps.python.org/pep-0604
SCREAMING_SNAKE_CASE__		:					Optional[int]						=					sys.version_info >= (3, 10)







def     _a				(      lowercase__  :			Any=None     ,  lowercase__  :			Tuple=None ):







     '''simple docstring'''


     return field(default_factory=lambda: default     ,  metadata=lowercase__ )

@dataclass
class       snake_case					:
  lowercase_			=     42
  lowercase_			=     42
  lowercase_			=     42
  lowercase_			=     42




@dataclass
class       snake_case					:
  lowercase_			=     42
  lowercase_			=     field(default='toto'   ,						metadata={'help': 'help message'}						)




@dataclass
class       snake_case					:
  lowercase_			=     False
  lowercase_			=     True
  lowercase_			=     None




class       snake_case					(      UpperCamelCase_						):
  lowercase_			=     'titi'
  lowercase_			=     'toto'




class       snake_case					(      UpperCamelCase_						):
  lowercase_			=     'titi'
  lowercase_			=     'toto'
  lowercase_			=     42




@dataclass
class       snake_case					:
  lowercase_			=     "toto"




  def 	__lowercase(      self     :	List[str]				)->       str:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				Tuple			=		BasicEnum(self.foo				)




@dataclass
class       snake_case					:
  lowercase_			=     "toto"




  def 	__lowercase(      self     :	Any				)->       Union[str, Any]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				int			=		MixedTypeEnum(self.foo				)




@dataclass
class       snake_case					:
  lowercase_			=     None
  lowercase_			=     field(default=UpperCamelCase_   ,						metadata={'help': 'help message'}						)
  lowercase_			=     None
  lowercase_			=     list_field(default=[]						)
  lowercase_			=     list_field(default=[]						)




@dataclass
class       snake_case					:
  lowercase_			=     list_field(default=[]						)
  lowercase_			=     list_field(default=[1, 2, 3]						)
  lowercase_			=     list_field(default=['Hallo', 'Bonjour', 'Hello']						)
  lowercase_			=     list_field(default=[0.1, 0.2, 0.3]						)




@dataclass
class       snake_case					:
  lowercase_			=     field()
  lowercase_			=     field()
  lowercase_			=     field()




  def 	__lowercase(      self     :	Optional[int]				)->       str:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				int			=		BasicEnum(self.required_enum				)




@dataclass
class       snake_case					:
  lowercase_			=     42
  lowercase_			=     field()
  lowercase_			=     None
  lowercase_			=     field(default='toto'   ,						metadata={'help': 'help message'}						)
  lowercase_			=     list_field(default=['Hallo', 'Bonjour', 'Hello']						)


if is_python_no_less_than_3_10:

      @dataclass
      class       snake_case					:
        lowercase_			=     False
        lowercase_			=     True
        lowercase_			=     None




      @dataclass
      class       snake_case					:
        lowercase_			=     None
        lowercase_			=     field(default=UpperCamelCase_   ,						metadata={'help': 'help message'}						)
        lowercase_			=     None
        lowercase_			=     list_field(default=[]						)
        lowercase_			=     list_field(default=[]						)







class       snake_case					(      unittest.TestCase						):




  def 	__lowercase(      self     :	int     , a_     :	argparse.ArgumentParser     , a_     :	argparse.ArgumentParser				)->       Tuple:




       """simple docstring"""
       self.assertEqual(len(a._actions				)     , len(b._actions				)				)
       for x, y in zip(a._actions     , b._actions				):
            SCREAMING_SNAKE_CASE__				:				List[str]			=		{k: v for k, v in vars(a_				).items() if k != 'container'}
            SCREAMING_SNAKE_CASE__				:				Dict			=		{k: v for k, v in vars(a_				).items() if k != 'container'}

            # Choices with mixed type have custom function as "type"
            # So we need to compare results directly for equality
            if xx.get('choices'     , a_				) and yy.get('choices'     , a_				):
                 for expected_choice in yy["choices"] + xx["choices"]:
                      self.assertEqual(xx['type'](a_				)     , yy['type'](a_				)				)
                 del xx["type"], yy["type"]

            self.assertEqual(a_     , a_				)




  def 	__lowercase(      self     :	int				)->       List[str]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				Dict			=		HfArgumentParser(a_				)

       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		argparse.ArgumentParser()
       expected.add_argument('--foo'     , type=a_     , required=a_				)
       expected.add_argument('--bar'     , type=a_     , required=a_				)
       expected.add_argument('--baz'     , type=a_     , required=a_				)
       expected.add_argument('--flag'     , type=a_     , default=a_     , const=a_     , nargs='?'				)
       self.argparsersEqual(a_     , a_				)

       SCREAMING_SNAKE_CASE__				:				List[Any]			=		['--foo', '1', '--baz', 'quux', '--bar', '0.5']
       ((SCREAMING_SNAKE_CASE__)			,							)				:				Tuple			=		parser.parse_args_into_dataclasses(a_     , look_for_args_file=a_				)
       self.assertFalse(example.flag				)




  def 	__lowercase(      self     :	Dict				)->       Optional[Any]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				Any			=		HfArgumentParser(a_				)

       SCREAMING_SNAKE_CASE__				:				Dict			=		argparse.ArgumentParser()
       expected.add_argument('--foo'     , default=42     , type=a_				)
       expected.add_argument('--baz'     , default='toto'     , type=a_     , help='help message'				)
       self.argparsersEqual(a_     , a_				)




  def 	__lowercase(      self     :	List[Any]				)->       Optional[Any]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				Any			=		argparse.ArgumentParser()
       expected.add_argument('--foo'     , type=a_     , default=a_     , const=a_     , nargs='?'				)
       expected.add_argument('--baz'     , type=a_     , default=a_     , const=a_     , nargs='?'				)
       # A boolean no_* argument always has to come after its "default: True" regular counter-part
       # and its default must be set to False
       expected.add_argument('--no_baz'     , action='store_false'     , default=a_     , dest='baz'				)
       expected.add_argument('--opt'     , type=a_     , default=a_				)

       SCREAMING_SNAKE_CASE__				:				str			=		[WithDefaultBoolExample]
       if is_python_no_less_than_3_10:
            dataclass_types.append(a_				)

       for dataclass_type in dataclass_types:
            SCREAMING_SNAKE_CASE__				:				Union[str, Any]			=		HfArgumentParser(a_				)
            self.argparsersEqual(a_     , a_				)

            SCREAMING_SNAKE_CASE__				:				Union[str, Any]			=		parser.parse_args([]				)
            self.assertEqual(a_     , Namespace(foo=a_     , baz=a_     , opt=a_				)				)

            SCREAMING_SNAKE_CASE__				:				List[Any]			=		parser.parse_args(['--foo', '--no_baz']				)
            self.assertEqual(a_     , Namespace(foo=a_     , baz=a_     , opt=a_				)				)

            SCREAMING_SNAKE_CASE__				:				Dict			=		parser.parse_args(['--foo', '--baz']				)
            self.assertEqual(a_     , Namespace(foo=a_     , baz=a_     , opt=a_				)				)

            SCREAMING_SNAKE_CASE__				:				List[str]			=		parser.parse_args(['--foo', 'True', '--baz', 'True', '--opt', 'True']				)
            self.assertEqual(a_     , Namespace(foo=a_     , baz=a_     , opt=a_				)				)

            SCREAMING_SNAKE_CASE__				:				Dict			=		parser.parse_args(['--foo', 'False', '--baz', 'False', '--opt', 'False']				)
            self.assertEqual(a_     , Namespace(foo=a_     , baz=a_     , opt=a_				)				)




  def 	__lowercase(      self     :	Union[str, Any]				)->       List[Any]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				List[Any]			=		HfArgumentParser(a_				)

       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		argparse.ArgumentParser()
       expected.add_argument(
           '--foo'     , default='toto'     , choices=['titi', 'toto', 42]     , type=make_choice_type_function(['titi', 'toto', 42]				)     , )
       self.argparsersEqual(a_     , a_				)

       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		parser.parse_args([]				)
       self.assertEqual(args.foo     , 'toto'				)
       SCREAMING_SNAKE_CASE__				:				List[str]			=		parser.parse_args_into_dataclasses([]				)[0]
       self.assertEqual(enum_ex.foo     , MixedTypeEnum.toto				)

       SCREAMING_SNAKE_CASE__				:				Tuple			=		parser.parse_args(['--foo', 'titi']				)
       self.assertEqual(args.foo     , 'titi'				)
       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		parser.parse_args_into_dataclasses(['--foo', 'titi']				)[0]
       self.assertEqual(enum_ex.foo     , MixedTypeEnum.titi				)

       SCREAMING_SNAKE_CASE__				:				str			=		parser.parse_args(['--foo', '42']				)
       self.assertEqual(args.foo     , 42				)
       SCREAMING_SNAKE_CASE__				:				str			=		parser.parse_args_into_dataclasses(['--foo', '42']				)[0]
       self.assertEqual(enum_ex.foo     , MixedTypeEnum.fourtytwo				)




  def 	__lowercase(      self     :	Union[str, Any]				)->       Union[str, Any]:




       """simple docstring"""

       @dataclass
       class       snake_case					:
         lowercase_			=     "toto"




       SCREAMING_SNAKE_CASE__				:				Tuple			=		HfArgumentParser(a_				)

       SCREAMING_SNAKE_CASE__				:				Union[str, Any]			=		argparse.ArgumentParser()
       expected.add_argument(
           '--foo'     , default='toto'     , choices=('titi', 'toto', 42)     , type=make_choice_type_function(['titi', 'toto', 42]				)     , )
       self.argparsersEqual(a_     , a_				)

       SCREAMING_SNAKE_CASE__				:				List[Any]			=		parser.parse_args([]				)
       self.assertEqual(args.foo     , 'toto'				)

       SCREAMING_SNAKE_CASE__				:				int			=		parser.parse_args(['--foo', 'titi']				)
       self.assertEqual(args.foo     , 'titi'				)

       SCREAMING_SNAKE_CASE__				:				List[Any]			=		parser.parse_args(['--foo', '42']				)
       self.assertEqual(args.foo     , 42				)




  def 	__lowercase(      self     :	Any				)->       Optional[int]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				Any			=		HfArgumentParser(a_				)

       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		argparse.ArgumentParser()
       expected.add_argument('--foo_int'     , nargs='+'     , default=[]     , type=a_				)
       expected.add_argument('--bar_int'     , nargs='+'     , default=[1, 2, 3]     , type=a_				)
       expected.add_argument('--foo_str'     , nargs='+'     , default=['Hallo', 'Bonjour', 'Hello']     , type=a_				)
       expected.add_argument('--foo_float'     , nargs='+'     , default=[0.1, 0.2, 0.3]     , type=a_				)

       self.argparsersEqual(a_     , a_				)

       SCREAMING_SNAKE_CASE__				:				int			=		parser.parse_args([]				)
       self.assertEqual(
           a_     , Namespace(foo_int=[]     , bar_int=[1, 2, 3]     , foo_str=['Hallo', 'Bonjour', 'Hello']     , foo_float=[0.1, 0.2, 0.3]				)     , )

       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		parser.parse_args('--foo_int 1 --bar_int 2 3 --foo_str a b c --foo_float 0.1 0.7'.split()				)
       self.assertEqual(a_     , Namespace(foo_int=[1]     , bar_int=[2, 3]     , foo_str=['a', 'b', 'c']     , foo_float=[0.1, 0.7]				)				)




  def 	__lowercase(      self     :	Optional[Any]				)->       Optional[int]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		argparse.ArgumentParser()
       expected.add_argument('--foo'     , default=a_     , type=a_				)
       expected.add_argument('--bar'     , default=a_     , type=a_     , help='help message'				)
       expected.add_argument('--baz'     , default=a_     , type=a_				)
       expected.add_argument('--ces'     , nargs='+'     , default=[]     , type=a_				)
       expected.add_argument('--des'     , nargs='+'     , default=[]     , type=a_				)

       SCREAMING_SNAKE_CASE__				:				int			=		[OptionalExample]
       if is_python_no_less_than_3_10:
            dataclass_types.append(a_				)

       for dataclass_type in dataclass_types:
            SCREAMING_SNAKE_CASE__				:				Dict			=		HfArgumentParser(a_				)

            self.argparsersEqual(a_     , a_				)

            SCREAMING_SNAKE_CASE__				:				List[Any]			=		parser.parse_args([]				)
            self.assertEqual(a_     , Namespace(foo=a_     , bar=a_     , baz=a_     , ces=[]     , des=[]				)				)

            SCREAMING_SNAKE_CASE__				:				str			=		parser.parse_args('--foo 12 --bar 3.14 --baz 42 --ces a b c --des 1 2 3'.split()				)
            self.assertEqual(a_     , Namespace(foo=12     , bar=3.14     , baz='42'     , ces=['a', 'b', 'c']     , des=[1, 2, 3]				)				)




  def 	__lowercase(      self     :	Optional[Any]				)->       List[Any]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				Any			=		HfArgumentParser(a_				)

       SCREAMING_SNAKE_CASE__				:				Dict			=		argparse.ArgumentParser()
       expected.add_argument('--required_list'     , nargs='+'     , type=a_     , required=a_				)
       expected.add_argument('--required_str'     , type=a_     , required=a_				)
       expected.add_argument(
           '--required_enum'     , type=make_choice_type_function(['titi', 'toto']				)     , choices=['titi', 'toto']     , required=a_     , )
       self.argparsersEqual(a_     , a_				)




  def 	__lowercase(      self     :	Any				)->       Dict:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				str			=		HfArgumentParser(a_				)

       SCREAMING_SNAKE_CASE__				:				Tuple			=		argparse.ArgumentParser()
       expected.add_argument('--foo'     , type=a_     , required=a_				)
       expected.add_argument(
           '--required_enum'     , type=make_choice_type_function(['titi', 'toto']				)     , choices=['titi', 'toto']     , required=a_     , )
       expected.add_argument('--opt'     , type=a_     , default=a_				)
       expected.add_argument('--baz'     , default='toto'     , type=a_     , help='help message'				)
       expected.add_argument('--foo_str'     , nargs='+'     , default=['Hallo', 'Bonjour', 'Hello']     , type=a_				)
       self.argparsersEqual(a_     , a_				)




  def 	__lowercase(      self     :	Tuple				)->       Dict:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				Union[str, Any]			=		HfArgumentParser(a_				)

       SCREAMING_SNAKE_CASE__				:				Union[str, Any]			=		{
           'foo': 12,
           'bar': 3.14,
           'baz': '42',
           'flag': True,
       }

       SCREAMING_SNAKE_CASE__				:				Tuple			=		parser.parse_dict(a_				)[0]
       SCREAMING_SNAKE_CASE__				:				Dict			=		BasicExample(**a_				)
       self.assertEqual(a_     , a_				)




  def 	__lowercase(      self     :	Any				)->       Dict:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				List[Any]			=		HfArgumentParser(a_				)

       SCREAMING_SNAKE_CASE__				:				Dict			=		{
           'foo': 12,
           'bar': 3.14,
           'baz': '42',
           'flag': True,
           'extra': 42,
       }

       self.assertRaises(a_     , parser.parse_dict     , a_     , allow_extra_keys=a_				)




  def 	__lowercase(      self     :	int				)->       Union[str, Any]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				int			=		HfArgumentParser(a_				)

       SCREAMING_SNAKE_CASE__				:				str			=		{
           'foo': 12,
           'bar': 3.14,
           'baz': '42',
           'flag': True,
       }
       with tempfile.TemporaryDirectory() as tmp_dir:
            SCREAMING_SNAKE_CASE__				:				Union[str, Any]			=		os.path.join(a_     , 'temp_json'				)
            os.mkdir(a_				)
            with open(temp_local_path + '.json'     , 'w+'				) as f:
                 json.dump(a_     , a_				)
            SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		parser.parse_yaml_file(Path(temp_local_path + '.json'				)				)[0]

       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		BasicExample(**a_				)
       self.assertEqual(a_     , a_				)




  def 	__lowercase(      self     :	List[Any]				)->       int:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				int			=		HfArgumentParser(a_				)

       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		{
           'foo': 12,
           'bar': 3.14,
           'baz': '42',
           'flag': True,
       }
       with tempfile.TemporaryDirectory() as tmp_dir:
            SCREAMING_SNAKE_CASE__				:				Any			=		os.path.join(a_     , 'temp_yaml'				)
            os.mkdir(a_				)
            with open(temp_local_path + '.yaml'     , 'w+'				) as f:
                 yaml.dump(a_     , a_				)
            SCREAMING_SNAKE_CASE__				:				Any			=		parser.parse_yaml_file(Path(temp_local_path + '.yaml'				)				)[0]
       SCREAMING_SNAKE_CASE__				:				List[str]			=		BasicExample(**a_				)
       self.assertEqual(a_     , a_				)




  def 	__lowercase(      self     :	Tuple				)->       Union[str, Any]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				Tuple			=		HfArgumentParser(a_				)
       self.assertIsNotNone(a_				)




import itertools
import random
import unittest

import numpy as np

from transformers import BatchFeature, SpeechTaFeatureExtractor
from transformers.testing_utils import require_torch
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_torch_available():
      import torch


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







def     _a				(      lowercase__  :			List[str]     ,  lowercase__  :			List[Any]=1.0     ,  lowercase__  :			Optional[int]=None     ,  lowercase__  :			List[str]=None ):







     '''simple docstring'''


     if rng is None:
          SCREAMING_SNAKE_CASE__				:				Optional[int]			=		global_rng

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

     return values

@require_torch
class       snake_case					(      unittest.TestCase						):




  def __init__(      self     :	List[Any]     , a_     :	Optional[Any]     , a_     :	Union[str, Any]=7     , a_     :	Any=400     , a_     :	List[Any]=2000     , a_     :	Tuple=1     , a_     :	Optional[int]=0.0     , a_     :	Optional[Any]=1_6000     , a_     :	str=True     , a_     :	Union[str, Any]=80     , a_     :	Dict=16     , a_     :	Tuple=64     , a_     :	Any="hann_window"     , a_     :	Union[str, Any]=80     , a_     :	List[Any]=7600     , a_     :	Optional[Any]=1e-1_0     , a_     :	Dict=True     , )->       Dict:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		parent
       SCREAMING_SNAKE_CASE__				:				List[Any]			=		batch_size
       SCREAMING_SNAKE_CASE__				:				str			=		min_seq_length
       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		max_seq_length
       SCREAMING_SNAKE_CASE__				:				str			=		(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
       SCREAMING_SNAKE_CASE__				:				int			=		feature_size
       SCREAMING_SNAKE_CASE__				:				str			=		padding_value
       SCREAMING_SNAKE_CASE__				:				Any			=		sampling_rate
       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		do_normalize
       SCREAMING_SNAKE_CASE__				:				int			=		num_mel_bins
       SCREAMING_SNAKE_CASE__				:				int			=		hop_length
       SCREAMING_SNAKE_CASE__				:				str			=		win_length
       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		win_function
       SCREAMING_SNAKE_CASE__				:				List[str]			=		fmin
       SCREAMING_SNAKE_CASE__				:				Dict			=		fmax
       SCREAMING_SNAKE_CASE__				:				int			=		mel_floor
       SCREAMING_SNAKE_CASE__				:				Tuple			=		return_attention_mask




  def 	__lowercase(      self     :	Dict				)->       Dict:




       """simple docstring"""
       return {
           "feature_size": self.feature_size,
           "padding_value": self.padding_value,
           "sampling_rate": self.sampling_rate,
           "do_normalize": self.do_normalize,
           "num_mel_bins": self.num_mel_bins,
           "hop_length": self.hop_length,
           "win_length": self.win_length,
           "win_function": self.win_function,
           "fmin": self.fmin,
           "fmax": self.fmax,
           "mel_floor": self.mel_floor,
           "return_attention_mask": self.return_attention_mask,
       }




  def 	__lowercase(      self     :	List[Any]     , a_     :	str=False     , a_     :	List[Any]=False				)->       Optional[Any]:




       """simple docstring"""
       def _flatten(a_     :	int				):
            return list(itertools.chain(*a_				)				)

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

       return speech_inputs




  def 	__lowercase(      self     :	Any     , a_     :	int=False     , a_     :	Any=False				)->       Union[str, Any]:




       """simple docstring"""
       if equal_length:
            SCREAMING_SNAKE_CASE__				:				str			=		[floats_list((self.max_seq_length, self.num_mel_bins)				) for _ in range(self.batch_size				)]
       else:
            # make sure that inputs increase in size
            SCREAMING_SNAKE_CASE__				:				Tuple			=		[
                floats_list((x, self.num_mel_bins)				)
                for x in range(self.min_seq_length     , self.max_seq_length     , self.seq_length_diff				)
            ]

       if numpify:
            SCREAMING_SNAKE_CASE__				:				List[str]			=		[np.asarray(a_				) for x in speech_inputs]

       return speech_inputs




@require_torch
class       snake_case					(      UpperCamelCase_   ,						unittest.TestCase						):
  lowercase_			=     SpeechTaFeatureExtractor




  def 	__lowercase(      self     :	List[Any]				)->       List[Any]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				Any			=		SpeechTaFeatureExtractionTester(self				)




  def 	__lowercase(      self     :	Any     , a_     :	Optional[int]				)->       List[str]:




       """simple docstring"""
       self.assertTrue(np.all(np.mean(a_     , axis=0				) < 1e-3				)				)
       self.assertTrue(np.all(np.abs(np.var(a_     , axis=0				) - 1				) < 1e-3				)				)




  def 	__lowercase(      self     :	Tuple				)->       Dict:




       """simple docstring"""
       # Tests that all call wrap to encode_plus and batch_encode_plus
       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
       # create three inputs of length 800, 1000, and 1200
       SCREAMING_SNAKE_CASE__				:				str			=		[floats_list((1, x)				)[0] for x in range(800     , 1400     , 200				)]
       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		[np.asarray(a_				) for speech_input in speech_inputs]

       # Test not batched input
       SCREAMING_SNAKE_CASE__				:				Union[str, Any]			=		feat_extract(speech_inputs[0]     , return_tensors='np'				).input_values
       SCREAMING_SNAKE_CASE__				:				Union[str, Any]			=		feat_extract(np_speech_inputs[0]     , return_tensors='np'				).input_values
       self.assertTrue(np.allclose(a_     , a_     , atol=1e-3				)				)

       # Test batched
       SCREAMING_SNAKE_CASE__				:				List[Any]			=		feat_extract(a_     , return_tensors='np'				).input_values
       SCREAMING_SNAKE_CASE__				:				List[str]			=		feat_extract(a_     , return_tensors='np'				).input_values
       for enc_seq_a, enc_seq_a in zip(a_     , a_				):
            self.assertTrue(np.allclose(a_     , a_     , atol=1e-3				)				)




  def 	__lowercase(      self     :	List[Any]				)->       List[Any]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				int			=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
       SCREAMING_SNAKE_CASE__				:				Dict			=		[floats_list((1, x)				)[0] for x in range(800     , 1400     , 200				)]

       SCREAMING_SNAKE_CASE__				:				int			=		['longest', 'max_length', 'do_not_pad']
       SCREAMING_SNAKE_CASE__				:				Tuple			=		[None, 1600, None]
       for max_length, padding in zip(a_     , a_				):
            SCREAMING_SNAKE_CASE__				:				str			=		feat_extract(a_     , padding=a_     , max_length=a_     , return_tensors='np'				)
            SCREAMING_SNAKE_CASE__				:				Optional[int]			=		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][:1000]				)
            self.assertTrue(input_values[0][1000:].sum() < 1e-6				)
            self._check_zero_mean_unit_variance(input_values[2][:1200]				)




  def 	__lowercase(      self     :	List[Any]				)->       List[str]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				int			=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
       SCREAMING_SNAKE_CASE__				:				List[Any]			=		range(800     , 1400     , 200				)
       SCREAMING_SNAKE_CASE__				:				int			=		[floats_list((1, x)				)[0] for x in lengths]

       SCREAMING_SNAKE_CASE__				:				int			=		['longest', 'max_length', 'do_not_pad']
       SCREAMING_SNAKE_CASE__				:				Union[str, Any]			=		[None, 1600, None]

       for max_length, padding in zip(a_     , a_				):
            SCREAMING_SNAKE_CASE__				:				List[str]			=		feat_extract(a_     , max_length=a_     , padding=a_				)
            SCREAMING_SNAKE_CASE__				:				int			=		processed.input_values

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




  def 	__lowercase(      self     :	int				)->       Tuple:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				Dict			=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
       SCREAMING_SNAKE_CASE__				:				Tuple			=		[floats_list((1, x)				)[0] for x in range(800     , 1400     , 200				)]
       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		feat_extract(
           a_     , truncation=a_     , max_length=1000     , padding='max_length'     , return_tensors='np'				)
       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		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 	__lowercase(      self     :	Optional[Any]				)->       str:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				str			=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		[floats_list((1, x)				)[0] for x in range(800     , 1400     , 200				)]
       SCREAMING_SNAKE_CASE__				:				List[str]			=		feat_extract(
           a_     , truncation=a_     , max_length=1000     , padding='longest'     , return_tensors='np'				)
       SCREAMING_SNAKE_CASE__				:				int			=		processed.input_values

       self._check_zero_mean_unit_variance(input_values[0, :800]				)
       self._check_zero_mean_unit_variance(input_values[1, :1000]				)
       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, 1000)				)

       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		[floats_list((1, x)				)[0] for x in range(800     , 1400     , 200				)]
       SCREAMING_SNAKE_CASE__				:				str			=		feat_extract(
           a_     , truncation=a_     , max_length=2000     , padding='longest'     , return_tensors='np'				)
       SCREAMING_SNAKE_CASE__				:				Union[str, Any]			=		processed.input_values

       self._check_zero_mean_unit_variance(input_values[0, :800]				)
       self._check_zero_mean_unit_variance(input_values[1, :1000]				)
       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, 1200)				)




  def 	__lowercase(      self     :	Any				)->       str:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				List[str]			=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		np.random.rand(100				).astype(np.floataa				)
       SCREAMING_SNAKE_CASE__				:				int			=		np_speech_inputs.tolist()

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




  def 	__lowercase(      self     :	Any				)->       Optional[int]:




       """simple docstring"""
       # Tests that all call wrap to encode_plus and batch_encode_plus
       SCREAMING_SNAKE_CASE__				:				List[str]			=		self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()				)
       # create three inputs of length 800, 1000, and 1200
       SCREAMING_SNAKE_CASE__				:				List[Any]			=		[floats_list((1, x)				)[0] for x in range(800     , 1400     , 200				)]
       SCREAMING_SNAKE_CASE__				:				Dict			=		[np.asarray(a_				) for speech_input in speech_inputs]

       # Test feature size
       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		feature_extractor(audio_target=a_     , padding=a_     , return_tensors='np'				).input_values
       self.assertTrue(input_values.ndim == 3				)
       self.assertTrue(input_values.shape[-1] == feature_extractor.num_mel_bins				)

       # Test not batched input
       SCREAMING_SNAKE_CASE__				:				Tuple			=		feature_extractor(speech_inputs[0]     , return_tensors='np'				).input_values
       SCREAMING_SNAKE_CASE__				:				int			=		feature_extractor(np_speech_inputs[0]     , return_tensors='np'				).input_values
       self.assertTrue(np.allclose(a_     , a_     , atol=1e-3				)				)

       # Test batched
       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		feature_extractor(a_     , return_tensors='np'				).input_values
       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		feature_extractor(a_     , return_tensors='np'				).input_values
       for enc_seq_a, enc_seq_a in zip(a_     , a_				):
            self.assertTrue(np.allclose(a_     , a_     , atol=1e-3				)				)

       # Test 2-D numpy arrays are batched.
       SCREAMING_SNAKE_CASE__				:				List[str]			=		[floats_list((1, x)				)[0] for x in (800, 800, 800)]
       SCREAMING_SNAKE_CASE__				:				List[str]			=		np.asarray(a_				)
       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		feature_extractor(a_     , return_tensors='np'				).input_values
       SCREAMING_SNAKE_CASE__				:				str			=		feature_extractor(a_     , return_tensors='np'				).input_values
       for enc_seq_a, enc_seq_a in zip(a_     , a_				):
            self.assertTrue(np.allclose(a_     , a_     , atol=1e-3				)				)




  def 	__lowercase(      self     :	Dict				)->       int:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				str			=		self.feat_extract_tester.prepare_inputs_for_target()
       SCREAMING_SNAKE_CASE__				:				List[Any]			=		self.feature_extraction_class(**self.feat_extract_dict				)
       SCREAMING_SNAKE_CASE__				:				Union[str, Any]			=		feat_extract.model_input_names[0]

       SCREAMING_SNAKE_CASE__				:				Any			=		BatchFeature({input_name: speech_inputs}				)

       self.assertTrue(all(len(a_				) == len(a_				) for x, y in zip(a_     , processed_features[input_name]				)				)				)

       SCREAMING_SNAKE_CASE__				:				str			=		self.feat_extract_tester.prepare_inputs_for_target(equal_length=a_				)
       SCREAMING_SNAKE_CASE__				:				Dict			=		BatchFeature({input_name: speech_inputs}     , tensor_type='np'				)

       SCREAMING_SNAKE_CASE__				:				List[Any]			=		processed_features[input_name]

       if len(batch_features_input.shape				) < 3:
            SCREAMING_SNAKE_CASE__				:				int			=		batch_features_input[:, :, None]

       self.assertTrue(
           batch_features_input.shape
           == (self.feat_extract_tester.batch_size, len(speech_inputs[0]				), self.feat_extract_tester.num_mel_bins)				)




  @require_torch
  def 	__lowercase(      self     :	List[Any]				)->       List[Any]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		self.feat_extract_tester.prepare_inputs_for_target(equal_length=a_				)
       SCREAMING_SNAKE_CASE__				:				Dict			=		self.feature_extraction_class(**self.feat_extract_dict				)
       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		feat_extract.model_input_names[0]

       SCREAMING_SNAKE_CASE__				:				Any			=		BatchFeature({input_name: speech_inputs}     , tensor_type='pt'				)

       SCREAMING_SNAKE_CASE__				:				Union[str, Any]			=		processed_features[input_name]

       if len(batch_features_input.shape				) < 3:
            SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		batch_features_input[:, :, None]

       self.assertTrue(
           batch_features_input.shape
           == (self.feat_extract_tester.batch_size, len(speech_inputs[0]				), self.feat_extract_tester.num_mel_bins)				)




  @require_torch
  def 	__lowercase(      self     :	Tuple				)->       List[str]:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				int			=		self.feature_extraction_class(**self.feat_extract_dict				)
       SCREAMING_SNAKE_CASE__				:				Dict			=		self.feat_extract_tester.prepare_inputs_for_target()
       SCREAMING_SNAKE_CASE__				:				str			=		feat_extract.model_input_names[0]

       SCREAMING_SNAKE_CASE__				:				List[Any]			=		BatchFeature({input_name: speech_inputs}				)

       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		feat_extract.num_mel_bins  # hack!

       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		feat_extract.pad(a_     , padding='longest'     , return_tensors='np'				)[input_name]
       SCREAMING_SNAKE_CASE__				:				Any			=		feat_extract.pad(a_     , padding='longest'     , return_tensors='pt'				)[input_name]

       self.assertTrue(abs(input_np.astype(np.floataa				).sum() - input_pt.numpy().astype(np.floataa				).sum()				) < 1e-2				)




  def 	__lowercase(      self     :	Any				)->       Dict:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				List[Any]			=		self.feat_extract_dict
       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		True
       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		self.feature_extraction_class(**a_				)
       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		self.feat_extract_tester.prepare_inputs_for_target()
       SCREAMING_SNAKE_CASE__				:				Any			=		[len(a_				) for x in speech_inputs]
       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		feat_extract.model_input_names[0]

       SCREAMING_SNAKE_CASE__				:				Union[str, Any]			=		BatchFeature({input_name: speech_inputs}				)

       SCREAMING_SNAKE_CASE__				:				Union[str, Any]			=		feat_extract.num_mel_bins  # hack!

       SCREAMING_SNAKE_CASE__				:				Any			=		feat_extract.pad(a_     , padding='longest'     , return_tensors='np'				)
       self.assertIn('attention_mask'     , a_				)
       self.assertListEqual(list(processed.attention_mask.shape				)     , list(processed[input_name].shape[:2]				)				)
       self.assertListEqual(processed.attention_mask.sum(-1				).tolist()     , a_				)




  def 	__lowercase(      self     :	str				)->       Any:




       """simple docstring"""
       SCREAMING_SNAKE_CASE__				:				Any			=		self.feat_extract_dict
       SCREAMING_SNAKE_CASE__				:				Union[str, Any]			=		True
       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		self.feature_extraction_class(**a_				)
       SCREAMING_SNAKE_CASE__				:				Optional[int]			=		self.feat_extract_tester.prepare_inputs_for_target()
       SCREAMING_SNAKE_CASE__				:				Tuple			=		[len(a_				) for x in speech_inputs]
       SCREAMING_SNAKE_CASE__				:				Union[str, Any]			=		feat_extract.model_input_names[0]

       SCREAMING_SNAKE_CASE__				:				Dict			=		BatchFeature({input_name: speech_inputs}				)
       SCREAMING_SNAKE_CASE__				:				str			=		min(a_				)

       SCREAMING_SNAKE_CASE__				:				Any			=		feat_extract.num_mel_bins  # hack!

       SCREAMING_SNAKE_CASE__				:				int			=		feat_extract.pad(
           a_     , padding='max_length'     , max_length=a_     , truncation=a_     , return_tensors='np'				)
       self.assertIn('attention_mask'     , a_				)
       self.assertListEqual(
           list(processed_pad.attention_mask.shape				)     , [processed_pad[input_name].shape[0], max_length]				)
       self.assertListEqual(
           processed_pad.attention_mask[:, :max_length].sum(-1				).tolist()     , [max_length for x in speech_inputs]				)




  def 	__lowercase(      self     :	Optional[int]     , a_     :	List[str]				)->       Any:




       """simple docstring"""
       from datasets import load_dataset

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

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




  def 	__lowercase(      self     :	List[str]				)->       List[Any]:




       """simple docstring"""
       # fmt: off
       SCREAMING_SNAKE_CASE__				:				List[Any]			=		torch.tensor(
           [2.3_8_0_4e-0_3, 2.0_7_5_2e-0_3, 1.9_8_3_6e-0_3, 2.1_0_5_7e-0_3, 1.6_1_7_4e-0_3,
            3.0_5_1_8e-0_4, 9.1_5_5_3e-0_5, 3.3_5_6_9e-0_4, 9.7_6_5_6e-0_4, 1.8_3_1_1e-0_3,
            2.0_1_4_2e-0_3, 2.1_0_5_7e-0_3, 1.7_3_9_5e-0_3, 4.5_7_7_6e-0_4, -3.9_6_7_3e-0_4,
            4.5_7_7_6e-0_4, 1.0_0_7_1e-0_3, 9.1_5_5_3e-0_5, 4.8_8_2_8e-0_4, 1.1_5_9_7e-0_3,
            7.3_2_4_2e-0_4, 9.4_6_0_4e-0_4, 1.8_0_0_5e-0_3, 1.8_3_1_1e-0_3, 8.8_5_0_1e-0_4,
            4.2_7_2_5e-0_4, 4.8_8_2_8e-0_4, 7.3_2_4_2e-0_4, 1.0_9_8_6e-0_3, 2.1_0_5_7e-0_3]				)
       # fmt: on

       SCREAMING_SNAKE_CASE__				:				List[str]			=		self._load_datasamples(1				)
       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		SpeechTaFeatureExtractor()
       SCREAMING_SNAKE_CASE__				:				List[str]			=		feature_extractor(a_     , return_tensors='pt'				).input_values
       self.assertEquals(input_values.shape     , (1, 9_3680)				)
       self.assertTrue(torch.allclose(input_values[0, :30]     , a_     , atol=1e-6				)				)




  def 	__lowercase(      self     :	Tuple				)->       List[Any]:




       """simple docstring"""
       # fmt: off
       SCREAMING_SNAKE_CASE__				:				Union[str, Any]			=		torch.tensor(
           [-2.6870, -3.0104, -3.1356, -3.5352, -3.0044, -3.0353, -3.4719, -3.6777,
            -3.1520, -2.9435, -2.6553, -2.8795, -2.9944, -2.5921, -3.0279, -3.0386,
            -3.0864, -3.1291, -3.2353, -2.7444, -2.6831, -2.7287, -3.1761, -3.1571,
            -3.2726, -3.0582, -3.1007, -3.4533, -3.4695, -3.0998]				)
       # fmt: on

       SCREAMING_SNAKE_CASE__				:				Optional[Any]			=		self._load_datasamples(1				)
       SCREAMING_SNAKE_CASE__				:				int			=		SpeechTaFeatureExtractor()
       SCREAMING_SNAKE_CASE__				:				str			=		feature_extractor(audio_target=a_     , return_tensors='pt'				).input_values
       self.assertEquals(input_values.shape     , (1, 366, 80)				)
       self.assertTrue(torch.allclose(input_values[0, 0, :30]     , a_     , atol=1e-4				)				)



