





from __future__ import annotations

import collections
import tempfile
import unittest

import numpy as np

from transformers.testing_utils import require_tf, require_vision, slow
from transformers.utils import is_tf_available, is_vision_available

from ...test_modeling_tf_common import floats_tensor, ids_tensor, random_attention_mask
from ..bert.test_modeling_tf_bert import TFBertModelTester
from ..clip.test_modeling_tf_clip import TFCLIPVisionModelTester
from ..deit.test_modeling_tf_deit import TFDeiTModelTester
from ..roberta.test_modeling_tf_roberta import TFRobertaModelTester
from ..vit.test_modeling_tf_vit import TFViTModelTester


if is_tf_available():
  from transformers import (
      TFBertModel,
      TFCLIPVisionModel,
      TFDeiTModel,
      TFRobertaModel,
      TFVisionTextDualEncoderModel,
      TFViTModel,
      VisionTextDualEncoderConfig,
  )

if is_vision_available():
  from PIL import Image

  from transformers import VisionTextDualEncoderProcessor






def       _A  (      lowerCamelCase   ):
   if isinstance(lowerCamelCase							, collections.abc.Iterable   ):
      return x
   return (x, x)







@require_tf
class   __lowerCAmelCase			:





   def 	_snake_case ( self					,			snake_case					,			snake_case      )				->  List[str]:
      """simple docstring"""





      pass





   def 	_snake_case ( self      )				->  List[Any]:
      """simple docstring"""





      pass





   def 	_snake_case ( self      )				->  Dict:
      """simple docstring"""





      pass





   def 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case=None					,			**snake_case      )				->  Any:
      """simple docstring"""





      a__	:						Union[str, Any]      		=  VisionTextDualEncoderConfig.from_vision_text_configs(snake_case					,			snake_case      )

      a__	:						Tuple      		=  TFVisionTextDualEncoderModel(snake_case      )

      a__	:						List[Any]      		=  model(input_ids=snake_case					,			pixel_values=snake_case					,			attention_mask=snake_case      )

      self.assertEqual(output["text_embeds"].shape					,			(input_ids.shape[0], config.projection_dim)      )
      self.assertEqual(output["image_embeds"].shape					,			(pixel_values.shape[0], config.projection_dim)      )





   def 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case=None					,			**snake_case      )				->  Dict:
      """simple docstring"""





      a__		,     a__	:						Optional[int]      		=  self.get_vision_text_model(snake_case					,			snake_case      )
      a__	:						Optional[Any]      		=  TFVisionTextDualEncoderModel(vision_model=snake_case					,			text_model=snake_case      )

      a__	:						Tuple      		=  model(input_ids=snake_case					,			pixel_values=snake_case					,			attention_mask=snake_case      )

      self.assertEqual(output["text_embeds"].shape					,			(input_ids.shape[0], model.config.projection_dim)      )
      self.assertEqual(output["image_embeds"].shape					,			(pixel_values.shape[0], model.config.projection_dim)      )





   def 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case=None					,			**snake_case      )				->  Optional[int]:
      """simple docstring"""





      a__		,     a__	:						Optional[int]      		=  self.get_vision_text_model(snake_case					,			snake_case      )
      a__	:						Tuple      		=  {"vision_model": vision_model, "text_model": text_model}
      a__	:						Optional[Any]      		=  TFVisionTextDualEncoderModel.from_vision_text_pretrained(**snake_case      )

      a__	:						str      		=  model(input_ids=snake_case					,			pixel_values=snake_case					,			attention_mask=snake_case      )

      self.assertEqual(output["text_embeds"].shape					,			(input_ids.shape[0], model.config.projection_dim)      )
      self.assertEqual(output["image_embeds"].shape					,			(pixel_values.shape[0], model.config.projection_dim)      )





   def 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case=None					,			**snake_case      )				->  Any:
      """simple docstring"""





      a__		,     a__	:						Dict      		=  self.get_vision_text_model(snake_case					,			snake_case      )
      a__	:						Dict      		=  TFVisionTextDualEncoderModel(vision_model=snake_case					,			text_model=snake_case      )

      a__	:						Dict      		=  model(input_ids=snake_case					,			pixel_values=snake_case					,			attention_mask=snake_case      )
      a__	:						Optional[int]      		=  output[0].numpy()

      with tempfile.TemporaryDirectory() as tmpdirname:
         model.save_pretrained(snake_case      )
         a__	:						int      		=  TFVisionTextDualEncoderModel.from_pretrained(snake_case      )

         a__	:						int      		=  model(input_ids=snake_case					,			pixel_values=snake_case					,			attention_mask=snake_case      )
         a__	:						Optional[Any]      		=  after_output[0].numpy()
         a__	:						Any      		=  np.amax(np.abs(out_a - out_a      )      )
         self.assertLessEqual(snake_case					,			1E-5      )





   def 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case=None					,			**snake_case      )				->  str:
      """simple docstring"""





      a__		,     a__	:						Tuple      		=  self.get_vision_text_model(snake_case					,			snake_case      )
      a__	:						int      		=  TFVisionTextDualEncoderModel(vision_model=snake_case					,			text_model=snake_case      )

      a__	:						List[Any]      		=  model(
          input_ids=snake_case					,			pixel_values=snake_case					,			attention_mask=snake_case					,			output_attentions=snake_case      )

      a__	:						int      		=  output.vision_model_output.attentions
      self.assertEqual(len(snake_case      )					,			vision_config.num_hidden_layers      )

      # in ViT, the seq_len equals the number of patches + 1 (we add 1 for the [CLS] token)
      a__	:						Optional[int]      		=  to_atuple(vision_model.config.image_size      )
      a__	:						List[str]      		=  to_atuple(vision_model.config.patch_size      )
      a__	:						Dict      		=  (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
      a__	:						Optional[Any]      		=  num_patches + 1
      self.assertEqual(vision_attentions[0].shape[-3:]					,			(vision_config.num_attention_heads, seq_len, seq_len)      )

      a__	:						Any      		=  output.text_model_output.attentions
      self.assertEqual(len(snake_case      )					,			text_config.num_hidden_layers      )

      self.assertEqual(
          text_attentions[0].shape[-3:]					,			(text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])					,			)





   def 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case      )				->  Dict:
      """simple docstring"""





      a__	:						Union[str, Any]      		=  np.abs((a - b)      ).max()
      self.assertLessEqual(snake_case					,			snake_case					,			F"""Difference between torch and flax is {diff} (>= {tol})."""      )





   def 	_snake_case ( self      )				->  Union[str, Any]:
      """simple docstring"""





      a__	:						str      		=  self.prepare_config_and_inputs()
      self.check_vision_text_dual_encoder_model(**snake_case      )





   def 	_snake_case ( self      )				->  List[Any]:
      """simple docstring"""





      a__	:						Union[str, Any]      		=  self.prepare_config_and_inputs()
      self.check_model_from_pretrained_configs(**snake_case      )





   def 	_snake_case ( self      )				->  Dict:
      """simple docstring"""





      a__	:						Union[str, Any]      		=  self.prepare_config_and_inputs()
      self.check_vision_text_dual_encoder_from_pretrained(**snake_case      )





   def 	_snake_case ( self      )				->  Any:
      """simple docstring"""





      a__	:						List[str]      		=  self.prepare_config_and_inputs()
      self.check_save_load(**snake_case      )





   def 	_snake_case ( self      )				->  List[str]:
      """simple docstring"""





      a__	:						Optional[int]      		=  self.prepare_config_and_inputs()
      self.check_vision_text_output_attention(**snake_case      )





   @slow
   def 	_snake_case ( self      )				->  Any:
      """simple docstring"""





      a__		,     a__	:						Tuple      		=  self.get_pretrained_model_and_inputs()

      a__	:						List[str]      		=  model_a(**snake_case      )
      a__	:						Optional[Any]      		=  outputs[0].numpy()

      with tempfile.TemporaryDirectory() as tmp_dirname:
         model_a.save_pretrained(snake_case      )
         a__	:						str      		=  TFVisionTextDualEncoderModel.from_pretrained(snake_case      )

         a__	:						Tuple      		=  model_a(**snake_case      )
         a__	:						Tuple      		=  after_outputs[0].numpy()
         a__	:						str      		=  np.amax(np.abs(out_a - out_a      )      )
         self.assertLessEqual(snake_case					,			1E-5      )







