



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						:




							def __init__(	self					:	str    ,    A_					:	Optional[Any]    ,    A_					:	Any=False    ,    A_					:	List[str]=1_3    ,    A_					:	Tuple=(3_2, 1_2_8)    ,    A_					:	Union[str, Any]=4    ,    A_					:	int=3    ,    A_					:	Optional[Any]=2_7    ,    A_					:	Optional[int]=3_8    ,    A_					:	Tuple=9_9    ,    A_					:	List[Any]=9_9    ,    A_					:	Any=3_2    ,    A_					:	Any=5    ,    A_					:	int=4    ,    A_					:	Union[str, Any]=4.0    ,    A_					:	List[str]=2_5_7    ,    A_					:	Any=None    ,    ):
									lowerCAmelCase_      :					List[Any]     =			parent
									lowerCAmelCase_      :					List[str]     =			is_training
									lowerCAmelCase_      :					Tuple     =			batch_size
									lowerCAmelCase_      :					Optional[int]     =			image_size
									lowerCAmelCase_      :					List[str]     =			patch_size
									lowerCAmelCase_      :					Dict     =			num_channels
									lowerCAmelCase_      :					str     =			max_token_length
									lowerCAmelCase_      :					Union[str, Any]     =			num_character_labels
									lowerCAmelCase_      :					int     =			num_bpe_labels
									lowerCAmelCase_      :					str     =			num_wordpiece_labels
									lowerCAmelCase_      :					Union[str, Any]     =			hidden_size
									lowerCAmelCase_      :					int     =			num_hidden_layers
									lowerCAmelCase_      :					Optional[Any]     =			num_attention_heads
									lowerCAmelCase_      :					List[str]     =			mlp_ratio
									lowerCAmelCase_      :					int     =			patch_embeds_hidden_size
									lowerCAmelCase_      :					Any     =			output_hidden_states




							def        UpperCAmelCase__      (	self					:	Union[str, Any]):
									lowerCAmelCase_      :					str     =			floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]])
									lowerCAmelCase_      :					Optional[Any]     =			self.get_config()
									return config, pixel_values




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







							def        UpperCAmelCase__      (	self					:	Tuple):
									lowerCAmelCase_      :					int     =			self.prepare_config_and_inputs()
									lowerCAmelCase_ ,  lowerCAmelCase_      :					List[Any]     =			config_and_inputs
									lowerCAmelCase_      :					str     =			{'''pixel_values''': pixel_values}
									return config, inputs_dict





@require_torch
class     __snake_case						(					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):
									lowerCAmelCase_      :					Dict     =			MgpstrModelTester(self)
									lowerCAmelCase_      :					Optional[int]     =			ConfigTester(self    ,    config_class=A_    ,    has_text_modality=A_)




							def        UpperCAmelCase__      (	self					:	Optional[Any]):
									self.config_tester.run_common_tests()




							def        UpperCAmelCase__      (	self					:	Any):
									lowerCAmelCase_      :					List[str]     =			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        UpperCAmelCase__      (	self					:	Dict):
									pass




							def        UpperCAmelCase__      (	self					:	Optional[Any]):
									lowerCAmelCase_ ,  lowerCAmelCase_      :					Optional[Any]     =			self.model_tester.prepare_config_and_inputs_for_common()

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




							def        UpperCAmelCase__      (	self					:	int):
									lowerCAmelCase_ ,  lowerCAmelCase_      :					Union[str, Any]     =			self.model_tester.prepare_config_and_inputs_for_common()

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

											lowerCAmelCase_      :					List[str]     =			['''pixel_values''']
											self.assertListEqual(arg_names[:1]    ,    A_)




							@unittest.skip(reason='''MgpstrModel does not support feedforward chunking''')
							def        UpperCAmelCase__      (	self					:	List[str]):
									pass




							def        UpperCAmelCase__      (	self					:	Optional[Any]):
									lowerCAmelCase_ ,  lowerCAmelCase_      :					Union[str, Any]     =			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_      :					Optional[int]     =			True
											lowerCAmelCase_      :					List[str]     =			model_class(A_)
											self.assertTrue(model.is_gradient_checkpointing)




							def        UpperCAmelCase__      (	self					:	str):
									def check_hidden_states_output(A_					:	List[str]    ,    A_					:	Dict    ,    A_					:	Any):
											lowerCAmelCase_      :					Union[str, Any]     =			model_class(A_)
											model.to(A_)
											model.eval()

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

											lowerCAmelCase_      :					int     =			outputs.hidden_states

											lowerCAmelCase_      :					Tuple     =			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]    ,    )

									lowerCAmelCase_ ,  lowerCAmelCase_      :					Any     =			self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
											lowerCAmelCase_      :					List[Any]     =			True
											check_hidden_states_output(A_    ,    A_    ,    A_)

											# check that output_hidden_states also work using config
											del inputs_dict["output_hidden_states"]
											lowerCAmelCase_      :					Union[str, Any]     =			True

											check_hidden_states_output(A_    ,    A_    ,    A_)




							def        UpperCAmelCase__      (	self					:	Any):
									lowerCAmelCase_ ,  lowerCAmelCase_      :					Tuple     =			self.model_tester.prepare_config_and_inputs_for_common()

									lowerCAmelCase_      :					Any     =			_config_zero_init(A_)
									for model_class in self.all_model_classes:
											lowerCAmelCase_      :					int     =			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        UpperCAmelCase__      (	self					:	List[str]):
									pass





