





import os
import tempfile
import unittest

from transformers import DistilBertConfig, is_torch_available
from transformers.testing_utils import require_torch, require_torch_gpu, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
      import torch

      from transformers import (
          DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST,
          DistilBertForMaskedLM,
          DistilBertForMultipleChoice,
          DistilBertForQuestionAnswering,
          DistilBertForSequenceClassification,
          DistilBertForTokenClassification,
          DistilBertModel,
      )
class     SCREAMING_SNAKE_CASE       (UpperCAmelCase				):


   def __init__(   self      :    List[Any]      ,					a      :    Tuple      ,					a      :    Dict=13      ,					a      :    List[str]=7      ,					a      :    Union[str, Any]=True      ,					a      :    Union[str, Any]=True      ,					a      :    str=False      ,					a      :    Optional[int]=True      ,					a      :    int=99      ,					a      :    Optional[int]=32      ,					a      :    Tuple=5      ,					a      :    Dict=4      ,					a      :    str=37      ,					a      :    List[Any]="gelu"      ,					a      :    Optional[Any]=0.1      ,					a      :    List[str]=0.1      ,					a      :    int=512      ,					a      :    Tuple=16      ,					a      :    Dict=2      ,					a      :    Optional[Any]=0.02      ,					a      :    Any=3      ,					a      :    List[Any]=4      ,					a      :    Optional[int]=None      ,					)->       Optional[int]:





     """simple docstring"""







     lowercase__           =  parent
     lowercase__           =  batch_size
     lowercase__           =  seq_length
     lowercase__           =  is_training
     lowercase__           =  use_input_mask
     lowercase__           =  use_token_type_ids
     lowercase__           =  use_labels
     lowercase__           =  vocab_size
     lowercase__           =  hidden_size
     lowercase__           =  num_hidden_layers
     lowercase__           =  num_attention_heads
     lowercase__           =  intermediate_size
     lowercase__           =  hidden_act
     lowercase__           =  hidden_dropout_prob
     lowercase__           =  attention_probs_dropout_prob
     lowercase__           =  max_position_embeddings
     lowercase__           =  type_vocab_size
     lowercase__           =  type_sequence_label_size
     lowercase__           =  initializer_range
     lowercase__           =  num_labels
     lowercase__           =  num_choices
     lowercase__           =  scope


   def 	SCREAMING_SNAKE_CASE_							(   self      :    Optional[int]	)->       Optional[Any]:





     """simple docstring"""







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

     lowercase__           =  None
     if self.use_input_mask:
       lowercase__           =  random_attention_mask([self.batch_size, self.seq_length]	)

     lowercase__           =  None
     lowercase__           =  None
     lowercase__           =  None
     if self.use_labels:
       lowercase__           =  ids_tensor([self.batch_size]      ,					self.type_sequence_label_size	)
       lowercase__           =  ids_tensor([self.batch_size, self.seq_length]      ,					self.num_labels	)
       lowercase__           =  ids_tensor([self.batch_size]      ,					self.num_choices	)

     lowercase__           =  self.get_config()

     return config, input_ids, input_mask, sequence_labels, token_labels, choice_labels


   def 	SCREAMING_SNAKE_CASE_							(   self      :    str	)->       str:





     """simple docstring"""







     return DistilBertConfig(
         vocab_size=self.vocab_size      ,					dim=self.hidden_size      ,					n_layers=self.num_hidden_layers      ,					n_heads=self.num_attention_heads      ,					hidden_dim=self.intermediate_size      ,					hidden_act=self.hidden_act      ,					dropout=self.hidden_dropout_prob      ,					attention_dropout=self.attention_probs_dropout_prob      ,					max_position_embeddings=self.max_position_embeddings      ,					initializer_range=self.initializer_range      ,					)


   def 	SCREAMING_SNAKE_CASE_							(   self      :    Dict      ,					a      :    int      ,					a      :    List[Any]      ,					a      :    int      ,					a      :    str      ,					a      :    Tuple      ,					a      :    Any	)->       Tuple:





     """simple docstring"""







     lowercase__           =  DistilBertModel(config=a	)
     model.to(a	)
     model.eval()
     lowercase__           =  model(a      ,					a	)
     lowercase__           =  model(a	)
     self.parent.assertEqual(result.last_hidden_state.shape      ,					(self.batch_size, self.seq_length, self.hidden_size)	)


   def 	SCREAMING_SNAKE_CASE_							(   self      :    List[str]      ,					a      :    Any      ,					a      :    List[str]      ,					a      :    Optional[int]      ,					a      :    List[Any]      ,					a      :    str      ,					a      :    Any	)->       int:





     """simple docstring"""







     lowercase__           =  DistilBertForMaskedLM(config=a	)
     model.to(a	)
     model.eval()
     lowercase__           =  model(a      ,					attention_mask=a      ,					labels=a	)
     self.parent.assertEqual(result.logits.shape      ,					(self.batch_size, self.seq_length, self.vocab_size)	)


   def 	SCREAMING_SNAKE_CASE_							(   self      :    str      ,					a      :    Optional[int]      ,					a      :    Any      ,					a      :    Optional[int]      ,					a      :    List[Any]      ,					a      :    List[Any]      ,					a      :    Dict	)->       int:





     """simple docstring"""







     lowercase__           =  DistilBertForQuestionAnswering(config=a	)
     model.to(a	)
     model.eval()
     lowercase__           =  model(
         a      ,					attention_mask=a      ,					start_positions=a      ,					end_positions=a	)
     self.parent.assertEqual(result.start_logits.shape      ,					(self.batch_size, self.seq_length)	)
     self.parent.assertEqual(result.end_logits.shape      ,					(self.batch_size, self.seq_length)	)


   def 	SCREAMING_SNAKE_CASE_							(   self      :    Dict      ,					a      :    Union[str, Any]      ,					a      :    Optional[Any]      ,					a      :    Dict      ,					a      :    Tuple      ,					a      :    Tuple      ,					a      :    Union[str, Any]	)->       int:





     """simple docstring"""







     lowercase__           =  self.num_labels
     lowercase__           =  DistilBertForSequenceClassification(a	)
     model.to(a	)
     model.eval()
     lowercase__           =  model(a      ,					attention_mask=a      ,					labels=a	)
     self.parent.assertEqual(result.logits.shape      ,					(self.batch_size, self.num_labels)	)


   def 	SCREAMING_SNAKE_CASE_							(   self      :    Union[str, Any]      ,					a      :    Any      ,					a      :    int      ,					a      :    Union[str, Any]      ,					a      :    Optional[Any]      ,					a      :    Union[str, Any]      ,					a      :    Optional[Any]	)->       int:





     """simple docstring"""







     lowercase__           =  self.num_labels
     lowercase__           =  DistilBertForTokenClassification(config=a	)
     model.to(a	)
     model.eval()

     lowercase__           =  model(a      ,					attention_mask=a      ,					labels=a	)
     self.parent.assertEqual(result.logits.shape      ,					(self.batch_size, self.seq_length, self.num_labels)	)


   def 	SCREAMING_SNAKE_CASE_							(   self      :    List[str]      ,					a      :    Optional[Any]      ,					a      :    Tuple      ,					a      :    Tuple      ,					a      :    Any      ,					a      :    str      ,					a      :    Any	)->       Optional[Any]:





     """simple docstring"""







     lowercase__           =  self.num_choices
     lowercase__           =  DistilBertForMultipleChoice(config=a	)
     model.to(a	)
     model.eval()
     lowercase__           =  input_ids.unsqueeze(1	).expand(-1      ,					self.num_choices      ,					-1	).contiguous()
     lowercase__           =  input_mask.unsqueeze(1	).expand(-1      ,					self.num_choices      ,					-1	).contiguous()
     lowercase__           =  model(
         a      ,					attention_mask=a      ,					labels=a      ,					)
     self.parent.assertEqual(result.logits.shape      ,					(self.batch_size, self.num_choices)	)


   def 	SCREAMING_SNAKE_CASE_							(   self      :    Dict	)->       int:





     """simple docstring"""







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






