Skip to content

Use ir methods to replace onnx helper #2091

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 15 commits into
base: main
Choose a base branch
from

Conversation

justinchuby
Copy link
Collaborator

@justinchuby justinchuby commented Mar 7, 2025

Ban onnx.helper and onnx.numpy_helper because they can be slow. Selectively enable usages of some with noqa: TID251 and updated usages of the rest.

Fix ir.tensor to generate float32 tensors when a plain python float is provided.

Copy link

codecov bot commented Mar 7, 2025

❌ 8 Tests Failed:

Tests completed Failed Passed Skipped
13892 8 13884 1901
View the top 3 failed test(s) by shortest run time
::tests.functions.onnxfns2_test
Stack Traces | 0s run time
onnxscript/ir/serde.py:99: in wrapper
    return func(*args, **kwargs)
onnxscript/ir/serde.py:1497: in serialize_tensor_into
    tensor_proto.raw_data = from_.tobytes()
onnxscript/ir/_core.py:453: in tobytes
    assert self.dtype.itemsize == array.itemsize, "Bug: The itemsize should match"
E   AssertionError: Bug: The itemsize should match

The above exception was the direct cause of the following exception:
tests/functions/onnxfns2_test.py:6: in <module>
    from tests.models import onnxfns2
tests/models/onnxfns2.py:87: in <module>
    @script()
onnxscript/main.py:89: in transform
    result = script_check(f_ast, opset, env, src, default_opset=default_opset)
onnxscript/main.py:33: in script_check
    return convert.translate_function_def(f)
onnxscript/converter.py:1460: in translate_function_def
    fn_ir = self._translate_function_def_common(stmt)
onnxscript/converter.py:1447: in _translate_function_def_common
    self._translate_stmt(s, index_of_stmt=i)
onnxscript/converter.py:981: in _translate_stmt
    return self._translate_if_stmt(node)
onnxscript/converter.py:1106: in _translate_if_stmt
    test = self._translate_expr(stmt.test, "cond").name
onnxscript/converter.py:559: in _translate_expr
    r = self._translate_compare_expr(node)
onnxscript/converter.py:906: in _translate_compare_expr
    right = self._translate_expr(node.comparators[0])
onnxscript/converter.py:565: in _translate_expr
    r = self._emit_const(self._eval_constant_expr(node), target, self._source_of(node))
onnxscript/converter.py:415: in _emit_const
    tensor = autocast.pyvalue_to_onnx_tensor(ovar, pyvalue)
onnxscript/_internal/autocast.py:27: in pyvalue_to_onnx_tensor
    return ir.serde.serialize_tensor(ir.tensor(pyvalue, name=tensor_name))
onnxscript/ir/serde.py:1461: in serialize_tensor
    serialize_tensor_into(tensor_proto, from_=tensor)
