code: 
  
  
  
  """simple docstring"""
  import argparse
  import os
  from functools import reduce
  
  import fairseq
  import torch
  from datasets import load_dataset
  
  from transformers import WavaVecaProcessor, logging
  from transformers.models.dataavec.configuration_dataavec_audio import DataaVecAudioConfig
  
  # Copied from https://github.com/pytorch/fairseq/blob/main/examples/data2vec/models/data2vec_audio.py
  from transformers.models.dataavec.dataavec_audio import DataaVecAudioModel as Dummy  # noqa: F401
  from transformers.models.dataavec.modeling_dataavec_audio import DataaVecAudioForCTC, DataaVecAudioModel
  
  
  logging.set_verbosity_info()
  _lowerCAmelCase					:int  =      logging.get_logger(__name__)
  
  _lowerCAmelCase					:Union[str, Any]  =      {
      'post_extract_proj': 'feature_projection.projection',
      'models.0.layer_norm': 'feature_projection.layer_norm',
      'self_attn.k_proj': 'encoder.layers.*.attention.k_proj',
      'self_attn.v_proj': 'encoder.layers.*.attention.v_proj',
      'self_attn.q_proj': 'encoder.layers.*.attention.q_proj',
      'self_attn.out_proj': 'encoder.layers.*.attention.out_proj',
      'self_attn_layer_norm': 'encoder.layers.*.layer_norm',
      'fc1': 'encoder.layers.*.feed_forward.intermediate_dense',
      'fc2': 'encoder.layers.*.feed_forward.output_dense',
      'final_layer_norm': 'encoder.layers.*.final_layer_norm',
      'encoder.layer_norm': 'encoder.layer_norm',
      'w2v_model.layer_norm': 'feature_projection.layer_norm',
      'w2v_encoder.proj': 'lm_head',
      'mask_emb': 'masked_spec_embed',
  }
  _lowerCAmelCase					:Dict  =      [
      'lm_head',
  ]
  
  
  
  
  
  def 				lowerCamelCase_		(UpperCamelCase__	:	Tuple  , UpperCamelCase__	:	Tuple  , UpperCamelCase__	:	Dict  , UpperCamelCase__	:	Tuple  , UpperCamelCase__	:	Optional[Any]							):
      for attribute in key.split('''.'''							):
          _UpperCAmelCase							:						List[Any]       							=					getattr(UpperCamelCase__  , UpperCamelCase__							)
  
      if weight_type is not None:
          _UpperCAmelCase							:						Union[str, Any]       							=					getattr(UpperCamelCase__  , UpperCamelCase__							).shape
      else:
          _UpperCAmelCase							:						int       							=					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":
          _UpperCAmelCase							:						str       							=					value
      elif weight_type == "weight_g":
          _UpperCAmelCase							:						Tuple       							=					value
      elif weight_type == "weight_v":
          _UpperCAmelCase							:						Optional[Any]       							=					value
      elif weight_type == "bias":
          _UpperCAmelCase							:						List[Any]       							=					value
      else:
          _UpperCAmelCase							:						str       							=					value
  
      logger.info(F'{key + "." + weight_type if weight_type is not None else ""} was initialized from {full_name}.'							)
  
  
  
  
  
  def 				lowerCamelCase_		(UpperCamelCase__	:	Tuple  , UpperCamelCase__	:	Dict  , UpperCamelCase__	:	Dict							):
      _UpperCAmelCase							:						Any       							=					[]
      _UpperCAmelCase							:						Any       							=					fairseq_model.state_dict()
  
      if not is_headless:
          _UpperCAmelCase							:						Tuple       							=					hf_model.dataavec_audio.feature_extractor
          _UpperCAmelCase							:						Tuple       							=					hf_model.dataavec_audio.encoder.pos_conv_embed
  
      else:
          _UpperCAmelCase							:						Dict       							=					hf_model.feature_extractor
          _UpperCAmelCase							:						Optional[int]       							=					hf_model.encoder.pos_conv_embed
  
      for name, value in fairseq_dict.items():
          _UpperCAmelCase							:						Union[str, Any]       							=					False
          if "conv_layers" in name:
              load_conv_layer(
                  UpperCamelCase__  , UpperCamelCase__  , UpperCamelCase__  , UpperCamelCase__  , )
              _UpperCAmelCase							:						Dict       							=					True
          elif "pos_conv" in name:
              load_pos_conv_layer(
                  UpperCamelCase__  , UpperCamelCase__  , UpperCamelCase__  , UpperCamelCase__  , )
              _UpperCAmelCase							:						int       							=					True
          else:
              for key, mapped_key in MAPPING.items():
                  if not is_headless:
                      _UpperCAmelCase							:						List[str]       							=					'''data2vec_audio.''' + 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]:
                      _UpperCAmelCase							:						Tuple       							=					True
                      if "*" in mapped_key:
                          _UpperCAmelCase							:						Union[str, Any]       							=					name.split(UpperCamelCase__							)[0].split('''.'''							)[-2]
                          _UpperCAmelCase							:						str       							=					mapped_key.replace('''*'''  , UpperCamelCase__							)
                      if "weight_g" in name:
                          _UpperCAmelCase							:						List[Any]       							=					'''weight_g'''
                      elif "weight_v" in name:
                          _UpperCAmelCase							:						Tuple       							=					'''weight_v'''
                      elif "bias" in name:
                          _UpperCAmelCase							:						List[str]       							=					'''bias'''
                      elif "weight" in name:
                          # TODO: don't match quantizer.weight_proj
                          _UpperCAmelCase							:						Optional[Any]       							=					'''weight'''
                      else:
                          _UpperCAmelCase							:						str       							=					None
                      set_recursively(UpperCamelCase__  , UpperCamelCase__  , UpperCamelCase__  , UpperCamelCase__  , UpperCamelCase__							)
                  continue
          if not is_used:
              unused_weights.append(UpperCamelCase__							)
  
      logger.warning(F'Unused weights: {unused_weights}'							)
  
  
  
  
  
  def 				lowerCamelCase_		(UpperCamelCase__	:	List[str]  , UpperCamelCase__	:	List[Any]							):
      _UpperCAmelCase							:						Dict       							=					path.split('''.'''							)
      return reduce(UpperCamelCase__  , UpperCamelCase__  , UpperCamelCase__							)
  
  
  
  
  
  def 				lowerCamelCase_		(UpperCamelCase__	:	Tuple  , UpperCamelCase__	:	List[str]  , UpperCamelCase__	:	Tuple  , UpperCamelCase__	:	List[str]							):
      _UpperCAmelCase							:						Tuple       							=					access_by_string(UpperCamelCase__  , UpperCamelCase__							)
      _UpperCAmelCase							:						str       							=					hf_weight.data
  
      if fsq_value.shape != hf_value.shape:
          raise ValueError(F'{full_name} has size {fsq_value.shape}, but {hf_value.shape} was found.'							)
      _UpperCAmelCase							:						Tuple       							=					fsq_value
      logger.info(F'{full_name} was correctly initialized from {hf_weight_path}.'							)
  
  
  
  
  
  def 				lowerCamelCase_		(UpperCamelCase__	:	Union[str, Any]  , UpperCamelCase__	:	Optional[Any]  , UpperCamelCase__	:	Optional[Any]  , UpperCamelCase__	:	int							):
      _UpperCAmelCase							:						Tuple       							=					full_name.split('''conv_layers.'''							)[-1]
      _UpperCAmelCase							:						int       							=					name.split('''.'''							)
      _UpperCAmelCase							:						Dict       							=					int(items[0]							)
      _UpperCAmelCase							:						Any       							=					int(items[1]							)
  
      _UpperCAmelCase							:						Optional[int]       							=					name.split('''.'''							)[-1]
      if type_id == 0:
          _UpperCAmelCase							:						Optional[int]       							=					'''conv'''
      elif type_id == 2:
          _UpperCAmelCase							:						Dict       							=					'''layer_norm'''
      else:
          unused_weights.append(UpperCamelCase__							)
          return
  
      set_weights(UpperCamelCase__  , UpperCamelCase__  , UpperCamelCase__  , F'conv_layers.{layer_id}.{layer_type}.{weight_type}'							)
  
  
  
  
  
  def 				lowerCamelCase_		(UpperCamelCase__	:	Optional[int]  , UpperCamelCase__	:	Tuple  , UpperCamelCase__	:	int  , UpperCamelCase__	:	Union[str, Any]							):
      _UpperCAmelCase							:						int       							=					full_name.split('''pos_conv.'''							)[-1]
      _UpperCAmelCase							:						int       							=					name.split('''.'''							)
      _UpperCAmelCase							:						Dict       							=					int(items[0]							)
      _UpperCAmelCase							:						Union[str, Any]       							=					int(items[1]							)
  
      _UpperCAmelCase							:						List[str]       							=					name.split('''.'''							)[-1]
      if type_id != 0:
          unused_weights.append(UpperCamelCase__							)
          return
      else:
          _UpperCAmelCase							:						Tuple       							=					'''conv'''
  
      set_weights(UpperCamelCase__  , UpperCamelCase__  , UpperCamelCase__  , F'layers.{layer_id}.{layer_type}.{weight_type}'							)
  
  
  
  
  
  @torch.no_grad()
  def 				lowerCamelCase_		(UpperCamelCase__	:	Optional[int]  , UpperCamelCase__	:	List[str]  , UpperCamelCase__	:	Tuple=None  , UpperCamelCase__	:	Union[str, Any]=None  , UpperCamelCase__	:	str=True							):
      if config_path is not None:
          _UpperCAmelCase							:						str       							=					DataaVecAudioConfig.from_pretrained(UpperCamelCase__							)
      else:
          _UpperCAmelCase							:						List[str]       							=					DataaVecAudioConfig()
  
      if not is_finetuned:
          # Modify final_proj layer name
          _UpperCAmelCase							:						Any       							=					DataaVecAudioModel(UpperCamelCase__							)
          _UpperCAmelCase							:						Optional[Any]       							=					os.path.dirname(UpperCamelCase__							)
  
          _UpperCAmelCase							:						Optional[int]       							=					torch.load(UpperCamelCase__							)
          _UpperCAmelCase							:						Optional[int]       							=					state_dict['''model'''].pop('''final_proj.0.weight'''							)
          _UpperCAmelCase							:						Union[str, Any]       							=					state_dict['''model'''].pop('''final_proj.0.bias'''							)
          _UpperCAmelCase							:						str       							=					os.path.join(UpperCamelCase__  , '''converted.pt'''							)
          torch.save(UpperCamelCase__  , UpperCamelCase__							)
      else:
          _UpperCAmelCase							:						Any       							=					DataaVecAudioForCTC(UpperCamelCase__							)
          _UpperCAmelCase							:						Any       							=					checkpoint_path
  
      def load_dataavec(UpperCamelCase__	:	Optional[Any]							):
          _UpperCAmelCase							,       _UpperCAmelCase							,       _UpperCAmelCase							:						List[Any]       							=					fairseq.checkpoint_utils.load_model_ensemble_and_task([path]							)
          return model[0].eval()
  
      _UpperCAmelCase							:						str       							=					load_dataavec(UpperCamelCase__							)
  
      recursively_load_weights(UpperCamelCase__  , UpperCamelCase__  , not is_finetuned							)
  
      _UpperCAmelCase							:						Dict       							=					WavaVecaProcessor.from_pretrained('''facebook/wav2vec2-large-lv60'''							)
  
      _UpperCAmelCase							:						str       							=					load_dataset('''patrickvonplaten/librispeech_asr_dummy'''  , '''clean'''  , split='''validation'''							)
      _UpperCAmelCase							:						Optional[int]       							=					[x['''array'''] for x in ds[:4]['''audio''']]
  
      _UpperCAmelCase							:						Dict       							=					processor(UpperCamelCase__  , return_tensors='''pt'''  , padding=UpperCamelCase__							)
  
      _UpperCAmelCase							:						Union[str, Any]       							=					inputs.input_values
      _UpperCAmelCase							:						Any       							=					inputs.attention_mask
      #    input_values = inputs.input_values[:, :-1]
      #    attention_mask = inputs.attention_mask[:, :-1]
  
      hf_wavavec.eval()
      model.eval()
      if is_finetuned:
          _UpperCAmelCase							:						List[str]       							=					model(source=UpperCamelCase__  , padding_mask=(1 - attention_mask)  , mask=UpperCamelCase__  , features_only=UpperCamelCase__							)[
              '''encoder_out'''
          ].transpose(0  , 1							)
          _UpperCAmelCase							:						List[str]       							=					hf_wavavec(UpperCamelCase__  , attention_mask=UpperCamelCase__							)['''logits''']
  
          _UpperCAmelCase							:						int       							=					torch.argmax(UpperCamelCase__  , dim=-1							)
          _UpperCAmelCase							:						List[Any]       							=					processor.batch_decode(UpperCamelCase__							)
  
          print(F'Expected Output: {ds[:4]["text"]}, Pred: {output_string}'							)
      else:
          _UpperCAmelCase							:						str       							=					model(source=UpperCamelCase__  , padding_mask=(1 - attention_mask)  , mask=UpperCamelCase__  , features_only=UpperCamelCase__							)[
              '''layer_results'''
          ][-1][0].transpose(0  , 1							)
          _UpperCAmelCase							:						Optional[Any]       							=					hf_wavavec(UpperCamelCase__  , attention_mask=UpperCamelCase__							)['''last_hidden_state''']
  
      print(our_output.shape  , their_output.shape							)
      _UpperCAmelCase							:						str       							=					torch.max(torch.abs(our_output - their_output							)							).item()
      print(F'max_absolute_diff = {max_absolute_diff}'							)  # ~ 1e-7
      _UpperCAmelCase							:						List[Any]       							=					torch.allclose(UpperCamelCase__  , UpperCamelCase__  , atol=1E-3							)
      print('''Do both models output the same tensors?'''  , '''🔥''' if success else '''💩'''							)
      if not success:
          raise Exception('''Something went wRoNg'''							)
  
      hf_wavavec.save_pretrained(UpperCamelCase__							)
  
      if is_finetuned:
          processor.save_pretrained(UpperCamelCase__							)
      else:
          processor.feature_extractor.save_pretrained(UpperCamelCase__							)
  
  
  if __name__ == "__main__":
    _lowerCAmelCase					:Union[str, Any]  =      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'
    )
    _lowerCAmelCase					:Optional[Any]  =      parser.parse_args()
    convert_wavaveca_checkpoint(
        args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned
    )
  
  
  
