#025

per_token_group_quant_fp8

fp8_e4m3 vllm · both · vllm.model_executor.layers.quantization.utils.fp8_utils · importance 0.5%

Reference Implementation

reference.py
import torch
import torch.nn as nn

class Model(nn.Module):

    def __init__(self, group_size: int) -> None:
        super().__init__()
        self.group_size = group_size

    def forward(self, x: torch.Tensor) -> dict[str, torch.Tensor]:
        (m, n) = x.shape
        if getattr(torch.version, 'hip', None) and hasattr(torch, 'float8_e4m3fnuz'):
            fp8_dtype = torch.float8_e4m3fnuz
            fp8_max = 224.0
        else:
            fp8_dtype = torch.float8_e4m3fn
            fp8_max = torch.finfo(torch.float8_e4m3fn).max
        x_fp32 = x.float()
        x_groups = x_fp32.view(m, n // self.group_size, self.group_size)
        x_abs_max = x_groups.abs().amax(dim=-1)
        scales = torch.clamp(x_abs_max / fp8_max, min=1e-10)
        quantized = (x_fp32 / scales.repeat_interleave(self.group_size, dim=1)).clamp(-fp8_max, fp8_max).to(fp8_dtype)
        return {'quantized': quantized, 'scales': scales}

Shapes

TSOL hardware:
# token_counthidden_size TSOL(XPU-A)TProdS
0 12048 0.00 us 7.60 us 0.0%
1 375042048 43.93 us 106.10 us 41.4%
2 750082048 87.86 us 199.60 us 44.0%
3 856322048 100.30 us 223.30 us 44.9%
4 1281922048 150.15 us 325.70 us 46.1%
5 2568962048 300.91 us 639.80 us 47.0%
6 5007362048 586.52 us 1.26 ms 46.6%
7 10290562048 1.21 ms 2.62 ms 46.0%
8 20581122048 2.41 ms 5.33 ms 45.2%
9 30871682048 3.62 ms 7.13 ms 50.7%
10 17168 0.00 us 7.60 us 0.0%
11 7267168 2.98 us 13.90 us 21.4%
12 9697168 3.97 us 15.60 us 25.4%
13 19337168 7.92 us 23.10 us 34.3%
14 41927168 17.19 us 47.60 us 36.1%
15 86677168 35.53 us 91.50 us 38.8%
16 167687168 68.74 us 154.10 us 44.6%
17 404487168 165.82 us 355.80 us 46.6%
18 586887168 240.60 us 515.10 us 46.7%

Input Generation

input.py
import torch


def _make_inputs(token_count: int, hidden_size: int) -> dict[str, torch.Tensor]:
    x = torch.randn(token_count, hidden_size, dtype=torch.bfloat16, device='cuda')
    return {'x': x}