Commit 63b4c4d
authored
Enable 128k context for Gemma4-31B CUDA (pytorch#20316)
## Enable 128k context for Gemma4-31B (CUDA, TurboQuant TQ4 KV)
### What & why
This PR enables 128k context end-to-end (export + C++ CUDA runtime) by
using TurboQuant **TQ4 (4-bit)** format and fixing the fused TQ4
attention kernel so decode/prefill scale with the *actual* context
length and are CUDA-graph capturable. The 50 sliding-window layers are
unchanged (2,048-entry ring cache).
With these fixes, enabling long context is just `--max-seq-len 131072
--turboquant`.
### Changes
- **`TurboQuantKVCache.update` → `index_copy_`**: write the compressed
cache via a static
scatter so the decode step is CUDA-graph-capturable (the previous
slice-assignment
lowered to `index_put_`, which breaks graph capture).
- **`tq4_sdpa` kv_len clamp**: an optional on-device int32 `kv_len`
scalar bounds the
kernel's KV loop to the filled context instead of the full pre-allocated
131,072 buffer
→ decode/prefill become **O(context)** instead of O(max_seq_len). It is
read on-device
(no host sync) so it stays correct across CUDA-graph replays.
`kv_len=None` falls back
to the original full-loop behavior (the shared Qwen path is
byte-identical).
- **`tq4_sdpa` prefill causal block-skip**: skip fully-masked causal
upper-triangle KV
blocks during chunked prefill.
- **Tests**: extend `test_tq4_sdpa.py` to cover the actual 128k paths —
long-KV kv_len
clamp (with a large-magnitude "garbage tail" beyond `kv_len` as a
built-in negative
control) and bottom-right `mask_is_causal` chunked-prefill, at
Gemma-global (D=512) and
Qwen (D=256) shapes, plus a gated 131,072 case. Runs in the existing
`unittest-cuda` CI.
### Results
Model: Gemma4-31B (GGUF int6 weights) + TQ4 KV @128k. Hardware: A100
80GB. C++ CUDA
runner, `--cuda_graph`, temperature 0.
- **Works e2e**: 128k export + C++ CUDA runtime produce coherent output.
- **Memory**: runtime peak **~27.0 GiB** (export peak 32.05 GiB) —
runtime fits a 32 GB
card, and is context-independent (KV buffers are pre-allocated at load).
- **Quality**: needle-in-haystack retrieval is exact; per-position logit
cosine of
TQ4-KV vs bf16-KV is ~0.9997 and holds to the full 128k.
Throughput (decode is the instantaneous, windowed rate measured at each
KV depth):
| Phase | Context length | Throughput (tok/s) |
|---------|---------------:|-------------------:|
| Prefill | 2,048 | 2,247 |
| Decode | 128 | 45.6 |
| Decode | 512 | 43.4 |
| Decode | 2,048 | 36.5 |
| Decode | 8,192 | 22.3 |
| Decode | 32,768 | 8.7 |
| Decode | 131,072 | N/A (too long) |
### Known limitation / follow-up
1. Decode throughput degrades with context depth: the global-layer TQ4
attention is
O(context) and currently launches few CTAs (no split-K), so deep-context
decode is
under-parallelized. Adding split-K / flash-decoding to `tq4_sdpa` is the
natural
follow-up to speed up decode at long context.
2. Exportation memory consumption is too large to fit in consumer-based
GPU like 5090, make it impossible to export on user server. Should
optimize the gguf exportation path for better support.1 parent 0e65ba6 commit 63b4c4d
5 files changed
Lines changed: 364 additions & 8 deletions
File tree
- backends/cuda
- tests
- triton/kernels
- examples/models/gemma4_31b
- extension/llm/modules/turboquant
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
438 | 438 | | |
439 | 439 | | |
440 | 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 | + | |
| 483 | + | |
| 484 | + | |
| 485 | + | |
| 486 | + | |
| 487 | + | |
| 488 | + | |
| 489 | + | |
| 490 | + | |
| 491 | + | |
| 492 | + | |
| 493 | + | |
| 494 | + | |
| 495 | + | |
| 496 | + | |
| 497 | + | |
| 498 | + | |
| 499 | + | |
| 500 | + | |
| 501 | + | |
| 502 | + | |
| 503 | + | |
| 504 | + | |
| 505 | + | |
| 506 | + | |
| 507 | + | |
| 508 | + | |
| 509 | + | |
| 510 | + | |
| 511 | + | |
| 512 | + | |
| 513 | + | |
| 514 | + | |
| 515 | + | |
| 516 | + | |
| 517 | + | |
| 518 | + | |
| 519 | + | |
| 520 | + | |
| 521 | + | |
| 522 | + | |
| 523 | + | |
| 524 | + | |
| 525 | + | |
| 526 | + | |
| 527 | + | |
| 528 | + | |
| 529 | + | |
| 530 | + | |
| 531 | + | |
| 532 | + | |
| 533 | + | |
| 534 | + | |
| 535 | + | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
| 571 | + | |
| 572 | + | |
| 573 | + | |
| 574 | + | |
| 575 | + | |
| 576 | + | |
| 577 | + | |
| 578 | + | |
| 579 | + | |
| 580 | + | |
| 581 | + | |
| 582 | + | |
| 583 | + | |
| 584 | + | |
| 585 | + | |
| 586 | + | |
| 587 | + | |
| 588 | + | |
| 589 | + | |
| 590 | + | |
| 591 | + | |
| 592 | + | |
| 593 | + | |
| 594 | + | |
| 595 | + | |
| 596 | + | |
| 597 | + | |
| 598 | + | |
| 599 | + | |
| 600 | + | |
| 601 | + | |
| 602 | + | |
| 603 | + | |
| 604 | + | |
| 605 | + | |
| 606 | + | |
| 607 | + | |
| 608 | + | |
| 609 | + | |
| 610 | + | |
| 611 | + | |
| 612 | + | |
| 613 | + | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
| 663 | + | |
| 664 | + | |
| 665 | + | |
| 666 | + | |
| 667 | + | |
| 668 | + | |
| 669 | + | |
| 670 | + | |
| 671 | + | |
| 672 | + | |
| 673 | + | |
| 674 | + | |
| 675 | + | |
| 676 | + | |
441 | 677 | | |
442 | 678 | | |
443 | 679 | | |
| |||
0 commit comments