Skip to content

Conversation

@gramalingam
Copy link
Collaborator

@gramalingam gramalingam commented Nov 15, 2025

Migrate onnxscript converter to use onnx ir.

PLEASE DO NOT MERGE YET. May want to wait for release 0.6

Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
@codecov
Copy link

codecov bot commented Nov 15, 2025

Codecov Report

❌ Patch coverage is 82.46154% with 57 lines in your changes missing coverage. Please review.
✅ Project coverage is 70.10%. Comparing base (c1bfdfc) to head (86e10dd).
⚠️ Report is 3 commits behind head on main.
✅ All tests successful. No failed tests found.

Files with missing lines Patch % Lines
onnxscript/irbuilder.py 73.52% 15 Missing and 3 partials ⚠️
onnxscript/converter.py 89.87% 9 Missing and 7 partials ⚠️
onnxscript/values.py 76.19% 9 Missing and 6 partials ⚠️
onnxscript/converter_test.py 80.76% 3 Missing and 2 partials ⚠️
...ewriter/ort_fusions/fused_matmul_rule_sets_test.py 0.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #2706      +/-   ##
==========================================
+ Coverage   70.04%   70.10%   +0.06%     
==========================================
  Files         226      226              
  Lines       27177    27132      -45     
  Branches     2734     2739       +5     
==========================================
- Hits        19036    19022      -14     
+ Misses       7197     7165      -32     
- Partials      944      945       +1     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
@gramalingam gramalingam changed the title [DRAFT] Test ir builder migration Migrate onnxscript convert to use onnx ir Nov 17, 2025
@gramalingam gramalingam changed the title Migrate onnxscript convert to use onnx ir Migrate onnxscript converter to use onnx ir Nov 17, 2025
@gramalingam gramalingam marked this pull request as ready for review November 17, 2025 15:47
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
type_and_shape = ir.from_proto(typeinfo.to_type_proto())
value.type = type_and_shape.type
value.shape = type_and_shape.shape
except AttributeError:
Signed-off-by: Ganesan Ramalingam <[email protected]>
Signed-off-by: Ganesan Ramalingam <[email protected]>
pass
value.meta["typeinfo"] = typeinfo


Check notice

Code scanning / CodeQL

Empty except Note

'except' clause does nothing but pass and there is no explanatory comment.

Copilot Autofix

AI 9 days ago

To fix this issue, the empty except AttributeError: block should be replaced with exception handling that does something useful. The most appropriate fix is to log a warning (using the module's logger) indicating that an AttributeError was encountered while setting the type info, which helps diagnose and debug the cause without terminating program execution. This must be done in the function set_type_info in onnxscript/converter.py. No new methods or imports are required, as the logger logger is already defined and available in the module. The fix is local: replace pass with a logging call in the specified region.

Suggested changeset 1
onnxscript/converter.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/onnxscript/converter.py b/onnxscript/converter.py
--- a/onnxscript/converter.py
+++ b/onnxscript/converter.py
@@ -119,8 +119,10 @@
         type_and_shape = ir.from_proto(typeinfo.to_type_proto())
         value.type = type_and_shape.type
         value.shape = type_and_shape.shape
-    except AttributeError:
-        pass
+    except AttributeError as ex:
+        logger.warning(
+            "Failed to set type and shape info due to AttributeError in set_type_info: %s", ex
+        )
     value.meta["typeinfo"] = typeinfo
 
 
EOF
@@ -119,8 +119,10 @@
type_and_shape = ir.from_proto(typeinfo.to_type_proto())
value.type = type_and_shape.type
value.shape = type_and_shape.shape
except AttributeError:
pass
except AttributeError as ex:
logger.warning(
"Failed to set type and shape info due to AttributeError in set_type_info: %s", ex
)
value.meta["typeinfo"] = typeinfo


Copilot is powered by AI and may make mistakes. Always verify output.
Unable to commit as this autofix suggestion is now outdated
type_and_shape = ir.from_proto(typeinfo.to_type_proto())
value.type = type_and_shape.type
value.shape = type_and_shape.shape
except AttributeError:

Check notice

Code scanning / CodeQL

Empty except Note

'except' clause does nothing but pass and there is no explanatory comment.

Copilot Autofix

AI 9 days ago

The best fix is to properly handle the exception by notifying the user or logging the error, so that missing or erroneous type information does not go unnoticed. At minimum, add a logging statement at the appropriate level (e.g., warning or error), describing what happened and including useful contextual information. This way, if an AttributeError is raised, it is recorded and is much less likely to go undetected.

Specifically, edit the file onnxscript/irbuilder.py, in the function set_type_info, replacing the empty except AttributeError: block (lines 130–131) with an except block that logs the exception with a helpful message, including details about the value and typeinfo involved.

No new imports are needed since logging is already imported, and the logger onnxscript is already defined as logger.

Suggested changeset 1
onnxscript/irbuilder.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/onnxscript/irbuilder.py b/onnxscript/irbuilder.py
--- a/onnxscript/irbuilder.py
+++ b/onnxscript/irbuilder.py
@@ -127,8 +127,13 @@
         type_and_shape = ir.from_proto(typeinfo.to_type_proto())
         value.type = type_and_shape.type
         value.shape = type_and_shape.shape
-    except AttributeError:
-        pass
+    except AttributeError as e:
+        logger.warning(
+            "Failed to set type and shape on IR value '%s' with typeinfo '%s': %s",
+            getattr(value, "name", "<unknown>"),
+            repr(typeinfo),
+            e,
+        )
     value.meta["typeinfo"] = typeinfo
 
 
EOF
@@ -127,8 +127,13 @@
type_and_shape = ir.from_proto(typeinfo.to_type_proto())
value.type = type_and_shape.type
value.shape = type_and_shape.shape
except AttributeError:
pass
except AttributeError as e:
logger.warning(
"Failed to set type and shape on IR value '%s' with typeinfo '%s': %s",
getattr(value, "name", "<unknown>"),
repr(typeinfo),
e,
)
value.meta["typeinfo"] = typeinfo


Copilot is powered by AI and may make mistakes. Always verify output.
Unable to commit as this autofix suggestion is now outdated
type_and_shape = ir.from_proto(typeinfo.to_type_proto())
value.type = type_and_shape.type
value.shape = type_and_shape.shape
except AttributeError:
Signed-off-by: Ganesan Ramalingam <[email protected]>
@titaiwangms titaiwangms changed the title Migrate onnxscript converter to use onnx ir [Do Not Merge]Migrate onnxscript converter to use onnx ir Nov 21, 2025


def select_ir_version(version: int, domain: str = "") -> int:
"""Selects a suitable ONNX ir_version for a given opset version."""
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Probably default the minimal version to a more recent one, like v10, such that the generated model is compatible with newer tool chains (metadata)? This can also be a good default to push for adoption

"""Selects a suitable ONNX ir_version for a given opset version."""
if domain == "":
domain = "ai.onnx"
if (domain, version) not in onnx.helper.OP_SET_ID_VERSION_MAP:
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would consider adding inline noqas for TID251 so we allow the helper module in a case by case basis. There is also a possibility that it is not needed.

onnx.helper.make_opsetid(domain, version) for domain, version in opsets.items()
]

return onnx.helper.make_model(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Curious why we do this instead of converting from an IR Model?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

Development

Successfully merging this pull request may close these issues.

4 participants