






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       ,   _UpperCAmelCase       ,   _UpperCAmelCase=False       ,   _UpperCAmelCase=13       ,   _UpperCAmelCase=(32, 128)       ,   _UpperCAmelCase=4       ,   _UpperCAmelCase=3       ,   _UpperCAmelCase=27       ,   _UpperCAmelCase=38       ,   _UpperCAmelCase=99       ,   _UpperCAmelCase=99       ,   _UpperCAmelCase=32       ,   _UpperCAmelCase=5       ,   _UpperCAmelCase=4       ,   _UpperCAmelCase=4.0       ,   _UpperCAmelCase=257       ,   _UpperCAmelCase=None       ,   )		->							List[Any]:
        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     )		->							Any:
        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     )		->							Dict:
        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       ,   _UpperCAmelCase       ,   _UpperCAmelCase     )		->							Tuple:
        lowerCamelCase											=MgpstrForSceneTextRecognition(_UpperCAmelCase     )
        model.to(_UpperCAmelCase     )
        model.eval()
        with torch.no_grad():
           lowerCamelCase											=model(_UpperCAmelCase     )
        self.parent.assertEqual(
            generated_ids[0][0].shape       ,   (self.batch_size, self.max_token_length, self.num_character_labels)     )





     def 	lowerCamelCase       (  self     )		->							Optional[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_							(     UpperCamelCase							,    UpperCamelCase							,    unittest.TestCase   ):
     __lowerCamelCase					:						List[str]     =    (MgpstrForSceneTextRecognition,) if is_torch_available() else ()
     __lowerCamelCase					:						List[Any]     =    {'''feature-extraction''': MgpstrForSceneTextRecognition} if is_torch_available() else {}
     __lowerCamelCase					:						List[str]     =    False

     __lowerCamelCase					:						str     =    False
     __lowerCamelCase					:						Optional[int]     =    False
     __lowerCamelCase					:						Dict     =    False
     __lowerCamelCase					:						List[str]     =    False





     def 	lowerCamelCase       (  self     )		->							Optional[int]:
        lowerCamelCase											=MgpstrModelTester(self     )
        lowerCamelCase											=ConfigTester(self       ,   config_class=_UpperCAmelCase       ,   has_text_modality=_UpperCAmelCase     )





     def 	lowerCamelCase       (  self     )		->							List[Any]:
        self.config_tester.run_common_tests()





     def 	lowerCamelCase       (  self     )		->							Optional[int]:
        lowerCamelCase											=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 	lowerCamelCase       (  self     )		->							str:
        pass





     def 	lowerCamelCase       (  self     )		->							int:
        lowerCamelCase							,					lowerCamelCase											=self.model_tester.prepare_config_and_inputs_for_common()

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





     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(_UpperCAmelCase     )
           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]       ,   _UpperCAmelCase     )





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





     def 	lowerCamelCase       (  self     )		->							int:
        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(_UpperCAmelCase     )
           self.assertTrue(model.is_gradient_checkpointing     )





     def 	lowerCamelCase       (  self     )		->							Tuple:
        def check_hidden_states_output(_UpperCAmelCase       ,   _UpperCAmelCase       ,   _UpperCAmelCase     ):
           lowerCamelCase											=model_class(_UpperCAmelCase     )
           model.to(_UpperCAmelCase     )
           model.eval()

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

           lowerCamelCase											=outputs.hidden_states

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

        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(_UpperCAmelCase       ,   _UpperCAmelCase       ,   _UpperCAmelCase     )

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

           check_hidden_states_output(_UpperCAmelCase       ,   _UpperCAmelCase       ,   _UpperCAmelCase     )





     def 	lowerCamelCase       (  self     )		->							Any:
        lowerCamelCase							,					lowerCamelCase											=self.model_tester.prepare_config_and_inputs_for_common()

        lowerCamelCase											=_config_zero_init(_UpperCAmelCase     )
        for model_class in self.all_model_classes:
           lowerCamelCase											=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 	lowerCamelCase       (  self     )		->							Tuple:
        pass






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







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





     @slow
     def 	lowerCamelCase       (  self     )		->							List[Any]:
        lowerCamelCase											="""alibaba-damo/mgp-str-base"""
        lowerCamelCase											=MgpstrForSceneTextRecognition.from_pretrained(_UpperCAmelCase     ).to(_UpperCAmelCase     )
        lowerCamelCase											=MgpstrProcessor.from_pretrained(_UpperCAmelCase     )

        lowerCamelCase											=prepare_img()
        lowerCamelCase											=processor(images=_UpperCAmelCase       ,   return_tensors="""pt"""     ).pixel_values.to(_UpperCAmelCase     )

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

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

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

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







