


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						, _UpperCAmelCase						, _UpperCAmelCase=False						, _UpperCAmelCase=13						, _UpperCAmelCase=(32, 1_28)						, _UpperCAmelCase=4						, _UpperCAmelCase=3						, _UpperCAmelCase=27						, _UpperCAmelCase=38						, _UpperCAmelCase=99						, _UpperCAmelCase=99						, _UpperCAmelCase=32						, _UpperCAmelCase=5						, _UpperCAmelCase=4						, _UpperCAmelCase=4.0						, _UpperCAmelCase=2_57						, _UpperCAmelCase=None						, ):
       snake_case_			= parent
       snake_case_			= is_training
       snake_case_			= batch_size
       snake_case_			= image_size
       snake_case_			= patch_size
       snake_case_			= num_channels
       snake_case_			= max_token_length
       snake_case_			= num_character_labels
       snake_case_			= num_bpe_labels
       snake_case_			= num_wordpiece_labels
       snake_case_			= hidden_size
       snake_case_			= num_hidden_layers
       snake_case_			= num_attention_heads
       snake_case_			= mlp_ratio
       snake_case_			= patch_embeds_hidden_size
       snake_case_			= output_hidden_states



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



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





 def 		UpperCamelCase__	(      self   ):
       snake_case_			= self.prepare_config_and_inputs()
       snake_case_			,       snake_case_			= config_and_inputs
       snake_case_			= {'''pixel_values''': pixel_values}
       return config, inputs_dict







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

 '''simple docstring'''

 __snake_case				=    (MgpstrForSceneTextRecognition,) if is_torch_available() else ()
 __snake_case				=    {"feature-extraction": MgpstrForSceneTextRecognition} if is_torch_available() else {}
 __snake_case				=    False

 __snake_case				=    False
 __snake_case				=    False
 __snake_case				=    False
 __snake_case				=    False



 def 		UpperCamelCase__	(      self   ):
       snake_case_			= MgpstrModelTester(self   )
       snake_case_			= ConfigTester(self						, config_class=_UpperCAmelCase						, has_text_modality=_UpperCAmelCase   )



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



 def 		UpperCamelCase__	(      self   ):
       snake_case_			= self.model_tester.prepare_config_and_inputs()
       self.model_tester.create_and_check_model(*_UpperCAmelCase   )



 @unittest.skip(reason='''MgpstrModel does not use inputs_embeds'''   )
 def 		UpperCamelCase__	(      self   ):
       pass



 def 		UpperCamelCase__	(      self   ):
       snake_case_			,       snake_case_			= self.model_tester.prepare_config_and_inputs_for_common()

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



 def 		UpperCamelCase__	(      self   ):
       snake_case_			,       snake_case_			= self.model_tester.prepare_config_and_inputs_for_common()

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

             snake_case_			= ['''pixel_values''']
             self.assertListEqual(arg_names[:1]						, _UpperCAmelCase   )



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



 def 		UpperCamelCase__	(      self   ):
       snake_case_			,       snake_case_			= 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

             snake_case_			= True
             snake_case_			= model_class(_UpperCAmelCase   )
             self.assertTrue(model.is_gradient_checkpointing   )



 def 		UpperCamelCase__	(      self   ):
       def check_hidden_states_output(_UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase   ):
             snake_case_			= model_class(_UpperCAmelCase   )
             model.to(_UpperCAmelCase   )
             model.eval()

             with torch.no_grad():
                   snake_case_			= model(**self._prepare_for_class(_UpperCAmelCase						, _UpperCAmelCase   )   )

             snake_case_			= outputs.hidden_states

             snake_case_			= getattr(
                 self.model_tester						, '''expected_num_hidden_layers'''						, self.model_tester.num_hidden_layers + 1   )
             self.assertEqual(len(_UpperCAmelCase   )						, _UpperCAmelCase   )

             self.assertListEqual(
                 list(hidden_states[0].shape[-2:]   )						, [self.model_tester.patch_embeds_hidden_size, self.model_tester.hidden_size]						, )

       snake_case_			,       snake_case_			= self.model_tester.prepare_config_and_inputs_for_common()

       for model_class in self.all_model_classes:
             snake_case_			= True
             check_hidden_states_output(_UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase   )

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

             check_hidden_states_output(_UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase   )



 def 		UpperCamelCase__	(      self   ):
       snake_case_			,       snake_case_			= self.model_tester.prepare_config_and_inputs_for_common()

       snake_case_			= _config_zero_init(_UpperCAmelCase   )
       for model_class in self.all_model_classes:
             snake_case_			= model_class(config=_UpperCAmelCase   )
             for name, param in model.named_parameters():
                   if isinstance(_UpperCAmelCase						, (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   ):
       pass






def 			__lowerCAmelCase       ()->    List[Any]:




      """simple docstring"""

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







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

 '''simple docstring'''

 @slow
 def 		UpperCamelCase__	(      self   ):
       snake_case_			= '''alibaba-damo/mgp-str-base'''
       snake_case_			= MgpstrForSceneTextRecognition.from_pretrained(_UpperCAmelCase   ).to(_UpperCAmelCase   )
       snake_case_			= MgpstrProcessor.from_pretrained(_UpperCAmelCase   )

       snake_case_			= prepare_img()
       snake_case_			= processor(images=_UpperCAmelCase						, return_tensors='''pt'''   ).pixel_values.to(_UpperCAmelCase   )

       # forward pass
       with torch.no_grad():
             snake_case_			= model(_UpperCAmelCase   )

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

       snake_case_			= processor.batch_decode(outputs.logits   )
       snake_case_			= '''ticket'''

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

       snake_case_			= 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=_UpperCAmelCase						, )

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



