



import inspect
import unittest

from transformers import RegNetConfig
from transformers.file_utils import cached_property, is_torch_available, is_vision_available
from transformers.testing_utils import require_torch, require_vision, slow, torch_device

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


if is_torch_available():
  import torch
  from torch import nn

  from transformers import RegNetForImageClassification, RegNetModel
  from transformers.models.regnet.modeling_regnet import REGNET_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
  from PIL import Image

  from transformers import AutoImageProcessor





class 				__lowerCAmelCase  :




  def __init__(       self							,     lowerCAmelCase							,     lowerCAmelCase=3							,     lowerCAmelCase=32							,     lowerCAmelCase=3							,     lowerCAmelCase=10							,     lowerCAmelCase=[10, 20, 30, 40]							,     lowerCAmelCase=[1, 1, 2, 1]							,     lowerCAmelCase=True							,     lowerCAmelCase=True							,     lowerCAmelCase="relu"							,     lowerCAmelCase=3							,     lowerCAmelCase=None							,     )						->				Dict:
        '''simple docstring'''






        _lowercase			 =parent
        _lowercase			 =batch_size
        _lowercase			 =image_size
        _lowercase			 =num_channels
        _lowercase			 =embeddings_size
        _lowercase			 =hidden_sizes
        _lowercase			 =depths
        _lowercase			 =is_training
        _lowercase			 =use_labels
        _lowercase			 =hidden_act
        _lowercase			 =num_labels
        _lowercase			 =scope
        _lowercase			 =len(lowerCAmelCase      )




  def 						A__  (       self      )						->				Optional[Any]:
        '''simple docstring'''






        _lowercase			 =floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]      )

        _lowercase			 =None
        if self.use_labels:
              _lowercase			 =ids_tensor([self.batch_size]							,     self.num_labels      )

        _lowercase			 =self.get_config()

        return config, pixel_values, labels




  def 						A__  (       self      )						->				List[Any]:
        '''simple docstring'''






        return RegNetConfig(
            num_channels=self.num_channels							,     embeddings_size=self.embeddings_size							,     hidden_sizes=self.hidden_sizes							,     depths=self.depths							,     hidden_act=self.hidden_act							,     num_labels=self.num_labels							,     )




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase      )						->				List[Any]:
        '''simple docstring'''






        _lowercase			 =RegNetModel(config=lowerCAmelCase      )
        model.to(lowerCAmelCase      )
        model.eval()
        _lowercase			 =model(lowerCAmelCase      )
        # expected last hidden states: B, C, H // 32, W // 32
        self.parent.assertEqual(
            result.last_hidden_state.shape							,     (self.batch_size, self.hidden_sizes[-1], self.image_size // 32, self.image_size // 32)							,     )




  def 						A__  (       self							,     lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase      )						->				Optional[Any]:
        '''simple docstring'''






        _lowercase			 =self.num_labels
        _lowercase			 =RegNetForImageClassification(lowerCAmelCase      )
        model.to(lowerCAmelCase      )
        model.eval()
        _lowercase			 =model(lowerCAmelCase							,     labels=lowerCAmelCase      )
        self.parent.assertEqual(result.logits.shape							,     (self.batch_size, self.num_labels)      )




  def 						A__  (       self      )						->				Any:
        '''simple docstring'''






        _lowercase			 =self.prepare_config_and_inputs()
        _lowercase , _lowercase , _lowercase			 =config_and_inputs
        _lowercase			 ={'pixel_values': pixel_values}
        return config, inputs_dict





@require_torch
class 				__lowerCAmelCase  (      UpperCamelCase__    ,    UpperCamelCase__    ,    unittest.TestCase							):
  _a		    =						(RegNetModel, RegNetForImageClassification) if is_torch_available() else ()
  _a		    =						(
      {"""feature-extraction""": RegNetModel, """image-classification""": RegNetForImageClassification}
      if is_torch_available()
      else {}
  )

  _a		    =						False
  _a		    =						False
  _a		    =						False
  _a		    =						False




  def 						A__  (       self      )						->				str:
        '''simple docstring'''






        _lowercase			 =RegNetModelTester(self      )
        _lowercase			 =ConfigTester(self							,     config_class=lowerCAmelCase							,     has_text_modality=lowerCAmelCase      )




  def 						A__  (       self      )						->				List[str]:
        '''simple docstring'''






        self.create_and_test_config_common_properties()
        self.config_tester.create_and_test_config_to_json_string()
        self.config_tester.create_and_test_config_to_json_file()
        self.config_tester.create_and_test_config_from_and_save_pretrained()
        self.config_tester.create_and_test_config_with_num_labels()
        self.config_tester.check_config_can_be_init_without_params()
        self.config_tester.check_config_arguments_init()




  def 						A__  (       self      )						->				Optional[int]:
        '''simple docstring'''






        return




  @unittest.skip(reason='RegNet does not use inputs_embeds'      )
  def 						A__  (       self      )						->				int:
        '''simple docstring'''






        pass




  @unittest.skip(reason='RegNet does not support input and output embeddings'      )
  def 						A__  (       self      )						->				int:
        '''simple docstring'''






        pass




  def 						A__  (       self      )						->				Optional[Any]:
        '''simple docstring'''






        _lowercase , _lowercase			 =self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
              _lowercase			 =model_class(lowerCAmelCase      )
              _lowercase			 =inspect.signature(model.forward      )
              # signature.parameters is an OrderedDict => so arg_names order is deterministic
              _lowercase			 =[*signature.parameters.keys()]

              _lowercase			 =['pixel_values']
              self.assertListEqual(arg_names[:1]							,     lowerCAmelCase      )




  def 						A__  (       self      )						->				List[Any]:
        '''simple docstring'''






        _lowercase			 =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*lowerCAmelCase      )




  def 						A__  (       self      )						->				List[Any]:
        '''simple docstring'''






        _lowercase , _lowercase			 =self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
              _lowercase			 =model_class(config=lowerCAmelCase      )
              for name, module in model.named_modules():
                    if isinstance(lowerCAmelCase							,     (nn.BatchNormad, nn.GroupNorm)      ):
                          self.assertTrue(
                              torch.all(module.weight == 1      )							,     msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''							,     )
                          self.assertTrue(
                              torch.all(module.bias == 0      )							,     msg=F'''Parameter {name} of model {model_class} seems not properly initialized'''							,     )




  def 						A__  (       self      )						->				Tuple:
        '''simple docstring'''






        def check_hidden_states_output(lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase      ):
              _lowercase			 =model_class(lowerCAmelCase      )
              model.to(lowerCAmelCase      )
              model.eval()

              with torch.no_grad():
                    _lowercase			 =model(**self._prepare_for_class(lowerCAmelCase							,     lowerCAmelCase      )      )

              _lowercase			 =outputs.encoder_hidden_states if config.is_encoder_decoder else outputs.hidden_states

              _lowercase			 =self.model_tester.num_stages
              self.assertEqual(len(lowerCAmelCase      )							,     expected_num_stages + 1      )

              # RegNet's feature maps are of shape (batch_size, num_channels, height, width)
              self.assertListEqual(
                  list(hidden_states[0].shape[-2:]      )							,     [self.model_tester.image_size // 2, self.model_tester.image_size // 2]							,     )

        _lowercase , _lowercase			 =self.model_tester.prepare_config_and_inputs_for_common()
        _lowercase			 =['basic', 'bottleneck']
        for model_class in self.all_model_classes:
              for layer_type in layers_type:
                    _lowercase			 =layer_type
                    _lowercase			 =True
                    check_hidden_states_output(lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase      )

                    # check that output_hidden_states also work using config
                    del inputs_dict["output_hidden_states"]
                    _lowercase			 =True

                    check_hidden_states_output(lowerCAmelCase							,     lowerCAmelCase							,     lowerCAmelCase      )




  def 						A__  (       self      )						->				List[Any]:
        '''simple docstring'''






        _lowercase			 =self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_image_classification(*lowerCAmelCase      )




  @slow
  def 						A__  (       self      )						->				Dict:
        '''simple docstring'''






        for model_name in REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
              _lowercase			 =RegNetModel.from_pretrained(lowerCAmelCase      )
              self.assertIsNotNone(lowerCAmelCase      )
def   a   (    )					->   Union[str, Any]:





      """simple docstring"""



      _lowercase			 =Image.open('./tests/fixtures/tests_samples/COCO/000000039769.png'  )
      return image







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




  @cached_property
  def 						A__  (       self      )						->				Dict:
        '''simple docstring'''






        return (
            AutoImageProcessor.from_pretrained(REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]      )
            if is_vision_available()
            else None
        )




  @slow
  def 						A__  (       self      )						->				int:
        '''simple docstring'''






        _lowercase			 =RegNetForImageClassification.from_pretrained(REGNET_PRETRAINED_MODEL_ARCHIVE_LIST[0]      ).to(lowerCAmelCase      )

        _lowercase			 =self.default_image_processor
        _lowercase			 =prepare_img()
        _lowercase			 =image_processor(images=lowerCAmelCase							,     return_tensors='pt'      ).to(lowerCAmelCase      )

        # forward pass
        with torch.no_grad():
              _lowercase			 =model(**lowerCAmelCase      )

        # verify the logits
        _lowercase			 =torch.Size((1, 1_000)      )
        self.assertEqual(outputs.logits.shape							,     lowerCAmelCase      )

        _lowercase			 =torch.tensor([-0.4180, -1.5051, -3.4836]      ).to(lowerCAmelCase      )

        self.assertTrue(torch.allclose(outputs.logits[0, :3]							,     lowerCAmelCase							,     atol=1e-4      )      )





import argparse

import torch
from datasets import load_dataset
from donut import DonutModel

from transformers import (
    DonutImageProcessor,
    DonutProcessor,
    DonutSwinConfig,
    DonutSwinModel,
    MBartConfig,
    MBartForCausalLM,
    VisionEncoderDecoderModel,
    XLMRobertaTokenizerFast,
)
def   a   (    A__			:     Tuple  )					->   Any:





      """simple docstring"""



      _lowercase			 =model.config

      _lowercase			 =DonutSwinConfig(
          image_size=original_config.input_size   ,     patch_size=4   ,     depths=original_config.encoder_layer   ,     num_heads=[4, 8, 16, 32]   ,     window_size=original_config.window_size   ,     embed_dim=128   ,     )
      _lowercase			 =MBartConfig(
          is_decoder=A__   ,     is_encoder_decoder=A__   ,     add_cross_attention=A__   ,     decoder_layers=original_config.decoder_layer   ,     max_position_embeddings=original_config.max_position_embeddings   ,     vocab_size=len(
              model.decoder.tokenizer  )   ,     scale_embedding=A__   ,     add_final_layer_norm=A__   ,     )

      return encoder_config, decoder_config
def   a   (    A__			:     Union[str, Any]  )					->   str:





      """simple docstring"""



      if "encoder.model" in name:
            _lowercase			 =name.replace('encoder.model'   ,     'encoder'  )
      if "decoder.model" in name:
            _lowercase			 =name.replace('decoder.model'   ,     'decoder'  )
      if "patch_embed.proj" in name:
            _lowercase			 =name.replace('patch_embed.proj'   ,     'embeddings.patch_embeddings.projection'  )
      if "patch_embed.norm" in name:
            _lowercase			 =name.replace('patch_embed.norm'   ,     'embeddings.norm'  )
      if name.startswith('encoder'  ):
            if "layers" in name:
                  _lowercase			 ='encoder.' + name
            if "attn.proj" in name:
                  _lowercase			 =name.replace('attn.proj'   ,     'attention.output.dense'  )
            if "attn" in name and "mask" not in name:
                  _lowercase			 =name.replace('attn'   ,     'attention.self'  )
            if "norm1" in name:
                  _lowercase			 =name.replace('norm1'   ,     'layernorm_before'  )
            if "norm2" in name:
                  _lowercase			 =name.replace('norm2'   ,     'layernorm_after'  )
            if "mlp.fc1" in name:
                  _lowercase			 =name.replace('mlp.fc1'   ,     'intermediate.dense'  )
            if "mlp.fc2" in name:
                  _lowercase			 =name.replace('mlp.fc2'   ,     'output.dense'  )

            if name == "encoder.norm.weight":
                  _lowercase			 ='encoder.layernorm.weight'
            if name == "encoder.norm.bias":
                  _lowercase			 ='encoder.layernorm.bias'

      return name
def   a   (    A__			:     Optional[Any]   ,     A__			:     List[Any]  )					->   List[str]:





      """simple docstring"""



      for key in orig_state_dict.copy().keys():
            _lowercase			 =orig_state_dict.pop(A__  )

            if "qkv" in key:
                  _lowercase			 =key.split('.'  )
                  _lowercase			 =int(key_split[3]  )
                  _lowercase			 =int(key_split[5]  )
                  _lowercase			 =model.encoder.encoder.layers[layer_num].blocks[block_num].attention.self.all_head_size

                  if "weight" in key:
                        _lowercase			 =val[:dim, :]
                        _lowercase			 =val[dim : dim * 2, :]
                        _lowercase			 =val[-dim:, :]
                  else:
                        _lowercase			 =val[:dim]
                        _lowercase			 =val[dim : dim * 2]
                        _lowercase			 =val[-dim:]
            elif "attn_mask" in key or key in ["encoder.model.norm.weight", "encoder.model.norm.bias"]:
                  # HuggingFace implementation doesn't use attn_mask buffer
                  # and model doesn't use final LayerNorms for the encoder
                  pass
            else:
                  _lowercase			 =val

      return orig_state_dict
def   a   (    A__			:     str   ,     A__			:     List[str]=None   ,     A__			:     List[Any]=False  )					->   List[str]:





      """simple docstring"""



      _lowercase			 =DonutModel.from_pretrained(A__  ).eval()

      # load HuggingFace model
      _lowercase , _lowercase			 =get_configs(A__  )
      _lowercase			 =DonutSwinModel(A__  )
      _lowercase			 =MBartForCausalLM(A__  )
      _lowercase			 =VisionEncoderDecoderModel(encoder=A__   ,     decoder=A__  )
      model.eval()

      _lowercase			 =original_model.state_dict()
      _lowercase			 =convert_state_dict(A__   ,     A__  )
      model.load_state_dict(A__  )

      # verify results on scanned document
      _lowercase			 =load_dataset('hf-internal-testing/example-documents'  )
      _lowercase			 =dataset['test'][0]['image'].convert('RGB'  )

      _lowercase			 =XLMRobertaTokenizerFast.from_pretrained(A__   ,     from_slow=A__  )
      _lowercase			 =DonutImageProcessor(
          do_align_long_axis=original_model.config.align_long_axis   ,     size=original_model.config.input_size[::-1]  )
      _lowercase			 =DonutProcessor(A__   ,     A__  )
      _lowercase			 =processor(A__   ,     return_tensors='pt'  ).pixel_values

      if model_name == "naver-clova-ix/donut-base-finetuned-docvqa":
            _lowercase			 ='<s_docvqa><s_question>{user_input}</s_question><s_answer>'
            _lowercase			 ='When is the coffee break?'
            _lowercase			 =task_prompt.replace('{user_input}'   ,     A__  )
      elif model_name == "naver-clova-ix/donut-base-finetuned-rvlcdip":
            _lowercase			 ='<s_rvlcdip>'
      elif model_name in [
          "naver-clova-ix/donut-base-finetuned-cord-v1",
          "naver-clova-ix/donut-base-finetuned-cord-v1-2560",
      ]:
            _lowercase			 ='<s_cord>'
      elif model_name == "naver-clova-ix/donut-base-finetuned-cord-v2":
            _lowercase			 ='s_cord-v2>'
      elif model_name == "naver-clova-ix/donut-base-finetuned-zhtrainticket":
            _lowercase			 ='<s_zhtrainticket>'
      elif model_name in ["naver-clova-ix/donut-proto", "naver-clova-ix/donut-base"]:
            # use a random prompt
            _lowercase			 ='hello world'
      else:
            raise ValueError('Model name not supported'  )
      _lowercase			 =original_model.decoder.tokenizer(A__   ,     add_special_tokens=A__   ,     return_tensors='pt'  )[
          'input_ids'
      ]

      _lowercase			 =original_model.encoder.model.patch_embed(A__  )
      _lowercase , _lowercase			 =model.encoder.embeddings(A__  )
      assert torch.allclose(A__   ,     A__   ,     atol=1e-3  )

      # verify encoder hidden states
      _lowercase			 =original_model.encoder(A__  )
      _lowercase			 =model.encoder(A__  ).last_hidden_state
      assert torch.allclose(A__   ,     A__   ,     atol=1e-2  )

      # verify decoder hidden states
      _lowercase			 =original_model(A__   ,     A__   ,     A__  ).logits
      _lowercase			 =model(A__   ,     decoder_input_ids=A__  ).logits
      assert torch.allclose(A__   ,     A__   ,     atol=1e-3  )
      print('Looks ok!'  )

      if pytorch_dump_folder_path is not None:
            print(F'''Saving model and processor to {pytorch_dump_folder_path}'''  )
            model.save_pretrained(A__  )
            processor.save_pretrained(A__  )

      if push_to_hub:
            model.push_to_hub('nielsr/' + model_name.split('/'  )[-1]   ,     commit_message='Update model'  )
            processor.push_to_hub('nielsr/' + model_name.split('/'  )[-1]   ,     commit_message='Update model'  )


if __name__ == "__main__":
  lowercase_             =     argparse.ArgumentParser()
  # Required parameters
  parser.add_argument(
      '--model_name',
      default='naver-clova-ix/donut-base-finetuned-docvqa',
      required=False,
      type=str,
      help='Name of the original model you\'d like to convert.',
  )
  parser.add_argument(
      '--pytorch_dump_folder_path',
      default=None,
      required=False,
      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 and processor to the 🤗 hub.',
  )

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


