






"""simple docstring"""





import inspect
import unittest

import requests

from transformers import MgpstrConfig
from transformers.testing_utils import require_torch, require_vision, slow, torch_device
from transformers.utils import is_torch_available, is_vision_available

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


if is_torch_available():
  import torch
  from torch import nn

  from transformers import MgpstrForSceneTextRecognition


if is_vision_available():
  from PIL import Image

  from transformers import MgpstrProcessor







class 						_a  :





   '''simple docstring'''






   def __init__(       self					, UpperCAmelCase_					, UpperCAmelCase_=False					, UpperCAmelCase_=13					, UpperCAmelCase_=(32, 128)					, UpperCAmelCase_=4					, UpperCAmelCase_=3					, UpperCAmelCase_=27					, UpperCAmelCase_=38					, UpperCAmelCase_=99					, UpperCAmelCase_=99					, UpperCAmelCase_=32					, UpperCAmelCase_=5					, UpperCAmelCase_=4					, UpperCAmelCase_=4.0					, UpperCAmelCase_=257					, UpperCAmelCase_=None					, )			->   Union[str, Any]:




          '''simple docstring'''




          lowercase__:							List[Any]         =     parent
          lowercase__:							Dict         =     is_training
          lowercase__:							List[Any]         =     batch_size
          lowercase__:							Tuple         =     image_size
          lowercase__:							List[str]         =     patch_size
          lowercase__:							Tuple         =     num_channels
          lowercase__:							int         =     max_token_length
          lowercase__:							int         =     num_character_labels
          lowercase__:							List[str]         =     num_bpe_labels
          lowercase__:							Dict         =     num_wordpiece_labels
          lowercase__:							Any         =     hidden_size
          lowercase__:							List[str]         =     num_hidden_layers
          lowercase__:							str         =     num_attention_heads
          lowercase__:							Any         =     mlp_ratio
          lowercase__:							Dict         =     patch_embeds_hidden_size
          lowercase__:							Any         =     output_hidden_states





   def __lowercase							(       self)			->   str:




          '''simple docstring'''




          lowercase__:							Union[str, Any]         =     floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]])
          lowercase__:							str         =     self.get_config()
          return config, pixel_values





   def __lowercase							(       self)			->   Any:




          '''simple docstring'''




          return MgpstrConfig(
              image_size=self.image_size					, patch_size=self.patch_size					, num_channels=self.num_channels					, max_token_length=self.max_token_length					, num_character_labels=self.num_character_labels					, num_bpe_labels=self.num_bpe_labels					, num_wordpiece_labels=self.num_wordpiece_labels					, hidden_size=self.hidden_size					, num_hidden_layers=self.num_hidden_layers					, num_attention_heads=self.num_attention_heads					, mlp_ratio=self.mlp_ratio					, output_hidden_states=self.output_hidden_states					, )





   def __lowercase							(       self					, UpperCAmelCase_					, UpperCAmelCase_)			->   Optional[Any]:




          '''simple docstring'''




          lowercase__:							Dict         =     MgpstrForSceneTextRecognition(UpperCAmelCase_)
          model.to(UpperCAmelCase_)
          model.eval()
          with torch.no_grad():
                 lowercase__:							Optional[Any]         =     model(UpperCAmelCase_)
          self.parent.assertEqual(
              generated_ids[0][0].shape					, (self.batch_size, self.max_token_length, self.num_character_labels))





   def __lowercase							(       self)			->   int:




          '''simple docstring'''




          lowercase__:							Optional[int]         =     self.prepare_config_and_inputs()
          lowercase__    ,      lowercase__:							Dict         =     config_and_inputs
          lowercase__:							str         =     {"pixel_values": pixel_values}
          return config, inputs_dict







