






"""simple docstring"""







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


lowerCAmelCase_		:			str				   =					random.Random()






def 					_lowerCAmelCase				(					lowerCAmelCase		,					lowerCAmelCase=1.0		,					lowerCAmelCase=None		,					lowerCAmelCase=None						):







       '''simple docstring'''
       if rng is None:
              UpperCAmelCase					=	global_rng

       UpperCAmelCase					=	[]
       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      UpperCamelCase_       (				unittest.TestCase     ):



  def __init__(					self       ,						snake_case__       ,						snake_case__=7       ,						snake_case__=4_00       ,						snake_case__=20_00       ,						snake_case__=1       ,						snake_case__=0.0       ,						snake_case__=2_40_00       ,						snake_case__=True       ,						)	->	int:



         """simple docstring"""





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



  def 						UpperCamelCase_ (					self )	->	int:



         """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,
         }



  def 						UpperCamelCase_ (					self       ,						snake_case__=False       ,						snake_case__=False )	->	int:



         """simple docstring"""





         def _flatten(snake_case__ ):
                return list(itertools.chain(*snake_case__ ) )

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

         return audio_inputs





@require_torch
class      UpperCamelCase_       (				a_      ,    unittest.TestCase     ):
  _A				:    Any =		EncodecFeatureExtractor



  def 						UpperCamelCase_ (					self )	->	str:



         """simple docstring"""





         UpperCAmelCase					=	EnCodecFeatureExtractionTester(self )



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



         """simple docstring"""





         UpperCAmelCase					=	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
         # create three inputs of length 800, 1000, and 1200
         UpperCAmelCase					=	[floats_list((1, x) )[0] for x in range(8_00       ,						14_00       ,						2_00 )]
         UpperCAmelCase					=	[np.asarray(snake_case__ ) for audio_input in audio_inputs]

         # Test not batched input
         UpperCAmelCase					=	feat_extract(audio_inputs[0]       ,						return_tensors="""np""" ).input_values
         UpperCAmelCase					=	feat_extract(np_audio_inputs[0]       ,						return_tensors="""np""" ).input_values
         self.assertTrue(np.allclose(snake_case__       ,						snake_case__       ,						atol=1e-3 ) )

         # Test batched
         UpperCAmelCase					=	feat_extract(snake_case__       ,						padding=snake_case__       ,						return_tensors="""np""" ).input_values
         UpperCAmelCase					=	feat_extract(snake_case__       ,						padding=snake_case__       ,						return_tensors="""np""" ).input_values
         for enc_seq_a, enc_seq_a in zip(snake_case__       ,						snake_case__ ):
                self.assertTrue(np.allclose(snake_case__       ,						snake_case__       ,						atol=1e-3 ) )



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



         """simple docstring"""





         UpperCAmelCase					=	self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict() )
         UpperCAmelCase					=	np.random.rand(1_00 ).astype(np.floataa )
         UpperCAmelCase					=	np_audio_inputs.tolist()

         for inputs in [py_audio_inputs, np_audio_inputs]:
                UpperCAmelCase					=	feature_extractor.pad([{"""input_values""": inputs}]       ,						return_tensors="""np""" )
                self.assertTrue(np_processed.input_values.dtype == np.floataa )
                UpperCAmelCase					=	feature_extractor.pad([{"""input_values""": inputs}]       ,						return_tensors="""pt""" )
                self.assertTrue(pt_processed.input_values.dtype == torch.floataa )



  def 						UpperCamelCase_ (					self       ,						snake_case__ )	->	int:



         """simple docstring"""





         from datasets import load_dataset

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

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



  def 						UpperCamelCase_ (					self )	->	Dict:



         """simple docstring"""





         UpperCAmelCase					=	torch.tensor(
             [2.3_804e-03, 2.0_752e-03, 1.9_836e-03, 2.1_057e-03, 1.6_174e-03,
              3.0_518e-04, 9.1_553e-05, 3.3_569e-04, 9.7_656e-04, 1.8_311e-03,
              2.0_142e-03, 2.1_057e-03, 1.7_395e-03, 4.5_776e-04, -3.9_673e-04,
              4.5_776e-04, 1.0_071e-03, 9.1_553e-05, 4.8_828e-04, 1.1_597e-03,
              7.3_242e-04, 9.4_604e-04, 1.8_005e-03, 1.8_311e-03, 8.8_501e-04,
              4.2_725e-04, 4.8_828e-04, 7.3_242e-04, 1.0_986e-03, 2.1_057e-03] )
         # fmt: on
         UpperCAmelCase					=	self._load_datasamples(1 )
         UpperCAmelCase					=	EncodecFeatureExtractor()
         UpperCAmelCase					=	feature_extractor(snake_case__       ,						return_tensors="""pt""" ).input_values
         self.assertEquals(input_values.shape       ,						(1, 1, 9_36_80) )
         self.assertTrue(torch.allclose(input_values[0, 0, :30]       ,						snake_case__       ,						atol=1e-6 ) )



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



         """simple docstring"""





         UpperCAmelCase					=	torch.tensor(
             [2.3_804e-03, 2.0_752e-03, 1.9_836e-03, 2.1_057e-03, 1.6_174e-03,
              3.0_518e-04, 9.1_553e-05, 3.3_569e-04, 9.7_656e-04, 1.8_311e-03,
              2.0_142e-03, 2.1_057e-03, 1.7_395e-03, 4.5_776e-04, -3.9_673e-04,
              4.5_776e-04, 1.0_071e-03, 9.1_553e-05, 4.8_828e-04, 1.1_597e-03,
              7.3_242e-04, 9.4_604e-04, 1.8_005e-03, 1.8_311e-03, 8.8_501e-04,
              4.2_725e-04, 4.8_828e-04, 7.3_242e-04, 1.0_986e-03, 2.1_057e-03] )
         # fmt: on
         UpperCAmelCase					=	self._load_datasamples(1 )
         UpperCAmelCase					=	[np.tile(input_audio[0][None]       ,						reps=(2, 1) )]
         input_audio[0][1] *= 0.5
         UpperCAmelCase					=	EncodecFeatureExtractor(feature_size=2 )
         UpperCAmelCase					=	feature_extractor(snake_case__       ,						return_tensors="""pt""" ).input_values
         self.assertEquals(input_values.shape       ,						(1, 2, 9_36_80) )
         self.assertTrue(torch.allclose(input_values[0, 0, :30]       ,						snake_case__       ,						atol=1e-6 ) )
         self.assertTrue(torch.allclose(input_values[0, 1, :30]       ,						EXPECTED_INPUT_VALUES * 0.5       ,						atol=1e-6 ) )



  def 						UpperCamelCase_ (					self )	->	Tuple:



         """simple docstring"""





         UpperCAmelCase					=	self._load_datasamples(2 )
         # would be easier if the stride was like
         UpperCAmelCase					=	EncodecFeatureExtractor(feature_size=1       ,						chunk_length_s=1       ,						overlap=0.01 )

         # pad and trunc raise an error ?
         with self.assertRaisesRegex(
             snake_case__       ,						"""^Both padding and truncation were set. Make sure you only set one.$"""       ,						):
                UpperCAmelCase					=	feature_extractor(
                    snake_case__       ,						padding="""max_length"""       ,						truncation=snake_case__       ,						return_tensors="""pt""" ).input_values

         # truncate to chunk
         UpperCAmelCase					=	feature_extractor(snake_case__       ,						truncation=snake_case__       ,						return_tensors="""pt""" ).input_values
         self.assertEquals(truncated_outputs.shape       ,						(2, 1, 7_15_20) )  # 2 chunks

         # force truncate to max_length
         UpperCAmelCase					=	feature_extractor(
             snake_case__       ,						truncation=snake_case__       ,						max_length=4_80_00       ,						return_tensors="""pt""" ).input_values
         self.assertEquals(truncated_outputs.shape       ,						(2, 1, 4_80_00) )

         # pad to chunk
         UpperCAmelCase					=	feature_extractor(snake_case__       ,						padding=snake_case__       ,						return_tensors="""pt""" ).input_values
         self.assertEquals(padded_outputs.shape       ,						(2, 1, 9_52_80) )

         # pad to chunk
         UpperCAmelCase					=	feature_extractor(snake_case__       ,						return_tensors="""pt""" ).input_values
         self.assertEquals(truncated_outputs.shape       ,						(2, 1, 9_52_80) )

         # force pad to max length
         UpperCAmelCase					=	feature_extractor(
             snake_case__       ,						padding="""max_length"""       ,						max_length=10_00_00       ,						return_tensors="""pt""" ).input_values
         self.assertEquals(truncated_outputs.shape       ,						(2, 1, 10_00_00) )

         # force no pad
         with self.assertRaisesRegex(
             snake_case__       ,						"""^Unable to create tensor, you should probably activate padding with 'padding=True' to have batched tensors with the same length.$"""       ,						):
                UpperCAmelCase					=	feature_extractor(snake_case__       ,						padding=snake_case__       ,						return_tensors="""pt""" ).input_values

         UpperCAmelCase					=	feature_extractor(input_audio[0]       ,						padding=snake_case__       ,						return_tensors="""pt""" ).input_values
         self.assertEquals(truncated_outputs.shape       ,						(1, 1, 9_36_80) )

         # no pad if no chunk_length_s
         UpperCAmelCase					=	None
         with self.assertRaisesRegex(
             snake_case__       ,						"""^Unable to create tensor, you should probably activate padding with 'padding=True' to have batched tensors with the same length.$"""       ,						):
                UpperCAmelCase					=	feature_extractor(snake_case__       ,						padding=snake_case__       ,						return_tensors="""pt""" ).input_values

         UpperCAmelCase					=	feature_extractor(input_audio[0]       ,						padding=snake_case__       ,						return_tensors="""pt""" ).input_values
         self.assertEquals(truncated_outputs.shape       ,						(1, 1, 9_36_80) )

         # no pad if no overlap
         UpperCAmelCase					=	2
         UpperCAmelCase					=	None
         with self.assertRaisesRegex(
             snake_case__       ,						"""^Unable to create tensor, you should probably activate padding with 'padding=True' to have batched tensors with the same length.$"""       ,						):
                UpperCAmelCase					=	feature_extractor(snake_case__       ,						padding=snake_case__       ,						return_tensors="""pt""" ).input_values

         UpperCAmelCase					=	feature_extractor(input_audio[0]       ,						padding=snake_case__       ,						return_tensors="""pt""" ).input_values
         self.assertEquals(truncated_outputs.shape       ,						(1, 1, 9_36_80) )








"""simple docstring"""







import unittest
from timeit import timeit






def 					_lowerCAmelCase				(					lowerCAmelCase		,					lowerCAmelCase						):







       '''simple docstring'''
       UpperCAmelCase					=	first_num if first_num >= second_num else second_num
       UpperCAmelCase					=	max_num
       while (common_mult % first_num > 0) or (common_mult % second_num > 0):
              common_mult += max_num
       return common_mult






def 					_lowerCAmelCase				(					lowerCAmelCase		,					lowerCAmelCase						):







       '''simple docstring'''
       return b if a == 0 else greatest_common_divisor(b % a		,					lowerCAmelCase						)






def 					_lowerCAmelCase				(					lowerCAmelCase		,					lowerCAmelCase						):







       '''simple docstring'''
       return first_num // greatest_common_divisor(lowerCAmelCase		,					lowerCAmelCase						) * second_num






def 					_lowerCAmelCase				(					):







       '''simple docstring'''
       UpperCAmelCase					=	(
           """from __main__ import least_common_multiple_slow, least_common_multiple_fast"""
       )
       print(
           """least_common_multiple_slow():"""		,					timeit("""least_common_multiple_slow(1000, 999)"""		,					setup=lowerCAmelCase						)		,					)
       print(
           """least_common_multiple_fast():"""		,					timeit("""least_common_multiple_fast(1000, 999)"""		,					setup=lowerCAmelCase						)		,					)





class      UpperCamelCase_       (				unittest.TestCase     ):
  _A				:    List[str] =		(
      (10, 20),
      (13, 15),
      (4, 31),
      (10, 42),
      (43, 34),
      (5, 12),
      (12, 25),
      (10, 25),
      (6, 9),
  )
  _A				:    int =		(20, 195, 124, 210, 1462, 60, 300, 50, 18)



  def 						UpperCamelCase_ (					self )	->	Any:



         """simple docstring"""





         for i, (first_num, second_num) in enumerate(self.test_inputs ):
                UpperCAmelCase					=	least_common_multiple_slow(snake_case__       ,						snake_case__ )
                UpperCAmelCase					=	least_common_multiple_fast(snake_case__       ,						snake_case__ )
                with self.subTest(i=snake_case__ ):
                       self.assertEqual(snake_case__       ,						self.expected_results[i] )
                       self.assertEqual(snake_case__       ,						self.expected_results[i] )


if __name__ == "__main__":
 benchmark()
 unittest.main()
