





'''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


__UpperCamelCase      :   Tuple								=     random.Random()





def      lowercase  (  lowerCAmelCase :						Dict   , lowerCAmelCase :						Dict=1.0   , lowerCAmelCase :						Tuple=None   , lowerCAmelCase :						Dict=None):



				"""simple docstring"""


				if rng is None:
								_A :				List[str]				=  global_rng

				_A :				Optional[int]				=  []
				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  lowerCamelCase__				(						unittest.TestCase						):


	"""simple docstring"""





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




	def 				_lowerCamelCase				(					self			)       ->				Optional[int]:
					return {
					    "feature_size": self.feature_size,
					    "padding_value": self.padding_value,
					    "sampling_rate": self.sampling_rate,
					    "return_attention_mask": self.return_attention_mask,
					}




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__=False   ,     UpperCAmelCase__=False			)       ->				List[Any]:



					def _flatten(UpperCAmelCase__			):
									return list(itertools.chain(*__UpperCAmelCase			)			)

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

					return audio_inputs



@require_torch
class  lowerCamelCase__				(						_lowerCAmelCase  ,							unittest.TestCase						):


	"""simple docstring"""





	__magic_name__							   =		EncodecFeatureExtractor




	def 				_lowerCamelCase				(					self			)       ->				Tuple:
					_A :				Optional[Any]				=  EnCodecFeatureExtractionTester(self			)




	def 				_lowerCamelCase				(					self			)       ->				List[str]:
					_A :				Optional[int]				=  self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
					# create three inputs of length 800, 1000, and 1200
					_A :				Tuple				=  [floats_list((1, x)			)[0] for x in range(8_0_0   ,     1_4_0_0   ,     2_0_0			)]
					_A :				Union[str, Any]				=  [np.asarray(__UpperCAmelCase			) for audio_input in audio_inputs]

					# Test not batched input
					_A :				List[Any]				=  feat_extract(audio_inputs[0]   ,     return_tensors='''np'''			).input_values
					_A :				Optional[int]				=  feat_extract(np_audio_inputs[0]   ,     return_tensors='''np'''			).input_values
					self.assertTrue(np.allclose(__UpperCAmelCase   ,     __UpperCAmelCase   ,     atol=1e-3			)			)

					# Test batched
					_A :				List[str]				=  feat_extract(__UpperCAmelCase   ,     padding=__UpperCAmelCase   ,     return_tensors='''np'''			).input_values
					_A :				str				=  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			)			)




	def 				_lowerCamelCase				(					self			)       ->				str:
					_A :				str				=  self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()			)
					_A :				Optional[Any]				=  np.random.rand(1_0_0			).astype(np.floataa			)
					_A :				Any				=  np_audio_inputs.tolist()

					for inputs in [py_audio_inputs, np_audio_inputs]:
									_A :				Optional[int]				=  feature_extractor.pad([{'''input_values''': inputs}]   ,     return_tensors='''np'''			)
									self.assertTrue(np_processed.input_values.dtype == np.floataa			)
									_A :				Union[str, Any]				=  feature_extractor.pad([{'''input_values''': inputs}]   ,     return_tensors='''pt'''			)
									self.assertTrue(pt_processed.input_values.dtype == torch.floataa			)




	def 				_lowerCamelCase				(					self   ,     UpperCAmelCase__			)       ->				List[str]:
					from datasets import load_dataset

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

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




	def 				_lowerCamelCase				(					self			)       ->				Any:
					_A :				Optional[int]				=  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
					_A :				Any				=  self._load_datasamples(1			)
					_A :				int				=  EncodecFeatureExtractor()
					_A :				Tuple				=  feature_extractor(__UpperCAmelCase   ,     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]   ,     __UpperCAmelCase   ,     atol=1e-6			)			)




	def 				_lowerCamelCase				(					self			)       ->				Dict:
					_A :				int				=  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
					_A :				Optional[Any]				=  self._load_datasamples(1			)
					_A :				Dict				=  [np.tile(input_audio[0][None]   ,     reps=(2, 1)			)]
					input_audio[0][1] *= 0.5
					_A :				int				=  EncodecFeatureExtractor(feature_size=2			)
					_A :				Union[str, Any]				=  feature_extractor(__UpperCAmelCase   ,     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]   ,     __UpperCAmelCase   ,     atol=1e-6			)			)
					self.assertTrue(torch.allclose(input_values[0, 1, :3_0]   ,     EXPECTED_INPUT_VALUES * 0.5   ,     atol=1e-6			)			)




	def 				_lowerCamelCase				(					self			)       ->				int:
					_A :				Any				=  self._load_datasamples(2			)
					# would be easier if the stride was like
					_A :				Optional[int]				=  EncodecFeatureExtractor(feature_size=1   ,     chunk_length_s=1   ,     overlap=0.0_1			)

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

					# truncate to chunk
					_A :				List[str]				=  feature_extractor(__UpperCAmelCase   ,     truncation=__UpperCAmelCase   ,     return_tensors='''pt'''			).input_values
					self.assertEquals(truncated_outputs.shape   ,     (2, 1, 7_1_5_2_0)			)  # 2 chunks

					# force truncate to max_length
					_A :				Tuple				=  feature_extractor(
					    __UpperCAmelCase   ,     truncation=__UpperCAmelCase   ,     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
					_A :				List[Any]				=  feature_extractor(__UpperCAmelCase   ,     padding=__UpperCAmelCase   ,     return_tensors='''pt'''			).input_values
					self.assertEquals(padded_outputs.shape   ,     (2, 1, 9_5_2_8_0)			)

					# pad to chunk
					_A :				Optional[int]				=  feature_extractor(__UpperCAmelCase   ,     return_tensors='''pt'''			).input_values
					self.assertEquals(truncated_outputs.shape   ,     (2, 1, 9_5_2_8_0)			)

					# force pad to max length
					_A :				List[str]				=  feature_extractor(
					    __UpperCAmelCase   ,     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(
					    __UpperCAmelCase   ,     '''^Unable to create tensor, you should probably activate padding with \'padding=True\' to have batched tensors with the same length.$'''   ,     ):
									_A :				Optional[Any]				=  feature_extractor(__UpperCAmelCase   ,     padding=__UpperCAmelCase   ,     return_tensors='''pt'''			).input_values

					_A :				Dict				=  feature_extractor(input_audio[0]   ,     padding=__UpperCAmelCase   ,     return_tensors='''pt'''			).input_values
					self.assertEquals(truncated_outputs.shape   ,     (1, 1, 9_3_6_8_0)			)

					# no pad if no chunk_length_s
					_A :				List[Any]				=  None
					with self.assertRaisesRegex(
					    __UpperCAmelCase   ,     '''^Unable to create tensor, you should probably activate padding with \'padding=True\' to have batched tensors with the same length.$'''   ,     ):
									_A :				Optional[Any]				=  feature_extractor(__UpperCAmelCase   ,     padding=__UpperCAmelCase   ,     return_tensors='''pt'''			).input_values

					_A :				Union[str, Any]				=  feature_extractor(input_audio[0]   ,     padding=__UpperCAmelCase   ,     return_tensors='''pt'''			).input_values
					self.assertEquals(truncated_outputs.shape   ,     (1, 1, 9_3_6_8_0)			)

					# no pad if no overlap
					_A :				List[Any]				=  2
					_A :				Tuple				=  None
					with self.assertRaisesRegex(
					    __UpperCAmelCase   ,     '''^Unable to create tensor, you should probably activate padding with \'padding=True\' to have batched tensors with the same length.$'''   ,     ):
									_A :				str				=  feature_extractor(__UpperCAmelCase   ,     padding=__UpperCAmelCase   ,     return_tensors='''pt'''			).input_values

					_A :				Optional[int]				=  feature_extractor(input_audio[0]   ,     padding=__UpperCAmelCase   ,     return_tensors='''pt'''			).input_values
					self.assertEquals(truncated_outputs.shape   ,     (1, 1, 9_3_6_8_0)			)

