Skip to content

[IR] Display constant tensors for Value #2248

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 17 commits into from
May 1, 2025
Merged

Conversation

justinchuby
Copy link
Collaborator

@justinchuby justinchuby commented Apr 30, 2025

Display constant values and simplify the value repr string when fields are empty.

>>> from onnxscript import ir
>>> v = ir.Value(name="v1", const_value=ir.tensor(1))
>>> v
Value(name='v1', const_value=Tensor<INT64,[]>(array(1), name=None))
>>> v = ir.Value(name="v1", const_value=ir.tensor([[1]]))
>>> v
Value(name='v1', const_value=Tensor<INT64,[1,1]>(array([[1]]), name=None))
>>> print(v)
%"v1"<?,?>{Tensor<INT64,[1,1]>(array([[1]]), name=None)}

Fix #2073

Display constant values and simplify the value repr string when fields are empty
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 repr method for the Value class so that it displays constant tensor values and simplifies the representation when optional fields are empty. Key changes include:

  • Conditionally appending type, shape, producer, index, and const_value fields to the repr string.
  • Removing the default "None" string for missing producer information.
  • Improving the overall consistency of field formatting in the repr output.

Copy link

codecov bot commented Apr 30, 2025

❌ 3 Tests Failed:

Tests completed Failed Passed Skipped
14461 3 14458 1879
View the top 3 failed test(s) by shortest run time
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0467_test_hardsigmoid_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_hardsigmoid_example'

The above exception was the direct cause of the following exception:
.nox\test_ort_nightly\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_hardsigmoid_example' (e=No module named 'tests.onnx_backend_test_code.test_hardsigmoid_example') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_hardsigmoid_example.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_hardsigmoid_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 opset22
E   
E   @script()
E   def bck_test_hardsigmoid_example(x: FLOAT[3]) -> (FLOAT[3]):
E       y = opset22.HardSigmoid(x, alpha=0.5, beta=0.6000000238418579)
E       return y
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0641_test_max_uint64
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_max_uint64'

The above exception was the direct cause of the following exception:
.nox\test_ort_nightly\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_max_uint64' (e=No module named 'tests.onnx_backend_test_code.test_max_uint64') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_max_uint64.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_max_uint64.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 UINT64
E   from onnxscript.onnx_opset import opset13
E   
E   @script()
E   def bck_test_max_uint64(data_0: UINT64[3], data_1: UINT64[3]) -> (UINT64[3]):
E       result = opset13.Max(data_0, data_1)
E       return result
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0791_test_quantizelinear_int16
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_quantizelinear_int16'

The above exception was the direct cause of the following exception:
.nox\test_ort_nightly\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_quantizelinear_int16' (e=No module named 'tests.onnx_backend_test_code.test_quantizelinear_int16') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_quantizelinear_int16.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_quantizelinear_int16.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, INT16
E   from onnxscript.onnx_opset import opset21
E   
E   @script()
E   def bck_test_quantizelinear_int16(x: FLOAT[16], y_scale: FLOAT, y_zero_point: INT16) -> (INT16[16]):
E       y = opset21.QuantizeLinear(x, y_scale, y_zero_point)
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 April 30, 2025 01:21
@justinchuby justinchuby added topic: ux User experience related topics module: IR Intermediate representation labels Apr 30, 2025
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 improves the string representations of constant Value objects by including tensor details when available and simplifying the output when fields are empty. Key changes include:

  • Adding conditional inclusion of type, shape, and producer information in repr.
  • Introducing a shortened representation for const_value in repr and a similar change in str when the tensor size is small.

@justinchuby justinchuby changed the title Display constant tensors for Value [IR] Display constant tensors for Value Apr 30, 2025
@gramalingam
Copy link
Collaborator

Does this show up in the displayed form of a Node? Eg., if we have a node Y = Add(X, c) where c is a constant 1, for example? Would the displayed representation inside a node for an (input) value be too dense (with type/shape etc.)? If so, can we have a more compact representation for an input-value representation inside a node? May be just the name "c" and the value "1"?

@justinchuby
Copy link
Collaborator Author

justinchuby commented Apr 30, 2025

In a node, it won't show. In a graph it will show in the initializers section

graph(
    name=anonymous_graph:124652572372048,
    inputs=(

    ),
    outputs=(

    ),
    initializers=(
        %"init"<?,?>{Tensor<INT64,[5]>(array([1, 1, 1, 1, 1]), name=None)}
    ),
) {
    0 |  # node_Add_0
         %"val_0"<?,?> ⬅️ ::Add(%"init", %"init")
    return 
}

A Constant node looks like

%"anonymous:124652572372688"<?,?> ⬅️ ::Constant() {value=Tensor<INT64,[5]>(array([1, 1, 1, 1, 1]), name=None)}

Is that ok? Or maybe we want to see it directly in the node?

@justinchuby
Copy link
Collaborator Author

I updated for it to show

%"anonymous:132887616237136"<?,?> ⬅️ ::Add(%"init"{Tensor<INT64,[5]>(array([1, 1, 1, 1, 1]), name=None)}, %"init"{Tensor<INT64,[5]>(array([1, 1, 1, 1, 1]), name=None)})

Copy link
Contributor

@titaiwangms titaiwangms left a comment

Choose a reason for hiding this comment

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

What do we have when it's larger than 10 elements?

@github-project-automation github-project-automation bot moved this from Todo to Done in ONNX Script Review Board May 1, 2025
@justinchuby
Copy link
Collaborator Author

It will show {...}

@gramalingam
Copy link
Collaborator

I updated for it to show

%"anonymous:132887616237136"<?,?> ⬅️ ::Add(%"init"{Tensor<INT64,[5]>(array([1, 1, 1, 1, 1]), name=None)}, %"init"{Tensor<INT64,[5]>(array([1, 1, 1, 1, 1]), name=None)})

Great! (My original request, at least, was for the ability to see it in the node.) What would be even better (IMO) is if this can be made "less busy" ... in typical usage, just the values will suffice (you want to see "0" or "1" or "0.36712") ... given that the full information is available elsewhere, if a user really wants to know, a shorter version should work better here.

I do think there is a difference between seeing %"Y" <- ::Add (%"X", %"init"{1}) and %"Y" <- ::Add (%"X", %"init"{Tensor<INT64,[1]>(array([1]), name=None)}) ... in favor of the first.

@justinchuby
Copy link
Collaborator Author

Sounds goo - I will create a follow up

@justinchuby justinchuby merged commit db414d7 into main May 1, 2025
23 of 27 checks passed
@justinchuby justinchuby deleted the justinchu/display-constants branch May 1, 2025 18:08
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: IR Intermediate representation topic: ux User experience related topics
Projects
Development

Successfully merging this pull request may close these issues.

[IR] Display small values for readability
3 participants