Skip to content

[rewriter] Decouple llama rule sets and make API explicit #2388

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

Open
wants to merge 8 commits into
base: main
Choose a base branch
from

Conversation

Copilot
Copy link

@Copilot Copilot AI commented Jun 14, 2025

This PR addresses the misleading naming and tangled organization of rewrite rules by decoupling the llama_rule_sets.py module and creating a more explicit API.

Problem

The original llama_rule_sets.py contained general optimization rules that weren't specific to Llama models, making the naming misleading. The API didn't explicitly specify what rules were being applied, making it unclear what optimizations were happening.

# Before: Unclear what this does
from onnxscript.rewriter import llama_rule_sets
rules = llama_rule_sets.llama_p0_rule_set()  # What rules? Why "llama"? What's "p0"?

Solution

1. Created basic_rules.py with explicit naming

  • Moved all general optimization rules to a new basic_rules.py module
  • Used descriptive function name: basic_optimization_rules()
  • Added comprehensive documentation for each rule

2. Made API explicit for fine-grained control

# New explicit API - users know exactly what they're getting
from onnxscript.rewriter import basic_rules

# Use all basic optimizations (recommended default)
rules = basic_rules.basic_optimization_rules()

# Or use specific individual rules
transpose_rule = basic_rules.transpose_identity_rule
cast_rule = basic_rules.cast_identity_rule

# Or create custom rule combinations
custom_rules = basic_rules.orp.RewriteRuleSet([
    basic_rules.transpose_identity_rule,
    basic_rules.cast_identity_rule,
])

3. Updated default rewriter to be explicit

# Before (in rewriter/__init__.py)
*llama_rule_sets.llama_p0_rule_set().rules,

# After - much clearer what's being applied
*basic_rules.basic_optimization_rules().rules,

4. Maintained backward compatibility

  • llama_rule_sets.py now serves as a compatibility wrapper
  • All existing APIs continue to work with deprecation warnings
  • Existing tests pass unchanged

Available Rules

The new API provides access to these optimization rules:

  • cast_cast_rule - Eliminates consecutive casts
  • cast_identity_rule - Removes redundant casts
  • expand_identity_rule - Removes no-op expands
  • reshape_reshape_rule - Combines consecutive reshapes
  • slice_split_rule - Converts slices to splits when beneficial
  • transpose_identity_rule - Removes identity transposes
  • transpose_transpose_rule - Combines consecutive transposes
  • unsqueeze_unsqueeze_rule - Combines consecutive unsqueezes
  • squeeze_reshape_1d_rule - Optimizes 1D squeeze+reshape patterns

Migration

# OLD (deprecated but still works)
from onnxscript.rewriter import llama_rule_sets
rules = llama_rule_sets.llama_p0_rule_set()

# NEW (recommended)
from onnxscript.rewriter import basic_rules
rules = basic_rules.basic_optimization_rules()

This change resolves the core issue by making the optimizer API explicitly specify what rules are being applied, while providing users with fine-grained control over optimization behavior.

Fixes #2128.


💬 Share your feedback on Copilot coding agent for the chance to win a $200 gift card! Click here to start the survey.

@Copilot Copilot AI changed the title [WIP] [rewriter] Decouple llama rule sets [rewriter] Decouple llama rule sets and make API explicit Jun 14, 2025
@Copilot Copilot AI requested a review from justinchuby June 14, 2025 15:50
Copilot finished work on behalf of justinchuby June 14, 2025 15:50
Copy link
Collaborator

@justinchuby justinchuby left a comment

Choose a reason for hiding this comment

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

Please rename onnxscript/rewriter/llama_rule_sets_test.py and remove onnxscript/rewriter/llama_rule_sets.py entirely

Copy link
Author

Copilot AI commented Jun 14, 2025

Please rename onnxscript/rewriter/llama_rule_sets_test.py and remove onnxscript/rewriter/llama_rule_sets.py entirely

Done! Removed onnxscript/rewriter/llama_rule_sets.py entirely and renamed onnxscript/rewriter/llama_rule_sets_test.py to deprecated_llama_rule_sets_test.py. The backward compatibility layer has been completely eliminated as requested. See commit 362095c.

