Make bmm batch invariant injection optional#12118
Conversation
Summary of ChangesHello @fzyzcjy, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed! This pull request introduces a new level of control over the batch invariant mode by making the inclusion of batch matrix multiplication (bmm) operations optional. This change allows users to selectively enable or disable bmm's batch invariant behavior, providing greater flexibility and potentially avoiding conflicts or performance issues in specific use cases without affecting other batch invariant operations. Highlights
Using Gemini Code AssistThe full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips. Invoking Gemini You can request assistance from Gemini at any point by creating a comment using either
Customization To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a Limitations & Feedback Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here. You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension. Footnotes
|
There was a problem hiding this comment.
Code Review
This pull request makes the batch matrix multiplication (bmm) batch invariant injection optional by adding an enable_bmm parameter to the enable_batch_invariant_mode function. The implementation is straightforward and correctly makes the bmm-related monkey-patching and library implementation conditional. While the change is correct, I've identified a pre-existing issue where the set_batch_invariant_mode context manager does not correctly restore torch.bmm after exiting, which could lead to unexpected behavior. I've left a detailed comment on this. Additionally, to improve the usability of this new feature, you might consider propagating the enable_bmm parameter to the set_batch_invariant_mode context manager.
| # Also monkeypatch torch.bmm directly as a fallback | ||
| _original_torch_bmm = torch.bmm | ||
| torch.bmm = bmm_batch_invariant |
There was a problem hiding this comment.
The monkey-patching of torch.bmm is a significant side effect. The set_batch_invariant_mode context manager does not revert this patch on exit, which can cause torch.bmm to remain patched and lead to unexpected behavior in code outside of the context.
While disable_batch_invariant_mode handles this correctly, the context manager should also ensure cleanup by calling it.
A more robust implementation of set_batch_invariant_mode might look like this:
@contextlib.contextmanager
def set_batch_invariant_mode(enabled: bool = True, **kwargs):
was_enabled = is_batch_invariant_mode_enabled()
# Storing full state to handle nesting might be more complex
if enabled == was_enabled:
yield
return
if enabled:
enable_batch_invariant_mode(**kwargs)
else:
disable_batch_invariant_mode()
try:
yield
finally:
if enabled and not was_enabled:
disable_batch_invariant_mode()
elif not enabled and was_enabled:
# Restore previous state. This is simplified; a full implementation
# might need to restore the exact previous configuration.
enable_batch_invariant_mode()Since set_batch_invariant_mode is not changed in this PR, I'm highlighting this pre-existing issue here as this change interacts with it. Fixing this would make the batch invariant mode much safer.
This reverts commit bed3044.
Motivation
Modifications
Accuracy Tests
Benchmarking and Profiling
Checklist