Fix flashinfer autotune to only wrap run_once()#19004
Conversation
Summary of ChangesHello @ch-wan, 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 refines the FlashInfer autotuning mechanism by ensuring that the autotuning context is applied with greater precision. Previously, the autotuner wrapped setup operations like buffer creation and metadata initialization, which are not relevant to kernel performance. The changes now isolate the autotuning to only the actual kernel execution, improving the logical correctness of the autotuning process without impacting performance or model outputs. Highlights
🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console. Changelog
Activity
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 refactors the FlashInfer autotuning logic to more precisely wrap only the kernel execution (run_once) instead of the entire _dummy_run method. This is a good improvement for correctness and to avoid tuning unnecessary setup operations. However, the refactoring has inadvertently removed the torch.inference_mode() context, which could lead to performance issues and increased memory usage during autotuning. I've added a comment to restore it.
| self._dummy_run( | ||
| batch_size=self.req_to_token_pool.size, | ||
| run_ctx=autotune(), | ||
| ) |
There was a problem hiding this comment.
The torch.inference_mode() context manager was removed in this refactoring. This means that autotuning will now run with gradient tracking enabled, which can be slower and consume more memory. It should be added back to ensure autotuning is performed in inference mode.
| self._dummy_run( | |
| batch_size=self.req_to_token_pool.size, | |
| run_ctx=autotune(), | |
| ) | |
| with torch.inference_mode(): | |
| self._dummy_run( | |
| batch_size=self.req_to_token_pool.size, | |
| run_ctx=autotune(), | |
| ) |
fd291a0 to
75228b5
Compare
75228b5 to
26d2380
Compare
Motivation
The
_flashinfer_autotunemethod previously wrapped the entire_dummy_runcall inside theautotune()context. This meant that buffer creation,ForwardBatchconstruction, andattn_backend.init_forward_metadata()(which calls flashinfer's.plan()/.begin_forward()metadata operations) were all unnecessarily wrapped by the autotuner.Only the actual attention kernel execution inside
run_once()→self.model.forward(...)should be autotuned.Modifications
run_ctxparameter to_dummy_run()that accepts an optional context manager.autotune()context is now applied only aroundrun_once(), not the entire_dummy_run.empty_context()utility as the no-op fallback whenrun_ctxis not provided._dummy_run(warmup/profiling paths are unaffected).Accuracy Tests
N/A — no change to model outputs; this only narrows the scope of the autotune context.
Benchmarking and Profiling
N/A — no expected performance change; the same kernels are autotuned, just with a more precise context boundary.
Checklist
Review Process
/tag-run-ci-label,/rerun-failed-ci,/tag-and-rerun-ci