
"""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			:       Tuple									=				logging.get_logger(__name__)

UpperCamelCase			:       List[Any]									=				{
    "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			:       List[Any]									=				[
    "lm_head",
    "quantizer.weight_proj",
    "quantizer.codevectors",
    "project_q",
    "project_hid",
    "projector",
    "classifier",
]






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






def 							A		(       snake_case   :Optional[int]       ,      snake_case   :str       ,      snake_case   :Optional[int]       ,      snake_case   :Any       ,      snake_case   :List[Any]	)   ->			str:
 for attribute in key.split('.'	):
  __UpperCamelCase       						= getattr(snake_case       ,      snake_case	)

 __UpperCamelCase       						= None
 for param_key in PARAM_MAPPING.keys():
  if full_name.endswith(snake_case	):
   __UpperCamelCase       						= PARAM_MAPPING[full_name.split('.'	)[-1]]
   __UpperCamelCase       						= 'param'

 if weight_type is not None and weight_type != "param":
  __UpperCamelCase       						= getattr(snake_case       ,      snake_case	).shape
 elif weight_type is not None and weight_type == "param":
  __UpperCamelCase       						= hf_pointer
  for attribute in hf_param_name.split('.'	):
   __UpperCamelCase       						= getattr(snake_case       ,      snake_case	)
  __UpperCamelCase       						= shape_pointer.shape

  # let's reduce dimension
  __UpperCamelCase       						= value[0]
 else:
  __UpperCamelCase       						= 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       						= value
 elif weight_type == "weight_g":
  __UpperCamelCase       						= value
 elif weight_type == "weight_v":
  __UpperCamelCase       						= value
 elif weight_type == "bias":
  __UpperCamelCase       						= value
 elif weight_type == "param":
  for attribute in hf_param_name.split('.'	):
   __UpperCamelCase       						= getattr(snake_case       ,      snake_case	)
  __UpperCamelCase       						= value
 else:
  __UpperCamelCase       						= value

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






def 							A		(       snake_case   :Any       ,      snake_case   :Tuple       ,      snake_case   :Optional[Any]       ,      snake_case   :Dict       ,      snake_case   :Optional[Any]	)   ->			int:
 __UpperCamelCase       						= None
 for param_key in PARAM_MAPPING.keys():
  if full_name.endswith(snake_case	):
   __UpperCamelCase       						= PARAM_MAPPING[full_name.split('.'	)[-1]]
   __UpperCamelCase       						= 'param'

 if weight_type is not None and weight_type != "param":
  __UpperCamelCase       						= '.'.join([key, weight_type]	)
 elif weight_type is not None and weight_type == "param":
  __UpperCamelCase       						= '.'.join([key, hf_param_name]	)
 else:
  __UpperCamelCase       						= key

 __UpperCamelCase       						= value if 'lm_head' in full_key else value[0]


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






def 							A		(       snake_case   :int       ,      snake_case   :Dict       ,      snake_case   :Optional[Any]	)   ->			List[Any]:
 __UpperCamelCase       						= []
 __UpperCamelCase       						= fairseq_model.state_dict()

 __UpperCamelCase       						= hf_model.wavaveca.feature_extractor

 for name, value in fairseq_dict.items():
  __UpperCamelCase       						= False
  if "conv_layers" in name:
   load_conv_layer(
       snake_case       ,      snake_case       ,      snake_case       ,      snake_case       ,      hf_model.config.feat_extract_norm == 'group'       ,      )
   __UpperCamelCase       						= True
  else:
   __UpperCamelCase       						= load_wavaveca_layer(snake_case       ,      snake_case       ,      snake_case	)
  if not is_used:
   unused_weights.append(snake_case	)

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






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






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

 if is_seq_class:
  __UpperCamelCase       						= read_txt_into_dict(snake_case	)
  __UpperCamelCase       						= idalabel
  __UpperCamelCase       						= WavaVecaForSequenceClassification(snake_case	)
  __UpperCamelCase       						= WavaVecaFeatureExtractor(
      feature_size=1       ,      sampling_rate=1_6_0_0_0       ,      padding_value=0       ,      do_normalize=snake_case       ,      return_attention_mask=snake_case       ,      )
  feature_extractor.save_pretrained(snake_case	)

 elif is_finetuned:
  if dict_path:
   __UpperCamelCase       						= Dictionary.load(snake_case	)

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

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

  __UpperCamelCase       						= WavaVecaForCTC(snake_case	)
 else:
  __UpperCamelCase       						= WavaVecaForPreTraining(snake_case	)

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

  __UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase       						= fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path]       ,      task=snake_case	)

 __UpperCamelCase       						= model[0].eval()

 recursively_load_weights(snake_case       ,      snake_case       ,      not is_finetuned	)

 hf_wavavec.save_pretrained(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"
      )
      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			:       Tuple									=				parser.parse_args()

      UpperCamelCase			:       Optional[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,
      )





"""simple docstring"""

import argparse
import struct
import unittest

class __lowerCAmelCase	:





     def __init__(			self			,  __UpperCAmelCase   ):

      '''simple docstring'''




      __UpperCamelCase       						= data

      # Initialize hash values
      __UpperCamelCase       						= [
          0x6a_09_e6_67,
          0xbb_67_ae_85,
          0x3c_6e_f3_72,
          0xa5_4f_f5_3a,
          0x51_0e_52_7f,
          0x9b_05_68_8c,
          0x1f_83_d9_ab,
          0x5b_e0_cd_19,
      ]

      # Initialize round constants
      __UpperCamelCase       						= [
          0x42_8a_2f_98,
          0x71_37_44_91,
          0xb5_c0_fb_cf,
          0xe9_b5_db_a5,
          0x39_56_c2_5b,
          0x59_f1_11_f1,
          0x92_3f_82_a4,
          0xab_1c_5e_d5,
          0xd8_07_aa_98,
          0x12_83_5b_01,
          0x24_31_85_be,
          0x55_0c_7d_c3,
          0x72_be_5d_74,
          0x80_de_b1_fe,
          0x9b_dc_06_a7,
          0xc1_9b_f1_74,
          0xe4_9b_69_c1,
          0xef_be_47_86,
          0x0f_c1_9d_c6,
          0x24_0c_a1_cc,
          0x2d_e9_2c_6f,
          0x4a_74_84_aa,
          0x5c_b0_a9_dc,
          0x76_f9_88_da,
          0x98_3e_51_52,
          0xa8_31_c6_6d,
          0xb0_03_27_c8,
          0xbf_59_7f_c7,
          0xc6_e0_0b_f3,
          0xd5_a7_91_47,
          0x06_ca_63_51,
          0x14_29_29_67,
          0x27_b7_0a_85,
          0x2e_1b_21_38,
          0x4d_2c_6d_fc,
          0x53_38_0d_13,
          0x65_0a_73_54,
          0x76_6a_0a_bb,
          0x81_c2_c9_2e,
          0x92_72_2c_85,
          0xa2_bf_e8_a1,
          0xa8_1a_66_4b,
          0xc2_4b_8b_70,
          0xc7_6c_51_a3,
          0xd1_92_e8_19,
          0xd6_99_06_24,
          0xf4_0e_35_85,
          0x10_6a_a0_70,
          0x19_a4_c1_16,
          0x1e_37_6c_08,
          0x27_48_77_4c,
          0x34_b0_bc_b5,
          0x39_1c_0c_b3,
          0x4e_d8_aa_4a,
          0x5b_9c_ca_4f,
          0x68_2e_6f_f3,
          0x74_8f_82_ee,
          0x78_a5_63_6f,
          0x84_c8_78_14,
          0x8c_c7_02_08,
          0x90_be_ff_fa,
          0xa4_50_6c_eb,
          0xbe_f9_a3_f7,
          0xc6_71_78_f2,
      ]

      __UpperCamelCase       						= self.preprocessing(self.data   )
      self.final_hash()





     @staticmethod
     def 		UpperCAmelCase      (			__UpperCAmelCase   ):

      '''simple docstring'''




      __UpperCamelCase       						= b'\x80' + (b'\x00' * (63 - (len(__UpperCAmelCase   ) + 8) % 64))
      __UpperCamelCase       						= struct.pack('>Q'			,  (len(__UpperCAmelCase   ) * 8)   )
      return data + padding + big_endian_integer





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      __UpperCamelCase       						= [
          self.preprocessed_data[x : x + 64]
          for x in range(0			,  len(self.preprocessed_data   )			,  64   )
      ]

      for block in self.blocks:
       # Convert the given block into a list of 4 byte integers
       __UpperCamelCase       						= list(struct.unpack('>16L'			,  __UpperCAmelCase   )   )
       # add 48 0-ed integers
       words += [0] * 48

       __UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase       						= self.hashes

       for index in range(0			,  64   ):
        if index > 15:
         # modify the zero-ed indexes at the end of the array
         __UpperCamelCase       						= (
             self.ror(words[index - 15]			,  7   )
             ^ self.ror(words[index - 15]			,  18   )
             ^ (words[index - 15] >> 3)
         )
         __UpperCamelCase       						= (
             self.ror(words[index - 2]			,  17   )
             ^ self.ror(words[index - 2]			,  19   )
             ^ (words[index - 2] >> 10)
         )

         __UpperCamelCase       						= (
             words[index - 16] + sa + words[index - 7] + sa
         ) % 0x1_00_00_00_00

        # Compression
        __UpperCamelCase       						= self.ror(__UpperCAmelCase			,  6   ) ^ self.ror(__UpperCAmelCase			,  11   ) ^ self.ror(__UpperCAmelCase			,  25   )
        __UpperCamelCase       						= (e & f) ^ ((~e & 0xff_ff_ff_ff) & g)
        __UpperCamelCase       						= (
            h + sa + ch + self.round_constants[index] + words[index]
        ) % 0x1_00_00_00_00
        __UpperCamelCase       						= self.ror(__UpperCAmelCase			,  2   ) ^ self.ror(__UpperCAmelCase			,  13   ) ^ self.ror(__UpperCAmelCase			,  22   )
        __UpperCamelCase       						= (a & b) ^ (a & c) ^ (b & c)
        __UpperCamelCase       						= (sa + maj) % 0x1_00_00_00_00

        __UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase						,			__UpperCamelCase       						= (
            g,
            f,
            e,
            ((d + tempa) % 0x1_00_00_00_00),
            c,
            b,
            a,
            ((tempa + tempa) % 0x1_00_00_00_00),
        )

       __UpperCamelCase       						= [a, b, c, d, e, f, g, h]

       # Modify final values
       __UpperCamelCase       						= [
           ((element + mutated_hash_values[index]) % 0x1_00_00_00_00)
           for index, element in enumerate(self.hashes   )
       ]

      __UpperCamelCase       						= ''.join([hex(__UpperCAmelCase   )[2:].zfill(8   ) for value in self.hashes]   )







     def 		UpperCAmelCase      (			self			,  __UpperCAmelCase			,  __UpperCAmelCase   ):

      '''simple docstring'''




      return 0xff_ff_ff_ff & (value << (32 - rotations)) | (value >> rotations)


class __lowerCAmelCase	(   unittest.TestCase    ):





     def 		UpperCAmelCase      (			self   ):

      '''simple docstring'''




      import hashlib

      __UpperCamelCase       						= bytes('Test String'			,  'utf-8'   )
      self.assertEqual(SHAaaa(__UpperCAmelCase   ).hash			,  hashlib.shaaaa(__UpperCAmelCase   ).hexdigest()   )






def 							A		(       )   ->			None:
 import doctest

 doctest.testmod()

 __UpperCamelCase       						= argparse.ArgumentParser()
 parser.add_argument(
     '-s'       ,      '--string'       ,      dest='input_string'       ,      default='Hello World!! Welcome to Cryptography'       ,      help='Hash the string'       ,      )
 parser.add_argument(
     '-f'       ,      '--file'       ,      dest='input_file'       ,      help='Hash contents of a file'	)

 __UpperCamelCase       						= parser.parse_args()

 __UpperCamelCase       						= args.input_string

 # hash input should be a bytestring
 if args.input_file:
  with open(args.input_file       ,      'rb'	) as f:
   __UpperCamelCase       						= f.read()
 else:
  __UpperCamelCase       						= bytes(snake_case       ,      'utf-8'	)

 print(SHAaaa(snake_case	).hash	)


if __name__ == "__main__":
      main()



