


import argparse
import os
import re


__UpperCamelCase    :						str     =							'src/diffusers'

# Pattern that looks at the indentation in a line.
__UpperCamelCase    :						Optional[int]     =							re.compile(R'^(\s*)\S')
# Pattern that matches `"key":" and puts `key` in group 0.
__UpperCamelCase    :						Optional[int]     =							re.compile(R'^\s*"([^"]+)":')
# Pattern that matches `_import_structure["key"]` and puts `key` in group 0.
__UpperCamelCase    :						Tuple     =							re.compile(R'^\s*_import_structure\["([^"]+)"\]')
# Pattern that matches `"key",` and puts `key` in group 0.
__UpperCamelCase    :						Dict     =							re.compile(R'^\s*"([^"]+)",\s*$')
# Pattern that matches any `[stuff]` and puts `stuff` in group 0.
__UpperCamelCase    :						Optional[int]     =							re.compile(R'\[([^\]]+)\]')





def   _UpperCAmelCase       (     UpperCAmelCase      :			str      ):
      """simple docstring"""




      __lowerCamelCase     :      Union[str, Any]       						=						_re_indent.search(UpperCAmelCase      )
      return "" if search is None else search.groups()[0]





def   _UpperCAmelCase       (     UpperCAmelCase      :			Optional[Any]						,	UpperCAmelCase      :			List[Any]=""						,	UpperCAmelCase      :			str=None						,	UpperCAmelCase      :			List[Any]=None      ):
      """simple docstring"""




      __lowerCamelCase     :      Optional[int]       						=						0
      __lowerCamelCase     :      Union[str, Any]       						=						code.split("""\n"""      )
      if start_prompt is not None:
            while not lines[index].startswith(UpperCAmelCase      ):
                  index += 1
            __lowerCamelCase     :      int       						=						["""\n""".join(lines[:index]      )]
      else:
            __lowerCamelCase     :      Union[str, Any]       						=						[]

      # We split into blocks until we get to the `end_prompt` (or the end of the block).
      __lowerCamelCase     :      List[str]       						=						[lines[index]]
      index += 1
      while index < len(UpperCAmelCase      ) and (end_prompt is None or not lines[index].startswith(UpperCAmelCase      )):
            if len(lines[index]      ) > 0 and get_indent(lines[index]      ) == indent_level:
                  if len(UpperCAmelCase      ) > 0 and get_indent(current_block[-1]      ).startswith(indent_level + """ """      ):
                        current_block.append(lines[index]      )
                        blocks.append("""\n""".join(UpperCAmelCase      )      )
                        if index < len(UpperCAmelCase      ) - 1:
                              __lowerCamelCase     :      List[str]       						=						[lines[index + 1]]
                              index += 1
                        else:
                              __lowerCamelCase     :      Union[str, Any]       						=						[]
                  else:
                        blocks.append("""\n""".join(UpperCAmelCase      )      )
                        __lowerCamelCase     :      str       						=						[lines[index]]
            else:
                  current_block.append(lines[index]      )
            index += 1

      # Adds current block if it's nonempty.
      if len(UpperCAmelCase      ) > 0:
            blocks.append("""\n""".join(UpperCAmelCase      )      )

      # Add final block after end_prompt if provided.
      if end_prompt is not None and index < len(UpperCAmelCase      ):
            blocks.append("""\n""".join(lines[index:]      )      )

      return blocks





def   _UpperCAmelCase       (     UpperCAmelCase      :			int      ):
      """simple docstring"""




      def _inner(UpperCAmelCase      :			List[str]      ):
            return key(UpperCAmelCase      ).lower().replace("""_"""						,	""""""      )

      return _inner





def   _UpperCAmelCase       (     UpperCAmelCase      :			Dict						,	UpperCAmelCase      :			Any=None      ):
      """simple docstring"""




      def noop(UpperCAmelCase      :			int      ):
            return x

      if key is None:
            __lowerCamelCase     :      Optional[int]       						=						noop
      # Constants are all uppercase, they go first.
      __lowerCamelCase     :      List[Any]       						=						[obj for obj in objects if key(UpperCAmelCase      ).isupper()]
      # Classes are not all uppercase but start with a capital, they go second.
      __lowerCamelCase     :      List[str]       						=						[obj for obj in objects if key(UpperCAmelCase      )[0].isupper() and not key(UpperCAmelCase      ).isupper()]
      # Functions begin with a lowercase, they go last.
      __lowerCamelCase     :      str       						=						[obj for obj in objects if not key(UpperCAmelCase      )[0].isupper()]

      __lowerCamelCase     :      Optional[int]       						=						ignore_underscore(UpperCAmelCase      )
      return sorted(UpperCAmelCase						,	key=UpperCAmelCase      ) + sorted(UpperCAmelCase						,	key=UpperCAmelCase      ) + sorted(UpperCAmelCase						,	key=UpperCAmelCase      )





