




'''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      _a    :
       def __init__(      self		:      Any  ,							lowercase		:      Any  ,							lowercase		:      str=False  ,							lowercase		:      Tuple=13  ,							lowercase		:      Any=(32, 128)  ,							lowercase		:      Optional[int]=4  ,							lowercase		:      str=3  ,							lowercase		:      int=27  ,							lowercase		:      Optional[Any]=38  ,							lowercase		:      Union[str, Any]=99  ,							lowercase		:      List[Any]=99  ,							lowercase		:      str=32  ,							lowercase		:      Any=5  ,							lowercase		:      Dict=4  ,							lowercase		:      Optional[Any]=4.0  ,							lowercase		:      str=257  ,							lowercase		:      Union[str, Any]=None  ,							):



           '''simple docstring'''

           UpperCAmelCase     			=						parent
           UpperCAmelCase     			=						is_training
           UpperCAmelCase     			=						batch_size
           UpperCAmelCase     			=						image_size
           UpperCAmelCase     			=						patch_size
           UpperCAmelCase     			=						num_channels
           UpperCAmelCase     			=						max_token_length
           UpperCAmelCase     			=						num_character_labels
           UpperCAmelCase     			=						num_bpe_labels
           UpperCAmelCase     			=						num_wordpiece_labels
           UpperCAmelCase     			=						hidden_size
           UpperCAmelCase     			=						num_hidden_layers
           UpperCAmelCase     			=						num_attention_heads
           UpperCAmelCase     			=						mlp_ratio
           UpperCAmelCase     			=						patch_embeds_hidden_size
           UpperCAmelCase     			=						output_hidden_states
       def        A (      self		:      Union[str, Any]							):



           '''simple docstring'''

           UpperCAmelCase     			=						floats_tensor([self.batch_size, self.num_channels, self.image_size[0], self.image_size[1]]							)
           UpperCAmelCase     			=						self.get_config()
           return config, pixel_values
       def        A (      self		:      List[str]							):



           '''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  ,							lowercase		:      Optional[int]  ,							lowercase		:      List[Any]							):



           '''simple docstring'''

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







       def        A (      self		:      List[Any]							):



           '''simple docstring'''

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






