import argparse
import struct
import unittest







class __snake_case				:




   def __init__(    self			:				int       ,	_lowercase			:				List[Any]      ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__										=      data

     # Initialize hash values
     SCREAMING_SNAKE_CASE__										=      [
         0X6_a_0_9_e_6_6_7,
         0Xb_b_6_7_a_e_8_5,
         0X3_c_6_e_f_3_7_2,
         0Xa_5_4_f_f_5_3_a,
         0X5_1_0_e_5_2_7_f,
         0X9_b_0_5_6_8_8_c,
         0X1_f_8_3_d_9_a_b,
         0X5_b_e_0_c_d_1_9,
     ]

     # Initialize round constants
     SCREAMING_SNAKE_CASE__										=      [
         0X4_2_8_a_2_f_9_8,
         0X7_1_3_7_4_4_9_1,
         0Xb_5_c_0_f_b_c_f,
         0Xe_9_b_5_d_b_a_5,
         0X3_9_5_6_c_2_5_b,
         0X5_9_f_1_1_1_f_1,
         0X9_2_3_f_8_2_a_4,
         0Xa_b_1_c_5_e_d_5,
         0Xd_8_0_7_a_a_9_8,
         0X1_2_8_3_5_b_0_1,
         0X2_4_3_1_8_5_b_e,
         0X5_5_0_c_7_d_c_3,
         0X7_2_b_e_5_d_7_4,
         0X8_0_d_e_b_1_f_e,
         0X9_b_d_c_0_6_a_7,
         0Xc_1_9_b_f_1_7_4,
         0Xe_4_9_b_6_9_c_1,
         0Xe_f_b_e_4_7_8_6,
         0X0_f_c_1_9_d_c_6,
         0X2_4_0_c_a_1_c_c,
         0X2_d_e_9_2_c_6_f,
         0X4_a_7_4_8_4_a_a,
         0X5_c_b_0_a_9_d_c,
         0X7_6_f_9_8_8_d_a,
         0X9_8_3_e_5_1_5_2,
         0Xa_8_3_1_c_6_6_d,
         0Xb_0_0_3_2_7_c_8,
         0Xb_f_5_9_7_f_c_7,
         0Xc_6_e_0_0_b_f_3,
         0Xd_5_a_7_9_1_4_7,
         0X0_6_c_a_6_3_5_1,
         0X1_4_2_9_2_9_6_7,
         0X2_7_b_7_0_a_8_5,
         0X2_e_1_b_2_1_3_8,
         0X4_d_2_c_6_d_f_c,
         0X5_3_3_8_0_d_1_3,
         0X6_5_0_a_7_3_5_4,
         0X7_6_6_a_0_a_b_b,
         0X8_1_c_2_c_9_2_e,
         0X9_2_7_2_2_c_8_5,
         0Xa_2_b_f_e_8_a_1,
         0Xa_8_1_a_6_6_4_b,
         0Xc_2_4_b_8_b_7_0,
         0Xc_7_6_c_5_1_a_3,
         0Xd_1_9_2_e_8_1_9,
         0Xd_6_9_9_0_6_2_4,
         0Xf_4_0_e_3_5_8_5,
         0X1_0_6_a_a_0_7_0,
         0X1_9_a_4_c_1_1_6,
         0X1_e_3_7_6_c_0_8,
         0X2_7_4_8_7_7_4_c,
         0X3_4_b_0_b_c_b_5,
         0X3_9_1_c_0_c_b_3,
         0X4_e_d_8_a_a_4_a,
         0X5_b_9_c_c_a_4_f,
         0X6_8_2_e_6_f_f_3,
         0X7_4_8_f_8_2_e_e,
         0X7_8_a_5_6_3_6_f,
         0X8_4_c_8_7_8_1_4,
         0X8_c_c_7_0_2_0_8,
         0X9_0_b_e_f_f_f_a,
         0Xa_4_5_0_6_c_e_b,
         0Xb_e_f_9_a_3_f_7,
         0Xc_6_7_1_7_8_f_2,
     ]

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




   @staticmethod
   def 	__a						(    _lowercase			:				int      ):



     """simple docstring"""






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




   def 	__a						(    self			:				int      ):



     """simple docstring"""






     SCREAMING_SNAKE_CASE__										=      [
         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
       SCREAMING_SNAKE_CASE__										=      list(struct.unpack(""">16L"""       ,	_lowercase      )      )
       # add 48 0-ed integers
       words += [0] * 48

       SCREAMING_SNAKE_CASE__							,     SCREAMING_SNAKE_CASE__							,     SCREAMING_SNAKE_CASE__							,     SCREAMING_SNAKE_CASE__							,     SCREAMING_SNAKE_CASE__							,     SCREAMING_SNAKE_CASE__							,     SCREAMING_SNAKE_CASE__							,     SCREAMING_SNAKE_CASE__										=      self.hashes

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

           SCREAMING_SNAKE_CASE__										=      (
               words[index - 16] + sa + words[index - 7] + sa
           ) % 0X1_0_0_0_0_0_0_0_0

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

         SCREAMING_SNAKE_CASE__							,     SCREAMING_SNAKE_CASE__							,     SCREAMING_SNAKE_CASE__							,     SCREAMING_SNAKE_CASE__							,     SCREAMING_SNAKE_CASE__							,     SCREAMING_SNAKE_CASE__							,     SCREAMING_SNAKE_CASE__							,     SCREAMING_SNAKE_CASE__										=      (
             g,
             f,
             e,
             ((d + tempa) % 0X1_0_0_0_0_0_0_0_0),
             c,
             b,
             a,
             ((tempa + tempa) % 0X1_0_0_0_0_0_0_0_0),
         )

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

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

     SCREAMING_SNAKE_CASE__										=      """""".join([hex(_lowercase      )[2:].zfill(8      ) for value in self.hashes]      )




   def 	__a						(    self			:				Tuple       ,	_lowercase			:				Dict       ,	_lowercase			:				List[Any]      ):



     """simple docstring"""






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







class __snake_case				(						unittest.TestCase		):




   def 	__a						(    self			:				Optional[Any]      ):



     """simple docstring"""






     import hashlib

     SCREAMING_SNAKE_CASE__										=      bytes("""Test String"""       ,	"""utf-8"""      )
     self.assertEqual(SHAaaa(_lowercase      ).hash       ,	hashlib.shaaaa(_lowercase      ).hexdigest()      )





def 	__SCREAMING_SNAKE_CASE  (					)							->					None:







  """simple docstring"""





  import doctest

  doctest.testmod()

  SCREAMING_SNAKE_CASE__										=      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"""	)

  SCREAMING_SNAKE_CASE__										=      parser.parse_args()

  SCREAMING_SNAKE_CASE__										=      args.input_string

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

  print(SHAaaa(__lowerCamelCase	).hash	)


if __name__ == "__main__":
   main()







'''simple docstring'''

import argparse
import json
import os

import fairseq
import torch
from fairseq.data import Dictionary

from transformers import (
    HubertConfig,
    HubertForCTC,
    HubertModel,
    WavaVecaCTCTokenizer,
    WavaVecaFeatureExtractor,
    WavaVecaProcessor,
    logging,
)


logging.set_verbosity_info()
lowercase_					= logging.get_logger(__name__)

lowercase_					= {
    """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""",
    """w2v_encoder.proj""": """lm_head""",
    """mask_emb""": """masked_spec_embed""",
}





def lowerCamelCase						(     __lowerCamelCase :    Dict					,       __lowerCamelCase :    Optional[int]					,       __lowerCamelCase :    int					,       __lowerCamelCase :    Union[str, Any]					,       __lowerCamelCase :    List[Any]    )						->Union[str, Any]:
    for attribute in key.split("""."""    ):
        _SCREAMING_SNAKE_CASE									=			getattr(__lowerCamelCase					,       __lowerCamelCase    )

    if weight_type is not None:
        _SCREAMING_SNAKE_CASE									=			getattr(__lowerCamelCase					,       __lowerCamelCase    ).shape
    else:
        _SCREAMING_SNAKE_CASE									=			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":
        _SCREAMING_SNAKE_CASE									=			value
    elif weight_type == "weight_g":
        _SCREAMING_SNAKE_CASE									=			value
    elif weight_type == "weight_v":
        _SCREAMING_SNAKE_CASE									=			value
    elif weight_type == "bias":
        _SCREAMING_SNAKE_CASE									=			value
    else:
        _SCREAMING_SNAKE_CASE									=			value

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





def lowerCamelCase						(     __lowerCamelCase :    Optional[int]					,       __lowerCamelCase :    Optional[Any]					,       __lowerCamelCase :    List[Any]    )						->Any:
    _SCREAMING_SNAKE_CASE									=			[]
    _SCREAMING_SNAKE_CASE									=			fairseq_model.state_dict()

    _SCREAMING_SNAKE_CASE									=			hf_model.hubert.feature_extractor if is_finetuned else hf_model.feature_extractor

    for name, value in fairseq_dict.items():
        _SCREAMING_SNAKE_CASE									=			False
        if "conv_layers" in name:
            load_conv_layer(
                __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       hf_model.config.feat_extract_norm == """group"""					,       )
            _SCREAMING_SNAKE_CASE									=			True
        else:
            for key, mapped_key in MAPPING.items():
                _SCREAMING_SNAKE_CASE									=			"""hubert.""" + mapped_key if (is_finetuned and mapped_key != """lm_head""") else mapped_key

                if key in name or (key.split("""w2v_model."""    )[-1] == name.split("""."""    )[0] and not is_finetuned):
                    _SCREAMING_SNAKE_CASE									=			True
                    if "*" in mapped_key:
                        _SCREAMING_SNAKE_CASE									=			name.split(__lowerCamelCase    )[0].split("""."""    )[-2]
                        _SCREAMING_SNAKE_CASE									=			mapped_key.replace("""*"""					,       __lowerCamelCase    )
                    if "weight_g" in name:
                        _SCREAMING_SNAKE_CASE									=			"""weight_g"""
                    elif "weight_v" in name:
                        _SCREAMING_SNAKE_CASE									=			"""weight_v"""
                    elif "weight" in name:
                        _SCREAMING_SNAKE_CASE									=			"""weight"""
                    elif "bias" in name:
                        _SCREAMING_SNAKE_CASE									=			"""bias"""
                    else:
                        _SCREAMING_SNAKE_CASE									=			None
                    set_recursively(__lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase    )
                continue
        if not is_used:
            unused_weights.append(__lowerCamelCase    )

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





def lowerCamelCase						(     __lowerCamelCase :    Dict					,       __lowerCamelCase :    Any					,       __lowerCamelCase :    List[Any]					,       __lowerCamelCase :    Optional[int]					,       __lowerCamelCase :    Union[str, Any]    )						->Union[str, Any]:
    _SCREAMING_SNAKE_CASE									=			full_name.split("""conv_layers."""    )[-1]
    _SCREAMING_SNAKE_CASE									=			name.split("""."""    )
    _SCREAMING_SNAKE_CASE									=			int(items[0]    )
    _SCREAMING_SNAKE_CASE									=			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.'
            )
            _SCREAMING_SNAKE_CASE									=			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.'
            )
            _SCREAMING_SNAKE_CASE									=			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."
            )
            _SCREAMING_SNAKE_CASE									=			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.'
            )
            _SCREAMING_SNAKE_CASE									=			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 lowerCamelCase						(     __lowerCamelCase :    str					,       __lowerCamelCase :    str					,       __lowerCamelCase :    List[Any]=None					,       __lowerCamelCase :    Optional[int]=None					,       __lowerCamelCase :    Union[str, Any]=True    )						->Optional[int]:
    if config_path is not None:
        _SCREAMING_SNAKE_CASE									=			HubertConfig.from_pretrained(__lowerCamelCase    )
    else:
        _SCREAMING_SNAKE_CASE									=			HubertConfig()

    if is_finetuned:
        if dict_path:
            _SCREAMING_SNAKE_CASE									=			Dictionary.load(__lowerCamelCase    )

            # important change bos & pad token id since CTC symbol is <pad> and
            # not <s> as in fairseq
            _SCREAMING_SNAKE_CASE									=			target_dict.pad_index
            _SCREAMING_SNAKE_CASE									=			target_dict.bos_index
            _SCREAMING_SNAKE_CASE									=			target_dict.eos_index
            _SCREAMING_SNAKE_CASE									=			len(target_dict.symbols    )
            _SCREAMING_SNAKE_CASE									=			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    )
            with open(__lowerCamelCase					,       """w"""					,       encoding="""utf-8"""    ) as vocab_handle:
                json.dump(target_dict.indices					,       __lowerCamelCase    )
            _SCREAMING_SNAKE_CASE									=			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					,       )
            _SCREAMING_SNAKE_CASE									=			True if config.feat_extract_norm == """layer""" else False
            _SCREAMING_SNAKE_CASE									=			WavaVecaFeatureExtractor(
                feature_size=1					,       sampling_rate=1_6000					,       padding_value=0					,       do_normalize=__lowerCamelCase					,       return_attention_mask=__lowerCamelCase					,       )
            _SCREAMING_SNAKE_CASE									=			WavaVecaProcessor(feature_extractor=__lowerCamelCase					,       tokenizer=__lowerCamelCase    )
            processor.save_pretrained(__lowerCamelCase    )

        _SCREAMING_SNAKE_CASE									=			HubertForCTC(__lowerCamelCase    )
    else:
        _SCREAMING_SNAKE_CASE									=			HubertModel(__lowerCamelCase    )

    if is_finetuned:
        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			fairseq.checkpoint_utils.load_model_ensemble_and_task(
            [checkpoint_path]					,       arg_overrides={"""data""": """/""".join(dict_path.split("""/"""    )[:-1]    )}    )
    else:
        _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE				,       _SCREAMING_SNAKE_CASE									=			fairseq.checkpoint_utils.load_model_ensemble_and_task([checkpoint_path]    )

    _SCREAMING_SNAKE_CASE									=			model[0].eval()

    recursively_load_weights(__lowerCamelCase					,       __lowerCamelCase					,       __lowerCamelCase    )

    hf_wavavec.save_pretrained(__lowerCamelCase    )


if __name__ == "__main__":
      lowercase_					= 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"""
      )
      lowercase_					= parser.parse_args()
      convert_hubert_checkpoint(
          args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.dict_path, not args.not_finetuned
      )



