



"""simple docstring"""





from functools import reduce

lowerCamelCase      =		(
    """73167176531330624919225119674426574742355349194934"""
    """96983520312774506326239578318016984801869478851843"""
    """85861560789112949495459501737958331952853208805511"""
    """12540698747158523863050715693290963295227443043557"""
    """66896648950445244523161731856403098711121722383113"""
    """62229893423380308135336276614282806444486645238749"""
    """30358907296290491560440772390713810515859307960866"""
    """70172427121883998797908792274921901699720888093776"""
    """65727333001053367881220235421809751254540594752243"""
    """52584907711670556013604839586446706324415722155397"""
    """53697817977846174064955149290862569321978468622482"""
    """83972241375657056057490261407972968652414535100474"""
    """82166370484403199890008895243450658541227588666881"""
    """16427171479924442928230863465674813919123162824586"""
    """17866458359124566529476545682848912883142607690042"""
    """24219022671055626321111109370544217506941658960408"""
    """07198403850962455444362981230987879927244284909188"""
    """84580156166097919133875499200524063689912560717606"""
    """05886116467109405077541002256983155200055935729725"""
    """71636269561882670428252483600823257530420752963450"""
)
def      a__			(   lowerCAmelCase__ = N   ):
      return max(
          # mypy cannot properly interpret reduce
          int(reduce(lambda lowerCAmelCase__			,    lowerCAmelCase__   : str(int(lowerCAmelCase__   ) * int(lowerCAmelCase__   )   )			,    n[i : i + 13]   )   )
          for i in range(len(lowerCAmelCase__   ) - 12   )   )


if __name__ == "__main__":
  print(F"{solution() = }")









"""simple docstring"""





import argparse
import re

import torch
from CLAP import create_model

from transformers import AutoFeatureExtractor, ClapConfig, ClapModel


lowerCamelCase      =		{
    """text_branch""": """text_model""",
    """audio_branch""": """audio_model.audio_encoder""",
    """attn""": """attention.self""",
    """self.proj""": """output.dense""",
    """attention.self_mask""": """attn_mask""",
    """mlp.fc1""": """intermediate.dense""",
    """mlp.fc2""": """output.dense""",
    """norm1""": """layernorm_before""",
    """norm2""": """layernorm_after""",
    """bn0""": """batch_norm""",
}

lowerCamelCase      =		AutoFeatureExtractor.from_pretrained("""laion/clap-htsat-unfused""", truncation="""rand_trunc""")
def      a__			(   lowerCAmelCase__			,    lowerCAmelCase__=False   ):
      UpperCAmelCase_				,					UpperCAmelCase_    				=							create_model(
          "HTSAT-tiny"			,    "roberta"			,    lowerCAmelCase__			,    precision="fp32"			,    device="cuda:0" if torch.cuda.is_available() else "cpu"			,    enable_fusion=lowerCAmelCase__			,    fusion_type="aff_2d" if enable_fusion else None			,    )
      return model, model_cfg
def      a__			(   lowerCAmelCase__   ):
      UpperCAmelCase_    				=							{}

      UpperCAmelCase_    				=							r".*sequential.(\d+).*"
      UpperCAmelCase_    				=							r".*_projection.(\d+).*"

      for key, value in state_dict.items():
            # check if any key needs to be modified
            for key_to_modify, new_key in KEYS_TO_MODIFY_MAPPING.items():
                  if key_to_modify in key:
                        UpperCAmelCase_    				=							key.replace(lowerCAmelCase__			,    lowerCAmelCase__   )

            if re.match(lowerCAmelCase__			,    lowerCAmelCase__   ):
                  # replace sequential layers with list
                  UpperCAmelCase_    				=							re.match(lowerCAmelCase__			,    lowerCAmelCase__   ).group(1   )

                  UpperCAmelCase_    				=							key.replace(f"""sequential.{sequential_layer}."""			,    f"""layers.{int(lowerCAmelCase__   )//3}.linear."""   )
            elif re.match(lowerCAmelCase__			,    lowerCAmelCase__   ):
                  UpperCAmelCase_    				=							int(re.match(lowerCAmelCase__			,    lowerCAmelCase__   ).group(1   )   )

                  # Because in CLAP they use `nn.Sequential`...
                  UpperCAmelCase_    				=							1 if projecton_layer == 0 else 2

                  UpperCAmelCase_    				=							key.replace(f"""_projection.{projecton_layer}."""			,    f"""_projection.linear{transformers_projection_layer}."""   )

            if "audio" and "qkv" in key:
                  # split qkv into query key and value
                  UpperCAmelCase_    				=							value
                  UpperCAmelCase_    				=							mixed_qkv.size(0   ) // 3

                  UpperCAmelCase_    				=							mixed_qkv[:qkv_dim]
                  UpperCAmelCase_    				=							mixed_qkv[qkv_dim : qkv_dim * 2]
                  UpperCAmelCase_    				=							mixed_qkv[qkv_dim * 2 :]

                  UpperCAmelCase_    				=							query_layer
                  UpperCAmelCase_    				=							key_layer
                  UpperCAmelCase_    				=							value_layer
            else:
                  UpperCAmelCase_    				=							value

      return model_state_dict
def      a__			(   lowerCAmelCase__			,    lowerCAmelCase__			,    lowerCAmelCase__			,    lowerCAmelCase__=False   ):
      UpperCAmelCase_				,					UpperCAmelCase_    				=							init_clap(lowerCAmelCase__			,    enable_fusion=lowerCAmelCase__   )

      clap_model.eval()
      UpperCAmelCase_    				=							clap_model.state_dict()
      UpperCAmelCase_    				=							rename_state_dict(lowerCAmelCase__   )

      UpperCAmelCase_    				=							ClapConfig()
      UpperCAmelCase_    				=							enable_fusion
      UpperCAmelCase_    				=							ClapModel(lowerCAmelCase__   )

      # ignore the spectrogram embedding layer
      model.load_state_dict(lowerCAmelCase__			,    strict=lowerCAmelCase__   )

      model.save_pretrained(lowerCAmelCase__   )
      transformers_config.save_pretrained(lowerCAmelCase__   )


if __name__ == "__main__":
  lowerCamelCase      =		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("""--config_path""", default=None, type=str, help="""Path to hf config.json of model to convert""")
  parser.add_argument("""--enable_fusion""", action="""store_true""", help="""Whether to enable fusion or not""")
  lowerCamelCase      =		parser.parse_args()

  convert_clap_checkpoint(args.checkpoint_path, args.pytorch_dump_folder_path, args.config_path, args.enable_fusion)




