vllm.models.deepseek_v41.nvidia.flash_mla_mega_attn
¶
DeepSeek V4.1 attention on FlashMLA's mega-attention kernel (SM100).
One kernel does Q RoPE, sparse attention, the inverse RoPE of the output and
its FP8 cast, and writes straight into the buffer wo_a consumes. So the
layer declares accepts_unnormed_unroped_query -- the kernel RoPEs Q itself,
leaving the fused KV insert only the zero-pad to the kernel's head count -- and
its _alloc_attn_out / _o_proj pair speaks QuantizedActivation instead
of bf16, since the output needs no rotation or quantization here.
wq_b rows and wo_a columns are permuted once at load so the surrounding
GEMMs speak the kernel's chunk-interleaved layouts directly. A step's prefill
and decode segments write disjoint token ranges of one output buffer pair, so
a single wo_a einsum covers the whole step.
Classes:
-
DeepseekV4MegaAttnAttention–FlashMLA mega-attention layer for DeepSeek V4.1 (SM100).
Functions:
-
alloc_mega_attn_output–Allocate the output buffer pair the mega-attention kernels write into.
-
is_flashmla_mega_attn_supported–Whether FlashMLA's fused mega-attention kernels are usable here.
DeepseekV4MegaAttnAttention
¶
Bases: DeepseekV4FlashMLAAttention
FlashMLA mega-attention layer for DeepSeek V4.1 (SM100).
Methods:
-
finalize_loaded_weights–Permute wq_b rows / wo_a columns into the kernel's layouts.
-
is_available_for–Whether this layer can serve the configured model on this device.
Source code in vllm/models/deepseek_v41/nvidia/flash_mla_mega_attn.py
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 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 | |
_decode_compressed_kv_and_topk(flashmla_metadata, swa_metadata)
¶
The compressed cache and its [n, topk] slot indices / lengths.
Source code in vllm/models/deepseek_v41/nvidia/flash_mla_mega_attn.py
_forward_decode_mega(q, positions_int32, flashmla_metadata, swa_metadata, out)
¶
Mega attention over the paged quantized caches, in place into out.
The caches are [num_blocks, page, 1, bytes], the record taken from
bytes (528 V4.1 fp8, 288 V4.1 NVFP4; NVFP4 only as the compressed
cache beside an fp8 SWA one), and the indices [s_q, topk] int32
slot ids (block * page + offset, -1 invalid).
Source code in vllm/models/deepseek_v41/nvidia/flash_mla_mega_attn.py
_o_proj(attn_out, positions)
¶
Grouped wo_a then wo_b over the kernel's quantized output.
The inverse RoPE and the FP8 cast happened inside the attention
kernel, and wo_a is permuted for its output layout, so there is
nothing to rotate or quantize here and positions is unused. The
scale already is DeepGEMM's packed-ue8m0 MN-major layout, so the
einsum consumes the kernel's output with no repacking.
Source code in vllm/models/deepseek_v41/nvidia/flash_mla_mega_attn.py
finalize_loaded_weights()
¶
Permute wq_b rows / wo_a columns into the kernel's layouts.
Idempotent: a second post-load pass must not permute twice.
Source code in vllm/models/deepseek_v41/nvidia/flash_mla_mega_attn.py
is_available_for(vllm_config)
classmethod
¶
Whether this layer can serve the configured model on this device.
The kernel is SM100-only and wants WV_GROUP_SIZE heads per wo_a
group, which stops holding once TP divides the head count far enough
(TP16 on a 64-head model leaves 4). __init__ raises on both, so the
default-backend selector asks here first rather than turning an
unsupported topology into a startup crash.
Source code in vllm/models/deepseek_v41/nvidia/flash_mla_mega_attn.py
_token_slice(out, start, end)
¶
The [start, end) token slice of a mega-attention output buffer.
Source code in vllm/models/deepseek_v41/nvidia/flash_mla_mega_attn.py
alloc_mega_attn_output(num_tokens, n_wv_group, device)
¶
Allocate the output buffer pair the mega-attention kernels write into.
One pair per forward step: the prefill and decode segments fill disjoint
token ranges, so a single fp8_einsum over all num_tokens can
consume the result instead of one call per segment.
The scale buffer is MN-major -- stride 1 along tokens, head-dim stride
ceil4(num_tokens) -- which is both what the kernel requires of a
caller-provided buffer and what DeepGEMM expects for this N.
Source code in vllm/models/deepseek_v41/nvidia/flash_mla_mega_attn.py
is_flashmla_mega_attn_supported()
¶
Whether FlashMLA's fused mega-attention kernels are usable here.