Skip to content

Conversation

KarelZe
Copy link
Contributor

@KarelZe KarelZe commented Jun 17, 2025

SkipLayerNormFusion does currently not fuse ops, if stash_type is at default (=1) or epsilon is at default (=1e-5) for LayerNormalization and SimplifiedLayerNormalization

This pr:

  • fixes handling default attrs in LayerNormalization, SimplifiedLayerNormalization
  • adds BART encoder as new test model. I added this model as some of the stash types are at default. The model is versatile and can also be used to test other fusions e.g., EmbedLayerNormalization.
  • allows for commuted inputs.

Closes #2378.

@shubhambhokare1 @justinchuby Could you please review? Any feedback is greatly appreciated.

Copy link

codecov bot commented Jun 17, 2025

❌ 5 Tests Failed:

Tests completed Failed Passed Skipped
1383 5 1378 688
View the top 3 failed test(s) by shortest run time
::onnxscript.tools.training_helper
Stack Traces | 0s run time
ImportError while importing test module '.../onnxscript/tools/training_helper.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
.../hostedtoolcache/Python/3.11.13.../x64/lib/python3.11/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
onnxscript/tools/training_helper.py:6: in <module>
    from torch.onnx import _OrtBackend, _OrtBackendOptions
E   ImportError: cannot import name '_OrtBackend' from 'torch.onnx' (.../onnxscript/onnxscript/.nox.../test_torch_nightly/lib/python3.11.../torch/onnx/__init__.py)
::onnxscript.tools.transformers_models.llama_test
Stack Traces | 0s run time
ImportError while importing test module '.../tools/transformers_models/llama_test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
.../hostedtoolcache/Python/3.11.13.../x64/lib/python3.11/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.../tools/transformers_models/llama_test.py:12: in <module>
    import onnxscript.tools.training_helper
onnxscript/tools/training_helper.py:6: in <module>
    from torch.onnx import _OrtBackend, _OrtBackendOptions
E   ImportError: cannot import name '_OrtBackend' from 'torch.onnx' (.../onnxscript/onnxscript/.nox.../test_torch_nightly/lib/python3.11.../torch/onnx/__init__.py)
::onnxscript.tools.transformers_models.mistral_test
Stack Traces | 0s run time
ImportError while importing test module '.../tools/transformers_models/mistral_test.py'.
Hint: make sure your test modules/packages have valid Python names.
Traceback:
.../hostedtoolcache/Python/3.11.13.../x64/lib/python3.11/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
.../tools/transformers_models/mistral_test.py:14: in <module>
    import onnxscript.tools.training_helper
onnxscript/tools/training_helper.py:6: in <module>
    from torch.onnx import _OrtBackend, _OrtBackendOptions
E   ImportError: cannot import name '_OrtBackend' from 'torch.onnx' (.../onnxscript/onnxscript/.nox.../test_torch_nightly/lib/python3.11.../torch/onnx/__init__.py)

To view more test analytics, go to the Test Analytics Dashboard
📋 Got 3 mins? Take this short survey to help us improve Test Analytics.

@KarelZe KarelZe marked this pull request as ready for review June 17, 2025 12:13
@KarelZe KarelZe marked this pull request as draft June 17, 2025 12:47
@justinchuby justinchuby requested review from gramalingam and Copilot and removed request for gramalingam June 17, 2025 15:57
@justinchuby
Copy link
Collaborator

@gramalingam

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 how default attributes (epsilon, stash_type) are handled in both LayerNormalization and SimplifiedLayerNormalization fusions, adds a BART encoder model to the fusion tests, and introduces commuted-input support for SkipLayerNormalization rules.

  • Extract default epsilon from the matched node instead of requiring it in the pattern signature
  • Add test_bart_encoder to validate fusion with default-attribute cases
  • Enable commuted-input variants by applying .commute() to fusion rules

Reviewed Changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated 1 comment.

File Description
skip_normalization_test.py Added test_bart_encoder to cover default-attribute fusions
skip_normalization.py Refactored patterns to drop default attrs, extract epsilon in rewrite, and apply rule commutation
Comments suppressed due to low confidence (2)

onnxscript/rewriter/ort_fusions/skip_normalization_test.py:73

  • The test uses fuse_skip_layer_normalization(model) but there is no import for that symbol in this file. Please add from onnxscript.rewriter.ort_fusions.skip_normalization import fuse_skip_layer_normalization (or adjust the import path) to ensure the function is available.
        fuse_skip_layer_normalization(model)

onnxscript/rewriter/ort_fusions/skip_normalization.py:231

  • The new .commute() calls are applied only to the full SkipLayerNormalization rules. To allow commuted inputs for SkipSimplifiedLayerNormalization as well, you should apply .commute() to the simplified-layer ruleset (if defined) or include those here before applying apply_fusion_rules.
skip_layer_normalization_ruleset = pattern.RewriteRuleSet(

@KarelZe KarelZe marked this pull request as ready for review June 18, 2025 04:14
@justinchuby justinchuby enabled auto-merge (squash) July 3, 2025 18:57
@justinchuby justinchuby disabled auto-merge July 3, 2025 18:58
@justinchuby
Copy link
Collaborator

Looks like there are some comments unaddressed

@KarelZe
Copy link
Contributor Author

KarelZe commented Jul 6, 2025

@justinchuby Sorry, was busy at work. Resolved one issue, for the other I'd need @gramalingam s input. 👍

@titaiwangms titaiwangms self-requested a review July 10, 2025 17:37
@justinchuby
Copy link
Collaborator

@KarelZe feel free to take a look at the final round of comments. After that I think we are ready to merge. Thanks!

@KarelZe
Copy link
Contributor Author

KarelZe commented Jul 15, 2025

@justinchuby Should be complete now🙌

Just one more question, would you be open to /interested in adding EmbedLayerNormalization fusion to onnxscript? I could maybe contribute some rewrite rules in a separate pr.

@KarelZe KarelZe requested a review from gramalingam July 15, 2025 20:01
@justinchuby
Copy link
Collaborator

@justinchuby Should be complete now🙌

Just one more question, would you be open to /interested in adding EmbedLayerNormalization fusion to onnxscript? I could maybe contribute some rewrite rules in a separate pr.

Thanks! Would let @gramalingam comment

@justinchuby justinchuby enabled auto-merge (squash) July 16, 2025 15:19
@gramalingam
Copy link
Collaborator

@justinchuby Should be complete now🙌

Just one more question, would you be open to /interested in adding EmbedLayerNormalization fusion to onnxscript? I could maybe contribute some rewrite rules in a separate pr.

Sure, that would be great, thanks!

@justinchuby justinchuby merged commit 061f62b into microsoft:main Jul 18, 2025
26 of 32 checks passed
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.

Handling of default attrs of LayerNormalization in SkipLayerNormFusion
3 participants