


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     :    Optional[Any]	,							lowerCamelCase_     :    Tuple	,							lowerCamelCase_     :    int=False	,							lowerCamelCase_     :    Dict=13	,							lowerCamelCase_     :    Optional[Any]=(32, 128)	,							lowerCamelCase_     :    Optional[Any]=4	,							lowerCamelCase_     :    str=3	,							lowerCamelCase_     :    int=27	,							lowerCamelCase_     :    int=38	,							lowerCamelCase_     :    Union[str, Any]=99	,							lowerCamelCase_     :    Tuple=99	,							lowerCamelCase_     :    Union[str, Any]=32	,							lowerCamelCase_     :    int=5	,							lowerCamelCase_     :    Union[str, Any]=4	,							lowerCamelCase_     :    Optional[Any]=4.0	,							lowerCamelCase_     :    Any=257	,							lowerCamelCase_     :    Any=None	,							):
								_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        lowerCAmelCase__			(				self     :    Tuple					):
								_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        lowerCAmelCase__			(				self     :    Optional[Any]					):
								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     :    List[Any]	,							lowerCamelCase_     :    List[Any]	,							lowerCamelCase_     :    str					):
								_lowerCAmelCase  						=MgpstrForSceneTextRecognition(lowerCamelCase_					)
								model.to(lowerCamelCase_					)
								model.eval()
								with torch.no_grad():
												_lowerCAmelCase  						=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]					):
								_lowerCAmelCase  						=self.prepare_config_and_inputs()
								_lowerCAmelCase						,							_lowerCAmelCase  						=config_and_inputs
								_lowerCAmelCase  						={"""pixel_values""": pixel_values}
								return config, inputs_dict


@require_torch
class        __lowerCamelCase      (				lowerCamelCase_				,		lowerCamelCase_				,		unittest.TestCase ):


				"""simple docstring"""







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

				a_:		Union[str, Any]					      =	False
				a_:		Any					      =	False
				a_:		List[str]					      =	False
				a_:		Optional[int]					      =	False






				def        lowerCAmelCase__			(				self     :    List[Any]					):
								_lowerCAmelCase  						=MgpstrModelTester(self					)
								_lowerCAmelCase  						=ConfigTester(self	,							config_class=lowerCamelCase_	,							has_text_modality=lowerCamelCase_					)






				def        lowerCAmelCase__			(				self     :    Any					):
								self.config_tester.run_common_tests()






				def        lowerCAmelCase__			(				self     :    int					):
								_lowerCAmelCase  						=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[int]					):
								pass






				def        lowerCAmelCase__			(				self     :    List[str]					):
								_lowerCAmelCase						,							_lowerCAmelCase  						=self.model_tester.prepare_config_and_inputs_for_common()

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






				def        lowerCAmelCase__			(				self     :    Tuple					):
								_lowerCAmelCase						,							_lowerCAmelCase  						=self.model_tester.prepare_config_and_inputs_for_common()

								for model_class in self.all_model_classes:
												_lowerCAmelCase  						=model_class(lowerCamelCase_					)
												_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]	,							lowerCamelCase_					)






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






				def        lowerCAmelCase__			(				self     :    str					):
								_lowerCAmelCase						,							_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(lowerCamelCase_					)
												self.assertTrue(model.is_gradient_checkpointing					)






				def        lowerCAmelCase__			(				self     :    str					):
								def check_hidden_states_output(lowerCamelCase_     :    Optional[int]	,							lowerCamelCase_     :    List[Any]	,							lowerCamelCase_     :    int					):
												_lowerCAmelCase  						=model_class(lowerCamelCase_					)
												model.to(lowerCamelCase_					)
												model.eval()

												with torch.no_grad():
																_lowerCAmelCase  						=model(**self._prepare_for_class(lowerCamelCase_	,							lowerCamelCase_					)					)

												_lowerCAmelCase  						=outputs.hidden_states

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

								_lowerCAmelCase						,							_lowerCAmelCase  						=self.model_tester.prepare_config_and_inputs_for_common()

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

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

												check_hidden_states_output(lowerCamelCase_	,							lowerCamelCase_	,							lowerCamelCase_					)






				def        lowerCAmelCase__			(				self     :    int					):
								_lowerCAmelCase						,							_lowerCAmelCase  						=self.model_tester.prepare_config_and_inputs_for_common()

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

