


'''simple docstring'''






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


__UpperCAmelCase					       =							random.Random()


def       _snake_case     (  A				,					A=1.0				,					A=None				,					A=None )		->					List[Any]:
							if rng is None:
														lowerCAmelCase__      =			global_rng

							lowerCAmelCase__      =			[]
							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      ,					lowerCamelCase_      ,					lowerCamelCase_=7      ,					lowerCamelCase_=4_00      ,					lowerCamelCase_=20_00      ,					lowerCamelCase_=1      ,					lowerCamelCase_=0.0      ,					lowerCamelCase_=1_60_00      ,					lowerCamelCase_=True      ,					lowerCamelCase_=80      ,					lowerCamelCase_=16      ,					lowerCamelCase_=64      ,					lowerCamelCase_="hann_window"      ,					lowerCamelCase_=80      ,					lowerCamelCase_=76_00      ,					lowerCamelCase_=1e-10      ,					lowerCamelCase_=True      ,					) ->							List[Any]:
														lowerCAmelCase__      =			parent
														lowerCAmelCase__      =			batch_size
														lowerCAmelCase__      =			min_seq_length
														lowerCAmelCase__      =			max_seq_length
														lowerCAmelCase__      =			(self.max_seq_length - self.min_seq_length) // (self.batch_size - 1)
														lowerCAmelCase__      =			feature_size
														lowerCAmelCase__      =			padding_value
														lowerCAmelCase__      =			sampling_rate
														lowerCAmelCase__      =			do_normalize
														lowerCAmelCase__      =			num_mel_bins
														lowerCAmelCase__      =			hop_length
														lowerCAmelCase__      =			win_length
														lowerCAmelCase__      =			win_function
														lowerCAmelCase__      =			fmin
														lowerCAmelCase__      =			fmax
														lowerCAmelCase__      =			mel_floor
														lowerCAmelCase__      =			return_attention_mask







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Any:
														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 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_=False      ,					lowerCamelCase_=False     ) ->							str:
														def _flatten(lowerCamelCase_     ):
																					return list(itertools.chain(*lowerCamelCase_     )     )

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

														return speech_inputs







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_=False      ,					lowerCamelCase_=False     ) ->							int:
														if equal_length:
																					lowerCAmelCase__      =			[floats_list((self.max_seq_length, self.num_mel_bins)     ) for _ in range(self.batch_size     )]
														else:
																					# make sure that inputs increase in size
																					lowerCAmelCase__      =			[
																					    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:
																					lowerCAmelCase__      =			[np.asarray(lowerCamelCase_     ) for x in speech_inputs]

														return speech_inputs




@require_torch
class 	a__       (							a__       ,       unittest.TestCase						):





							'''simple docstring'''






							lowercase__    :     Tuple					=   SpeechTaFeatureExtractor







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							int:
														lowerCAmelCase__      =			SpeechTaFeatureExtractionTester(self     )







							def 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_     ) ->							Dict:
														self.assertTrue(np.all(np.mean(lowerCamelCase_      ,					axis=0     ) < 1e-3     )     )
														self.assertTrue(np.all(np.abs(np.var(lowerCamelCase_      ,					axis=0     ) - 1     ) < 1e-3     )     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Union[str, Any]:
														# Tests that all call wrap to encode_plus and batch_encode_plus
														lowerCAmelCase__      =			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()     )
														# create three inputs of length 800, 1000, and 1200
														lowerCAmelCase__      =			[floats_list((1, x)     )[0] for x in range(8_00      ,					14_00      ,					2_00     )]
														lowerCAmelCase__      =			[np.asarray(lowerCamelCase_     ) for speech_input in speech_inputs]

														# Test not batched input
														lowerCAmelCase__      =			feat_extract(speech_inputs[0]      ,					return_tensors='''np'''     ).input_values
														lowerCAmelCase__      =			feat_extract(np_speech_inputs[0]      ,					return_tensors='''np'''     ).input_values
														self.assertTrue(np.allclose(lowerCamelCase_      ,					lowerCamelCase_      ,					atol=1e-3     )     )

														# Test batched
														lowerCAmelCase__      =			feat_extract(lowerCamelCase_      ,					return_tensors='''np'''     ).input_values
														lowerCAmelCase__      =			feat_extract(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 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Dict:
														lowerCAmelCase__      =			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()     )
														lowerCAmelCase__      =			[floats_list((1, x)     )[0] for x in range(8_00      ,					14_00      ,					2_00     )]

														lowerCAmelCase__      =			['''longest''', '''max_length''', '''do_not_pad''']
														lowerCAmelCase__      =			[None, 16_00, None]
														for max_length, padding in zip(lowerCamelCase_      ,					lowerCamelCase_     ):
																					lowerCAmelCase__      =			feat_extract(lowerCamelCase_      ,					padding=lowerCamelCase_      ,					max_length=lowerCamelCase_      ,					return_tensors='''np'''     )
																					lowerCAmelCase__      =			processed.input_values

																					self._check_zero_mean_unit_variance(input_values[0][:8_00]     )
																					self.assertTrue(input_values[0][8_00:].sum() < 1e-6     )
																					self._check_zero_mean_unit_variance(input_values[1][:10_00]     )
																					self.assertTrue(input_values[0][10_00:].sum() < 1e-6     )
																					self._check_zero_mean_unit_variance(input_values[2][:12_00]     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Tuple:
														lowerCAmelCase__      =			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()     )
														lowerCAmelCase__      =			range(8_00      ,					14_00      ,					2_00     )
														lowerCAmelCase__      =			[floats_list((1, x)     )[0] for x in lengths]

														lowerCAmelCase__      =			['''longest''', '''max_length''', '''do_not_pad''']
														lowerCAmelCase__      =			[None, 16_00, None]

														for max_length, padding in zip(lowerCamelCase_      ,					lowerCamelCase_     ):
																					lowerCAmelCase__      =			feat_extract(lowerCamelCase_      ,					max_length=lowerCamelCase_      ,					padding=lowerCamelCase_     )
																					lowerCAmelCase__      =			processed.input_values

																					self._check_zero_mean_unit_variance(input_values[0][:8_00]     )
																					self._check_zero_mean_unit_variance(input_values[1][:10_00]     )
																					self._check_zero_mean_unit_variance(input_values[2][:12_00]     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Optional[Any]:
														lowerCAmelCase__      =			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()     )
														lowerCAmelCase__      =			[floats_list((1, x)     )[0] for x in range(8_00      ,					14_00      ,					2_00     )]
														lowerCAmelCase__      =			feat_extract(
														    lowerCamelCase_      ,					truncation=lowerCamelCase_      ,					max_length=10_00      ,					padding='''max_length'''      ,					return_tensors='''np'''     )
														lowerCAmelCase__      =			processed.input_values

														self._check_zero_mean_unit_variance(input_values[0, :8_00]     )
														self._check_zero_mean_unit_variance(input_values[1]     )
														self._check_zero_mean_unit_variance(input_values[2]     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Tuple:
														lowerCAmelCase__      =			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()     )
														lowerCAmelCase__      =			[floats_list((1, x)     )[0] for x in range(8_00      ,					14_00      ,					2_00     )]
														lowerCAmelCase__      =			feat_extract(
														    lowerCamelCase_      ,					truncation=lowerCamelCase_      ,					max_length=10_00      ,					padding='''longest'''      ,					return_tensors='''np'''     )
														lowerCAmelCase__      =			processed.input_values

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

														lowerCAmelCase__      =			[floats_list((1, x)     )[0] for x in range(8_00      ,					14_00      ,					2_00     )]
														lowerCAmelCase__      =			feat_extract(
														    lowerCamelCase_      ,					truncation=lowerCamelCase_      ,					max_length=20_00      ,					padding='''longest'''      ,					return_tensors='''np'''     )
														lowerCAmelCase__      =			processed.input_values

														self._check_zero_mean_unit_variance(input_values[0, :8_00]     )
														self._check_zero_mean_unit_variance(input_values[1, :10_00]     )
														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, 12_00)     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Optional[int]:
														lowerCAmelCase__      =			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()     )
														lowerCAmelCase__      =			np.random.rand(1_00     ).astype(np.floataa     )
														lowerCAmelCase__      =			np_speech_inputs.tolist()

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







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Tuple:
														# Tests that all call wrap to encode_plus and batch_encode_plus
														lowerCAmelCase__      =			self.feature_extraction_class(**self.feat_extract_tester.prepare_feat_extract_dict()     )
														# create three inputs of length 800, 1000, and 1200
														lowerCAmelCase__      =			[floats_list((1, x)     )[0] for x in range(8_00      ,					14_00      ,					2_00     )]
														lowerCAmelCase__      =			[np.asarray(lowerCamelCase_     ) for speech_input in speech_inputs]

														# Test feature size
														lowerCAmelCase__      =			feature_extractor(audio_target=lowerCamelCase_      ,					padding=lowerCamelCase_      ,					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
														lowerCAmelCase__      =			feature_extractor(speech_inputs[0]      ,					return_tensors='''np'''     ).input_values
														lowerCAmelCase__      =			feature_extractor(np_speech_inputs[0]      ,					return_tensors='''np'''     ).input_values
														self.assertTrue(np.allclose(lowerCamelCase_      ,					lowerCamelCase_      ,					atol=1e-3     )     )

														# Test batched
														lowerCAmelCase__      =			feature_extractor(lowerCamelCase_      ,					return_tensors='''np'''     ).input_values
														lowerCAmelCase__      =			feature_extractor(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     )     )

														# Test 2-D numpy arrays are batched.
														lowerCAmelCase__      =			[floats_list((1, x)     )[0] for x in (8_00, 8_00, 8_00)]
														lowerCAmelCase__      =			np.asarray(lowerCamelCase_     )
														lowerCAmelCase__      =			feature_extractor(lowerCamelCase_      ,					return_tensors='''np'''     ).input_values
														lowerCAmelCase__      =			feature_extractor(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 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Dict:
														lowerCAmelCase__      =			self.feat_extract_tester.prepare_inputs_for_target()
														lowerCAmelCase__      =			self.feature_extraction_class(**self.feat_extract_dict     )
														lowerCAmelCase__      =			feat_extract.model_input_names[0]

														lowerCAmelCase__      =			BatchFeature({input_name: speech_inputs}     )

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

														lowerCAmelCase__      =			self.feat_extract_tester.prepare_inputs_for_target(equal_length=lowerCamelCase_     )
														lowerCAmelCase__      =			BatchFeature({input_name: speech_inputs}      ,					tensor_type='''np'''     )

														lowerCAmelCase__      =			processed_features[input_name]

														if len(batch_features_input.shape     ) < 3:
																					lowerCAmelCase__      =			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 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Dict:
														lowerCAmelCase__      =			self.feat_extract_tester.prepare_inputs_for_target(equal_length=lowerCamelCase_     )
														lowerCAmelCase__      =			self.feature_extraction_class(**self.feat_extract_dict     )
														lowerCAmelCase__      =			feat_extract.model_input_names[0]

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

														lowerCAmelCase__      =			processed_features[input_name]

														if len(batch_features_input.shape     ) < 3:
																					lowerCAmelCase__      =			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 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[str]:
														lowerCAmelCase__      =			self.feature_extraction_class(**self.feat_extract_dict     )
														lowerCAmelCase__      =			self.feat_extract_tester.prepare_inputs_for_target()
														lowerCAmelCase__      =			feat_extract.model_input_names[0]

														lowerCAmelCase__      =			BatchFeature({input_name: speech_inputs}     )

														lowerCAmelCase__      =			feat_extract.num_mel_bins  # hack!

														lowerCAmelCase__      =			feat_extract.pad(lowerCamelCase_      ,					padding='''longest'''      ,					return_tensors='''np'''     )[input_name]
														lowerCAmelCase__      =			feat_extract.pad(lowerCamelCase_      ,					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 						__SCREAMING_SNAKE_CASE							(	self     ) ->							List[Any]:
														lowerCAmelCase__      =			self.feat_extract_dict
														lowerCAmelCase__      =			True
														lowerCAmelCase__      =			self.feature_extraction_class(**lowerCamelCase_     )
														lowerCAmelCase__      =			self.feat_extract_tester.prepare_inputs_for_target()
														lowerCAmelCase__      =			[len(lowerCamelCase_     ) for x in speech_inputs]
														lowerCAmelCase__      =			feat_extract.model_input_names[0]

														lowerCAmelCase__      =			BatchFeature({input_name: speech_inputs}     )

														lowerCAmelCase__      =			feat_extract.num_mel_bins  # hack!

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







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Union[str, Any]:
														lowerCAmelCase__      =			self.feat_extract_dict
														lowerCAmelCase__      =			True
														lowerCAmelCase__      =			self.feature_extraction_class(**lowerCamelCase_     )
														lowerCAmelCase__      =			self.feat_extract_tester.prepare_inputs_for_target()
														lowerCAmelCase__      =			[len(lowerCamelCase_     ) for x in speech_inputs]
														lowerCAmelCase__      =			feat_extract.model_input_names[0]

														lowerCAmelCase__      =			BatchFeature({input_name: speech_inputs}     )
														lowerCAmelCase__      =			min(lowerCamelCase_     )

														lowerCAmelCase__      =			feat_extract.num_mel_bins  # hack!

														lowerCAmelCase__      =			feat_extract.pad(
														    lowerCamelCase_      ,					padding='''max_length'''      ,					max_length=lowerCamelCase_      ,					truncation=lowerCamelCase_      ,					return_tensors='''np'''     )
														self.assertIn('''attention_mask'''      ,					lowerCamelCase_     )
														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 						__SCREAMING_SNAKE_CASE							(	self      ,					lowerCamelCase_     ) ->							Optional[Any]:
														from datasets import load_dataset

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

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







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Optional[int]:
														# fmt: off
														lowerCAmelCase__      =			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

														lowerCAmelCase__      =			self._load_datasamples(1     )
														lowerCAmelCase__      =			SpeechTaFeatureExtractor()
														lowerCAmelCase__      =			feature_extractor(lowerCamelCase_      ,					return_tensors='''pt'''     ).input_values
														self.assertEquals(input_values.shape      ,					(1, 9_36_80)     )
														self.assertTrue(torch.allclose(input_values[0, :30]      ,					lowerCamelCase_      ,					atol=1e-6     )     )







							def 						__SCREAMING_SNAKE_CASE							(	self     ) ->							Dict:
														# fmt: off
														lowerCAmelCase__      =			torch.tensor(
														    [-2.6_870, -3.0_104, -3.1_356, -3.5_352, -3.0_044, -3.0_353, -3.4_719, -3.6_777,
														     -3.1_520, -2.9_435, -2.6_553, -2.8_795, -2.9_944, -2.5_921, -3.0_279, -3.0_386,
														     -3.0_864, -3.1_291, -3.2_353, -2.7_444, -2.6_831, -2.7_287, -3.1_761, -3.1_571,
														     -3.2_726, -3.0_582, -3.1_007, -3.4_533, -3.4_695, -3.0_998]     )
														# fmt: on

														lowerCAmelCase__      =			self._load_datasamples(1     )
														lowerCAmelCase__      =			SpeechTaFeatureExtractor()
														lowerCAmelCase__      =			feature_extractor(audio_target=lowerCamelCase_      ,					return_tensors='''pt'''     ).input_values
														self.assertEquals(input_values.shape      ,					(1, 3_66, 80)     )
														self.assertTrue(torch.allclose(input_values[0, 0, :30]      ,					lowerCamelCase_      ,					atol=1e-4     )     )



'''simple docstring'''






import argparse
import json
import os

import fairseq
import torch
from fairseq.data import Dictionary

from transformers import (
    WavaVecaConformerConfig,
    WavaVecaConformerForCTC,
    WavaVecaConformerForPreTraining,
    WavaVecaCTCTokenizer,
    WavaVecaFeatureExtractor,
    WavaVecaProcessor,
    logging,
)


logging.set_verbosity_info()
__UpperCAmelCase					       =							logging.get_logger(__name__)

__UpperCAmelCase					       =							{
    '''post_extract_proj''': '''feature_projection.projection''',
    '''encoder.pos_conv.0''': '''encoder.pos_conv_embed.conv''',
    '''self_attn.linear_k''': '''encoder.layers.*.self_attn.linear_k''',
    '''self_attn.linear_v''': '''encoder.layers.*.self_attn.linear_v''',
    '''self_attn.linear_q''': '''encoder.layers.*.self_attn.linear_q''',
    '''self_attn.pos_bias_u''': '''encoder.layers.*.self_attn.pos_bias_u''',
    '''self_attn.pos_bias_v''': '''encoder.layers.*.self_attn.pos_bias_v''',
    '''self_attn.linear_out''': '''encoder.layers.*.self_attn.linear_out''',
    '''self_attn.linear_pos''': '''encoder.layers.*.self_attn.linear_pos''',
    '''self_attn.rotary_emb''': '''encoder.embed_positions''',
    '''self_attn_layer_norm''': '''encoder.layers.*.self_attn_layer_norm''',
    '''conv_module.pointwise_conv1''': '''encoder.layers.*.conv_module.pointwise_conv1''',
    '''conv_module.pointwise_conv2''': '''encoder.layers.*.conv_module.pointwise_conv2''',
    '''conv_module.depthwise_conv''': '''encoder.layers.*.conv_module.depthwise_conv''',
    '''conv_module.batch_norm''': '''encoder.layers.*.conv_module.batch_norm''',
    '''conv_module.layer_norm''': '''encoder.layers.*.conv_module.layer_norm''',
    '''ffn1.w_1''': '''encoder.layers.*.ffn1.intermediate_dense''',
    '''ffn1.w_2''': '''encoder.layers.*.ffn1.output_dense''',
    '''ffn1.layer_norm''': '''encoder.layers.*.ffn1_layer_norm''',
    '''ffn2.w_1''': '''encoder.layers.*.ffn2.intermediate_dense''',
    '''ffn2.w_2''': '''encoder.layers.*.ffn2.output_dense''',
    '''ffn2.layer_norm''': '''encoder.layers.*.ffn2_layer_norm''',
    '''final_layer_norm''': '''encoder.layers.*.final_layer_norm''',
    '''encoder.layer_norm''': '''encoder.layer_norm''',
    '''w2v_model.layer_norm''': '''feature_projection.layer_norm''',
    '''quantizer.weight_proj''': '''quantizer.weight_proj''',
    '''quantizer.vars''': '''quantizer.codevectors''',
    '''project_q''': '''project_q''',
    '''final_proj''': '''project_hid''',
    '''w2v_encoder.proj''': '''lm_head''',
    '''mask_emb''': '''masked_spec_embed''',
}
__UpperCAmelCase					       =							[
    '''lm_head''',
    '''quantizer.weight_proj''',
    '''quantizer.codevectors''',
    '''project_q''',
    '''project_hid''',
]


def       _snake_case     (  A				,					A				,					A				,					A				,					A )		->					Optional[Any]:
							for attribute in key.split('''.''' ):
														lowerCAmelCase__      =			getattr(A				,					A )

							if weight_type is not None:
														lowerCAmelCase__      =			getattr(A				,					A ).shape
							else:
														lowerCAmelCase__      =			hf_pointer.shape

							if hf_shape != value.shape:
														raise ValueError(
														    F"""Shape of hf {key + "." + weight_type if weight_type is not None else ""} is {hf_shape}, but should be"""
														    F""" {value.shape} for {full_name}""" )

							if weight_type == "weight":
														lowerCAmelCase__      =			value
							elif weight_type == "weight_g":
														lowerCAmelCase__      =			value
							elif weight_type == "weight_v":
														lowerCAmelCase__      =			value
							elif weight_type == "bias":
														lowerCAmelCase__      =			value
							elif weight_type == "running_mean":
														lowerCAmelCase__      =			value
							elif weight_type == "running_var":
														lowerCAmelCase__      =			value
							elif weight_type == "num_batches_tracked":
														lowerCAmelCase__      =			value
							elif weight_type == "inv_freq":
														lowerCAmelCase__      =			value
							else:
														lowerCAmelCase__      =			value

							logger.info(F"""{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.""" )


def       _snake_case     (  A				,					A				,					A )		->					Any:
							lowerCAmelCase__      =			[]
							lowerCAmelCase__      =			fairseq_model.state_dict()

							lowerCAmelCase__      =			hf_model.wavaveca_conformer.feature_extractor

							for name, value in fairseq_dict.items():
														lowerCAmelCase__      =			False
														if "conv_layers" in name:
																					load_conv_layer(
																					    A				,					A				,					A				,					A				,					hf_model.config.feat_extract_norm == '''group'''				,					)
																					lowerCAmelCase__      =			True
														else:
																					for key, mapped_key in MAPPING.items():
																												lowerCAmelCase__      =			'''wav2vec2_conformer.''' + mapped_key if mapped_key not in TOP_LEVEL_KEYS else mapped_key
																												if key in name or key.split('''w2v_model.''' )[-1] == name.split('''.''' )[0]:
																																			lowerCAmelCase__      =			True
																																			if "*" in mapped_key:
																																										lowerCAmelCase__      =			name.split(A )[0].split('''.''' )[-2]
																																										lowerCAmelCase__      =			mapped_key.replace('''*'''				,					A )
																																			if "pos_bias_u" in name:
																																										lowerCAmelCase__      =			None
																																			elif "pos_bias_v" in name:
																																										lowerCAmelCase__      =			None
																																			elif "weight_g" in name:
																																										lowerCAmelCase__      =			'''weight_g'''
																																			elif "weight_v" in name:
																																										lowerCAmelCase__      =			'''weight_v'''
																																			elif "bias" in name:
																																										lowerCAmelCase__      =			'''bias'''
																																			elif "weight" in name:
																																										# TODO: don't match quantizer.weight_proj
																																										lowerCAmelCase__      =			'''weight'''
																																			elif "running_mean" in name:
																																										lowerCAmelCase__      =			'''running_mean'''
																																			elif "inv_freq" in name:
																																										lowerCAmelCase__      =			'''inv_freq'''
																																			elif "running_var" in name:
																																										lowerCAmelCase__      =			'''running_var'''
																																			elif "num_batches_tracked" in name:
																																										lowerCAmelCase__      =			'''num_batches_tracked'''
																																			else:
																																										lowerCAmelCase__      =			None
																																			set_recursively(A				,					A				,					A				,					A				,					A )
																												continue
														if not is_used:
																					unused_weights.append(A )

							logger.warning(F"""Unused weights: {unused_weights}""" )


def       _snake_case     (  A				,					A				,					A				,					A				,					A )		->					Tuple:
							lowerCAmelCase__      =			full_name.split('''conv_layers.''' )[-1]
							lowerCAmelCase__      =			name.split('''.''' )
							lowerCAmelCase__      =			int(items[0] )
							lowerCAmelCase__      =			int(items[1] )

							if type_id == 0:
														if "bias" in name:
																					if value.shape != feature_extractor.conv_layers[layer_id].conv.bias.data.shape:
																												raise ValueError(
																												    F"""{full_name} has size {value.shape}, but"""
																												    F""" {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.""" )
																					lowerCAmelCase__      =			value
																					logger.info(F"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" )
														elif "weight" in name:
																					if value.shape != feature_extractor.conv_layers[layer_id].conv.weight.data.shape:
																												raise ValueError(
																												    F"""{full_name} has size {value.shape}, but"""
																												    F""" {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.""" )
																					lowerCAmelCase__      =			value
																					logger.info(F"""Feat extract conv layer {layer_id} was initialized from {full_name}.""" )
							elif (type_id == 2 and not use_group_norm) or (type_id == 2 and layer_id == 0 and use_group_norm):
														if "bias" in name:
																					if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape:
																												raise ValueError(
																												    F"""{full_name} has size {value.shape}, but"""
																												    F""" {feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape} was found.""" )
																					lowerCAmelCase__      =			value
																					logger.info(F"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" )
														elif "weight" in name:
																					if value.shape != feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape:
																												raise ValueError(
																												    F"""{full_name} has size {value.shape}, but"""
																												    F""" {feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape} was found.""" )
																					lowerCAmelCase__      =			value
																					logger.info(F"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.""" )
							else:
														unused_weights.append(A )


@torch.no_grad()
def       _snake_case     (  A				,					A				,					A=None				,					A=None				,					A=True )		->					Optional[int]:
							if config_path is not None:
														lowerCAmelCase__      =			WavaVecaConformerConfig.from_pretrained(A				,					hidden_act='''swish''' )
							else:
														lowerCAmelCase__      =			WavaVecaConformerConfig()

							if "rope" in checkpoint_path:
														lowerCAmelCase__      =			'''rotary'''

							if is_finetuned:
														if dict_path:
																					lowerCAmelCase__      =			Dictionary.load(A )

																					# important change bos & pad token id since CTC symbol is <pad> and
																					# not <s> as in fairseq
																					lowerCAmelCase__      =			target_dict.pad_index
																					lowerCAmelCase__      =			target_dict.bos_index
																					lowerCAmelCase__      =			target_dict.eos_index
																					lowerCAmelCase__      =			len(target_dict.symbols )
																					lowerCAmelCase__      =			os.path.join(A				,					'''vocab.json''' )
																					if not os.path.isdir(A ):
																												logger.error('''--pytorch_dump_folder_path ({}) should be a directory'''.format(A ) )
																												return
																					os.makedirs(A				,					exist_ok=A )
																					lowerCAmelCase__      =			target_dict.indices

																					# fairseq has the <pad> and <s> switched
																					lowerCAmelCase__      =			0
																					lowerCAmelCase__      =			1
																					with open(A				,					'''w'''				,					encoding='''utf-8''' ) as vocab_handle:
																												json.dump(A				,					A )
																					lowerCAmelCase__      =			WavaVecaCTCTokenizer(
																					    A				,					unk_token=target_dict.unk_word				,					pad_token=target_dict.pad_word				,					bos_token=target_dict.bos_word				,					eos_token=target_dict.eos_word				,					word_delimiter_token='''|'''				,					do_lower_case=A				,					)
																					lowerCAmelCase__      =			True if config.feat_extract_norm == '''layer''' else False
																					lowerCAmelCase__      =			WavaVecaFeatureExtractor(
																					    feature_size=1				,					sampling_rate=16000				,					padding_value=0				,					do_normalize=A				,					return_attention_mask=A				,					)
																					lowerCAmelCase__      =			WavaVecaProcessor(feature_extractor=A				,					tokenizer=A )
																					processor.save_pretrained(A )

														lowerCAmelCase__      =			WavaVecaConformerForCTC(A )
							else:
														lowerCAmelCase__      =			WavaVecaConformerForPreTraining(A )

							if is_finetuned:
														lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__      =			fairseq.checkpoint_utils.load_model_ensemble_and_task(
														    [checkpoint_path]				,					arg_overrides={'''data''': '''/'''.join(dict_path.split('''/''' )[:-1] )} )
							else:
														lowerCAmelCase__      =			argparse.Namespace(task='''audio_pretraining''' )
														lowerCAmelCase__      =			fairseq.tasks.setup_task(A )

														lowerCAmelCase__     ,      lowerCAmelCase__     ,      lowerCAmelCase__      =			fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path]				,					task=A )

							lowerCAmelCase__      =			model[0].eval()

							recursively_load_weights(A				,					A				,					not is_finetuned )

							hf_wavavec.save_pretrained(A )


if __name__ == "__main__":
		__UpperCAmelCase					       =							argparse.ArgumentParser()
		parser.add_argument('''--pytorch_dump_folder_path''', default=None, type=str, help='''Path to the output PyTorch model.''')
		parser.add_argument('''--checkpoint_path''', default=None, type=str, help='''Path to fairseq checkpoint''')
		parser.add_argument('''--dict_path''', default=None, type=str, help='''Path to dict of fine-tuned model''')
		parser.add_argument('''--config_path''', default=None, type=str, help='''Path to hf config.json of model to convert''')
		parser.add_argument(
		    '''--not_finetuned''', action='''store_true''', help='''Whether the model to convert is a fine-tuned model or not'''
		)
		__UpperCAmelCase					       =							parser.parse_args()
		convert_wavaveca_conformer_checkpoint(
		    args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned
		)