
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


__lowerCAmelCase					:			Dict			      =		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 						__lowerCAmelCase	(	lowerCAmelCase_      ):



      """simple docstring"""







      def __init__(      self		:       Tuple				,       *_snake_case		:       Optional[Any]				,       _snake_case		:       List[Any]=None				,       _snake_case		:       str=None				,       _snake_case		:       str=None				,       **_snake_case		:       List[str]				):
          super().__init__(*UpperCAmelCase__				,       **UpperCAmelCase__				)
          __lowercase		:			Any	    =       eval_examples
          __lowercase		:			Optional[int]	    =       post_process_function
          __lowercase		:			Dict	    =       quant_trainer_args
          __lowercase		:			Optional[Any]	    =       128  # default number of calibration samples




      def 		snake_case_			(      self		:       Dict				,       _snake_case		:       Tuple=None				):
          if calib_dataset is None and self.calib_dataset is None:
              raise ValueError('''Trainer: calibration requires an calib_dataset.'''				)
          __lowercase		:			List[str]	    =       calib_dataset if calib_dataset is not None else self.calib_dataset

          __lowercase		:			List[str]	    =       self._remove_unused_columns(UpperCAmelCase__				,       description='''Calibration'''				)

          return DataLoader(
              UpperCAmelCase__				,       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=UpperCAmelCase__				,       )




      def 		snake_case_			(      self		:       Any				,       _snake_case		:       Optional[int]=None				):
          __lowercase		:			List[Any]	    =       self.train_dataset if calib_dataset is None else calib_dataset
          __lowercase		:			str	    =       self.get_calib_dataloader(UpperCAmelCase__				)

          __lowercase		:			Dict	    =       self.model
          quant_trainer.configure_model(UpperCAmelCase__				,       self.quant_trainer_args				,       calib=UpperCAmelCase__				)
          model.eval()
          quant_trainer.enable_calibration(UpperCAmelCase__				)

          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(UpperCAmelCase__				):
              # Prediction step
              __lowercase    ,      __lowercase    ,      __lowercase		:			int	    =       self.prediction_step(UpperCAmelCase__				,       UpperCAmelCase__				,       prediction_loss_only=UpperCAmelCase__				)
              if (step + 1) * calib_dataloader.batch_size >= self.calib_num:
                  break

          quant_trainer.finish_calibration(UpperCAmelCase__				,       self.quant_trainer_args				)
          __lowercase		:			int	    =       model




      def 		snake_case_			(      self		:       List[str]				,       _snake_case		:       Tuple=None				,       _snake_case		:       Optional[Any]=None				,       _snake_case		:       Optional[Any]=None				,       _snake_case		:       str = "eval"				):
          __lowercase		:			Optional[Any]	    =       self.eval_dataset if eval_dataset is None else eval_dataset
          __lowercase		:			str	    =       self.get_eval_dataloader(UpperCAmelCase__				)
          __lowercase		:			Any	    =       self.eval_examples if eval_examples is None else eval_examples

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

          if self.post_process_function is not None and self.compute_metrics is not None:
              __lowercase		:			Tuple	    =       self.post_process_function(UpperCAmelCase__				,       UpperCAmelCase__				,       output.predictions				)
              __lowercase		:			Optional[Any]	    =       self.compute_metrics(UpperCAmelCase__				)

              # Prefix all keys with metric_key_prefix + '_'
              for key in list(metrics.keys()				):
                  if not key.startswith(F'{metric_key_prefix}_'				):
                      __lowercase		:			Tuple	    =       metrics.pop(UpperCAmelCase__				)

              self.log(UpperCAmelCase__				)
          else:
              __lowercase		:			Tuple	    =       {}

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

          __lowercase		:			Any	    =       self.callback_handler.on_evaluate(self.args				,       self.state				,       self.control				,       UpperCAmelCase__				)
          return metrics




      def 		snake_case_			(      self		:       List[Any]				,       _snake_case		:       int				,       _snake_case		:       List[str]				,       _snake_case		:       Dict=None				,       _snake_case		:       str = "test"				):
          __lowercase		:			List[Any]	    =       self.get_test_dataloader(UpperCAmelCase__				)

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

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

          __lowercase		:			Any	    =       self.post_process_function(UpperCAmelCase__				,       UpperCAmelCase__				,       output.predictions				,       '''predict'''				)
          __lowercase		:			List[str]	    =       self.compute_metrics(UpperCAmelCase__				)

          # Prefix all keys with metric_key_prefix + '_'
          for key in list(metrics.keys()				):
              if not key.startswith(F'{metric_key_prefix}_'				):
                  __lowercase		:			int	    =       metrics.pop(UpperCAmelCase__				)

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




      def 		snake_case_			(      self		:       Any				,       _snake_case		:       Dict="./"				):
          __lowercase		:			Optional[int]	    =       self.eval_dataset
          __lowercase		:			Tuple	    =       self.get_eval_dataloader(UpperCAmelCase__				)

          __lowercase		:			str	    =       next(iter(UpperCAmelCase__				)				)

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

          # convert to tuple
          __lowercase		:			str	    =       tuple(v.to(UpperCAmelCase__				) for k, v in batch.items()				)

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

          __lowercase		:			Tuple	    =       True

          __lowercase		:			Dict	    =       self.model.to(UpperCAmelCase__				)

          model.eval()
          model.float()

          __lowercase		:			Optional[int]	    =       model.module if hasattr(UpperCAmelCase__				,       '''module'''				) else model
          quant_trainer.configure_model(UpperCAmelCase__				,       self.quant_trainer_args				)

          __lowercase		:			List[Any]	    =       os.path.join(UpperCAmelCase__				,       '''model.onnx'''				)
          logger.info(F'exporting model to {output_model_file}'				)

          __lowercase		:			Optional[int]	    =       {0: '''batch_size''', 1: '''seq_len'''}

          torch.onnx.export(
              UpperCAmelCase__				,       UpperCAmelCase__				,       UpperCAmelCase__				,       export_params=UpperCAmelCase__				,       opset_version=13				,       do_constant_folding=UpperCAmelCase__				,       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=UpperCAmelCase__				,       )
          logger.info('''onnx export finished'''				)




