Skip to content

Make bmm batch invariant injection optional#12118

Merged
fzyzcjy merged 3 commits intosgl-project:mainfrom
fzyzcjy:feat/optional_bmm
Oct 26, 2025
Merged

Make bmm batch invariant injection optional#12118
fzyzcjy merged 3 commits intosgl-project:mainfrom
fzyzcjy:feat/optional_bmm

Conversation

@fzyzcjy
Copy link
Collaborator

@fzyzcjy fzyzcjy commented Oct 25, 2025

Motivation

Modifications

Accuracy Tests

Benchmarking and Profiling

Checklist

@gemini-code-assist
Copy link
Contributor

Summary of Changes

Hello @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

  • Optional BMM Injection: The enable_batch_invariant_mode function now includes an optional enable_bmm boolean parameter, defaulting to True.
  • Conditional BMM Monkey-Patching: The batch invariant injection for aten::bmm and the direct monkey-patching of torch.bmm are now conditional, only occurring if enable_bmm is set to True.
Using Gemini Code Assist

The 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 /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

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 .gemini/ folder in the base of the repository. Detailed instructions can be found here.

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

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

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.

Comment on lines +546 to +548
# Also monkeypatch torch.bmm directly as a fallback
_original_torch_bmm = torch.bmm
torch.bmm = bmm_batch_invariant
Copy link
Contributor

Choose a reason for hiding this comment

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

high

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.
@fzyzcjy fzyzcjy merged commit c001deb into sgl-project:main Oct 26, 2025
99 of 104 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants

Comments