


"""simple docstring"""


import subprocess
import sys
import warnings
from argparse import ArgumentParser
from pathlib import Path

from packaging import version

from .. import AutoFeatureExtractor, AutoImageProcessor, AutoProcessor, AutoTokenizer
from ..utils import logging
from ..utils.import_utils import is_optimum_available
from .convert import export, validate_model_outputs
from .features import FeaturesManager
from .utils import get_preprocessor


a_											=     "1.5.0"

a_											=     ["vision-encoder-decoder"]






def   a__			(					__lowercase )						->						Tuple:
				if is_optimum_available():
								from optimum.version import __version__ as optimum_version

								_A       	=      version.parse(__lowercase )
								if parsed_optimum_version < version.parse(__lowercase ):
												raise RuntimeError(
												    f"""transformers.onnx requires optimum >= {MIN_OPTIMUM_VERSION} but {optimum_version} is installed. You """
												    "can upgrade optimum by running: pip install -U optimum[exporters]" )
				else:
								raise RuntimeError(
								    "transformers.onnx requires optimum to run, you can install the library by running: pip install "
								    "optimum[exporters]" )
				_A       	=      [
				    sys.executable,
				    "-m",
				    "optimum.exporters.onnx",
				    f"""--model {args.model}""",
				    f"""--task {args.feature}""",
				    f"""--framework {args.framework}""" if args.framework is not None else "",
				    f"""{args.output}""",
				]
				_A       	=      subprocess.Popen(" ".join(__lowercase )  ,      stdout=subprocess.PIPE  ,      shell=__lowercase )
				proc.wait()

				logger.info(
				    "The export was done by optimum.exporters.onnx. We recommend using to use this package directly in future, as "
				    "transformers.onnx is deprecated, and will be removed in v5. You can find more information here: "
				    "https://huggingface.co/docs/optimum/exporters/onnx/usage_guides/export_a_model." )






def   a__			(					__lowercase )						->						List[Any]:
				_A       	=      args.output if args.output.is_file() else args.output.joinpath("model.onnx" )
				if not args.output.parent.exists():
								args.output.parent.mkdir(parents=__lowercase )

				# Allocate the model
				_A       	=      FeaturesManager.get_model_from_feature(
				    args.feature  ,      args.model  ,      framework=args.framework  ,      cache_dir=args.cache_dir )

				_A	,		_A       	=      FeaturesManager.check_supported_model_or_raise(__lowercase  ,      feature=args.feature )
				_A       	=      model_onnx_config(model.config )

				if model_kind in ENCODER_DECODER_MODELS:
								_A       	=      model.get_encoder()
								_A       	=      model.get_decoder()

								_A       	=      onnx_config.get_encoder_config(encoder_model.config )
								_A       	=      onnx_config.get_decoder_config(
								    encoder_model.config  ,      decoder_model.config  ,      feature=args.feature )

								if args.opset is None:
												_A       	=      max(encoder_onnx_config.default_onnx_opset  ,      decoder_onnx_config.default_onnx_opset )

								if args.opset < min(encoder_onnx_config.default_onnx_opset  ,      decoder_onnx_config.default_onnx_opset ):
												raise ValueError(
												    f"""Opset {args.opset} is not sufficient to export {model_kind}. At least """
												    f""" {min(encoder_onnx_config.default_onnx_opset  ,      decoder_onnx_config.default_onnx_opset )} is required.""" )

								_A       	=      AutoFeatureExtractor.from_pretrained(args.model )

								_A	,		_A       	=      export(
								    __lowercase  ,      __lowercase  ,      __lowercase  ,      args.opset  ,      args.output.parent.joinpath("encoder_model.onnx" )  ,      )

								validate_model_outputs(
								    __lowercase  ,      __lowercase  ,      __lowercase  ,      args.output.parent.joinpath("encoder_model.onnx" )  ,      __lowercase  ,      args.atol if args.atol else encoder_onnx_config.atol_for_validation  ,      )

								_A       	=      AutoTokenizer.from_pretrained(args.model )

								_A	,		_A       	=      export(
								    __lowercase  ,      __lowercase  ,      __lowercase  ,      args.opset  ,      args.output.parent.joinpath("decoder_model.onnx" )  ,      )

								validate_model_outputs(
								    __lowercase  ,      __lowercase  ,      __lowercase  ,      args.output.parent.joinpath("decoder_model.onnx" )  ,      __lowercase  ,      args.atol if args.atol else decoder_onnx_config.atol_for_validation  ,      )
								logger.info(
								    f"""All good, model saved at: {args.output.parent.joinpath('encoder_model.onnx' ).as_posix()},"""
								    f""" {args.output.parent.joinpath('decoder_model.onnx' ).as_posix()}""" )

				else:
								# Instantiate the appropriate preprocessor
								if args.preprocessor == "auto":
												_A       	=      get_preprocessor(args.model )
								elif args.preprocessor == "tokenizer":
												_A       	=      AutoTokenizer.from_pretrained(args.model )
								elif args.preprocessor == "image_processor":
												_A       	=      AutoImageProcessor.from_pretrained(args.model )
								elif args.preprocessor == "feature_extractor":
												_A       	=      AutoFeatureExtractor.from_pretrained(args.model )
								elif args.preprocessor == "processor":
												_A       	=      AutoProcessor.from_pretrained(args.model )
								else:
												raise ValueError(f"""Unknown preprocessor type \'{args.preprocessor}\'""" )

								# Ensure the requested opset is sufficient
								if args.opset is None:
												_A       	=      onnx_config.default_onnx_opset

								if args.opset < onnx_config.default_onnx_opset:
												raise ValueError(
												    f"""Opset {args.opset} is not sufficient to export {model_kind}. """
												    f"""At least  {onnx_config.default_onnx_opset} is required.""" )

								_A	,		_A       	=      export(
								    __lowercase  ,      __lowercase  ,      __lowercase  ,      args.opset  ,      args.output  ,      )

								if args.atol is None:
												_A       	=      onnx_config.atol_for_validation

								validate_model_outputs(__lowercase  ,      __lowercase  ,      __lowercase  ,      args.output  ,      __lowercase  ,      args.atol )
								logger.info(f"""All good, model saved at: {args.output.as_posix()}""" )
								warnings.warn(
								    "The export was done by transformers.onnx which is deprecated and will be removed in v5. We recommend"
								    " using optimum.exporters.onnx in future. You can find more information here:"
								    " https://huggingface.co/docs/optimum/exporters/onnx/usage_guides/export_a_model."  ,      __lowercase  ,      )