@require_tf
class   __lowerCAmelCase			(					_UpperCamelCase		,unittest.TestCase  ):





   def 	_snake_case ( self      )				->  int:
      """simple docstring"""





      a__	:						int      		=  TFVisionTextDualEncoderModel.from_vision_text_pretrained(
          "hf-internal-testing/tiny-random-vit"					,			"hf-internal-testing/tiny-random-bert"      )
      a__	:						int      		=  13
      a__	:						Any      		=  floats_tensor(
          [
              batch_size,
              model.vision_model.config.num_channels,
              model.vision_model.config.image_size,
              model.vision_model.config.image_size,
          ]      )
      a__	:						Any      		=  ids_tensor([batch_size, 4]					,			model.text_model.config.vocab_size      )
      a__	:						int      		=  random_attention_mask([batch_size, 4]      )
      a__	:						Optional[int]      		=  {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

      return model, inputs





   def 	_snake_case ( self					,			snake_case					,			snake_case      )				->  Union[str, Any]:
      """simple docstring"""





      a__	:						List[str]      		=  TFViTModel(snake_case					,			name="vision_model"      )
      a__	:						List[Any]      		=  TFBertModel(snake_case					,			name="text_model"      )
      return vision_model, text_model





   def 	_snake_case ( self      )				->  Dict:
      """simple docstring"""





      a__	:						Optional[Any]      		=  TFViTModelTester(self      )
      a__	:						Any      		=  TFBertModelTester(self      )
      a__	:						str      		=  vit_model_tester.prepare_config_and_inputs()
      a__	:						str      		=  bert_model_tester.prepare_config_and_inputs()

      a__		,     a__		,     a__	:						Dict      		=  vision_config_and_inputs

      (
          (
          a__
      )		,     (
          a__
      )		,     (
          a__
      )		,     (
          a__
      )		,     (
          a__
      )		,     (
          a__
      )		,     (
          a__
      )		,     
      )	:						List[Any]      		=  text_config_and_inputs

      return {
          "text_config": text_config,
          "vision_config": vision_config,
          "pixel_values": pixel_values,
          "attention_mask": input_mask,
          "input_ids": input_ids,
          "text_token_type_ids": token_type_ids,
          "text_sequence_labels": sequence_labels,
          "text_token_labels": token_labels,
          "text_choice_labels": choice_labels,
      }







@require_tf
class   __lowerCAmelCase			(					_UpperCamelCase		,unittest.TestCase  ):





   def 	_snake_case ( self      )				->  Tuple:
      """simple docstring"""





      a__	:						List[str]      		=  TFVisionTextDualEncoderModel.from_vision_text_pretrained(
          "Rocketknight1/tiny-random-deit-tf"					,			"hf-internal-testing/tiny-random-roberta"      )
      a__	:						str      		=  13
      a__	:						str      		=  floats_tensor(
          [
              batch_size,
              model.vision_model.config.num_channels,
              model.vision_model.config.image_size,
              model.vision_model.config.image_size,
          ]      )
      a__	:						Any      		=  ids_tensor([batch_size, 4]					,			model.text_model.config.vocab_size      )
      a__	:						List[Any]      		=  random_attention_mask([batch_size, 4]      )
      a__	:						Tuple      		=  {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

      return model, inputs





   def 	_snake_case ( self					,			snake_case					,			snake_case					,			snake_case					,			snake_case					,			snake_case=None					,			**snake_case      )				->  Tuple:
      """simple docstring"""





      a__		,     a__	:						Optional[int]      		=  self.get_vision_text_model(snake_case					,			snake_case      )
      a__	:						str      		=  TFVisionTextDualEncoderModel(vision_model=snake_case					,			text_model=snake_case      )

      a__	:						int      		=  model(
          input_ids=snake_case					,			pixel_values=snake_case					,			attention_mask=snake_case					,			output_attentions=snake_case      )

      a__	:						Optional[int]      		=  output.vision_model_output.attentions
      self.assertEqual(len(snake_case      )					,			vision_config.num_hidden_layers      )

      # in DEiT, the seq_len equals the number of patches + 2 (we add 2 for the [CLS] and distillation tokens)
      a__	:						Union[str, Any]      		=  to_atuple(vision_model.config.image_size      )
      a__	:						Tuple      		=  to_atuple(vision_model.config.patch_size      )
      a__	:						List[Any]      		=  (image_size[1] // patch_size[1]) * (image_size[0] // patch_size[0])
      a__	:						int      		=  num_patches + 2
      self.assertEqual(vision_attentions[0].shape[-3:]					,			(vision_config.num_attention_heads, seq_len, seq_len)      )

      a__	:						int      		=  output.text_model_output.attentions
      self.assertEqual(len(snake_case      )					,			text_config.num_hidden_layers      )

      self.assertEqual(
          text_attentions[0].shape[-3:]					,			(text_config.num_attention_heads, input_ids.shape[-1], input_ids.shape[-1])					,			)





   def 	_snake_case ( self					,			snake_case					,			snake_case      )				->  str:
      """simple docstring"""





      a__	:						Union[str, Any]      		=  TFDeiTModel(snake_case					,			name="vision_model"      )
      a__	:						int      		=  TFRobertaModel(snake_case					,			name="text_model"      )
      return vision_model, text_model





   def 	_snake_case ( self      )				->  int:
      """simple docstring"""





      a__	:						Dict      		=  TFDeiTModelTester(self      )
      a__	:						Optional[int]      		=  TFRobertaModelTester(self      )
      a__	:						Dict      		=  vit_model_tester.prepare_config_and_inputs()
      a__	:						Dict      		=  bert_model_tester.prepare_config_and_inputs()

      a__		,     a__		,     a__	:						Any      		=  vision_config_and_inputs

      (
          (
          a__
      )		,     (
          a__
      )		,     (
          a__
      )		,     (
          a__
      )		,     (
          a__
      )		,     (
          a__
      )		,     (
          a__
      )		,     
      )	:						Optional[int]      		=  text_config_and_inputs

      return {
          "text_config": text_config,
          "vision_config": vision_config,
          "pixel_values": pixel_values,
          "attention_mask": input_mask,
          "input_ids": input_ids,
          "text_token_type_ids": token_type_ids,
          "text_sequence_labels": sequence_labels,
          "text_token_labels": token_labels,
          "text_choice_labels": choice_labels,
      }







@require_tf
class   __lowerCAmelCase			(					_UpperCamelCase		,unittest.TestCase  ):





   def 	_snake_case ( self      )				->  str:
      """simple docstring"""





      a__	:						List[str]      		=  TFVisionTextDualEncoderModel.from_vision_text_pretrained(
          "Rocketknight1/tiny-random-clip-tf"					,			"hf-internal-testing/tiny-random-bert"      )
      a__	:						List[str]      		=  13
      a__	:						Any      		=  floats_tensor(
          [
              batch_size,
              model.vision_model.config.num_channels,
              model.vision_model.config.image_size,
              model.vision_model.config.image_size,
          ]      )
      a__	:						Optional[int]      		=  ids_tensor([batch_size, 4]					,			model.text_model.config.vocab_size      )
      a__	:						Dict      		=  random_attention_mask([batch_size, 4]      )
      a__	:						Optional[int]      		=  {"pixel_values": pixel_values, "input_ids": input_ids, "attention_mask": attention_mask}

      return model, inputs





   def 	_snake_case ( self					,			snake_case					,			snake_case      )				->  Optional[Any]:
      """simple docstring"""





      a__	:						Dict      		=  TFCLIPVisionModel(snake_case					,			name="vision_model"      )
      a__	:						Union[str, Any]      		=  TFBertModel(snake_case					,			name="text_model"      )
      return vision_model, text_model





   def 	_snake_case ( self      )				->  Tuple:
      """simple docstring"""





      a__	:						Optional[int]      		=  TFCLIPVisionModelTester(self      )
      a__	:						List[Any]      		=  TFBertModelTester(self      )
      a__	:						Tuple      		=  clip_model_tester.prepare_config_and_inputs()
      a__	:						List[Any]      		=  bert_model_tester.prepare_config_and_inputs()

      a__		,     a__	:						str      		=  vision_config_and_inputs

      (
          (
          a__
      )		,     (
          a__
      )		,     (
          a__
      )		,     (
          a__
      )		,     (
          a__
      )		,     (
          a__
      )		,     (
          a__
      )		,     
      )	:						List[Any]      		=  text_config_and_inputs

      return {
          "text_config": text_config,
          "vision_config": vision_config,
          "pixel_values": pixel_values,
          "attention_mask": input_mask,
          "input_ids": input_ids,
          "text_token_type_ids": token_type_ids,
          "text_sequence_labels": sequence_labels,
          "text_token_labels": token_labels,
          "text_choice_labels": choice_labels,
      }







@require_vision
@require_tf
class   __lowerCAmelCase			(					unittest.TestCase  ):





   @slow
   def 	_snake_case ( self      )				->  Optional[Any]:
      """simple docstring"""





      a__	:						Union[str, Any]      		=  TFVisionTextDualEncoderModel.from_pretrained(
          "clip-italian/clip-italian"					,			logit_scale_init_value=1.0					,			from_pt=snake_case      )
      a__	:						Dict      		=  VisionTextDualEncoderProcessor.from_pretrained("clip-italian/clip-italian"      )

      a__	:						int      		=  Image.open("./tests/fixtures/tests_samples/COCO/000000039769.png"      )
      a__	:						Optional[Any]      		=  processor(
          text=["una foto di un gatto", "una foto di un cane"]					,			images=snake_case					,			padding=snake_case					,			return_tensors="np"      )

      a__	:						List[Any]      		=  model(**snake_case      )

      # verify the logits
      self.assertEqual(outputs.logits_per_image.shape					,			(inputs.pixel_values.shape[0], inputs.input_ids.shape[0])      )
      self.assertEqual(
          outputs.logits_per_text.shape					,			(inputs.input_ids.shape[0], inputs.pixel_values.shape[0])					,			)

      a__	:						Optional[Any]      		=  np.array([[1.2_284_727, 0.3_104_122]]      )

      self.assertTrue(np.allclose(outputs.logits_per_image.numpy()					,			snake_case					,			atol=1E-3      )      )








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


SCREAMING_SNAKE_CASE__       :			List[str]			      =						{
    """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       _A  (      lowerCamelCase   ):
   a__	:						Optional[int]      		=  ["layers", "blocks"]
   for k in ignore_keys:
      state_dict.pop(lowerCamelCase							, lowerCamelCase   )


SCREAMING_SNAKE_CASE__       :			List[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       _A  (      lowerCamelCase   ):
   a__	:						Tuple      		=  list(s_dict.keys()   )
   for key in keys:
      a__	:						Optional[Any]      		=  key
      for k, v in WHISPER_MAPPING.items():
         if k in key:
            a__	:						Optional[int]      		=  new_key.replace(lowerCamelCase							, lowerCamelCase   )

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

      a__	:						Dict      		=  s_dict.pop(lowerCamelCase   )
   return s_dict






def       _A  (      lowerCamelCase   ):
   a__		,     a__	:						Any      		=  emb.weight.shape
   a__	:						Optional[Any]      		=  nn.Linear(lowerCamelCase							, lowerCamelCase							, bias=lowerCamelCase   )
   a__	:						Optional[Any]      		=  emb.weight.data
   return lin_layer






def       _A  (      lowerCamelCase							, lowerCamelCase   ):
   os.makedirs(lowerCamelCase							, exist_ok=lowerCamelCase   )
   a__	:						Optional[Any]      		=  os.path.basename(lowerCamelCase   )

   a__	:						List[Any]      		=  url.split("/"   )[-2]
   a__	:						Tuple      		=  os.path.join(lowerCamelCase							, lowerCamelCase   )

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

   if os.path.isfile(lowerCamelCase   ):
      a__	:						Any      		=  open(lowerCamelCase							, "rb"   ).read()
      if hashlib.shaaaa(lowerCamelCase   ).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(lowerCamelCase   ) as source, open(lowerCamelCase							, "wb"   ) as output:
      with tqdm(
          total=int(source.info().get("Content-Length"   )   )							, ncols=80							, unit="iB"							, unit_scale=lowerCamelCase							, unit_divisor=1024   ) as loop:
         while True:
            a__	:						Optional[Any]      		=  source.read(8192   )
            if not buffer:
               break

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

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

   a__	:						Tuple      		=  WhisperConfig(
       vocab_size=dimensions["n_vocab"]							, encoder_ffn_dim=lowerCamelCase							, decoder_ffn_dim=lowerCamelCase							, 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__	:						Optional[Any]      		=  WhisperForConditionalGeneration(lowerCamelCase   )
   a__		,     a__	:						Tuple      		=  model.model.load_state_dict(lowerCamelCase							, strict=lowerCamelCase   )
   if len(lowerCamelCase   ) > 0 and not set(lowerCamelCase   ) <= {
       "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__	:						Any      		=  make_linear_from_emb(model.model.decoder.embed_tokens   )
   else:
      a__	:						str      		=  proj_out_weights

   model.save_pretrained(lowerCamelCase   )


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

  convert_openai_whisper_to_tfms(args.checkpoint_path, args.pytorch_dump_folder_path)

