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					:	Optional[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 		a__						(					A__					):

					def __init__(    self      :       Optional[int],*_A      :       Tuple,_A      :       List[Any]=None,_A      :       Tuple=None,_A      :       Optional[int]=None,**_A      :       Union[str, Any]    ):







							"""simple docstring"""






							super().__init__(*_A,**_A    )
							SCREAMING_SNAKE_CASE_  :		Tuple				      =    eval_examples
							SCREAMING_SNAKE_CASE_  :		str				      =    post_process_function
							SCREAMING_SNAKE_CASE_  :		Dict				      =    quant_trainer_args
							SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    128  # default number of calibration samples

					def 				__UpperCamelCase   (    self      :       Optional[int],_A      :       Optional[Any]=None    ):







							"""simple docstring"""






							if calib_dataset is None and self.calib_dataset is None:
									raise ValueError("Trainer: calibration requires an calib_dataset."    )
							SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    calib_dataset if calib_dataset is not None else self.calib_dataset

							SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    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      :       List[Any],_A      :       Optional[Any]=None    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		int				      =    self.train_dataset if calib_dataset is None else calib_dataset
							SCREAMING_SNAKE_CASE_  :		Dict				      =    self.get_calib_dataloader(_A    )

							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    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
									SCREAMING_SNAKE_CASE_      ,    SCREAMING_SNAKE_CASE_      ,    SCREAMING_SNAKE_CASE_  :		Optional[int]				      =    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    )
							SCREAMING_SNAKE_CASE_  :		Any				      =    model

					def 				__UpperCamelCase   (    self      :       Optional[Any],_A      :       Dict=None,_A      :       List[str]=None,_A      :       Union[str, Any]=None,_A      :       str = "eval"    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		str				      =    self.eval_dataset if eval_dataset is None else eval_dataset
							SCREAMING_SNAKE_CASE_  :		Any				      =    self.get_eval_dataloader(_A    )
							SCREAMING_SNAKE_CASE_  :		Optional[Any]				      =    self.eval_examples if eval_examples is None else eval_examples

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

							if self.post_process_function is not None and self.compute_metrics is not None:
									SCREAMING_SNAKE_CASE_  :		Any				      =    self.post_process_function(_A,_A,output.predictions    )
									SCREAMING_SNAKE_CASE_  :		Any				      =    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}_'    ):
													SCREAMING_SNAKE_CASE_  :		str				      =    metrics.pop(_A    )

									self.log(_A    )
							else:
									SCREAMING_SNAKE_CASE_  :		Union[str, 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()    )

							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    self.callback_handler.on_evaluate(self.args,self.state,self.control,_A    )
							return metrics

					def 				__UpperCamelCase   (    self      :       Any,_A      :       Optional[Any],_A      :       List[Any],_A      :       str=None,_A      :       str = "test"    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		List[str]				      =    self.get_test_dataloader(_A    )

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

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

							SCREAMING_SNAKE_CASE_  :		List[str]				      =    self.post_process_function(_A,_A,output.predictions,"predict"    )
							SCREAMING_SNAKE_CASE_  :		int				      =    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}_'    ):
											SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    metrics.pop(_A    )

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






					def 				__UpperCamelCase   (    self      :       Optional[int],_A      :       Any="./"    ):







							"""simple docstring"""






							SCREAMING_SNAKE_CASE_  :		Any				      =    self.eval_dataset
							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    self.get_eval_dataloader(_A    )

							SCREAMING_SNAKE_CASE_  :		int				      =    next(iter(_A    )    )

							# saving device - to make it consistent
							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    torch.device("cuda" if torch.cuda.is_available() else "cpu"    )

							# convert to tuple
							SCREAMING_SNAKE_CASE_  :		List[Any]				      =    tuple(v.to(_A    ) for k, v in batch.items()    )

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

							SCREAMING_SNAKE_CASE_  :		Dict				      =    True

							SCREAMING_SNAKE_CASE_  :		int				      =    self.model.to(_A    )

							model.eval()
							model.float()

							SCREAMING_SNAKE_CASE_  :		Any				      =    model.module if hasattr(_A,"module"    ) else model
							quant_trainer.configure_model(_A,self.quant_trainer_args    )

							SCREAMING_SNAKE_CASE_  :		int				      =    os.path.join(_A,"model.onnx"    )
							logger.info(F'exporting model to {output_model_file}'    )

							SCREAMING_SNAKE_CASE_  :		Union[str, Any]				      =    {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"    )

from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available, is_vision_available


__lowerCamelCase					:	Union[str, Any]       =  {
    '''configuration_chinese_clip''': [
        '''CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''ChineseCLIPConfig''',
        '''ChineseCLIPOnnxConfig''',
        '''ChineseCLIPTextConfig''',
        '''ChineseCLIPVisionConfig''',
    ],
    '''processing_chinese_clip''': ['''ChineseCLIPProcessor'''],
}

try:
		if not is_vision_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__lowerCamelCase					:	Any       =  ['''ChineseCLIPFeatureExtractor''']
		__lowerCamelCase					:	Optional[int]       =  ['''ChineseCLIPImageProcessor''']

try:
		if not is_torch_available():
				raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
		pass
else:
		__lowerCamelCase					:	int       =  [
		    '''CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST''',
		    '''ChineseCLIPModel''',
		    '''ChineseCLIPPreTrainedModel''',
		    '''ChineseCLIPTextModel''',
		    '''ChineseCLIPVisionModel''',
		]

if TYPE_CHECKING:
		from .configuration_chinese_clip import (
		    CHINESE_CLIP_PRETRAINED_CONFIG_ARCHIVE_MAP,
		    ChineseCLIPConfig,
		    ChineseCLIPOnnxConfig,
		    ChineseCLIPTextConfig,
		    ChineseCLIPVisionConfig,
		)
		from .processing_chinese_clip import ChineseCLIPProcessor

		try:
				if not is_vision_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .feature_extraction_chinese_clip import ChineseCLIPFeatureExtractor, ChineseCLIPImageProcessor

		try:
				if not is_torch_available():
						raise OptionalDependencyNotAvailable()
		except OptionalDependencyNotAvailable:
				pass
		else:
				from .modeling_chinese_clip import (
				    CHINESE_CLIP_PRETRAINED_MODEL_ARCHIVE_LIST,
				    ChineseCLIPModel,
				    ChineseCLIPPreTrainedModel,
				    ChineseCLIPTextModel,
				    ChineseCLIPVisionModel,
				)

else:
		import sys

		__lowerCamelCase					:	Any       =  _LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)