def   _UpperCAmelCase       (     UpperCAmelCase      :			int      ):
      """simple docstring"""




      def _replace(UpperCAmelCase      :			Tuple      ):
            __lowerCamelCase     :      int       						=						match.groups()[0]
            if "," not in imports:
                  return f"""[{imports}]"""
            __lowerCamelCase     :      Optional[Any]       						=						[part.strip().replace("""\""""						,	""""""      ) for part in imports.split(""","""      )]
            # We will have a final empty element if the line finished with a comma.
            if len(keys[-1]      ) == 0:
                  __lowerCamelCase     :      Tuple       						=						keys[:-1]
            return "[" + ", ".join([f"""\"{k}\"""" for k in sort_objects(UpperCAmelCase      )]      ) + "]"

      __lowerCamelCase     :      Dict       						=						import_statement.split("""\n"""      )
      if len(UpperCAmelCase      ) > 3:
            # Here we have to sort internal imports that are on several lines (one per name):
            # key: [
            #     "object1",
            #     "object2",
            #     ...
            # ]

            # We may have to ignore one or two lines on each side.
            __lowerCamelCase     :      int       						=						2 if lines[1].strip() == """[""" else 1
            __lowerCamelCase     :      Any       						=						[(i, _re_strip_line.search(UpperCAmelCase      ).groups()[0]) for i, line in enumerate(lines[idx:-idx]      )]
            __lowerCamelCase     :      int       						=						sort_objects(UpperCAmelCase						,	key=lambda UpperCAmelCase      : x[1]      )
            __lowerCamelCase     :      List[Any]       						=						[lines[x[0] + idx] for x in sorted_indices]
            return "\n".join(lines[:idx] + sorted_lines + lines[-idx:]      )
      elif len(UpperCAmelCase      ) == 3:
            # Here we have to sort internal imports that are on one separate line:
            # key: [
            #     "object1", "object2", ...
            # ]
            if _re_bracket_content.search(lines[1]      ) is not None:
                  __lowerCamelCase     :      Dict       						=						_re_bracket_content.sub(_replace						,	lines[1]      )
            else:
                  __lowerCamelCase     :      int       						=						[part.strip().replace("""\""""						,	""""""      ) for part in lines[1].split(""","""      )]
                  # We will have a final empty element if the line finished with a comma.
                  if len(keys[-1]      ) == 0:
                        __lowerCamelCase     :      List[Any]       						=						keys[:-1]
                  __lowerCamelCase     :      Optional[Any]       						=						get_indent(lines[1]      ) + """, """.join([f"""\"{k}\"""" for k in sort_objects(UpperCAmelCase      )]      )
            return "\n".join(UpperCAmelCase      )
      else:
            # Finally we have to deal with imports fitting on one line
            __lowerCamelCase     :      Optional[int]       						=						_re_bracket_content.sub(_replace						,	UpperCAmelCase      )
            return import_statement





