






"""simple docstring"""



import unittest

from transformers import YosoConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor, random_attention_mask
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
 import torch

 from transformers import (
     YosoForMaskedLM,
     YosoForMultipleChoice,
     YosoForQuestionAnswering,
     YosoForSequenceClassification,
     YosoForTokenClassification,
     YosoModel,
 )
 from transformers.models.yoso.modeling_yoso import YOSO_PRETRAINED_MODEL_ARCHIVE_LIST




class  lowerCAmelCase__ :
   def __init__(		self			:  str       , snake_case__			:  int       , snake_case__			:  List[Any]=1_3       , snake_case__			:  List[str]=7       , snake_case__			:  Dict=True       , snake_case__			:  List[Any]=True       , snake_case__			:  Dict=True       , snake_case__			:  int=True       , snake_case__			:  Union[str, Any]=9_9       , snake_case__			:  str=3_2       , snake_case__			:  Optional[int]=5       , snake_case__			:  Optional[Any]=4       , snake_case__			:  Dict=3_7       , snake_case__			:  Optional[Any]="gelu"       , snake_case__			:  List[Any]=0.1       , snake_case__			:  str=0.1       , snake_case__			:  int=5_1_2       , snake_case__			:  Tuple=1_6       , snake_case__			:  Optional[Any]=2       , snake_case__			:  Optional[Any]=0.02       , snake_case__			:  List[Any]=3       , snake_case__			:  List[Any]=4       , snake_case__			:  Tuple=None       , ):


        '''simple docstring'''
        UpperCAmelCase__	:						str       =       parent
        UpperCAmelCase__	:						str       =       batch_size
        UpperCAmelCase__	:						List[str]       =       seq_length
        UpperCAmelCase__	:						List[Any]       =       is_training
        UpperCAmelCase__	:						Any       =       use_input_mask
        UpperCAmelCase__	:						Union[str, Any]       =       use_token_type_ids
        UpperCAmelCase__	:						Tuple       =       use_labels
        UpperCAmelCase__	:						Any       =       vocab_size
        UpperCAmelCase__	:						str       =       hidden_size
        UpperCAmelCase__	:						List[str]       =       num_hidden_layers
        UpperCAmelCase__	:						List[Any]       =       num_attention_heads
        UpperCAmelCase__	:						int       =       intermediate_size
        UpperCAmelCase__	:						str       =       hidden_act
        UpperCAmelCase__	:						List[Any]       =       hidden_dropout_prob
        UpperCAmelCase__	:						str       =       attention_probs_dropout_prob
        UpperCAmelCase__	:						Optional[Any]       =       max_position_embeddings
        UpperCAmelCase__	:						Tuple       =       type_vocab_size
        UpperCAmelCase__	:						Dict       =       type_sequence_label_size
        UpperCAmelCase__	:						int       =       initializer_range
        UpperCAmelCase__	:						List[Any]       =       num_labels
        UpperCAmelCase__	:						int       =       num_choices
        UpperCAmelCase__	:						Dict       =       scope
   def 			__a       (		self			:  Optional[Any]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						List[str]       =       ids_tensor([self.batch_size, self.seq_length]       , self.vocab_size       )

        UpperCAmelCase__	:						Any       =       None
        if self.use_input_mask:
             UpperCAmelCase__	:						List[Any]       =       random_attention_mask([self.batch_size, self.seq_length]       )

        UpperCAmelCase__	:						str       =       None
        if self.use_token_type_ids:
             UpperCAmelCase__	:						int       =       ids_tensor([self.batch_size, self.seq_length]       , self.type_vocab_size       )

        UpperCAmelCase__	:						Any       =       None
        UpperCAmelCase__	:						int       =       None
        UpperCAmelCase__	:						Union[str, Any]       =       None
        if self.use_labels:
             UpperCAmelCase__	:						str       =       ids_tensor([self.batch_size]       , self.type_sequence_label_size       )
             UpperCAmelCase__	:						Tuple       =       ids_tensor([self.batch_size, self.seq_length]       , self.num_labels       )
             UpperCAmelCase__	:						str       =       ids_tensor([self.batch_size]       , self.num_choices       )

        UpperCAmelCase__	:						Union[str, Any]       =       self.get_config()

        return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels
   def 			__a       (		self			:  int       ):


        '''simple docstring'''
        return YosoConfig(
            vocab_size=self.vocab_size       , hidden_size=self.hidden_size       , num_hidden_layers=self.num_hidden_layers       , num_attention_heads=self.num_attention_heads       , intermediate_size=self.intermediate_size       , hidden_act=self.hidden_act       , hidden_dropout_prob=self.hidden_dropout_prob       , attention_probs_dropout_prob=self.attention_probs_dropout_prob       , max_position_embeddings=self.max_position_embeddings       , type_vocab_size=self.type_vocab_size       , is_decoder=snake_case__       , initializer_range=self.initializer_range       , )
   def 			__a       (		self			:  Dict       ):


        '''simple docstring'''
        UpperCAmelCase__	:						List[str]       =       self.get_config()
        UpperCAmelCase__	:						str       =       3_0_0
        return config
   def 			__a       (		self			:  int       ):


        '''simple docstring'''
        (
            (
            UpperCAmelCase__
        )       ,							(
            UpperCAmelCase__
        )       ,							(
            UpperCAmelCase__
        )       ,							(
            UpperCAmelCase__
        )       ,							(
            UpperCAmelCase__
        )       ,							(
            UpperCAmelCase__
        )       ,							(
            UpperCAmelCase__
        )       ,							
        )	:						Union[str, Any]       =       self.prepare_config_and_inputs()

        UpperCAmelCase__	:						List[Any]       =       True
        UpperCAmelCase__	:						Union[str, Any]       =       floats_tensor([self.batch_size, self.seq_length, self.hidden_size]       )
        UpperCAmelCase__	:						Optional[Any]       =       ids_tensor([self.batch_size, self.seq_length]       , vocab_size=2       )

        return (
            config,
            input_ids,
            token_type_ids,
            input_mask,
            sequence_labels,
            token_labels,
            choice_labels,
            encoder_hidden_states,
            encoder_attention_mask,
        )
   def 			__a       (		self			:  Tuple       , snake_case__			:  Dict       , snake_case__			:  List[str]       , snake_case__			:  Optional[int]       , snake_case__			:  str       , snake_case__			:  Tuple       , snake_case__			:  Any       , snake_case__			:  Any       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Tuple       =       YosoModel(config=snake_case__       )
        model.to(snake_case__       )
        model.eval()
        UpperCAmelCase__	:						List[str]       =       model(snake_case__       , attention_mask=snake_case__       , token_type_ids=snake_case__       )
        UpperCAmelCase__	:						Dict       =       model(snake_case__       , token_type_ids=snake_case__       )
        UpperCAmelCase__	:						Optional[int]       =       model(snake_case__       )
        self.parent.assertEqual(result.last_hidden_state.shape       , (self.batch_size, self.seq_length, self.hidden_size)       )
   def 			__a       (		self			:  str       , snake_case__			:  Dict       , snake_case__			:  Union[str, Any]       , snake_case__			:  List[str]       , snake_case__			:  List[Any]       , snake_case__			:  Optional[int]       , snake_case__			:  Dict       , snake_case__			:  str       , snake_case__			:  Union[str, Any]       , snake_case__			:  Union[str, Any]       , ):


        '''simple docstring'''
        UpperCAmelCase__	:						Union[str, Any]       =       True
        UpperCAmelCase__	:						Optional[Any]       =       YosoModel(snake_case__       )
        model.to(snake_case__       )
        model.eval()
        UpperCAmelCase__	:						Optional[int]       =       model(
            snake_case__       , attention_mask=snake_case__       , token_type_ids=snake_case__       , encoder_hidden_states=snake_case__       , encoder_attention_mask=snake_case__       , )
        UpperCAmelCase__	:						Union[str, Any]       =       model(
            snake_case__       , attention_mask=snake_case__       , token_type_ids=snake_case__       , encoder_hidden_states=snake_case__       , )
        UpperCAmelCase__	:						Optional[int]       =       model(snake_case__       , attention_mask=snake_case__       , token_type_ids=snake_case__       )
        self.parent.assertEqual(result.last_hidden_state.shape       , (self.batch_size, self.seq_length, self.hidden_size)       )
   def 			__a       (		self			:  Tuple       , snake_case__			:  Optional[int]       , snake_case__			:  Tuple       , snake_case__			:  List[str]       , snake_case__			:  Dict       , snake_case__			:  str       , snake_case__			:  List[Any]       , snake_case__			:  Optional[Any]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						List[str]       =       YosoForMaskedLM(config=snake_case__       )
        model.to(snake_case__       )
        model.eval()
        UpperCAmelCase__	:						Dict       =       model(snake_case__       , attention_mask=snake_case__       , token_type_ids=snake_case__       , labels=snake_case__       )
        self.parent.assertEqual(result.logits.shape       , (self.batch_size, self.seq_length, self.vocab_size)       )
   def 			__a       (		self			:  str       , snake_case__			:  Optional[Any]       , snake_case__			:  Tuple       , snake_case__			:  Optional[int]       , snake_case__			:  int       , snake_case__			:  Tuple       , snake_case__			:  Optional[Any]       , snake_case__			:  List[str]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Any       =       YosoForQuestionAnswering(config=snake_case__       )
        model.to(snake_case__       )
        model.eval()
        UpperCAmelCase__	:						str       =       model(
            snake_case__       , attention_mask=snake_case__       , token_type_ids=snake_case__       , start_positions=snake_case__       , end_positions=snake_case__       , )
        self.parent.assertEqual(result.start_logits.shape       , (self.batch_size, self.seq_length)       )
        self.parent.assertEqual(result.end_logits.shape       , (self.batch_size, self.seq_length)       )
   def 			__a       (		self			:  str       , snake_case__			:  Tuple       , snake_case__			:  List[str]       , snake_case__			:  List[Any]       , snake_case__			:  Optional[Any]       , snake_case__			:  List[str]       , snake_case__			:  List[Any]       , snake_case__			:  Any       ):


        '''simple docstring'''
        UpperCAmelCase__	:						List[str]       =       self.num_labels
        UpperCAmelCase__	:						Tuple       =       YosoForSequenceClassification(snake_case__       )
        model.to(snake_case__       )
        model.eval()
        UpperCAmelCase__	:						Optional[Any]       =       model(snake_case__       , attention_mask=snake_case__       , token_type_ids=snake_case__       , labels=snake_case__       )
        self.parent.assertEqual(result.logits.shape       , (self.batch_size, self.num_labels)       )
   def 			__a       (		self			:  str       , snake_case__			:  Dict       , snake_case__			:  List[str]       , snake_case__			:  int       , snake_case__			:  List[Any]       , snake_case__			:  Tuple       , snake_case__			:  Optional[int]       , snake_case__			:  Any       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Any       =       self.num_labels
        UpperCAmelCase__	:						Optional[int]       =       YosoForTokenClassification(config=snake_case__       )
        model.to(snake_case__       )
        model.eval()
        UpperCAmelCase__	:						Tuple       =       model(snake_case__       , attention_mask=snake_case__       , token_type_ids=snake_case__       , labels=snake_case__       )
        self.parent.assertEqual(result.logits.shape       , (self.batch_size, self.seq_length, self.num_labels)       )
   def 			__a       (		self			:  List[Any]       , snake_case__			:  Optional[Any]       , snake_case__			:  List[Any]       , snake_case__			:  str       , snake_case__			:  int       , snake_case__			:  Optional[int]       , snake_case__			:  Optional[Any]       , snake_case__			:  List[str]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						List[str]       =       self.num_choices
        UpperCAmelCase__	:						Tuple       =       YosoForMultipleChoice(config=snake_case__       )
        model.to(snake_case__       )
        model.eval()
        UpperCAmelCase__	:						Optional[int]       =       input_ids.unsqueeze(1       ).expand(-1       , self.num_choices       , -1       ).contiguous()
        UpperCAmelCase__	:						Optional[int]       =       token_type_ids.unsqueeze(1       ).expand(-1       , self.num_choices       , -1       ).contiguous()
        UpperCAmelCase__	:						Tuple       =       input_mask.unsqueeze(1       ).expand(-1       , self.num_choices       , -1       ).contiguous()
        UpperCAmelCase__	:						Optional[Any]       =       model(
            snake_case__       , attention_mask=snake_case__       , token_type_ids=snake_case__       , labels=snake_case__       , )
        self.parent.assertEqual(result.logits.shape       , (self.batch_size, self.num_choices)       )




   def 			__a       (		self			:  Tuple       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Union[str, Any]       =       self.prepare_config_and_inputs()
        (
            (
            UpperCAmelCase__
        )       ,							(
            UpperCAmelCase__
        )       ,							(
            UpperCAmelCase__
        )       ,							(
            UpperCAmelCase__
        )       ,							(
            UpperCAmelCase__
        )       ,							(
            UpperCAmelCase__
        )       ,							(
            UpperCAmelCase__
        )       ,							
        )	:						Optional[Any]       =       config_and_inputs
        UpperCAmelCase__	:						Optional[int]       =       {"input_ids": input_ids, "token_type_ids": token_type_ids, "attention_mask": input_mask}
        return config, inputs_dict




@require_torch
class  lowerCAmelCase__ (						__magic_name__       ,	__magic_name__       ,	unittest.TestCase      ):
   SCREAMING_SNAKE_CASE_         =(
       (
           YosoModel,
           YosoForMaskedLM,
           YosoForMultipleChoice,
           YosoForQuestionAnswering,
           YosoForSequenceClassification,
           YosoForTokenClassification,
       )
       if is_torch_available()
       else ()
   )
   SCREAMING_SNAKE_CASE_         =False
   SCREAMING_SNAKE_CASE_         =False
   SCREAMING_SNAKE_CASE_         =False

   SCREAMING_SNAKE_CASE_         =()
   SCREAMING_SNAKE_CASE_         =(
       {
           '''feature-extraction''': YosoModel,
           '''fill-mask''': YosoForMaskedLM,
           '''question-answering''': YosoForQuestionAnswering,
           '''text-classification''': YosoForSequenceClassification,
           '''token-classification''': YosoForTokenClassification,
           '''zero-shot''': YosoForSequenceClassification,
       }
       if is_torch_available()
       else {}
   )
   def 			__a       (		self			:  Optional[Any]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Union[str, Any]       =       YosoModelTester(self       )
        UpperCAmelCase__	:						List[Any]       =       ConfigTester(self       , config_class=snake_case__       , hidden_size=3_7       )
   def 			__a       (		self			:  Tuple       ):


        '''simple docstring'''
        self.config_tester.run_common_tests()
   def 			__a       (		self			:  Dict       ):


        '''simple docstring'''
        UpperCAmelCase__	:						str       =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*snake_case__       )
   def 			__a       (		self			:  List[Any]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						str       =       self.model_tester.prepare_config_and_inputs()
        for type in ["absolute", "relative_key", "relative_key_query"]:
             UpperCAmelCase__	:						Dict       =       type
             self.model_tester.create_and_check_model(*snake_case__       )
   def 			__a       (		self			:  Optional[int]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						str       =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_masked_lm(*snake_case__       )
   def 			__a       (		self			:  str       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Union[str, Any]       =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_multiple_choice(*snake_case__       )
   def 			__a       (		self			:  str       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Tuple       =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_question_answering(*snake_case__       )
   def 			__a       (		self			:  List[str]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Tuple       =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_sequence_classification(*snake_case__       )
   def 			__a       (		self			:  List[Any]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Optional[Any]       =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_for_token_classification(*snake_case__       )
   @slow
   def 			__a       (		self			:  Dict       ):


        '''simple docstring'''
        for model_name in YOSO_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
             UpperCAmelCase__	:						List[Any]       =       YosoModel.from_pretrained(snake_case__       )
             self.assertIsNotNone(snake_case__       )




   def 			__a       (		self			:  Any       ):


        '''simple docstring'''
        return




@require_torch
class  lowerCAmelCase__ (						unittest.TestCase      ):
   @slow
   def 			__a       (		self			:  Any       ):


        '''simple docstring'''
        UpperCAmelCase__	:						List[str]       =       YosoModel.from_pretrained("uw-madison/yoso-4096"       )
        UpperCAmelCase__	:						Union[str, Any]       =       torch.tensor([[0, 1, 2, 3, 4, 5]]       )

        with torch.no_grad():
             UpperCAmelCase__	:						Tuple       =       model(snake_case__       )[0]

        UpperCAmelCase__	:						Any       =       torch.Size((1, 6, 7_6_8)       )
        self.assertEqual(output.shape       , snake_case__       )

        UpperCAmelCase__	:						str       =       torch.tensor(
            [[[-0.0611, 0.1242, 0.0840], [0.0280, -0.0048, 0.1125], [0.0106, 0.0226, 0.0751]]]       )

        self.assertTrue(torch.allclose(output[:, :3, :3]       , snake_case__       , atol=1e-4       )       )
   @slow
   def 			__a       (		self			:  List[str]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Optional[Any]       =       YosoForMaskedLM.from_pretrained("uw-madison/yoso-4096"       )
        UpperCAmelCase__	:						int       =       torch.tensor([[0, 1, 2, 3, 4, 5]]       )

        with torch.no_grad():
             UpperCAmelCase__	:						Optional[int]       =       model(snake_case__       )[0]

        UpperCAmelCase__	:						Tuple       =       5_0_2_6_5

        UpperCAmelCase__	:						Optional[Any]       =       torch.Size((1, 6, vocab_size)       )
        self.assertEqual(output.shape       , snake_case__       )

        UpperCAmelCase__	:						Dict       =       torch.tensor(
            [[[-2.1313, -3.7285, -2.2407], [-2.7047, -3.3314, -2.6408], [0.0629, -2.5166, -0.3356]]]       )

        self.assertTrue(torch.allclose(output[:, :3, :3]       , snake_case__       , atol=1e-4       )       )




   @slow
   def 			__a       (		self			:  int       ):


        '''simple docstring'''
        UpperCAmelCase__	:						str       =       YosoForMaskedLM.from_pretrained("uw-madison/yoso-4096"       )
        UpperCAmelCase__	:						Tuple       =       torch.arange(4_0_9_6       ).unsqueeze(0       )

        with torch.no_grad():
             UpperCAmelCase__	:						Optional[Any]       =       model(snake_case__       )[0]

        UpperCAmelCase__	:						Tuple       =       5_0_2_6_5

        UpperCAmelCase__	:						Union[str, Any]       =       torch.Size((1, 4_0_9_6, vocab_size)       )
        self.assertEqual(output.shape       , snake_case__       )

        UpperCAmelCase__	:						Dict       =       torch.tensor(
            [[[-2.3914, -4.3742, -5.0956], [-4.0988, -4.2384, -7.0406], [-3.1427, -3.7192, -6.6800]]]       )

        self.assertTrue(torch.allclose(output[:, :3, :3]       , snake_case__       , atol=1e-4       )       )










"""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__ :
   def __init__(		self			:  int       , snake_case__			:  Tuple       , snake_case__			:  Any=False       , snake_case__			:  Optional[Any]=1_3       , snake_case__			:  str=(3_2, 1_2_8)       , snake_case__			:  int=4       , snake_case__			:  int=3       , snake_case__			:  int=2_7       , snake_case__			:  Tuple=3_8       , snake_case__			:  Dict=9_9       , snake_case__			:  Union[str, Any]=9_9       , snake_case__			:  Union[str, Any]=3_2       , snake_case__			:  Optional[Any]=5       , snake_case__			:  Optional[Any]=4       , snake_case__			:  str=4.0       , snake_case__			:  List[Any]=2_5_7       , snake_case__			:  int=None       , ):


        '''simple docstring'''
        UpperCAmelCase__	:						Optional[Any]       =       parent
        UpperCAmelCase__	:						Union[str, Any]       =       is_training
        UpperCAmelCase__	:						Tuple       =       batch_size
        UpperCAmelCase__	:						List[Any]       =       image_size
        UpperCAmelCase__	:						str       =       patch_size
        UpperCAmelCase__	:						List[str]       =       num_channels
        UpperCAmelCase__	:						Dict       =       max_token_length
        UpperCAmelCase__	:						List[Any]       =       num_character_labels
        UpperCAmelCase__	:						List[Any]       =       num_bpe_labels
        UpperCAmelCase__	:						List[Any]       =       num_wordpiece_labels
        UpperCAmelCase__	:						List[Any]       =       hidden_size
        UpperCAmelCase__	:						Optional[int]       =       num_hidden_layers
        UpperCAmelCase__	:						Union[str, Any]       =       num_attention_heads
        UpperCAmelCase__	:						Union[str, Any]       =       mlp_ratio
        UpperCAmelCase__	:						int       =       patch_embeds_hidden_size
        UpperCAmelCase__	:						int       =       output_hidden_states
   def 			__a       (		self			:  List[str]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Any       =       floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]       )
        UpperCAmelCase__	:						Optional[int]       =       self.get_config()
        return config, pixel_values
   def 			__a       (		self			:  List[Any]       ):


        '''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 			__a       (		self			:  Any       , snake_case__			:  int       , snake_case__			:  Dict       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Union[str, Any]       =       MgpstrForSceneTextRecognition(snake_case__       )
        model.to(snake_case__       )
        model.eval()
        with torch.no_grad():
             UpperCAmelCase__	:						Any       =       model(snake_case__       )
        self.parent.assertEqual(
            generated_ids[0][0].shape       , (self.batch_size, self.max_token_length, self.num_character_labels)       )




   def 			__a       (		self			:  List[str]       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Optional[Any]       =       self.prepare_config_and_inputs()
        UpperCAmelCase__       ,							UpperCAmelCase__	:						int       =       config_and_inputs
        UpperCAmelCase__	:						List[str]       =       {"pixel_values": pixel_values}
        return config, inputs_dict




@require_torch
class  lowerCAmelCase__ (						__magic_name__       ,	__magic_name__       ,	unittest.TestCase      ):
   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 			__a       (		self			:  str       ):


        '''simple docstring'''
        UpperCAmelCase__	:						int       =       MgpstrModelTester(self       )
        UpperCAmelCase__	:						List[str]       =       ConfigTester(self       , config_class=snake_case__       , has_text_modality=snake_case__       )
   def 			__a       (		self			:  Optional[Any]       ):


        '''simple docstring'''
        self.config_tester.run_common_tests()
   def 			__a       (		self			:  int       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Optional[Any]       =       self.model_tester.prepare_config_and_inputs()
        self.model_tester.create_and_check_model(*snake_case__       )
   @unittest.skip(reason="MgpstrModel does not use inputs_embeds"       )
   def 			__a       (		self			:  str       ):


        '''simple docstring'''
        pass
   def 			__a       (		self			:  Union[str, Any]       ):


        '''simple docstring'''
        UpperCAmelCase__       ,							UpperCAmelCase__	:						Optional[int]       =       self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             UpperCAmelCase__	:						Dict       =       model_class(snake_case__       )
             self.assertIsInstance(model.get_input_embeddings()       , (nn.Module)       )
             UpperCAmelCase__	:						Optional[Any]       =       model.get_output_embeddings()
             self.assertTrue(x is None or isinstance(snake_case__       , nn.Linear       )       )
   def 			__a       (		self			:  List[str]       ):


        '''simple docstring'''
        UpperCAmelCase__       ,							UpperCAmelCase__	:						Any       =       self.model_tester.prepare_config_and_inputs_for_common()

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

             UpperCAmelCase__	:						Optional[Any]       =       ["pixel_values"]
             self.assertListEqual(arg_names[:1]       , snake_case__       )
   @unittest.skip(reason="MgpstrModel does not support feedforward chunking"       )
   def 			__a       (		self			:  List[str]       ):


        '''simple docstring'''
        pass
   def 			__a       (		self			:  Dict       ):


        '''simple docstring'''
        UpperCAmelCase__       ,							UpperCAmelCase__	:						List[str]       =       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

             UpperCAmelCase__	:						Union[str, Any]       =       True
             UpperCAmelCase__	:						Union[str, Any]       =       model_class(snake_case__       )
             self.assertTrue(model.is_gradient_checkpointing       )
   def 			__a       (		self			:  Any       ):


        '''simple docstring'''
        def check_hidden_states_output(snake_case__			:  List[Any]       , snake_case__			:  List[Any]       , snake_case__			:  List[str]       ):
             UpperCAmelCase__	:						int       =       model_class(snake_case__       )
             model.to(snake_case__       )
             model.eval()

             with torch.no_grad():
                  UpperCAmelCase__	:						Tuple       =       model(**self._prepare_for_class(snake_case__       , snake_case__       )       )

             UpperCAmelCase__	:						List[str]       =       outputs.hidden_states

             UpperCAmelCase__	:						Tuple       =       getattr(
                 self.model_tester       , "expected_num_hidden_layers"       , self.model_tester.num_hidden_layers + 1       )
             self.assertEqual(len(snake_case__       )       , snake_case__       )

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

        UpperCAmelCase__       ,							UpperCAmelCase__	:						Dict       =       self.model_tester.prepare_config_and_inputs_for_common()

        for model_class in self.all_model_classes:
             UpperCAmelCase__	:						Tuple       =       True
             check_hidden_states_output(snake_case__       , snake_case__       , snake_case__       )

             # check that output_hidden_states also work using config
             del inputs_dict["output_hidden_states"]
             UpperCAmelCase__	:						Dict       =       True

             check_hidden_states_output(snake_case__       , snake_case__       , snake_case__       )
   def 			__a       (		self			:  Dict       ):


        '''simple docstring'''
        UpperCAmelCase__       ,							UpperCAmelCase__	:						Tuple       =       self.model_tester.prepare_config_and_inputs_for_common()

        UpperCAmelCase__	:						Dict       =       _config_zero_init(snake_case__       )
        for model_class in self.all_model_classes:
             UpperCAmelCase__	:						int       =       model_class(config=snake_case__       )
             for name, param in model.named_parameters():
                  if isinstance(snake_case__       , (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 			__a       (		self			:  Tuple       ):


        '''simple docstring'''
        pass




def   SCREAMING_SNAKE_CASE__					(     )->		List[str]:






     '''simple docstring'''



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




@require_vision
@require_torch
class  lowerCAmelCase__ (						unittest.TestCase      ):
   @slow
   def 			__a       (		self			:  Tuple       ):


        '''simple docstring'''
        UpperCAmelCase__	:						Optional[int]       =       "alibaba-damo/mgp-str-base"
        UpperCAmelCase__	:						Dict       =       MgpstrForSceneTextRecognition.from_pretrained(snake_case__       ).to(snake_case__       )
        UpperCAmelCase__	:						int       =       MgpstrProcessor.from_pretrained(snake_case__       )

        UpperCAmelCase__	:						Dict       =       prepare_img()
        UpperCAmelCase__	:						List[str]       =       processor(images=snake_case__       , return_tensors="pt"       ).pixel_values.to(snake_case__       )

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

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

        UpperCAmelCase__	:						List[str]       =       processor.batch_decode(outputs.logits       )
        UpperCAmelCase__	:						Dict       =       "ticket"

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

        UpperCAmelCase__	:						int       =       torch.tensor(
            [[[-39.5397, -44.4024, -36.1844], [-61.4709, -63.8639, -58.3454], [-74.0225, -68.5494, -71.2164]]]       , device=snake_case__       , )

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


