Skip to content

Add add/remove initializers passes #2253

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 4 commits into from
May 2, 2025

Conversation

justinchuby
Copy link
Collaborator

@justinchuby justinchuby commented Apr 30, 2025

Implement RemoveInitializersFromInputsPass and AddInitializersToInputsPass in onnxscript/ir/passes/common/constant_manipulation.py.

  • RemoveInitializersFromInputsPass

    • Add RemoveInitializersFromInputsPass class to find and remove graph inputs with const_value.
    • Implement call method to remove inputs with const_value from graph.inputs.
    • Register RemoveInitializersFromInputsPass in the __all__ list.
  • AddInitializersToInputsPass

    • Add AddInitializersToInputsPass class to find and add initializers to the graph inputs.
    • Implement call method to add all initializers to the graph inputs if not already present.
    • Register AddInitializersToInputsPass in the __all__ list.
  • Tests

    • Add test cases for RemoveInitializersFromInputsPass in onnxscript/ir/passes/common/constant_manipulation_test.py to verify removal of inputs with const_value.
    • Add test cases for AddInitializersToInputsPass in onnxscript/ir/passes/common/constant_manipulation_test.py to verify addition of initializers to the graph inputs.

For more details, open the Copilot Workspace session.

Implement `RemoveInitializersFromInputsPass` and `AddInitializersToInputsPass` in `onnxscript/ir/passes/common/constant_manipulation.py`.

* **RemoveInitializersFromInputsPass**
  - Add `RemoveInitializersFromInputsPass` class to find and remove graph inputs with `const_value`.
  - Implement `call` method to remove inputs with `const_value` from `graph.inputs`.
  - Register `RemoveInitializersFromInputsPass` in the `__all__` list.

* **AddInitializersToInputsPass**
  - Add `AddInitializersToInputsPass` class to find and add initializers to the graph inputs.
  - Implement `call` method to add all initializers to the graph inputs if not already present.
  - Register `AddInitializersToInputsPass` in the `__all__` list.

* **Tests**
  - Add test cases for `RemoveInitializersFromInputsPass` in `onnxscript/ir/passes/common/constant_manipulation_test.py` to verify removal of inputs with `const_value`.
  - Add test cases for `AddInitializersToInputsPass` in `onnxscript/ir/passes/common/constant_manipulation_test.py` to verify addition of initializers to the graph inputs.

---

For more details, open the [Copilot Workspace session](https://copilot-workspace.githubnext.com/microsoft/onnxscript?shareId=XXXX-XXXX-XXXX-XXXX).
Copy link

codecov bot commented Apr 30, 2025

❌ 7 Tests Failed:

Tests completed Failed Passed Skipped
13873 7 13866 2338
View the top 3 failed test(s) by shortest run time
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0385_test_gathernd_example_int32
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.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_gathernd_example_int32'

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_gathernd_example_int32' (e=No module named 'tests.onnx_backend_test_code.test_gathernd_example_int32') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_gathernd_example_int32.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_gathernd_example_int32.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, INT64
E   from onnxscript.onnx_opset import opset13
E   
E   @script()
E   def bck_test_gathernd_example_int32(data: INT32[2,2], indices: INT64[2,2]) -> (INT32[2]):
E       output = opset13.GatherND(data, indices)
E       return output
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0507_test_gemm_default_matrix_bias
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_gemm_default_matrix_bias'

The above exception was the direct cause of the following exception:
.nox\test_torch_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_gemm_default_matrix_bias' (e=No module named 'tests.onnx_backend_test_code.test_gemm_default_matrix_bias') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_gemm_default_matrix_bias.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_gemm_default_matrix_bias.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_gemm_default_matrix_bias(a: FLOAT[3,6], b: FLOAT[6,4], c: FLOAT[3,4]) -> (FLOAT[3,4]):
E       y = opset13.Gemm(a, b, c)
E       return y
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0649_test_min_float32
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.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_min_float32'

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_min_float32' (e=No module named 'tests.onnx_backend_test_code.test_min_float32') (file: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_min_float32.py', absolute path: 'D:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_min_float32.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_float32(data_0: FLOAT[3], data_1: FLOAT[3]) -> (FLOAT[3]):
E       result = opset13.Min(data_0, data_1)
E       return result

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 marked this pull request as draft April 30, 2025 15:34
justinchuby added a commit that referenced this pull request May 1, 2025
- Remove the `remove_initialized_inputs` option in dce because the
contract of the pass it that it does not modify model signature. Fixed
bugs where initializers are removed. Instead, users can use
#2253 to remove the
initialized inputs first.
- Additionally updated RemoveUnusedOpsetsPass to always retain the
default opset.

---------

Co-authored-by: Copilot <[email protected]>
@justinchuby justinchuby marked this pull request as ready for review May 1, 2025 23:58
@justinchuby justinchuby requested review from titaiwangms, Copilot, gramalingam and shubhambhokare1 and removed request for titaiwangms May 1, 2025 23:58
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 implements two passes for constant manipulation in ONNX graphs by removing and adding initializers from/to graph inputs, and updates test cases accordingly.

  • Introduces RemoveInitializersFromInputsPass to filter initializer values from graph inputs.
  • Implements AddInitializersToInputsPass to add missing initializers to graph inputs.
  • Updates and expands tests in constant_manipulation_test.py.

Reviewed Changes

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

File Description
onnxscript/ir/passes/common/constant_manipulation.py Adds two new pass classes and registers them in all.
onnxscript/ir/passes/common/constant_manipulation_test.py Adds tests to verify the behavior of both RemoveInitializersFromInputsPass and AddInitializersToInputsPass.

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.

Put it into optimizer?

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

justinchuby commented May 2, 2025

Put it into optimizer?

For now pytorch exporter doesn't need it because we don't produce models with initializers being inputs by default. When users turn the option on they are requesting initializers to the inputs explicitly so we don't want to undo it for users. Maybe put into ort optimizations if needed?

@justinchuby justinchuby enabled auto-merge (squash) May 2, 2025 22:53
@justinchuby justinchuby merged commit 5283d9d into main May 2, 2025
21 of 27 checks passed
@justinchuby justinchuby deleted the justinchuby/add-remove-initializers-pass branch May 2, 2025 22:55
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.

2 participants