def   _UpperCAmelCase       (     UpperCAmelCase      :			Tuple						,	UpperCAmelCase      :			Tuple=True      ):
      """simple docstring"""




      with open(UpperCAmelCase						,	"""r"""      ) as f:
            __lowerCamelCase     :      List[Any]       						=						f.read()

      if "_import_structure" not in code:
            return

      # Blocks of indent level 0
      __lowerCamelCase     :      Optional[Any]       						=						split_code_in_indented_blocks(
          UpperCAmelCase						,	start_prompt="""_import_structure = {"""						,	end_prompt="""if TYPE_CHECKING:"""      )

      # We ignore block 0 (everything until start_prompt) and the last block (everything after end_prompt).
      for block_idx in range(1						,	len(UpperCAmelCase      ) - 1      ):
            # Check if the block contains some `_import_structure`s thingy to sort.
            __lowerCamelCase     :      Optional[int]       						=						main_blocks[block_idx]
            __lowerCamelCase     :      Union[str, Any]       						=						block.split("""\n"""      )

            # Get to the start of the imports.
            __lowerCamelCase     :      List[str]       						=						0
            while line_idx < len(UpperCAmelCase      ) and "_import_structure" not in block_lines[line_idx]:
                  # Skip dummy import blocks
                  if "import dummy" in block_lines[line_idx]:
                        __lowerCamelCase     :      str       						=						len(UpperCAmelCase      )
                  else:
                        line_idx += 1
            if line_idx >= len(UpperCAmelCase      ):
                  continue

            # Ignore beginning and last line: they don't contain anything.
            __lowerCamelCase     :      List[Any]       						=						"""\n""".join(block_lines[line_idx:-1]      )
            __lowerCamelCase     :      Optional[int]       						=						get_indent(block_lines[1]      )
            # Slit the internal block into blocks of indent level 1.
            __lowerCamelCase     :      List[str]       						=						split_code_in_indented_blocks(UpperCAmelCase						,	indent_level=UpperCAmelCase      )
            # We have two categories of import key: list or _import_structure[key].append/extend
            __lowerCamelCase     :      Optional[int]       						=						_re_direct_key if """_import_structure""" in block_lines[0] else _re_indirect_key
            # Grab the keys, but there is a trap: some lines are empty or just comments.
            __lowerCamelCase     :      Dict       						=						[(pattern.search(UpperCAmelCase      ).groups()[0] if pattern.search(UpperCAmelCase      ) is not None else None) for b in internal_blocks]
            # We only sort the lines with a key.
            __lowerCamelCase     :      List[Any]       						=						[(i, key) for i, key in enumerate(UpperCAmelCase      ) if key is not None]
            __lowerCamelCase     :      Any       						=						[x[0] for x in sorted(UpperCAmelCase						,	key=lambda UpperCAmelCase      : x[1]      )]

            # We reorder the blocks by leaving empty lines/comments as they were and reorder the rest.
            __lowerCamelCase     :      Any       						=						0
            __lowerCamelCase     :      Union[str, Any]       						=						[]
            for i in range(len(UpperCAmelCase      )      ):
                  if keys[i] is None:
                        reordered_blocks.append(internal_blocks[i]      )
                  else:
                        __lowerCamelCase     :      List[Any]       						=						sort_objects_in_import(internal_blocks[sorted_indices[count]]      )
                        reordered_blocks.append(UpperCAmelCase      )
                        count += 1

        # And we put our main block back together with its first and last line.
            __lowerCamelCase     :      Tuple       						=						"""\n""".join(block_lines[:line_idx] + reordered_blocks + [block_lines[-1]]      )

      if code != "\n".join(UpperCAmelCase      ):
            if check_only:
                  return True
            else:
                  print(f"""Overwriting {file}."""      )
                  with open(UpperCAmelCase						,	"""w"""      ) as f:
                        f.write("""\n""".join(UpperCAmelCase      )      )





def   _UpperCAmelCase       (     UpperCAmelCase      :			Tuple=True      ):
      """simple docstring"""




      __lowerCamelCase     :      Optional[Any]       						=						[]
      for root, _, files in os.walk(UpperCAmelCase      ):
            if "__init__.py" in files:
                  __lowerCamelCase     :      str       						=						sort_imports(os.path.join(UpperCAmelCase						,	"""__init__.py"""      )						,	check_only=UpperCAmelCase      )
                  if result:
                        __lowerCamelCase     :      Dict       						=						[os.path.join(UpperCAmelCase						,	"""__init__.py"""      )]
      if len(UpperCAmelCase      ) > 0:
            raise ValueError(f"""Would overwrite {len(UpperCAmelCase      )} files, run `make style`."""      )


if __name__ == "__main__":
    __UpperCamelCase    :						int     =							argparse.ArgumentParser()
    parser.add_argument('--check_only', action='store_true', help='Whether to only check or fix style.')
    __UpperCamelCase    :						Any     =							parser.parse_args()

    sort_imports_in_all_inits(check_only=args.check_only)







import argparse
import hashlib
import os
import urllib
import warnings

import torch
from torch import nn
from tqdm import tqdm

from transformers import WhisperConfig, WhisperForConditionalGeneration


