
"""simple docstring"""






import logging
import os

import quant_trainer
import torch
from torch.utils.data import DataLoader

from transformers import Trainer, is_torch_tpu_available
from transformers.trainer_utils import PredictionOutput


__SCREAMING_SNAKE_CASE							=     logging.getLogger(__name__)

if is_torch_tpu_available(check_device=False):
 import torch_xla.core.xla_model as xm
 import torch_xla.debug.metrics as met





class 				a__					(						__snake_case       ):



     def __init__(							self :List[Any]      ,  *_lowerCamelCase :List[str]      ,  _lowerCamelCase :Tuple=None      ,  _lowerCamelCase :List[Any]=None      ,  _lowerCamelCase :Dict=None      ,  **_lowerCamelCase :Optional[Any]   ):







      '''simple docstring'''



      super().__init__(*__lowerCamelCase      ,  **__lowerCamelCase   )
      UpperCamelCase_     :					str   	=eval_examples
      UpperCamelCase_     :					Tuple   	=post_process_function
      UpperCamelCase_     :					Tuple   	=quant_trainer_args
      UpperCamelCase_     :					List[str]   	=128  # default number of calibration samples



     def  lowerCamelCase_						(							self :Union[str, Any]      ,  _lowerCamelCase :int=None   ):







      '''simple docstring'''



      if calib_dataset is None and self.calib_dataset is None:
       raise ValueError('Trainer: calibration requires an calib_dataset.'   )
      UpperCamelCase_     :					List[Any]   	=calib_dataset if calib_dataset is not None else self.calib_dataset

      UpperCamelCase_     :					Dict   	=self._remove_unused_columns(__lowerCamelCase      ,  description='Calibration'   )

      return DataLoader(
          __lowerCamelCase      ,  batch_size=self.args.eval_batch_size      ,  collate_fn=self.data_collator      ,  drop_last=self.args.dataloader_drop_last      ,  num_workers=self.args.dataloader_num_workers      ,  pin_memory=self.args.dataloader_pin_memory      ,  shuffle=__lowerCamelCase      ,  )



     def  lowerCamelCase_						(							self :str      ,  _lowerCamelCase :Optional[int]=None   ):







      '''simple docstring'''



      UpperCamelCase_     :					Any   	=self.train_dataset if calib_dataset is None else calib_dataset
      UpperCamelCase_     :					Any   	=self.get_calib_dataloader(__lowerCamelCase   )

      UpperCamelCase_     :					Dict   	=self.model
      quant_trainer.configure_model(__lowerCamelCase      ,  self.quant_trainer_args      ,  calib=__lowerCamelCase   )
      model.eval()
      quant_trainer.enable_calibration(__lowerCamelCase   )

      logger.info('***** Running calibration *****'   )
      logger.info(f'''  Num examples = {self.calib_num}'''   )
      logger.info(f'''  Batch size = {calib_dataloader.batch_size}'''   )

      for step, inputs in enumerate(__lowerCamelCase   ):
       # Prediction step
       UpperCamelCase_	,     UpperCamelCase_	,     UpperCamelCase_     :					List[Any]   	=self.prediction_step(__lowerCamelCase      ,  __lowerCamelCase      ,  prediction_loss_only=__lowerCamelCase   )
       if (step + 1) * calib_dataloader.batch_size >= self.calib_num:
        break

      quant_trainer.finish_calibration(__lowerCamelCase      ,  self.quant_trainer_args   )
      UpperCamelCase_     :					int   	=model



     def  lowerCamelCase_						(							self :Union[str, Any]      ,  _lowerCamelCase :str=None      ,  _lowerCamelCase :Optional[int]=None      ,  _lowerCamelCase :int=None      ,  _lowerCamelCase :str = "eval"   ):







      '''simple docstring'''



      UpperCamelCase_     :					Tuple   	=self.eval_dataset if eval_dataset is None else eval_dataset
      UpperCamelCase_     :					int   	=self.get_eval_dataloader(__lowerCamelCase   )
      UpperCamelCase_     :					int   	=self.eval_examples if eval_examples is None else eval_examples

      # Temporarily disable metric computation, we will do it in the loop here.
      UpperCamelCase_     :					List[str]   	=self.compute_metrics
      UpperCamelCase_     :					List[str]   	=None
      UpperCamelCase_     :					Optional[int]   	=self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
      try:
       UpperCamelCase_     :					Optional[Any]   	=eval_loop(
           __lowerCamelCase      ,  description='Evaluation'      ,  prediction_loss_only=True if compute_metrics is None else None      ,  ignore_keys=__lowerCamelCase      ,  )
      finally:
       UpperCamelCase_     :					Union[str, Any]   	=compute_metrics

      if self.post_process_function is not None and self.compute_metrics is not None:
       UpperCamelCase_     :					Dict   	=self.post_process_function(__lowerCamelCase      ,  __lowerCamelCase      ,  output.predictions   )
       UpperCamelCase_     :					Any   	=self.compute_metrics(__lowerCamelCase   )

       # Prefix all keys with metric_key_prefix + '_'
       for key in list(metrics.keys()   ):
        if not key.startswith(f'''{metric_key_prefix}_'''   ):
         UpperCamelCase_     :					Optional[Any]   	=metrics.pop(__lowerCamelCase   )

       self.log(__lowerCamelCase   )
      else:
       UpperCamelCase_     :					Optional[Any]   	={}

      if self.args.tpu_metrics_debug or self.args.debug:
       # tpu-comment: Logging debug metrics for PyTorch/XLA (compile, execute times, ops, etc.)
       xm.master_print(met.metrics_report()   )

      UpperCamelCase_     :					Union[str, Any]   	=self.callback_handler.on_evaluate(self.args      ,  self.state      ,  self.control      ,  __lowerCamelCase   )
      return metrics



     def  lowerCamelCase_						(							self :Optional[Any]      ,  _lowerCamelCase :Optional[Any]      ,  _lowerCamelCase :Union[str, Any]      ,  _lowerCamelCase :List[str]=None      ,  _lowerCamelCase :str = "test"   ):







      '''simple docstring'''



      UpperCamelCase_     :					int   	=self.get_test_dataloader(__lowerCamelCase   )

      # Temporarily disable metric computation, we will do it in the loop here.
      UpperCamelCase_     :					Optional[Any]   	=self.compute_metrics
      UpperCamelCase_     :					Optional[Any]   	=None
      UpperCamelCase_     :					str   	=self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
      try:
       UpperCamelCase_     :					str   	=eval_loop(
           __lowerCamelCase      ,  description='Prediction'      ,  prediction_loss_only=True if compute_metrics is None else None      ,  ignore_keys=__lowerCamelCase      ,  )
      finally:
       UpperCamelCase_     :					str   	=compute_metrics

      if self.post_process_function is None or self.compute_metrics is None:
       return output

      UpperCamelCase_     :					Dict   	=self.post_process_function(__lowerCamelCase      ,  __lowerCamelCase      ,  output.predictions      ,  'predict'   )
      UpperCamelCase_     :					List[str]   	=self.compute_metrics(__lowerCamelCase   )

      # Prefix all keys with metric_key_prefix + '_'
      for key in list(metrics.keys()   ):
       if not key.startswith(f'''{metric_key_prefix}_'''   ):
        UpperCamelCase_     :					Dict   	=metrics.pop(__lowerCamelCase   )

      return PredictionOutput(predictions=predictions.predictions      ,  label_ids=predictions.label_ids      ,  metrics=__lowerCamelCase   )




     def  lowerCamelCase_						(							self :List[str]      ,  _lowerCamelCase :Tuple="./"   ):







      '''simple docstring'''



      UpperCamelCase_     :					int   	=self.eval_dataset
      UpperCamelCase_     :					str   	=self.get_eval_dataloader(__lowerCamelCase   )

      UpperCamelCase_     :					List[str]   	=next(iter(__lowerCamelCase   )   )

      # saving device - to make it consistent
      UpperCamelCase_     :					Any   	=torch.device('cuda' if torch.cuda.is_available() else 'cpu'   )

      # convert to tuple
      UpperCamelCase_     :					Optional[int]   	=tuple(v.to(__lowerCamelCase   ) for k, v in batch.items()   )

      logger.info('Converting model to be onnx compatible'   )
      from pytorch_quantization.nn import TensorQuantizer

      UpperCamelCase_     :					Optional[int]   	=True

      UpperCamelCase_     :					Any   	=self.model.to(__lowerCamelCase   )

      model.eval()
      model.float()

      UpperCamelCase_     :					int   	=model.module if hasattr(__lowerCamelCase      ,  'module'   ) else model
      quant_trainer.configure_model(__lowerCamelCase      ,  self.quant_trainer_args   )

      UpperCamelCase_     :					List[str]   	=os.path.join(__lowerCamelCase      ,  'model.onnx'   )
      logger.info(f'''exporting model to {output_model_file}'''   )

      UpperCamelCase_     :					Tuple   	={0: 'batch_size', 1: 'seq_len'}

      torch.onnx.export(
          __lowerCamelCase      ,  __lowerCamelCase      ,  __lowerCamelCase      ,  export_params=__lowerCamelCase      ,  opset_version=13      ,  do_constant_folding=__lowerCamelCase      ,  input_names=['input_ids', 'attention_mask', 'token_type_ids']      ,  output_names=['output_start_logits', 'output_end_logits']      ,  dynamic_axes={
              'input_ids': axes,
              'attention_mask': axes,
              'token_type_ids': axes,
              'output_start_logits': axes,
              'output_end_logits': axes,
          }      ,  verbose=__lowerCamelCase      ,  )
      logger.info('onnx export finished'   )