@require_torch
class 						_a  (		lowercase_							,	lowercase_							,	unittest.TestCase       ):





   '''simple docstring'''






   UpperCamelCase__     	=							(MgpstrForSceneTextRecognition,) if is_torch_available() else ()
   UpperCamelCase__     	=							{"""feature-extraction""": MgpstrForSceneTextRecognition} if is_torch_available() else {}
   UpperCamelCase__     	=							False

   UpperCamelCase__     	=							False
   UpperCamelCase__     	=							False
   UpperCamelCase__     	=							False
   UpperCamelCase__     	=							False





   def __lowercase							(       self)			->   str:




          '''simple docstring'''




          lowercase__:							str         =     MgpstrModelTester(self)
          lowercase__:							Tuple         =     ConfigTester(self					, config_class=UpperCAmelCase_					, has_text_modality=UpperCAmelCase_)





   def __lowercase							(       self)			->   List[Any]:




          '''simple docstring'''




          self.config_tester.run_common_tests()





   def __lowercase							(       self)			->   Tuple:




          '''simple docstring'''




          lowercase__:							str         =     self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*UpperCAmelCase_)





   @unittest.skip(reason="MgpstrModel does not use inputs_embeds")
   def __lowercase							(       self)			->   Union[str, Any]:




          '''simple docstring'''




          pass





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




          '''simple docstring'''




          lowercase__    ,      lowercase__:							int         =     self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                 lowercase__:							List[Any]         =     model_class(UpperCAmelCase_)
                 self.assertIsInstance(model.get_input_embeddings()					, (nn.Module))
                 lowercase__:							Dict         =     model.get_output_embeddings()
                 self.assertTrue(x is None or isinstance(UpperCAmelCase_					, nn.Linear))





   def __lowercase							(       self)			->   str:




          '''simple docstring'''




          lowercase__    ,      lowercase__:							str         =     self.model_tester.prepare_config_and_inputs_for_common()

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

                 lowercase__:							Any         =     ["pixel_values"]
                 self.assertListEqual(arg_names[:1]					, UpperCAmelCase_)





   @unittest.skip(reason="MgpstrModel does not support feedforward chunking")
   def __lowercase							(       self)			->   Optional[Any]:




          '''simple docstring'''




          pass





   def __lowercase							(       self)			->   Tuple:




          '''simple docstring'''




          lowercase__    ,      lowercase__:							str         =     self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                 if not model_class.supports_gradient_checkpointing:
                        continue

                 lowercase__:							List[str]         =     True
                 lowercase__:							Tuple         =     model_class(UpperCAmelCase_)
                 self.assertTrue(model.is_gradient_checkpointing)





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




          '''simple docstring'''




          def check_hidden_states_output(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_):
                 lowercase__:							Optional[Any]         =     model_class(UpperCAmelCase_)
                 model.to(UpperCAmelCase_)
                 model.eval()

                 with torch.no_grad():
                        lowercase__:							str         =     model(**self._prepare_for_class(UpperCAmelCase_					, UpperCAmelCase_))

                 lowercase__:							Union[str, Any]         =     outputs.hidden_states

                 lowercase__:							Any         =     getattr(
                     self.model_tester					, "expected_num_hidden_layers"					, self.model_tester.num_hidden_layers + 1)
                 self.assertEqual(len(UpperCAmelCase_)					, UpperCAmelCase_)

                 self.assertListEqual(
                     list(hidden_states[0].shape[-2:])					, [self.model_tester.patch_embeds_hidden_size, self.model_tester.hidden_size]					, )

          lowercase__    ,      lowercase__:							Optional[int]         =     self.model_tester.prepare_config_and_inputs_for_common()

          for model_class in self.all_model_classes:
                 lowercase__:							List[str]         =     True
                 check_hidden_states_output(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_)

                 # check that output_hidden_states also work using config
                 del inputs_dict["output_hidden_states"]
                 lowercase__:							Tuple         =     True

                 check_hidden_states_output(UpperCAmelCase_					, UpperCAmelCase_					, UpperCAmelCase_)





   def __lowercase							(       self)			->   Optional[int]:




          '''simple docstring'''




          lowercase__    ,      lowercase__:							Any         =     self.model_tester.prepare_config_and_inputs_for_common()

          lowercase__:							Dict         =     _config_zero_init(UpperCAmelCase_)
          for model_class in self.all_model_classes:
                 lowercase__:							str         =     model_class(config=UpperCAmelCase_)
                 for name, param in model.named_parameters():
                        if isinstance(UpperCAmelCase_					, (nn.Linear, nn.Convad, nn.LayerNorm)):
                               if param.requires_grad:
                                      self.assertIn(
                                          ((param.data.mean() * 1E9).round() / 1E9).item()					, [0.0, 1.0]					, msg=F"""Parameter {name} of model {model_class} seems not properly initialized"""					, )





   @unittest.skip(reason="Retain_grad is tested in individual model tests")
   def __lowercase							(       self)			->   Tuple:




          '''simple docstring'''




          pass







