


from typing import Dict, List, Optional, Union

import numpy as np

from .feature_extraction_utils import BatchFeature, FeatureExtractionMixin
from .utils import PaddingStrategy, TensorType, is_tf_tensor, is_torch_tensor, logging, to_numpy


__snake_case            =   logging.get_logger(__name__)




class    UpperCAmelCase					(       __snake_case    ):




   def __init__(						self							:		Dict       ,       __magic_name__							:		int       ,       __magic_name__							:		int       ,       __magic_name__							:		float       ,       **__magic_name__							:		Tuple						):







      """simple docstring"""







      UpperCamelCase         =					feature_size
      UpperCamelCase         =					sampling_rate
      UpperCamelCase         =					padding_value

      UpperCamelCase         =					kwargs.pop("""padding_side"""       ,       """right"""						)
      UpperCamelCase         =					kwargs.pop("""return_attention_mask"""       ,       __magic_name__						)

      super().__init__(**__magic_name__						)




   def   lowerCamelCase_					(						self							:		Any       ,       __magic_name__							:		Union[
           BatchFeature,
           List[BatchFeature],
           Dict[str, BatchFeature],
           Dict[str, List[BatchFeature]],
           List[Dict[str, BatchFeature]],
       ]       ,       __magic_name__							:		Union[bool, str, PaddingStrategy] = True       ,       __magic_name__							:		Optional[int] = None       ,       __magic_name__							:		bool = False       ,       __magic_name__							:		Optional[int] = None       ,       __magic_name__							:		Optional[bool] = None       ,       __magic_name__							:		Optional[Union[str, TensorType]] = None       ,       ):







      """simple docstring"""







      if isinstance(__magic_name__       ,       (list, tuple)						) and isinstance(processed_features[0]       ,       (dict, BatchFeature)						):
         UpperCamelCase         =					{
             key: [example[key] for example in processed_features] for key in processed_features[0].keys()
         }

      # The model's main input name, usually `input_values`, has be passed for padding
      if self.model_input_names[0] not in processed_features:
         raise ValueError(
             """You should supply an instance of `transformers.BatchFeature` or list of `transformers.BatchFeature`"""
             F' to this method that includes {self.model_input_names[0]}, but you provided'
             F' {list(processed_features.keys()						)}'						)

      UpperCamelCase         =					processed_features[self.model_input_names[0]]
      UpperCamelCase         =					(
          return_attention_mask if return_attention_mask is not None else self.return_attention_mask
      )

      if len(__magic_name__						) == 0:
         if return_attention_mask:
            UpperCamelCase         =					[]
         return processed_features

      # If we have PyTorch/TF tensors or lists as inputs, we cast them as Numpy arrays
      # and rebuild them afterwards if no return_tensors is specified
      # Note that we lose the specific device the tensor may be on for PyTorch

      UpperCamelCase         =					required_input[0]
      if isinstance(__magic_name__       ,       (list, tuple)						):
         # first_element might be an empty list/tuple in some edge cases so we grab the first non empty element.
         UpperCamelCase         =					0
         while len(required_input[index]						) == 0:
            index += 1
         if index < len(__magic_name__						):
            UpperCamelCase         =					required_input[index][0]

      if return_tensors is None:
         if is_tf_tensor(__magic_name__						):
            UpperCamelCase         =					"""tf"""
         elif is_torch_tensor(__magic_name__						):
            UpperCamelCase         =					"""pt"""
         elif isinstance(__magic_name__       ,       (int, float, list, tuple, np.ndarray)						):
            UpperCamelCase         =					"""np"""
         else:
            raise ValueError(
                F'type of {first_element} unknown: {type(__magic_name__						)}. '
                """Should be one of a python, numpy, pytorch or tensorflow object."""						)

      for key, value in processed_features.items():
         if isinstance(value[0]       ,       (int, float)						):
            UpperCamelCase         =					to_numpy(__magic_name__						)
         else:
            UpperCamelCase         =					[to_numpy(__magic_name__						) for v in value]

        # Convert padding_strategy in PaddingStrategy
      UpperCamelCase         =					self._get_padding_strategies(padding=__magic_name__       ,       max_length=__magic_name__						)

      UpperCamelCase         =					processed_features[self.model_input_names[0]]

      UpperCamelCase         =					len(__magic_name__						)
      if not all(len(__magic_name__						) == batch_size for v in processed_features.values()						):
         raise ValueError("""Some items in the output dictionary have a different batch size than others."""						)

      UpperCamelCase         =					[]
      for i in range(__magic_name__						):
         UpperCamelCase         =					{k: v[i] for k, v in processed_features.items()}
         # truncation
         UpperCamelCase         =					self._truncate(
             __magic_name__       ,       max_length=__magic_name__       ,       pad_to_multiple_of=__magic_name__       ,       truncation=__magic_name__       ,       )
         truncated_inputs.append(__magic_name__						)

      if padding_strategy == PaddingStrategy.LONGEST:
         # make sure that `max_length` cannot be longer than the longest truncated length
         UpperCamelCase         =					max(len(input_slice[self.model_input_names[0]]						) for input_slice in truncated_inputs						)
         UpperCamelCase         =					PaddingStrategy.MAX_LENGTH

      UpperCamelCase         =					{}
      for i in range(__magic_name__						):
         # padding
         UpperCamelCase         =					self._pad(
             truncated_inputs[i]       ,       max_length=__magic_name__       ,       padding_strategy=__magic_name__       ,       pad_to_multiple_of=__magic_name__       ,       return_attention_mask=__magic_name__       ,       )

         for key, value in outputs.items():
            if key not in batch_outputs:
               UpperCamelCase         =					[]
            if value.dtype is np.dtype(np.floataa						):
               UpperCamelCase         =					value.astype(np.floataa						)
            batch_outputs[key].append(__magic_name__						)

      return BatchFeature(__magic_name__       ,       tensor_type=__magic_name__						)




   def   lowerCamelCase_					(						self							:		Optional[Any]       ,       __magic_name__							:		Union[Dict[str, np.ndarray], BatchFeature]       ,       __magic_name__							:		Optional[int] = None       ,       __magic_name__							:		PaddingStrategy = PaddingStrategy.DO_NOT_PAD       ,       __magic_name__							:		Optional[int] = None       ,       __magic_name__							:		Optional[bool] = None       ,       ):







      """simple docstring"""







      UpperCamelCase         =					processed_features[self.model_input_names[0]]

      if padding_strategy == PaddingStrategy.LONGEST:
         UpperCamelCase         =					len(__magic_name__						)

      if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
         UpperCamelCase         =					((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

      UpperCamelCase         =					padding_strategy != PaddingStrategy.DO_NOT_PAD and len(__magic_name__						) < max_length

      if return_attention_mask and "attention_mask" not in processed_features:
         UpperCamelCase         =					np.ones(len(__magic_name__						)       ,       dtype=np.intaa						)

      if needs_to_be_padded:
         UpperCamelCase         =					max_length - len(__magic_name__						)
         if self.padding_side == "right":
            if return_attention_mask:
               UpperCamelCase         =					np.pad(
                   processed_features["""attention_mask"""]       ,       (0, difference)						)
            UpperCamelCase         =					((0, difference), (0, 0)) if self.feature_size > 1 else (0, difference)
            UpperCamelCase         =					np.pad(
                __magic_name__       ,       __magic_name__       ,       """constant"""       ,       constant_values=self.padding_value						)
         elif self.padding_side == "left":
            if return_attention_mask:
               UpperCamelCase         =					np.pad(
                   processed_features["""attention_mask"""]       ,       (difference, 0)						)
            UpperCamelCase         =					((difference, 0), (0, 0)) if self.feature_size > 1 else (difference, 0)
            UpperCamelCase         =					np.pad(
                __magic_name__       ,       __magic_name__       ,       """constant"""       ,       constant_values=self.padding_value						)
         else:
            raise ValueError("""Invalid padding strategy:""" + str(self.padding_side						)						)

      return processed_features




   def   lowerCamelCase_					(						self							:		Tuple       ,       __magic_name__							:		Union[Dict[str, np.ndarray], BatchFeature]       ,       __magic_name__							:		Optional[int] = None       ,       __magic_name__							:		Optional[int] = None       ,       __magic_name__							:		Optional[bool] = None       ,       ):







      """simple docstring"""







      if not truncation:
         return processed_features
      elif truncation and max_length is None:
         raise ValueError("""When setting ``truncation=True``, make sure that ``max_length`` is defined."""						)

      UpperCamelCase         =					processed_features[self.model_input_names[0]]

      # find `max_length` that fits `pad_to_multiple_of`
      if max_length is not None and pad_to_multiple_of is not None and (max_length % pad_to_multiple_of != 0):
         UpperCamelCase         =					((max_length // pad_to_multiple_of) + 1) * pad_to_multiple_of

      UpperCamelCase         =					len(__magic_name__						) > max_length

      if needs_to_be_truncated:
         UpperCamelCase         =					processed_features[self.model_input_names[0]][:max_length]
         if "attention_mask" in processed_features:
            UpperCamelCase         =					processed_features["""attention_mask"""][:max_length]

      return processed_features





   def   lowerCamelCase_					(						self							:		List[Any]       ,       __magic_name__							:		List[str]=False       ,       __magic_name__							:		List[str]=None						):







      """simple docstring"""







      if padding is not False:
         if padding is True:
            UpperCamelCase         =					PaddingStrategy.LONGEST  # Default to pad to the longest sequence in the batch
         elif not isinstance(__magic_name__       ,       __magic_name__						):
            UpperCamelCase         =					PaddingStrategy(__magic_name__						)
         elif isinstance(__magic_name__       ,       __magic_name__						):
            UpperCamelCase         =					padding
      else:
         UpperCamelCase         =					PaddingStrategy.DO_NOT_PAD

      # Set max length if needed
      if max_length is None:
         if padding_strategy == PaddingStrategy.MAX_LENGTH:
            raise ValueError(
                F'When setting ``padding={PaddingStrategy.MAX_LENGTH}``, make sure that max_length is defined'						)

        # Test if we have a padding value
      if padding_strategy != PaddingStrategy.DO_NOT_PAD and (self.padding_value is None):
         raise ValueError(
             """Asking to pad but the feature_extractor does not have a padding value. Please select a value to use"""
             """ as `padding_value`. For example: `feature_extractor.padding_value = 0.0`."""						)

      return padding_strategy








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


__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    UpperCAmelCase					(       __snake_case    ):




			def __init__(						self							:		Dict       ,       *__magic_name__							:		Union[str, Any]       ,       __magic_name__							:		Any=None       ,       __magic_name__							:		Union[str, Any]=None       ,       __magic_name__							:		Any=None       ,       **__magic_name__							:		Optional[Any]						):







						"""simple docstring"""







						super().__init__(*__magic_name__       ,       **__magic_name__						)
						UpperCamelCase         =					eval_examples
						UpperCamelCase         =					post_process_function
						UpperCamelCase         =					quant_trainer_args
						UpperCamelCase         =					1_2_8  # default number of calibration samples




			def   lowerCamelCase_					(						self							:		str       ,       __magic_name__							:		List[Any]=None						):







						"""simple docstring"""







						if calib_dataset is None and self.calib_dataset is None:
									raise ValueError("""Trainer: calibration requires an calib_dataset."""						)
						UpperCamelCase         =					calib_dataset if calib_dataset is not None else self.calib_dataset

						UpperCamelCase         =					self._remove_unused_columns(__magic_name__       ,       description="""Calibration"""						)

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




			def   lowerCamelCase_					(						self							:		Tuple       ,       __magic_name__							:		List[str]=None						):







						"""simple docstring"""







						UpperCamelCase         =					self.train_dataset if calib_dataset is None else calib_dataset
						UpperCamelCase         =					self.get_calib_dataloader(__magic_name__						)

						UpperCamelCase         =					self.model
						quant_trainer.configure_model(__magic_name__       ,       self.quant_trainer_args       ,       calib=__magic_name__						)
						model.eval()
						quant_trainer.enable_calibration(__magic_name__						)

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

						quant_trainer.finish_calibration(__magic_name__       ,       self.quant_trainer_args						)
						UpperCamelCase         =					model




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







						"""simple docstring"""







						UpperCamelCase         =					self.eval_dataset if eval_dataset is None else eval_dataset
						UpperCamelCase         =					self.get_eval_dataloader(__magic_name__						)
						UpperCamelCase         =					self.eval_examples if eval_examples is None else eval_examples

						# Temporarily disable metric computation, we will do it in the loop here.
						UpperCamelCase         =					self.compute_metrics
						UpperCamelCase         =					None
						UpperCamelCase         =					self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
						try:
									UpperCamelCase         =					eval_loop(
									    __magic_name__       ,       description="""Evaluation"""       ,       prediction_loss_only=True if compute_metrics is None else None       ,       ignore_keys=__magic_name__       ,       )
						finally:
									UpperCamelCase         =					compute_metrics

						if self.post_process_function is not None and self.compute_metrics is not None:
									UpperCamelCase         =					self.post_process_function(__magic_name__       ,       __magic_name__       ,       output.predictions						)
									UpperCamelCase         =					self.compute_metrics(__magic_name__						)

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

									self.log(__magic_name__						)
						else:
									UpperCamelCase         =					{}

						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         =					self.callback_handler.on_evaluate(self.args       ,       self.state       ,       self.control       ,       __magic_name__						)
						return metrics




			def   lowerCamelCase_					(						self							:		Dict       ,       __magic_name__							:		List[str]       ,       __magic_name__							:		List[Any]       ,       __magic_name__							:		List[Any]=None       ,       __magic_name__							:		str = "test"						):







						"""simple docstring"""







						UpperCamelCase         =					self.get_test_dataloader(__magic_name__						)

						# Temporarily disable metric computation, we will do it in the loop here.
						UpperCamelCase         =					self.compute_metrics
						UpperCamelCase         =					None
						UpperCamelCase         =					self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
						try:
									UpperCamelCase         =					eval_loop(
									    __magic_name__       ,       description="""Prediction"""       ,       prediction_loss_only=True if compute_metrics is None else None       ,       ignore_keys=__magic_name__       ,       )
						finally:
									UpperCamelCase         =					compute_metrics

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

						UpperCamelCase         =					self.post_process_function(__magic_name__       ,       __magic_name__       ,       output.predictions       ,       """predict"""						)
						UpperCamelCase         =					self.compute_metrics(__magic_name__						)

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

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





			def   lowerCamelCase_					(						self							:		List[str]       ,       __magic_name__							:		Optional[Any]="./"						):







						"""simple docstring"""







						UpperCamelCase         =					self.eval_dataset
						UpperCamelCase         =					self.get_eval_dataloader(__magic_name__						)

						UpperCamelCase         =					next(iter(__magic_name__						)						)

						# saving device - to make it consistent
						UpperCamelCase         =					torch.device("""cuda""" if torch.cuda.is_available() else """cpu"""						)

						# convert to tuple
						UpperCamelCase         =					tuple(v.to(__magic_name__						) for k, v in batch.items()						)

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

						UpperCamelCase         =					True

						UpperCamelCase         =					self.model.to(__magic_name__						)

						model.eval()
						model.float()

						UpperCamelCase         =					model.module if hasattr(__magic_name__       ,       """module"""						) else model
						quant_trainer.configure_model(__magic_name__       ,       self.quant_trainer_args						)

						UpperCamelCase         =					os.path.join(__magic_name__       ,       """model.onnx"""						)
						logger.info(F'exporting model to {output_model_file}'						)

						UpperCamelCase         =					{0: """batch_size""", 1: """seq_len"""}

						torch.onnx.export(
						    __magic_name__       ,       __magic_name__       ,       __magic_name__       ,       export_params=__magic_name__       ,       opset_version=1_3       ,       do_constant_folding=__magic_name__       ,       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=__magic_name__       ,       )
						logger.info("""onnx export finished"""						)