import copy
import os
from typing import Union

from ...configuration_utils import PretrainedConfig
from ...utils import logging


__a	 =      logging.get_logger(__name__)

__a	 =      {
    """google/pix2struct-textcaps-base""": (
        """https://huggingface.co/google/pix2struct-textcaps-base/resolve/main/config.json"""
    ),
}






class 	__lowercase					(	__snake_case	):
      UpperCamelCase										=							'''pix2struct_text_model'''
      UpperCamelCase										=							['''past_key_values''']
      UpperCamelCase										=							{
          '''hidden_size''': '''hidden_size''',
          '''num_attention_heads''': '''num_heads''',
          '''num_hidden_layers''': '''num_layers''',
      }





      def __init__(		self    :       Optional[Any]       ,							__lowerCamelCase    :       Optional[int]=5_0_2_4_4       ,							__lowerCamelCase    :       int=7_6_8       ,							__lowerCamelCase    :       Union[str, Any]=6_4       ,							__lowerCamelCase    :       Dict=2_0_4_8       ,							__lowerCamelCase    :       int=1_2       ,							__lowerCamelCase    :       Any=1_2       ,							__lowerCamelCase    :       Union[str, Any]=3_2       ,							__lowerCamelCase    :       Any=1_2_8       ,							__lowerCamelCase    :       Union[str, Any]=0.1       ,							__lowerCamelCase    :       str=1e-6       ,							__lowerCamelCase    :       int=1.0       ,							__lowerCamelCase    :       Optional[int]="gelu_new"       ,							__lowerCamelCase    :       int=0       ,							__lowerCamelCase    :       int=False       ,							__lowerCamelCase    :       Optional[Any]=0       ,							__lowerCamelCase    :       Dict=1       ,							__lowerCamelCase    :       List[Any]=False       ,							__lowerCamelCase    :       Optional[Any]=True       ,							**__lowerCamelCase    :       Dict       ,							)     -> int:






         """simple docstring"""







         UpperCAmelCase      					=  vocab_size
         UpperCAmelCase      					=  hidden_size
         UpperCAmelCase      					=  d_kv
         UpperCAmelCase      					=  d_ff
         UpperCAmelCase      					=  num_layers
         UpperCAmelCase      					=  num_heads
         UpperCAmelCase      					=  relative_attention_num_buckets
         UpperCAmelCase      					=  relative_attention_max_distance
         UpperCAmelCase      					=  dropout_rate
         UpperCAmelCase      					=  layer_norm_epsilon
         UpperCAmelCase      					=  initializer_factor
         UpperCAmelCase      					=  use_cache

         UpperCAmelCase      					=  eos_token_id
         UpperCAmelCase      					=  decoder_start_token_id

         # for backwards compatibility
         UpperCAmelCase      					=  dense_act_fn

         super().__init__(
             pad_token_id=__lowerCamelCase       ,							eos_token_id=__lowerCamelCase       ,							decoder_start_token_id=__lowerCamelCase       ,							tie_word_embeddings=__lowerCamelCase       ,							is_decoder=__lowerCamelCase       ,							**__lowerCamelCase       ,							)





      @classmethod
      def 							_lowercase					(		cls    :       List[Any]       ,							__lowerCamelCase    :       Union[str, os.PathLike]       ,							**__lowerCamelCase    :       Any							)     -> "PretrainedConfig":






         """simple docstring"""







         cls._set_token_in_kwargs(__lowerCamelCase							)

         UpperCAmelCase					,	UpperCAmelCase      					=  cls.get_config_dict(__lowerCamelCase       ,							**__lowerCamelCase							)

         # get the text config dict if we are loading from Pix2StructConfig
         if config_dict.get("""model_type"""							) == "pix2struct":
            UpperCAmelCase      					=  config_dict["""text_config"""]

         if "model_type" in config_dict and hasattr(cls       ,							"""model_type"""							) and config_dict["model_type"] != cls.model_type:
            logger.warning(
                F"""You are using a model of type {config_dict['model_type']} to instantiate a model of type """
                F"""{cls.model_type}. This is not supported for all configurations of models and can yield errors."""							)

         return cls.from_dict(__lowerCamelCase       ,							**__lowerCamelCase							)