def 		snake_case_   (				):






				'''simple docstring'''






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





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


				"""simple docstring"""







				@slow
				def        lowerCAmelCase__			(				self     :    Optional[int]					):
								_lowerCAmelCase  						="""alibaba-damo/mgp-str-base"""
								_lowerCAmelCase  						=MgpstrForSceneTextRecognition.from_pretrained(lowerCamelCase_					).to(lowerCamelCase_					)
								_lowerCAmelCase  						=MgpstrProcessor.from_pretrained(lowerCamelCase_					)

								_lowerCAmelCase  						=prepare_img()
								_lowerCAmelCase  						=processor(images=lowerCamelCase_	,							return_tensors="""pt"""					).pixel_values.to(lowerCamelCase_					)

								# forward pass
								with torch.no_grad():
												_lowerCAmelCase  						=model(lowerCamelCase_					)

								# 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]	,							lowerCamelCase_					)

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

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





from functools import partial
from typing import Any, Callable, Dict, List, Type, TypeVar, Union, overload

import torch
import torch.nn as nn
import torch.types

def 		snake_case_   (				lowercase__     :    torch.Tensor       ,     lowercase__     :    torch.Tensor       ,     lowercase__     :    bool     ):






				'''simple docstring'''






				if not inplace:
								_lowerCAmelCase  						=ma + ma
				else:
								ma += ma

				return ma

def 		snake_case_   (				lowercase__     :    torch.Tensor       ,     lowercase__     :    List[int]     ):






				'''simple docstring'''






				_lowerCAmelCase  						=-1 * len(lowercase__     )
				_lowerCAmelCase  						=list(range(len(tensor.shape[:zero_index]     )     )     )
				return tensor.permute(first_inds + [zero_index + i for i in inds]     )

def 		snake_case_   (				lowercase__     :    torch.Tensor       ,     lowercase__     :    int     ):






				'''simple docstring'''






				return t.reshape(t.shape[:-no_dims] + (-1,)     )

def 		snake_case_   (				lowercase__     :    torch.Tensor       ,     lowercase__     :    torch.Tensor       ,     lowercase__     :    int       ,     lowercase__     :    float = 1e-4     ):






				'''simple docstring'''






				_lowerCAmelCase  						=mask.expand(*value.shape     )
				return torch.sum(mask * value       ,     dim=lowercase__     ) / (eps + torch.sum(lowercase__       ,     dim=lowercase__     ))

def 		snake_case_   (				lowercase__     :    torch.Tensor       ,     lowercase__     :    torch.types.Number = 2.3_1_2_5       ,     lowercase__     :    torch.types.Number = 2_1.6_8_7_5       ,     lowercase__     :    int = 64     ):






				'''simple docstring'''






				_lowerCAmelCase  						=torch.linspace(lowercase__       ,     lowercase__       ,     no_bins - 1       ,     device=pts.device     )
				_lowerCAmelCase  						=torch.sqrt(torch.sum((pts.unsqueeze(-2     ) - pts.unsqueeze(-3     )) ** 2       ,     dim=-1     )     )
				return torch.bucketize(lowercase__       ,     lowercase__     )

def 		snake_case_   (				lowercase__     :    Callable[[list], Any]       ,     lowercase__     :    List[dict]     ):






				'''simple docstring'''






				_lowerCAmelCase  						=dicts[0]
				_lowerCAmelCase  						={}
				for k, v in first.items():
								_lowerCAmelCase  						=[d[k] for d in dicts]
								if isinstance(lowercase__       ,     lowercase__     ):
												_lowerCAmelCase  						=dict_multimap(lowercase__       ,     lowercase__     )
								else:
												_lowerCAmelCase  						=fn(lowercase__     )

				return new_dict

