Skip to content

[torchlib] Fix scatter reduce on error cases #2287

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 3 commits into from
May 9, 2025

Conversation

justinchuby
Copy link
Collaborator

@justinchuby justinchuby commented May 9, 2025

Fix three errors

value = ir.tensor([np.iinfo(dtype.numpy()).min], dtype=dtype)
                       ^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/numpy/_core/getlimits.py", line 706, in __init__
    raise ValueError("Invalid integer data type %r." % (self.kind,))
ValueError: Invalid integer data type 'b'.
Traceback (most recent call last):
  File "/Users/runner/work/torch-onnx-op-matrix/torch-onnx-op-matrix/op_matrix/onnx_dynamo_op_survey.py", line 54, in check_single_op
    onnx.checker.check_model(onnx_model, full_check=True)  # type: ignore
    ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/Library/Frameworks/Python.framework/Versions/3.11/lib/python3.11/site-packages/onnx/checker.py", line 180, in check_model
    C.check_model(
onnx.onnx_cpp2py_export.checker.ValidationError: Mismatched attribute type in 'node_ConstantOfShape_1 : value'. Expected: 'TENSOR', actual: 'INT'

==> Context: Bad node spec for node. Name: node_ConstantOfShape_1 OpType: ConstantOfShape

Fix a case for bfloat16 when min should be max.

Copy link

codecov bot commented May 9, 2025

❌ 3 Tests Failed:

Tests completed Failed Passed Skipped
14468 3 14465 1880
View the top 3 failed test(s) by shortest run time
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_1000_test_scatternd_multiply
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_scatternd_multiply'

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_scatternd_multiply' (e=No module named 'tests.onnx_backend_test_code.test_scatternd_multiply') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_scatternd_multiply.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_scatternd_multiply.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_scatternd_multiply(data: FLOAT[4,4,4], indices: INT64[2,1], updates: FLOAT[2,4,4]) -> (FLOAT[4,4,4]):
E       y = opset18.ScatterND(data, indices, updates, reduction='mul')
E       return y
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0059_test_argmin_keepdims_random_select_last_index
Stack Traces | 0.006s run time
onnxscript\backend\onnx_export_test.py:137: in extract_functions
    mod = importlib.import_module(import_name)
C:\hostedtoolcache\windows\Python\3.12.10\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_argmin_keepdims_random_select_last_index'

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_argmin_keepdims_random_select_last_index' (e=No module named 'tests.onnx_backend_test_code.test_argmin_keepdims_random_select_last_index') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_argmin_keepdims_random_select_last_index.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_argmin_keepdims_random_select_last_index.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 opset13
E   
E   @script()
E   def bck_test_argmin_keepdims_random_select_last_index(data: FLOAT[2,3,4]) -> (INT64[2,1,4]):
E       result = opset13.ArgMin(data, axis=1, keepdims=1, select_last_index=1)
E       return result
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_1151_test_softmax_negative_axis_expanded_ver18
Stack Traces | 0.006s 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_softmax_negative_axis_expanded_ver18'

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_softmax_negative_axis_expanded_ver18' (e=No module named 'tests.onnx_backend_test_code.test_softmax_negative_axis_expanded_ver18') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_softmax_negative_axis_expanded_ver18.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_softmax_negative_axis_expanded_ver18.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_softmax_negative_axis_expanded_ver18(x: FLOAT[3,4,5]) -> (FLOAT[3,4,5]):
E       Softmax_test_softmax_negative_axis_expanded_function_axes = opset18.Constant(value=make_tensor("value", 7, dims=[1], vals=[-1]))
E       Softmax_test_softmax_negative_axis_expanded_function_X_ReduceMax = opset18.ReduceMax(x, Softmax_test_softmax_negative_axis_expanded_function_axes, keepdims=1)
E       Softmax_test_softmax_negative_axis_expanded_function_X_Sub = opset18.Sub(x, Softmax_test_softmax_negative_axis_expanded_function_X_ReduceMax)
E       Softmax_test_softmax_negative_axis_expanded_function_X_Exp = opset18.Exp(Softmax_test_softmax_negative_axis_expanded_function_X_Sub)
E       Softmax_test_softmax_negative_axis_expanded_function_X_ReduceSum = opset18.ReduceSum(Softmax_test_softmax_negative_axis_expanded_function_X_Exp, Softmax_test_softmax_negative_axis_expanded_function_axes, keepdims=1)
E       y = opset18.Div(Softmax_test_softmax_negative_axis_expanded_function_X_Exp, Softmax_test_softmax_negative_axis_expanded_function_X_ReduceSum)
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 review from titaiwangms and Copilot May 9, 2025 14:24
@justinchuby justinchuby added the module: torchlib Related to the torch/aten function lib in development label May 9, 2025
@justinchuby justinchuby added this to the 0.2.6 milestone May 9, 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 fixes error cases in the scatter reduce operation by correcting the tensor initialization values for BFLOAT16, BOOL, and non-reduction modes.

  • Fixes the BFLOAT16 branch in the max reduction case by using torch.finfo(torch.bfloat16).max.
  • Adds proper handling for BOOL type in both min (False) and max (True) branches and ensures a consistent tensor type is returned for the "none" reduction case.
Comments suppressed due to low confidence (3)

onnxscript/function_libs/torch_lib/ops/core.py:7630

  • The addition of boolean handling in the 'min' branch for scatter reduce appears to resolve the error with BOOL types; please confirm that using 'False' is semantically correct for a minimum reduction with boolean values.
elif dtype == ir.DataType.BOOL:
                value = ir.tensor([False], dtype=dtype)

onnxscript/function_libs/torch_lib/ops/core.py:7643

  • Changing the BFLOAT16 case to use the maximum value for a max reduction correctly addresses the error, but please double-check that this change aligns with the intended behavior compared to the PyTorch semantics.
value = ir.tensor([torch.finfo(torch.bfloat16).max], dtype=dtype)

onnxscript/function_libs/torch_lib/ops/core.py:7656

  • Replacing the literal 0 with a tensor wrapping ensures consistency in type handling; please verify that this change maintains the expected behavior in downstream operations.
value = ir.tensor([0], dtype=dtype)

@github-project-automation github-project-automation bot moved this from Todo to Done in ONNX Script Review Board May 9, 2025
@justinchuby justinchuby enabled auto-merge (squash) May 9, 2025 15:07
@justinchuby justinchuby merged commit 8d98094 into main May 9, 2025
23 of 27 checks passed
@justinchuby justinchuby deleted the justinchu/fix-scatter-reduce branch May 9, 2025 15:23
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
module: torchlib Related to the torch/aten function lib in development
Projects
Development

Successfully merging this pull request may close these issues.

2 participants