def   a__			(					)						->						Union[str, Any]:
				_A       	=      ArgumentParser("Hugging Face Transformers ONNX exporter" )
				parser.add_argument(
				    "-m"  ,      "--model"  ,      type=__lowercase  ,      required=__lowercase  ,      help="Model ID on huggingface.co or path on disk to load model from." )
				parser.add_argument(
				    "--feature"  ,      default="default"  ,      help="The type of features to export the model with."  ,      )
				parser.add_argument("--opset"  ,      type=__lowercase  ,      default=__lowercase  ,      help="ONNX opset version to export the model with." )
				parser.add_argument(
				    "--atol"  ,      type=__lowercase  ,      default=__lowercase  ,      help="Absolute difference tolerance when validating the model." )
				parser.add_argument(
				    "--framework"  ,      type=__lowercase  ,      choices=["pt", "tf"]  ,      default=__lowercase  ,      help=(
				        "The framework to use for the ONNX export."
				        " If not provided, will attempt to use the local checkpoint's original framework"
				        " or what is available in the environment."
				    )  ,      )
				parser.add_argument("output"  ,      type=__lowercase  ,      help="Path indicating where to store generated ONNX model." )
				parser.add_argument("--cache_dir"  ,      type=__lowercase  ,      default=__lowercase  ,      help="Path indicating where to store cache." )
				parser.add_argument(
				    "--preprocessor"  ,      type=__lowercase  ,      choices=["auto", "tokenizer", "feature_extractor", "image_processor", "processor"]  ,      default="auto"  ,      help="Which type of preprocessor to use. 'auto' tries to automatically detect it."  ,      )
				parser.add_argument(
				    "--export_with_transformers"  ,      action="store_true"  ,      help=(
				        "Whether to use transformers.onnx instead of optimum.exporters.onnx to perform the ONNX export. It can be "
				        "useful when exporting a model supported in transformers but not in optimum, otherwise it is not "
				        "recommended."
				    )  ,      )

				_A       	=      parser.parse_args()
				if args.export_with_transformers or not is_optimum_available():
								export_with_transformers(__lowercase )
				else:
								export_with_optimum(__lowercase )


