



'''simple docstring'''






import os
from datetime import datetime as dt

from github import Github


UpperCamelCase__:   Union[str, Any]								=			[
    "good first issue",
    "feature request",
    "wip",
]


def  snake_case_     (					)      ->   int:
							UpperCAmelCase :       Dict 							=		Github(os.environ['''GITHUB_TOKEN''']  )
							UpperCAmelCase :       Any 							=		g.get_repo('''huggingface/accelerate'''  )
							UpperCAmelCase :       str 							=		repo.get_issues(state='''open'''  )

							for issue in open_issues:
														UpperCAmelCase :       Optional[int] 							=		sorted([comment for comment in issue.get_comments()]   ,      key=lambda _lowerCAmelCase  : i.created_at   ,      reverse=_lowerCAmelCase  )
														UpperCAmelCase :       Optional[Any] 							=		comments[0] if len(_lowerCAmelCase  ) > 0 else None
														UpperCAmelCase :       Optional[int] 							=		dt.utcnow()
														UpperCAmelCase :       Union[str, Any] 							=		(current_time - issue.updated_at).days
														UpperCAmelCase :       Optional[Any] 							=		(current_time - issue.created_at).days
														if (
														    last_comment is not None
														    and last_comment.user.login == "github-actions[bot]"
														    and days_since_updated > 7
														    and days_since_creation >= 30
														    and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels()  )
														):
																					# Close issue since it has been 7 days of inactivity since bot mention.
																					issue.edit(state='''closed'''  )
														elif (
														    days_since_updated > 23
														    and days_since_creation >= 30
														    and not any(label.name.lower() in LABELS_TO_EXEMPT for label in issue.get_labels()  )
														):
																					# Add stale comment
																					issue.create_comment(
																					    '''This issue has been automatically marked as stale because it has not had '''
																					    '''recent activity. If you think this still needs to be addressed '''
																					    '''please comment on this thread.\n\nPlease note that issues that do not follow the '''
																					    '''[contributing guidelines](https://github.com/huggingface/accelerate/blob/main/CONTRIBUTING.md) '''
																					    '''are likely to be ignored.'''  )


if __name__ == "__main__":
						main()










'''simple docstring'''






import argparse
import json
import os

import fairseq
import torch
from fairseq.data import Dictionary

from transformers import (
    WavaVecaConfig,
    WavaVecaCTCTokenizer,
    WavaVecaFeatureExtractor,
    WavaVecaForCTC,
    WavaVecaForPreTraining,
    WavaVecaProcessor,
    logging,
)
from transformers.models.wavaveca.modeling_wavaveca import WavaVecaForSequenceClassification


logging.set_verbosity_info()
UpperCamelCase__:   int								=			logging.get_logger(__name__)

UpperCamelCase__:   Dict								=			{
    "post_extract_proj": "feature_projection.projection",
    "encoder.pos_conv.0": "encoder.pos_conv_embed.conv",
    "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",
    "adapter_layer": "encoder.layers.*.adapter_layer",
    "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",
    "pooling_layer.linear": "projector",
    "pooling_layer.projection": "classifier",
}
UpperCamelCase__:   Optional[Any]								=			[
    "lm_head",
    "quantizer.weight_proj",
    "quantizer.codevectors",
    "project_q",
    "project_hid",
    "projector",
    "classifier",
]


def  snake_case_     (					_lowerCAmelCase  :						str  )      ->   Union[str, Any]:
							UpperCAmelCase :       Optional[int] 							=		{}
							with open(_lowerCAmelCase   ,      '''r'''  ) as file:
														for line_number, line in enumerate(_lowerCAmelCase  ):
																					UpperCAmelCase :       List[str] 							=		line.strip()
																					if line:
																												UpperCAmelCase :       str 							=		line.split()
																												UpperCAmelCase :       Union[str, Any] 							=		line_number
																												UpperCAmelCase :       List[Any] 							=		words[0]
																												UpperCAmelCase :       Union[str, Any] 							=		value
							return result


