





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




					"""simple docstring"""


					def __init__(		self							:	List[str]     ,		lowerCamelCase_							:	Optional[int]     ,		lowerCamelCase_							:	Optional[int]=False     ,		lowerCamelCase_							:	Union[str, Any]=13     ,		lowerCamelCase_							:	Dict=(32, 1_28)     ,		lowerCamelCase_							:	int=4     ,		lowerCamelCase_							:	str=3     ,		lowerCamelCase_							:	Tuple=27     ,		lowerCamelCase_							:	Dict=38     ,		lowerCamelCase_							:	Any=99     ,		lowerCamelCase_							:	str=99     ,		lowerCamelCase_							:	List[str]=32     ,		lowerCamelCase_							:	Union[str, Any]=5     ,		lowerCamelCase_							:	Union[str, Any]=4     ,		lowerCamelCase_							:	List[Any]=4.0     ,		lowerCamelCase_							:	List[Any]=2_57     ,		lowerCamelCase_							:	List[Any]=None     ,		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Dict						=	parent
							SCREAMING_SNAKE_CASE				:							Optional[int]						=	is_training
							SCREAMING_SNAKE_CASE				:							Tuple						=	batch_size
							SCREAMING_SNAKE_CASE				:							Optional[int]						=	image_size
							SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	patch_size
							SCREAMING_SNAKE_CASE				:							Optional[int]						=	num_channels
							SCREAMING_SNAKE_CASE				:							Optional[int]						=	max_token_length
							SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	num_character_labels
							SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	num_bpe_labels
							SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	num_wordpiece_labels
							SCREAMING_SNAKE_CASE				:							Optional[Any]						=	hidden_size
							SCREAMING_SNAKE_CASE				:							Optional[int]						=	num_hidden_layers
							SCREAMING_SNAKE_CASE				:							str						=	num_attention_heads
							SCREAMING_SNAKE_CASE				:							Optional[Any]						=	mlp_ratio
							SCREAMING_SNAKE_CASE				:							Dict						=	patch_embeds_hidden_size
							SCREAMING_SNAKE_CASE				:							str						=	output_hidden_states


					def lowerCamelCase_	(		self							:	Tuple		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]		)
							SCREAMING_SNAKE_CASE				:							int						=	self.get_config()
							return config, pixel_values


					def lowerCamelCase_	(		self							:	List[str]		):

							'''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 lowerCamelCase_	(		self							:	Any     ,		lowerCamelCase_							:	Tuple     ,		lowerCamelCase_							:	Union[str, Any]		):

							'''simple docstring'''



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



					def lowerCamelCase_	(		self							:	Union[str, Any]		):

							'''simple docstring'''



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







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




					"""simple docstring"""
					SCREAMING_SNAKE_CASE__						=					(MgpstrForSceneTextRecognition,) if is_torch_available() else ()
					SCREAMING_SNAKE_CASE__						=					{'feature-extraction': MgpstrForSceneTextRecognition} if is_torch_available() else {}
					SCREAMING_SNAKE_CASE__						=					False

					SCREAMING_SNAKE_CASE__						=					False
					SCREAMING_SNAKE_CASE__						=					False
					SCREAMING_SNAKE_CASE__						=					False
					SCREAMING_SNAKE_CASE__						=					False


					def lowerCamelCase_	(		self							:	Tuple		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							List[str]						=	MgpstrModelTester(self		)
							SCREAMING_SNAKE_CASE				:							int						=	ConfigTester(self     ,		config_class=lowerCamelCase_     ,		has_text_modality=lowerCamelCase_		)


					def lowerCamelCase_	(		self							:	str		):

							'''simple docstring'''



							self.config_tester.run_common_tests()


					def lowerCamelCase_	(		self							:	Optional[Any]		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Optional[int]						=	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 lowerCamelCase_	(		self							:	Optional[Any]		):

							'''simple docstring'''



							pass


					def lowerCamelCase_	(		self							:	List[Any]		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Any						=	self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
									SCREAMING_SNAKE_CASE				:							int						=	model_class(lowerCamelCase_		)
									self.assertIsInstance(model.get_input_embeddings()     ,		(nn.Module)		)
									SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	model.get_output_embeddings()
									self.assertTrue(x is None or isinstance(lowerCamelCase_     ,		nn.Linear		)		)


					def lowerCamelCase_	(		self							:	List[str]		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							List[str]						=	self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
									SCREAMING_SNAKE_CASE				:							Optional[Any]						=	model_class(lowerCamelCase_		)
									SCREAMING_SNAKE_CASE				:							Optional[Any]						=	inspect.signature(model.forward		)
									# signature.parameters is an OrderedDict => so arg_names order is deterministic
									SCREAMING_SNAKE_CASE				:							List[str]						=	[*signature.parameters.keys()]

									SCREAMING_SNAKE_CASE				:							Dict						=	["""pixel_values"""]
									self.assertListEqual(arg_names[:1]     ,		lowerCamelCase_		)


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

							'''simple docstring'''



							pass


					def lowerCamelCase_	(		self							:	Optional[Any]		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Dict						=	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				:							str						=	True
									SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	model_class(lowerCamelCase_		)
									self.assertTrue(model.is_gradient_checkpointing		)


					def lowerCamelCase_	(		self							:	Any		):

							'''simple docstring'''



							def check_hidden_states_output(lowerCamelCase_							:	Optional[int]     ,		lowerCamelCase_							:	Optional[int]     ,		lowerCamelCase_							:	List[Any]		):
									SCREAMING_SNAKE_CASE				:							Any						=	model_class(lowerCamelCase_		)
									model.to(lowerCamelCase_		)
									model.eval()

									with torch.no_grad():
											SCREAMING_SNAKE_CASE				:							Optional[int]						=	model(**self._prepare_for_class(lowerCamelCase_     ,		lowerCamelCase_		)		)

									SCREAMING_SNAKE_CASE				:							int						=	outputs.hidden_states

									SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	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]     ,		)

							SCREAMING_SNAKE_CASE				:							int						=	self.model_tester.prepare_config_and_inputs_for_common()

							for model_class in self.all_model_classes:
									SCREAMING_SNAKE_CASE				:							Tuple						=	True
									check_hidden_states_output(lowerCamelCase_     ,		lowerCamelCase_     ,		lowerCamelCase_		)

									# check that output_hidden_states also work using config
									del inputs_dict["output_hidden_states"]
									SCREAMING_SNAKE_CASE				:							List[Any]						=	True

									check_hidden_states_output(lowerCamelCase_     ,		lowerCamelCase_     ,		lowerCamelCase_		)


					def lowerCamelCase_	(		self							:	Optional[Any]		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Dict						=	self.model_tester.prepare_config_and_inputs_for_common()

							SCREAMING_SNAKE_CASE				:							str						=	_config_zero_init(lowerCamelCase_		)
							for model_class in self.all_model_classes:
									SCREAMING_SNAKE_CASE				:							Tuple						=	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 lowerCamelCase_	(		self							:	int		):

							'''simple docstring'''



							pass







def 						__A						(       ):


		"""simple docstring"""



		SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	"""https://i.postimg.cc/ZKwLg2Gw/367-14.png"""
		SCREAMING_SNAKE_CASE				:							List[Any]						=	Image.open(requests.get(lowerCamelCase_	,	stream=lowerCamelCase_					).raw					).convert("""RGB"""					)
		return im




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




					"""simple docstring"""


					@slow
					def lowerCamelCase_	(		self							:	Optional[Any]		):

							'''simple docstring'''



							SCREAMING_SNAKE_CASE				:							Dict						=	"""alibaba-damo/mgp-str-base"""
							SCREAMING_SNAKE_CASE				:							List[Any]						=	MgpstrForSceneTextRecognition.from_pretrained(lowerCamelCase_		).to(lowerCamelCase_		)
							SCREAMING_SNAKE_CASE				:							Any						=	MgpstrProcessor.from_pretrained(lowerCamelCase_		)

							SCREAMING_SNAKE_CASE				:							List[Any]						=	prepare_img()
							SCREAMING_SNAKE_CASE				:							int						=	processor(images=lowerCamelCase_     ,		return_tensors="""pt"""		).pixel_values.to(lowerCamelCase_		)

							# forward pass
							with torch.no_grad():
									SCREAMING_SNAKE_CASE				:							List[str]						=	model(lowerCamelCase_		)

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

							SCREAMING_SNAKE_CASE				:							int						=	processor.batch_decode(outputs.logits		)
							SCREAMING_SNAKE_CASE				:							Tuple						=	"""ticket"""

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

							SCREAMING_SNAKE_CASE				:							List[Any]						=	torch.tensor(
							    [[[-39.53_97, -44.40_24, -36.18_44], [-61.47_09, -63.86_39, -58.34_54], [-74.02_25, -68.54_94, -71.21_64]]]     ,		device=lowerCamelCase_     ,		)

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










'''simple docstring'''




from ...configuration_utils import PretrainedConfig
from ...utils import logging


__UpperCAmelCase         =   logging.get_logger(__name__)

__UpperCAmelCase         =   {
    """uw-madison/yoso-4096""": """https://huggingface.co/uw-madison/yoso-4096/resolve/main/config.json""",
    # See all YOSO models at https://huggingface.co/models?filter=yoso
}




class       UpperCamelCase__			(			lowercase_	):




					"""simple docstring"""
					SCREAMING_SNAKE_CASE__						=					'''yoso'''


					def __init__(		self							:	str     ,		lowerCamelCase_							:	Tuple=5_02_65     ,		lowerCamelCase_							:	int=7_68     ,		lowerCamelCase_							:	Optional[Any]=12     ,		lowerCamelCase_							:	int=12     ,		lowerCamelCase_							:	Optional[Any]=30_72     ,		lowerCamelCase_							:	str="gelu"     ,		lowerCamelCase_							:	Union[str, Any]=0.1     ,		lowerCamelCase_							:	int=0.1     ,		lowerCamelCase_							:	Optional[int]=40_96     ,		lowerCamelCase_							:	str=1     ,		lowerCamelCase_							:	str=0.02     ,		lowerCamelCase_							:	Dict=1e-12     ,		lowerCamelCase_							:	Tuple="absolute"     ,		lowerCamelCase_							:	Any=True     ,		lowerCamelCase_							:	Dict=9     ,		lowerCamelCase_							:	Any=64     ,		lowerCamelCase_							:	Union[str, Any]=None     ,		lowerCamelCase_							:	List[str]=True     ,		lowerCamelCase_							:	Dict=True     ,		lowerCamelCase_							:	int=1     ,		lowerCamelCase_							:	Tuple=0     ,		lowerCamelCase_							:	Dict=2     ,		**lowerCamelCase_							:	Tuple     ,		):

							'''simple docstring'''



							super().__init__(pad_token_id=lowerCamelCase_     ,		bos_token_id=lowerCamelCase_     ,		eos_token_id=lowerCamelCase_     ,		**lowerCamelCase_		)

							SCREAMING_SNAKE_CASE				:							int						=	vocab_size
							SCREAMING_SNAKE_CASE				:							List[Any]						=	max_position_embeddings
							SCREAMING_SNAKE_CASE				:							Tuple						=	hidden_size
							SCREAMING_SNAKE_CASE				:							Dict						=	num_hidden_layers
							SCREAMING_SNAKE_CASE				:							Any						=	num_attention_heads
							SCREAMING_SNAKE_CASE				:							int						=	intermediate_size
							SCREAMING_SNAKE_CASE				:							Optional[Any]						=	hidden_act
							SCREAMING_SNAKE_CASE				:							Optional[Any]						=	hidden_dropout_prob
							SCREAMING_SNAKE_CASE				:							List[Any]						=	attention_probs_dropout_prob
							SCREAMING_SNAKE_CASE				:							str						=	initializer_range
							SCREAMING_SNAKE_CASE				:							List[str]						=	type_vocab_size
							SCREAMING_SNAKE_CASE				:							List[str]						=	layer_norm_eps
							SCREAMING_SNAKE_CASE				:							Any						=	position_embedding_type
							SCREAMING_SNAKE_CASE				:							Optional[int]						=	use_expectation
							SCREAMING_SNAKE_CASE				:							int						=	hash_code_len
							SCREAMING_SNAKE_CASE				:							Union[str, Any]						=	num_hash
							SCREAMING_SNAKE_CASE				:							Optional[Any]						=	conv_window
							SCREAMING_SNAKE_CASE				:							Optional[Any]						=	use_fast_hash
							SCREAMING_SNAKE_CASE				:							Optional[int]						=	lsh_backward



