
"""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 		UpperCAmelCase_	:
 """simple docstring"""




 def __init__( self       : Tuple     ,   a_       : Any     ,   a_       : List[str]=False     ,   a_       : int=13     ,   a_       : Dict=(32, 1_28)     ,   a_       : Optional[int]=4     ,   a_       : List[Any]=3     ,   a_       : Optional[Any]=27     ,   a_       : Any=38     ,   a_       : Optional[int]=99     ,   a_       : Optional[int]=99     ,   a_       : Optional[Any]=32     ,   a_       : Dict=5     ,   a_       : Union[str, Any]=4     ,   a_       : Optional[Any]=4.0     ,   a_       : int=2_57     ,   a_       : List[str]=None     ,   )->					Optional[int]:



      """simple docstring"""

      UpperCAmelCase_						:    int    					=  parent
      UpperCAmelCase_						:    Any    					=  is_training
      UpperCAmelCase_						:    Any    					=  batch_size
      UpperCAmelCase_						:    List[Any]    					=  image_size
      UpperCAmelCase_						:    Optional[int]    					=  patch_size
      UpperCAmelCase_						:    Union[str, Any]    					=  num_channels
      UpperCAmelCase_						:    List[Any]    					=  max_token_length
      UpperCAmelCase_						:    Union[str, Any]    					=  num_character_labels
      UpperCAmelCase_						:    int    					=  num_bpe_labels
      UpperCAmelCase_						:    int    					=  num_wordpiece_labels
      UpperCAmelCase_						:    Union[str, Any]    					=  hidden_size
      UpperCAmelCase_						:    Any    					=  num_hidden_layers
      UpperCAmelCase_						:    str    					=  num_attention_heads
      UpperCAmelCase_						:    List[str]    					=  mlp_ratio
      UpperCAmelCase_						:    Optional[Any]    					=  patch_embeds_hidden_size
      UpperCAmelCase_						:    Optional[Any]    					=  output_hidden_states




 def   a      ( self       : List[str] )->					Dict:



      """simple docstring"""

      UpperCAmelCase_						:    Tuple    					=  floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]] )
      UpperCAmelCase_						:    List[Any]    					=  self.get_config()
      return config, pixel_values




 def   a      ( self       : Tuple )->					List[str]:



      """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   a      ( self       : Tuple     ,   a_       : str     ,   a_       : int )->					List[Any]:



      """simple docstring"""

      UpperCAmelCase_						:    Tuple    					=  MgpstrForSceneTextRecognition(a_ )
      model.to(a_ )
      model.eval()
      with torch.no_grad():
           UpperCAmelCase_						:    str    					=  model(a_ )
      self.parent.assertEqual(
          generated_ids[0][0].shape     ,   (self.batch_size, self.max_token_length, self.num_character_labels) )






 def   a      ( self       : int )->					int:



      """simple docstring"""

      UpperCAmelCase_						:    Union[str, Any]    					=  self.prepare_config_and_inputs()
      UpperCAmelCase_						:    Any    					=  config_and_inputs
      UpperCAmelCase_						:    List[str]    					=  {"""pixel_values""": pixel_values}
      return config, inputs_dict



