Skip to content
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

[IR] Refactor TensorBase to simplify implementation #2081

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

Conversation

justinchuby
Copy link
Collaborator

Move name, doc_string, meta and metadata fields to the base class

Move name, doc_string, meta and metadata fields to the base class
@justinchuby justinchuby added the module: IR Intermediate representation label Feb 28, 2025

Choose a reason for hiding this comment

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

PR Overview

This PR refactors the TensorBase class to simplify its implementation by consolidating common fields (name, doc_string, metadata, and metadata_props) to the base class. Key changes include:

  • Adding an init method and slots in TensorBase to manage common attributes.
  • Removing duplicate metadata properties from Tensor, ExternalTensor, and StringTensor.
  • Updating TensorProtoTensor in the serde module to call the new TensorBase constructor.

Reviewed Changes

File Description
onnxscript/ir/_core.py Refactors TensorBase by adding common initialization and removing metadata duplicates in subclasses.
onnxscript/ir/serde.py Updates TensorProtoTensor by removing inlined metadata_props deserialization and adjusting imports.

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

Comments suppressed due to low confidence (1)

onnxscript/ir/serde.py:246

  • The metadata_props deserialization from proto (previously done via deserialize_metadata_props) has been removed in TensorProtoTensor.init. If metadata is expected to be propagated from the proto, consider reintroducing this assignment.
super().__init__()
Copy link

codecov bot commented Feb 28, 2025

❌ 42 Tests Failed:

Tests completed Failed Passed Skipped
9670 42 9628 1942
View the top 3 failed test(s) by shortest run time
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0753_test_optional_has_element_empty_no_input_name_optional_input
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_optional_has_element_empty_no_input_name_optional_input'

The above exception was the direct cause of the following exception:
.nox\test_onnx_weekly\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_optional_has_element_empty_no_input_name_optional_input' (e=No module named 'tests.onnx_backend_test_code.test_optional_has_element_empty_no_input_name_optional_input') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_optional_has_element_empty_no_input_name_optional_input.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_optional_has_element_empty_no_input_name_optional_input.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 opset18
E   
E   @script()
E   def bck_test_optional_has_element_empty_no_input_name_optional_input() -> (BOOL):
E       output = opset18.OptionalHasElement(None)
E       return output
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0945_test_reshape_reordered_last_dims
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.12.9\x64\Lib\importlib\__init__.py:90: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
E   ModuleNotFoundError: No module named 'tests.onnx_backend_test_code.test_reshape_reordered_last_dims'

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_reshape_reordered_last_dims' (e=No module named 'tests.onnx_backend_test_code.test_reshape_reordered_last_dims') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_reshape_reordered_last_dims.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_reshape_reordered_last_dims.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 opset21
E   
E   @script()
E   def bck_test_reshape_reordered_last_dims(data: FLOAT[2,3,4], shape: INT64[3]) -> (FLOAT[2,4,3]):
E       reshaped = opset21.Reshape(data, shape)
E       return reshaped
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0975_test_resize_upsample_scales_nearest
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.10.11\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_resize_upsample_scales_nearest'

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_resize_upsample_scales_nearest' (e=No module named 'tests.onnx_backend_test_code.test_resize_upsample_scales_nearest') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_resize_upsample_scales_nearest.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_resize_upsample_scales_nearest.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 opset19
E   
E   @script()
E   def bck_test_resize_upsample_scales_nearest(X: FLOAT[1,1,2,2], scales: FLOAT[4]) -> (FLOAT[1,1,4,6]):
E       Y = opset19.Resize(X, None, scales, mode='nearest')
E       return Y

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 requested a review from Copilot February 28, 2025 16:31

Choose a reason for hiding this comment

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

PR Overview

This PR refactors the TensorBase implementation by moving metadata, documentation, and name fields to the base class, thereby simplifying the initialization logic across tensor-related classes.

  • Consolidated common field initialization (name, doc_string, metadata_props) into TensorBase.
  • Removed duplicate property definitions and assignments from Tensor subclasses.
  • Updated import statements to reflect the removal of now-unnecessary dependencies.

Reviewed Changes

File Description
onnxscript/ir/serde.py Updated TensorProtoTensor init to delegate metadata_props to the base class.
onnxscript/ir/_core.py Added common fields to TensorBase and updated subclass constructors accordingly.

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

Comments suppressed due to low confidence (3)

onnxscript/ir/serde.py:247

  • The refactor moves metadata properties initialization to the base class. Consider adding tests to ensure that metadata_props deserialization from proto remains consistent.
def __init__(self, proto: onnx.TensorProto) -> None:

onnxscript/ir/_core.py:107

  • The new init in TensorBase consolidates common fields; please ensure that all subclasses correctly pass these parameters to maintain consistent behavior. Adding unit tests for these changes is recommended.
def __init__(self, name: str | None = None, doc_string: str | None = None, metadata_props: dict[str, str] | None = None) -> None:

onnxscript/ir/_core.py:397

  • The removal of redundant assignments (e.g. name, doc_string, metadata_props) in the Tensor subclass appears appropriate; please verify through tests that these fields are correctly initialized via the base class.
super().__init__(name=name, doc_string=doc_string, metadata_props=metadata_props)
@justinchuby justinchuby marked this pull request as draft February 28, 2025 19:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: IR Intermediate representation
Projects
Development

Successfully merging this pull request may close these issues.

1 participant