code: 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    __lowerCAmelCase  :
  
  
  
   '''simple docstring'''
  
   def __init__(			self       :		List[str]       ,			lowerCamelCase       :		List[str]       ,			lowerCamelCase       :		int=False       ,			lowerCamelCase       :		int=1_3       ,			lowerCamelCase       :		Optional[int]=(3_2, 1_2_8)       ,			lowerCamelCase       :		Optional[int]=4       ,			lowerCamelCase       :		List[Any]=3       ,			lowerCamelCase       :		Optional[int]=2_7       ,			lowerCamelCase       :		int=3_8       ,			lowerCamelCase       :		Optional[int]=9_9       ,			lowerCamelCase       :		Tuple=9_9       ,			lowerCamelCase       :		Optional[int]=3_2       ,			lowerCamelCase       :		List[Any]=5       ,			lowerCamelCase       :		Optional[Any]=4       ,			lowerCamelCase       :		Optional[Any]=4.0       ,			lowerCamelCase       :		Optional[int]=2_5_7       ,			lowerCamelCase       :		Tuple=None       ,			) ->							int:
          _UpperCamelCase     :       int  						=  parent
          _UpperCamelCase     :       int  						=  is_training
          _UpperCamelCase     :       Optional[int]  						=  batch_size
          _UpperCamelCase     :       Any  						=  image_size
          _UpperCamelCase     :       List[Any]  						=  patch_size
          _UpperCamelCase     :       str  						=  num_channels
          _UpperCamelCase     :       List[str]  						=  max_token_length
          _UpperCamelCase     :       List[str]  						=  num_character_labels
          _UpperCamelCase     :       Dict  						=  num_bpe_labels
          _UpperCamelCase     :       Union[str, Any]  						=  num_wordpiece_labels
          _UpperCamelCase     :       Optional[Any]  						=  hidden_size
          _UpperCamelCase     :       Tuple  						=  num_hidden_layers
          _UpperCamelCase     :       List[str]  						=  num_attention_heads
          _UpperCamelCase     :       str  						=  mlp_ratio
          _UpperCamelCase     :       Tuple  						=  patch_embeds_hidden_size
          _UpperCamelCase     :       int  						=  output_hidden_states
  
   def 					__snake_case					(			self       :		str  ) ->							str:
          _UpperCamelCase     :       List[Any]  						=  floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]  )
          _UpperCamelCase     :       List[str]  						=  self.get_config()
          return config, pixel_values
  
   def 					__snake_case					(			self       :		Optional[int]  ) ->							List[str]:
          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 					__snake_case					(			self       :		Union[str, Any]       ,			lowerCamelCase       :		Union[str, Any]       ,			lowerCamelCase       :		List[Any]  ) ->							Dict:
          _UpperCamelCase     :       List[str]  						=  MgpstrForSceneTextRecognition(lowerCamelCase  )
          model.to(lowerCamelCase  )
          model.eval()
          with torch.no_grad():
                 _UpperCamelCase     :       Union[str, Any]  						=  model(lowerCamelCase  )
          self.parent.assertEqual(
              generated_ids[0][0].shape       ,			(self.batch_size, self.max_token_length, self.num_character_labels)  )
  
   def 					__snake_case					(			self       :		Tuple  ) ->							Tuple:
          _UpperCamelCase     :       Any  						=  self.prepare_config_and_inputs()
          _UpperCamelCase						,      _UpperCamelCase     :       Optional[int]  						=  config_and_inputs
          _UpperCamelCase     :       str  						=  {'pixel_values': pixel_values}
          return config, inputs_dict
  
  
  
  
  
  
  
  @require_torch
  class    __lowerCAmelCase  (       a_,	a_,	unittest.TestCase						):
  
  
  
   '''simple docstring'''
   _lowerCamelCase						:	List[Any]				    = (MgpstrForSceneTextRecognition,) if is_torch_available() else ()
   _lowerCamelCase						:	Any				    = {'feature-extraction': MgpstrForSceneTextRecognition} if is_torch_available() else {}
   _lowerCamelCase						:	Dict				    = False
  
   _lowerCamelCase						:	Union[str, Any]				    = False
   _lowerCamelCase						:	List[str]				    = False
   _lowerCamelCase						:	Optional[Any]				    = False
   _lowerCamelCase						:	Dict				    = False
  
   def 					__snake_case					(			self       :		Dict  ) ->							List[Any]:
          _UpperCamelCase     :       Union[str, Any]  						=  MgpstrModelTester(self  )
          _UpperCamelCase     :       Tuple  						=  ConfigTester(self       ,			config_class=lowerCamelCase       ,			has_text_modality=lowerCamelCase  )
  
   def 					__snake_case					(			self       :		List[Any]  ) ->							Any:
          self.config_tester.run_common_tests()
  
   def 					__snake_case					(			self       :		int  ) ->							Any:
          _UpperCamelCase     :       str  						=  self.model_tester.prepare_config_and_inputs()
          self.model_tester.create_and_check_model(*lowerCamelCase  )
  
   @unittest.skip(reason='MgpstrModel does not use inputs_embeds'  )
   def 					__snake_case					(			self       :		Tuple  ) ->							Dict:
          pass
  
   def 					__snake_case					(			self       :		Dict  ) ->							Dict:
          _UpperCamelCase						,      _UpperCamelCase     :       Tuple  						=  self.model_tester.prepare_config_and_inputs_for_common()
  
          for model_class in self.all_model_classes:
                 _UpperCamelCase     :       int  						=  model_class(lowerCamelCase  )
                 self.assertIsInstance(model.get_input_embeddings()       ,			(nn.Module)  )
                 _UpperCamelCase     :       Any  						=  model.get_output_embeddings()
                 self.assertTrue(x is None or isinstance(lowerCamelCase       ,			nn.Linear  )  )
  
   def 					__snake_case					(			self       :		List[Any]  ) ->							Tuple:
          _UpperCamelCase						,      _UpperCamelCase     :       Tuple  						=  self.model_tester.prepare_config_and_inputs_for_common()
  
          for model_class in self.all_model_classes:
                 _UpperCamelCase     :       Tuple  						=  model_class(lowerCamelCase  )
                 _UpperCamelCase     :       str  						=  inspect.signature(model.forward  )
                 # signature.parameters is an OrderedDict => so arg_names order is deterministic
                 _UpperCamelCase     :       List[str]  						=  [*signature.parameters.keys()]
  
                 _UpperCamelCase     :       Optional[Any]  						=  ['pixel_values']
                 self.assertListEqual(arg_names[:1]       ,			lowerCamelCase  )
  
   @unittest.skip(reason='MgpstrModel does not support feedforward chunking'  )
   def 					__snake_case					(			self       :		Optional[Any]  ) ->							Optional[Any]:
          pass
  
   def 					__snake_case					(			self       :		str  ) ->							List[Any]:
          _UpperCamelCase						,      _UpperCamelCase     :       Optional[int]  						=  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     :       Dict  						=  True
                 _UpperCamelCase     :       Optional[int]  						=  model_class(lowerCamelCase  )
                 self.assertTrue(model.is_gradient_checkpointing  )
  
   def 					__snake_case					(			self       :		Tuple  ) ->							Tuple:
          def check_hidden_states_output(lowerCamelCase       :		int       ,			lowerCamelCase       :		Optional[Any]       ,			lowerCamelCase       :		Optional[int]  ):
                 _UpperCamelCase     :       Dict  						=  model_class(lowerCamelCase  )
                 model.to(lowerCamelCase  )
                 model.eval()
  
                 with torch.no_grad():
                        _UpperCamelCase     :       int  						=  model(**self._prepare_for_class(lowerCamelCase       ,			lowerCamelCase  )  )
  
                 _UpperCamelCase     :       int  						=  outputs.hidden_states
  
                 _UpperCamelCase     :       int  						=  getattr(
                     self.model_tester       ,			'expected_num_hidden_layers'       ,			self.model_tester.num_hidden_layers + 1  )
                 self.assertEqual(len(lowerCamelCase  )       ,			lowerCamelCase  )
  
                 self.assertListEqual(
                     list(hidden_states[0].shape[-2:]  )       ,			[self.model_tester.patch_embeds_hidden_size, self.model_tester.hidden_size]       ,			)
  
          _UpperCamelCase						,      _UpperCamelCase     :       Any  						=  self.model_tester.prepare_config_and_inputs_for_common()
  
          for model_class in self.all_model_classes:
                 _UpperCamelCase     :       str  						=  True
                 check_hidden_states_output(lowerCamelCase       ,			lowerCamelCase       ,			lowerCamelCase  )
  
                 # check that output_hidden_states also work using config
                 del inputs_dict["output_hidden_states"]
                 _UpperCamelCase     :       str  						=  True
  
                 check_hidden_states_output(lowerCamelCase       ,			lowerCamelCase       ,			lowerCamelCase  )
  
   def 					__snake_case					(			self       :		List[Any]  ) ->							Optional[Any]:
          _UpperCamelCase						,      _UpperCamelCase     :       Union[str, Any]  						=  self.model_tester.prepare_config_and_inputs_for_common()
  
          _UpperCamelCase     :       Optional[Any]  						=  _config_zero_init(lowerCamelCase  )
          for model_class in self.all_model_classes:
                 _UpperCamelCase     :       Union[str, Any]  						=  model_class(config=lowerCamelCase  )
                 for name, param in model.named_parameters():
                        if isinstance(lowerCamelCase       ,			(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 					__snake_case					(			self       :		List[str]  ) ->							str:
          pass
  
  
  
  
  
  
  
  def SCREAMING_SNAKE_CASE				(     ):
         _UpperCamelCase     :       Dict  						=  'https://i.postimg.cc/ZKwLg2Gw/367-14.png'
         _UpperCamelCase     :       Any  						=  Image.open(requests.get(lowercase_		,						stream=lowercase_				).raw				).convert('RGB'				)
         return im
  
  
  
  
  
  @require_vision
  @require_torch
  class    __lowerCAmelCase  (       unittest.TestCase						):
  
  
  
   '''simple docstring'''
  
   @slow
   def 					__snake_case					(			self       :		Dict  ) ->							Dict:
          _UpperCamelCase     :       str  						=  'alibaba-damo/mgp-str-base'
          _UpperCamelCase     :       Optional[Any]  						=  MgpstrForSceneTextRecognition.from_pretrained(lowerCamelCase  ).to(lowerCamelCase  )
          _UpperCamelCase     :       int  						=  MgpstrProcessor.from_pretrained(lowerCamelCase  )
  
          _UpperCamelCase     :       Tuple  						=  prepare_img()
          _UpperCamelCase     :       List[str]  						=  processor(images=lowerCamelCase       ,			return_tensors='pt'  ).pixel_values.to(lowerCamelCase  )
  
          # forward pass
          with torch.no_grad():
                 _UpperCamelCase     :       Tuple  						=  model(lowerCamelCase  )
  
          # verify the logits
          self.assertEqual(outputs.logits[0].shape       ,			torch.Size((1, 2_7, 3_8)  )  )
  
          _UpperCamelCase     :       str  						=  processor.batch_decode(outputs.logits  )
          _UpperCamelCase     :       Dict  						=  'ticket'
  
          self.assertEqual(out_strs['generated_text'][0]       ,			lowerCamelCase  )
  
          _UpperCamelCase     :       Optional[int]  						=  torch.tensor(
              [[[-3_9.5_3_9_7, -4_4.4_0_2_4, -3_6.1_8_4_4], [-6_1.4_7_0_9, -6_3.8_6_3_9, -5_8.3_4_5_4], [-7_4.0_2_2_5, -6_8.5_4_9_4, -7_1.2_1_6_4]]]       ,			device=lowerCamelCase       ,			)
  
          self.assertTrue(torch.allclose(outputs.logits[0][:, 1:4, 1:4]       ,			lowerCamelCase       ,			atol=1e-4  )  )
  
  
  
  
style_context: from .chunk_utils import chunk_layer
  from .data_transforms import make_atomaa_masks
  from .feats import atomaa_to_atomaa, frames_and_literature_positions_to_atomaa_pos, torsion_angles_to_frames
  from .loss import compute_predicted_aligned_error, compute_tm
  from .protein import Protein as OFProtein
  from .protein import to_pdb
  from .rigid_utils import Rigid, Rotation
  from .tensor_utils import dict_multimap, flatten_final_dims, permute_final_dims
  
  
  
  