


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    __lowercase  :

							"""simple docstring"""


							def __init__(    self				, A_				, A_=False				, A_=13				, A_=(32, 128)				, A_=4				, A_=3				, A_=27				, A_=38				, A_=99				, A_=99				, A_=32				, A_=5				, A_=4				, A_=4.0				, A_=257				, A_=None				, )->	Optional[Any]:
											_SCREAMING_SNAKE_CASE      = parent
											_SCREAMING_SNAKE_CASE      = is_training
											_SCREAMING_SNAKE_CASE      = batch_size
											_SCREAMING_SNAKE_CASE      = image_size
											_SCREAMING_SNAKE_CASE      = patch_size
											_SCREAMING_SNAKE_CASE      = num_channels
											_SCREAMING_SNAKE_CASE      = max_token_length
											_SCREAMING_SNAKE_CASE      = num_character_labels
											_SCREAMING_SNAKE_CASE      = num_bpe_labels
											_SCREAMING_SNAKE_CASE      = num_wordpiece_labels
											_SCREAMING_SNAKE_CASE      = hidden_size
											_SCREAMING_SNAKE_CASE      = num_hidden_layers
											_SCREAMING_SNAKE_CASE      = num_attention_heads
											_SCREAMING_SNAKE_CASE      = mlp_ratio
											_SCREAMING_SNAKE_CASE      = patch_embeds_hidden_size
											_SCREAMING_SNAKE_CASE      = output_hidden_states





							def 		__magic_name__			(    self	)->	Any:
											_SCREAMING_SNAKE_CASE      = floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]	)
											_SCREAMING_SNAKE_CASE      = self.get_config()
											return config, pixel_values





							def 		__magic_name__			(    self	)->	Optional[int]:
											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 		__magic_name__			(    self				, A_				, A_	)->	Any:
											_SCREAMING_SNAKE_CASE      = MgpstrForSceneTextRecognition(A_	)
											model.to(A_	)
											model.eval()
											with torch.no_grad():
															_SCREAMING_SNAKE_CASE      = model(A_	)
											self.parent.assertEqual(
											    generated_ids[0][0].shape				, (self.batch_size, self.max_token_length, self.num_character_labels)	)





							def 		__magic_name__			(    self	)->	List[Any]:
											_SCREAMING_SNAKE_CASE      = self.prepare_config_and_inputs()
											_SCREAMING_SNAKE_CASE      ,      _SCREAMING_SNAKE_CASE      = config_and_inputs
											_SCREAMING_SNAKE_CASE      = {'pixel_values': pixel_values}
											return config, inputs_dict