if __name__ == "__main__":
		a_											=     logging.get_logger("transformers.onnx")  # pylint: disable=invalid-name
		logger.setLevel(logging.INFO)
		main()





"""simple docstring"""

import tempfile
import unittest

from transformers import DPRConfig, is_torch_available
from transformers.testing_utils import require_torch, slow, torch_device

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


if is_torch_available():
		import torch

		from transformers import DPRContextEncoder, DPRQuestionEncoder, DPRReader, DPRReaderTokenizer
		from transformers.models.dpr.modeling_dpr import (
		    DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
		    DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST,
		    DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST,
		)
class      A__   :




	def __init__( self	:						Optional[int]   ,     lowercase	:						List[Any]   ,     lowercase	:						Optional[int]=13   ,     lowercase	:						List[Any]=7   ,     lowercase	:						Any=False   ,     lowercase	:						Optional[int]=True   ,     lowercase	:						Any=True   ,     lowercase	:						Optional[Any]=True   ,     lowercase	:						Tuple=99   ,     lowercase	:						Union[str, Any]=32   ,     lowercase	:						Any=5   ,     lowercase	:						Dict=4   ,     lowercase	:						Dict=37   ,     lowercase	:						Tuple="gelu"   ,     lowercase	:						str=0.1   ,     lowercase	:						Union[str, Any]=0.1   ,     lowercase	:						Any=512   ,     lowercase	:						Optional[Any]=16   ,     lowercase	:						Any=2   ,     lowercase	:						List[str]=0.02   ,     lowercase	:						List[str]=3   ,     lowercase	:						Optional[int]=4   ,     lowercase	:						Any=None   ,     lowercase	:						Optional[int]=0   ,     )       ->			Optional[Any]:
						__snake_case        =						parent
						__snake_case        =						batch_size
						__snake_case        =						seq_length
						__snake_case        =						is_training
						__snake_case        =						use_input_mask
						__snake_case        =						use_token_type_ids
						__snake_case        =						use_labels
						__snake_case        =						vocab_size
						__snake_case        =						hidden_size
						__snake_case        =						num_hidden_layers
						__snake_case        =						num_attention_heads
						__snake_case        =						intermediate_size
						__snake_case        =						hidden_act
						__snake_case        =						hidden_dropout_prob
						__snake_case        =						attention_probs_dropout_prob
						__snake_case        =						max_position_embeddings
						__snake_case        =						type_vocab_size
						__snake_case        =						type_sequence_label_size
						__snake_case        =						initializer_range
						__snake_case        =						num_labels
						__snake_case        =						num_choices
						__snake_case        =						scope
						__snake_case        =						projection_dim




	def 				a_		( self	:						str						)       ->			Optional[Any]:
						__snake_case        =						ids_tensor([self.batch_size, self.seq_length]   ,     self.vocab_size						)

						__snake_case        =						None
						if self.use_input_mask:
											__snake_case        =						random_attention_mask([self.batch_size, self.seq_length]						)

						__snake_case        =						None
						if self.use_token_type_ids:
											__snake_case        =						ids_tensor([self.batch_size, self.seq_length]   ,     self.type_vocab_size						)

						__snake_case        =						None
						__snake_case        =						None
						__snake_case        =						None
						if self.use_labels:
											__snake_case        =						ids_tensor([self.batch_size]   ,     self.type_sequence_label_size						)
											__snake_case        =						ids_tensor([self.batch_size, self.seq_length]   ,     self.num_labels						)
											__snake_case        =						ids_tensor([self.batch_size]   ,     self.num_choices						)

						__snake_case        =						self.get_config()

						return config, input_ids, token_type_ids, input_mask, sequence_labels, token_labels, choice_labels




	def 				a_		( self	:						str						)       ->			str:
						return DPRConfig(
						    projection_dim=self.projection_dim   ,     vocab_size=self.vocab_size   ,     hidden_size=self.hidden_size   ,     num_hidden_layers=self.num_hidden_layers   ,     num_attention_heads=self.num_attention_heads   ,     intermediate_size=self.intermediate_size   ,     hidden_act=self.hidden_act   ,     hidden_dropout_prob=self.hidden_dropout_prob   ,     attention_probs_dropout_prob=self.attention_probs_dropout_prob   ,     max_position_embeddings=self.max_position_embeddings   ,     type_vocab_size=self.type_vocab_size   ,     initializer_range=self.initializer_range   ,     )




	def 				a_		( self	:						Optional[Any]   ,     lowercase	:						str   ,     lowercase	:						Optional[Any]   ,     lowercase	:						str   ,     lowercase	:						Dict   ,     lowercase	:						Union[str, Any]   ,     lowercase	:						Any   ,     lowercase	:						List[str]						)       ->			Optional[Any]:
						__snake_case        =						DPRContextEncoder(config=lowercase						)
						model.to(lowercase						)
						model.eval()
						__snake_case        =						model(lowercase   ,     attention_mask=lowercase   ,     token_type_ids=lowercase						)
						__snake_case        =						model(lowercase   ,     token_type_ids=lowercase						)
						__snake_case        =						model(lowercase						)
						self.parent.assertEqual(result.pooler_output.shape   ,     (self.batch_size, self.projection_dim or self.hidden_size)						)




	def 				a_		( self	:						Tuple   ,     lowercase	:						Tuple   ,     lowercase	:						Union[str, Any]   ,     lowercase	:						List[str]   ,     lowercase	:						Optional[int]   ,     lowercase	:						Any   ,     lowercase	:						Tuple   ,     lowercase	:						Union[str, Any]						)       ->			Optional[int]:
						__snake_case        =						DPRQuestionEncoder(config=lowercase						)
						model.to(lowercase						)
						model.eval()
						__snake_case        =						model(lowercase   ,     attention_mask=lowercase   ,     token_type_ids=lowercase						)
						__snake_case        =						model(lowercase   ,     token_type_ids=lowercase						)
						__snake_case        =						model(lowercase						)
						self.parent.assertEqual(result.pooler_output.shape   ,     (self.batch_size, self.projection_dim or self.hidden_size)						)




	def 				a_		( self	:						str   ,     lowercase	:						List[Any]   ,     lowercase	:						int   ,     lowercase	:						Optional[Any]   ,     lowercase	:						Any   ,     lowercase	:						Tuple   ,     lowercase	:						Optional[Any]   ,     lowercase	:						int						)       ->			str:
						__snake_case        =						DPRReader(config=lowercase						)
						model.to(lowercase						)
						model.eval()
						__snake_case        =						model(
						    lowercase   ,     attention_mask=lowercase   ,     )

						self.parent.assertEqual(result.start_logits.shape   ,     (self.batch_size, self.seq_length)						)
						self.parent.assertEqual(result.end_logits.shape   ,     (self.batch_size, self.seq_length)						)
						self.parent.assertEqual(result.relevance_logits.shape   ,     (self.batch_size,)						)




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

