






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

	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						,      )->   Dict:







							'''simple docstring'''





							lowerCAmelCase__	 =  parent
							lowerCAmelCase__	 =  is_training
							lowerCAmelCase__	 =  batch_size
							lowerCAmelCase__	 =  image_size
							lowerCAmelCase__	 =  patch_size
							lowerCAmelCase__	 =  num_channels
							lowerCAmelCase__	 =  max_token_length
							lowerCAmelCase__	 =  num_character_labels
							lowerCAmelCase__	 =  num_bpe_labels
							lowerCAmelCase__	 =  num_wordpiece_labels
							lowerCAmelCase__	 =  hidden_size
							lowerCAmelCase__	 =  num_hidden_layers
							lowerCAmelCase__	 =  num_attention_heads
							lowerCAmelCase__	 =  mlp_ratio
							lowerCAmelCase__	 =  patch_embeds_hidden_size
							lowerCAmelCase__	 =  output_hidden_states

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







							'''simple docstring'''





							lowerCAmelCase__	 =  floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]    )
							lowerCAmelCase__	 =  self.get_config()
							return config, pixel_values

	def 						UpperCAmelCase					(							self    )->   Union[str, 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 						UpperCAmelCase					(							self						,      __UpperCAmelCase						,      __UpperCAmelCase    )->   Optional[int]:







							'''simple docstring'''





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


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







							'''simple docstring'''





							lowerCAmelCase__	 =  self.prepare_config_and_inputs()
							lowerCAmelCase__	 =  config_and_inputs
							lowerCAmelCase__	 =  {"""pixel_values""": pixel_values}
							return config, inputs_dict





@require_torch
class        lowercase__       (     UpperCAmelCase__,   UpperCAmelCase__,   unittest.TestCase		):
	a_             =(MgpstrForSceneTextRecognition,) if is_torch_available() else ()
	a_             ={'feature-extraction': MgpstrForSceneTextRecognition} if is_torch_available() else {}
	a_             =False

	a_             =False
	a_             =False
	a_             =False
	a_             =False

	def 						UpperCAmelCase					(							self    )->   str:







							'''simple docstring'''





							lowerCAmelCase__	 =  MgpstrModelTester(self    )
							lowerCAmelCase__	 =  ConfigTester(self						,      config_class=__UpperCAmelCase						,      has_text_modality=__UpperCAmelCase    )

	def 						UpperCAmelCase					(							self    )->   Dict:







							'''simple docstring'''





							self.config_tester.run_common_tests()

	def 						UpperCAmelCase					(							self    )->   int:







							'''simple docstring'''





							lowerCAmelCase__	 =  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 						UpperCAmelCase					(							self    )->   str:







							'''simple docstring'''





							pass

	def 						UpperCAmelCase					(							self    )->   Tuple:







							'''simple docstring'''





							lowerCAmelCase__	 =  self.model_tester.prepare_config_and_inputs_for_common()

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

	def 						UpperCAmelCase					(							self    )->   Optional[Any]:







							'''simple docstring'''





							lowerCAmelCase__	 =  self.model_tester.prepare_config_and_inputs_for_common()

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

													lowerCAmelCase__	 =  ["""pixel_values"""]
													self.assertListEqual(arg_names[:1]						,      __UpperCAmelCase    )

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







							'''simple docstring'''





							pass

	def 						UpperCAmelCase					(							self    )->   Optional[Any]:







							'''simple docstring'''





							lowerCAmelCase__	 =  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

													lowerCAmelCase__	 =  True
													lowerCAmelCase__	 =  model_class(__UpperCAmelCase    )
													self.assertTrue(model.is_gradient_checkpointing    )

	def 						UpperCAmelCase					(							self    )->   Any:







							'''simple docstring'''





							def check_hidden_states_output(__UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase    ):
													lowerCAmelCase__	 =  model_class(__UpperCAmelCase    )
													model.to(__UpperCAmelCase    )
													model.eval()

													with torch.no_grad():
																			lowerCAmelCase__	 =  model(**self._prepare_for_class(__UpperCAmelCase						,      __UpperCAmelCase    )    )

													lowerCAmelCase__	 =  outputs.hidden_states

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

							lowerCAmelCase__	 =  self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
													lowerCAmelCase__	 =  True
													check_hidden_states_output(__UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase    )

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

													check_hidden_states_output(__UpperCAmelCase						,      __UpperCAmelCase						,      __UpperCAmelCase    )

	def 						UpperCAmelCase					(							self    )->   Optional[Any]:







							'''simple docstring'''





							lowerCAmelCase__	 =  self.model_tester.prepare_config_and_inputs_for_common()

							lowerCAmelCase__	 =  _config_zero_init(__UpperCAmelCase    )
							for model_class in self.all_model_classes:
													lowerCAmelCase__	 =  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 						UpperCAmelCase					(							self    )->   Optional[Any]:







							'''simple docstring'''





							pass





def     _a					(       )	->				List[Any]:
						"""simple docstring"""







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





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

	@slow
	def 						UpperCAmelCase					(							self    )->   Tuple:







							'''simple docstring'''





							lowerCAmelCase__	 =  """alibaba-damo/mgp-str-base"""
							lowerCAmelCase__	 =  MgpstrForSceneTextRecognition.from_pretrained(__UpperCAmelCase    ).to(__UpperCAmelCase    )
							lowerCAmelCase__	 =  MgpstrProcessor.from_pretrained(__UpperCAmelCase    )

							lowerCAmelCase__	 =  prepare_img()
							lowerCAmelCase__	 =  processor(images=__UpperCAmelCase						,      return_tensors="pt"    ).pixel_values.to(__UpperCAmelCase    )

							# forward pass
							with torch.no_grad():
													lowerCAmelCase__	 =  model(__UpperCAmelCase    )

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

							lowerCAmelCase__	 =  processor.batch_decode(outputs.logits    )
							lowerCAmelCase__	 =  """ticket"""

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

							lowerCAmelCase__	 =  torch.tensor(
							    [[[-39.5397, -44.4024, -36.1844], [-61.4709, -63.8639, -58.3454], [-74.0225, -68.5494, -71.2164]]]						,      device=__UpperCAmelCase						,      )

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














import unittest

import numpy as np
import PIL
import torch

from diffusers.image_processor import VaeImageProcessor


class        lowercase__       (     unittest.TestCase		):

	@property
	def 						UpperCAmelCase					(							self    )->   List[str]:







							'''simple docstring'''





							lowerCAmelCase__	 =  1
							lowerCAmelCase__	 =  3
							lowerCAmelCase__	 =  8
							lowerCAmelCase__	 =  8

							lowerCAmelCase__	 =  torch.rand((batch_size, num_channels, height, width)    )

							return sample

	def 						UpperCAmelCase					(							self						,      __UpperCAmelCase    )->   Dict:







							'''simple docstring'''





							if isinstance(image[0]						,      PIL.Image.Image    ):
													return np.stack([np.array(__UpperCAmelCase    ) for i in image]						,      axis=0    )
							elif isinstance(__UpperCAmelCase						,      torch.Tensor    ):
													return image.cpu().numpy().transpose(0						,      2						,      3						,      1    )
							return image

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







							'''simple docstring'''





							lowerCAmelCase__	 =  VaeImageProcessor(do_resize=__UpperCAmelCase						,      do_normalize=__UpperCAmelCase    )

							lowerCAmelCase__	 =  self.dummy_sample
							lowerCAmelCase__	 =  self.to_np(__UpperCAmelCase    )

							for output_type in ["pt", "np", "pil"]:
													lowerCAmelCase__	 =  image_processor.postprocess(
													    image_processor.preprocess(__UpperCAmelCase    )						,      output_type=__UpperCAmelCase						,      )
													lowerCAmelCase__	 =  self.to_np(__UpperCAmelCase    )
													lowerCAmelCase__	 =  (input_np * 255).round() if output_type == "pil" else input_np
													assert (
													    np.abs(in_np - out_np    ).max() < 1E-6
													), F"decoded output does not match input for output_type {output_type}"

	def 						UpperCAmelCase					(							self    )->   int:







							'''simple docstring'''





							lowerCAmelCase__	 =  VaeImageProcessor(do_resize=__UpperCAmelCase						,      do_normalize=__UpperCAmelCase    )
							lowerCAmelCase__	 =  self.dummy_sample.cpu().numpy().transpose(0						,      2						,      3						,      1    )

							for output_type in ["pt", "np", "pil"]:
													lowerCAmelCase__	 =  image_processor.postprocess(image_processor.preprocess(__UpperCAmelCase    )						,      output_type=__UpperCAmelCase    )

													lowerCAmelCase__	 =  self.to_np(__UpperCAmelCase    )
													lowerCAmelCase__	 =  (input_np * 255).round() if output_type == "pil" else input_np
													assert (
													    np.abs(in_np - out_np    ).max() < 1E-6
													), F"decoded output does not match input for output_type {output_type}"

	def 						UpperCAmelCase					(							self    )->   Any:







							'''simple docstring'''





							lowerCAmelCase__	 =  VaeImageProcessor(do_resize=__UpperCAmelCase						,      do_normalize=__UpperCAmelCase    )

							lowerCAmelCase__	 =  self.dummy_sample.cpu().numpy().transpose(0						,      2						,      3						,      1    )
							lowerCAmelCase__	 =  image_processor.numpy_to_pil(__UpperCAmelCase    )

							for output_type in ["pt", "np", "pil"]:
													lowerCAmelCase__	 =  image_processor.postprocess(image_processor.preprocess(__UpperCAmelCase    )						,      output_type=__UpperCAmelCase    )
													for i, o in zip(__UpperCAmelCase						,      __UpperCAmelCase    ):
																			lowerCAmelCase__	 =  np.array(__UpperCAmelCase    )
																			lowerCAmelCase__	 =  self.to_np(__UpperCAmelCase    ) if output_type == "pil" else (self.to_np(__UpperCAmelCase    ) * 255).round()
																			assert (
																			    np.abs(in_np - out_np    ).max() < 1E-6
																			), F"decoded output does not match input for output_type {output_type}"

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







							'''simple docstring'''





							lowerCAmelCase__	 =  VaeImageProcessor(do_resize=__UpperCAmelCase						,      do_normalize=__UpperCAmelCase    )

							lowerCAmelCase__	 =  self.dummy_sample
							lowerCAmelCase__	 =  input_pt_ad.squeeze(0    )

							lowerCAmelCase__	 =  image_processor.postprocess(
							    image_processor.preprocess(__UpperCAmelCase    )						,      output_type="np"						,      )
							lowerCAmelCase__	 =  image_processor.postprocess(
							    image_processor.preprocess(__UpperCAmelCase    )						,      output_type="np"						,      )

							lowerCAmelCase__	 =  self.to_np(self.dummy_sample    )
							lowerCAmelCase__	 =  input_np_ad.squeeze(0    )

							lowerCAmelCase__	 =  image_processor.postprocess(
							    image_processor.preprocess(__UpperCAmelCase    )						,      output_type="np"						,      )
							lowerCAmelCase__	 =  image_processor.postprocess(
							    image_processor.preprocess(__UpperCAmelCase    )						,      output_type="np"						,      )

							assert np.abs(out_pt_ad - out_pt_ad    ).max() < 1E-6
							assert np.abs(out_np_ad - out_np_ad    ).max() < 1E-6


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







							'''simple docstring'''





							lowerCAmelCase__	 =  VaeImageProcessor(do_resize=__UpperCAmelCase						,      do_normalize=__UpperCAmelCase    )

							lowerCAmelCase__	 =  self.dummy_sample
							lowerCAmelCase__	 =  list(__UpperCAmelCase    )

							lowerCAmelCase__	 =  image_processor.postprocess(
							    image_processor.preprocess(__UpperCAmelCase    )						,      output_type="np"						,      )

							lowerCAmelCase__	 =  image_processor.postprocess(
							    image_processor.preprocess(__UpperCAmelCase    )						,      output_type="np"						,      )

							lowerCAmelCase__	 =  self.to_np(self.dummy_sample    )
							list(__UpperCAmelCase    )

							lowerCAmelCase__	 =  image_processor.postprocess(
							    image_processor.preprocess(__UpperCAmelCase    )						,      output_type="np"						,      )

							lowerCAmelCase__	 =  image_processor.postprocess(
							    image_processor.preprocess(__UpperCAmelCase    )						,      output_type="np"						,      )

							assert np.abs(out_pt_ad - out_pt_list    ).max() < 1E-6
							assert np.abs(out_np_ad - out_np_list    ).max() < 1E-6