from collections import OrderedDict
from typing import Any, Mapping, Optional

from ... import PreTrainedTokenizer
from ...configuration_utils import PretrainedConfig
from ...file_utils import TensorType, is_torch_available
from ...onnx import OnnxConfig, OnnxConfigWithPast, OnnxSeqaSeqConfigWithPast
from ...onnx.utils import compute_effective_axis_dimension
from ...utils import logging


UpperCAmelCase     =       logging.get_logger(__name__)

UpperCAmelCase     =       {
    """facebook/blenderbot-3B""": """https://huggingface.co/facebook/blenderbot-3B/resolve/main/config.json""",
    # See all Blenderbot models at https://huggingface.co/models?filter=blenderbot
}







class lowerCAmelCase_		(					lowerCamelCase__			):

 '''simple docstring'''

 __snake_case				=    "blenderbot"
 __snake_case				=    ["past_key_values"]
 __snake_case				=    {"num_attention_heads": "encoder_attention_heads", "hidden_size": "d_model"}



 def __init__(      self						, _UpperCAmelCase=80_08						, _UpperCAmelCase=1_28						, _UpperCAmelCase=2						, _UpperCAmelCase=1_02_40						, _UpperCAmelCase=32						, _UpperCAmelCase=24						, _UpperCAmelCase=1_02_40						, _UpperCAmelCase=32						, _UpperCAmelCase=0.0						, _UpperCAmelCase=0.0						, _UpperCAmelCase=True						, _UpperCAmelCase=True						, _UpperCAmelCase="gelu"						, _UpperCAmelCase=25_60						, _UpperCAmelCase=0.1						, _UpperCAmelCase=0.0						, _UpperCAmelCase=0.0						, _UpperCAmelCase=0.02						, _UpperCAmelCase=1						, _UpperCAmelCase=False						, _UpperCAmelCase=0						, _UpperCAmelCase=1						, _UpperCAmelCase=2						, _UpperCAmelCase=3						, _UpperCAmelCase=2						, **_UpperCAmelCase						, ):
       snake_case_			= vocab_size
       snake_case_			= max_position_embeddings
       snake_case_			= d_model
       snake_case_			= encoder_ffn_dim
       snake_case_			= encoder_layers
       snake_case_			= encoder_attention_heads
       snake_case_			= decoder_ffn_dim
       snake_case_			= decoder_layers
       snake_case_			= decoder_attention_heads
       snake_case_			= dropout
       snake_case_			= attention_dropout
       snake_case_			= activation_dropout
       snake_case_			= activation_function
       snake_case_			= init_std
       snake_case_			= encoder_layerdrop
       snake_case_			= decoder_layerdrop
       snake_case_			= use_cache
       snake_case_			= encoder_layers
       snake_case_			= scale_embedding  # scale factor will be sqrt(d_model) if True

       super().__init__(
           pad_token_id=_UpperCAmelCase						, bos_token_id=_UpperCAmelCase						, eos_token_id=_UpperCAmelCase						, is_encoder_decoder=_UpperCAmelCase						, decoder_start_token_id=_UpperCAmelCase						, encoder_no_repeat_ngram_size=_UpperCAmelCase						, forced_eos_token_id=_UpperCAmelCase						, **_UpperCAmelCase						, )







