


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()
__UpperCamelCase							:							List[Any]    							=					logging.get_logger(__name__)

__UpperCamelCase							:							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",
}
__UpperCamelCase							:							int    							=					[
    "lm_head",
]

def       _a	(					SCREAMING_SNAKE_CASE						:					Optional[int]     ,			SCREAMING_SNAKE_CASE						:					int     ,			SCREAMING_SNAKE_CASE						:					Optional[int]     ,			SCREAMING_SNAKE_CASE						:					Optional[Any]     ,			SCREAMING_SNAKE_CASE						:					List[Any]  ):







 """simple docstring"""



 for attribute in key.split('''.'''  ):
  UpperCamelCase__       :     Any 							= getattr(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  )

 if weight_type is not None:
  UpperCamelCase__       :     Optional[int] 							= getattr(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  ).shape
 else:
  UpperCamelCase__       :     str 							= 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__       :     Union[str, Any] 							= value
 elif weight_type == "weight_g":
  UpperCamelCase__       :     str 							= value
 elif weight_type == "weight_v":
  UpperCamelCase__       :     str 							= value
 elif weight_type == "bias":
  UpperCamelCase__       :     str 							= value
 else:
  UpperCamelCase__       :     Union[str, Any] 							= value

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

def       _a	(					SCREAMING_SNAKE_CASE						:					int     ,			SCREAMING_SNAKE_CASE						:					Union[str, Any]     ,			SCREAMING_SNAKE_CASE						:					str  ):







 """simple docstring"""



 UpperCamelCase__       :     Dict 							= []
 UpperCamelCase__       :     Dict 							= fairseq_model.state_dict()

 if not is_headless:
  UpperCamelCase__       :     str 							= hf_model.dataavec_audio.feature_extractor
  UpperCamelCase__       :     Optional[Any] 							= hf_model.dataavec_audio.encoder.pos_conv_embed

 else:
  UpperCamelCase__       :     Optional[Any] 							= hf_model.feature_extractor
  UpperCamelCase__       :     Tuple 							= hf_model.encoder.pos_conv_embed

 for name, value in fairseq_dict.items():
  UpperCamelCase__       :     str 							= False
  if "conv_layers" in name:
   load_conv_layer(
       SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE     ,			)
   UpperCamelCase__       :     int 							= True
  elif "pos_conv" in name:
   load_pos_conv_layer(
       SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE     ,			)
   UpperCamelCase__       :     Optional[int] 							= True
  else:
   for key, mapped_key in MAPPING.items():
    if not is_headless:
     UpperCamelCase__       :     int 							= '''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__       :     List[str] 							= True
     if "*" in mapped_key:
      UpperCamelCase__       :     int 							= name.split(SCREAMING_SNAKE_CASE  )[0].split('''.'''  )[-2]
      UpperCamelCase__       :     int 							= mapped_key.replace('''*'''     ,			SCREAMING_SNAKE_CASE  )
     if "weight_g" in name:
      UpperCamelCase__       :     List[str] 							= '''weight_g'''
     elif "weight_v" in name:
      UpperCamelCase__       :     Any 							= '''weight_v'''
     elif "bias" in name:
      UpperCamelCase__       :     Optional[int] 							= '''bias'''
     elif "weight" in name:
      # TODO: don't match quantizer.weight_proj
      UpperCamelCase__       :     str 							= '''weight'''
     else:
      UpperCamelCase__       :     int 							= None
     set_recursively(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  )
    continue
  if not is_used:
   unused_weights.append(SCREAMING_SNAKE_CASE  )

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

def       _a	(					SCREAMING_SNAKE_CASE						:					int     ,			SCREAMING_SNAKE_CASE						:					Optional[Any]  ):







 """simple docstring"""



 UpperCamelCase__       :     str 							= path.split('''.'''  )
 return reduce(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  )

def       _a	(					SCREAMING_SNAKE_CASE						:					Union[str, Any]     ,			SCREAMING_SNAKE_CASE						:					int     ,			SCREAMING_SNAKE_CASE						:					Any     ,			SCREAMING_SNAKE_CASE						:					List[str]  ):







 """simple docstring"""



 UpperCamelCase__       :     Any 							= access_by_string(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  )
 UpperCamelCase__       :     Optional[int] 							= 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__       :     Optional[int] 							= fsq_value
 logger.info(F"{full_name} was correctly initialized from {hf_weight_path}."  )

def       _a	(					SCREAMING_SNAKE_CASE						:					Tuple     ,			SCREAMING_SNAKE_CASE						:					Optional[int]     ,			SCREAMING_SNAKE_CASE						:					Optional[int]     ,			SCREAMING_SNAKE_CASE						:					str  ):







 """simple docstring"""



 UpperCamelCase__       :     Optional[Any] 							= full_name.split('''conv_layers.'''  )[-1]
 UpperCamelCase__       :     Dict 							= name.split('''.'''  )
 UpperCamelCase__       :     Optional[int] 							= int(items[0]  )
 UpperCamelCase__       :     Any 							= int(items[1]  )

 UpperCamelCase__       :     str 							= name.split('''.'''  )[-1]
 if type_id == 0:
  UpperCamelCase__       :     str 							= '''conv'''
 elif type_id == 2:
  UpperCamelCase__       :     int 							= '''layer_norm'''
 else:
  unused_weights.append(SCREAMING_SNAKE_CASE  )
  return

 set_weights(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE     ,			F"conv_layers.{layer_id}.{layer_type}.{weight_type}"  )

def       _a	(					SCREAMING_SNAKE_CASE						:					Optional[int]     ,			SCREAMING_SNAKE_CASE						:					Optional[int]     ,			SCREAMING_SNAKE_CASE						:					Tuple     ,			SCREAMING_SNAKE_CASE						:					Any  ):







 """simple docstring"""



 UpperCamelCase__       :     Optional[int] 							= full_name.split('''pos_conv.'''  )[-1]
 UpperCamelCase__       :     Union[str, Any] 							= name.split('''.'''  )
 UpperCamelCase__       :     Optional[int] 							= int(items[0]  )
 UpperCamelCase__       :     int 							= int(items[1]  )

 UpperCamelCase__       :     Tuple 							= name.split('''.'''  )[-1]
 if type_id != 0:
  unused_weights.append(SCREAMING_SNAKE_CASE  )
  return
 else:
  UpperCamelCase__       :     List[Any] 							= '''conv'''

 set_weights(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE     ,			F"layers.{layer_id}.{layer_type}.{weight_type}"  )

@torch.no_grad()
def       _a	(					SCREAMING_SNAKE_CASE						:					Optional[Any]     ,			SCREAMING_SNAKE_CASE						:					Dict     ,			SCREAMING_SNAKE_CASE						:					Union[str, Any]=None     ,			SCREAMING_SNAKE_CASE						:					List[Any]=None     ,			SCREAMING_SNAKE_CASE						:					int=True  ):







 """simple docstring"""



 if config_path is not None:
  UpperCamelCase__       :     Union[str, Any] 							= DataaVecAudioConfig.from_pretrained(SCREAMING_SNAKE_CASE  )
 else:
  UpperCamelCase__       :     Optional[int] 							= DataaVecAudioConfig()

 if not is_finetuned:
  # Modify final_proj layer name
  UpperCamelCase__       :     Optional[Any] 							= DataaVecAudioModel(SCREAMING_SNAKE_CASE  )
  UpperCamelCase__       :     int 							= os.path.dirname(SCREAMING_SNAKE_CASE  )

  UpperCamelCase__       :     Optional[int] 							= torch.load(SCREAMING_SNAKE_CASE  )
  UpperCamelCase__       :     Any 							= state_dict['''model'''].pop('''final_proj.0.weight'''  )
  UpperCamelCase__       :     int 							= state_dict['''model'''].pop('''final_proj.0.bias'''  )
  UpperCamelCase__       :     Optional[Any] 							= os.path.join(SCREAMING_SNAKE_CASE     ,			'''converted.pt'''  )
  torch.save(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  )
 else:
  UpperCamelCase__       :     Union[str, Any] 							= DataaVecAudioForCTC(SCREAMING_SNAKE_CASE  )
  UpperCamelCase__       :     str 							= checkpoint_path

 def load_dataavec(SCREAMING_SNAKE_CASE						:					Optional[int]  ):
  UpperCamelCase__							,					UpperCamelCase__							,					UpperCamelCase__       :     int 							= fairseq.checkpoint_utils.load_model_ensemble_and_task([path]  )
  return model[0].eval()

 UpperCamelCase__       :     List[Any] 							= load_dataavec(SCREAMING_SNAKE_CASE  )

 recursively_load_weights(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE     ,			not is_finetuned  )

 UpperCamelCase__       :     Optional[Any] 							= WavaVecaProcessor.from_pretrained('''facebook/wav2vec2-large-lv60'''  )

 UpperCamelCase__       :     Any 							= load_dataset('''patrickvonplaten/librispeech_asr_dummy'''     ,			'''clean'''     ,			split='''validation'''  )
 UpperCamelCase__       :     int 							= [x['''array'''] for x in ds[:4]['''audio''']]

 UpperCamelCase__       :     Optional[Any] 							= processor(SCREAMING_SNAKE_CASE     ,			return_tensors='''pt'''     ,			padding=SCREAMING_SNAKE_CASE  )

 UpperCamelCase__       :     Optional[int] 							= inputs.input_values
 UpperCamelCase__       :     List[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=SCREAMING_SNAKE_CASE     ,			padding_mask=(1 - attention_mask)     ,			mask=SCREAMING_SNAKE_CASE     ,			features_only=SCREAMING_SNAKE_CASE  )[
      '''encoder_out'''
  ].transpose(0     ,			1  )
  UpperCamelCase__       :     Tuple 							= hf_wavavec(SCREAMING_SNAKE_CASE     ,			attention_mask=SCREAMING_SNAKE_CASE  )['''logits''']

  UpperCamelCase__       :     int 							= torch.argmax(SCREAMING_SNAKE_CASE     ,			dim=-1  )
  UpperCamelCase__       :     List[str] 							= processor.batch_decode(SCREAMING_SNAKE_CASE  )

  print(F"Expected Output: {ds[:4]['text']}, Pred: {output_string}"  )
 else:
  UpperCamelCase__       :     Optional[Any] 							= model(source=SCREAMING_SNAKE_CASE     ,			padding_mask=(1 - attention_mask)     ,			mask=SCREAMING_SNAKE_CASE     ,			features_only=SCREAMING_SNAKE_CASE  )[
      '''layer_results'''
  ][-1][0].transpose(0     ,			1  )
  UpperCamelCase__       :     Optional[int] 							= hf_wavavec(SCREAMING_SNAKE_CASE     ,			attention_mask=SCREAMING_SNAKE_CASE  )['''last_hidden_state''']

 print(our_output.shape     ,			their_output.shape  )
 UpperCamelCase__       :     int 							= torch.max(torch.abs(our_output - their_output  )  ).item()
 print(F"max_absolute_diff = {max_absolute_diff}"  )  # ~ 1e-7
 UpperCamelCase__       :     int 							= torch.allclose(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE     ,			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(SCREAMING_SNAKE_CASE  )

 if is_finetuned:
  processor.save_pretrained(SCREAMING_SNAKE_CASE  )
 else:
  processor.feature_extractor.save_pretrained(SCREAMING_SNAKE_CASE  )


if __name__ == "__main__":
       __UpperCamelCase							:							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"
       )
       __UpperCamelCase							:							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
       )






import argparse
import os
import re


# All paths are set with the intent you should run this script from the root of the repo with the command
# python utils/check_dummies.py
__UpperCamelCase							:							Optional[Any]    							=					"src/transformers"

# Matches is_xxx_available()
__UpperCamelCase							:							str    							=					re.compile(r"is\_([a-z_]*)_available()")
# Matches from xxx import bla
__UpperCamelCase							:							Union[str, Any]    							=					re.compile(r"\s+from\s+\S*\s+import\s+([^\(\s].*)\n")
__UpperCamelCase							:							Dict    							=					re.compile(r"^\s+if\s+not\s+\(?is\_[a-z_]*\_available\(\)")


__UpperCamelCase							:							Any    							=					"\n{0} = None\n"

__UpperCamelCase							:							Tuple    							=					"\nclass {0}(metaclass=DummyObject):\n    _backends = {1}\n\n    def __init__(self, *args, **kwargs):\n        requires_backends(self, {1})\n"


__UpperCamelCase							:							Optional[Any]    							=					"\ndef {0}(*args, **kwargs):\n    requires_backends({0}, {1})\n"

def       _a	(					SCREAMING_SNAKE_CASE						:					str  ):







 """simple docstring"""



 if _re_test_backend.search(SCREAMING_SNAKE_CASE  ) is None:
  return None
 UpperCamelCase__       :     List[str] 							= [b[0] for b in _re_backend.findall(SCREAMING_SNAKE_CASE  )]
 backends.sort()
 return "_and_".join(SCREAMING_SNAKE_CASE  )

def       _a	(					):







 """simple docstring"""



 with open(os.path.join(SCREAMING_SNAKE_CASE     ,			'''__init__.py'''  )     ,			'''r'''     ,			encoding='''utf-8'''     ,			newline='''\n'''  ) as f:
  UpperCamelCase__       :     Union[str, Any] 							= f.readlines()

 # Get to the point we do the actual imports for type checking
 UpperCamelCase__       :     int 							= 0
 while not lines[line_index].startswith('''if TYPE_CHECKING'''  ):
  line_index += 1

 UpperCamelCase__       :     Optional[Any] 							= {}
 # Go through the end of the file
 while line_index < len(SCREAMING_SNAKE_CASE  ):
  # If the line is an if is_backend_available, we grab all objects associated.
  UpperCamelCase__       :     Any 							= find_backend(lines[line_index]  )
  if backend is not None:
   while not lines[line_index].startswith('''    else:'''  ):
    line_index += 1
   line_index += 1

   UpperCamelCase__       :     List[str] 							= []
   # Until we unindent, add backend objects to the list
   while len(lines[line_index]  ) <= 1 or lines[line_index].startswith(''' ''' * 8  ):
    UpperCamelCase__       :     str 							= lines[line_index]
    UpperCamelCase__       :     Union[str, Any] 							= _re_single_line_import.search(SCREAMING_SNAKE_CASE  )
    if single_line_import_search is not None:
     objects.extend(single_line_import_search.groups()[0].split(''', '''  )  )
    elif line.startswith(''' ''' * 12  ):
     objects.append(line[12:-2]  )
    line_index += 1

   UpperCamelCase__       :     Optional[int] 							= objects
  else:
   line_index += 1

 return backend_specific_objects

def       _a	(					SCREAMING_SNAKE_CASE						:					str     ,			SCREAMING_SNAKE_CASE						:					List[str]  ):







 """simple docstring"""



 if name.isupper():
  return DUMMY_CONSTANT.format(SCREAMING_SNAKE_CASE  )
 elif name.islower():
  return DUMMY_FUNCTION.format(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  )
 else:
  return DUMMY_CLASS.format(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  )

def       _a	(					SCREAMING_SNAKE_CASE						:					Dict=None  ):







 """simple docstring"""



 if backend_specific_objects is None:
  UpperCamelCase__       :     Any 							= read_init()
 # For special correspondence backend to module name as used in the function requires_modulename
 UpperCamelCase__       :     Optional[Any] 							= {}

 for backend, objects in backend_specific_objects.items():
  UpperCamelCase__       :     int 							= '''[''' + ''', '''.join(F"\"{b}\"" for b in backend.split('''_and_'''  )  ) + ''']'''
  UpperCamelCase__       :     Any 							= '''# This file is autogenerated by the command `make fix-copies`, do not edit.\n'''
  dummy_file += "from ..utils import DummyObject, requires_backends\n\n"
  dummy_file += "\n".join([create_dummy_object(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  ) for o in objects]  )
  UpperCamelCase__       :     List[str] 							= dummy_file

 return dummy_files

