"""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


lowerCAmelCase_     =	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  _snake_case		(    __snake_case ):







				"""simple docstring"""






				def __init__(      self							:							Tuple      ,    *_A							:							Dict      ,    _A							:							Dict=None      ,    _A							:							Tuple=None      ,    _A							:							Dict=None      ,    **_A							:							Any):



											"""simple docstring"""







											super().__init__(*A_      ,    **A_)
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    eval_examples
											_SCREAMING_SNAKE_CASE		:						Dict			  =    post_process_function
											_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    quant_trainer_args
											_SCREAMING_SNAKE_CASE		:						Any			  =    1_2_8  # default number of calibration samples





				def 		_lowerCAmelCase		(      self							:							Dict      ,    _A							:							Union[str, 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		:						Any			  =    calib_dataset if calib_dataset is not None else self.calib_dataset

											_SCREAMING_SNAKE_CASE		:						Dict			  =    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 		_lowerCAmelCase		(      self							:							Dict      ,    _A							:							Any=None):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Any			  =    self.train_dataset if calib_dataset is None else calib_dataset
											_SCREAMING_SNAKE_CASE		:						Dict			  =    self.get_calib_dataloader(A_)

											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    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		:						List[Any]			  =    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		:						str			  =    model





				def 		_lowerCAmelCase		(      self							:							Optional[Any]      ,    _A							:							Optional[int]=None      ,    _A							:							Optional[Any]=None      ,    _A							:							Tuple=None      ,    _A							:							List[Any] = "eval"):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[Any]			  =    self.eval_dataset if eval_dataset is None else eval_dataset
											_SCREAMING_SNAKE_CASE		:						Any			  =    self.get_eval_dataloader(A_)
											_SCREAMING_SNAKE_CASE		:						List[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[Any]			  =    self.compute_metrics
											_SCREAMING_SNAKE_CASE		:						Dict			  =    None
											_SCREAMING_SNAKE_CASE		:						Any			  =    self.prediction_loop if self.args.use_legacy_prediction_loop else self.evaluation_loop
											try:
																		_SCREAMING_SNAKE_CASE		:						int			  =    eval_loop(
																		    A_      ,    description="""Evaluation"""      ,    prediction_loss_only=True if compute_metrics is None else None      ,    ignore_keys=A_      ,    )
											finally:
																		_SCREAMING_SNAKE_CASE		:						Union[str, Any]			  =    compute_metrics

											if self.post_process_function is not None and self.compute_metrics is not None:
																		_SCREAMING_SNAKE_CASE		:						List[str]			  =    self.post_process_function(A_      ,    A_      ,    output.predictions)
																		_SCREAMING_SNAKE_CASE		:						Dict			  =    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		:						Optional[int]			  =    metrics.pop(A_)

																		self.log(A_)
											else:
																		_SCREAMING_SNAKE_CASE		:						List[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		:						Tuple			  =    self.callback_handler.on_evaluate(self.args      ,    self.state      ,    self.control      ,    A_)
											return metrics





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



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						Optional[int]			  =    self.get_test_dataloader(A_)

											# Temporarily disable metric computation, we will do it in the loop here.
											_SCREAMING_SNAKE_CASE		:						int			  =    self.compute_metrics
											_SCREAMING_SNAKE_CASE		:						Tuple			  =    None
											_SCREAMING_SNAKE_CASE		:						int			  =    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		:						Dict			  =    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		:						Optional[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		:						Tuple			  =    metrics.pop(A_)

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





				def 		_lowerCAmelCase		(      self							:							List[Any]      ,    _A							:							Any="./"):



											"""simple docstring"""







											_SCREAMING_SNAKE_CASE		:						str			  =    self.eval_dataset
											_SCREAMING_SNAKE_CASE		:						Dict			  =    self.get_eval_dataloader(A_)

											_SCREAMING_SNAKE_CASE		:						List[str]			  =    next(iter(A_))

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

											# convert to tuple
											_SCREAMING_SNAKE_CASE		:						Union[str, 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		:						str			  =    True

											_SCREAMING_SNAKE_CASE		:						int			  =    self.model.to(A_)

											model.eval()
											model.float()

											_SCREAMING_SNAKE_CASE		:						Tuple			  =    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		:						List[Any]			  =    {0: "batch_size", 1: "seq_len"}

											torch.onnx.export(
											    A_      ,    A_      ,    A_      ,    export_params=A_      ,    opset_version=1_3      ,    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""")



"""simple docstring"""





from typing import TYPE_CHECKING

from ...utils import OptionalDependencyNotAvailable, _LazyModule, is_torch_available


lowerCAmelCase_     =	{
    '''configuration_clipseg''': [
        '''CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP''',
        '''CLIPSegConfig''',
        '''CLIPSegTextConfig''',
        '''CLIPSegVisionConfig''',
    ],
    '''processing_clipseg''': ['''CLIPSegProcessor'''],
}

try:
			if not is_torch_available():
						raise OptionalDependencyNotAvailable()
except OptionalDependencyNotAvailable:
			pass
else:
			lowerCAmelCase_     =	[
			    '''CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST''',
			    '''CLIPSegModel''',
			    '''CLIPSegPreTrainedModel''',
			    '''CLIPSegTextModel''',
			    '''CLIPSegVisionModel''',
			    '''CLIPSegForImageSegmentation''',
			]

if TYPE_CHECKING:
			from .configuration_clipseg import (
			    CLIPSEG_PRETRAINED_CONFIG_ARCHIVE_MAP,
			    CLIPSegConfig,
			    CLIPSegTextConfig,
			    CLIPSegVisionConfig,
			)
			from .processing_clipseg import CLIPSegProcessor

			try:
						if not is_torch_available():
									raise OptionalDependencyNotAvailable()
			except OptionalDependencyNotAvailable:
						pass
			else:
						from .modeling_clipseg import (
						    CLIPSEG_PRETRAINED_MODEL_ARCHIVE_LIST,
						    CLIPSegForImageSegmentation,
						    CLIPSegModel,
						    CLIPSegPreTrainedModel,
						    CLIPSegTextModel,
						    CLIPSegVisionModel,
						)

else:
			import sys

			lowerCAmelCase_     =	_LazyModule(__name__, globals()['''__file__'''], _import_structure, module_spec=__spec__)


