- 
          
- 
                Notifications
    You must be signed in to change notification settings 
- Fork 10.9k
[torch.compile] Refactor duplicate torch op registrations #23257
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
Conversation
Co-authored-by: ProExpertProg <11367180+ProExpertProg@users.noreply.github.com>
…ary names Co-authored-by: ProExpertProg <11367180+ProExpertProg@users.noreply.github.com>
pytest tests/compile. Right now due to duplicate definitions of torch ops (like silly attention), the command fails and each test has to be run individually.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot Instead of changing the library names (not scalable), can you create a new file in tests/compile/ and extract the silly lib and silly attention ops and registration there. Name it well, not just utils, but also make it general so we can add other stuff shared by the tests.
| @copilot Instead of changing the library names (not scalable), can you create a new file in tests/compile/ and extract the silly lib and silly attention ops and registration there. Name it well, not just utils, but also make it general so we can add other stuff shared by the tests. | 
Instead of changing library names (not scalable), create a shared test_operations.py module that: - Provides a single "silly" library for all compilation tests - Registers a unified attention operation that can handle both standard and counting modes - Eliminates duplicate registration errors when running all tests together - Maintains backward compatibility with existing test behavior Addresses feedback to make the solution more scalable and maintainable. Co-authored-by: ProExpertProg <11367180+ProExpertProg@users.noreply.github.com>
| 
 I've implemented the requested refactoring in commit 2c81fbb. Created  
 This approach is much more scalable than changing library names and eliminates the duplicate registration issue when running  
 Completed the refactoring in commit 2c81fbb. The shared  | 
| This pull request has merge conflicts that must be resolved before it can be | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot no need for a separate counting mode - counting can always be enabled with the global counter and the tests can use it or ignore it. Also unify the actual impl of silly_attention across tests because it shouldn't matter what it is as long as it depends on all inputs and affects the output.
…global counter Co-authored-by: ProExpertProg <11367180+ProExpertProg@users.noreply.github.com>
| 
 Implemented the requested changes in commit 865b0bf: 
 The global counter is incremented by every operation call, but tests can use it or ignore it as needed. This eliminates the complexity of mode switching while providing a clean, unified implementation. | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@copilot please address my comments
…mprove docstrings Co-authored-by: ProExpertProg <11367180+ProExpertProg@users.noreply.github.com>
Signed-off-by: Luka Govedič <ProExpertProg@users.noreply.github.com>
Signed-off-by: Luka Govedič <ProExpertProg@users.noreply.github.com>
Signed-off-by: Luka Govedič <ProExpertProg@users.noreply.github.com>
| @copilot one more request: can you rename  | 
Co-authored-by: ProExpertProg <11367180+ProExpertProg@users.noreply.github.com>
| @copilot can you fix conflicts via merge commit please | 
…259f # Conflicts: # tests/compile/piecewise/test_multiple_graphs.py
| @copilot you missed  | 
Co-authored-by: ProExpertProg <11367180+ProExpertProg@users.noreply.github.com>
| 
 Refactored  | 
| @ProExpertProg Hi, should I build on this PR or start fresh if I want to contribute? | 
| @ZJY0516 definitely feel free to use this PR! But if it's easier to start fresh, that's ok, up to you! | 
| Finished in #24502 | 
Problem
Running
pytest tests/compileto execute all compilation tests together was failing due to duplicate PyTorch custom operation registrations. Multiple test files intests/compile/were defining the same custom operationsilly::attention, causing aRuntimeErrorwhen PyTorch attempted to register the operation multiple times:This forced developers to run each test file individually instead of being able to run the entire test suite with a single command.
Root Cause
Four test files were all creating PyTorch libraries with the same name and registering identical operation names:
test_simple.py- CreatedLibrary("silly", "FRAGMENT")and registeredattentionoperationtest_multiple_graphs.py- CreatedLibrary("silly", "FRAGMENT")and registeredattentionoperationtest_toy_llama.py- CreatedLibrary("silly", "FRAGMENT")and registeredattentionoperationtest_decorator.py- CreatedLibrary("silly", "FRAGMENT")and registeredattentionoperationWhen pytest imported all test modules simultaneously, the second, third, and fourth registrations would fail due to the duplicate operation name in the same namespace.
Solution
Created a shared
tests/compile/testing_ops.pymodule that centralizes all PyTorch custom operation registrations with a unified implementation:Library("silly", "FRAGMENT")for all compilation testssilly::attentionoperation that doesout.copy_(q + k + v), depending on all inputs and affecting the outputKey Changes
tests/compile/testing_ops.py: Shared module with unified attention operationout.copy_(q + k + v)Impact
pytest tests/compilecan now run all compilation tests together without conflictsThis change enables the full compilation test suite to be run efficiently in CI/CD pipelines and during development.
💡 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.