Repository navigation
Fix self.temp_O buffer shape for batched inference - #4099
danielhanchen wants to merge 1 commit into
Conversation
The temp_O output buffer was allocated as (1, bsz, hidden_size) but torch.matmul expects (bsz, 1, hidden_size) when bsz > 1. With the wrong shape, PyTorch silently resizes the output tensor, producing a UserWarning about deprecated resize behavior that will become an error in a future release. This only affects the Pattern A allocation path (when attention_size != hidden_size, e.g. Mistral Nemo 12b). The Pattern B alias path already gets the correct shape from temp_QA[1]. When bsz == 1, the shapes are identical so there is no behavioral change for single-sequence inference. Files changed: - unsloth/models/llama.py - unsloth/models/gemma2.py - unsloth/models/granite.py - unsloth/models/cohere.py - unsloth/models/qwen3.py - unsloth/models/falcon_h1.py
Summary of ChangesHello @danielhanchen, 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 addresses a critical tensor shape mismatch that previously caused 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 is an excellent pull request that fixes a subtle but important bug in the shape of the self.temp_O buffer for batched inference. The fix is correct, applied consistently across all relevant files, and will prevent a UserWarning from becoming a hard error in a future PyTorch release. The detailed explanation and thorough test plan are much appreciated. I have one minor suggestion regarding a potentially misleading comment.
| self.RH_Q = torch.empty((bsz, n_heads, 1, head_dim), dtype = dtype, device = device) | ||
| # Only for Gemma2 | ||
| self.temp_O = torch.empty((1, bsz, hidden_size), dtype = dtype, device = device) | ||
| self.temp_O = torch.empty((bsz, 1, hidden_size), dtype = dtype, device = device) |
There was a problem hiding this comment.
|
The same changes exist in #4100 |
|
Superseded by PR #4100 which includes the temp_O shape fix and all other batched generation fixes. |
Summary
self.temp_Oallocation shape from(1, bsz, hidden_size)to(bsz, 1, hidden_size)in 6 model filestorch.matmulto silently resize the output tensor whenbsz > 1, producingUserWarning: An output with one or more elements was resizedDetails
The Pattern A allocation path (triggered when
attention_size != hidden_size, e.g. Mistral Nemo 12b) allocatedself.temp_Oas(1, bsz, hidden_size). However,fast_linear_forwardcallstorch.matmul(X, W.t(), out=self.temp_O)whereXhas shape(bsz, 1, in_features), so the output should be(bsz, 1, hidden_size).The Pattern B alias path (
self.temp_O = self.temp_QA[1][:, :, :hidden_size]) already produces the correct(bsz, 1, hidden_size)shape. Whenbsz == 1, both shapes are identical(1, 1, hidden_size), so this only manifests with batched inference.Files changed
unsloth/models/llama.pyLlamaAttention_fast_forward_inferenceunsloth/models/gemma2.pyGemma2Attention_fast_forward_inferenceunsloth/models/granite.pyGraniteAttention_fast_forward_inferenceunsloth/models/cohere.pyCohereAttention_fast_forward_inferenceunsloth/models/qwen3.pyQwen3Attention_fast_forward_inferenceunsloth/models/falcon_h1.pyFalconH1Attention_fast_forward_inferenceTest plan
Test results: 20 models, batched generation (bsz=3), zero resize warnings
Note: This fix is independent of #4083 (batched left-padded generation). The shape bug pre-dates that PR and affects any batched inference path.