@require_torch
class      A__   (   lowercase_  ,						lowercase_  ,						unittest.TestCase  ):
	A_				:					Dict      =   (
	    (
	        DPRContextEncoder,
	        DPRQuestionEncoder,
	        DPRReader,
	    )
	    if is_torch_available()
	    else ()
	)
	A_				:					Any      =   {"""feature-extraction""": DPRQuestionEncoder} if is_torch_available() else {}

	A_				:					List[Any]      =   False
	A_				:					Union[str, Any]      =   False  # why?
	A_				:					Any      =   False
	A_				:					List[str]      =   False




	def 				a_		( self	:						Union[str, Any]						)       ->			Dict:
						__snake_case        =						DPRModelTester(self						)
						__snake_case        =						ConfigTester(self   ,     config_class=lowercase   ,     hidden_size=37						)




	def 				a_		( self	:						Optional[int]						)       ->			Optional[int]:
						self.config_tester.run_common_tests()




	def 				a_		( self	:						Dict						)       ->			List[str]:
						__snake_case        =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_context_encoder(*lowercase						)




	def 				a_		( self	:						Any						)       ->			Tuple:
						__snake_case        =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_question_encoder(*lowercase						)




	def 				a_		( self	:						Any						)       ->			Optional[int]:
						__snake_case        =						self.model_tester.prepare_config_and_inputs()
						self.model_tester.create_and_check_reader(*lowercase						)




	def 				a_		( self	:						Tuple						)       ->			str:
						__snake_case        =						self.model_tester.prepare_config_and_inputs()[0]

						__snake_case        =						DPRQuestionEncoder(config=lowercase						)
						model.to(lowercase						)
						model.eval()

						with tempfile.TemporaryDirectory() as tmp_dirname:
											model.save_pretrained(lowercase						)
											__snake_case        =						DPRQuestionEncoder.from_pretrained(lowercase   ,     projection_dim=512						)

						self.assertIsNotNone(lowercase						)




	@slow
	def 				a_		( self	:						Any						)       ->			Optional[Any]:
						for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__snake_case        =						DPRContextEncoder.from_pretrained(lowercase						)
											self.assertIsNotNone(lowercase						)

						for model_name in DPR_CONTEXT_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__snake_case        =						DPRContextEncoder.from_pretrained(lowercase						)
											self.assertIsNotNone(lowercase						)

						for model_name in DPR_QUESTION_ENCODER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__snake_case        =						DPRQuestionEncoder.from_pretrained(lowercase						)
											self.assertIsNotNone(lowercase						)

						for model_name in DPR_READER_PRETRAINED_MODEL_ARCHIVE_LIST[:1]:
											__snake_case        =						DPRReader.from_pretrained(lowercase						)
											self.assertIsNotNone(lowercase						)