def  snake_case_     (					_lowerCAmelCase  :						int   ,      _lowerCAmelCase  :						Optional[Any]   ,      _lowerCAmelCase  :						Optional[Any]   ,      _lowerCAmelCase  :						Tuple   ,      _lowerCAmelCase  :						str  )      ->   int:
							for attribute in key.split('''.'''  ):
														UpperCAmelCase :       Dict 							=		getattr(_lowerCAmelCase   ,      _lowerCAmelCase  )

							UpperCAmelCase :       Dict 							=		None
							for param_key in PARAM_MAPPING.keys():
														if full_name.endswith(_lowerCAmelCase  ):
																					UpperCAmelCase :       Any 							=		PARAM_MAPPING[full_name.split('''.'''  )[-1]]
																					UpperCAmelCase :       Dict 							=		'''param'''

							if weight_type is not None and weight_type != "param":
														UpperCAmelCase :       Optional[int] 							=		getattr(_lowerCAmelCase   ,      _lowerCAmelCase  ).shape
							elif weight_type is not None and weight_type == "param":
														UpperCAmelCase :       List[Any] 							=		hf_pointer
														for attribute in hf_param_name.split('''.'''  ):
																					UpperCAmelCase :       Optional[Any] 							=		getattr(_lowerCAmelCase   ,      _lowerCAmelCase  )
														UpperCAmelCase :       int 							=		shape_pointer.shape

														# let's reduce dimension
														UpperCAmelCase :       Union[str, Any] 							=		value[0]
							else:
														UpperCAmelCase :       List[Any] 							=		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 :       int 							=		value
							elif weight_type == "weight_g":
														UpperCAmelCase :       str 							=		value
							elif weight_type == "weight_v":
														UpperCAmelCase :       Dict 							=		value
							elif weight_type == "bias":
														UpperCAmelCase :       str 							=		value
							elif weight_type == "param":
														for attribute in hf_param_name.split('''.'''  ):
																					UpperCAmelCase :       int 							=		getattr(_lowerCAmelCase   ,      _lowerCAmelCase  )
														UpperCAmelCase :       Optional[int] 							=		value
							else:
														UpperCAmelCase :       Tuple 							=		value

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


def  snake_case_     (					_lowerCAmelCase  :						Optional[int]   ,      _lowerCAmelCase  :						Any   ,      _lowerCAmelCase  :						List[Any]   ,      _lowerCAmelCase  :						Dict   ,      _lowerCAmelCase  :						List[Any]  )      ->   List[Any]:
							UpperCAmelCase :       List[str] 							=		None
							for param_key in PARAM_MAPPING.keys():
														if full_name.endswith(_lowerCAmelCase  ):
																					UpperCAmelCase :       List[str] 							=		PARAM_MAPPING[full_name.split('''.'''  )[-1]]
																					UpperCAmelCase :       Any 							=		'''param'''

							if weight_type is not None and weight_type != "param":
														UpperCAmelCase :       Optional[int] 							=		'''.'''.join([key, weight_type]  )
							elif weight_type is not None and weight_type == "param":
														UpperCAmelCase :       Optional[int] 							=		'''.'''.join([key, hf_param_name]  )
							else:
														UpperCAmelCase :       List[Any] 							=		key

							UpperCAmelCase :       Tuple 							=		value if '''lm_head''' in full_key else value[0]


UpperCamelCase__:   Tuple								=			{
    "W_a": "linear_1.weight",
    "W_b": "linear_2.weight",
    "b_a": "linear_1.bias",
    "b_b": "linear_2.bias",
    "ln_W": "norm.weight",
    "ln_b": "norm.bias",
}


def  snake_case_     (					_lowerCAmelCase  :						str   ,      _lowerCAmelCase  :						List[str]   ,      _lowerCAmelCase  :						Any=None   ,      _lowerCAmelCase  :						Optional[Any]=None  )      ->   int:
							UpperCAmelCase :       List[Any] 							=		False
							for key, mapped_key in MAPPING.items():
														UpperCAmelCase :       int 							=		'''wav2vec2.''' + 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 :       Optional[Any] 							=		True
																					if "*" in mapped_key:
																												UpperCAmelCase :       Tuple 							=		name.split(_lowerCAmelCase  )[0].split('''.'''  )[-2]
																												UpperCAmelCase :       List[Any] 							=		mapped_key.replace('''*'''   ,      _lowerCAmelCase  )
																					if "weight_g" in name:
																												UpperCAmelCase :       str 							=		'''weight_g'''
																					elif "weight_v" in name:
																												UpperCAmelCase :       int 							=		'''weight_v'''
																					elif "bias" in name:
																												UpperCAmelCase :       int 							=		'''bias'''
																					elif "weight" in name:
																												# TODO: don't match quantizer.weight_proj
																												UpperCAmelCase :       List[str] 							=		'''weight'''
																					else:
																												UpperCAmelCase :       Dict 							=		None
																					if hf_dict is not None:
																												rename_dict(_lowerCAmelCase   ,      _lowerCAmelCase   ,      _lowerCAmelCase   ,      _lowerCAmelCase   ,      _lowerCAmelCase  )
																					else:
																												set_recursively(_lowerCAmelCase   ,      _lowerCAmelCase   ,      _lowerCAmelCase   ,      _lowerCAmelCase   ,      _lowerCAmelCase  )
																					return is_used
							return is_used


