

import inspect
import unittest

from transformers import SegformerConfig, is_torch_available, is_vision_available
from transformers.models.auto import get_values
from transformers.testing_utils import require_torch, slow, torch_device

from ...test_configuration_common import ConfigTester
from ...test_modeling_common import ModelTesterMixin, floats_tensor, ids_tensor
from ...test_pipeline_mixin import PipelineTesterMixin


if is_torch_available():
	import torch

	from transformers import (
	    MODEL_MAPPING,
	    SegformerForImageClassification,
	    SegformerForSemanticSegmentation,
	    SegformerModel,
	)
	from transformers.models.segformer.modeling_segformer import SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST


if is_vision_available():
	from PIL import Image

	from transformers import SegformerImageProcessor



class        A__(_A							):
						"""simple docstring"""






						def 			UpperCamelCase__					(    self    )     -> str:
									a_						:						List[str] 							= self.config_class(**self.inputs_dict    )
									self.parent.assertTrue(hasattr(a_  ,      """hidden_sizes"""    )    )
									self.parent.assertTrue(hasattr(a_  ,      """num_attention_heads"""    )    )
									self.parent.assertTrue(hasattr(a_  ,      """num_encoder_blocks"""    )    )







class        A__:
						"""simple docstring"""






						def __init__(    self  ,      _lowercase  ,      _lowercase=13  ,      _lowercase=64  ,      _lowercase=3  ,      _lowercase=4  ,      _lowercase=[2, 2, 2, 2]  ,      _lowercase=[8, 4, 2, 1]  ,      _lowercase=[16, 32, 64, 128]  ,      _lowercase=[1, 4, 8, 16]  ,      _lowercase=[1, 2, 4, 8]  ,      _lowercase=True  ,      _lowercase=True  ,      _lowercase="gelu"  ,      _lowercase=0.1  ,      _lowercase=0.1  ,      _lowercase=0.0_2  ,      _lowercase=3  ,      _lowercase=None  ,      )     -> Any:
									a_						:						Optional[Any] 							= parent
									a_						:						str 							= batch_size
									a_						:						int 							= image_size
									a_						:						Tuple 							= num_channels
									a_						:						Union[str, Any] 							= num_encoder_blocks
									a_						:						Tuple 							= sr_ratios
									a_						:						Any 							= depths
									a_						:						List[Any] 							= hidden_sizes
									a_						:						List[str] 							= downsampling_rates
									a_						:						Dict 							= num_attention_heads
									a_						:						Any 							= is_training
									a_						:						Dict 							= use_labels
									a_						:						List[Any] 							= hidden_act
									a_						:						List[str] 							= hidden_dropout_prob
									a_						:						Dict 							= attention_probs_dropout_prob
									a_						:						Union[str, Any] 							= initializer_range
									a_						:						List[str] 							= num_labels
									a_						:						int 							= scope





						def 			UpperCamelCase__					(    self    )     -> Dict:
									a_						:						Union[str, Any] 							= floats_tensor([self.batch_size, self.num_channels, self.image_size, self.image_size]    )

									a_						:						str 							= None
									if self.use_labels:
												a_						:						Any 							= ids_tensor([self.batch_size, self.image_size, self.image_size]  ,      self.num_labels    )

									a_						:						int 							= self.get_config()
									return config, pixel_values, labels





						def 			UpperCamelCase__					(    self    )     -> List[str]:
									return SegformerConfig(
									    image_size=self.image_size  ,      num_channels=self.num_channels  ,      num_encoder_blocks=self.num_encoder_blocks  ,      depths=self.depths  ,      hidden_sizes=self.hidden_sizes  ,      num_attention_heads=self.num_attention_heads  ,      hidden_act=self.hidden_act  ,      hidden_dropout_prob=self.hidden_dropout_prob  ,      attention_probs_dropout_prob=self.attention_probs_dropout_prob  ,      initializer_range=self.initializer_range  ,      )





						def 			UpperCamelCase__					(    self  ,      _lowercase  ,      _lowercase  ,      _lowercase    )     -> int:
									a_						:						Optional[int] 							= SegformerModel(config=a_    )
									model.to(a_    )
									model.eval()
									a_						:						Tuple 							= model(a_    )
									a_						:						str 							= self.image_size // (self.downsampling_rates[-1] * 2)
									self.parent.assertEqual(
									    result.last_hidden_state.shape  ,      (self.batch_size, self.hidden_sizes[-1], expected_height, expected_width)    )





						def 			UpperCamelCase__					(    self  ,      _lowercase  ,      _lowercase  ,      _lowercase    )     -> Optional[Any]:
									a_						:						Dict 							= self.num_labels
									a_						:						Any 							= SegformerForSemanticSegmentation(a_    )
									model.to(a_    )
									model.eval()
									a_						:						Any 							= model(a_    )
									self.parent.assertEqual(
									    result.logits.shape  ,      (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)    )
									a_						:						Any 							= model(a_  ,      labels=a_    )
									self.parent.assertEqual(
									    result.logits.shape  ,      (self.batch_size, self.num_labels, self.image_size // 4, self.image_size // 4)    )
									self.parent.assertGreater(result.loss  ,      0.0    )





						def 			UpperCamelCase__					(    self  ,      _lowercase  ,      _lowercase  ,      _lowercase    )     -> Any:
									a_						:						Optional[int] 							= 1
									a_						:						Union[str, Any] 							= SegformerForSemanticSegmentation(config=a_    )
									model.to(a_    )
									model.eval()
									a_						:						Dict 							= torch.randint(0  ,      1  ,      (self.batch_size, self.image_size, self.image_size)    ).to(a_    )
									a_						:						Optional[int] 							= model(a_  ,      labels=a_    )
									self.parent.assertGreater(result.loss  ,      0.0    )





						def 			UpperCamelCase__					(    self    )     -> List[Any]:
									a_						:						List[str] 							= self.prepare_config_and_inputs()
									a_						:						Optional[Any] 							= config_and_inputs
									a_						:						List[str] 							= {"pixel_values": pixel_values}
									return config, inputs_dict







@require_torch
class        A__(_A,  _A,  unittest.TestCase							):
						"""simple docstring"""






						_A    :				List[str]       					=			(
						    (
						        SegformerModel,
						        SegformerForSemanticSegmentation,
						        SegformerForImageClassification,
						    )
						    if is_torch_available()
						    else ()
						)
						_A    :				Union[str, Any]       					=			(
						    {
						        '''feature-extraction''': SegformerModel,
						        '''image-classification''': SegformerForImageClassification,
						        '''image-segmentation''': SegformerForSemanticSegmentation,
						    }
						    if is_torch_available()
						    else {}
						)

						_A    :				List[Any]       					=			True
						_A    :				Dict       					=			False
						_A    :				Dict       					=			False
						_A    :				Optional[int]       					=			False





						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									a_						:						Optional[Any] 							= SegformerModelTester(self    )
									a_						:						Optional[int] 							= SegformerConfigTester(self  ,      config_class=a_    )





						def 			UpperCamelCase__					(    self    )     -> List[str]:
									self.config_tester.run_common_tests()





						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									a_						:						Dict 							= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_model(*a_    )





						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									a_						:						Optional[int] 							= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_binary_image_segmentation(*a_    )





						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									a_						:						Dict 							= self.model_tester.prepare_config_and_inputs()
									self.model_tester.create_and_check_for_image_segmentation(*a_    )





						@unittest.skip("""SegFormer does not use inputs_embeds"""    )
						def 			UpperCamelCase__					(    self    )     -> List[str]:
									pass





						@unittest.skip("""SegFormer does not have get_input_embeddings method and get_output_embeddings methods"""    )
						def 			UpperCamelCase__					(    self    )     -> Tuple:
									pass





						def 			UpperCamelCase__					(    self    )     -> Union[str, Any]:
									a_						:						Optional[Any] 							= self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
												a_						:						Optional[int] 							= model_class(a_    )
												a_						:						Optional[int] 							= inspect.signature(model.forward    )
												# signature.parameters is an OrderedDict => so arg_names order is deterministic
												a_						:						Optional[int] 							= [*signature.parameters.keys()]

												a_						:						List[Any] 							= ["pixel_values"]
												self.assertListEqual(arg_names[:1]  ,      a_    )





						def 			UpperCamelCase__					(    self    )     -> Union[str, Any]:
									a_						:						List[str] 							= self.model_tester.prepare_config_and_inputs_for_common()
									a_						:						List[str] 							= True

									for model_class in self.all_model_classes:
												a_						:						Any 							= True
												a_						:						str 							= False
												a_						:						Dict 							= True
												a_						:						Dict 							= model_class(a_    )
												model.to(a_    )
												model.eval()
												with torch.no_grad():
															a_						:						Optional[int] 							= model(**self._prepare_for_class(a_  ,      a_    )    )
												a_						:						Optional[Any] 							= outputs.attentions

												a_						:						List[Any] 							= sum(self.model_tester.depths    )
												self.assertEqual(len(a_    )  ,      a_    )

												# check that output_attentions also work using config
												del inputs_dict["output_attentions"]
												a_						:						Union[str, Any] 							= True
												a_						:						Optional[int] 							= model_class(a_    )
												model.to(a_    )
												model.eval()
												with torch.no_grad():
															a_						:						int 							= model(**self._prepare_for_class(a_  ,      a_    )    )
												a_						:						Optional[int] 							= outputs.attentions

												self.assertEqual(len(a_    )  ,      a_    )

												# verify the first attentions (first block, first layer)
												a_						:						List[Any] 							= (self.model_tester.image_size // 4) ** 2
												a_						:						List[Any] 							= (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
												self.assertListEqual(
												    list(attentions[0].shape[-3:]    )  ,      [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]  ,      )

												# verify the last attentions (last block, last layer)
												a_						:						Dict 							= (self.model_tester.image_size // 32) ** 2
												a_						:						Dict 							= (self.model_tester.image_size // (32 * self.model_tester.sr_ratios[-1])) ** 2
												self.assertListEqual(
												    list(attentions[-1].shape[-3:]    )  ,      [self.model_tester.num_attention_heads[-1], expected_seq_len, expected_reduced_seq_len]  ,      )
												a_						:						str 							= len(a_    )

												# Check attention is always last and order is fine
												a_						:						Any 							= True
												a_						:						List[Any] 							= True
												a_						:						List[Any] 							= model_class(a_    )
												model.to(a_    )
												model.eval()
												with torch.no_grad():
															a_						:						Any 							= model(**self._prepare_for_class(a_  ,      a_    )    )

												self.assertEqual(out_len + 1  ,      len(a_    )    )

												a_						:						int 							= outputs.attentions

												self.assertEqual(len(a_    )  ,      a_    )
												# verify the first attentions (first block, first layer)
												a_						:						Union[str, Any] 							= (self.model_tester.image_size // 4) ** 2
												a_						:						Optional[int] 							= (self.model_tester.image_size // (4 * self.model_tester.sr_ratios[0])) ** 2
												self.assertListEqual(
												    list(self_attentions[0].shape[-3:]    )  ,      [self.model_tester.num_attention_heads[0], expected_seq_len, expected_reduced_seq_len]  ,      )





						def 			UpperCamelCase__					(    self    )     -> Any:
									def check_hidden_states_output(_lowercase  ,      _lowercase  ,      _lowercase    ):
												a_						:						int 							= model_class(a_    )
												model.to(a_    )
												model.eval()

												with torch.no_grad():
															a_						:						Optional[Any] 							= model(**self._prepare_for_class(a_  ,      a_    )    )

												a_						:						Optional[int] 							= outputs.hidden_states

												a_						:						Tuple 							= self.model_tester.num_encoder_blocks
												self.assertEqual(len(a_    )  ,      a_    )

												# verify the first hidden states (first block)
												self.assertListEqual(
												    list(hidden_states[0].shape[-3:]    )  ,      [
												        self.model_tester.hidden_sizes[0],
												        self.model_tester.image_size // 4,
												        self.model_tester.image_size // 4,
												    ]  ,      )

									a_						:						Tuple 							= self.model_tester.prepare_config_and_inputs_for_common()

									for model_class in self.all_model_classes:
												a_						:						Tuple 							= True
												check_hidden_states_output(a_  ,      a_  ,      a_    )

												# check that output_hidden_states also work using config
												del inputs_dict["output_hidden_states"]
												a_						:						List[Any] 							= True

												check_hidden_states_output(a_  ,      a_  ,      a_    )





						def 			UpperCamelCase__					(    self    )     -> List[Any]:
									if not self.model_tester.is_training:
												return

									a_						:						Optional[int] 							= self.model_tester.prepare_config_and_inputs_for_common()
									a_						:						List[Any] 							= True

									for model_class in self.all_model_classes:
												if model_class in get_values(a_    ):
															continue

												a_						:						Optional[Any] 							= model_class(a_    )
												model.to(a_    )
												model.train()
												a_						:						Union[str, Any] 							= self._prepare_for_class(a_  ,      a_  ,      return_labels=a_    )
												a_						:						Optional[Any] 							= model(**a_    ).loss
												loss.backward()





						@unittest.skip("""Will be fixed soon by reducing the size of the model used for common tests."""    )
						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									pass





						@slow
						def 			UpperCamelCase__					(    self    )     -> int:
									for model_name in SEGFORMER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
												a_						:						Any 							= SegformerModel.from_pretrained(a_    )
												self.assertIsNotNone(a_    )







def 					_UpperCAmelCase				(						):

			'''simple docstring'''



			a_						:						Optional[Any] 							= Image.open("""./tests/fixtures/tests_samples/COCO/000000039769.png""")
			return image



@require_torch
class        A__(unittest.TestCase							):
						"""simple docstring"""






						@slow
						def 			UpperCamelCase__					(    self    )     -> int:
									# only resize + normalize
									a_						:						int 							= SegformerImageProcessor(
									    image_scale=(512, 512)  ,      keep_ratio=a_  ,      align=a_  ,      do_random_crop=a_    )
									a_						:						str 							= SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512"""    ).to(
									    a_    )

									a_						:						List[Any] 							= prepare_img()
									a_						:						Any 							= image_processor(images=a_  ,      return_tensors="""pt"""    )
									a_						:						Optional[int] 							= encoded_inputs.pixel_values.to(a_    )

									with torch.no_grad():
												a_						:						Union[str, Any] 							= model(a_    )

									a_						:						Union[str, Any] 							= torch.Size((1, model.config.num_labels, 128, 128)    )
									self.assertEqual(outputs.logits.shape  ,      a_    )

									a_						:						Optional[Any] 							= torch.tensor(
									    [
									        [[-4.6_3_1_0, -5.5_2_3_2, -6.2_3_5_6], [-5.1_9_2_1, -6.1_4_4_4, -6.5_9_9_6], [-5.4_4_2_4, -6.2_7_9_0, -6.7_5_7_4]],
									        [[-1_2.1_3_9_1, -1_3.3_1_2_2, -1_3.9_5_5_4], [-1_2.8_7_3_2, -1_3.9_3_5_2, -1_4.3_5_6_3], [-1_2.9_4_3_8, -1_3.8_2_2_6, -1_4.2_5_1_3]],
									        [[-1_2.5_1_3_4, -1_3.4_6_8_6, -1_4.4_9_1_5], [-1_2.8_6_6_9, -1_4.4_3_4_3, -1_4.7_7_5_8], [-1_3.2_5_2_3, -1_4.5_8_1_9, -1_5.0_6_9_4]],
									    ]    ).to(a_    )
									self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]  ,      a_  ,      atol=1e-4    )    )





						@slow
						def 			UpperCamelCase__					(    self    )     -> Optional[int]:
									# only resize + normalize
									a_						:						Tuple 							= SegformerImageProcessor(
									    image_scale=(512, 512)  ,      keep_ratio=a_  ,      align=a_  ,      do_random_crop=a_    )
									a_						:						Dict 							= SegformerForSemanticSegmentation.from_pretrained(
									    """nvidia/segformer-b1-finetuned-cityscapes-1024-1024"""    ).to(a_    )

									a_						:						Optional[Any] 							= prepare_img()
									a_						:						Any 							= image_processor(images=a_  ,      return_tensors="""pt"""    )
									a_						:						Tuple 							= encoded_inputs.pixel_values.to(a_    )

									with torch.no_grad():
												a_						:						Any 							= model(a_    )

									a_						:						Optional[Any] 							= torch.Size((1, model.config.num_labels, 128, 128)    )
									self.assertEqual(outputs.logits.shape  ,      a_    )

									a_						:						str 							= torch.tensor(
									    [
									        [[-1_3.5_7_4_8, -1_3.9_1_1_1, -1_2.6_5_0_0], [-1_4.3_5_0_0, -1_5.3_6_8_3, -1_4.2_3_2_8], [-1_4.7_5_3_2, -1_6.0_4_2_4, -1_5.6_0_8_7]],
									        [[-1_7.1_6_5_1, -1_5.8_7_2_5, -1_2.9_6_5_3], [-1_7.2_5_8_0, -1_7.3_7_1_8, -1_4.8_2_2_3], [-1_6.6_0_5_8, -1_6.8_7_8_3, -1_6.7_4_5_2]],
									        [[-3.6_4_5_6, -3.0_2_0_9, -1.4_2_0_3], [-3.0_7_9_7, -3.1_9_5_9, -2.0_0_0_0], [-1.8_7_5_7, -1.9_2_1_7, -1.6_9_9_7]],
									    ]    ).to(a_    )
									self.assertTrue(torch.allclose(outputs.logits[0, :3, :3, :3]  ,      a_  ,      atol=1e-1    )    )





						@slow
						def 			UpperCamelCase__					(    self    )     -> Union[str, Any]:
									# only resize + normalize
									a_						:						List[str] 							= SegformerImageProcessor(
									    image_scale=(512, 512)  ,      keep_ratio=a_  ,      align=a_  ,      do_random_crop=a_    )
									a_						:						int 							= SegformerForSemanticSegmentation.from_pretrained("""nvidia/segformer-b0-finetuned-ade-512-512"""    ).to(
									    a_    )

									a_						:						str 							= prepare_img()
									a_						:						Optional[int] 							= image_processor(images=a_  ,      return_tensors="""pt"""    )
									a_						:						Optional[Any] 							= encoded_inputs.pixel_values.to(a_    )

									with torch.no_grad():
												a_						:						Tuple 							= model(a_    )

									a_						:						Dict 							= outputs.logits.detach().cpu()

									a_						:						List[Any] 							= image_processor.post_process_semantic_segmentation(outputs=a_  ,      target_sizes=[(500, 300)]    )
									a_						:						Dict 							= torch.Size((500, 300)    )
									self.assertEqual(segmentation[0].shape  ,      a_    )

									a_						:						Optional[int] 							= image_processor.post_process_semantic_segmentation(outputs=a_    )
									a_						:						Union[str, Any] 							= torch.Size((128, 128)    )
									self.assertEqual(segmentation[0].shape  ,      a_    )









'''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 		lowerCamelCase	(   _A							):



	def __init__( self		,   *a_		,   a_=None		,   a_=None		,   a_=None		,   **a_						):
			super().__init__(*a_		,   **a_						)
			lowerCAmelCase  :     Tuple       						= eval_examples
			lowerCAmelCase  :     List[Any]       						= post_process_function
			lowerCAmelCase  :     List[Any]       						= quant_trainer_args
			lowerCAmelCase  :     Dict       						= 128  # default number of calibration samples



	def 	_lowerCamelCase     ( self		,   a_=None						):
			if calib_dataset is None and self.calib_dataset is None:
					raise ValueError("Trainer: calibration requires an calib_dataset."						)
			lowerCAmelCase  :     List[str]       						= calib_dataset if calib_dataset is not None else self.calib_dataset

			lowerCAmelCase  :     Union[str, 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 	_lowerCamelCase     ( self		,   a_=None						):
			lowerCAmelCase  :     str       						= self.train_dataset if calib_dataset is None else calib_dataset
			lowerCAmelCase  :     str       						= self.get_calib_dataloader(a_						)

			lowerCAmelCase  :     Tuple       						= 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
					lowerCAmelCase						,		lowerCAmelCase						,		lowerCAmelCase  :     Tuple       						= 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						)
			lowerCAmelCase  :     Union[str, Any]       						= model



	def 	_lowerCamelCase     ( self		,   a_=None		,   a_=None		,   a_=None		,   a_ = "eval"						):
			lowerCAmelCase  :     Optional[Any]       						= self.eval_dataset if eval_dataset is None else eval_dataset
			lowerCAmelCase  :     Optional[Any]       						= self.get_eval_dataloader(a_						)
			lowerCAmelCase  :     Union[str, Any]       						= self.eval_examples if eval_examples is None else eval_examples

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

			if self.post_process_function is not None and self.compute_metrics is not None:
					lowerCAmelCase  :     Optional[Any]       						= self.post_process_function(a_		,   a_		,   output.predictions						)
					lowerCAmelCase  :     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}_'''						):
									lowerCAmelCase  :     Any       						= metrics.pop(a_						)

					self.log(a_						)
			else:
					lowerCAmelCase  :     str       						= {}

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

			lowerCAmelCase  :     Dict       						= self.callback_handler.on_evaluate(self.args		,   self.state		,   self.control		,   a_						)
			return metrics



	def 	_lowerCamelCase     ( self		,   a_		,   a_		,   a_=None		,   a_ = "test"						):
			lowerCAmelCase  :     int       						= self.get_test_dataloader(a_						)

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

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

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

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





	def 	_lowerCamelCase     ( self		,   a_="./"						):
			lowerCAmelCase  :     Optional[Any]       						= self.eval_dataset
			lowerCAmelCase  :     Optional[int]       						= self.get_eval_dataloader(a_						)

			lowerCAmelCase  :     Union[str, Any]       						= next(iter(a_						)						)

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

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

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

			lowerCAmelCase  :     Any       						= True

			lowerCAmelCase  :     Dict       						= self.model.to(a_						)

			model.eval()
			model.float()

			lowerCAmelCase  :     Union[str, Any]       						= model.module if hasattr(a_		,   "module"						) else model
			quant_trainer.configure_model(a_		,   self.quant_trainer_args						)

			lowerCAmelCase  :     Any       						= os.path.join(a_		,   "model.onnx"						)
			logger.info(F'''exporting model to {output_model_file}'''						)

			lowerCAmelCase  :     List[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"						)



