1818import numpy as np
1919import onnx
2020import onnx_graphsurgeon as gs
21+ import onnxscript
2122from onnx import helper , numpy_helper
2223
2324import 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.
0 commit comments