@require_torch
class     SCREAMING_SNAKE_CASE       (UpperCAmelCase      ,     UpperCAmelCase      ,     unittest.TestCase				):
   _UpperCamelCase   :		List[Any]			 =	(
       (
           DistilBertModel,
           DistilBertForMaskedLM,
           DistilBertForMultipleChoice,
           DistilBertForQuestionAnswering,
           DistilBertForSequenceClassification,
           DistilBertForTokenClassification,
       )
       if is_torch_available()
       else None
   )
   _UpperCamelCase   :		List[str]			 =	(
       {
           'feature-extraction': DistilBertModel,
           'fill-mask': DistilBertForMaskedLM,
           'question-answering': DistilBertForQuestionAnswering,
           'text-classification': DistilBertForSequenceClassification,
           'token-classification': DistilBertForTokenClassification,
           'zero-shot': DistilBertForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   _UpperCamelCase   :		int			 =	True
   _UpperCamelCase   :		Any			 =	True
   _UpperCamelCase   :		List[Any]			 =	True
   _UpperCamelCase   :		str			 =	True


   def 	SCREAMING_SNAKE_CASE_							(   self      :    Union[str, Any]	)->       int:





     """simple docstring"""







     lowercase__           =  DistilBertModelTester(self	)
     lowercase__           =  ConfigTester(self      ,					config_class=a      ,					dim=37	)


   def 	SCREAMING_SNAKE_CASE_							(   self      :    Union[str, Any]	)->       List[Any]:





     """simple docstring"""







     self.config_tester.run_common_tests()


   def 	SCREAMING_SNAKE_CASE_							(   self      :    Optional[int]	)->       Tuple:





     """simple docstring"""







     lowercase__           =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_model(*a	)


   def 	SCREAMING_SNAKE_CASE_							(   self      :    List[str]	)->       List[str]:





     """simple docstring"""







     lowercase__           =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_for_masked_lm(*a	)


   def 	SCREAMING_SNAKE_CASE_							(   self      :    Optional[Any]	)->       int:





     """simple docstring"""







     lowercase__           =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_for_question_answering(*a	)


   def 	SCREAMING_SNAKE_CASE_							(   self      :    Dict	)->       Optional[Any]:





     """simple docstring"""







     lowercase__           =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_for_sequence_classification(*a	)


   def 	SCREAMING_SNAKE_CASE_							(   self      :    str	)->       Dict:





     """simple docstring"""







     lowercase__           =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_for_token_classification(*a	)


   def 	SCREAMING_SNAKE_CASE_							(   self      :    Optional[int]	)->       List[str]:





     """simple docstring"""







     lowercase__           =  self.model_tester.prepare_config_and_inputs()
     self.model_tester.create_and_check_distilbert_for_multiple_choice(*a	)


   @slow
   def 	SCREAMING_SNAKE_CASE_							(   self      :    List[str]	)->       str:





     """simple docstring"""







     for model_name in DISTILBERT_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
       lowercase__           =  DistilBertModel.from_pretrained(a	)
       self.assertIsNotNone(a	)


   @slow
   @require_torch_gpu
   def 	SCREAMING_SNAKE_CASE_							(   self      :    Union[str, Any]	)->       int:





     """simple docstring"""







     lowercase__      ,   lowercase__           =  self.model_tester.prepare_config_and_inputs_for_common()
     for model_class in self.all_model_classes:
       # BertForMultipleChoice behaves incorrectly in JIT environments.
       if model_class == DistilBertForMultipleChoice:
         return

       lowercase__           =  True
       lowercase__           =  model_class(config=a	)

       lowercase__           =  self._prepare_for_class(a      ,					a	)
       lowercase__           =  torch.jit.trace(
           a      ,					(inputs_dict['input_ids'].to('cpu'	), inputs_dict['attention_mask'].to('cpu'	))	)

       with tempfile.TemporaryDirectory() as tmp:
         torch.jit.save(a      ,					os.path.join(a      ,					'traced_model.pt'	)	)
         lowercase__           =  torch.jit.load(os.path.join(a      ,					'traced_model.pt'	)      ,					map_location=a	)
         loaded(inputs_dict['input_ids'].to(a	)      ,					inputs_dict['attention_mask'].to(a	)	)






@require_torch
class     SCREAMING_SNAKE_CASE       (unittest.TestCase				):


   @slow
   def 	SCREAMING_SNAKE_CASE_							(   self      :    Union[str, Any]	)->       Tuple:





     """simple docstring"""







     lowercase__           =  DistilBertModel.from_pretrained('distilbert-base-uncased'	)
     lowercase__           =  torch.tensor([[0, 345, 232, 328, 740, 140, 1_695, 69, 6_078, 1_588, 2]]	)
     lowercase__           =  torch.tensor([[0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]	)
     with torch.no_grad():
       lowercase__           =  model(a      ,					attention_mask=a	)[0]
     lowercase__           =  torch.Size((1, 11, 768)	)
     self.assertEqual(output.shape      ,					a	)
     lowercase__           =  torch.tensor(
         [[[-0.1639, 0.3299, 0.1648], [-0.1746, 0.3289, 0.1710], [-0.1884, 0.3357, 0.1810]]]	)

     self.assertTrue(torch.allclose(output[:, 1:4, 1:4]      ,					a      ,					atol=1E-4	)	)









import argparse
import json
import re
from pathlib import Path

import requests
import torch
from huggingface_hub import hf_hub_download
from PIL import Image

from transformers import (
    MobileNetVaConfig,
    MobileNetVaForImageClassification,
    MobileNetVaImageProcessor,
    load_tf_weights_in_mobilenet_va,
)
from transformers.utils import logging


logging.set_verbosity_info()
lowercase_          =		logging.get_logger(__name__)




def       __UpperCamelCase  (_SCREAMING_SNAKE_CASE		)    ->       Any:
  lowercase__           =  MobileNetVaConfig(layer_norm_eps=0.0_0_1		)

  if "_quant" in model_name:
    raise ValueError('Quantized models are not supported.'		)

  lowercase__           =  re.match(R'^mobilenet_v1_([^_]*)_([^_]*)$'		,						_SCREAMING_SNAKE_CASE		)
  if matches:
    lowercase__           =  float(matches[1]		)
    lowercase__           =  int(matches[2]		)

  # The TensorFlow version of MobileNetV1 predicts 1001 classes instead of
  # the usual 1000. The first class (index 0) is "background".
  lowercase__           =  1001
  lowercase__           =  'imagenet-1k-id2label.json'
  lowercase__           =  'huggingface/label-files'
  lowercase__           =  json.load(open(hf_hub_download(_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		,						repo_type='dataset'		)		,						'r'		)		)
  lowercase__           =  {int(_SCREAMING_SNAKE_CASE		) + 1: v for k, v in idalabel.items()}
  lowercase__           =  'background'
  lowercase__           =  idalabel
  lowercase__           =  {v: k for k, v in idalabel.items()}

  return config




def       __UpperCamelCase  ()    ->       int:
  lowercase__           =  'http://images.cocodataset.org/val2017/000000039769.jpg'
  lowercase__           =  Image.open(requests.get(_SCREAMING_SNAKE_CASE		,						stream=_SCREAMING_SNAKE_CASE		).raw		)
  return im




@torch.no_grad()
def       __UpperCamelCase  (_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE=False		)    ->       Union[str, Any]:
  lowercase__           =  get_mobilenet_va_config(_SCREAMING_SNAKE_CASE		)

  # Load 🤗 model
  lowercase__           =  MobileNetVaForImageClassification(_SCREAMING_SNAKE_CASE		).eval()

  # Load weights from TensorFlow checkpoint
  load_tf_weights_in_mobilenet_va(_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		,						_SCREAMING_SNAKE_CASE		)

  # Check outputs on an image, prepared by MobileNetV1ImageProcessor
  lowercase__           =  MobileNetVaImageProcessor(
      crop_size={'width': config.image_size, 'height': config.image_size}		,						size={'shortest_edge': config.image_size + 32}		,						)
  lowercase__           =  image_processor(images=prepare_img()		,						return_tensors='pt'		)
  lowercase__           =  model(**_SCREAMING_SNAKE_CASE		)
  lowercase__           =  outputs.logits

  assert logits.shape == (1, 1001)

  if model_name == "mobilenet_v1_1.0_224":
    lowercase__           =  torch.tensor([-4.1_7_3_9, -1.1_2_3_3, 3.1_2_0_5]		)
  elif model_name == "mobilenet_v1_0.75_192":
    lowercase__           =  torch.tensor([-3.9_4_4_0, -2.3_1_4_1, -0.3_3_3_3]		)
  else:
    lowercase__           =  None

  if expected_logits is not None:
    assert torch.allclose(logits[0, :3]		,						_SCREAMING_SNAKE_CASE		,						atol=1E-4		)

  Path(_SCREAMING_SNAKE_CASE		).mkdir(exist_ok=_SCREAMING_SNAKE_CASE		)
  print(F"""Saving model {model_name} to {pytorch_dump_folder_path}"""		)
  model.save_pretrained(_SCREAMING_SNAKE_CASE		)
  print(F"""Saving image processor to {pytorch_dump_folder_path}"""		)
  image_processor.save_pretrained(_SCREAMING_SNAKE_CASE		)

  if push_to_hub:
    print('Pushing to the hub...'		)
    lowercase__           =  'google/' + model_name
    image_processor.push_to_hub(_SCREAMING_SNAKE_CASE		)
    model.push_to_hub(_SCREAMING_SNAKE_CASE		)


if __name__ == "__main__":
      lowercase_          =		argparse.ArgumentParser()
      # Required parameters
      parser.add_argument(
          """--model_name""",
          default="""mobilenet_v1_1.0_224""",
          type=str,
          help="""Name of the MobileNetV1 model you'd like to convert. Should in the form 'mobilenet_v1_<depth>_<size>'.""",
      )
      parser.add_argument(
          """--checkpoint_path""", required=True, type=str, help="""Path to the original TensorFlow checkpoint (.ckpt file)."""
      )
      parser.add_argument(
          """--pytorch_dump_folder_path""", required=True, type=str, help="""Path to the output PyTorch model directory."""
      )
      parser.add_argument(
          """--push_to_hub""", action="""store_true""", help="""Whether or not to push the converted model to the 🤗 hub."""
      )

      lowercase_          =		parser.parse_args()
      convert_movilevit_checkpoint(
          args.model_name, args.checkpoint_path, args.pytorch_dump_folder_path, args.push_to_hub
      )