def       UpperCamelCase(  ):
		lowerCAmelCase_      :					Any     =			'''https://i.postimg.cc/ZKwLg2Gw/367-14.png'''
		lowerCAmelCase_      :					str     =			Image.open(requests.get(__UpperCamelCase					,stream=__UpperCamelCase			).raw			).convert('''RGB'''			)
		return im





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




							@slow
							def        UpperCAmelCase__      (	self					:	Dict):
									lowerCAmelCase_      :					Dict     =			'''alibaba-damo/mgp-str-base'''
									lowerCAmelCase_      :					Tuple     =			MgpstrForSceneTextRecognition.from_pretrained(A_).to(A_)
									lowerCAmelCase_      :					Tuple     =			MgpstrProcessor.from_pretrained(A_)

									lowerCAmelCase_      :					Optional[int]     =			prepare_img()
									lowerCAmelCase_      :					str     =			processor(images=A_    ,    return_tensors='''pt''').pixel_values.to(A_)

									# forward pass
									with torch.no_grad():
											lowerCAmelCase_      :					Optional[int]     =			model(A_)

									# verify the logits
									self.assertEqual(outputs.logits[0].shape    ,    torch.Size((1, 2_7, 3_8)))

									lowerCAmelCase_      :					Any     =			processor.batch_decode(outputs.logits)
									lowerCAmelCase_      :					Dict     =			'''ticket'''

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

									lowerCAmelCase_      :					List[Any]     =			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))










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


A__  :  Tuple       = logging.get_logger(__name__)

A__  :  Tuple       = {
    '''facebook/opt-125m''': '''https://huggingface.co/facebook/opt-125m/blob/main/config.json''',
    '''facebook/opt-350m''': '''https://huggingface.co/facebook/opt-350m/blob/main/config.json''',
    '''facebook/opt-1.3b''': '''https://huggingface.co/facebook/opt-1.3b/blob/main/config.json''',
    '''facebook/opt-2.7b''': '''https://huggingface.co/facebook/opt-2.7b/blob/main/config.json''',
    '''facebook/opt-6.7b''': '''https://huggingface.co/facebook/opt-6.7b/blob/main/config.json''',
    '''facebook/opt-13b''': '''https://huggingface.co/facebook/opt-13b/blob/main/config.json''',
}



class     __snake_case						(					UpperCamelCase_						):
							_a      				=			'''opt'''
							_a      				=			['''past_key_values''']




							def __init__(	self					:	Optional[int]    ,    A_					:	Any=5_0_2_7_2    ,    A_					:	Tuple=7_6_8    ,    A_					:	Optional[Any]=1_2    ,    A_					:	str=3_0_7_2    ,    A_					:	Dict=2_0_4_8    ,    A_					:	List[Any]=True    ,    A_					:	Optional[int]=False    ,    A_					:	Optional[int]=None    ,    A_					:	Dict=0.1    ,    A_					:	str=0.0    ,    A_					:	Union[str, Any]=1_2    ,    A_					:	List[str]="relu"    ,    A_					:	Dict=0.0    ,    A_					:	Union[str, Any]=0.02    ,    A_					:	Optional[Any]=True    ,    A_					:	Optional[Any]=1    ,    A_					:	List[Any]=2    ,    A_					:	Dict=2    ,    A_					:	Optional[int]=True    ,    A_					:	Optional[int]=True    ,    **A_					:	List[Any]    ,    ):
									super().__init__(
									    pad_token_id=A_    ,    bos_token_id=A_    ,    eos_token_id=A_    ,    **A_    ,    )
									lowerCAmelCase_      :					List[Any]     =			vocab_size
									lowerCAmelCase_      :					Union[str, Any]     =			max_position_embeddings
									lowerCAmelCase_      :					Optional[int]     =			num_attention_heads
									lowerCAmelCase_      :					int     =			word_embed_proj_dim if word_embed_proj_dim is not None else hidden_size
									lowerCAmelCase_      :					List[Any]     =			ffn_dim
									lowerCAmelCase_      :					Any     =			hidden_size
									lowerCAmelCase_      :					Union[str, Any]     =			num_hidden_layers
									lowerCAmelCase_      :					str     =			dropout
									lowerCAmelCase_      :					List[str]     =			attention_dropout
									lowerCAmelCase_      :					Any     =			activation_function
									lowerCAmelCase_      :					Union[str, Any]     =			init_std
									lowerCAmelCase_      :					int     =			layerdrop
									lowerCAmelCase_      :					Optional[int]     =			use_cache
									lowerCAmelCase_      :					int     =			do_layer_norm_before
									# We keep these variables at `True` for backward compatibility.
									lowerCAmelCase_      :					Optional[Any]     =			enable_bias
									lowerCAmelCase_      :					Optional[int]     =			layer_norm_elementwise_affine

									# Note that the only purpose of `_remove_final_layer_norm` is to keep backward compatibility
									# with checkpoints that have been fine-tuned before transformers v4.20.1
									# see https://github.com/facebookresearch/metaseq/pull/164
									lowerCAmelCase_      :					Optional[Any]     =			_remove_final_layer_norm