@require_torch
class      _a    (      __a				,				__a				,				unittest.TestCase					):
       __a							:	Optional[Any]  							=   (MgpstrForSceneTextRecognition,) if is_torch_available() else ()
       __a							:	Any  							=   {"""feature-extraction""": MgpstrForSceneTextRecognition} if is_torch_available() else {}
       __a							:	List[str]  							=   False

       __a							:	Union[str, Any]  							=   False
       __a							:	List[str]  							=   False
       __a							:	Optional[Any]  							=   False
       __a							:	List[str]  							=   False
       def        A (      self		:      Dict							):



           '''simple docstring'''

           UpperCAmelCase     			=						MgpstrModelTester(self							)
           UpperCAmelCase     			=						ConfigTester(self  ,							config_class=lowercase  ,							has_text_modality=lowercase							)
       def        A (      self		:      List[str]							):



           '''simple docstring'''

           self.config_tester.run_common_tests()
       def        A (      self		:      int							):



           '''simple docstring'''

           UpperCAmelCase     			=						self.model_tester.prepare_config_and_inputs()
           self.model_tester.create_and_check_model(*lowercase							)
       @unittest.skip(reason='''MgpstrModel does not use inputs_embeds'''							)
       def        A (      self		:      int							):



           '''simple docstring'''

           pass
       def        A (      self		:      Union[str, Any]							):



           '''simple docstring'''

           UpperCAmelCase			,	UpperCAmelCase     			=						self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               UpperCAmelCase     			=						model_class(lowercase							)
               self.assertIsInstance(model.get_input_embeddings()  ,							(nn.Module)							)
               UpperCAmelCase     			=						model.get_output_embeddings()
               self.assertTrue(x is None or isinstance(lowercase  ,							nn.Linear							)							)
       def        A (      self		:      str							):



           '''simple docstring'''

           UpperCAmelCase			,	UpperCAmelCase     			=						self.model_tester.prepare_config_and_inputs_for_common()

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

               UpperCAmelCase     			=						['''pixel_values''']
               self.assertListEqual(arg_names[:1]  ,							lowercase							)
       @unittest.skip(reason='''MgpstrModel does not support feedforward chunking'''							)
       def        A (      self		:      Tuple							):



           '''simple docstring'''

           pass
       def        A (      self		:      int							):



           '''simple docstring'''

           UpperCAmelCase			,	UpperCAmelCase     			=						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     			=						True
               UpperCAmelCase     			=						model_class(lowercase							)
               self.assertTrue(model.is_gradient_checkpointing							)
       def        A (      self		:      List[Any]							):



           '''simple docstring'''

           def check_hidden_states_output(lowercase		:      int  ,							lowercase		:      Tuple  ,							lowercase		:      Optional[Any]							):
               UpperCAmelCase     			=						model_class(lowercase							)
               model.to(lowercase							)
               model.eval()

               with torch.no_grad():
                   UpperCAmelCase     			=						model(**self._prepare_for_class(lowercase  ,							lowercase							)							)

               UpperCAmelCase     			=						outputs.hidden_states

               UpperCAmelCase     			=						getattr(
                   self.model_tester  ,							'''expected_num_hidden_layers'''  ,							self.model_tester.num_hidden_layers + 1							)
               self.assertEqual(len(lowercase							)  ,							lowercase							)

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

           UpperCAmelCase			,	UpperCAmelCase     			=						self.model_tester.prepare_config_and_inputs_for_common()

           for model_class in self.all_model_classes:
               UpperCAmelCase     			=						True
               check_hidden_states_output(lowercase  ,							lowercase  ,							lowercase							)

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

               check_hidden_states_output(lowercase  ,							lowercase  ,							lowercase							)
       def        A (      self		:      Optional[int]							):



           '''simple docstring'''

           UpperCAmelCase			,	UpperCAmelCase     			=						self.model_tester.prepare_config_and_inputs_for_common()

           UpperCAmelCase     			=						_config_zero_init(lowercase							)
           for model_class in self.all_model_classes:
               UpperCAmelCase     			=						model_class(config=lowercase							)
               for name, param in model.named_parameters():
                   if isinstance(lowercase  ,							(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		:      Union[str, Any]							):



           '''simple docstring'''

           pass






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







@require_vision
@require_torch
class      _a    (      unittest.TestCase					):
       @slow
       def        A (      self		:      Union[str, Any]							):



           '''simple docstring'''

           UpperCAmelCase     			=						'''alibaba-damo/mgp-str-base'''
           UpperCAmelCase     			=						MgpstrForSceneTextRecognition.from_pretrained(lowercase							).to(lowercase							)
           UpperCAmelCase     			=						MgpstrProcessor.from_pretrained(lowercase							)

           UpperCAmelCase     			=						prepare_img()
           UpperCAmelCase     			=						processor(images=lowercase  ,							return_tensors='''pt'''							).pixel_values.to(lowercase							)

           # forward pass
           with torch.no_grad():
               UpperCAmelCase     			=						model(lowercase							)

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

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

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

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

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










'''simple docstring'''







import argparse
import logging
import sys
import time

import tensorflow as tf
from datasets import load_dataset

from transformers import AutoTokenizer, TFAutoModelForSequenceClassification


if __name__ == "__main__":
   A					       =argparse.ArgumentParser()

   # Hyperparameters sent by the client are passed as command-line arguments to the script.
   parser.add_argument('--epochs', type=int, default=1)
   parser.add_argument('--per_device_train_batch_size', type=int, default=16)
   parser.add_argument('--per_device_eval_batch_size', type=int, default=8)
   parser.add_argument('--model_name_or_path', type=str)
   parser.add_argument('--learning_rate', type=str, default=5E-5)
   parser.add_argument('--do_train', type=bool, default=True)
   parser.add_argument('--do_eval', type=bool, default=True)
   parser.add_argument('--output_dir', type=str)

   A       , A					       =parser.parse_known_args()

   # overwrite batch size until we have tf_glue.py
   A					       =16
   A					       =16

   # Set up logging
   A					       =logging.getLogger(__name__)

   logging.basicConfig(
       level=logging.getLevelName('INFO'),
       handlers=[logging.StreamHandler(sys.stdout)],
       format='%(asctime)s - %(name)s - %(levelname)s - %(message)s',
   )

   # Load model and tokenizer
   A					       =TFAutoModelForSequenceClassification.from_pretrained(args.model_name_or_path)
   A					       =AutoTokenizer.from_pretrained(args.model_name_or_path)

   # Load dataset
   A       , A					       =load_dataset('imdb', split=['train', 'test'])
   A					       =train_dataset.shuffle().select(range(50_00))  # smaller the size for train dataset to 5k
   A					       =test_dataset.shuffle().select(range(5_00))  # smaller the size for test dataset to 500

   # Preprocess train dataset
   A					       =train_dataset.map(
       lambda e: tokenizer(e['text'], truncation=True, padding='max_length'), batched=True
   )
   train_dataset.set_format(type='tensorflow', columns=['input_ids', 'attention_mask', 'label'])

   A					       ={
       x: train_dataset[x].to_tensor(default_value=0, shape=[None, tokenizer.model_max_length])
       for x in ['input_ids', 'attention_mask']
   }
   A					       =tf.data.Dataset.from_tensor_slices((train_features, train_dataset['label'])).batch(
       args.per_device_train_batch_size
   )

   # Preprocess test dataset
   A					       =test_dataset.map(
       lambda e: tokenizer(e['text'], truncation=True, padding='max_length'), batched=True
   )
   test_dataset.set_format(type='tensorflow', columns=['input_ids', 'attention_mask', 'label'])

   A					       ={
       x: test_dataset[x].to_tensor(default_value=0, shape=[None, tokenizer.model_max_length])
       for x in ['input_ids', 'attention_mask']
   }
   A					       =tf.data.Dataset.from_tensor_slices((test_features, test_dataset['label'])).batch(
       args.per_device_eval_batch_size
   )

   # fine optimizer and loss
   A					       =tf.keras.optimizers.Adam(learning_rate=args.learning_rate)
   A					       =tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True)
   A					       =[tf.keras.metrics.SparseCategoricalAccuracy()]
   model.compile(optimizer=optimizer, loss=loss, metrics=metrics)

   A					       =time.time()
   A					       =model.fit(tf_train_dataset, epochs=args.epochs, batch_size=args.per_device_train_batch_size)
   A					       =time.time() - start_train_time

   logger.info('*** Train ***')
   logger.info(f"""train_runtime = {end_train_time}""")
   for key, value in train_results.history.items():
      logger.info(f"""  {key} = {value}""")