onnxscript/ir/serde.py:101: in wrapper
    raise SerdeError(
E   onnxscript.ir.serde.SerdeError: Error calling serialize_tensor_into with: Tensor<STRING,[]>(array('DCR', dtype='<U3'), name='const_0')
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0884_test_reduce_min_default_axes_keepdims_example
Stack Traces | 0.004s run time
onnxscript\backend\onnx_export_test.py:137: in extract_functions
    mod = importlib.import_module(import_name)
C:\hostedtoolcache\windows\Python\3.11.9\x64\Lib\importlib\__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
E   ModuleNotFoundError: No module named 'tests.onnx_backend_test_code.test_reduce_min_default_axes_keepdims_example'

The above exception was the direct cause of the following exception:
.nox\test\Lib\site-packages\parameterized\parameterized.py:620: in standalone_func
    return func(*(a + p.args), **p.kwargs, **kw)
onnxscript\backend\onnx_export_test.py:271: in test_export2python_produces_correct_onnx_script_model
    functions = extract_functions(backend_test.name, code, self.test_folder)
onnxscript\backend\onnx_export_test.py:139: in extract_functions
    raise AssertionError(
E   AssertionError: Unable to import 'tests.onnx_backend_test_code.test_reduce_min_default_axes_keepdims_example' (e=No module named 'tests.onnx_backend_test_code.test_reduce_min_default_axes_keepdims_example') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_reduce_min_default_axes_keepdims_example.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_reduce_min_default_axes_keepdims_example.py', current folder: D:\a\onnxscript\onnxscript
E   ---- CONTENT --
E   import numpy
E   from onnx import TensorProto
E   from onnx.helper import make_tensor
E   from onnxscript import script, external_tensor
E   from onnxscript.values import Opset
E   from onnxscript.onnx_types import FLOAT
E   from onnxscript.onnx_opset import opset18
E   
E   @script()
E   def bck_test_reduce_min_default_axes_keepdims_example(data: FLOAT[3,2,2]) -> (FLOAT[1,1,1]):
E       reduced = opset18.ReduceMin(data, keepdims=1)
E       return reduced
tests.operator_test.TestConverter::test_const_promotion
Stack Traces | 0.004s run time
tests/operator_test.py:41: in test_const_promotion
    onnxscript.testing.assert_isomorphic_function(explicit_plus1, implicit_plus1)
onnxscript/testing/__init__.py:38: in assert_isomorphic_function
    assert _isomorphic(_to_function_proto(fn1), _to_function_proto(fn2))
E   AssertionError

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

@justinchuby justinchuby marked this pull request as draft March 7, 2025 20:27
@justinchuby justinchuby marked this pull request as ready for review March 24, 2025 23:20
Copy link
Contributor

@Copilot Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull Request Overview

This PR aims to deprecate the use of onnx.helper and onnx.numpy_helper in favor of ir-based methods to improve performance. Key changes include replacing multiple onnx.helper calls with either their ir or ir.serde counterparts (and adding “# noqa: TID251” comments) and updating imports and utility functions accordingly.

Reviewed Changes

Copilot reviewed 21 out of 21 changed files in this pull request and generated no comments.

Show a summary per file
File Description
onnxscript/tensor.py Updated import and added noqa comment on onnx.helper usage.
onnxscript/_internal/utils.py Applied noqa comments to onnx.helper calls in utility functions.
onnxscript/optimizer/_constant_folding.py Replaced onnx.numpy_helper.from_array with ir.serde.serialize_tensor.
onnxscript/main.py Updated model export logic to use ir.Model and ir.save.
onnxscript/optimizer/_legacy/constant_folding.py, etc. Added ruff: noqa: TID251 markers and adjusted helper calls across files.
onnxscript/_internal/autocast.py Replaced tensor conversion via numpy_helper with ir.serde.serialize_tensor.
onnxscript/rewriter/llama_rule_sets.py Modified allowed tensor types check to use as_int() instead of value.
onnxscript/rewriter/cast_constant_of_shape.py and others Changed onnx.helper.make_tensor calls to use ir.tensor.
onnxscript/function_libs/torch_lib/ops/nn.py Updated empty-tensor creation to use ir.tensor.
Comments suppressed due to low confidence (2)

onnxscript/rewriter/llama_rule_sets.py:65

  • Ensure that converting the attribute value with as_int() returns the expected integer representation that matches the entries in _allowed_tensor_types. Verify that edge cases or unexpected types do not cause the check to fail.
to.as_int() in cls._allowed_tensor_types

onnxscript/_internal/autocast.py:39

  • Confirm that the new serialization using ir.serde.serialize_tensor produces outputs consistent with the previous behavior of numpy_helper.from_array, ensuring compatibility with downstream tensor utilities.
return ir.serde.serialize_tensor(ir.tensor(pyvalue, name=tensor_name))

titaiwangms
titaiwangms previously approved these changes Mar 24, 2025
@justinchuby justinchuby disabled auto-merge March 25, 2025 00:09
@justinchuby justinchuby enabled auto-merge (squash) March 25, 2025 00:11
@justinchuby justinchuby disabled auto-merge March 25, 2025 00:17
@justinchuby justinchuby enabled auto-merge (squash) March 25, 2025 00:28
@justinchuby justinchuby disabled auto-merge March 25, 2025 00:31
@justinchuby justinchuby enabled auto-merge (squash) March 25, 2025 00:35
@justinchuby justinchuby disabled auto-merge March 25, 2025 00:36
@justinchuby justinchuby enabled auto-merge (squash) March 25, 2025 00:36
@justinchuby justinchuby disabled auto-merge March 25, 2025 01:06
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.

3 participants