



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







				'''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					,					):
								a__			 =						parent
								a__			 =						is_training
								a__			 =						batch_size
								a__			 =						image_size
								a__			 =						patch_size
								a__			 =						num_channels
								a__			 =						max_token_length
								a__			 =						num_character_labels
								a__			 =						num_bpe_labels
								a__			 =						num_wordpiece_labels
								a__			 =						hidden_size
								a__			 =						num_hidden_layers
								a__			 =						num_attention_heads
								a__			 =						mlp_ratio
								a__			 =						patch_embeds_hidden_size
								a__			 =						output_hidden_states


				def _a (							self						):
								a__			 =						floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]						)
								a__			 =						self.get_config()
								return config, pixel_values


				def _a (							self						):
								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					,					a_					,					a_						):
								a__			 =						MgpstrForSceneTextRecognition(a_						)
								model.to(a_						)
								model.eval()
								with torch.no_grad():
												a__			 =						model(a_						)
								self.parent.assertEqual(
								    generated_ids[0][0].shape					,					(self.batch_size, self.max_token_length, self.num_character_labels)						)







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




@require_torch
class 	__snake_case (				UpperCAmelCase__     ,UpperCAmelCase__     ,unittest.TestCase):







				'''simple docstring'''






				UpperCamelCase__			: Dict             =   (MgpstrForSceneTextRecognition,) if is_torch_available() else ()
				UpperCamelCase__			: Optional[int]             =   {'feature-extraction': MgpstrForSceneTextRecognition} if is_torch_available() else {}
				UpperCamelCase__			: Union[str, Any]             =   False

				UpperCamelCase__			: Dict             =   False
				UpperCamelCase__			: int             =   False
				UpperCamelCase__			: Tuple             =   False
				UpperCamelCase__			: Optional[Any]             =   False


				def _a (							self						):
								a__			 =						MgpstrModelTester(self						)
								a__			 =						ConfigTester(self					,					config_class=a_					,					has_text_modality=a_						)


				def _a (							self						):
								self.config_tester.run_common_tests()


				def _a (							self						):
								a__			 =						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						):
								pass


				def _a (							self						):
								a__			 =						self.model_tester.prepare_config_and_inputs_for_common()

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


				def _a (							self						):
								a__			 =						self.model_tester.prepare_config_and_inputs_for_common()

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

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


				@unittest.skip(reason="""MgpstrModel does not support feedforward chunking"""						)
				def _a (							self						):
								pass


				def _a (							self						):
								a__			 =						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

												a__			 =						True
												a__			 =						model_class(a_						)
												self.assertTrue(model.is_gradient_checkpointing						)


				def _a (							self						):




								def check_hidden_states_output(a_					,					a_					,					a_						):
												a__			 =						model_class(a_						)
												model.to(a_						)
												model.eval()

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

												a__			 =						outputs.hidden_states

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

								a__			 =						self.model_tester.prepare_config_and_inputs_for_common()

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

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

												check_hidden_states_output(a_					,					a_					,					a_						)


				def _a (							self						):
								a__			 =						self.model_tester.prepare_config_and_inputs_for_common()

								a__			 =						_config_zero_init(a_						)
								for model_class in self.all_model_classes:
												a__			 =						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						):
								pass




def      A_ (		):

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




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







				'''simple docstring'''






				@slow
				def _a (							self						):
								a__			 =						"""alibaba-damo/mgp-str-base"""
								a__			 =						MgpstrForSceneTextRecognition.from_pretrained(a_						).to(a_						)
								a__			 =						MgpstrProcessor.from_pretrained(a_						)

								a__			 =						prepare_img()
								a__			 =						processor(images=a_					,					return_tensors="""pt"""						).pixel_values.to(a_						)

								# forward pass
								with torch.no_grad():
												a__			 =						model(a_						)

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

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

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

								a__			 =						torch.tensor(
								    [[[-39.5_397, -44.4_024, -36.1_844], [-61.4_709, -63.8_639, -58.3_454], [-74.0_225, -68.5_494, -71.2_164]]]					,					device=a_					,					)

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






import shutil
import tempfile
import unittest

import numpy as np
import pytest

from transformers.testing_utils import require_vision
from transformers.utils import is_vision_available


if is_vision_available():
	from PIL import Image

	from transformers import AutoProcessor, BertTokenizer, CLIPImageProcessor, GitProcessor, PreTrainedTokenizerFast

@require_vision
class 	__snake_case (				unittest.TestCase):







							'''simple docstring'''






							def _a (							self						):
											a__			 =						tempfile.mkdtemp()

											a__			 =						CLIPImageProcessor()
											a__			 =						BertTokenizer.from_pretrained(
											    """hf-internal-testing/tiny-random-BertModel"""					,					model_input_names=["""input_ids""", """attention_mask"""]						)

											a__			 =						GitProcessor(a_					,					a_						)

											processor.save_pretrained(self.tmpdirname						)


							def _a (							self					,					**a_						):
											return AutoProcessor.from_pretrained(self.tmpdirname					,					**a_						).tokenizer


							def _a (							self					,					**a_						):
											return AutoProcessor.from_pretrained(self.tmpdirname					,					**a_						).image_processor


							def _a (							self						):
											shutil.rmtree(self.tmpdirname						)


							def _a (							self						):
											a__			 =						[np.random.randint(255					,					size=(3, 30, 400)					,					dtype=np.uinta						)]

											a__			 =						[Image.fromarray(np.moveaxis(a_					,					0					,					-1						)						) for x in image_inputs]

											return image_inputs


							def _a (							self						):
											a__			 =						GitProcessor(tokenizer=self.get_tokenizer()					,					image_processor=self.get_image_processor()						)
											processor.save_pretrained(self.tmpdirname						)

											a__			 =						self.get_tokenizer(bos_token="""(BOS)"""					,					eos_token="""(EOS)"""						)
											a__			 =						self.get_image_processor(do_normalize=a_					,					padding_value=1.0						)

											a__			 =						GitProcessor.from_pretrained(
											    self.tmpdirname					,					bos_token="""(BOS)"""					,					eos_token="""(EOS)"""					,					do_normalize=a_					,					padding_value=1.0						)

											self.assertEqual(processor.tokenizer.get_vocab()					,					tokenizer_add_kwargs.get_vocab()						)
											self.assertIsInstance(processor.tokenizer					,					a_						)

											self.assertEqual(processor.image_processor.to_json_string()					,					image_processor_add_kwargs.to_json_string()						)
											self.assertIsInstance(processor.image_processor					,					a_						)


							def _a (							self						):
											a__			 =						self.get_image_processor()
											a__			 =						self.get_tokenizer()

											a__			 =						GitProcessor(tokenizer=a_					,					image_processor=a_						)

											a__			 =						self.prepare_image_inputs()

											a__			 =						image_processor(a_					,					return_tensors="""np"""						)
											a__			 =						processor(images=a_					,					return_tensors="""np"""						)

											for key in input_feat_extract.keys():
															self.assertAlmostEqual(input_feat_extract[key].sum()					,					input_processor[key].sum()					,					delta=1E-2						)


							def _a (							self						):
											a__			 =						self.get_image_processor()
											a__			 =						self.get_tokenizer()

											a__			 =						GitProcessor(tokenizer=a_					,					image_processor=a_						)

											a__			 =						"""lower newer"""

											a__			 =						processor(text=a_						)

											a__			 =						tokenizer(a_					,					return_token_type_ids=a_						)

											for key in encoded_tok.keys():
															self.assertListEqual(encoded_tok[key]					,					encoded_processor[key]						)


							def _a (							self						):
											a__			 =						self.get_image_processor()
											a__			 =						self.get_tokenizer()

											a__			 =						GitProcessor(tokenizer=a_					,					image_processor=a_						)

											a__			 =						"""lower newer"""
											a__			 =						self.prepare_image_inputs()

											a__			 =						processor(text=a_					,					images=a_						)

											self.assertListEqual(list(inputs.keys()						)					,					["""input_ids""", """attention_mask""", """pixel_values"""]						)

											# test if it raises when no input is passed
											with pytest.raises(a_						):
															processor()


							def _a (							self						):
											a__			 =						self.get_image_processor()
											a__			 =						self.get_tokenizer()

											a__			 =						GitProcessor(tokenizer=a_					,					image_processor=a_						)

											a__			 =						[[1, 4, 5, 8, 1, 0, 8], [3, 4, 3, 1, 1, 8, 9]]

											a__			 =						processor.batch_decode(a_						)
											a__			 =						tokenizer.batch_decode(a_						)

											self.assertListEqual(a_					,					a_						)







							def _a (							self						):
											a__			 =						self.get_image_processor()
											a__			 =						self.get_tokenizer()

											a__			 =						GitProcessor(tokenizer=a_					,					image_processor=a_						)

											a__			 =						"""lower newer"""
											a__			 =						self.prepare_image_inputs()

											a__			 =						processor(text=a_					,					images=a_						)

											# For now the processor supports only ['input_ids', 'attention_mask', 'pixel_values']
											self.assertListEqual(list(inputs.keys()						)					,					["""input_ids""", """attention_mask""", """pixel_values"""]						)

