# Copyright (c) ONNX Project Contributors# SPDX-License-Identifier: Apache-2.0"""onnx version converterThis enables users to convert their models between different opsets within thedefault domain ("" or "ai.onnx")."""from__future__importannotationsimportonnximportonnx.onnx_cpp2py_export.version_converterasC# noqa: N812fromonnximportModelProto
[docs]defconvert_version(model:ModelProto,target_version:int)->ModelProto:"""Convert opset version of the ModelProto. Arguments: model: Model. target_version: Target opset version. Returns: Converted model. Raises: RuntimeError when some necessary conversion is not supported. """ifnotisinstance(model,ModelProto):raiseTypeError(f"VersionConverter only accepts ModelProto as model, incorrect type: {type(model)}")ifnotisinstance(target_version,int):raiseTypeError(f"VersionConverter only accepts int as target_version, incorrect type: {type(target_version)}")model_str=model.SerializeToString()converted_model_str=C.convert_version(model_str,target_version)returnonnx.load_from_string(converted_model_str)