class lowerCAmelCase_		(					lowerCamelCase__			):

 '''simple docstring'''

 @property
 def 		UpperCamelCase__	(      self   ):
       if self.task in ["default", "seq2seq-lm"]:
             snake_case_			= OrderedDict(
                 [
                     ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                     ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
                 ]   )
             if self.use_past:
                   snake_case_			= {0: '''batch'''}
                   snake_case_			= {0: '''batch''', 1: '''past_decoder_sequence + sequence'''}
             else:
                   snake_case_			= {0: '''batch''', 1: '''decoder_sequence'''}
                   snake_case_			= {0: '''batch''', 1: '''decoder_sequence'''}
             if self.use_past:
                   self.fill_with_past_key_values_(_UpperCAmelCase						, direction='''inputs'''   )
       elif self.task == "causal-lm":
             snake_case_			= OrderedDict(
                 [
                     ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                     ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
                 ]   )
             if self.use_past:
                   snake_case_			,       snake_case_			= self.num_layers
                   for i in range(_UpperCAmelCase   ):
                         snake_case_			= {0: '''batch''', 2: '''past_sequence + sequence'''}
                         snake_case_			= {0: '''batch''', 2: '''past_sequence + sequence'''}
       else:
             snake_case_			= OrderedDict(
                 [
                     ('''input_ids''', {0: '''batch''', 1: '''encoder_sequence'''}),
                     ('''attention_mask''', {0: '''batch''', 1: '''encoder_sequence'''}),
                     ('''decoder_input_ids''', {0: '''batch''', 1: '''decoder_sequence'''}),
                     ('''decoder_attention_mask''', {0: '''batch''', 1: '''decoder_sequence'''}),
                 ]   )

       return common_inputs



 @property
 # Copied from transformers.models.bart.configuration_bart.BartOnnxConfig.outputs
 def 		UpperCamelCase__	(      self   ):
       if self.task in ["default", "seq2seq-lm"]:
             snake_case_			= super().outputs
       else:
             snake_case_			= super(_UpperCAmelCase						, self   ).outputs
             if self.use_past:
                   snake_case_			,       snake_case_			= self.num_layers
                   for i in range(_UpperCAmelCase   ):
                         snake_case_			= {0: '''batch''', 2: '''past_sequence + sequence'''}
                         snake_case_			= {0: '''batch''', 2: '''past_sequence + sequence'''}
       return common_outputs



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase						, _UpperCAmelCase = -1						, _UpperCAmelCase = -1						, _UpperCAmelCase = False						, _UpperCAmelCase = None						, ):
       snake_case_			= self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
           _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase   )
       # Generate decoder inputs
       snake_case_			= seq_length if not self.use_past else 1
       snake_case_			= self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
           _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase   )
       snake_case_			= {F'''decoder_{name}''': tensor for name, tensor in decoder_inputs.items()}
       snake_case_			= dict(**_UpperCAmelCase						, **_UpperCAmelCase   )

       if self.use_past:
             if not is_torch_available():
                   raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''   )
             else:
                   import torch
             snake_case_			,       snake_case_			= common_inputs['''input_ids'''].shape
             snake_case_			= common_inputs['''decoder_input_ids'''].shape[1]
             snake_case_			,       snake_case_			= self.num_attention_heads
             snake_case_			= (
                 batch,
                 num_encoder_attention_heads,
                 encoder_seq_length,
                 self._config.hidden_size // num_encoder_attention_heads,
             )
             snake_case_			= decoder_seq_length
             snake_case_			= (
                 batch,
                 num_decoder_attention_heads,
                 decoder_past_length,
                 self._config.hidden_size // num_decoder_attention_heads,
             )
             snake_case_			= torch.cat(
                 [common_inputs['''decoder_attention_mask'''], torch.ones(_UpperCAmelCase						, _UpperCAmelCase   )]						, dim=1   )
             snake_case_			= []
             snake_case_			,       snake_case_			= self.num_layers

             for _ in range(_UpperCAmelCase   ):
                   common_inputs["past_key_values"].append(
                       (
                           torch.zeros(_UpperCAmelCase   ),
                           torch.zeros(_UpperCAmelCase   ),
                           torch.zeros(_UpperCAmelCase   ),
                           torch.zeros(_UpperCAmelCase   ),
                       )   )
       return common_inputs



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase						, _UpperCAmelCase = -1						, _UpperCAmelCase = -1						, _UpperCAmelCase = False						, _UpperCAmelCase = None						, ):
       snake_case_			= self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
           _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase   )

       if self.use_past:
             if not is_torch_available():
                   raise ValueError('''Cannot generate dummy past_keys inputs without PyTorch installed.'''   )
             else:
                   import torch
             snake_case_			,       snake_case_			= common_inputs['''input_ids'''].shape
             snake_case_			= seqlen
             snake_case_			,       snake_case_			= self.num_layers
             snake_case_			,       snake_case_			= self.num_attention_heads
             snake_case_			= (
                 batch,
                 num_encoder_attention_heads,
                 past_key_values_length,
                 self._config.hidden_size // num_encoder_attention_heads,
             )
             snake_case_			= common_inputs['''attention_mask'''].dtype
             snake_case_			= torch.cat(
                 [common_inputs['''attention_mask'''], torch.ones(_UpperCAmelCase						, _UpperCAmelCase						, dtype=_UpperCAmelCase   )]						, dim=1   )
             snake_case_			= [
                 (torch.zeros(_UpperCAmelCase   ), torch.zeros(_UpperCAmelCase   )) for _ in range(_UpperCAmelCase   )
             ]
       return common_inputs



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase						, _UpperCAmelCase = -1						, _UpperCAmelCase = -1						, _UpperCAmelCase = False						, _UpperCAmelCase = None						, ):
       # Copied from OnnxConfig.generate_dummy_inputs
       # Did not use super(OnnxConfigWithPast, self).generate_dummy_inputs for code clarity.
       # If dynamic axis (-1) we forward with a fixed dimension of 2 samples to avoid optimizations made by ONNX
       snake_case_			= compute_effective_axis_dimension(
           _UpperCAmelCase						, fixed_dimension=OnnxConfig.default_fixed_batch						, num_token_to_add=0   )

       # If dynamic axis (-1) we forward with a fixed dimension of 8 tokens to avoid optimizations made by ONNX
       snake_case_			= tokenizer.num_special_tokens_to_add(_UpperCAmelCase   )
       snake_case_			= compute_effective_axis_dimension(
           _UpperCAmelCase						, fixed_dimension=OnnxConfig.default_fixed_sequence						, num_token_to_add=_UpperCAmelCase   )

       # Generate dummy inputs according to compute batch and sequence
       snake_case_			= [''' '''.join([tokenizer.unk_token]   ) * seq_length] * batch_size
       snake_case_			= dict(tokenizer(_UpperCAmelCase						, return_tensors=_UpperCAmelCase   )   )
       return common_inputs



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase						, _UpperCAmelCase = -1						, _UpperCAmelCase = -1						, _UpperCAmelCase = False						, _UpperCAmelCase = None						, ):
       if self.task in ["default", "seq2seq-lm"]:
             snake_case_			= self._generate_dummy_inputs_for_default_and_seqaseq_lm(
                 _UpperCAmelCase						, batch_size=_UpperCAmelCase						, seq_length=_UpperCAmelCase						, is_pair=_UpperCAmelCase						, framework=_UpperCAmelCase   )

       elif self.task == "causal-lm":
             snake_case_			= self._generate_dummy_inputs_for_causal_lm(
                 _UpperCAmelCase						, batch_size=_UpperCAmelCase						, seq_length=_UpperCAmelCase						, is_pair=_UpperCAmelCase						, framework=_UpperCAmelCase   )
       else:
             snake_case_			= self._generate_dummy_inputs_for_sequence_classification_and_question_answering(
                 _UpperCAmelCase						, batch_size=_UpperCAmelCase						, seq_length=_UpperCAmelCase						, is_pair=_UpperCAmelCase						, framework=_UpperCAmelCase   )

       return common_inputs



 def 		UpperCamelCase__	(      self						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase   ):
       if self.task in ["default", "seq2seq-lm"]:
             snake_case_			= super()._flatten_past_key_values_(_UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase   )
       else:
             snake_case_			= super(_UpperCAmelCase						, self   )._flatten_past_key_values_(
                 _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase						, _UpperCAmelCase   )





 def 		UpperCamelCase__	(      self						, _UpperCAmelCase						, _UpperCAmelCase   ):
       if direction not in ["inputs", "outputs"]:
             raise ValueError(F'''direction must either be "inputs" or "outputs", but {direction} was given'''   )

       snake_case_			= '''past_key_values''' if direction == '''inputs''' else '''present'''
       snake_case_			,       snake_case_			= self.num_layers

       snake_case_			= '''past_encoder_sequence'''
       snake_case_			= '''past_decoder_sequence''' if direction == '''inputs''' else '''past_decoder_sequence + sequence'''

       for i in range(_UpperCAmelCase   ):
             snake_case_			= {0: '''batch''', 2: decoder_sequence}
             snake_case_			= {0: '''batch''', 2: decoder_sequence}
             snake_case_			= {0: '''batch''', 2: encoder_sequence}
             snake_case_			= {0: '''batch''', 2: encoder_sequence}