Skip to content

Update autocast.py to fix attribute creation error #2365

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

Merged
merged 2 commits into from
Jun 6, 2025

Conversation

justinchuby
Copy link
Collaborator

@justinchuby justinchuby commented Jun 4, 2025

This change should fix the type of errors like below (reported in pytorch/pytorch#153214 (comment)):

  File "/usr/local/lib/python3.12/dist-packages/google/protobuf/internal/python_message.py", line 589, in init
    _ReraiseTypeErrorWithFieldName(message_descriptor.name, field_name)
  File "/usr/local/lib/python3.12/dist-packages/google/protobuf/internal/python_message.py", line 478, in _ReraiseTypeErrorWithFieldName
    raise exc.with_traceback(sys.exc_info()[2])
  File "/usr/local/lib/python3.12/dist-packages/google/protobuf/internal/python_message.py", line 587, in init
    setattr(self, field_name, field_value)
  File "/usr/local/lib/python3.12/dist-packages/google/protobuf/internal/python_message.py", line 730, in field_setter
    raise TypeError(
TypeError: Cannot set onnx.AttributeProto.type to <AttrType.TENSOR: 4>: <AttrType.TENSOR: 4> has type <class 'onnx.onnx_cpp2py_export.defs.OpSchema.AttrType'>, but expected one of: (<class 'int'>,) for field AttributeProto.type

Copilot

This comment was marked as outdated.

Copy link

codecov bot commented Jun 4, 2025

❌ 4 Tests Failed:

Tests completed Failed Passed Skipped
14865 4 14861 2354
View the top 3 failed test(s) by shortest run time
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0118_test_bitwise_and_i32_2d
Stack Traces | 0.003s 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_bitwise_and_i32_2d'

The above exception was the direct cause of the following exception:
.nox\test_onnx_ir_git\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_bitwise_and_i32_2d' (e=No module named 'tests.onnx_backend_test_code.test_bitwise_and_i32_2d') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_bitwise_and_i32_2d.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_bitwise_and_i32_2d.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 INT32
E   from onnxscript.onnx_opset import opset18
E   
E   @script()
E   def bck_test_bitwise_and_i32_2d(x: INT32[3,4], y: INT32[3,4]) -> (INT32[3,4]):
E       bitwiseand = opset18.BitwiseAnd(x, y)
E       return bitwiseand
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0257_test_col2im_pads
Stack Traces | 0.003s 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_col2im_pads'

The above exception was the direct cause of the following exception:
.nox\test_onnx_ir_git\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_col2im_pads' (e=No module named 'tests.onnx_backend_test_code.test_col2im_pads') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_col2im_pads.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_col2im_pads.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, INT64
E   from onnxscript.onnx_opset import opset18
E   
E   @script()
E   def bck_test_col2im_pads(input: FLOAT[1,5,15], image_shape: INT64[2], block_shape: INT64[2]) -> (FLOAT[1,1,5,5]):
E       output = opset18.Col2Im(input, image_shape, block_shape, pads=[0, 1, 0, 1])
E       return output
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0756_test_or_bcast3v2d
Stack Traces | 0.003s 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_or_bcast3v2d'

The above exception was the direct cause of the following exception:
.nox\test_onnx_ir_git\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_or_bcast3v2d' (e=No module named 'tests.onnx_backend_test_code.test_or_bcast3v2d') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_or_bcast3v2d.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_or_bcast3v2d.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 BOOL
E   from onnxscript.onnx_opset import opset7
E   
E   @script()
E   def bck_test_or_bcast3v2d(x: BOOL[3,4,5], y: BOOL[4,5]) -> (BOOL[3,4,5]):
E       r_or = opset7.Or(x, y)
E       return r_or

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 added the merge at lgtm Reviewers can merge when they approve label Jun 4, 2025
@justinchuby justinchuby enabled auto-merge (squash) June 4, 2025 23:33
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 updates the internal helper for creating ONNX AttributeProto objects so that the type field is passed as an integer rather than an enum, fixing type errors when setting attributes.

  • Changed the attr_type annotation to use Python’s union syntax.
  • Wrapped attr_type in int() for all onnx.AttributeProto constructor calls.
Comments suppressed due to low confidence (1)

onnxscript/_internal/autocast.py:66

  • Add unit tests covering cases where attr_type is a tensor and where value is an empty list to ensure the int(attr_type) conversion is applied correctly.
return onnx.AttributeProto(name=key, type=int(attr_type))

@justinchuby justinchuby disabled auto-merge June 5, 2025 23:41
@justinchuby
Copy link
Collaborator Author

Updated to fix the callers instead

@justinchuby justinchuby requested a review from titaiwangms June 6, 2025 01:31
@github-project-automation github-project-automation bot moved this from Todo to Done in ONNX Script Review Board Jun 6, 2025
@titaiwangms
Copy link
Contributor

Are failing checks more than usual?

@justinchuby
Copy link
Collaborator Author

Are failing checks more than usual?

They seem like the usual windows flakiness & unresolved issues with the latest ort. We should fix them though at some point.

@justinchuby justinchuby merged commit dcb773f into main Jun 6, 2025
25 of 32 checks passed
@justinchuby justinchuby deleted the justinchu/proto-attr branch June 6, 2025 17:03
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merge at lgtm Reviewers can merge when they approve topic: onnxscript core
Projects
Development

Successfully merging this pull request may close these issues.

2 participants