Skip to content

Commit 21b9891

Browse files
authored
Merge branch 'main' into main
Signed-off-by: inisis <[email protected]>
2 parents 6c3b100 + e3e399a commit 21b9891

File tree

3 files changed

+25
-2
lines changed

3 files changed

+25
-2
lines changed

modelopt/onnx/autocast/graphsanitizer.py

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import numpy as np
1919
import onnx
2020
import onnx_graphsurgeon as gs
21+
import onnxscript
2122
from onnx import helper, numpy_helper
2223

2324
import modelopt.onnx.autocast.utils as utils
@@ -144,6 +145,7 @@ def convert_opset(self) -> None:
144145
"""Convert the model to the given opset version.
145146
146147
The method checks all opset imports and converts the model if any are below the minimum version.
148+
Uses onnxscript for conversion when available, which handles large models (>2GB) better.
147149
"""
148150
# Check all opset imports
149151
default_opsets = list(self.model.opset_import)
@@ -163,10 +165,30 @@ def convert_opset(self) -> None:
163165
if any(op.version < self.min_opset for op in default_opsets):
164166
invalid_opsets = [op.version for op in default_opsets if op.version < self.min_opset]
165167
try:
166-
self.model = onnx.version_converter.convert_version(self.model, self.min_opset)
168+
logger.info(
169+
f"Converting model from opset {invalid_opsets} to {self.min_opset} using onnxscript..."
170+
)
171+
172+
# Convert to onnxscript IR
173+
model_ir = onnxscript.ir.serde.deserialize_model(self.model)
174+
175+
# onnxscript handles conversion of large models better than the standard ONNX version_converter
176+
# Convert opset with fallback=True (automatically falls back to C API if needed)
177+
onnxscript.version_converter.convert_version(
178+
model_ir, target_version=self.min_opset, fallback=True
179+
)
180+
181+
# Convert back to ONNX proto
182+
self.model = onnxscript.ir.serde.serialize_model(model_ir)
183+
logger.info(f"Successfully converted model to opset {self.min_opset}")
167184
except Exception as e:
168185
logger.warning(f"Failed to convert model to opset {self.min_opset}: {e!s}")
169186
logger.warning(f"Attempting to continue with the original opsets: {invalid_opsets}")
187+
else:
188+
logger.debug(
189+
f"No opset conversion needed. Current opset {[op.version for op in default_opsets]} >= min_opset "
190+
"{self.min_opset}"
191+
)
170192

171193
def set_ir_version(self, max_ir_version: int | None) -> None:
172194
"""Set the model's IR version to the maximum supported version.

modelopt/onnx/autocast/precisionconverter.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ class InitializerConsumerTracker:
6868
OP_TYPES_NOT_SUPPORTED_IN_LOW_PRECISION = ["Upsample", "NonMaxSuppression", "Celu"]
6969

7070
# Temporarily block these ops in low precision, as they are not supported yet
71-
OP_TYPES_NOT_SUPPORTED_IN_LOW_PRECISION.extend(["Scan", "If", "Loop", "LSTM"])
71+
OP_TYPES_NOT_SUPPORTED_IN_LOW_PRECISION.extend(["Scan", "If", "Loop"])
7272

7373
# Mapping of op types to indices of inputs that should not be converted to low precision.
7474
SKIP_LOW_PRECISION_MAPPING_FP16 = {"Resize": {2}}

setup.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@
5252
"onnxruntime-gpu~=1.22.0 ; platform_machine != 'aarch64' and platform_system != 'Darwin' and platform_system != 'Windows'", # noqa: E501
5353
"onnxruntime-directml==1.20.0; platform_system == 'Windows'",
5454
"onnxscript", # For test_onnx_dynamo_export unit test
55+
"onnxscript", # For autocast opset conversion and test_onnx_dynamo_export unit test
5556
"polygraphy>=0.49.22",
5657
"onnxslim",
5758
],

0 commit comments

Comments
 (0)