



'''simple docstring'''







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		:  List[Any]   ,		__A		:  List[str]   ,		__A		:  int=False   ,		__A		:  List[str]=13   ,		__A		:  int=(32, 128)   ,		__A		:  Tuple=4   ,		__A		:  Tuple=3   ,		__A		:  Optional[Any]=27   ,		__A		:  int=38   ,		__A		:  str=99   ,		__A		:  Optional[int]=99   ,		__A		:  List[str]=32   ,		__A		:  Optional[int]=5   ,		__A		:  Tuple=4   ,		__A		:  List[Any]=4.0   ,		__A		:  Tuple=257   ,		__A		:  str=None   ,		)     ->			Optional[Any]:




								'''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      lowercase__							(  self		:  Any					)     ->			Optional[int]:




								'''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      lowercase__							(  self		:  int					)     ->			Optional[int]:




								'''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      lowercase__							(  self		:  int   ,		__A		:  List[Any]   ,		__A		:  Tuple					)     ->			Tuple:




								'''simple docstring'''





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




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




								'''simple docstring'''





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





@require_torch
class      lowerCamelCase__     (     _a,     _a,     unittest.TestCase				):






	'''simple docstring'''

	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      lowercase__							(  self		:  List[Any]					)     ->			str:




								'''simple docstring'''





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




	def      lowercase__							(  self		:  Optional[int]					)     ->			List[str]:




								'''simple docstring'''





								self.config_tester.run_common_tests()




	def      lowercase__							(  self		:  Optional[Any]					)     ->			Optional[int]:




								'''simple docstring'''





								lowerCAmelCase__  =   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      lowercase__							(  self		:  int					)     ->			Tuple:




								'''simple docstring'''





								pass




	def      lowercase__							(  self		:  str					)     ->			str:




								'''simple docstring'''





								lowerCAmelCase__  ,lowerCAmelCase__  =   self.model_tester.prepare_config_and_inputs_for_common()

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




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




								'''simple docstring'''





								lowerCAmelCase__  ,lowerCAmelCase__  =   self.model_tester.prepare_config_and_inputs_for_common()

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




	@unittest.skip(reason="""MgpstrModel does not support feedforward chunking"""					)
	def      lowercase__							(  self		:  List[Any]					)     ->			Optional[int]:




								'''simple docstring'''





								pass




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




								'''simple docstring'''





								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(__A					)
															self.assertTrue(model.is_gradient_checkpointing					)




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




								'''simple docstring'''





								def check_hidden_states_output(__A		:  int   ,		__A		:  Union[str, Any]   ,		__A		:  Dict					):
															lowerCAmelCase__  =   model_class(__A					)
															model.to(__A					)
															model.eval()

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

															lowerCAmelCase__  =   outputs.hidden_states

															lowerCAmelCase__  =   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__  =   self.model_tester.prepare_config_and_inputs_for_common()

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

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

															check_hidden_states_output(__A   ,		__A   ,		__A					)




	def      lowercase__							(  self		:  Optional[int]					)     ->			Any:




								'''simple docstring'''





								lowerCAmelCase__  ,lowerCAmelCase__  =   self.model_tester.prepare_config_and_inputs_for_common()

								lowerCAmelCase__  =   _config_zero_init(__A					)
								for model_class in self.all_model_classes:
															lowerCAmelCase__  =   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      lowercase__							(  self		:  List[str]					)     ->			Tuple:




								'''simple docstring'''





								pass





def 			_lowerCAmelCase(      ) ->							List[Any]:
							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      lowercase__							(  self		:  Any					)     ->			Optional[Any]:




								'''simple docstring'''





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

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

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

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

								lowerCAmelCase__  =   torch.tensor(
								    [[[-3_9.5_3_9_7, -4_4.4_0_2_4, -3_6.1_8_4_4], [-6_1.4_7_0_9, -6_3.8_6_3_9, -5_8.3_4_5_4], [-7_4.0_2_2_5, -6_8.5_4_9_4, -7_1.2_1_6_4]]]   ,		device=__A   ,		)

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












import os
import unittest

from transformers import is_torch_available
from transformers.testing_utils import require_sentencepiece, require_tokenizers, require_torch, slow, torch_device


if is_torch_available():
      import torch

      from transformers import GPTaLMHeadModel

@require_torch
@require_sentencepiece
@require_tokenizers
class 	__snake_case					(      unittest.TestCase ):






    '''simple docstring'''





    @slow
    @unittest.skip('''Model is not available.'''				)
    def 						A		( self    :		str				):

      """simple docstring"""





      __lowerCAmelCase								=					'''nvidia/megatron-gpt2-345m/'''
      if "MYDIR" in os.environ:
        __lowerCAmelCase								=					os.path.join(os.environ['''MYDIR''']      ,    snake_case				)
      __lowerCAmelCase								=					GPTaLMHeadModel.from_pretrained(snake_case				)
      model.to(snake_case				)
      model.half()

      __lowerCAmelCase								=					torch.tensor(
          [[101, 7_110, 1_005, 1_056, 2_023, 11_333, 17_413, 1_029, 102]]      ,    device=snake_case      ,    dtype=torch.long      ,    )

      with torch.no_grad():
        __lowerCAmelCase								=					model(snake_case				).logits

      __lowerCAmelCase								=					torch.Size((1, 9, 50_257)				)
      self.assertEqual(output.shape      ,    snake_case				)

      __lowerCAmelCase								=					torch.tensor(
          [
              4.9414,
              -0.2920,
              -1.2148,
              -4.0273,
              -0.5161,
              -5.2109,
              -1.2412,
              -1.8301,
              -1.7734,
              -4.7148,
              -0.2317,
              -1.0811,
              -2.1777,
              0.4141,
              -3.7969,
              -4.0586,
              -2.5332,
              -3.3809,
              4.3867,
          ]      ,    device=snake_case      ,    dtype=torch.half      ,    )

      for i in range(19				):
        __lowerCAmelCase						,    __lowerCAmelCase								=					8 * i // 17, 2_792 * i  # along the diagonal
        __lowerCAmelCase						,    __lowerCAmelCase								=					output[0, r, c], expected_diag[i]
        __lowerCAmelCase								=					f'''row={r} col={c} computed={computed} expected={expected}'''
        self.assertAlmostEqual(snake_case      ,    snake_case      ,    delta=1e-4      ,    msg=snake_case				)