Copilot finished work on behalf of justinchuby June 14, 2025 16:07
@justinchuby justinchuby requested a review from titaiwangms June 14, 2025 16:09
@justinchuby justinchuby marked this pull request as ready for review June 14, 2025 16:09
Copy link
Collaborator

Choose a reason for hiding this comment

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

Just move everything to onnxscript/rewriter/basic_rules_test.py and remove this file

Copy link
Author

Choose a reason for hiding this comment

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

Done! Removed deprecated_llama_rule_sets_test.py entirely since all its test functionality was already present in basic_rules_test.py with better naming. All tests continue to pass. See commit af80d0d.

Copy link

codecov bot commented Jun 14, 2025

❌ 3 Tests Failed:

Tests completed Failed Passed Skipped
16441 3 16438 2361
View the top 3 failed test(s) by shortest run time
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0250_test_clip_outbounds
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.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_clip_outbounds'

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_clip_outbounds' (e=No module named 'tests.onnx_backend_test_code.test_clip_outbounds') (file: 'C:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_clip_outbounds.py', absolute path: 'C:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_clip_outbounds.py', current folder: C:\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_clip_outbounds(x: FLOAT[3], min: FLOAT, max: FLOAT) -> (FLOAT[3]):
E       y = opset13.Clip(x, min, max)
E       return y
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_0668_test_mod_mixed_sign_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.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_mod_mixed_sign_int16'

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_mod_mixed_sign_int16' (e=No module named 'tests.onnx_backend_test_code.test_mod_mixed_sign_int16') (file: 'C:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_mod_mixed_sign_int16.py', absolute path: 'C:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_mod_mixed_sign_int16.py', current folder: C:\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 INT16
E   from onnxscript.onnx_opset import opset13
E   
E   @script()
E   def bck_test_mod_mixed_sign_int16(x: INT16[6], y: INT16[6]) -> (INT16[6]):
E       z = opset13.Mod(x, y)
E       return z
onnxscript.backend.onnx_export_test.TestOnnxBackEnd::test_export2python_produces_correct_onnx_script_model_1104_test_shape_start_1_end_2
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.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_shape_start_1_end_2'

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_shape_start_1_end_2' (e=No module named 'tests.onnx_backend_test_code.test_shape_start_1_end_2') (file: 'C:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_shape_start_1_end_2.py', absolute path: 'C:\\a\\onnxscript\\onnxscript\\tests\\onnx_backend_test_code\\test_shape_start_1_end_2.py', current folder: C:\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 opset21
E   
E   @script()
E   def bck_test_shape_start_1_end_2(x: FLOAT[3,4,5]) -> (INT64[1]):
E       y = opset21.Shape(x, end=2, start=1)
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.

Copilot finished work on behalf of justinchuby June 14, 2025 16:15
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.

lint

rule_set.apply_to_model(model)
rewritten_model = ir.serde.serialize_model(model)
rule.apply_to_model(model)
_rewritten_model = ir.serde.serialize_model(model)

Check notice

Code scanning / CodeQL

Unused local variable Note

Variable _rewritten_model is not used.

Copilot Autofix

AI about 7 hours ago

To fix the issue, the assignment to _rewritten_model on line 160 should be removed entirely, as the variable is not used anywhere in the code. This ensures that the code is clean and avoids unnecessary assignments. Since the right-hand side of the assignment (ir.serde.serialize_model(model)) does not have any side effects, it can be safely removed without impacting the functionality of the code.


Suggested changeset 1
onnxscript/rewriter/basic_rules_test.py

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/onnxscript/rewriter/basic_rules_test.py b/onnxscript/rewriter/basic_rules_test.py
--- a/onnxscript/rewriter/basic_rules_test.py
+++ b/onnxscript/rewriter/basic_rules_test.py
@@ -159,3 +159,3 @@
         rule.apply_to_model(model)
-        _rewritten_model = ir.serde.serialize_model(model)
+        # Removed unused variable _rewritten_model
 
EOF
@@ -159,3 +159,3 @@
rule.apply_to_model(model)
_rewritten_model = ir.serde.serialize_model(model)
# Removed unused variable _rewritten_model

Copilot is powered by AI and may make mistakes. Always verify output.
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.

[rewriter] Decouple llama rule sets
3 participants