import warnings
from collections import OrderedDict
from typing import Mapping

from packaging import version

from ...configuration_utils import PretrainedConfig
from ...onnx import OnnxConfig
from ...utils import logging


__lowerCAmelCase					:			Union[str, Any]			      =		logging.get_logger(__name__)

__lowerCAmelCase					:			Optional[int]			      =		{
    "nvidia/segformer-b0-finetuned-ade-512-512": (
        "https://huggingface.co/nvidia/segformer-b0-finetuned-ade-512-512/resolve/main/config.json"
    ),
    # See all SegFormer models at https://huggingface.co/models?filter=segformer
}


class 						__lowerCAmelCase	(	lowerCAmelCase_      ):



      """simple docstring"""







      A__	:			Dict        =    '''segformer'''




      def __init__(      self		:       Optional[Any]				,       _snake_case		:       List[str]=3				,       _snake_case		:       Union[str, Any]=4				,       _snake_case		:       str=[2, 2, 2, 2]				,       _snake_case		:       Dict=[8, 4, 2, 1]				,       _snake_case		:       List[Any]=[32, 64, 160, 256]				,       _snake_case		:       Dict=[7, 3, 3, 3]				,       _snake_case		:       List[Any]=[4, 2, 2, 2]				,       _snake_case		:       Tuple=[1, 2, 5, 8]				,       _snake_case		:       Optional[Any]=[4, 4, 4, 4]				,       _snake_case		:       Optional[int]="gelu"				,       _snake_case		:       Optional[int]=0.0				,       _snake_case		:       Optional[int]=0.0				,       _snake_case		:       int=0.1				,       _snake_case		:       Any=0.02				,       _snake_case		:       Optional[int]=0.1				,       _snake_case		:       Union[str, Any]=1E-6				,       _snake_case		:       Tuple=256				,       _snake_case		:       str=255				,       **_snake_case		:       Any				,       ):
          super().__init__(**_snake_case				)

          if "reshape_last_stage" in kwargs and kwargs["reshape_last_stage"] is False:
              warnings.warn(
                  '''Reshape_last_stage is set to False in this config. This argument is deprecated and will soon be'''
                  ''' removed, as the behaviour will default to that of reshape_last_stage = True.'''				,       _snake_case				,       )

          __lowercase		:			Any	    =       num_channels
          __lowercase		:			Optional[Any]	    =       num_encoder_blocks
          __lowercase		:			List[str]	    =       depths
          __lowercase		:			str	    =       sr_ratios
          __lowercase		:			List[str]	    =       hidden_sizes
          __lowercase		:			List[str]	    =       patch_sizes
          __lowercase		:			Dict	    =       strides
          __lowercase		:			Optional[int]	    =       mlp_ratios
          __lowercase		:			List[str]	    =       num_attention_heads
          __lowercase		:			Optional[int]	    =       hidden_act
          __lowercase		:			List[Any]	    =       hidden_dropout_prob
          __lowercase		:			Optional[Any]	    =       attention_probs_dropout_prob
          __lowercase		:			List[Any]	    =       classifier_dropout_prob
          __lowercase		:			str	    =       initializer_range
          __lowercase		:			Optional[int]	    =       drop_path_rate
          __lowercase		:			List[str]	    =       layer_norm_eps
          __lowercase		:			List[str]	    =       decoder_hidden_size
          __lowercase		:			Union[str, Any]	    =       kwargs.get('''reshape_last_stage'''				,       _snake_case				)
          __lowercase		:			str	    =       semantic_loss_ignore_index






class 						__lowerCAmelCase	(	lowerCAmelCase_      ):



      """simple docstring"""







      A__	:			Union[str, Any]        =    version.parse('''1.11'''      )




      @property
      def 		snake_case_			(      self		:       Dict				):
          return OrderedDict(
              [
                  ('''pixel_values''', {0: '''batch''', 1: '''num_channels''', 2: '''height''', 3: '''width'''}),
              ]				)




      @property
      def 		snake_case_			(      self		:       int				):
          return 1E-4




      @property
      def 		snake_case_			(      self		:       List[Any]				):
          return 12