def 							A(    ):
       """simple docstring"""





       lowercase__:							int         =     "https://i.postimg.cc/ZKwLg2Gw/367-14.png"
       lowercase__:							Dict         =     Image.open(requests.get(snake_case_		,  stream=snake_case_						).raw						).convert("RGB"						)
       return im







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





   '''simple docstring'''






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




          '''simple docstring'''




          lowercase__:							Dict         =     "alibaba-damo/mgp-str-base"
          lowercase__:							int         =     MgpstrForSceneTextRecognition.from_pretrained(UpperCAmelCase_).to(UpperCAmelCase_)
          lowercase__:							str         =     MgpstrProcessor.from_pretrained(UpperCAmelCase_)

          lowercase__:							Union[str, Any]         =     prepare_img()
          lowercase__:							List[Any]         =     processor(images=UpperCAmelCase_					, return_tensors="pt").pixel_values.to(UpperCAmelCase_)

          # forward pass
          with torch.no_grad():
                 lowercase__:							Dict         =     model(UpperCAmelCase_)

          # verify the logits
          self.assertEqual(outputs.logits[0].shape					, torch.Size((1, 27, 38)))

          lowercase__:							Any         =     processor.batch_decode(outputs.logits)
          lowercase__:							Optional[Any]         =     "ticket"

          self.assertEqual(out_strs["generated_text"][0]					, UpperCAmelCase_)

          lowercase__:							Optional[int]         =     torch.tensor(
              [[[-39.53_97, -44.40_24, -36.18_44], [-61.47_09, -63.86_39, -58.34_54], [-74.02_25, -68.54_94, -71.21_64]]]					, device=UpperCAmelCase_					, )

          self.assertTrue(torch.allclose(outputs.logits[0][:, 1:4, 1:4]					, UpperCAmelCase_					, atol=1E-4))







'''simple docstring'''





from collections import OrderedDict
from typing import Mapping

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


UpperCamelCase       :     Dict			=    logging.get_logger(__name__)

UpperCamelCase       :     Tuple			=    {
    """flaubert/flaubert_small_cased""": """https://huggingface.co/flaubert/flaubert_small_cased/resolve/main/config.json""",
    """flaubert/flaubert_base_uncased""": """https://huggingface.co/flaubert/flaubert_base_uncased/resolve/main/config.json""",
    """flaubert/flaubert_base_cased""": """https://huggingface.co/flaubert/flaubert_base_cased/resolve/main/config.json""",
    """flaubert/flaubert_large_cased""": """https://huggingface.co/flaubert/flaubert_large_cased/resolve/main/config.json""",
}



