





'''simple docstring'''







import torch
from torch import nn
class 							__SCREAMING_SNAKE_CASE    (nn.Module ):






				"""simple docstring"""







				def __init__(    self	:   int   ,							__a	:   Optional[int]   ,							__a	:   Dict   ,							__a	:   Dict   ,							__a	:   List[Any]   ,							__a	:   Union[str, Any]=1   ,							__a	:   int=False   ):
								super().__init__()

								_a				      =					n_token
								_a				      =					d_embed
								_a				      =					d_proj

								_a				      =					cutoffs + [n_token]
								_a				      =					[0] + self.cutoffs
								_a				      =					div_val

								_a				      =					self.cutoffs[0]
								_a				      =					len(self.cutoffs   ) - 1
								_a				      =					self.shortlist_size + self.n_clusters

								if self.n_clusters > 0:
												_a				      =					nn.Parameter(torch.zeros(self.n_clusters   ,							self.d_embed   )   )
												_a				      =					nn.Parameter(torch.zeros(self.n_clusters   )   )

								_a				      =					nn.ModuleList()
								_a				      =					nn.ParameterList()

								if div_val == 1:
												for i in range(len(self.cutoffs   )   ):
																if d_proj != d_embed:
																				self.out_projs.append(nn.Parameter(torch.FloatTensor(a_   ,							a_   )   )   )
																else:
																				self.out_projs.append(a_   )

												self.out_layers.append(nn.Linear(a_   ,							a_   )   )
								else:
												for i in range(len(self.cutoffs   )   ):
																_a				      =					self.cutoff_ends[i], self.cutoff_ends[i + 1]
																_a				      =					d_embed // (div_val**i)

																self.out_projs.append(nn.Parameter(torch.FloatTensor(a_   ,							a_   )   )   )

																self.out_layers.append(nn.Linear(a_   ,							r_idx - l_idx   )   )

								_a				      =					keep_order




				def     UpperCamelCase__  (    self	:   Optional[Any]   ,							__a	:   List[Any]   ,							__a	:   Optional[Any]   ,							__a	:   List[Any]   ,							__a	:   Dict   ):
								if proj is None:
												_a				      =					nn.functional.linear(a_   ,							a_   ,							bias=a_   )
								else:
												# if CUDA_MAJOR <= 9 and CUDA_MINOR <= 1:
												_a				      =					nn.functional.linear(a_   ,							proj.t().contiguous()   )
												_a				      =					nn.functional.linear(a_   ,							a_   ,							bias=a_   )
												# else:
												#     logit = torch.einsum('bd,de,ev->bv', (hidden, proj, weight.t()))
												#     if bias is not None:
												#         logit = logit + bias

								return logit




				def     UpperCamelCase__  (    self	:   int   ,							__a	:   Any   ,							__a	:   List[str]=None   ,							__a	:   Optional[Any]=False   ):

								if labels is not None:
												# Shift so that tokens < n predict n
												_a				      =					hidden[..., :-1, :].contiguous()
												_a				      =					labels[..., 1:].contiguous()
												_a				      =					hidden.view(-1   ,							hidden.size(-1   )   )
												_a				      =					labels.view(-1   )
												if hidden.size(0   ) != labels.size(0   ):
																raise RuntimeError("Input and labels should have the same size in the batch dimension."   )
								else:
												_a				      =					hidden.view(-1   ,							hidden.size(-1   )   )

								if self.n_clusters == 0:
												_a				      =					self._compute_logit(a_   ,							self.out_layers[0].weight   ,							self.out_layers[0].bias   ,							self.out_projs[0]   )
												if labels is not None:
																_a				      =					labels != -1_00
																_a				      =					torch.zeros_like(a_   ,							dtype=hidden.dtype   ,							device=hidden.device   )
																_a				      =					(
																    -nn.functional.log_softmax(a_   ,							dim=-1   )[mask].gather(1   ,							labels[mask].unsqueeze(1   )   ).squeeze(1   )
																)
												else:
																_a				      =					nn.functional.log_softmax(a_   ,							dim=-1   )
								else:
												# construct weights and biases
												_a				      =					[], []
												for i in range(len(self.cutoffs   )   ):
																if self.div_val == 1:
																				_a				      =					self.cutoff_ends[i], self.cutoff_ends[i + 1]
																				_a				      =					self.out_layers[0].weight[l_idx:r_idx]
																				_a				      =					self.out_layers[0].bias[l_idx:r_idx]
																else:
																				_a				      =					self.out_layers[i].weight
																				_a				      =					self.out_layers[i].bias

																if i == 0:
																				_a				      =					torch.cat([weight_i, self.cluster_weight]   ,							dim=0   )
																				_a				      =					torch.cat([bias_i, self.cluster_bias]   ,							dim=0   )

																weights.append(a_   )
																biases.append(a_   )

												_a				      =					weights[0], biases[0], self.out_projs[0]

												_a				      =					self._compute_logit(a_   ,							a_   ,							a_   ,							a_   )
												_a				      =					nn.functional.log_softmax(a_   ,							dim=1   )

												if labels is None:
																_a				      =					hidden.new_empty((head_logit.size(0   ), self.n_token)   )
												else:
																_a				      =					torch.zeros_like(a_   ,							dtype=hidden.dtype   ,							device=hidden.device   )

												_a				      =					0
												_a				      =					[0] + self.cutoffs
												for i in range(len(a_   ) - 1   ):
																_a				      =					cutoff_values[i], cutoff_values[i + 1]

																if labels is not None:
																				_a				      =					(labels >= l_idx) & (labels < r_idx)
																				_a				      =					mask_i.nonzero().squeeze()

																				if indices_i.numel() == 0:
																								continue

																				_a				      =					labels.index_select(0   ,							a_   ) - l_idx
																				_a				      =					head_logprob.index_select(0   ,							a_   )
																				_a				      =					hidden.index_select(0   ,							a_   )
																else:
																				_a				      =					hidden

																if i == 0:
																				if labels is not None:
																								_a				      =					head_logprob_i.gather(1   ,							target_i[:, None]   ).squeeze(1   )
																				else:
																								_a				      =					head_logprob[:, : self.cutoffs[0]]
																else:
																				_a				      =					weights[i], biases[i], self.out_projs[i]

																				_a				      =					self._compute_logit(a_   ,							a_   ,							a_   ,							a_   )
																				_a				      =					nn.functional.log_softmax(a_   ,							dim=1   )
																				_a				      =					self.cutoffs[0] + i - 1  # No probability for the head cluster
																				if labels is not None:
																								_a				      =					head_logprob_i[:, cluster_prob_idx] + tail_logprob_i.gather(
																								    1   ,							target_i[:, None]   ).squeeze(1   )
																				else:
																								_a				      =					head_logprob[:, cluster_prob_idx, None] + tail_logprob_i
																								_a				      =					logprob_i

																if labels is not None:
																				if (hasattr(self   ,							"keep_order"   ) and self.keep_order) or keep_order:
																								out.index_copy_(0   ,							a_   ,							-logprob_i   )
																				else:
																								out[offset : offset + logprob_i.size(0   )].copy_(-logprob_i   )
																				offset += logprob_i.size(0   )

								return out




				def     UpperCamelCase__  (    self	:   Optional[int]   ,							__a	:   Dict   ):
								if self.n_clusters == 0:
												_a				      =					self._compute_logit(a_   ,							self.out_layers[0].weight   ,							self.out_layers[0].bias   ,							self.out_projs[0]   )
												return nn.functional.log_softmax(a_   ,							dim=-1   )
								else:
												# construct weights and biases
												_a				      =					[], []
												for i in range(len(self.cutoffs   )   ):
																if self.div_val == 1:
																				_a				      =					self.cutoff_ends[i], self.cutoff_ends[i + 1]
																				_a				      =					self.out_layers[0].weight[l_idx:r_idx]
																				_a				      =					self.out_layers[0].bias[l_idx:r_idx]
																else:
																				_a				      =					self.out_layers[i].weight
																				_a				      =					self.out_layers[i].bias

																if i == 0:
																				_a				      =					torch.cat([weight_i, self.cluster_weight]   ,							dim=0   )
																				_a				      =					torch.cat([bias_i, self.cluster_bias]   ,							dim=0   )

																weights.append(a_   )
																biases.append(a_   )

												_a				      =					weights[0], biases[0], self.out_projs[0]
												_a				      =					self._compute_logit(a_   ,							a_   ,							a_   ,							a_   )

												_a				      =					hidden.new_empty((head_logit.size(0   ), self.n_token)   )
												_a				      =					nn.functional.log_softmax(a_   ,							dim=1   )

												_a				      =					[0] + self.cutoffs
												for i in range(len(a_   ) - 1   ):
																_a				      =					cutoff_values[i], cutoff_values[i + 1]

																if i == 0:
																				_a				      =					head_logprob[:, : self.cutoffs[0]]
																else:
																				_a				      =					weights[i], biases[i], self.out_projs[i]

																				_a				      =					self._compute_logit(a_   ,							a_   ,							a_   ,							a_   )
																				_a				      =					nn.functional.log_softmax(a_   ,							dim=1   )

																				_a				      =					head_logprob[:, -i] + tail_logprob_i
																				_a				      =					logprob_i

												return out





