




'''simple docstring'''


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


_snake_case				:				int       							=		{
    '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 		snake_case_   (UpperCamelCase	:  Dict				):





							'''simple docstring'''


							_a     					=    ['''layers''', '''blocks''']
							for k in ignore_keys:
														state_dict.pop(UpperCamelCase			,						UpperCamelCase				)


_snake_case				:				Union[str, Any]       							=		{
    '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 		snake_case_   (UpperCamelCase	:  Dict				):





							'''simple docstring'''


							_a     					=    list(s_dict.keys()				)
							for key in keys:
														_a     					=    key
														for k, v in WHISPER_MAPPING.items():
																					if k in key:
																												_a     					=    new_key.replace(UpperCamelCase			,						UpperCamelCase				)

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

														_a     					=    s_dict.pop(UpperCamelCase				)
							return s_dict
def 		snake_case_   (UpperCamelCase	:  Tuple				):





							'''simple docstring'''


							_a					,							_a     					=    emb.weight.shape
							_a     					=    nn.Linear(UpperCamelCase			,						UpperCamelCase			,						bias=UpperCamelCase				)
							_a     					=    emb.weight.data
							return lin_layer
def 		snake_case_   (UpperCamelCase	:  str			,						UpperCamelCase	:  str				):





							'''simple docstring'''


							os.makedirs(UpperCamelCase			,						exist_ok=UpperCamelCase				)
							_a     					=    os.path.basename(UpperCamelCase				)

							_a     					=    url.split('''/'''				)[-2]
							_a     					=    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				):
														_a     					=    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=1024				) as loop:
																					while True:
																												_a     					=    source.read(8192				)
																												if not buffer:
																																			break

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

							_a     					=    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 		snake_case_   (UpperCamelCase	:  Optional[int]			,						UpperCamelCase	:  Optional[Any]				):





							'''simple docstring'''


							if ".pt" not in checkpoint_path:
														_a     					=    _download(_MODELS[checkpoint_path]				)
							else:
														_a     					=    torch.load(UpperCamelCase			,						map_location='''cpu'''				)
							_a     					=    original_checkpoint['''dims''']
							_a     					=    original_checkpoint['''model_state_dict''']
							_a     					=    state_dict['''decoder.token_embedding.weight''']
							remove_ignore_keys_(UpperCamelCase				)
							rename_keys(UpperCamelCase				)
							_a     					=    True
							_a     					=    state_dict['''decoder.layers.0.fc1.weight'''].shape[0]

							_a     					=    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''']			,						)

							_a     					=    WhisperForConditionalGeneration(UpperCamelCase				)
							_a					,							_a     					=    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:
														_a     					=    make_linear_from_emb(model.model.decoder.embed_tokens				)
							else:
														_a     					=    proj_out_weights

							model.save_pretrained(UpperCamelCase				)


if __name__ == "__main__":
				_snake_case				:				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.')
				_snake_case				:				List[str]       							=		parser.parse_args()

				convert_openai_whisper_to_tfms(args.checkpoint_path, args.pytorch_dump_folder_path)







'''simple docstring'''


import copy
import random

from transformers import CLIPTokenizer

class       A   (		_a   ):







							def __init__(  self	:  str							,      *lowerCAmelCase_	:  int							,      **lowerCAmelCase_	:  List[str] )      ->							List[Any]:


														"""simple docstring"""


														super().__init__(*lowerCAmelCase_							,      **lowerCAmelCase_ )
														_a     					=    {}







							def        __lowerCAmelCase      (  self	:  Any							,      lowerCAmelCase_	:  Tuple							,      *lowerCAmelCase_	:  Any							,      **lowerCAmelCase_	:  Tuple )      ->							str:


														"""simple docstring"""


														_a     					=    super().add_tokens(lowerCAmelCase_							,      *lowerCAmelCase_							,      **lowerCAmelCase_ )
														if num_added_tokens == 0:
																					raise ValueError(
																					    F'The tokenizer already contains the token {placeholder_token}. Please pass a different'
																					    ''' `placeholder_token` that is not already in the tokenizer.''' )







							def        __lowerCAmelCase      (  self	:  Optional[Any]							,      lowerCAmelCase_	:  Dict							,      *lowerCAmelCase_	:  Tuple							,      lowerCAmelCase_	:  Tuple=1							,      **lowerCAmelCase_	:  int )      ->							Any:


														"""simple docstring"""


														_a     					=    []
														if num_vec_per_token == 1:
																					self.try_adding_tokens(lowerCAmelCase_							,      *lowerCAmelCase_							,      **lowerCAmelCase_ )
																					output.append(lowerCAmelCase_ )
														else:
																					_a     					=    []
																					for i in range(lowerCAmelCase_ ):
																												_a     					=    placeholder_token + F'_{i}'
																												self.try_adding_tokens(lowerCAmelCase_							,      *lowerCAmelCase_							,      **lowerCAmelCase_ )
																												output.append(lowerCAmelCase_ )
        # handle cases where there is a new placeholder token that contains the current placeholder token but is larger
														for token in self.token_map:
																					if token in placeholder_token:
																												raise ValueError(
																												    F'The tokenizer already has placeholder token {token} that can get confused with'
																												    F' {placeholder_token}keep placeholder tokens independent' )
														_a     					=    output







							def        __lowerCAmelCase      (  self	:  Dict							,      lowerCAmelCase_	:  List[Any]							,      lowerCAmelCase_	:  Tuple=False							,      lowerCAmelCase_	:  Optional[int]=1.0 )      ->							Tuple:


														"""simple docstring"""


														if isinstance(lowerCAmelCase_							,      lowerCAmelCase_ ):
																					_a     					=    []
																					for i in range(len(lowerCAmelCase_ ) ):
																												output.append(self.replace_placeholder_tokens_in_text(text[i]							,      vector_shuffle=lowerCAmelCase_ ) )
																					return output
														for placeholder_token in self.token_map:
																					if placeholder_token in text:
																												_a     					=    self.token_map[placeholder_token]
																												_a     					=    tokens[: 1 + int(len(lowerCAmelCase_ ) * prop_tokens_to_load )]
																												if vector_shuffle:
																																			_a     					=    copy.copy(lowerCAmelCase_ )
																																			random.shuffle(lowerCAmelCase_ )
																												_a     					=    text.replace(lowerCAmelCase_							,      ''' '''.join(lowerCAmelCase_ ) )
														return text







							def __call__(  self	:  List[str]							,      lowerCAmelCase_	:  str							,      *lowerCAmelCase_	:  int							,      lowerCAmelCase_	:  Optional[int]=False							,      lowerCAmelCase_	:  Union[str, Any]=1.0							,      **lowerCAmelCase_	:  Union[str, Any] )      ->							Union[str, Any]:


														"""simple docstring"""


														return super().__call__(
														    self.replace_placeholder_tokens_in_text(
														        lowerCAmelCase_							,      vector_shuffle=lowerCAmelCase_							,      prop_tokens_to_load=lowerCAmelCase_ )							,      *lowerCAmelCase_							,      **lowerCAmelCase_							,      )







							def        __lowerCAmelCase      (  self	:  List[str]							,      lowerCAmelCase_	:  int							,      *lowerCAmelCase_	:  str							,      lowerCAmelCase_	:  Any=False							,      lowerCAmelCase_	:  List[Any]=1.0							,      **lowerCAmelCase_	:  Union[str, Any] )      ->							Dict:


														"""simple docstring"""


														return super().encode(
														    self.replace_placeholder_tokens_in_text(
														        lowerCAmelCase_							,      vector_shuffle=lowerCAmelCase_							,      prop_tokens_to_load=lowerCAmelCase_ )							,      *lowerCAmelCase_							,      **lowerCAmelCase_							,      )