def 		snake_case_   (				lowercase__     :    torch.Tensor       ,     lowercase__     :    torch.Tensor     ):






				'''simple docstring'''






				_lowerCAmelCase  						=v_bins.view(((1,) * len(x.shape     )) + (len(lowercase__     ),)     )
				_lowerCAmelCase  						=x[..., None] - reshaped_bins
				_lowerCAmelCase  						=torch.argmin(torch.abs(lowercase__     )       ,     dim=-1     )
				return nn.functional.one_hot(lowercase__       ,     num_classes=len(lowercase__     )     ).float()

def 		snake_case_   (				lowercase__     :    torch.Tensor       ,     lowercase__     :    torch.Tensor       ,     lowercase__     :    int = 0       ,     lowercase__     :    int = 0     ):






				'''simple docstring'''






				_lowerCAmelCase  						=[]
				for i, s in enumerate(data.shape[:no_batch_dims]     ):
								_lowerCAmelCase  						=torch.arange(lowercase__     )
								_lowerCAmelCase  						=r.view(*(*((1,) * i), -1, *((1,) * (len(inds.shape     ) - i - 1)))     )
								ranges.append(lowercase__     )

				_lowerCAmelCase  						=[slice(lowercase__     ) for _ in range(len(data.shape     ) - no_batch_dims     )]
				_lowerCAmelCase  						=inds
				ranges.extend(lowercase__     )
				# Matt note: Editing this to get around the behaviour of using a list as an array index changing
				# in recent Numpy versions
				return data[tuple(lowercase__     )]


__SCREAMING_SNAKE_CASE	:							str           =  TypeVar('''T''')

def 		snake_case_   (				lowercase__     :    Callable[[T], Any]       ,     lowercase__     :    Dict[Any, Union[dict, list, tuple, T]]       ,     lowercase__     :    Type[T]     ):






				'''simple docstring'''






				_lowerCAmelCase  						={}
				for k, v in dic.items():
								if isinstance(lowercase__       ,     lowercase__     ):
												_lowerCAmelCase  						=dict_map(lowercase__       ,     lowercase__       ,     lowercase__     )
								else:
												_lowerCAmelCase  						=tree_map(lowercase__       ,     lowercase__       ,     lowercase__     )

				return new_dict

@overload
def 		snake_case_   (				lowercase__     :    Callable[[T], Any]       ,     lowercase__     :    T       ,     lowercase__     :    Type[T]     ):






				'''simple docstring'''






				...

@overload
def 		snake_case_   (				lowercase__     :    Callable[[T], Any]       ,     lowercase__     :    dict       ,     lowercase__     :    Type[T]     ):






				'''simple docstring'''






				...

@overload
def 		snake_case_   (				lowercase__     :    Callable[[T], Any]       ,     lowercase__     :    list       ,     lowercase__     :    Type[T]     ):






				'''simple docstring'''






				...

@overload
def 		snake_case_   (				lowercase__     :    Callable[[T], Any]       ,     lowercase__     :    tuple       ,     lowercase__     :    Type[T]     ):






				'''simple docstring'''






				...

def 		snake_case_   (				lowercase__     :    Optional[Any]       ,     lowercase__     :    str       ,     lowercase__     :    Optional[int]     ):






				'''simple docstring'''






				if isinstance(lowercase__       ,     lowercase__     ):
								return dict_map(lowercase__       ,     lowercase__       ,     lowercase__     )
				elif isinstance(lowercase__       ,     lowercase__     ):
								return [tree_map(lowercase__       ,     lowercase__       ,     lowercase__     ) for x in tree]
				elif isinstance(lowercase__       ,     lowercase__     ):
								return tuple(tree_map(lowercase__       ,     lowercase__       ,     lowercase__     ) for x in tree     )
				elif isinstance(lowercase__       ,     lowercase__     ):
								return fn(lowercase__     )
				else:
								print(type(lowercase__     )     )
								raise ValueError("""Not supported"""     )





__SCREAMING_SNAKE_CASE	:							Any           =  partial(tree_map, leaf_type=torch.Tensor)

