code: 
  
  
  import itertools
  import random
  import unittest
  
  import numpy as np
  
  from transformers import EncodecFeatureExtractor
  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    :				List[str]       =					random.Random()
  def     UpperCAmelCase__		(UpperCamelCase_							,UpperCamelCase_=1.0							,UpperCamelCase_=None							,UpperCamelCase_=None       ):
  
  
  
       """simple docstring"""
  
  
  
  
  
  
       if rng is None:
            snake_case					=       global_rng
  
       snake_case					=       []
       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 						A__      (					unittest.TestCase			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      def __init__( self    ,      __snake_case    ,      __snake_case=7    ,      __snake_case=4_0_0    ,      __snake_case=2_0_0_0    ,      __snake_case=1    ,      __snake_case=0.0    ,      __snake_case=2_4_0_0_0    ,      __snake_case=True    ,      ):
           snake_case					=       parent
           snake_case					=       batch_size
           snake_case					=       min_seq_length
           snake_case					=       max_seq_length
           snake_case					=       (self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
           snake_case					=       feature_size
           snake_case					=       padding_value
           snake_case					=       sampling_rate
           snake_case					=       return_attention_mask
      def 	a_    ( self					):
           return {
               "feature_size": self.feature_size,
               "padding_value": self.padding_value,
               "sampling_rate": self.sampling_rate,
               "return_attention_mask": self.return_attention_mask,
           }
  
  
  
  
      def 	a_    ( self    ,      __snake_case=False    ,      __snake_case=False					):
           def _flatten(__snake_case					):
                return list(itertools.chain(*_lowerCAmelCase					)					)
  
           if equal_length:
                snake_case					=       floats_list((self.batch_size, self.max_seq_length)					)
           else:
                # make sure that inputs increase in size
                snake_case					=       [
                    _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:
                snake_case					=       [np.asarray(_lowerCAmelCase					) for x in audio_inputs]
  
           return audio_inputs
  
  
  
  
  
  
  @require_torch
  class 						A__      (					A__ ,      unittest.TestCase			):
  
  
  
  
  
  
  
      """simple docstring"""
  
      __magic_name__    						=	EncodecFeatureExtractor
      def 	a_    ( self					):
           snake_case					=       EnCodecFeatureExtractionTester(self					)
      def 	a_    ( self					):
           # Tests that all call wrap to encode_plus and batch_encode_plus
           snake_case					=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
           # create three inputs of length 800, 1000, and 1200
           snake_case					=       [floats_list((1, x)					)[0] for x in range(8_0_0    ,      1_4_0_0    ,      2_0_0					)]
           snake_case					=       [np.asarray(_lowerCAmelCase					) for audio_input in audio_inputs]
  
           # Test not batched input
           snake_case					=       feat_extract(audio_inputs[0]    ,      return_tensors='''np'''					).input_values
           snake_case					=       feat_extract(np_audio_inputs[0]    ,      return_tensors='''np'''					).input_values
           self.assertTrue(np.allclose(_lowerCAmelCase    ,      _lowerCAmelCase    ,      atol=1E-3					)					)
  
           # Test batched
           snake_case					=       feat_extract(_lowerCAmelCase    ,      padding=_lowerCAmelCase    ,      return_tensors='''np'''					).input_values
           snake_case					=       feat_extract(_lowerCAmelCase    ,      padding=_lowerCAmelCase    ,      return_tensors='''np'''					).input_values
           for enc_seq_a, enc_seq_a in zip(_lowerCAmelCase    ,      _lowerCAmelCase					):
                self.assertTrue(np.allclose(_lowerCAmelCase    ,      _lowerCAmelCase    ,      atol=1E-3					)					)
      def 	a_    ( self					):
           snake_case					=       self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()					)
           snake_case					=       np.random.rand(1_0_0					).astype(np.floataa					)
           snake_case					=       np_audio_inputs.tolist()
  
           for inputs in [py_audio_inputs, np_audio_inputs]:
                snake_case					=       feature_extractor.pad([{'''input_values''': inputs}]    ,      return_tensors='''np'''					)
                self.assertTrue(np_processed.input_values.dtype == np.floataa					)
                snake_case					=       feature_extractor.pad([{'''input_values''': inputs}]    ,      return_tensors='''pt'''					)
                self.assertTrue(pt_processed.input_values.dtype == torch.floataa					)
      def 	a_    ( self    ,      __snake_case					):
           from datasets import load_dataset
  
           snake_case					=       load_dataset('''hf-internal-testing/librispeech_asr_dummy'''    ,      '''clean'''    ,      split='''validation'''					)
           # automatic decoding with librispeech
           snake_case					=       ds.sort('''id'''					).select(range(_lowerCAmelCase					)					)[:num_samples]['''audio''']
  
           return [x["array"] for x in audio_samples]
      def 	a_    ( self					):
           # fmt: off
           snake_case					=       torch.tensor(
               [2.38_04E-03, 2.07_52E-03, 1.98_36E-03, 2.10_57E-03, 1.61_74E-03,
                3.05_18E-04, 9.15_53E-05, 3.35_69E-04, 9.76_56E-04, 1.83_11E-03,
                2.01_42E-03, 2.10_57E-03, 1.73_95E-03, 4.57_76E-04, -3.96_73E-04,
                4.57_76E-04, 1.00_71E-03, 9.15_53E-05, 4.88_28E-04, 1.15_97E-03,
                7.32_42E-04, 9.46_04E-04, 1.80_05E-03, 1.83_11E-03, 8.85_01E-04,
                4.27_25E-04, 4.88_28E-04, 7.32_42E-04, 1.09_86E-03, 2.10_57E-03]					)
           # fmt: on
           snake_case					=       self._load_datasamples(1					)
           snake_case					=       EncodecFeatureExtractor()
           snake_case					=       feature_extractor(_lowerCAmelCase    ,      return_tensors='''pt'''					).input_values
           self.assertEquals(input_values.shape    ,      (1, 1, 9_3_6_8_0)					)
           self.assertTrue(torch.allclose(input_values[0, 0, :3_0]    ,      _lowerCAmelCase    ,      atol=1E-6					)					)
      def 	a_    ( self					):
           # fmt: off
           snake_case					=       torch.tensor(
               [2.38_04E-03, 2.07_52E-03, 1.98_36E-03, 2.10_57E-03, 1.61_74E-03,
                3.05_18E-04, 9.15_53E-05, 3.35_69E-04, 9.76_56E-04, 1.83_11E-03,
                2.01_42E-03, 2.10_57E-03, 1.73_95E-03, 4.57_76E-04, -3.96_73E-04,
                4.57_76E-04, 1.00_71E-03, 9.15_53E-05, 4.88_28E-04, 1.15_97E-03,
                7.32_42E-04, 9.46_04E-04, 1.80_05E-03, 1.83_11E-03, 8.85_01E-04,
                4.27_25E-04, 4.88_28E-04, 7.32_42E-04, 1.09_86E-03, 2.10_57E-03]					)
           # fmt: on
           snake_case					=       self._load_datasamples(1					)
           snake_case					=       [np.tile(input_audio[0][None]    ,      reps=(2, 1)					)]
           input_audio[0][1] *= 0.5
           snake_case					=       EncodecFeatureExtractor(feature_size=2					)
           snake_case					=       feature_extractor(_lowerCAmelCase    ,      return_tensors='''pt'''					).input_values
           self.assertEquals(input_values.shape    ,      (1, 2, 9_3_6_8_0)					)
           self.assertTrue(torch.allclose(input_values[0, 0, :3_0]    ,      _lowerCAmelCase    ,      atol=1E-6					)					)
           self.assertTrue(torch.allclose(input_values[0, 1, :3_0]    ,      EXPECTED_INPUT_VALUES * 0.5    ,      atol=1E-6					)					)
  
  
  
  
      def 	a_    ( self					):
           snake_case					=       self._load_datasamples(2					)
           # would be easier if the stride was like
           snake_case					=       EncodecFeatureExtractor(feature_size=1    ,      chunk_length_s=1    ,      overlap=0.01					)
  
           # pad and trunc raise an error ?
           with self.assertRaisesRegex(
               _lowerCAmelCase    ,      '''^Both padding and truncation were set. Make sure you only set one.$'''    ,      ):
                snake_case					=       feature_extractor(
                    _lowerCAmelCase    ,      padding='''max_length'''    ,      truncation=_lowerCAmelCase    ,      return_tensors='''pt'''					).input_values
  
           # truncate to chunk
           snake_case					=       feature_extractor(_lowerCAmelCase    ,      truncation=_lowerCAmelCase    ,      return_tensors='''pt'''					).input_values
           self.assertEquals(truncated_outputs.shape    ,      (2, 1, 7_1_5_2_0)					)  # 2 chunks
  
           # force truncate to max_length
           snake_case					=       feature_extractor(
               _lowerCAmelCase    ,      truncation=_lowerCAmelCase    ,      max_length=4_8_0_0_0    ,      return_tensors='''pt'''					).input_values
           self.assertEquals(truncated_outputs.shape    ,      (2, 1, 4_8_0_0_0)					)
  
           # pad to chunk
           snake_case					=       feature_extractor(_lowerCAmelCase    ,      padding=_lowerCAmelCase    ,      return_tensors='''pt'''					).input_values
           self.assertEquals(padded_outputs.shape    ,      (2, 1, 9_5_2_8_0)					)
  
           # pad to chunk
           snake_case					=       feature_extractor(_lowerCAmelCase    ,      return_tensors='''pt'''					).input_values
           self.assertEquals(truncated_outputs.shape    ,      (2, 1, 9_5_2_8_0)					)
  
           # force pad to max length
           snake_case					=       feature_extractor(
               _lowerCAmelCase    ,      padding='''max_length'''    ,      max_length=1_0_0_0_0_0    ,      return_tensors='''pt'''					).input_values
           self.assertEquals(truncated_outputs.shape    ,      (2, 1, 1_0_0_0_0_0)					)
  
           # force no pad
           with self.assertRaisesRegex(
               _lowerCAmelCase    ,      '''^Unable to create tensor, you should probably activate padding with \'padding=True\' to have batched tensors with the same length.$'''    ,      ):
                snake_case					=       feature_extractor(_lowerCAmelCase    ,      padding=_lowerCAmelCase    ,      return_tensors='''pt'''					).input_values
  
           snake_case					=       feature_extractor(input_audio[0]    ,      padding=_lowerCAmelCase    ,      return_tensors='''pt'''					).input_values
           self.assertEquals(truncated_outputs.shape    ,      (1, 1, 9_3_6_8_0)					)
  
           # no pad if no chunk_length_s
           snake_case					=       None
           with self.assertRaisesRegex(
               _lowerCAmelCase    ,      '''^Unable to create tensor, you should probably activate padding with \'padding=True\' to have batched tensors with the same length.$'''    ,      ):
                snake_case					=       feature_extractor(_lowerCAmelCase    ,      padding=_lowerCAmelCase    ,      return_tensors='''pt'''					).input_values
  
           snake_case					=       feature_extractor(input_audio[0]    ,      padding=_lowerCAmelCase    ,      return_tensors='''pt'''					).input_values
           self.assertEquals(truncated_outputs.shape    ,      (1, 1, 9_3_6_8_0)					)
  
           # no pad if no overlap
           snake_case					=       2
           snake_case					=       None
           with self.assertRaisesRegex(
               _lowerCAmelCase    ,      '''^Unable to create tensor, you should probably activate padding with \'padding=True\' to have batched tensors with the same length.$'''    ,      ):
                snake_case					=       feature_extractor(_lowerCAmelCase    ,      padding=_lowerCAmelCase    ,      return_tensors='''pt'''					).input_values
  
           snake_case					=       feature_extractor(input_audio[0]    ,      padding=_lowerCAmelCase    ,      return_tensors='''pt'''					).input_values
           self.assertEquals(truncated_outputs.shape    ,      (1, 1, 9_3_6_8_0)					)
  
  
  
  
style_context: 
  
  
  import torch
  
  from accelerate import Accelerator, DistributedType
  
  class 						A__      :
  
  
  
  
  
  
  
      """simple docstring"""
  
      def __enter__( self					):
           if self.enabled:
                snake_case					=       self.model.no_sync()
                self.model_sync_obj.__enter__()
  
           return self
      def __exit__( self    ,      __snake_case    ,      __snake_case    ,      __snake_case					):
           if self.enabled:
                # Average all models on exit
                self._sync_and_avg_model_params()
                self.model_sync_obj.__exit__(__snake_case    ,      __snake_case    ,      __snake_case					)
      def __init__( self    ,      __snake_case    ,      __snake_case    ,      __snake_case    ,      __snake_case = True					):
           if accelerator.distributed_type not in [
               DistributedType.NO,
               DistributedType.MULTI_CPU,
               DistributedType.MULTI_GPU,
           ]:
                raise NotImplementedError('''LocalSGD is supported only for CPUs and GPUs (no DeepSpeed or MegatronLM)'''					)
           snake_case					=       enabled and accelerator.distributed_type != DistributedType.NO
           snake_case					=       0
           if self.enabled:
                snake_case					=       accelerator
                snake_case					=       model
                snake_case					=       local_sgd_steps
      def 	a_    ( self					):
           self.num_steps += 1
           if not self.enabled:
                return
  
           if self.num_steps % self.local_sgd_steps == 0:
                self._sync_and_avg_model_params()
  
  
  
  
      def 	a_    ( self					):
           self.accelerator.wait_for_everyone()
           with self.accelerator.autocast():
                for param in self.model.parameters():
                     snake_case					=       self.accelerator.reduce(param.data    ,      reduction='''mean'''					)
  
  
  
  
  