'''simple docstring'''

import itertools
import os
import random
import tempfile
import unittest

import numpy as np

from transformers import TvltFeatureExtractor, is_datasets_available
from transformers.testing_utils import check_json_file_has_correct_format, require_torch, require_torchaudio
from transformers.utils.import_utils import is_torch_available

from ...test_sequence_feature_extraction_common import SequenceFeatureExtractionTestMixin


if is_torch_available():
	import torch

if is_datasets_available():
	from datasets import load_dataset

a							   =				random.Random()







def a_     (    __UpperCAmelCase				,							__UpperCAmelCase=1.0				,							__UpperCAmelCase=None				,							__UpperCAmelCase=None					)				->					List[str]:





			"""simple docstring"""







			if rng is None:
						snake_case:    Dict      							=global_rng

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

			return values






class 						a_		(     unittest.TestCase    ):


							def __init__(				self   :						int      ,						a_   :						Any      ,						a_   :						str=7      ,						a_   :						Tuple=4_0_0      ,						a_   :						List[Any]=2_0_0_0      ,						a_   :						str=2_0_4_8      ,						a_   :						List[str]=1_2_8      ,						a_   :						int=1      ,						a_   :						Tuple=5_1_2      ,						a_   :						Dict=3_0      ,						a_   :						Optional[int]=4_4_1_0_0      ,						)			->					Union[str, Any]:
										snake_case:    Union[str, Any]      							=parent
										snake_case:    Optional[Any]      							=batch_size
										snake_case:    Union[str, Any]      							=min_seq_length
										snake_case:    List[str]      							=max_seq_length
										snake_case:    str      							=(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
										snake_case:    int      							=spectrogram_length
										snake_case:    List[str]      							=feature_size
										snake_case:    Dict      							=num_audio_channels
										snake_case:    int      							=hop_length
										snake_case:    List[Any]      							=chunk_length
										snake_case:    Optional[Any]      							=sampling_rate


							def    UpperCamelCase     (				self   :						Any			)			->					Dict:
										return {
										    "spectrogram_length": self.spectrogram_length,
										    "feature_size": self.feature_size,
										    "num_audio_channels": self.num_audio_channels,
										    "hop_length": self.hop_length,
										    "chunk_length": self.chunk_length,
										    "sampling_rate": self.sampling_rate,
										}




							def    UpperCamelCase     (				self   :						Tuple      ,						a_   :						Optional[Any]=False      ,						a_   :						Tuple=False			)			->					Optional[int]:
										def _flatten(a_   :						Dict			):
													return list(itertools.chain(*a_			)			)

										if equal_length:
													snake_case:    Union[str, Any]      							=[floats_list((self.max_seq_length, self.feature_size)			) for _ in range(self.batch_size			)]
										else:
													# make sure that inputs increase in size
													snake_case:    Union[str, Any]      							=[
													    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:    Any      							=[np.asarray(a_			) for x in speech_inputs]
										return speech_inputs






@require_torch
@require_torchaudio
class 						a_		(     snake_case    ,   unittest.TestCase    ):
							UpperCAmelCase			:		Optional[Any]        =				TvltFeatureExtractor


							def    UpperCamelCase     (				self   :						int			)			->					int:
										snake_case:    Union[str, Any]      							=TvltFeatureExtractionTester(self			)


							def    UpperCamelCase     (				self   :						Optional[Any]			)			->					str:
										snake_case:    int      							=self.feature_extraction_class(**self.feat_extract_dict			)
										self.assertTrue(hasattr(a_      ,						'spectrogram_length'			)			)
										self.assertTrue(hasattr(a_      ,						'feature_size'			)			)
										self.assertTrue(hasattr(a_      ,						'num_audio_channels'			)			)
										self.assertTrue(hasattr(a_      ,						'hop_length'			)			)
										self.assertTrue(hasattr(a_      ,						'chunk_length'			)			)
										self.assertTrue(hasattr(a_      ,						'sampling_rate'			)			)


							def    UpperCamelCase     (				self   :						Tuple			)			->					Optional[int]:
										snake_case:    Tuple      							=self.feature_extraction_class(**self.feat_extract_dict			)

										with tempfile.TemporaryDirectory() as tmpdirname:
													snake_case:    Optional[Any]      							=feat_extract_first.save_pretrained(a_			)[0]
													check_json_file_has_correct_format(a_			)
													snake_case:    str      							=self.feature_extraction_class.from_pretrained(a_			)

										snake_case:    Optional[int]      							=feat_extract_first.to_dict()
										snake_case:    List[str]      							=feat_extract_second.to_dict()
										snake_case:    Optional[int]      							=dict_first.pop('mel_filters'			)
										snake_case:    List[Any]      							=dict_second.pop('mel_filters'			)
										self.assertTrue(np.allclose(a_      ,						a_			)			)
										self.assertEqual(a_      ,						a_			)


							def    UpperCamelCase     (				self   :						str			)			->					str:
										snake_case:    Any      							=self.feature_extraction_class(**self.feat_extract_dict			)

										with tempfile.TemporaryDirectory() as tmpdirname:
													snake_case:    int      							=os.path.join(a_      ,						'feat_extract.json'			)
													feat_extract_first.to_json_file(a_			)
													snake_case:    Union[str, Any]      							=self.feature_extraction_class.from_json_file(a_			)

										snake_case:    Optional[Any]      							=feat_extract_first.to_dict()
										snake_case:    Optional[Any]      							=feat_extract_second.to_dict()
										snake_case:    Union[str, Any]      							=dict_first.pop('mel_filters'			)
										snake_case:    Union[str, Any]      							=dict_second.pop('mel_filters'			)
										self.assertTrue(np.allclose(a_      ,						a_			)			)
										self.assertEqual(a_      ,						a_			)


							def    UpperCamelCase     (				self   :						Union[str, Any]			)			->					List[str]:
										# Initialize feature_extractor
										snake_case:    Tuple      							=self.feature_extraction_class(**self.feat_extract_dict			)

										# create three inputs of length 800, 1000, and 1200
										snake_case:    Any      							=[floats_list((1, x)			)[0] for x in range(8_0_0      ,						1_4_0_0      ,						2_0_0			)]
										snake_case:    Optional[Any]      							=[np.asarray(a_			) for speech_input in speech_inputs]

										# Test not batched input
										snake_case:    List[Any]      							=feature_extractor(np_speech_inputs[0]      ,						return_tensors='np'      ,						sampling_rate=4_4_1_0_0			).audio_values

										self.assertTrue(encoded_audios.ndim == 4			)
										self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size			)
										self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length			)
										self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels			)

										# Test batched
										snake_case:    Any      							=feature_extractor(a_      ,						return_tensors='np'      ,						sampling_rate=4_4_1_0_0			).audio_values

										self.assertTrue(encoded_audios.ndim == 4			)
										self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size			)
										self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length			)
										self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels			)

										# Test audio masking
										snake_case:    Optional[Any]      							=feature_extractor(
										    a_      ,						return_tensors='np'      ,						sampling_rate=4_4_1_0_0      ,						mask_audio=a_			).audio_values

										self.assertTrue(encoded_audios.ndim == 4			)
										self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size			)
										self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length			)
										self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels			)

										# Test 2-D numpy arrays are batched.
										snake_case:    List[Any]      							=[floats_list((1, x)			)[0] for x in (8_0_0, 8_0_0, 8_0_0)]
										snake_case:    List[str]      							=np.asarray(a_			)
										snake_case:    str      							=feature_extractor(a_      ,						return_tensors='np'      ,						sampling_rate=4_4_1_0_0			).audio_values
										self.assertTrue(encoded_audios.ndim == 4			)
										self.assertTrue(encoded_audios.shape[-1] == feature_extractor.feature_size			)
										self.assertTrue(encoded_audios.shape[-2] <= feature_extractor.spectrogram_length			)
										self.assertTrue(encoded_audios.shape[-3] == feature_extractor.num_channels			)


							def    UpperCamelCase     (				self   :						Any      ,						a_   :						str			)			->					Union[str, Any]:
										snake_case:    Dict      							=load_dataset('hf-internal-testing/librispeech_asr_dummy'      ,						'clean'      ,						split='validation'			)
										# automatic decoding with librispeech
										snake_case:    Optional[int]      							=ds.sort('id'			).select(range(a_			)			)[:num_samples]['audio']

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




							def    UpperCamelCase     (				self   :						Optional[Any]			)			->					Optional[int]:
										snake_case:    Tuple      							=self._load_datasamples(1			)
										snake_case:    Any      							=TvltFeatureExtractor()
										snake_case:    Any      							=feature_extractor(a_      ,						return_tensors='pt'			).audio_values

										self.assertEquals(audio_values.shape      ,						(1, 1, 1_9_2, 1_2_8)			)

										snake_case:    int      							=torch.tensor([[-0.3_0_3_2, -0.2_7_0_8], [-0.4_4_3_4, -0.4_0_0_7]]			)
										self.assertTrue(torch.allclose(audio_values[0, 0, :2, :2]      ,						a_      ,						atol=1E-4			)			)




