File size: 6,159 Bytes
0a2073b
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
import torch

from kernels.benchmark import Benchmark


_original_allclose = torch.allclose


def _bf16_max_ulp(input: torch.Tensor, other: torch.Tensor) -> int:
    got_bits = input.detach().cpu().view(torch.int16).to(torch.int32) & 0xFFFF
    exp_bits = other.detach().cpu().view(torch.int16).to(torch.int32) & 0xFFFF
    got_ordered = torch.where((got_bits & 0x8000) != 0, 0x8000 - (got_bits & 0x7FFF), got_bits)
    exp_ordered = torch.where((exp_bits & 0x8000) != 0, 0x8000 - (exp_bits & 0x7FFF), exp_bits)
    return int((got_ordered - exp_ordered).abs().max().item())


def _flashrt_allclose(input, other, rtol=1e-05, atol=1e-08, equal_nan=False):
    if input.dtype == torch.bfloat16 and other.dtype == torch.bfloat16:
        return _bf16_max_ulp(input, other) <= 5
    return _original_allclose(input, other, rtol=rtol, atol=atol, equal_nan=equal_nan)


torch.allclose = _flashrt_allclose


DECODE_SHAPES = [
    ("k4096_n1024", 4096, 1024),
    ("k4096_n4096", 4096, 4096),
    ("k4096_n12288", 4096, 12288),
    ("k12288_n1024", 12288, 1024),
    ("k12288_n4096", 12288, 4096),
    ("k12288_n12288", 12288, 12288),
]


def _swizzled_bytes(rows: int, cols: int) -> int:
    n_blocks = cols // 16
    return ((rows + 127) // 128) * ((n_blocks + 3) // 4) * 512


def _swizzle_constant_scale(rows: int, cols: int, value: int) -> torch.Tensor:
    return torch.full((_swizzled_bytes(rows, cols),), value, dtype=torch.uint8)


def _reference_swizzle(scales: torch.Tensor) -> torch.Tensor:
    rows, n_blocks = scales.shape
    n_col_super = (n_blocks + 3) // 4
    src = scales.cpu()
    out = torch.zeros(
        ((rows + 127) // 128) * n_col_super * 512,
        dtype=torch.uint8,
    )
    for row in range(rows):
        rb = row // 128
        ri = row % 128
        for block in range(n_blocks):
            cb = block // 4
            ci = block % 4
            super_idx = rb * n_col_super + cb
            inner_off = (ri % 32) * 16 + (ri // 32) * 4 + ci
            out[super_idx * 512 + inner_off] = src[row, block]
    return out


def _ue4m3_to_float(byte: int) -> float:
    sign = -1.0 if (byte & 0x80) else 1.0
    exp = (byte >> 3) & 0x0F
    mant = byte & 0x07
    if exp == 0:
        return sign * (mant / 8.0) * (2.0 ** -6)
    if exp == 15 and mant == 7:
        return 0.0
    return sign * (1.0 + mant / 8.0) * (2.0 ** (exp - 7))


def _ue4m3_lut() -> torch.Tensor:
    return torch.tensor([_ue4m3_to_float(i) for i in range(256)], dtype=torch.float32)


def _fp4_codebook() -> torch.Tensor:
    return torch.tensor(
        [
            0.0,
            0.5,
            1.0,
            1.5,
            2.0,
            3.0,
            4.0,
            6.0,
            -0.0,
            -0.5,
            -1.0,
            -1.5,
            -2.0,
            -3.0,
            -4.0,
            -6.0,
        ],
        dtype=torch.float32,
    )


def _unpack_fp4(packed: torch.Tensor) -> torch.Tensor:
    codebook = _fp4_codebook().to(packed.device)
    lo = packed & 0x0F
    hi = packed >> 4
    out = torch.empty(
        (packed.shape[0], packed.shape[1] * 2),
        device=packed.device,
        dtype=torch.float32,
    )
    out[:, 0::2] = codebook[lo.long()]
    out[:, 1::2] = codebook[hi.long()]
    return out


def _reference_smallm(
    a_packed: torch.Tensor,
    b_packed: torch.Tensor,
    sfa_linear: torch.Tensor,
    sfb_linear: torch.Tensor,
    K: int,
    alpha: float,
    chunk_rows: int = 256,
) -> torch.Tensor:
    device = b_packed.device
    N = b_packed.shape[0]
    lut = _ue4m3_lut().to(device)
    a = _unpack_fp4(a_packed.reshape(1, -1)).reshape(K)
    a_scale = lut[sfa_linear.reshape(-1).to(device).long()].repeat_interleave(16)
    a = a * a_scale
    sfb_linear = sfb_linear.to(device)
    out = torch.empty((N,), device=device, dtype=torch.bfloat16)
    for start in range(0, N, chunk_rows):
        end = min(start + chunk_rows, N)
        b = _unpack_fp4(b_packed[start:end])
        b_scale = lut[sfb_linear[start:end].long()].repeat_interleave(16, dim=1)
        expected = (b * b_scale * a.reshape(1, K)).sum(dim=1) * alpha
        out[start:end] = expected.to(torch.bfloat16)
    return out


class Nvfp4W4A4DecodeMatvecBenchmark(Benchmark):
    seed = 23

    def _setup_shape(self, K: int, N: int) -> None:
        torch.manual_seed(600 + K + N)
        if torch.cuda.is_available():
            torch.cuda.manual_seed_all(600 + K + N)
        self.K = K
        self.N = N
        self.alpha = 0.5
        self.a_packed = torch.randint(
            0, 256, (K // 2,), device=self.device, dtype=torch.uint8
        )
        self.b_packed = torch.randint(
            0, 256, (N, K // 2), device=self.device, dtype=torch.uint8
        )
        self.sfa_linear = torch.randint(0, 0x78, (1, K // 16), dtype=torch.uint8)
        self.sfb_linear = torch.randint(0, 0x78, (N, K // 16), dtype=torch.uint8)
        self.sfa = _reference_swizzle(self.sfa_linear).to(self.device)
        self.sfb = _reference_swizzle(self.sfb_linear).to(self.device)
        self.out = torch.empty((N,), device=self.device, dtype=torch.bfloat16)

    def _benchmark(self) -> None:
        self.kernel.nvfp4_w4a4_decode_matvec_bf16out(
            self.a_packed,
            self.b_packed,
            self.sfa,
            self.sfb,
            alpha=self.alpha,
            out=self.out,
        )

    def _reference(self) -> torch.Tensor:
        return _reference_smallm(
            self.a_packed,
            self.b_packed,
            self.sfa_linear,
            self.sfb_linear,
            self.K,
            self.alpha,
        )


def _register_shapes() -> None:
    for label, K, N in DECODE_SHAPES:

        def setup(self, K=K, N=N) -> None:
            self._setup_shape(K, N)

        def benchmark(self) -> None:
            self._benchmark()

        def verify(self) -> torch.Tensor:
            return self._reference()

        setattr(Nvfp4W4A4DecodeMatvecBenchmark, f"setup_{label}", setup)
        setattr(Nvfp4W4A4DecodeMatvecBenchmark, f"benchmark_{label}", benchmark)
        setattr(Nvfp4W4A4DecodeMatvecBenchmark, f"verify_{label}", verify)


_register_shapes()