def  snake_case_     (					_lowerCAmelCase  :						Union[str, Any]   ,      _lowerCAmelCase  :						Optional[Any]   ,      _lowerCAmelCase  :						Any  )      ->   Any:
							UpperCAmelCase :       Dict 							=		[]
							UpperCAmelCase :       Dict 							=		fairseq_model.state_dict()

							UpperCAmelCase :       Union[str, Any] 							=		hf_model.wavaveca.feature_extractor

							for name, value in fairseq_dict.items():
														UpperCAmelCase :       Dict 							=		False
														if "conv_layers" in name:
																					load_conv_layer(
																					    _lowerCAmelCase   ,      _lowerCAmelCase   ,      _lowerCAmelCase   ,      _lowerCAmelCase   ,      hf_model.config.feat_extract_norm == '''group'''   ,      )
																					UpperCAmelCase :       Any 							=		True
														else:
																					UpperCAmelCase :       Optional[Any] 							=		load_wavaveca_layer(_lowerCAmelCase   ,      _lowerCAmelCase   ,      _lowerCAmelCase  )
														if not is_used:
																					unused_weights.append(_lowerCAmelCase  )

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


def  snake_case_     (					_lowerCAmelCase  :						Optional[int]   ,      _lowerCAmelCase  :						Tuple   ,      _lowerCAmelCase  :						Any   ,      _lowerCAmelCase  :						Optional[int]   ,      _lowerCAmelCase  :						Optional[int]  )      ->   Union[str, Any]:
							UpperCAmelCase :       Any 							=		full_name.split('''conv_layers.'''  )[-1]
							UpperCAmelCase :       Optional[int] 							=		name.split('''.'''  )
							UpperCAmelCase :       Tuple 							=		int(items[0]  )
							UpperCAmelCase :       Tuple 							=		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."""  )
																					UpperCAmelCase :       Tuple 							=		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."""  )
																					UpperCAmelCase :       Union[str, Any] 							=		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."""  )
																					UpperCAmelCase :       Union[str, Any] 							=		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."""  )
																					UpperCAmelCase :       List[str] 							=		value
																					logger.info(f"""Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}."""  )
							else:
														unused_weights.append(_lowerCAmelCase  )