@require_torch
class    __lowercase  (       UpperCamelCase      ,     UpperCamelCase      ,     unittest.TestCase							):

							"""simple docstring"""


							SCREAMING_SNAKE_CASE   :					Optional[int]        =						(MgpstrForSceneTextRecognition,) if is_torch_available() else ()
							SCREAMING_SNAKE_CASE   :					Any        =						{"feature-extraction": MgpstrForSceneTextRecognition} if is_torch_available() else {}
							SCREAMING_SNAKE_CASE   :					List[Any]        =						False

							SCREAMING_SNAKE_CASE   :					Union[str, Any]        =						False
							SCREAMING_SNAKE_CASE   :					Optional[int]        =						False
							SCREAMING_SNAKE_CASE   :					Dict        =						False
							SCREAMING_SNAKE_CASE   :					Tuple        =						False





							def 		__magic_name__			(    self	)->	Any:
											_SCREAMING_SNAKE_CASE      = MgpstrModelTester(self	)
											_SCREAMING_SNAKE_CASE      = ConfigTester(self				, config_class=A_				, has_text_modality=A_	)





							def 		__magic_name__			(    self	)->	Optional[int]:
											self.config_tester.run_common_tests()





							def 		__magic_name__			(    self	)->	str:
											_SCREAMING_SNAKE_CASE      = 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 		__magic_name__			(    self	)->	int:
											pass





							def 		__magic_name__			(    self	)->	int:
											_SCREAMING_SNAKE_CASE      ,      _SCREAMING_SNAKE_CASE      = self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE      = model_class(A_	)
															self.assertIsInstance(model.get_input_embeddings()				, (nn.Module)	)
															_SCREAMING_SNAKE_CASE      = model.get_output_embeddings()
															self.assertTrue(x is None or isinstance(A_				, nn.Linear	)	)





							def 		__magic_name__			(    self	)->	int:
											_SCREAMING_SNAKE_CASE      ,      _SCREAMING_SNAKE_CASE      = self.model_tester.prepare_config_and_inputs_for_common()

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

															_SCREAMING_SNAKE_CASE      = ['pixel_values']
															self.assertListEqual(arg_names[:1]				, A_	)





							@unittest.skip(reason='MgpstrModel does not support feedforward chunking'	)
							def 		__magic_name__			(    self	)->	Dict:
											pass





							def 		__magic_name__			(    self	)->	List[Any]:
											_SCREAMING_SNAKE_CASE      ,      _SCREAMING_SNAKE_CASE      = 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

															_SCREAMING_SNAKE_CASE      = True
															_SCREAMING_SNAKE_CASE      = model_class(A_	)
															self.assertTrue(model.is_gradient_checkpointing	)





							def 		__magic_name__			(    self	)->	Dict:
											def check_hidden_states_output(A_				, A_				, A_	):
															_SCREAMING_SNAKE_CASE      = model_class(A_	)
															model.to(A_	)
															model.eval()

															with torch.no_grad():
																			_SCREAMING_SNAKE_CASE      = model(**self._prepare_for_class(A_				, A_	)	)

															_SCREAMING_SNAKE_CASE      = outputs.hidden_states

															_SCREAMING_SNAKE_CASE      = 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]				, )

											_SCREAMING_SNAKE_CASE      ,      _SCREAMING_SNAKE_CASE      = self.model_tester.prepare_config_and_inputs_for_common()

											for model_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE      = True
															check_hidden_states_output(A_				, A_				, A_	)

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

															check_hidden_states_output(A_				, A_				, A_	)





							def 		__magic_name__			(    self	)->	int:
											_SCREAMING_SNAKE_CASE      ,      _SCREAMING_SNAKE_CASE      = self.model_tester.prepare_config_and_inputs_for_common()

											_SCREAMING_SNAKE_CASE      = _config_zero_init(A_	)
											for model_class in self.all_model_classes:
															_SCREAMING_SNAKE_CASE      = 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 		__magic_name__			(    self	)->	Optional[int]:
											pass







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

				"""simple docstring"""






				_SCREAMING_SNAKE_CASE      = 'https://i.postimg.cc/ZKwLg2Gw/367-14.png'
				_SCREAMING_SNAKE_CASE      = Image.open(requests.get(UpperCAmelCase__   ,stream=UpperCAmelCase__							).raw							).convert('RGB'							)
				return im



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

							"""simple docstring"""


							@slow
							def 		__magic_name__			(    self	)->	Optional[int]:
											_SCREAMING_SNAKE_CASE      = 'alibaba-damo/mgp-str-base'
											_SCREAMING_SNAKE_CASE      = MgpstrForSceneTextRecognition.from_pretrained(A_	).to(A_	)
											_SCREAMING_SNAKE_CASE      = MgpstrProcessor.from_pretrained(A_	)

											_SCREAMING_SNAKE_CASE      = prepare_img()
											_SCREAMING_SNAKE_CASE      = processor(images=A_				, return_tensors='pt'	).pixel_values.to(A_	)

											# forward pass
											with torch.no_grad():
															_SCREAMING_SNAKE_CASE      = model(A_	)

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

											_SCREAMING_SNAKE_CASE      = processor.batch_decode(outputs.logits	)
											_SCREAMING_SNAKE_CASE      = 'ticket'

											self.assertEqual(out_strs['generated_text'][0]				, A_	)

											_SCREAMING_SNAKE_CASE      = 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	)	)




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    __lowercase  (       unittest.TestCase							):

							"""simple docstring"""


							SCREAMING_SNAKE_CASE   :					str        =						"hf-internal-testing/tiny-stable-diffusion-pipe"
							SCREAMING_SNAKE_CASE   :					str        =						1
							SCREAMING_SNAKE_CASE   :					Optional[Any]        =						77
							SCREAMING_SNAKE_CASE   :					Dict        =						32
							SCREAMING_SNAKE_CASE   :					Optional[Any]        =						4
							SCREAMING_SNAKE_CASE   :					Optional[int]        =						64
							SCREAMING_SNAKE_CASE   :					Tuple        =						torch.manual_seed(0							)





							def 		__magic_name__			(    self				, A_=0.9999	)->	Optional[int]:
											_SCREAMING_SNAKE_CASE      = UNetaDConditionModel.from_pretrained(self.model_id				, subfolder='unet'	)
											_SCREAMING_SNAKE_CASE      = unet.to(A_	)
											_SCREAMING_SNAKE_CASE      = EMAModel(unet.parameters()				, decay=A_				, model_cls=A_				, model_config=unet.config	)
											return unet, ema_unet





							def 		__magic_name__			(    self	)->	Optional[int]:
											_SCREAMING_SNAKE_CASE      = torch.randn(
											    self.batch_size				, self.num_in_channels				, self.latent_height				, self.latent_width				, generator=self.generator	).to(A_	)
											_SCREAMING_SNAKE_CASE      = torch.randint(0				, 1000				, size=(self.batch_size,)				, generator=self.generator	).to(A_	)
											_SCREAMING_SNAKE_CASE      = torch.randn(
											    self.batch_size				, self.prompt_length				, self.text_encoder_hidden_dim				, generator=self.generator	).to(A_	)
											return noisy_latents, timesteps, encoder_hidden_states





							def 		__magic_name__			(    self				, A_	)->	str:
											_SCREAMING_SNAKE_CASE      = {}
											for k, param in unet.state_dict().items():
															_SCREAMING_SNAKE_CASE      = torch.randn_like(A_	) + (param * torch.randn_like(A_	))
															updated_state_dict.update({k: updated_param}	)
											unet.load_state_dict(A_	)
											return unet





							def 		__magic_name__			(    self	)->	Dict:
											_SCREAMING_SNAKE_CASE      ,      _SCREAMING_SNAKE_CASE      = 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 		__magic_name__			(    self	)->	Optional[int]:
											_SCREAMING_SNAKE_CASE      ,      _SCREAMING_SNAKE_CASE      = 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()	)
											_SCREAMING_SNAKE_CASE      = list(unet.parameters()	)
											for s_param, param in zip(ema_unet.shadow_params				, A_	):
															assert torch.allclose(A_				, A_	)

											# 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				, A_	):
															assert torch.allclose(A_				, A_	)





							def 		__magic_name__			(    self	)->	Union[str, Any]:
											_SCREAMING_SNAKE_CASE      ,      _SCREAMING_SNAKE_CASE      = 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.
											_SCREAMING_SNAKE_CASE      = self.simulate_backprop(A_	)

											# 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.
											_SCREAMING_SNAKE_CASE      = list(unet_pseudo_updated_step_one.parameters()	)
											for s_param, param in zip(ema_unet.shadow_params				, A_	):
															assert ~torch.allclose(A_				, A_	)

											# 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				, A_	):
															assert ~torch.allclose(A_				, A_	)





							def 		__magic_name__			(    self	)->	Optional[int]:
											# If we call EMA step after a backpropagation consecutively for two times,
											# the shadow params from those two steps should be different.
											_SCREAMING_SNAKE_CASE      ,      _SCREAMING_SNAKE_CASE      = self.get_models()

											# First backprop + EMA
											_SCREAMING_SNAKE_CASE      = self.simulate_backprop(A_	)
											ema_unet.step(unet_step_one.parameters()	)
											_SCREAMING_SNAKE_CASE      = ema_unet.shadow_params

											# Second backprop + EMA
											_SCREAMING_SNAKE_CASE      = self.simulate_backprop(A_	)
											ema_unet.step(unet_step_two.parameters()	)
											_SCREAMING_SNAKE_CASE      = ema_unet.shadow_params

											for step_one, step_two in zip(A_				, A_	):
															assert ~torch.allclose(A_				, A_	)





							def 		__magic_name__			(    self	)->	Dict:
											# 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.
											_SCREAMING_SNAKE_CASE      ,      _SCREAMING_SNAKE_CASE      = self.get_models(decay=0.0	)
											_SCREAMING_SNAKE_CASE      = self.simulate_backprop(A_	)
											ema_unet.step(unet_step_one.parameters()	)
											_SCREAMING_SNAKE_CASE      = ema_unet.shadow_params

											_SCREAMING_SNAKE_CASE      = self.simulate_backprop(A_	)
											ema_unet.step(unet_step_two.parameters()	)
											_SCREAMING_SNAKE_CASE      = ema_unet.shadow_params

											for step_one, step_two in zip(A_				, A_	):
															assert torch.allclose(A_				, A_	)





							@skip_mps
							def 		__magic_name__			(    self	)->	Optional[Any]:
											_SCREAMING_SNAKE_CASE      ,      _SCREAMING_SNAKE_CASE      = self.get_models()
											_SCREAMING_SNAKE_CASE      ,      _SCREAMING_SNAKE_CASE      ,      _SCREAMING_SNAKE_CASE      = self.get_dummy_inputs()

											with tempfile.TemporaryDirectory() as tmpdir:
															ema_unet.save_pretrained(A_	)
															_SCREAMING_SNAKE_CASE      = UNetaDConditionModel.from_pretrained(A_				, model_cls=A_	)
															_SCREAMING_SNAKE_CASE      = loaded_unet.to(unet.device	)

											# Since no EMA step has been performed the outputs should match.
											_SCREAMING_SNAKE_CASE      = unet(A_				, A_				, A_	).sample
											_SCREAMING_SNAKE_CASE      = loaded_unet(A_				, A_				, A_	).sample

											assert torch.allclose(A_				, A_				, atol=1e-4	)