@require_torch
class 		UpperCAmelCase_	(lowerCamelCase_  , lowerCamelCase_  , unittest.TestCase						):
 """simple docstring"""




 UpperCamelCase_						:     Union[str, Any]				   =    (MgpstrForSceneTextRecognition,) if is_torch_available() else ()
 UpperCamelCase_						:     str				   =    {"""feature-extraction""": MgpstrForSceneTextRecognition} if is_torch_available() else {}
 UpperCamelCase_						:     str				   =    False

 UpperCamelCase_						:     Optional[int]				   =    False
 UpperCamelCase_						:     Tuple				   =    False
 UpperCamelCase_						:     int				   =    False
 UpperCamelCase_						:     List[Any]				   =    False




 def   a      ( self       : str )->					Optional[Any]:



      """simple docstring"""

      UpperCAmelCase_						:    List[Any]    					=  MgpstrModelTester(self )
      UpperCAmelCase_						:    int    					=  ConfigTester(self     ,   config_class=a_     ,   has_text_modality=a_ )




 def   a      ( self       : Dict )->					Tuple:



      """simple docstring"""

      self.config_tester.run_common_tests()




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



      """simple docstring"""

      UpperCAmelCase_						:    Tuple    					=  self.model_tester.prepare_config_and_inputs()
      self.model_tester.create_and_check_model(*a_ )




 @unittest.skip(reason="""MgpstrModel does not use inputs_embeds""" )
 def   a      ( self       : Any )->					str:



      """simple docstring"""

      pass




 def   a      ( self       : Optional[Any] )->					List[Any]:



      """simple docstring"""

      UpperCAmelCase_						:    Optional[Any]    					=  self.model_tester.prepare_config_and_inputs_for_common()

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




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



      """simple docstring"""

      UpperCAmelCase_						:    List[Any]    					=  self.model_tester.prepare_config_and_inputs_for_common()

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

           UpperCAmelCase_						:    Dict    					=  ["""pixel_values"""]
           self.assertListEqual(arg_names[:1]     ,   a_ )




 @unittest.skip(reason="""MgpstrModel does not support feedforward chunking""" )
 def   a      ( self       : List[str] )->					Union[str, Any]:



      """simple docstring"""

      pass




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



      """simple docstring"""

      UpperCAmelCase_						:    Tuple    					=  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

           UpperCAmelCase_						:    Union[str, Any]    					=  True
           UpperCAmelCase_						:    List[Any]    					=  model_class(a_ )
           self.assertTrue(model.is_gradient_checkpointing )




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



      """simple docstring"""

      def check_hidden_states_output(a_       : List[Any]     ,   a_       : List[Any]     ,   a_       : Dict ):
           UpperCAmelCase_						:    List[str]    					=  model_class(a_ )
           model.to(a_ )
           model.eval()

           with torch.no_grad():
                UpperCAmelCase_						:    int    					=  model(**self._prepare_for_class(a_     ,   a_ ) )

           UpperCAmelCase_						:    Any    					=  outputs.hidden_states

           UpperCAmelCase_						:    Optional[Any]    					=  getattr(
               self.model_tester     ,   """expected_num_hidden_layers"""     ,   self.model_tester.num_hidden_layers + 1 )
           self.assertEqual(len(a_ )     ,   a_ )

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

      UpperCAmelCase_						:    Dict    					=  self.model_tester.prepare_config_and_inputs_for_common()

      for model_class in self.all_model_classes:
           UpperCAmelCase_						:    str    					=  True
           check_hidden_states_output(a_     ,   a_     ,   a_ )

           # check that output_hidden_states also work using config
           del inputs_dict["output_hidden_states"]
           UpperCAmelCase_						:    List[str]    					=  True

           check_hidden_states_output(a_     ,   a_     ,   a_ )




 def   a      ( self       : str )->					Any:



      """simple docstring"""

      UpperCAmelCase_						:    int    					=  self.model_tester.prepare_config_and_inputs_for_common()

      UpperCAmelCase_						:    Optional[Any]    					=  _config_zero_init(a_ )
      for model_class in self.all_model_classes:
           UpperCAmelCase_						:    List[Any]    					=  model_class(config=a_ )
           for name, param in model.named_parameters():
                if isinstance(a_     ,   (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   a      ( self       : Optional[int] )->					Union[str, Any]:



      """simple docstring"""

      pass




def 			A_			(					)     ->							int:


     """simple docstring"""







     UpperCAmelCase_						:    int    					=  """https://i.postimg.cc/ZKwLg2Gw/367-14.png"""
     UpperCAmelCase_						:    List[Any]    					=  Image.open(requests.get(lowercase						, stream=lowercase      ).raw      ).convert("""RGB"""      )
     return im



@require_vision
@require_torch
class 		UpperCAmelCase_	(unittest.TestCase						):
 """simple docstring"""




 @slow
 def   a      ( self       : Optional[int] )->					List[Any]:



      """simple docstring"""

      UpperCAmelCase_						:    int    					=  """alibaba-damo/mgp-str-base"""
      UpperCAmelCase_						:    int    					=  MgpstrForSceneTextRecognition.from_pretrained(a_ ).to(a_ )
      UpperCAmelCase_						:    List[str]    					=  MgpstrProcessor.from_pretrained(a_ )

      UpperCAmelCase_						:    Any    					=  prepare_img()
      UpperCAmelCase_						:    Dict    					=  processor(images=a_     ,   return_tensors="""pt""" ).pixel_values.to(a_ )

      # forward pass
      with torch.no_grad():
           UpperCAmelCase_						:    List[str]    					=  model(a_ )

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

      UpperCAmelCase_						:    List[Any]    					=  processor.batch_decode(outputs.logits )
      UpperCAmelCase_						:    Dict    					=  """ticket"""

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

      UpperCAmelCase_						:    Tuple    					=  torch.tensor(
          [[[-39.5397, -44.4024, -36.1844], [-61.4709, -63.8639, -58.3454], [-74.0225, -68.5494, -71.2164]]]     ,   device=a_     ,   )

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



"""simple docstring"""







from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowercase_						    =					{"configuration_visual_bert": ["VISUAL_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP", "VisualBertConfig"]}

try:
       if not is_torch_available():
              raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
       pass
else:
       lowercase_						    =					[
           "VISUAL_BERT_PRETRAINED_MODEL_ARCHIVE_LIST",
           "VisualBertForMultipleChoice",
           "VisualBertForPreTraining",
           "VisualBertForQuestionAnswering",
           "VisualBertForRegionToPhraseAlignment",
           "VisualBertForVisualReasoning",
           "VisualBertLayer",
           "VisualBertModel",
           "VisualBertPreTrainedModel",
       ]


if TYPE_CHECKING:
       from .configuration_visual_bert import VISUAL_BERT_PRETRAINED_CONFIG_ARCHIVE_MAP, VisualBertConfig

       try:
              if not is_torch_available():
                     raise OptionalDependencyNotAvailable()
       except OptionalDependencyNotAvailable:
              pass
       else:
              from .modeling_visual_bert import (
                  VISUAL_BERT_PRETRAINED_MODEL_ARCHIVE_LIST,
                  VisualBertForMultipleChoice,
                  VisualBertForPreTraining,
                  VisualBertForQuestionAnswering,
                  VisualBertForRegionToPhraseAlignment,
                  VisualBertForVisualReasoning,
                  VisualBertLayer,
                  VisualBertModel,
                  VisualBertPreTrainedModel,
              )


else:
       import sys

       lowercase_						    =					_LazyModule(__name__, globals()["__file__"], _import_structure, module_spec=__spec__)

