


'''simple docstring'''






import argparse
import json
import os

import fairseq
import torch
from torch import nn

from transformers import (
    SpeechaTextaConfig,
    SpeechaTextaForCausalLM,
    SpeechaTextaTokenizer,
    SpeechEncoderDecoderConfig,
    SpeechEncoderDecoderModel,
    WavaVecaConfig,
    WavaVecaFeatureExtractor,
    WavaVecaModel,
    logging,
)


logging.set_verbosity_info()
__A   :    Dict											= logging.get_logger(__name__)

__A   :    List[str]											= {
    "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",
    "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",
}
__A   :    Tuple											= [
    "lm_head",
    "quantizer.weight_proj",
    "quantizer.codevectors",
    "project_q",
    "project_hid",
]







def 							UpperCamelCase_     (       A__   :  List[Any]				,       A__   :  Dict				,       A__   :  Union[str, Any]				,       A__   :  List[Any]				,       A__   :  Dict   ):
  '''simple docstring'''





  for attribute in key.split("""."""   ):
    lowerCAmelCase_     : List[Any]     =  getattr(A__				,       A__   )

  if weight_type is not None:
    lowerCAmelCase_     : int     =  getattr(A__				,       A__   ).shape
  else:
    lowerCAmelCase_     : List[str]     =  hf_pointer.shape

  assert hf_shape == value.shape, (
      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_     : Any     =  value
  elif weight_type == "weight_g":
    lowerCAmelCase_     : Optional[Any]     =  value
  elif weight_type == "weight_v":
    lowerCAmelCase_     : Union[str, Any]     =  value
  elif weight_type == "bias":
    lowerCAmelCase_     : Optional[Any]     =  value
  else:
    lowerCAmelCase_     : Tuple     =  value

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







def 							UpperCamelCase_     (       A__   :  int				,       A__   :  int   ):
  '''simple docstring'''





  lowerCAmelCase_     : List[Any]     =  []
  lowerCAmelCase_     : List[str]     =  fairseq_model.state_dict()

  lowerCAmelCase_     : int     =  hf_model.feature_extractor

  # if encoder has different dim to decoder -> use proj_weight
  lowerCAmelCase_     : Optional[Any]     =  None

  for name, value in fairseq_dict.items():
    lowerCAmelCase_     : List[Any]     =  False
    if "conv_layers" in name:
      load_conv_layer(
          A__				,       A__				,       A__				,       A__				,       hf_model.config.feat_extract_norm == """group"""				,       )
      lowerCAmelCase_     : Dict     =  True
    elif name.split("""."""   )[0] == "proj":
      lowerCAmelCase_     : Union[str, Any]     =  fairseq_model.proj
      lowerCAmelCase_     : Tuple     =  True
    else:
      for key, mapped_key in MAPPING.items():
        if key in name or key.split("""w2v_model."""   )[-1] == name.split("""."""   )[0]:
          lowerCAmelCase_     : Optional[int]     =  True
          if "*" in mapped_key:
            lowerCAmelCase_     : Optional[Any]     =  name.split(A__   )[0].split("""."""   )[-2]
            lowerCAmelCase_     : str     =  mapped_key.replace("""*"""				,       A__   )
          if "weight_g" in name:
            lowerCAmelCase_     : int     =  """weight_g"""
          elif "weight_v" in name:
            lowerCAmelCase_     : int     =  """weight_v"""
          elif "bias" in name:
            lowerCAmelCase_     : Optional[int]     =  """bias"""
          elif "weight" in name:
            lowerCAmelCase_     : Union[str, Any]     =  """weight"""
          else:
            lowerCAmelCase_     : List[Any]     =  None
          set_recursively(A__				,       A__				,       A__				,       A__				,       A__   )
        continue
    if not is_used:
      unused_weights.append(A__   )

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

  return proj_weight







def 							UpperCamelCase_     (       A__   :  List[Any]				,       A__   :  Dict				,       A__   :  Union[str, Any]				,       A__   :  Union[str, Any]				,       A__   :  List[str]   ):
  '''simple docstring'''





  lowerCAmelCase_     : Any     =  full_name.split("""conv_layers."""   )[-1]
  lowerCAmelCase_     : Dict     =  name.split("""."""   )
  lowerCAmelCase_     : Union[str, Any]     =  int(items[0]   )
  lowerCAmelCase_     : List[Any]     =  int(items[1]   )

  if type_id == 0:
    if "bias" in name:
      assert value.shape == feature_extractor.conv_layers[layer_id].conv.bias.data.shape, (
          f'{full_name} has size {value.shape}, but'
          f' {feature_extractor.conv_layers[layer_id].conv.bias.data.shape} was found.'
      )
      lowerCAmelCase_     : str     =  value
      logger.info(f'Feat extract conv layer {layer_id} was initialized from {full_name}.'   )
    elif "weight" in name:
      assert value.shape == feature_extractor.conv_layers[layer_id].conv.weight.data.shape, (
          f'{full_name} has size {value.shape}, but'
          f' {feature_extractor.conv_layers[layer_id].conv.weight.data.shape} was found.'
      )
      lowerCAmelCase_     : List[str]     =  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:
      assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.bias.data.shape, (
          f'{full_name} has size {value.shape}, but {feature_extractor[layer_id].layer_norm.bias.data.shape} was'
          " found."
      )
      lowerCAmelCase_     : str     =  value
      logger.info(f'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.'   )
    elif "weight" in name:
      assert value.shape == feature_extractor.conv_layers[layer_id].layer_norm.weight.data.shape, (
          f'{full_name} has size {value.shape}, but'
          f' {feature_extractor[layer_id].layer_norm.weight.data.shape} was found.'
      )
      lowerCAmelCase_     : List[str]     =  value
      logger.info(f'Feat extract layer norm weight of layer {layer_id} was initialized from {full_name}.'   )
  else:
    unused_weights.append(A__   )







def 							UpperCamelCase_     (       A__   :  str   ):
  '''simple docstring'''





  lowerCAmelCase_,				lowerCAmelCase_     : Optional[Any]     =  emb.weight.shape
  lowerCAmelCase_     : Optional[int]     =  nn.Linear(A__				,       A__				,       bias=A__   )
  lowerCAmelCase_     : int     =  emb.weight.data
  return lin_layer







def 							UpperCamelCase_     (       A__   :  str   ):
  '''simple docstring'''





  with open(A__				,       """r"""				,       encoding="""utf-8"""   ) as f:
    lowerCAmelCase_     : Any     =  f.readlines()
    lowerCAmelCase_     : str     =  [line.split(""" """   )[0] for line in lines]

  lowerCAmelCase_     : Optional[int]     =  len(A__   )

  lowerCAmelCase_     : Optional[int]     =  {
      """<s>""": 0,
      """<pad>""": 1,
      """</s>""": 2,
      """<unk>""": 3,
  }

  vocab_dict.update(dict(zip(A__				,       range(4				,       num_words + 4   )   )   )   )
  return vocab_dict







@torch.no_grad()
def 							UpperCamelCase_     (       A__   :  Tuple				,       A__   :  int				,       A__   :  Optional[Any]				,       A__   :  str				,       A__   :  Any				,       A__   :  Optional[int]				,       A__   :  Any				,       ):
  '''simple docstring'''





  lowerCAmelCase_     : str     =  WavaVecaConfig.from_pretrained(A__   )
  lowerCAmelCase_     : Optional[int]     =  SpeechaTextaConfig.from_pretrained(
      A__				,       vocab_size=A__				,       decoder_layers=A__				,       do_stable_layer_norm=A__   )

  lowerCAmelCase_     : Any     =  WavaVecaFeatureExtractor(
      feature_size=1				,       sampling_rate=1_60_00				,       padding_value=0				,       do_normalize=A__				,       return_attention_mask=A__				,       )

  lowerCAmelCase_,				lowerCAmelCase_,				lowerCAmelCase_     : Optional[int]     =  fairseq.checkpoint_utils.load_model_ensemble_and_task(
      [checkpoint_path]				,       arg_overrides={"""data""": """/""".join(dict_path.split("""/"""   )[:-1]   )}   )
  lowerCAmelCase_     : Optional[int]     =  model[0].eval()

  # set weights for wav2vec2 encoder
  lowerCAmelCase_     : List[Any]     =  WavaVecaModel(A__   )
  lowerCAmelCase_     : Union[str, Any]     =  recursively_load_weights_wavaveca(model.encoder				,       A__   )

  lowerCAmelCase_     : Tuple     =  SpeechaTextaForCausalLM(A__   )
  lowerCAmelCase_,				lowerCAmelCase_     : Any     =  hf_decoder.model.decoder.load_state_dict(model.decoder.state_dict()				,       strict=A__   )

  # set output linear layer
  unexpected_keys.remove("""embed_out"""   )
  lowerCAmelCase_     : Any     =  nn.Parameter(model.decoder.embed_out.detach()   )

  # layer norm is init to identity matrix so leaving it is fine
  logger.warning(f'The following keys are missing when loading the decoder weights: {missing_keys}'   )
  logger.warning(f'The following keys are unexpected when loading the decoder weights: {unexpected_keys}'   )

  lowerCAmelCase_     : Optional[Any]     =  SpeechEncoderDecoderModel(encoder=A__				,       decoder=A__   )
  lowerCAmelCase_     : List[Any]     =  False

  # add projection layer
  lowerCAmelCase_     : Any     =  nn.Parameter(projection_layer.weight   )
  lowerCAmelCase_     : List[str]     =  nn.Parameter(projection_layer.bias   )

  lowerCAmelCase_     : Any     =  create_vocab_dict(A__   )

  with open(os.path.join(A__				,       """vocab.json"""   )				,       """w"""   ) as fp:
    json.dump(A__				,       A__   )

  lowerCAmelCase_     : Dict     =  SpeechaTextaTokenizer(os.path.join(A__				,       """vocab.json"""   )   )
  tokenizer.save_pretrained(A__   )

  lowerCAmelCase_     : Union[str, Any]     =  hf_wavavec.config.to_dict()
  lowerCAmelCase_     : List[Any]     =  tokenizer.pad_token_id
  lowerCAmelCase_     : List[str]     =  tokenizer.bos_token_id
  lowerCAmelCase_     : Tuple     =  tokenizer.eos_token_id
  lowerCAmelCase_     : Union[str, Any]     =  """speech_to_text_2"""
  lowerCAmelCase_     : Optional[int]     =  """wav2vec2"""

  lowerCAmelCase_     : Tuple     =  SpeechEncoderDecoderConfig.from_dict(A__   )

  hf_wavavec.save_pretrained(A__   )
  feature_extractor.save_pretrained(A__   )


if __name__ == "__main__":
  __A   :    int											= 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(
      "--encoder_config_path",
      default="facebook/wav2vec2-large-lv60",
      type=str,
      help="Path to hf encoder wav2vec2 checkpoint config",
  )
  parser.add_argument(
      "--decoder_config_path",
      default="facebook/s2t-small-mustc-en-fr-st",
      type=str,
      help="Path to hf decoder s2t checkpoint config",
  )
  parser.add_argument("--vocab_size", default=1_0224, type=int, help="Vocab size of decoder")
  parser.add_argument("--num_decoder_layers", default=7, type=int, help="Number of decoder layers")

  __A   :    Union[str, Any]											= parser.parse_args()
  convert_wavaveca_checkpoint(
      args.checkpoint_path,
      args.pytorch_dump_folder_path,
      args.dict_path,
      encoder_config_path=args.encoder_config_path,
      decoder_config_path=args.decoder_config_path,
      vocab_size=args.vocab_size,
      num_decoder_layers=args.num_decoder_layers,
  )







'''simple docstring'''






from __future__ import annotations

# This is the precision for this function which can be altered.
# It is recommended for users to keep this number greater than or equal to 10.
__A   :    Tuple											= 10







def 							UpperCamelCase_     (       A__   :  int				,       A__   :  int				,       A__   :  list[int]				,       A__   :  int   ):
  '''simple docstring'''





  for i in range(A__				,       A__   ):
    if array[i] == target:
      return i
  return -1







def 							UpperCamelCase_     (       A__   :  list[int]				,       A__   :  int   ):
  '''simple docstring'''





  lowerCAmelCase_     : Optional[int]     =  0
  lowerCAmelCase_     : Union[str, Any]     =  len(A__   )
  while left <= right:
    if right - left < precision:
      return lin_search(A__				,       A__				,       A__				,       A__   )

    lowerCAmelCase_     : Dict     =  (left + right) // 3 + 1
    lowerCAmelCase_     : List[Any]     =  2 * (left + right) // 3 + 1

    if array[one_third] == target:
      return one_third
    elif array[two_third] == target:
      return two_third

    elif target < array[one_third]:
      lowerCAmelCase_     : List[str]     =  one_third - 1
    elif array[two_third] < target:
      lowerCAmelCase_     : Tuple     =  two_third + 1

    else:
      lowerCAmelCase_     : Tuple     =  one_third + 1
      lowerCAmelCase_     : Any     =  two_third - 1
  else:
    return -1







def 							UpperCamelCase_     (       A__   :  int				,       A__   :  int				,       A__   :  list[int]				,       A__   :  int   ):
  '''simple docstring'''





  if left < right:
    if right - left < precision:
      return lin_search(A__				,       A__				,       A__				,       A__   )
    lowerCAmelCase_     : Dict     =  (left + right) // 3 + 1
    lowerCAmelCase_     : Dict     =  2 * (left + right) // 3 + 1

    if array[one_third] == target:
      return one_third
    elif array[two_third] == target:
      return two_third

    elif target < array[one_third]:
      return rec_ternary_search(A__				,       one_third - 1				,       A__				,       A__   )
    elif array[two_third] < target:
      return rec_ternary_search(two_third + 1				,       A__				,       A__				,       A__   )
    else:
      return rec_ternary_search(one_third + 1				,       two_third - 1				,       A__				,       A__   )
  else:
    return -1


if __name__ == "__main__":
  import doctest

  doctest.testmod()

  __A   :    List[Any]											= input("Enter numbers separated by comma:\n").strip()
  __A   :    Union[str, Any]											= [int(item.strip()) for item in user_input.split(",")]
  assert collection == sorted(collection), F"List must be ordered.\n{collection}."
  __A   :    List[Any]											= int(input("Enter the number to be found in the list:\n").strip())
  __A   :    str											= ite_ternary_search(collection, target)
  __A   :    Dict											= rec_ternary_search(0, len(collection) - 1, collection, target)
  if resulta != -1:
    print(F'''Iterative search: {target} found at positions: {resulta}''')
    print(F'''Recursive search: {target} found at positions: {resulta}''')
  else:
    print("Not found")