@require_torch
class      A__   (   unittest.TestCase  ):




	@slow
	def 				a_		( self	:						List[str]						)       ->			Union[str, Any]:
						__snake_case        =						DPRQuestionEncoder.from_pretrained("""facebook/dpr-question_encoder-single-nq-base"""   ,     return_dict=lowercase						)
						model.to(lowercase						)

						__snake_case        =						torch.tensor(
						    [[101, 7_592, 1_010, 2_003, 2_026, 3_899, 10_140, 1_029, 102]]   ,     dtype=torch.long   ,     device=lowercase						)  # [CLS] hello, is my dog cute? [SEP]
						__snake_case        =						model(lowercase						)[0]  # embedding shape = (1, 768)
						# compare the actual values for a slice.
						__snake_case        =						torch.tensor(
						    [
						        [
						            0.03_23_62_53,
						            0.12_75_33_35,
						            0.16_81_85_09,
						            0.00_27_97_86,
						            0.3_89_69_33,
						            0.24_26_49_45,
						            0.2_17_89_71,
						            -0.02_33_52_27,
						            -0.08_48_19_59,
						            -0.14_32_41_17,
						        ]
						    ]   ,     dtype=torch.float   ,     device=lowercase   ,     )
						self.assertTrue(torch.allclose(output[:, :10]   ,     lowercase   ,     atol=1e-4						)						)




	@slow
	def 				a_		( self	:						Tuple						)       ->			int:
						__snake_case        =						DPRReaderTokenizer.from_pretrained("""facebook/dpr-reader-single-nq-base"""						)
						__snake_case        =						DPRReader.from_pretrained("""facebook/dpr-reader-single-nq-base"""						)
						model.to(lowercase						)

						__snake_case        =						tokenizer(
						    questions="""What is love ?"""   ,     titles="""Haddaway"""   ,     texts="""What Is Love is a song recorded by the artist Haddaway"""   ,     padding=lowercase   ,     return_tensors="""pt"""   ,     )
						encoded_inputs.to(lowercase						)

						__snake_case        =						model(**lowercase						)

						# compare the actual values for a slice.
						__snake_case        =						torch.tensor(
						    [[-10.30_05, -10.77_65, -11.48_72, -11.68_41, -11.93_12, -10.30_02, -9.85_44, -11.73_78, -12.08_21, -10.29_75]]   ,     dtype=torch.float   ,     device=lowercase   ,     )

						__snake_case        =						torch.tensor(
						    [[-11.06_84, -11.70_41, -11.53_97, -10.34_65, -10.87_91, -6.84_43, -11.99_59, -11.03_64, -10.00_96, -6.84_05]]   ,     dtype=torch.float   ,     device=lowercase   ,     )
						self.assertTrue(torch.allclose(outputs.start_logits[:, :10]   ,     lowercase   ,     atol=1e-4						)						)
						self.assertTrue(torch.allclose(outputs.end_logits[:, :10]   ,     lowercase   ,     atol=1e-4						)						)