class 	__lowercase					(	__snake_case	):
      UpperCamelCase										=							'''pix2struct_vision_model'''





      def __init__(		self    :       str       ,							__lowerCamelCase    :       Any=7_6_8       ,							__lowerCamelCase    :       Dict=7_6_8       ,							__lowerCamelCase    :       List[Any]=2_0_4_8       ,							__lowerCamelCase    :       List[Any]=6_4       ,							__lowerCamelCase    :       Union[str, Any]=1_2       ,							__lowerCamelCase    :       Optional[int]=1_2       ,							__lowerCamelCase    :       Tuple="gelu_new"       ,							__lowerCamelCase    :       Tuple=1e-6       ,							__lowerCamelCase    :       Any=0.0       ,							__lowerCamelCase    :       List[Any]=0.0       ,							__lowerCamelCase    :       int=1e-1_0       ,							__lowerCamelCase    :       Union[str, Any]=1.0       ,							__lowerCamelCase    :       Any=4_0_9_6       ,							__lowerCamelCase    :       Dict=3_2       ,							__lowerCamelCase    :       Any=1_2_8       ,							**__lowerCamelCase    :       Optional[int]       ,							)     -> Optional[Any]:






         """simple docstring"""







         super().__init__(**__lowerCamelCase							)

         UpperCAmelCase      					=  hidden_size
         UpperCAmelCase      					=  patch_embed_hidden_size
         UpperCAmelCase      					=  d_ff
         UpperCAmelCase      					=  dropout_rate
         UpperCAmelCase      					=  num_hidden_layers
         UpperCAmelCase      					=  num_attention_heads
         UpperCAmelCase      					=  initializer_range
         UpperCAmelCase      					=  initializer_factor
         UpperCAmelCase      					=  attention_dropout
         UpperCAmelCase      					=  layer_norm_eps
         UpperCAmelCase      					=  dense_act_fn
         UpperCAmelCase      					=  seq_len
         UpperCAmelCase      					=  relative_attention_num_buckets
         UpperCAmelCase      					=  relative_attention_max_distance
         UpperCAmelCase      					=  d_kv





      @classmethod
      def 							_lowercase					(		cls    :       Any       ,							__lowerCamelCase    :       Union[str, os.PathLike]       ,							**__lowerCamelCase    :       int							)     -> "PretrainedConfig":






         """simple docstring"""







         cls._set_token_in_kwargs(__lowerCamelCase							)

         UpperCAmelCase					,	UpperCAmelCase      					=  cls.get_config_dict(__lowerCamelCase       ,							**__lowerCamelCase							)

         # get the vision config dict if we are loading from Pix2StructConfig
         if config_dict.get("""model_type"""							) == "pix2struct":
            UpperCAmelCase      					=  config_dict["""vision_config"""]

         if "model_type" in config_dict and hasattr(cls       ,							"""model_type"""							) and config_dict["model_type"] != cls.model_type:
            logger.warning(
                F"""You are using a model of type {config_dict['model_type']} to instantiate a model of type """
                F"""{cls.model_type}. This is not supported for all configurations of models and can yield errors."""							)

         return cls.from_dict(__lowerCamelCase       ,							**__lowerCamelCase							)