@torch.no_grad()
def  snake_case_     (					_lowerCAmelCase  :						Union[str, Any]   ,      _lowerCAmelCase  :						List[Any]   ,      _lowerCAmelCase  :						Dict=None   ,      _lowerCAmelCase  :						List[Any]=None   ,      _lowerCAmelCase  :						int=True   ,      _lowerCAmelCase  :						Optional[int]=False  )      ->   Dict:
							if config_path is not None:
														UpperCAmelCase :       List[str] 							=		WavaVecaConfig.from_pretrained(_lowerCAmelCase  )
							else:
														UpperCAmelCase :       List[Any] 							=		WavaVecaConfig()

							if is_seq_class:
														UpperCAmelCase :       Optional[Any] 							=		read_txt_into_dict(_lowerCAmelCase  )
														UpperCAmelCase :       Optional[int] 							=		idalabel
														UpperCAmelCase :       Optional[Any] 							=		WavaVecaForSequenceClassification(_lowerCAmelCase  )
														UpperCAmelCase :       Dict 							=		WavaVecaFeatureExtractor(
														    feature_size=1   ,      sampling_rate=16000   ,      padding_value=0   ,      do_normalize=_lowerCAmelCase   ,      return_attention_mask=_lowerCAmelCase   ,      )
														feature_extractor.save_pretrained(_lowerCAmelCase  )

							elif is_finetuned:
														if dict_path:
																					UpperCAmelCase :       Dict 							=		Dictionary.load(_lowerCAmelCase  )

																					# important change bos & pad token id since CTC symbol is <pad> and
																					# not <s> as in fairseq
																					UpperCAmelCase :       Any 							=		target_dict.pad_index
																					UpperCAmelCase :       Tuple 							=		target_dict.bos_index
																					UpperCAmelCase :       Optional[int] 							=		target_dict.eos_index
																					UpperCAmelCase :       Union[str, Any] 							=		len(target_dict.symbols  )
																					UpperCAmelCase :       Dict 							=		os.path.join(_lowerCAmelCase   ,      '''vocab.json'''  )
																					if not os.path.isdir(_lowerCAmelCase  ):
																												logger.error('''--pytorch_dump_folder_path ({}) should be a directory'''.format(_lowerCAmelCase  )  )
																												return
																					os.makedirs(_lowerCAmelCase   ,      exist_ok=_lowerCAmelCase  )
																					UpperCAmelCase :       List[Any] 							=		target_dict.indices

																					# fairseq has the <pad> and <s> switched
																					UpperCAmelCase :       List[str] 							=		0
																					UpperCAmelCase :       List[str] 							=		1
																					with open(_lowerCAmelCase   ,      '''w'''   ,      encoding='''utf-8'''  ) as vocab_handle:
																												json.dump(_lowerCAmelCase   ,      _lowerCAmelCase  )
																					UpperCAmelCase :       Optional[int] 							=		WavaVecaCTCTokenizer(
																					    _lowerCAmelCase   ,      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=_lowerCAmelCase   ,      )
																					UpperCAmelCase :       int 							=		True if config.feat_extract_norm == '''layer''' else False
																					UpperCAmelCase :       int 							=		WavaVecaFeatureExtractor(
																					    feature_size=1   ,      sampling_rate=16000   ,      padding_value=0   ,      do_normalize=_lowerCAmelCase   ,      return_attention_mask=_lowerCAmelCase   ,      )
																					UpperCAmelCase :       str 							=		WavaVecaProcessor(feature_extractor=_lowerCAmelCase   ,      tokenizer=_lowerCAmelCase  )
																					processor.save_pretrained(_lowerCAmelCase  )

														UpperCAmelCase :       Union[str, Any] 							=		WavaVecaForCTC(_lowerCAmelCase  )
							else:
														UpperCAmelCase :       Dict 							=		WavaVecaForPreTraining(_lowerCAmelCase  )

							if is_finetuned or is_seq_class:
														UpperCAmelCase							,		UpperCAmelCase							,		UpperCAmelCase :       List[str] 							=		fairseq.checkpoint_utils.load_model_ensemble_and_task(
														    [checkpoint_path]   ,      arg_overrides={'''data''': '''/'''.join(dict_path.split('''/'''  )[:-1]  )}  )
							else:
														UpperCAmelCase :       Optional[Any] 							=		argparse.Namespace(task='''audio_pretraining'''  )
														UpperCAmelCase :       List[Any] 							=		fairseq.tasks.setup_task(_lowerCAmelCase  )

														UpperCAmelCase							,		UpperCAmelCase							,		UpperCAmelCase :       int 							=		fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path]   ,      task=_lowerCAmelCase  )

							UpperCAmelCase :       Optional[int] 							=		model[0].eval()

							recursively_load_weights(_lowerCAmelCase   ,      _lowerCAmelCase   ,      not is_finetuned  )

							hf_wavavec.save_pretrained(_lowerCAmelCase  )


if __name__ == "__main__":
						UpperCamelCase__:   Dict								=			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"
						)
						parser.add_argument(
						    "--is_seq_class",
						    action="store_true",
						    help="Whether the model to convert is a fine-tuned sequence classification model or not",
						)
						UpperCamelCase__:   Any								=			parser.parse_args()

						UpperCamelCase__:   int								=			not args.not_finetuned and not args.is_seq_class
						convert_wavaveca_checkpoint(
						    args.checkpoint_path,
						    args.pytorch_dump_folder_path,
						    args.config_path,
						    args.dict_path,
						    is_finetuned,
						    args.is_seq_class,
						)