style_context: 
  
  
  
  """simple docstring"""
  from collections import OrderedDict
  from typing import Any, List, Mapping, Optional
  
  from ... import PreTrainedTokenizer, TensorType, is_torch_available
  from ...configuration_utils import PretrainedConfig
  from ...onnx import OnnxConfigWithPast, PatchingSpec
  from ...utils import logging
  
  
  _lowerCAmelCase					:Union[str, Any]  =      logging.get_logger(__name__)
  
  _lowerCAmelCase					:List[str]  =      {
      'gpt2': 'https://huggingface.co/gpt2/resolve/main/config.json',
      'gpt2-medium': 'https://huggingface.co/gpt2-medium/resolve/main/config.json',
      'gpt2-large': 'https://huggingface.co/gpt2-large/resolve/main/config.json',
      'gpt2-xl': 'https://huggingface.co/gpt2-xl/resolve/main/config.json',
      'distilgpt2': 'https://huggingface.co/distilgpt2/resolve/main/config.json',
  }
  class 							_UpperCAmelCase							(				a   ):
  
  
  
  
  
       '''simple docstring'''
  
  
  
       a__             ='''gpt2'''
       a__             =['''past_key_values''']
       a__             ={
           '''hidden_size''': '''n_embd''',
           '''max_position_embeddings''': '''n_positions''',
           '''num_attention_heads''': '''n_head''',
           '''num_hidden_layers''': '''n_layer''',
       }
  
  
  
  
  
       def __init__(      self ,			A=5_0_2_5_7 ,			A=1_0_2_4 ,			A=7_6_8 ,			A=1_2 ,			A=1_2 ,			A=None ,			A="gelu_new" ,			A=0.1 ,			A=0.1 ,			A=0.1 ,			A=1E-5 ,			A=0.02 ,			A="cls_index" ,			A=True ,			A=None ,			A=True ,			A=0.1 ,			A=True ,			A=True ,			A=5_0_2_5_6 ,			A=5_0_2_5_6 ,			A=False ,			A=False ,			**A ,			)							->      Union[str, Any]:
           _UpperCAmelCase							:						Optional[int]       							=					vocab_size
           _UpperCAmelCase							:						Union[str, Any]       							=					n_positions
           _UpperCAmelCase							:						Optional[Any]       							=					n_embd
           _UpperCAmelCase							:						Tuple       							=					n_layer
           _UpperCAmelCase							:						Any       							=					n_head
           _UpperCAmelCase							:						Union[str, Any]       							=					n_inner
           _UpperCAmelCase							:						Optional[int]       							=					activation_function
           _UpperCAmelCase							:						Optional[int]       							=					resid_pdrop
           _UpperCAmelCase							:						Dict       							=					embd_pdrop
           _UpperCAmelCase							:						List[Any]       							=					attn_pdrop
           _UpperCAmelCase							:						Tuple       							=					layer_norm_epsilon
           _UpperCAmelCase							:						Optional[int]       							=					initializer_range
           _UpperCAmelCase							:						List[str]       							=					summary_type
           _UpperCAmelCase							:						Tuple       							=					summary_use_proj
           _UpperCAmelCase							:						List[Any]       							=					summary_activation
           _UpperCAmelCase							:						Optional[int]       							=					summary_first_dropout
           _UpperCAmelCase							:						int       							=					summary_proj_to_labels
           _UpperCAmelCase							:						Union[str, Any]       							=					scale_attn_weights
           _UpperCAmelCase							:						Any       							=					use_cache
           _UpperCAmelCase							:						Tuple       							=					scale_attn_by_inverse_layer_idx
           _UpperCAmelCase							:						Optional[Any]       							=					reorder_and_upcast_attn
  
           _UpperCAmelCase							:						Union[str, Any]       							=					bos_token_id
           _UpperCAmelCase							:						Optional[int]       							=					eos_token_id
  
           super().__init__(bos_token_id=A ,			eos_token_id=A ,			**A	)
  
  
  
  class 							_UpperCAmelCase							(				a   ):
  
  
  
  
  
       '''simple docstring'''
  
  
  
       def __init__(      self ,			A ,			A = "default" ,			A = None ,			A = False ,			)							->      str:
           super().__init__(A ,			task=A ,			patching_specs=A ,			use_past=A	)
           if not getattr(self._config ,			'''pad_token_id''' ,			A	):
               # TODO: how to do that better?
               _UpperCAmelCase							:						Optional[int]       							=					0
  
  
  
  
  
       @property
       def 				__lowerCAmelCase			(      self	)							->      Mapping[str, Mapping[int, str]]:
           _UpperCAmelCase							:						List[str]       							=					OrderedDict({'''input_ids''': {0: '''batch''', 1: '''sequence'''}}	)
           if self.use_past:
               self.fill_with_past_key_values_(A ,			direction='''inputs'''	)
               _UpperCAmelCase							:						Union[str, Any]       							=					{0: '''batch''', 1: '''past_sequence + sequence'''}
           else:
               _UpperCAmelCase							:						Tuple       							=					{0: '''batch''', 1: '''sequence'''}
  
           return common_inputs
  
  
  
  
  
       @property
       def 				__lowerCAmelCase			(      self	)							->      int:
           return self._config.n_layer
  
  
  
  
  
       @property
       def 				__lowerCAmelCase			(      self	)							->      int:
           return self._config.n_head
  
  
  
  
  
       def 				__lowerCAmelCase			(      self ,			A ,			A = -1 ,			A = -1 ,			A = False ,			A = None ,			)							->      Mapping[str, Any]:
           _UpperCAmelCase							:						int       							=					super(A ,			self	).generate_dummy_inputs(
               A ,			batch_size=A ,			seq_length=A ,			is_pair=A ,			framework=A	)
  
           # We need to order the input in the way they appears in the forward()
           _UpperCAmelCase							:						List[Any]       							=					OrderedDict({'''input_ids''': common_inputs['''input_ids''']}	)
  
           # Need to add the past_keys
           if self.use_past:
               if not is_torch_available():
                   raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''	)
               else:
                   import torch
  
                   _UpperCAmelCase							,       _UpperCAmelCase							:						Optional[Any]       							=					common_inputs['''input_ids'''].shape
                   # Not using the same length for past_key_values
                   _UpperCAmelCase							:						Optional[Any]       							=					seqlen + 2
                   _UpperCAmelCase							:						Union[str, Any]       							=					(
                       batch,
                       self.num_attention_heads,
                       past_key_values_length,
                       self._config.hidden_size // self.num_attention_heads,
                   )
                   _UpperCAmelCase							:						List[Any]       							=					[
                       (torch.zeros(A	), torch.zeros(A	)) for _ in range(self.num_layers	)
                   ]
  
           _UpperCAmelCase							:						int       							=					common_inputs['''attention_mask''']
           if self.use_past:
               _UpperCAmelCase							:						int       							=					ordered_inputs['''attention_mask'''].dtype
               _UpperCAmelCase							:						Dict       							=					torch.cat(
                   [ordered_inputs['''attention_mask'''], torch.ones(A ,			A ,			dtype=A	)] ,			dim=1	)
  
           return ordered_inputs
  
  
  
  
  
       @property
       def 				__lowerCAmelCase			(      self	)							->      int:
           return 1_3
  
  
  