Skip to content

Conversation

Copilot
Copy link
Contributor

@Copilot Copilot AI commented Jul 1, 2025

The fail helper function in onnxscript/rewriter/redundant_scatter_nd.py was incorrectly passing multiple arguments to MatchResult.fail(), causing a TypeError when pattern matching failed.

Problem

The error occurred when the rewriter tried to report match failures with multiple failure sources:

return fail("The shape of 'data' and 'updates' are different.", data, updates)

This resulted in:

TypeError: MatchResult.fail() takes from 1 to 3 positional arguments but 4 were given

The issue was that MatchResult.fail() only accepts 2 parameters after self:

  • reason: str - the failure reason
  • failure_source: Union[ir.Node, ir.Value, list[...]] | None - a single item or list of failure sources

But the helper function was passing all arguments directly: MatchResult().fail(*args).

Solution

Modified the fail helper function to properly handle multiple failure sources by collecting them into a list when calling MatchResult.fail():

def fail(reason, *failure_sources):
    if failure_sources:
        return onnxscript.rewriter.MatchResult().fail(reason, list(failure_sources))
    else:
        return onnxscript.rewriter.MatchResult().fail(reason)

This change:

  • ✅ Fixes the TypeError for calls with multiple failure sources
  • ✅ Maintains backward compatibility for existing single-argument calls
  • ✅ Follows the same pattern used correctly in other rewriter modules like matmul_add_to_gemm.py

Testing

Verified that all existing call patterns in the file work correctly:

  • fail("message") - reason only
  • fail("message", node) - reason + single source
  • fail("message", node1, node2) - reason + multiple sources

Fixes #2430.


💡 You can make Copilot smarter by setting up custom instructions, customizing its development environment and configuring Model Context Protocol (MCP) servers. Learn more Copilot coding agent tips in the docs.

Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
@Copilot Copilot AI changed the title [WIP] Fix onnxscript/rewriter/redundant_scatter_nd.py Fix MatchResult.fail() call signature in redundant_scatter_nd.py Jul 1, 2025
Copilot finished work on behalf of justinchuby July 1, 2025 19:05
@Copilot Copilot AI requested a review from justinchuby July 1, 2025 19:05
@justinchuby
Copy link
Collaborator

Just create the MatchResult object in check() and call result.fail() inside the method. Remove the fail() function.

…methods

Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jul 1, 2025

Just create the MatchResult object in check() and call result.fail() inside the method. Remove the fail() function.

Done! I've removed the fail() function and now create the MatchResult object in each check() method directly, calling result.fail() as requested. The changes follow the same pattern used in other rewriter modules. (commit 1902c59)

Copilot finished work on behalf of justinchuby July 1, 2025 19:28
@justinchuby justinchuby requested a review from gramalingam July 1, 2025 19:35
@justinchuby justinchuby marked this pull request as ready for review July 1, 2025 19:36
Copy link

codecov bot commented Jul 1, 2025

❌ 7 Tests Failed:

Tests completed Failed Passed Skipped
14956 7 14949 2346
View the top 3 failed test(s) by shortest run time
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0389_test_gather_2d_indices
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_gather_2d_indices'

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_gather_2d_indices' (e=No module named 'tests.onnx_backend_test_code.test_gather_2d_indices') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_gather_2d_indices.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_gather_2d_indices.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_gather_2d_indices(data: FLOAT[3,3], indices: INT64[1,2]) -> (FLOAT[3,1,2]):
E       y = opset13.Gather(data, indices, axis=1)
E       return y
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0656_test_min_two_inputs
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_min_two_inputs'

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_min_two_inputs' (e=No module named 'tests.onnx_backend_test_code.test_min_two_inputs') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_min_two_inputs.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_min_two_inputs.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 opset13
E   
E   @script()
E   def bck_test_min_two_inputs(data_0: FLOAT[3], data_1: FLOAT[3]) -> (FLOAT[3]):
E       result = opset13.Min(data_0, data_1)
E       return result
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_1111_test_sigmoid
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_sigmoid'

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_sigmoid' (e=No module named 'tests.onnx_backend_test_code.test_sigmoid') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_sigmoid.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_sigmoid.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 opset13
E   
E   @script()
E   def bck_test_sigmoid(x: FLOAT[3,4,5]) -> (FLOAT[3,4,5]):
E       y = opset13.Sigmoid(x)
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
Copy link
Collaborator

@copilot please run lintrunner f to format the files

Co-authored-by: justinchuby <11205048+justinchuby@users.noreply.github.com>
Copy link
Contributor Author

Copilot AI commented Jul 2, 2025

@copilot please run lintrunner f to format the files

Done! I've run lintrunner f to format the files. The formatter made some line-breaking changes to improve readability. (commit 138568a)

Copilot finished work on behalf of justinchuby July 2, 2025 22:48
@justinchuby justinchuby enabled auto-merge (squash) July 3, 2025 00:04
@justinchuby justinchuby assigned titaiwangms and unassigned justinchuby and Copilot Jul 3, 2025
@justinchuby justinchuby requested a review from titaiwangms July 3, 2025 00:26
@justinchuby justinchuby merged commit 138cb30 into main Jul 3, 2025
26 of 32 checks passed
@justinchuby justinchuby deleted the copilot/fix-2430 branch July 3, 2025 00:55
@github-project-automation github-project-automation bot moved this from Todo to Done in ONNX Script Review Board Jul 3, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
Development

Successfully merging this pull request may close these issues.

Fix onnxscript/rewriter/redundant_scatter_nd.py
4 participants