"""simple docstring"""




import os
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
					import torch

					from transformers import GPTaLMHeadModel

@require_torch
@require_sentencepiece
@require_tokenizers
class        __SCREAMING_SNAKE_CASE     (   unittest.TestCase							):







				@slow
				@unittest.skip('''Model is not available.'''  )
				def   lowercase_		(    self    :Union[str, Any]  )      ->					Optional[int]:




									"""simple docstring"""



									lowerCamelCase__		:   Any  				=					'''nvidia/megatron-gpt2-345m/'''
									if "MYDIR" in os.environ:
														lowerCamelCase__		:   str  				=					os.path.join(os.environ['''MYDIR''']      ,__UpperCAmelCase  )
									lowerCamelCase__		:   Dict  				=					GPTaLMHeadModel.from_pretrained(__UpperCAmelCase  )
									model.to(__UpperCAmelCase  )
									model.half()

									lowerCamelCase__		:   Dict  				=					torch.tensor(
									    [[1_01, 71_10, 10_05, 10_56, 20_23, 1_13_33, 1_74_13, 10_29, 1_02]]      ,device=__UpperCAmelCase      ,dtype=torch.long      ,)

									with torch.no_grad():
														lowerCamelCase__		:   Tuple  				=					model(__UpperCAmelCase  ).logits

									lowerCamelCase__		:   List[str]  				=					torch.Size((1, 9, 5_02_57)  )
									self.assertEqual(output.shape      ,__UpperCAmelCase  )

									lowerCamelCase__		:   Tuple  				=					torch.tensor(
									    [
									        4.9_414,
									        -0.2_920,
									        -1.2_148,
									        -4.0_273,
									        -0.5_161,
									        -5.2_109,
									        -1.2_412,
									        -1.8_301,
									        -1.7_734,
									        -4.7_148,
									        -0.2_317,
									        -1.0_811,
									        -2.1_777,
									        0.4_141,
									        -3.7_969,
									        -4.0_586,
									        -2.5_332,
									        -3.3_809,
									        4.3_867,
									    ]      ,device=__UpperCAmelCase      ,dtype=torch.half      ,)

									for i in range(19  ):
														lowerCamelCase__				,						lowerCamelCase__		:   Dict  				=					8 * i // 17, 27_92 * i  # along the diagonal
														lowerCamelCase__				,						lowerCamelCase__		:   Optional[int]  				=					output[0, r, c], expected_diag[i]
														lowerCamelCase__		:   Dict  				=					F"""row={r} col={c} computed={computed} expected={expected}"""
														self.assertAlmostEqual(__UpperCAmelCase      ,__UpperCAmelCase      ,delta=1E-4      ,msg=__UpperCAmelCase  )






