Repository navigation
Revert "[Bugfix] Fix scaled_mm output narrowing for 3D input tensors" (#38093) #40423
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
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,7 +1,6 @@ | ||
| # SPDX-License-Identifier: Apache-2.0 | ||
| # SPDX-FileCopyrightText: Copyright contributors to the vLLM project | ||
|
|
||
| import math | ||
|
|
||
| import torch | ||
|
|
||
|
|
@@ -14,13 +13,6 @@ | |
| ) | ||
|
|
||
|
|
||
| def _get_num_tokens(output_shape: list) -> int: | ||
| # torch._scaled_mm works with 2D tensors, so input tensors are | ||
| # flattened if they are 3D. If output_shape is 3D, num_tokens is | ||
| # the product of all dims except the last (hidden dim). | ||
| return math.prod(output_shape[:-1]) | ||
|
|
||
|
|
||
| class TorchFP8ScaledMMLinearKernel(FP8ScaledMMLinearKernel): | ||
| """ | ||
| Base class for FP8 linear kernels using Torch. | ||
|
|
@@ -86,8 +78,7 @@ def apply_scaled_mm( | |
| if type(output) is tuple and len(output) == 2: | ||
| output = output[0] | ||
|
|
||
| num_tokens = _get_num_tokens(output_shape) | ||
| return torch.narrow(output, 0, 0, num_tokens).view(*output_shape) | ||
| return torch.narrow(output, 0, 0, output_shape[0]).view(*output_shape) | ||
|
|
||
|
|
||
| class RowWiseTorchFP8ScaledMMLinearKernel(TorchFP8ScaledMMLinearKernel): | ||
|
|
@@ -154,8 +145,7 @@ def apply_scaled_mm( | |
| bias=bias, | ||
| ) | ||
|
|
||
| num_tokens = _get_num_tokens(output_shape) | ||
| return torch.narrow(output, 0, 0, num_tokens).view(*output_shape) | ||
| return torch.narrow(output, 0, 0, output_shape[0]).view(*output_shape) | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
|
|
||
| class ChannelWiseTorchFP8ScaledMMLinearKernel(TorchFP8ScaledMMLinearKernel): | ||
|
|
@@ -216,9 +206,8 @@ def apply_scaled_mm( | |
| output = output[0] | ||
|
|
||
| # Unpad (undo num_token_padding) | ||
| num_tokens = _get_num_tokens(output_shape) | ||
| output = torch.narrow(output, 0, 0, num_tokens) | ||
| x_scale = torch.narrow(As, 0, 0, num_tokens) | ||
| output = torch.narrow(output, 0, 0, output_shape[0]) | ||
| x_scale = torch.narrow(As, 0, 0, output_shape[0]) | ||
|
Comment on lines
+209
to
+210
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
|
|
||
| # DQ | ||
| # C = sw * sx * (X * W) + bias | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Reverting this change re-introduces a bug for 3D input tensors. When the input
xis 3D (e.g.,[batch, seq, hidden]),output_shapeis[batch, seq, out_features]. Usingoutput_shape[0]as the narrowing size only selects the firstbatchtokens, whereas the flattened GEMM output containsbatch * seqtokens. This will cause aRuntimeErrorin the subsequent.view(*output_shape)call due to a size mismatch (unlessseq == 1). Since the base classFP8ScaledMMLinearKernel.apply_weightsexplicitly supports 3D tensors by flattening them, this implementation should correctly unpad all tokens using the product of all dimensions except the last one (e.g.,math.prod(output_shape[:-1])).