__UpperCamelCase    :						Tuple     =							{
    'tiny.en': 'https://openaipublic.azureedge.net/main/whisper/models/d3dd57d32accea0b295c96e26691aa14d8822fac7d9d27d5dc00b4ca2826dd03/tiny.en.pt',
    'tiny': 'https://openaipublic.azureedge.net/main/whisper/models/65147644a518d12f04e32d6f3b26facc3f8dd46e5390956a9424a650c0ce22b9/tiny.pt',
    'base.en': 'https://openaipublic.azureedge.net/main/whisper/models/25a8566e1d0c1e2231d1c762132cd20e0f96a85d16145c3a00adf5d1ac670ead/base.en.pt',
    'base': 'https://openaipublic.azureedge.net/main/whisper/models/ed3a0b6b1c0edf879ad9b11b1af5a0e6ab5db9205f891f668f8b0e6c6326e34e/base.pt',
    'small.en': 'https://openaipublic.azureedge.net/main/whisper/models/f953ad0fd29cacd07d5a9eda5624af0f6bcf2258be67c92b79389873d91e0872/small.en.pt',
    'small': 'https://openaipublic.azureedge.net/main/whisper/models/9ecf779972d90ba49c06d968637d720dd632c55bbf19d441fb42bf17a411e794/small.pt',
    'medium.en': 'https://openaipublic.azureedge.net/main/whisper/models/d7440d1dc186f76616474e0ff0b3b6b879abc9d1a4926b7adfa41db2d497ab4f/medium.en.pt',
    'medium': 'https://openaipublic.azureedge.net/main/whisper/models/345ae4da62f9b3d59415adc60127b97c714f32e89e936602e85993674d08dcb1/medium.pt',
    'large': 'https://openaipublic.azureedge.net/main/whisper/models/e4b87e7e0bf463eb8e6956e646f1e277e901512310def2c24bf0e11bd3c28e9a/large.pt',
    'large-v2': 'https://openaipublic.azureedge.net/main/whisper/models/81f7c96c852ee8fc832187b0132e569d6c3065a3252ed18e56effd0b6a73e524/large-v2.pt',
}





def   _UpperCAmelCase       (     UpperCAmelCase      :			Dict      ):
      """simple docstring"""




      __lowerCamelCase     :      int       						=						["""layers""", """blocks"""]
      for k in ignore_keys:
            state_dict.pop(UpperCAmelCase						,	UpperCAmelCase      )


__UpperCamelCase    :						str     =							{
    'blocks': 'layers',
    'mlp.0': 'fc1',
    'mlp.2': 'fc2',
    'mlp_ln': 'final_layer_norm',
    '.attn.query': '.self_attn.q_proj',
    '.attn.key': '.self_attn.k_proj',
    '.attn.value': '.self_attn.v_proj',
    '.attn_ln': '.self_attn_layer_norm',
    '.attn.out': '.self_attn.out_proj',
    '.cross_attn.query': '.encoder_attn.q_proj',
    '.cross_attn.key': '.encoder_attn.k_proj',
    '.cross_attn.value': '.encoder_attn.v_proj',
    '.cross_attn_ln': '.encoder_attn_layer_norm',
    '.cross_attn.out': '.encoder_attn.out_proj',
    'decoder.ln.': 'decoder.layer_norm.',
    'encoder.ln.': 'encoder.layer_norm.',
    'token_embedding': 'embed_tokens',
    'encoder.positional_embedding': 'encoder.embed_positions.weight',
    'decoder.positional_embedding': 'decoder.embed_positions.weight',
    'ln_post': 'layer_norm',
}





def   _UpperCAmelCase       (     UpperCAmelCase      :			Dict      ):
      """simple docstring"""




      __lowerCamelCase     :      List[str]       						=						list(s_dict.keys()      )
      for key in keys:
            __lowerCamelCase     :      Union[str, Any]       						=						key
            for k, v in WHISPER_MAPPING.items():
                  if k in key:
                        __lowerCamelCase     :      Optional[int]       						=						new_key.replace(UpperCAmelCase						,	UpperCAmelCase      )

            print(f"""{key} -> {new_key}"""      )

            __lowerCamelCase     :      Dict       						=						s_dict.pop(UpperCAmelCase      )
      return s_dict





def   _UpperCAmelCase       (     UpperCAmelCase      :			Optional[int]      ):
      """simple docstring"""




      __lowerCamelCase						, __lowerCamelCase     :      Dict       						=						emb.weight.shape
      __lowerCamelCase     :      Tuple       						=						nn.Linear(UpperCAmelCase						,	UpperCAmelCase						,	bias=UpperCAmelCase      )
      __lowerCamelCase     :      Union[str, Any]       						=						emb.weight.data
      return lin_layer