class 	__lowercase					(	__snake_case	):
      UpperCamelCase										=							'''pix2struct'''
      UpperCamelCase										=							True





      def __init__(		self    :       str       ,							__lowerCamelCase    :       str=None       ,							__lowerCamelCase    :       Dict=None       ,							__lowerCamelCase    :       Optional[Any]=1.0       ,							__lowerCamelCase    :       str=0.02       ,							__lowerCamelCase    :       Any=False       ,							__lowerCamelCase    :       Tuple=False       ,							__lowerCamelCase    :       str=True       ,							**__lowerCamelCase    :       Dict       ,							)     -> Optional[Any]:






         """simple docstring"""







         super().__init__(tie_word_embeddings=__lowerCamelCase       ,							is_encoder_decoder=__lowerCamelCase       ,							**__lowerCamelCase							)

         if text_config is None:
            UpperCAmelCase      					=  {}
            logger.info("""text_config is None. Initializing the Pix2StructTextConfig with default values."""							)

         if vision_config is None:
            UpperCAmelCase      					=  {}
            logger.info("""vision_config is None. Initializing the Pix2StructVisionConfig with default values."""							)

         UpperCAmelCase      					=  PixaStructTextConfig(**__lowerCamelCase							)
         UpperCAmelCase      					=  PixaStructVisionConfig(**__lowerCamelCase							)

         UpperCAmelCase      					=  self.text_config.decoder_start_token_id
         UpperCAmelCase      					=  self.text_config.pad_token_id
         UpperCAmelCase      					=  self.text_config.eos_token_id

         UpperCAmelCase      					=  initializer_factor
         UpperCAmelCase      					=  initializer_range

         UpperCAmelCase      					=  self.initializer_range
         UpperCAmelCase      					=  self.initializer_range

         UpperCAmelCase      					=  is_vqa





      @classmethod
      def 							_lowercase					(		cls    :       List[Any]       ,							__lowerCamelCase    :       PixaStructTextConfig       ,							__lowerCamelCase    :       PixaStructVisionConfig       ,							**__lowerCamelCase    :       Dict							)     -> Union[str, Any]:






         """simple docstring"""







         return cls(text_config=text_config.to_dict()       ,							vision_config=vision_config.to_dict()       ,							**__lowerCamelCase							)





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






         """simple docstring"""







         UpperCAmelCase      					=  copy.deepcopy(self.__dict__							)
         UpperCAmelCase      					=  self.text_config.to_dict()
         UpperCAmelCase      					=  self.vision_config.to_dict()
         UpperCAmelCase      					=  self.__class__.model_type
         return output



