# SPDX-License-Identifier: Apache-2.0# Rather than using ONNX protobuf definition throughout our codebase,# we import ONNX protobuf definition here so that we can conduct quick# fixes by overwriting ONNX functions without changing any lines# elsewhere.fromonnximportonnx_pbasonnx_protofromonnximportdefs# Overwrite the make_tensor defined in onnx.helper because of a bug# (string tensor get assigned twice)fromonnximportmappingfromonnx.onnx_pbimportTensorProto,ValueInfoPrototry:# noqa: SIM105fromonnx.onnx_pbimportSparseTensorProtoexceptImportError:# onnx is too old.passfromonnx.helperimportsplit_complex_to_pairsdefmake_tensor_fixed(name,data_type,dims,vals,raw=False):""" Make a TensorProto with specified arguments. If raw is False, this function will choose the corresponding proto field to store the values based on data_type. If raw is True, use "raw_data" proto field to store the values, and values should be of type bytes in this case. """tensor=TensorProto()tensor.data_type=data_typetensor.name=nameifdata_type==TensorProto.COMPLEX64ordata_type==TensorProto.COMPLEX128:vals=split_complex_to_pairs(vals)ifraw:tensor.raw_data=valselse:field=mapping.STORAGE_TENSOR_TYPE_TO_FIELD[mapping.TENSOR_TYPE_TO_STORAGE_TENSOR_TYPE[data_type]]getattr(tensor,field).extend(vals)tensor.dims.extend(dims)returntensordefget_opset_number_from_onnx():""" Returns the latest opset version supported by the *onnx* package. """returndefs.onnx_opset_version()
[docs]defget_latest_tested_opset_version():""" This module relies on *onnxruntime* to test every converter. The function returns the most recent target opset tested with *onnxruntime* or the opset version specified by *onnx* package if this one is lower (return by `onnx.defs.onnx_opset_version()`). """from..import__max_supported_opset__returnmin(__max_supported_opset__,get_opset_number_from_onnx())