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     :       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     :       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     :       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     :       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     :       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     :       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: import tempfile
  import unittest
  
  import torch
  
  from diffusers import UNetaDConditionModel
  from diffusers.training_utils import EMAModel
  from diffusers.utils.testing_utils import enable_full_determinism, skip_mps, torch_device
  
  
  enable_full_determinism()
  
  
  
  
  class    __lowerCAmelCase  (       unittest.TestCase						):
  
  
  
   '''simple docstring'''
   _lowerCamelCase						:	str				    = 'hf-internal-testing/tiny-stable-diffusion-pipe'
   _lowerCamelCase						:	int				    = 1
   _lowerCamelCase						:	Any				    = 77
   _lowerCamelCase						:	List[Any]				    = 32
   _lowerCamelCase						:	List[str]				    = 4
   _lowerCamelCase						:	Dict				    = 64
   _lowerCamelCase						:	List[str]				    = torch.manual_seed(0						)
  
   def 					__snake_case					(			self       :		Any       ,			lowerCamelCase       :		Union[str, Any]=0.9_9_9_9  ) ->							Dict:
          _UpperCamelCase     :       int  						=  UNetaDConditionModel.from_pretrained(self.model_id       ,			subfolder='unet'  )
          _UpperCamelCase     :       Any  						=  unet.to(lowerCamelCase  )
          _UpperCamelCase     :       Union[str, Any]  						=  EMAModel(unet.parameters()       ,			decay=lowerCamelCase       ,			model_cls=lowerCamelCase       ,			model_config=unet.config  )
          return unet, ema_unet
  
   def 					__snake_case					(			self       :		Tuple  ) ->							Union[str, Any]:
          _UpperCamelCase     :       Tuple  						=  torch.randn(
              self.batch_size       ,			self.num_in_channels       ,			self.latent_height       ,			self.latent_width       ,			generator=self.generator  ).to(lowerCamelCase  )
          _UpperCamelCase     :       Union[str, Any]  						=  torch.randint(0       ,			1_0_0_0       ,			size=(self.batch_size,)       ,			generator=self.generator  ).to(lowerCamelCase  )
          _UpperCamelCase     :       int  						=  torch.randn(
              self.batch_size       ,			self.prompt_length       ,			self.text_encoder_hidden_dim       ,			generator=self.generator  ).to(lowerCamelCase  )
          return noisy_latents, timesteps, encoder_hidden_states
  
   def 					__snake_case					(			self       :		str       ,			lowerCamelCase       :		List[Any]  ) ->							List[Any]:
          _UpperCamelCase     :       int  						=  {}
          for k, param in unet.state_dict().items():
                 _UpperCamelCase     :       Optional[Any]  						=  torch.randn_like(lowerCamelCase  ) + (param * torch.randn_like(lowerCamelCase  ))
                 updated_state_dict.update({k: updated_param}  )
          unet.load_state_dict(lowerCamelCase  )
          return unet
  
   def 					__snake_case					(			self       :		List[Any]  ) ->							Tuple:
          _UpperCamelCase						,      _UpperCamelCase     :       Union[str, Any]  						=  self.get_models()
          # Take the first (hypothetical) EMA step.
          ema_unet.step(unet.parameters()  )
          assert ema_unet.optimization_step == 1
  
          # Take two more.
          for _ in range(2  ):
                 ema_unet.step(unet.parameters()  )
          assert ema_unet.optimization_step == 3
  
   def 					__snake_case					(			self       :		int  ) ->							Any:
          _UpperCamelCase						,      _UpperCamelCase     :       Any  						=  self.get_models()
          # Since the `unet` is not being updated (i.e., backprop'd)
          # there won't be any difference between the `params` of `unet`
          # and `ema_unet` even if we call `ema_unet.step(unet.parameters())`.
          ema_unet.step(unet.parameters()  )
          _UpperCamelCase     :       List[Any]  						=  list(unet.parameters()  )
          for s_param, param in zip(ema_unet.shadow_params       ,			lowerCamelCase  ):
                 assert torch.allclose(lowerCamelCase       ,			lowerCamelCase  )
  
          # The above holds true even if we call `ema.step()` multiple times since
          # `unet` params are still not being updated.
          for _ in range(4  ):
                 ema_unet.step(unet.parameters()  )
          for s_param, param in zip(ema_unet.shadow_params       ,			lowerCamelCase  ):
                 assert torch.allclose(lowerCamelCase       ,			lowerCamelCase  )
  
   def 					__snake_case					(			self       :		Dict  ) ->							Dict:
          _UpperCamelCase						,      _UpperCamelCase     :       Dict  						=  self.get_models()
          # Here we simulate the parameter updates for `unet`. Since there might
          # be some parameters which are initialized to zero we take extra care to
          # initialize their values to something non-zero before the multiplication.
          _UpperCamelCase     :       str  						=  self.simulate_backprop(lowerCamelCase  )
  
          # Take the EMA step.
          ema_unet.step(unet_pseudo_updated_step_one.parameters()  )
  
          # Now the EMA'd parameters won't be equal to the original model parameters.
          _UpperCamelCase     :       Optional[Any]  						=  list(unet_pseudo_updated_step_one.parameters()  )
          for s_param, param in zip(ema_unet.shadow_params       ,			lowerCamelCase  ):
                 assert ~torch.allclose(lowerCamelCase       ,			lowerCamelCase  )
  
          # Ensure this is the case when we take multiple EMA steps.
          for _ in range(4  ):
                 ema_unet.step(unet.parameters()  )
          for s_param, param in zip(ema_unet.shadow_params       ,			lowerCamelCase  ):
                 assert ~torch.allclose(lowerCamelCase       ,			lowerCamelCase  )
  
   def 					__snake_case					(			self       :		Union[str, Any]  ) ->							Tuple:
          # If we call EMA step after a backpropagation consecutively for two times,
          # the shadow params from those two steps should be different.
          _UpperCamelCase						,      _UpperCamelCase     :       Dict  						=  self.get_models()
  
          # First backprop + EMA
          _UpperCamelCase     :       Optional[Any]  						=  self.simulate_backprop(lowerCamelCase  )
          ema_unet.step(unet_step_one.parameters()  )
          _UpperCamelCase     :       Optional[Any]  						=  ema_unet.shadow_params
  
          # Second backprop + EMA
          _UpperCamelCase     :       Optional[int]  						=  self.simulate_backprop(lowerCamelCase  )
          ema_unet.step(unet_step_two.parameters()  )
          _UpperCamelCase     :       List[str]  						=  ema_unet.shadow_params
  
          for step_one, step_two in zip(lowerCamelCase       ,			lowerCamelCase  ):
                 assert ~torch.allclose(lowerCamelCase       ,			lowerCamelCase  )
  
   def 					__snake_case					(			self       :		str  ) ->							int:
          # If there's no decay even if there are backprops, EMA steps
          # won't take any effect i.e., the shadow params would remain the
          # same.
          _UpperCamelCase						,      _UpperCamelCase     :       List[str]  						=  self.get_models(decay=0.0  )
          _UpperCamelCase     :       Optional[Any]  						=  self.simulate_backprop(lowerCamelCase  )
          ema_unet.step(unet_step_one.parameters()  )
          _UpperCamelCase     :       Dict  						=  ema_unet.shadow_params
  
          _UpperCamelCase     :       List[str]  						=  self.simulate_backprop(lowerCamelCase  )
          ema_unet.step(unet_step_two.parameters()  )
          _UpperCamelCase     :       Union[str, Any]  						=  ema_unet.shadow_params
  
          for step_one, step_two in zip(lowerCamelCase       ,			lowerCamelCase  ):
                 assert torch.allclose(lowerCamelCase       ,			lowerCamelCase  )
  
   @skip_mps
   def 					__snake_case					(			self       :		Dict  ) ->							str:
          _UpperCamelCase						,      _UpperCamelCase     :       Optional[Any]  						=  self.get_models()
          _UpperCamelCase						,      _UpperCamelCase						,      _UpperCamelCase     :       Any  						=  self.get_dummy_inputs()
  
          with tempfile.TemporaryDirectory() as tmpdir:
                 ema_unet.save_pretrained(lowerCamelCase  )
                 _UpperCamelCase     :       List[str]  						=  UNetaDConditionModel.from_pretrained(lowerCamelCase       ,			model_cls=lowerCamelCase  )
                 _UpperCamelCase     :       Dict  						=  loaded_unet.to(unet.device  )
  
          # Since no EMA step has been performed the outputs should match.
          _UpperCamelCase     :       Optional[Any]  						=  unet(lowerCamelCase       ,			lowerCamelCase       ,			lowerCamelCase  ).sample
          _UpperCamelCase     :       List[str]  						=  loaded_unet(lowerCamelCase       ,			lowerCamelCase       ,			lowerCamelCase  ).sample
  
          assert torch.allclose(lowerCamelCase       ,			lowerCamelCase       ,			atol=1e-4  )
  
  
  
  