
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


_UpperCAmelCase :			int              =	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     lowercase (		_SCREAMING_SNAKE_CASE       ):



		def __init__(			self    ,				*A_    ,				A_=None    ,				A_=None    ,				A_=None    ,				**A_	)  -> int:
				"""simple docstring"""
				super().__init__(*A_    ,				**A_	)
				UpperCamelCase          =					eval_examples
				UpperCamelCase          =					post_process_function
				UpperCamelCase          =					quant_trainer_args
				UpperCamelCase          =					128  # default number of calibration samples



		def __UpperCamelCase			(			self    ,				A_=None	)  -> str:
				"""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(A_    ,				description='Calibration'	)

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



		def __UpperCamelCase			(			self    ,				A_=None	)  -> List[str]:
				"""simple docstring"""
				UpperCamelCase          =					self.train_dataset if calib_dataset is None else calib_dataset
				UpperCamelCase          =					self.get_calib_dataloader(A_	)

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

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

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



		def __UpperCamelCase			(			self    ,				A_=None    ,				A_=None    ,				A_=None    ,				A_ = "eval"	)  -> List[str]:
				"""simple docstring"""
				UpperCamelCase          =					self.eval_dataset if eval_dataset is None else eval_dataset
				UpperCamelCase          =					self.get_eval_dataloader(A_	)
				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(
						    A_    ,				description='Evaluation'    ,				prediction_loss_only=True if compute_metrics is None else None    ,				ignore_keys=A_    ,				)
				finally:
						UpperCamelCase          =					compute_metrics

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

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

						self.log(A_	)
				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    ,				A_	)
				return metrics



		def __UpperCamelCase			(			self    ,				A_    ,				A_    ,				A_=None    ,				A_ = "test"	)  -> List[str]:
				"""simple docstring"""
				UpperCamelCase          =					self.get_test_dataloader(A_	)

				# 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(
						    A_    ,				description='Prediction'    ,				prediction_loss_only=True if compute_metrics is None else None    ,				ignore_keys=A_    ,				)
				finally:
						UpperCamelCase          =					compute_metrics

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

				UpperCamelCase          =					self.post_process_function(A_    ,				A_    ,				output.predictions    ,				'predict'	)
				UpperCamelCase          =					self.compute_metrics(A_	)

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

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



		def __UpperCamelCase			(			self    ,				A_="./"	)  -> str:
				"""simple docstring"""
				UpperCamelCase          =					self.eval_dataset
				UpperCamelCase          =					self.get_eval_dataloader(A_	)

				UpperCamelCase          =					next(iter(A_	)	)

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

				# convert to tuple
				UpperCamelCase          =					tuple(v.to(A_	) 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(A_	)

				model.eval()
				model.float()

				UpperCamelCase          =					model.module if hasattr(A_    ,				'module'	) else model
				quant_trainer.configure_model(A_    ,				self.quant_trainer_args	)

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

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

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


import unittest

import torch
from torch import nn

from diffusers.models.activations import get_activation


class     lowercase (		unittest.TestCase       ):



		def __UpperCamelCase			(			self	)  -> List[Any]:
				"""simple docstring"""
				UpperCamelCase          =					get_activation('swish'	)

				self.assertIsInstance(A_    ,				nn.SiLU	)

				self.assertEqual(act(torch.tensor(-100    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertNotEqual(act(torch.tensor(-1    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertEqual(act(torch.tensor(0    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertEqual(act(torch.tensor(20    ,				dtype=torch.floataa	)	).item()    ,				20	)



		def __UpperCamelCase			(			self	)  -> Optional[int]:
				"""simple docstring"""
				UpperCamelCase          =					get_activation('silu'	)

				self.assertIsInstance(A_    ,				nn.SiLU	)

				self.assertEqual(act(torch.tensor(-100    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertNotEqual(act(torch.tensor(-1    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertEqual(act(torch.tensor(0    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertEqual(act(torch.tensor(20    ,				dtype=torch.floataa	)	).item()    ,				20	)



		def __UpperCamelCase			(			self	)  -> str:
				"""simple docstring"""
				UpperCamelCase          =					get_activation('mish'	)

				self.assertIsInstance(A_    ,				nn.Mish	)

				self.assertEqual(act(torch.tensor(-200    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertNotEqual(act(torch.tensor(-1    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertEqual(act(torch.tensor(0    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertEqual(act(torch.tensor(20    ,				dtype=torch.floataa	)	).item()    ,				20	)



		def __UpperCamelCase			(			self	)  -> Union[str, Any]:
				"""simple docstring"""
				UpperCamelCase          =					get_activation('gelu'	)

				self.assertIsInstance(A_    ,				nn.GELU	)

				self.assertEqual(act(torch.tensor(-100    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertNotEqual(act(torch.tensor(-1    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertEqual(act(torch.tensor(0    ,				dtype=torch.floataa	)	).item()    ,				0	)
				self.assertEqual(act(torch.tensor(20    ,				dtype=torch.floataa	)	).item()    ,				20	)