def   _UpperCAmelCase       (     UpperCAmelCase      :			str						,	UpperCAmelCase      :			str      ):
      """simple docstring"""




      os.makedirs(UpperCAmelCase						,	exist_ok=UpperCAmelCase      )
      __lowerCamelCase     :      Any       						=						os.path.basename(UpperCAmelCase      )

      __lowerCamelCase     :      int       						=						url.split("""/"""      )[-2]
      __lowerCamelCase     :      str       						=						os.path.join(UpperCAmelCase						,	UpperCAmelCase      )

      if os.path.exists(UpperCAmelCase      ) and not os.path.isfile(UpperCAmelCase      ):
            raise RuntimeError(f"""{download_target} exists and is not a regular file"""      )

      if os.path.isfile(UpperCAmelCase      ):
            __lowerCamelCase     :      Any       						=						open(UpperCAmelCase						,	"""rb"""      ).read()
            if hashlib.shaaaa(UpperCAmelCase      ).hexdigest() == expected_shaaaa:
                  return model_bytes
            else:
                  warnings.warn(f"""{download_target} exists, but the SHA256 checksum does not match; re-downloading the file"""      )

      with urllib.request.urlopen(UpperCAmelCase      ) as source, open(UpperCAmelCase						,	"""wb"""      ) as output:
            with tqdm(
                total=int(source.info().get("""Content-Length"""      )      )						,	ncols=80						,	unit="""iB"""						,	unit_scale=UpperCAmelCase						,	unit_divisor=1_024      ) as loop:
                  while True:
                        __lowerCamelCase     :      Union[str, Any]       						=						source.read(8_192      )
                        if not buffer:
                              break

                        output.write(UpperCAmelCase      )
                        loop.update(len(UpperCAmelCase      )      )

      __lowerCamelCase     :      List[str]       						=						open(UpperCAmelCase						,	"""rb"""      ).read()
      if hashlib.shaaaa(UpperCAmelCase      ).hexdigest() != expected_shaaaa:
            raise RuntimeError(
                """Model has been downloaded but the SHA256 checksum does not not match. Please retry loading the model."""      )

      return model_bytes





def   _UpperCAmelCase       (     UpperCAmelCase      :			List[Any]						,	UpperCAmelCase      :			Optional[int]      ):
      """simple docstring"""




      if ".pt" not in checkpoint_path:
            __lowerCamelCase     :      Optional[int]       						=						_download(_MODELS[checkpoint_path]      )
      else:
            __lowerCamelCase     :      Union[str, Any]       						=						torch.load(UpperCAmelCase						,	map_location="""cpu"""      )
      __lowerCamelCase     :      Any       						=						original_checkpoint["""dims"""]
      __lowerCamelCase     :      int       						=						original_checkpoint["""model_state_dict"""]
      __lowerCamelCase     :      Tuple       						=						state_dict["""decoder.token_embedding.weight"""]
      remove_ignore_keys_(UpperCAmelCase      )
      rename_keys(UpperCAmelCase      )
      __lowerCamelCase     :      int       						=						True
      __lowerCamelCase     :      Tuple       						=						state_dict["""decoder.layers.0.fc1.weight"""].shape[0]

      __lowerCamelCase     :      Any       						=						WhisperConfig(
          vocab_size=dimensions["""n_vocab"""]						,	encoder_ffn_dim=UpperCAmelCase						,	decoder_ffn_dim=UpperCAmelCase						,	num_mel_bins=dimensions["""n_mels"""]						,	d_model=dimensions["""n_audio_state"""]						,	max_target_positions=dimensions["""n_text_ctx"""]						,	encoder_layers=dimensions["""n_audio_layer"""]						,	encoder_attention_heads=dimensions["""n_audio_head"""]						,	decoder_layers=dimensions["""n_text_layer"""]						,	decoder_attention_heads=dimensions["""n_text_state"""]						,	max_source_positions=dimensions["""n_audio_ctx"""]						,	)

      __lowerCamelCase     :      List[str]       						=						WhisperForConditionalGeneration(UpperCAmelCase      )
      __lowerCamelCase						, __lowerCamelCase     :      str       						=						model.model.load_state_dict(UpperCAmelCase						,	strict=UpperCAmelCase      )
      if len(UpperCAmelCase      ) > 0 and not set(UpperCAmelCase      ) <= {
          "encoder.embed_positions.weights",
          "decoder.embed_positions.weights",
      }:
            raise ValueError(
                """Only `encoder.embed_positions.weights` and `decoder.embed_positions.weights`  are allowed to be missing,"""
                f""" but all the following weights are missing {missing}"""      )

      if tie_embeds:
            __lowerCamelCase     :      Tuple       						=						make_linear_from_emb(model.model.decoder.embed_tokens      )
      else:
            __lowerCamelCase     :      Tuple       						=						proj_out_weights

      model.save_pretrained(UpperCAmelCase      )


if __name__ == "__main__":
    __UpperCamelCase    :						Union[str, Any]     =							argparse.ArgumentParser()
    # # Required parameters
    parser.add_argument('--checkpoint_path', type=str, help='Patht to the downloaded checkpoints')
    parser.add_argument('--pytorch_dump_folder_path', default=None, type=str, help='Path to the output PyTorch model.')
    __UpperCamelCase    :						Tuple     =							parser.parse_args()

    convert_openai_whisper_to_tfms(args.checkpoint_path, args.pytorch_dump_folder_path)