class   UpperCamelCase					(       a_       ):







    """simple docstring"""





    A     :    int				=			"flaubert"
    A     :    List[str]				=			{
        "hidden_size": "emb_dim",
        "num_attention_heads": "n_heads",
        "num_hidden_layers": "n_layers",
        "n_words": "vocab_size",  # For backward compatibility
    }





    def __init__(					self     :						str							,							UpperCAmelCase_     :						Optional[int]=False							,							UpperCAmelCase_     :						List[Any]=0.0							,							UpperCAmelCase_     :						List[Any]=3_0_1_4_5							,							UpperCAmelCase_     :						Union[str, Any]=2_0_4_8							,							UpperCAmelCase_     :						Tuple=1_2							,							UpperCAmelCase_     :						str=1_6							,							UpperCAmelCase_     :						Tuple=0.1							,							UpperCAmelCase_     :						str=0.1							,							UpperCAmelCase_     :						str=True							,							UpperCAmelCase_     :						Union[str, Any]=False							,							UpperCAmelCase_     :						Any=False							,							UpperCAmelCase_     :						str=False							,							UpperCAmelCase_     :						Tuple=1							,							UpperCAmelCase_     :						str=True							,							UpperCAmelCase_     :						Tuple=5_1_2							,							UpperCAmelCase_     :						List[str]=2_0_4_8**-0.5							,							UpperCAmelCase_     :						List[Any]=1e-12							,							UpperCAmelCase_     :						Tuple=0.02							,							UpperCAmelCase_     :						Any=0							,							UpperCAmelCase_     :						List[Any]=1							,							UpperCAmelCase_     :						str=2							,							UpperCAmelCase_     :						Optional[Any]=3							,							UpperCAmelCase_     :						List[str]=5							,							UpperCAmelCase_     :						str=True							,							UpperCAmelCase_     :						List[Any]="first"							,							UpperCAmelCase_     :						Any=True							,							UpperCAmelCase_     :						Union[str, Any]=None							,							UpperCAmelCase_     :						Union[str, Any]=True							,							UpperCAmelCase_     :						Any=0.1							,							UpperCAmelCase_     :						List[Any]=5							,							UpperCAmelCase_     :						List[Any]=5							,							UpperCAmelCase_     :						Union[str, Any]=0							,							UpperCAmelCase_     :						Optional[Any]=0							,							UpperCAmelCase_     :						List[Any]=2							,							UpperCAmelCase_     :						List[Any]=0							,							**UpperCAmelCase_     :						List[Any]							,							):


         """simple docstring"""







         a					:							Any							= pre_norm
         a					:							str							= layerdrop
         a					:							int							= vocab_size
         a					:							str							= emb_dim
         a					:							Any							= n_layers
         a					:							Dict							= n_heads
         a					:							Union[str, Any]							= dropout
         a					:							List[Any]							= attention_dropout
         a					:							List[str]							= gelu_activation
         a					:							int							= sinusoidal_embeddings
         a					:							Union[str, Any]							= causal
         a					:							str							= asm
         a					:							Tuple							= n_langs
         a					:							Optional[Any]							= use_lang_emb
         a					:							str							= layer_norm_eps
         a					:							Optional[Any]							= bos_index
         a					:							Union[str, Any]							= eos_index
         a					:							str							= pad_index
         a					:							List[str]							= unk_index
         a					:							Dict							= mask_index
         a					:							Optional[Any]							= is_encoder
         a					:							str							= max_position_embeddings
         a					:							Optional[Any]							= embed_init_std
         a					:							str							= init_std
         a					:							Union[str, Any]							= summary_type
         a					:							Tuple							= summary_use_proj
         a					:							Tuple							= summary_activation
         a					:							str							= summary_proj_to_labels
         a					:							List[str]							= summary_first_dropout
         a					:							Any							= start_n_top
         a					:							Optional[Any]							= end_n_top
         a					:							List[Any]							= mask_token_id
         a					:							Optional[Any]							= lang_id

         if "n_words" in kwargs:
              a					:							int							= kwargs['n_words']

         super().__init__(pad_token_id=UpperCAmelCase_							,							bos_token_id=UpperCAmelCase_							,							**UpperCAmelCase_)






class   UpperCamelCase					(       a_       ):







    """simple docstring"""





    @property
    def    SCREAMING_SNAKE_CASE_     (					self     :						str):


         """simple docstring"""







         if self.task == "multiple-choice":
              a					:							int							= {0: 'batch', 1: 'choice', 2: 'sequence'}
         else:
              a					:							Tuple							= {0: 'batch', 1: 'sequence'}
         return OrderedDict(
             [
                 ('input_ids', dynamic_axis),
                 ('attention_mask', dynamic_axis),
             ])

