forked from vllm-project/vllm
-
-
Notifications
You must be signed in to change notification settings - Fork 2
Refine scheme #82
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
Open
yiliu30
wants to merge
5
commits into
ar-ext-m-moe
Choose a base branch
from
refine-scheme
base: ar-ext-m-moe
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Refine scheme #82
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
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
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
118 changes: 118 additions & 0 deletions
118
vllm/model_executor/layers/quantization/auto_round_vllm_extension/fp4_utils.py
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 |
|---|---|---|
| @@ -0,0 +1,118 @@ | ||
|
|
||
| from typing import Union, Optional | ||
| import torch | ||
|
|
||
|
|
||
|
|
||
| FLOAT_TO_E2M1 = [ | ||
| 0.0, | ||
| 0.5, | ||
| 1.0, | ||
| 1.5, | ||
| 2.0, | ||
| 3.0, | ||
| 4.0, | ||
| 6.0, | ||
| ] | ||
|
|
||
| # Module-level device tensor cache | ||
| _DEVICE_E2M1_TENSORS = {} | ||
|
|
||
| # Constants for FP4 values (E2M1 format) | ||
| _E2M1_VALUES = [0.0, 0.5, 1.0, 1.5, 2.0, 3.0, 4.0, 6.0] | ||
|
|
||
|
|
||
| def get_e2m1_tensor(device): | ||
| """Get device-specific E2M1 lookup tensor, creating it if needed.""" | ||
| device_str = str(device) | ||
| if device_str not in _DEVICE_E2M1_TENSORS: | ||
| _DEVICE_E2M1_TENSORS[device_str] = torch.tensor( | ||
| _E2M1_VALUES, dtype=torch.float32, device=device | ||
| ) | ||
| return _DEVICE_E2M1_TENSORS[device_str] | ||
|
|
||
|
|
||
|
|
||
|
|
||
| def pack_fp4_to_uint8(x: torch.Tensor) -> torch.Tensor: | ||
| m, n = x.shape | ||
| device = x.device | ||
|
|
||
| # Create lookup table for FP4 values to indices | ||
| # Map the absolute values to 0-7 indices | ||
| kE2M1 = get_e2m1_tensor(x.device) | ||
|
|
||
| # Find closest valid FP4 value index for each element | ||
| abs_x = torch.abs(x) | ||
| abs_diff_x = torch.abs(abs_x.unsqueeze(-1) - kE2M1) # [m, n, 8] | ||
| abs_indices = torch.argmin(abs_diff_x, dim=-1) # [m, n] | ||
|
|
||
| # Apply sign bit (bit 3) to get final 4-bit representation | ||
| indices = abs_indices + (torch.signbit(x).to(torch.long) << 3) | ||
|
|
||
| # Reshape to prepare for packing pairs of values | ||
| indices = indices.reshape(-1) | ||
|
|
||
| # Handle odd length by padding if necessary | ||
| if indices.numel() % 2 != 0: | ||
| indices = torch.cat([indices, torch.zeros(1, dtype=torch.long, device=device)]) | ||
|
|
||
| # Reshape to pair consecutive elements | ||
| indices = indices.reshape(-1, 2) | ||
|
|
||
| # Pack pairs of 4-bit values into 8-bit values | ||
| packed = (indices[:, 0] | (indices[:, 1] << 4)).to(torch.uint8) | ||
|
|
||
| return packed.reshape(m, n // 2) | ||
|
|
||
|
|
||
| def unpack_fp4_from_uint8( | ||
| a: torch.Tensor, m: int, n: int, dtype: Optional[torch.dtype] = torch.bfloat16 | ||
| ) -> torch.Tensor: | ||
| """ | ||
| Unpacks uint8 values into fp4. Each uint8 consists of two fp4 values | ||
| (i.e. first four bits correspond to one fp4 value, last four correspond to a | ||
| consecutive fp4 value). The bits represent an index, which are mapped to an fp4 | ||
| value. | ||
|
|
||
| :param a: tensor to unpack | ||
| :param m: original dim 0 size of the unpacked tensor | ||
| :param n: original dim 1 size of the unpacked tensor | ||
| :param dtype: dense dtype to cast the unpacked tensor to | ||
| """ | ||
| assert a.dtype == torch.uint8, f"expected uint8, got {a.dtype}" | ||
|
|
||
| # Vectorized nibble processing | ||
| a_flat = a.flatten() | ||
| high = (a_flat & 0xF0) >> 4 # Upper nibbles | ||
| low = a_flat & 0x0F # Lower nibbles | ||
|
|
||
| # Combine nibbles for batch processing | ||
| combined = torch.stack((low, high), dim=1).flatten() | ||
|
|
||
| # Vectorized sign and magnitude extraction | ||
| signs = (combined & 0x08).to(torch.bool) # Sign bits | ||
| abs_vals = (combined & 0x07).to(torch.long) # Magnitude indices | ||
|
|
||
| # Device-aware lookup and sign application | ||
| kE2M1 = get_e2m1_tensor(a.device) | ||
|
|
||
| values = kE2M1[abs_vals] * torch.where(signs, -1.0, 1.0) | ||
|
|
||
| # Reshape to final form | ||
| return values.reshape(m, n).to(dtype=dtype) | ||
|
|
||
|
|
||
|
|
||
| def cast_to_fp4(x): | ||
| sign = torch.sign(x) | ||
| x = torch.abs(x) | ||
| x[(x >= 0.0) & (x <= 0.25)] = 0.0 | ||
| x[(x > 0.25) & (x < 0.75)] = 0.5 | ||
| x[(x >= 0.75) & (x <= 1.25)] = 1.0 | ||
| x[(x > 1.25) & (x < 1.75)] = 1.5 | ||
| x[(x >= 1.75) & (x <= 2.5)] = 2.0 | ||
| x[(x > 2.5) & (x < 3.5)] = 3.0 | ||
| x[(x >= 3.5) & (x <= 5.0)] = 4.0 | ||
| x[x > 5.0] = 6.0 | ||
| return x * sign | ||
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
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
Oops, something went wrong.
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.
Following the removal of its only call site in
mxfp4_qdq_utils.py, this functioncast_to_fp4is now unused and can be removed. Additionally, its implementation using multiple masked assignments is inefficient on GPUs.