




'''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


A									={
    '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_							(_a			:							Tuple  ):
    UpperCAmelCase     =	['''layers''', '''blocks''']
    for k in ignore_keys:
        state_dict.pop(_a					,     _a  )


A									={
    '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_							(_a			:							List[Any]  ):
    UpperCAmelCase     =	list(s_dict.keys()  )
    for key in keys:
        UpperCAmelCase     =	key
        for k, v in WHISPER_MAPPING.items():
            if k in key:
                UpperCAmelCase     =	new_key.replace(_a					,     _a  )

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

        UpperCAmelCase     =	s_dict.pop(_a  )
    return s_dict





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





def 		snake_case_							(_a			:							str					,     _a			:							str  ):
    os.makedirs(_a					,     exist_ok=_a  )
    UpperCAmelCase     =	os.path.basename(_a  )

    UpperCAmelCase     =	url.split('''/'''  )[-2]
    UpperCAmelCase     =	os.path.join(_a					,     _a  )

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

    if os.path.isfile(_a  ):
        UpperCAmelCase     =	open(_a					,     '''rb'''  ).read()
        if hashlib.shaaaa(_a  ).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(_a  ) as source, open(_a					,     '''wb'''  ) as output:
        with tqdm(
            total=int(source.info().get('''Content-Length'''  )  )					,     ncols=8_0					,     unit='''iB'''					,     unit_scale=_a					,     unit_divisor=1_0_2_4  ) as loop:
            while True:
                UpperCAmelCase     =	source.read(8_1_9_2  )
                if not buffer:
                    break

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

    UpperCAmelCase     =	open(_a					,     '''rb'''  ).read()
    if hashlib.shaaaa(_a  ).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_							(_a			:							int					,     _a			:							Optional[Any]  ):
    if ".pt" not in checkpoint_path:
        UpperCAmelCase     =	_download(_MODELS[checkpoint_path]  )
    else:
        UpperCAmelCase     =	torch.load(_a					,     map_location='''cpu'''  )
    UpperCAmelCase     =	original_checkpoint['''dims''']
    UpperCAmelCase     =	original_checkpoint['''model_state_dict''']
    UpperCAmelCase     =	state_dict['''decoder.token_embedding.weight''']
    remove_ignore_keys_(_a  )
    rename_keys(_a  )
    UpperCAmelCase     =	True
    UpperCAmelCase     =	state_dict['''decoder.layers.0.fc1.weight'''].shape[0]

    UpperCAmelCase     =	WhisperConfig(
        vocab_size=dimensions['''n_vocab''']					,     encoder_ffn_dim=_a					,     decoder_ffn_dim=_a					,     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''']					,     )

    UpperCAmelCase     =	WhisperForConditionalGeneration(_a  )
    UpperCAmelCase ,  UpperCAmelCase     =	model.model.load_state_dict(_a					,     strict=_a  )
    if len(_a  ) > 0 and not set(_a  ) <= {
        "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:
        UpperCAmelCase     =	make_linear_from_emb(model.model.decoder.embed_tokens  )
    else:
        UpperCAmelCase     =	proj_out_weights

    model.save_pretrained(_a  )


if __name__ == "__main__":
   A									=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.')
   A									=parser.parse_args()

   convert_openai_whisper_to_tfms(args.checkpoint_path, args.pytorch_dump_folder_path)










'''simple docstring'''







import unittest

import numpy as np

from transformers import RobertaPreLayerNormConfig, is_flax_available
from transformers.testing_utils import require_flax, slow

from ...test_modeling_flax_common import FlaxModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask


if is_flax_available():
   import jax.numpy as jnp

   from transformers.models.roberta_prelayernorm.modeling_flax_roberta_prelayernorm import (
       FlaxRobertaPreLayerNormForCausalLM,
       FlaxRobertaPreLayerNormForMaskedLM,
       FlaxRobertaPreLayerNormForMultipleChoice,
       FlaxRobertaPreLayerNormForQuestionAnswering,
       FlaxRobertaPreLayerNormForSequenceClassification,
       FlaxRobertaPreLayerNormForTokenClassification,
       FlaxRobertaPreLayerNormModel,
   )


class     _a						(			unittest.TestCase				):
       def __init__(			self		:			List[Any]     ,       lowercase		:			Dict     ,       lowercase		:			List[str]=13     ,       lowercase		:			str=7     ,       lowercase		:			List[str]=True     ,       lowercase		:			List[str]=True     ,       lowercase		:			Optional[Any]=True     ,       lowercase		:			Optional[Any]=True     ,       lowercase		:			Any=99     ,       lowercase		:			Any=32     ,       lowercase		:			Any=5     ,       lowercase		:			Tuple=4     ,       lowercase		:			List[Any]=37     ,       lowercase		:			List[Any]="gelu"     ,       lowercase		:			int=0.1     ,       lowercase		:			Any=0.1     ,       lowercase		:			Optional[int]=512     ,       lowercase		:			List[str]=16     ,       lowercase		:			Union[str, Any]=2     ,       lowercase		:			int=0.02     ,       lowercase		:			int=4     ,       ):



           '''simple docstring'''

           UpperCAmelCase     =	parent
           UpperCAmelCase     =	batch_size
           UpperCAmelCase     =	seq_length
           UpperCAmelCase     =	is_training
           UpperCAmelCase     =	use_attention_mask
           UpperCAmelCase     =	use_token_type_ids
           UpperCAmelCase     =	use_labels
           UpperCAmelCase     =	vocab_size
           UpperCAmelCase     =	hidden_size
           UpperCAmelCase     =	num_hidden_layers
           UpperCAmelCase     =	num_attention_heads
           UpperCAmelCase     =	intermediate_size
           UpperCAmelCase     =	hidden_act
           UpperCAmelCase     =	hidden_dropout_prob
           UpperCAmelCase     =	attention_probs_dropout_prob
           UpperCAmelCase     =	max_position_embeddings
           UpperCAmelCase     =	type_vocab_size
           UpperCAmelCase     =	type_sequence_label_size
           UpperCAmelCase     =	initializer_range
           UpperCAmelCase     =	num_choices
       def 					A   (			self		:			Any       ):



           '''simple docstring'''

           UpperCAmelCase     =	ids_tensor([self.batch_size, self.seq_length]     ,       self.vocab_size       )

           UpperCAmelCase     =	None
           if self.use_attention_mask:
               UpperCAmelCase     =	random_attention_mask([self.batch_size, self.seq_length]       )

           UpperCAmelCase     =	None
           if self.use_token_type_ids:
               UpperCAmelCase     =	ids_tensor([self.batch_size, self.seq_length]     ,       self.type_vocab_size       )

           UpperCAmelCase     =	RobertaPreLayerNormConfig(
               vocab_size=self.vocab_size     ,       hidden_size=self.hidden_size     ,       num_hidden_layers=self.num_hidden_layers     ,       num_attention_heads=self.num_attention_heads     ,       intermediate_size=self.intermediate_size     ,       hidden_act=self.hidden_act     ,       hidden_dropout_prob=self.hidden_dropout_prob     ,       attention_probs_dropout_prob=self.attention_probs_dropout_prob     ,       max_position_embeddings=self.max_position_embeddings     ,       type_vocab_size=self.type_vocab_size     ,       is_decoder=lowercase     ,       initializer_range=self.initializer_range     ,       )

           return config, input_ids, token_type_ids, attention_mask
       def 					A   (			self		:			List[Any]       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.prepare_config_and_inputs()
           UpperCAmelCase ,  UpperCAmelCase ,  UpperCAmelCase ,  UpperCAmelCase     =	config_and_inputs
           UpperCAmelCase     =	{'''input_ids''': input_ids, '''token_type_ids''': token_type_ids, '''attention_mask''': attention_mask}
           return config, inputs_dict







       def 					A   (			self		:			Optional[Any]       ):



           '''simple docstring'''

           UpperCAmelCase     =	self.prepare_config_and_inputs()
           UpperCAmelCase ,  UpperCAmelCase ,  UpperCAmelCase ,  UpperCAmelCase     =	config_and_inputs

           UpperCAmelCase     =	True
           UpperCAmelCase     =	floats_tensor([self.batch_size, self.seq_length, self.hidden_size]       )
           UpperCAmelCase     =	ids_tensor([self.batch_size, self.seq_length]     ,       vocab_size=2       )

           return (
               config,
               input_ids,
               token_type_ids,
               encoder_hidden_states,
               encoder_attention_mask,
           )






@require_flax
# Copied from tests.models.roberta.test_modelling_flax_roberta.FlaxRobertaPreLayerNormModelTest with ROBERTA->ROBERTA_PRELAYERNORM,Roberta->RobertaPreLayerNorm,roberta-base->andreasmadsen/efficient_mlm_m0.40
class     _a						(			__a							,							unittest.TestCase				):
       __a       :  Any            =      True

       __a       :  str            =      (
           (
               FlaxRobertaPreLayerNormModel,
               FlaxRobertaPreLayerNormForCausalLM,
               FlaxRobertaPreLayerNormForMaskedLM,
               FlaxRobertaPreLayerNormForSequenceClassification,
               FlaxRobertaPreLayerNormForTokenClassification,
               FlaxRobertaPreLayerNormForMultipleChoice,
               FlaxRobertaPreLayerNormForQuestionAnswering,
           )
           if is_flax_available()
           else ()
       )
       def 					A   (			self		:			Any       ):



           '''simple docstring'''

           UpperCAmelCase     =	FlaxRobertaPreLayerNormModelTester(self       )







       @slow
       def 					A   (			self		:			Optional[int]       ):



           '''simple docstring'''

           for model_class_name in self.all_model_classes:
               UpperCAmelCase     =	model_class_name.from_pretrained('''andreasmadsen/efficient_mlm_m0.40'''     ,       from_pt=lowercase       )
               UpperCAmelCase     =	model(np.ones((1, 1)       )       )
               self.assertIsNotNone(lowercase       )






@require_flax
class     _a						(			unittest.TestCase				):
       @slow
       def 					A   (			self		:			Union[str, Any]       ):



           '''simple docstring'''

           UpperCAmelCase     =	FlaxRobertaPreLayerNormForMaskedLM.from_pretrained('''andreasmadsen/efficient_mlm_m0.40'''     ,       from_pt=lowercase       )

           UpperCAmelCase     =	np.array([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]     ,       dtype=jnp.intaa       )
           UpperCAmelCase     =	model(lowercase       )[0]
           UpperCAmelCase     =	[1, 11, 50_265]
           self.assertEqual(list(output.shape       )     ,       lowercase       )
           # compare the actual values for a slice.
           UpperCAmelCase     =	np.array(
               [[[40.4880, 18.0199, -5.2367], [-1.8877, -4.0885, 10.7085], [-2.2613, -5.6110, 7.2665]]]     ,       dtype=np.floataa       )
           self.assertTrue(np.allclose(output[:, :3, :3]     ,       lowercase     ,       atol=1E-4       )       )







       @slow
       def 					A   (			self		:			Optional[Any]       ):



           '''simple docstring'''

           UpperCAmelCase     =	FlaxRobertaPreLayerNormModel.from_pretrained('''andreasmadsen/efficient_mlm_m0.40'''     ,       from_pt=lowercase       )

           UpperCAmelCase     =	np.array([[0, 31_414, 232, 328, 740, 1_140, 12_695, 69, 46_078, 1_588, 2]]     ,       dtype=jnp.intaa       )
           UpperCAmelCase     =	model(lowercase       )[0]
           # compare the actual values for a slice.
           UpperCAmelCase     =	np.array(
               [[[0.0208, -0.0356, 0.0237], [-0.1569, -0.0411, -0.2626], [0.1879, 0.0125, -0.0089]]]     ,       dtype=np.floataa       )
           self.assertTrue(np.allclose(output[:, :3, :3]     ,       lowercase     ,       atol=1E-4       )       )




