class_docstr: Handles the ONNX Runtime quantization process for models shared on huggingface.co/models.
class_docstr_tok: ["Handles","the","ONNX","Runtime","quantization","process","for","models","shared","on","huggingface.co\/models","."]
docstr: Quantizes a model given the optimization specifications defined in `quantization_config`.
  
  Args:
      quantization_config (`QuantizationConfig`):
          The configuration containing the parameters related to quantization.
      save_dir (`Union[str, Path]`):
          The directory where the quantized model should be saved.
      file_suffix (`Optional[str]`, defaults to `"quantized"`):
          The file_suffix used to save the quantized model.
      calibration_tensors_range (`Optional[Dict[str, Tuple[float, float]]]`, defaults to `None`):
          The dictionary mapping the nodes name to their quantization ranges, used and required only when applying static quantization.
      use_external_data_format (`bool`, defaults to `False`):
          Whether to use external data format to store model which size is >= 2Gb.
      preprocessor (`Optional[QuantizationPreprocessor]`, defaults to `None`):
          The preprocessor to use to collect the nodes to include or exclude from quantization.
  
  Returns:
      The path of the resulting quantized model.
docstr_tok: ["Quantizes","a","model","given","the","optimization","specifications","defined","in","`","quantization_config","`",".","Args",":","quantization_config","(","`","QuantizationConfig","`",")",":","The","configuration","containing","the","parameters","related","to","quantization",".","save_dir","(","`","Union","[","str",",","Path","]","`",")",":","The","directory","where","the","quantized","model","should","be","saved",".","file_suffix","(","`","Optional","[","str","]","`",",","defaults","to","`","``","quantized","''","`",")",":","The","file_suffix","used","to","save","the","quantized","model",".","calibration_tensors_range","(","`","Optional","[","Dict","[","str",",","Tuple","[","float",",","float","]","]","]","`",",","defaults","to","`","None","`",")",":","The","dictionary","mapping","the","nodes","name","to","their","quantization","ranges",",","used","and","required","only","when","applying","static","quantization",".","use_external_data_format","(","`","bool","`",",","defaults","to","`","False","`",")",":","Whether","to","use","external","data","format","to","store","model","which","size","is",">","=","2Gb",".","preprocessor","(","`","Optional","[","QuantizationPreprocessor","]","`",",","defaults","to","`","None","`",")",":","The","preprocessor","to","use","to","collect","the","nodes","to","include","or","exclude","from","quantization",".","Returns",":","The","path","of","the","resulting","quantized","model","."]
code: def quantize(
      self,
      quantization_config: QuantizationConfig,
      save_dir: Union[str, Path],
      file_suffix: Optional[str] = "quantized",
      calibration_tensors_range: Optional[
          Dict[str, Tuple[float, float]]
      ] = None,
      use_external_data_format: bool = False,
      preprocessor: Optional[QuantizationPreprocessor] = None,
  ) -> Path:
      """
      Quantizes a model given the optimization specifications defined in `quantization_config`.
  
      Args:
          quantization_config (`QuantizationConfig`):
              The configuration containing the parameters related to quantization.
          save_dir (`Union[str, Path]`):
              The directory where the quantized model should be saved.
          file_suffix (`Optional[str]`, defaults to `"quantized"`):
              The file_suffix used to save the quantized model.
          calibration_tensors_range (`Optional[Dict[str, Tuple[float, float]]]`, defaults to `None`):
              The dictionary mapping the nodes name to their quantization ranges, used and required only when applying static quantization.
          use_external_data_format (`bool`, defaults to `False`):
              Whether to use external data format to store model which size is >= 2Gb.
          preprocessor (`Optional[QuantizationPreprocessor]`, defaults to `None`):
              The preprocessor to use to collect the nodes to include or exclude from quantization.
  
      Returns:
          The path of the resulting quantized model.
      """
      use_qdq = (
          quantization_config.is_static
          and quantization_config.format == QuantFormat.QDQ
      )
      save_dir = Path(save_dir)
      save_dir.mkdir(parents=True, exist_ok=True)
  
      if (
          quantization_config.is_static
          and calibration_tensors_range is None
      ):
          raise ValueError(
              "Requested static quantization in the QuantizationConfig, but no calibration ranges were provided. Please run calibration first using the quantizer fit method, or use dynamic quantization."
          )
      if not quantization_config.is_static:
          if quantization_config.mode != QuantizationMode.IntegerOps:
              LOGGER.warning(
                  f"ONNX Runtime dynamic quantization mode should be QuantizationMode.IntegerOps "
                  f"(got: {quantization_config.mode})."
              )
          if quantization_config.activations_dtype != QuantType.QUInt8:
              LOGGER.warning(
                  f"ONNX Runtime dynamic quantization activations data type should be QuantType.QUInt8 "
                  f"(got: {quantization_config.activations_dtype})."
              )
  
      LOGGER.info(
          f"Creating {'static' if quantization_config.is_static else 'dynamic'} quantizer: {quantization_config}"
      )
  
      if preprocessor is not None:
          LOGGER.info(
              "Preprocessor detected, collecting nodes to include/exclude"
          )
          nodes_to_quantize, nodes_to_exclude = preprocessor.collect(
              self.onnx_model_path
          )
  
          nodes_to_quantize.update(
              quantization_config.nodes_to_quantize
          )
          nodes_to_exclude.update(quantization_config.nodes_to_exclude)
  
          quantization_config.nodes_to_quantize = list(
              nodes_to_quantize
          )
          quantization_config.nodes_to_exclude = list(nodes_to_exclude)
  
      has_subgraphs = False
      onnx_model = onnx.load(Path(self.onnx_model_path).as_posix())
      for node in onnx_model.graph.node:
          if node.op_type in ["If", "Loop", "Scan", "SequenceMap"]:
              has_subgraphs = True
              break
  
      if has_subgraphs:
          if quantization_config.is_static:
              raise NotImplementedError(
                  "Static quantization is currently not supported for models with subgraphs."
              )
          if parse(ort_version) == Version("1.16.0"):
              raise ValueError(
                  "ONNX Runtime version v1.16.0 is not compatible with quantization for models with subgraphs, please downgrade to 1.15.1 or upgrade to a higher version. Reference: https://github.com/microsoft/onnxruntime/pull/17651"
              )
  
      quantizer_factory = QDQQuantizer if use_qdq else ONNXQuantizer
  
      if parse(ort_version) >= Version("1.13.0"):
          # The argument `input_qType` has been changed into `activation_qType` from ORT 1.13
          quantizer = quantizer_factory(
              model=onnx_model,
              static=quantization_config.is_static,
              per_channel=quantization_config.per_channel,
              mode=quantization_config.mode,
              weight_qType=quantization_config.weights_dtype,
              activation_qType=quantization_config.activations_dtype,
              tensors_range=calibration_tensors_range,
              reduce_range=quantization_config.reduce_range,
              nodes_to_quantize=quantization_config.nodes_to_quantize,
              nodes_to_exclude=quantization_config.nodes_to_exclude,
              op_types_to_quantize=[
                  operator.value
                  if isinstance(operator, ORTQuantizableOperator)
                  else operator
                  for operator in quantization_config.operators_to_quantize
              ],
              extra_options={
                  "WeightSymmetric": quantization_config.weights_symmetric,
                  "ActivationSymmetric": quantization_config.activations_symmetric,
                  "EnableSubgraph": has_subgraphs,
                  "ForceSymmetric": quantization_config.activations_symmetric
                  and quantization_config.weights_symmetric,
                  "AddQDQPairToWeight": quantization_config.qdq_add_pair_to_weight,
                  "DedicatedQDQPair": quantization_config.qdq_dedicated_pair,
                  "QDQOpTypePerChannelSupportToAxis": quantization_config.qdq_op_type_per_channel_support_to_axis,
              },
          )
      else:
          quantizer = quantizer_factory(
              model=onnx_model,
              static=quantization_config.is_static,
              per_channel=quantization_config.per_channel,
              mode=quantization_config.mode,
              weight_qType=quantization_config.weights_dtype,
              input_qType=quantization_config.activations_dtype,
              tensors_range=calibration_tensors_range,
              reduce_range=quantization_config.reduce_range,
              nodes_to_quantize=quantization_config.nodes_to_quantize,
              nodes_to_exclude=quantization_config.nodes_to_exclude,
              op_types_to_quantize=[
                  operator.value
                  if isinstance(operator, ORTQuantizableOperator)
                  else operator
                  for operator in quantization_config.operators_to_quantize
              ],
              extra_options={
                  "WeightSymmetric": quantization_config.weights_symmetric,
                  "ActivationSymmetric": quantization_config.activations_symmetric,
                  "EnableSubgraph": False,
                  "ForceSymmetric": quantization_config.activations_symmetric
                  and quantization_config.weights_symmetric,
                  "AddQDQPairToWeight": quantization_config.qdq_add_pair_to_weight,
                  "DedicatedQDQPair": quantization_config.qdq_dedicated_pair,
                  "QDQOpTypePerChannelSupportToAxis": quantization_config.qdq_op_type_per_channel_support_to_axis,
              },
          )
  
      LOGGER.info("Quantizing model...")
      quantizer.quantize_model()
  
      suffix = f"_{file_suffix}" if file_suffix else ""
      quantized_model_path = save_dir.joinpath(
          f"{self.onnx_model_path.stem}{suffix}"
      ).with_suffix(".onnx")
      LOGGER.info(
          f"Saving quantized model at: {save_dir} (external data format: "
          f"{use_external_data_format})"
      )
      quantizer.model.save_model_to_file(
          quantized_model_path.as_posix(), use_external_data_format
      )
  
      # Create and save the configuration summarizing all the parameters related to quantization
      ort_config = ORTConfig(
          quantization=quantization_config,
          use_external_data_format=use_external_data_format,
      )
      ort_config.save_pretrained(save_dir)
  
      if self.config is not None:
          self.config.save_pretrained(save_dir)
  
      maybe_save_preprocessors(self.onnx_model_path.parent, save_dir)
  
      return Path(save_dir)
  
imports: import logging
  import os
  from collections import defaultdict
  from pathlib import Path
  from typing import TYPE_CHECKING, Callable, Dict, List, Optional, Tuple, Union
  import onnx
  from datasets import Dataset, load_dataset
  from packaging.version import Version, parse
  from transformers import AutoConfig
  from onnxruntime import __version__
  from onnxruntime.quantization import CalibrationDataReader, QuantFormat, QuantizationMode, QuantType
  from onnxruntime.quantization.onnx_quantizer import ONNXQuantizer
  from onnxruntime.quantization.qdq_quantizer import QDQQuantizer
  from ..quantization_base import OptimumQuantizer
  from ..utils.save_utils import maybe_save_preprocessors
  from .None import ORTQuantizableOperator
  from .configuration import CalibrationConfig, ORTConfig, QuantizationConfig
  from .modeling_ort import ORTModel
  from .modeling_seq2seq import ORTModelForConditionalGeneration
  from .preprocessors import QuantizationPreprocessor
label_desc: Use image node_id 6 for calling the ORTQuantizer obj's underlying member method code with example usage: obj.quantize(quantization_config, save_dir, file_suffix, calibration_tensors_range, use_external_data_format, preprocessor) and returns: Path