import unittest

from transformers import ErnieMTokenizer
from transformers.testing_utils import get_tests_dir, require_sentencepiece, require_tokenizers, slow

from ...test_tokenization_common import TokenizerTesterMixin


_UpperCAmelCase				:	Any    						=    get_tests_dir('fixtures/spiece.model')


@require_sentencepiece
@require_tokenizers
class 		lowerCAmelCase_							(     UpperCamelCase							,    unittest.TestCase   ):
     __lowerCamelCase					:						List[Any]     =    ErnieMTokenizer
     __lowerCamelCase					:						Optional[Any]     =    False
     __lowerCamelCase					:						Optional[int]     =    True
     __lowerCamelCase					:						str     =    False
     __lowerCamelCase					:						Any     =    False





     def 	lowerCamelCase       (  self     )		->							Any:
        super().setUp()

        # We have a SentencePiece fixture for testing
        lowerCamelCase											=ErnieMTokenizer(_UpperCAmelCase       ,   unk_token="""<unk>"""       ,   pad_token="""<pad>"""     )
        tokenizer.save_pretrained(self.tmpdirname     )





     def 	lowerCamelCase       (  self       ,   _UpperCAmelCase     )		->							Union[str, Any]:
        lowerCamelCase											="""this is a test"""
        lowerCamelCase											="""this is a test"""
        return input_text, output_text





     def 	lowerCamelCase       (  self     )		->							List[Any]:
        lowerCamelCase											="""<pad>"""
        lowerCamelCase											=0

        self.assertEqual(self.get_tokenizer()._convert_token_to_id(_UpperCAmelCase     )       ,   _UpperCAmelCase     )
        self.assertEqual(self.get_tokenizer()._convert_id_to_token(_UpperCAmelCase     )       ,   _UpperCAmelCase     )





     def 	lowerCamelCase       (  self     )		->							List[str]:
        lowerCamelCase											=list(self.get_tokenizer().get_vocab().keys()     )

        self.assertEqual(vocab_keys[0]       ,   """<pad>"""     )
        self.assertEqual(vocab_keys[1]       ,   """<unk>"""     )
        self.assertEqual(vocab_keys[-1]       ,   """▁eloquent"""     )
        self.assertEqual(len(_UpperCAmelCase     )       ,   30000     )





     def 	lowerCamelCase       (  self     )		->							Any:
        self.assertEqual(self.get_tokenizer().vocab_size       ,   30000     )





     def 	lowerCamelCase       (  self     )		->							Any:
        if not self.test_rust_tokenizer:
           return

        lowerCamelCase											=self.get_tokenizer()
        lowerCamelCase											=self.get_rust_tokenizer()

        lowerCamelCase											="""I was born in 92000, and this is falsé."""

        lowerCamelCase											=tokenizer.tokenize(_UpperCAmelCase     )
        lowerCamelCase											=rust_tokenizer.tokenize(_UpperCAmelCase     )
        self.assertListEqual(_UpperCAmelCase       ,   _UpperCAmelCase     )

        lowerCamelCase											=tokenizer.encode(_UpperCAmelCase       ,   add_special_tokens=_UpperCAmelCase     )
        lowerCamelCase											=rust_tokenizer.encode(_UpperCAmelCase       ,   add_special_tokens=_UpperCAmelCase     )
        self.assertListEqual(_UpperCAmelCase       ,   _UpperCAmelCase     )

        lowerCamelCase											=self.get_rust_tokenizer()
        lowerCamelCase											=tokenizer.encode(_UpperCAmelCase     )
        lowerCamelCase											=rust_tokenizer.encode(_UpperCAmelCase     )
        self.assertListEqual(_UpperCAmelCase       ,   _UpperCAmelCase     )





     def 	lowerCamelCase       (  self     )		->							Union[str, Any]:
        lowerCamelCase											=ErnieMTokenizer(_UpperCAmelCase       ,   do_lower_case=_UpperCAmelCase       ,   unk_token="""<unk>"""       ,   pad_token="""<pad>"""     )

        lowerCamelCase											=tokenizer.tokenize("""This is a test"""     )
        self.assertListEqual(_UpperCAmelCase       ,   ["""▁this""", """▁is""", """▁a""", """▁test"""]     )

        self.assertListEqual(tokenizer.convert_tokens_to_ids(_UpperCAmelCase     )       ,   [48, 25, 21, 1289]     )

        lowerCamelCase											=tokenizer.tokenize("""I was born in 92000, and this is falsé."""     )
        # ErnieMTokenizer(paddlenlp implementation) outputs '9' instead of '_9' so to mimic that '_9' is changed to '9'
        self.assertListEqual(
            _UpperCAmelCase       ,   ["""▁i""", """▁was""", """▁born""", """▁in""", """9""", """2000""", """,""", """▁and""", """▁this""", """▁is""", """▁fal""", """s""", """é""", """."""]     )
        lowerCamelCase											=tokenizer.convert_tokens_to_ids(_UpperCAmelCase     )
        self.assertListEqual(_UpperCAmelCase       ,   [31, 23, 386, 19, 518, 3050, 15, 17, 48, 25, 8256, 18, 1, 9]     )

        lowerCamelCase											=tokenizer.convert_ids_to_tokens(_UpperCAmelCase     )
        self.assertListEqual(
            _UpperCAmelCase       ,   ["""▁i""", """▁was""", """▁born""", """▁in""", """9""", """2000""", """,""", """▁and""", """▁this""", """▁is""", """▁fal""", """s""", """<unk>""", """."""]       ,   )





     def 	lowerCamelCase       (  self     )		->							List[str]:
        lowerCamelCase											=ErnieMTokenizer(_UpperCAmelCase       ,   unk_token="""<unk>"""       ,   pad_token="""<pad>"""     )

        lowerCamelCase											=tokenizer.encode("""sequence builders"""     )
        lowerCamelCase											=tokenizer.encode("""multi-sequence build"""     )

        lowerCamelCase											=tokenizer.build_inputs_with_special_tokens(_UpperCAmelCase     )
        lowerCamelCase											=tokenizer.build_inputs_with_special_tokens(_UpperCAmelCase       ,   _UpperCAmelCase     )

        assert encoded_sentence == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id]
        assert encoded_pair == [tokenizer.cls_token_id] + text + [tokenizer.sep_token_id] + [
            tokenizer.sep_token_id
        ] + text_a + [tokenizer.sep_token_id]





     @slow
     def 	lowerCamelCase       (  self     )		->							Dict:
        # fmt: off
        lowerCamelCase											={"""input_ids""": [[0, 11062, 82772, 7, 15, 82772, 538, 51529, 237, 17198, 1290, 206, 9, 215175, 1314, 136, 17198, 1290, 206, 9, 56359, 42, 122009, 9, 16466, 16, 87344, 4537, 9, 4717, 78381, 6, 159958, 7, 15, 24480, 618, 4, 527, 22693, 9, 304, 4, 2777, 24480, 9874, 4, 43523, 594, 4, 803, 18392, 33189, 18, 4, 43523, 24447, 5, 5, 5, 16, 100, 24955, 83658, 9626, 144057, 15, 839, 22335, 16, 136, 24955, 83658, 83479, 15, 39102, 724, 16, 678, 645, 6460, 1328, 4589, 42, 122009, 115774, 23, 3559, 1328, 46876, 7, 136, 53894, 1940, 42227, 41159, 17721, 823, 425, 4, 27512, 98722, 206, 136, 5531, 4970, 919, 17336, 5, 2], [0, 20080, 618, 83, 82775, 47, 479, 9, 1517, 73, 53894, 333, 80581, 110117, 18811, 5256, 1295, 51, 152526, 297, 7986, 390, 124416, 538, 35431, 214, 98, 15044, 25737, 136, 7108, 43701, 23, 756, 135355, 7, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1], [0, 581, 63773, 119455, 6, 147797, 88203, 7, 645, 70, 21, 3285, 10269, 5, 2, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1]]}
        # fmt: on

        self.tokenizer_integration_test_util(
            expected_encoding=_UpperCAmelCase       ,   model_name="""susnato/ernie-m-base_pytorch"""       ,   sequences=[
                """Transformers (formerly known as pytorch-transformers and pytorch-pretrained-bert) provides """
                """general-purpose architectures (BERT, GPT-2, RoBERTa, XLM, DistilBert, XLNet...) for Natural """
                """Language Understanding (NLU) and Natural Language Generation (NLG) with over32+ pretrained """
                """models in100+ languages and deep interoperability between Jax, PyTorch and TensorFlow.""",
                """BERT is designed to pre-train deep bidirectional representations from unlabeled text by jointly """
                """conditioning on both left and right context in all layers.""",
                """The quick brown fox jumps over the lazy dog.""",
            ]       ,   )