def       _a	(					SCREAMING_SNAKE_CASE						:					Union[str, Any]=False  ):







 """simple docstring"""



 UpperCamelCase__       :     Dict 							= create_dummy_files()
 # For special correspondence backend to shortcut as used in utils/dummy_xxx_objects.py
 UpperCamelCase__       :     Tuple 							= {'''torch''': '''pt'''}

 # Locate actual dummy modules and read their content.
 UpperCamelCase__       :     Optional[Any] 							= os.path.join(SCREAMING_SNAKE_CASE     ,			'''utils'''  )
 UpperCamelCase__       :     int 							= {
     backend: os.path.join(SCREAMING_SNAKE_CASE     ,			F"dummy_{short_names.get(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  )}_objects.py"  )
     for backend in dummy_files.keys()
 }

 UpperCamelCase__       :     Optional[Any] 							= {}
 for backend, file_path in dummy_file_paths.items():
  if os.path.isfile(SCREAMING_SNAKE_CASE  ):
   with open(SCREAMING_SNAKE_CASE     ,			'''r'''     ,			encoding='''utf-8'''     ,			newline='''\n'''  ) as f:
    UpperCamelCase__       :     List[Any] 							= f.read()
  else:
   UpperCamelCase__       :     List[Any] 							= ''''''

 for backend in dummy_files.keys():
  if dummy_files[backend] != actual_dummies[backend]:
   if overwrite:
    print(
        F"Updating transformers.utils.dummy_{short_names.get(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  )}_objects.py as the main "
        '''__init__ has new objects.'''  )
    with open(dummy_file_paths[backend]     ,			'''w'''     ,			encoding='''utf-8'''     ,			newline='''\n'''  ) as f:
     f.write(dummy_files[backend]  )
   else:
    raise ValueError(
        '''The main __init__ has objects that are not present in '''
        F"transformers.utils.dummy_{short_names.get(SCREAMING_SNAKE_CASE     ,			SCREAMING_SNAKE_CASE  )}_objects.py. Run `make fix-copies` "
        '''to fix this.'''  )


if __name__ == "__main__":
       __UpperCamelCase							:							Optional[int]    							=					argparse.ArgumentParser()
       parser.add_argument("--fix_and_overwrite", action="store_true", help="Whether to fix inconsistencies.")
       __UpperCamelCase							:							str    							=					parser.parse_args()

       check_dummies(args.fix_and_overwrite)


