Commit d4ef67a
committed
add TurboQuant KV cache compression
WHT rotation + Lloyd-Max codebook quantization for the KV cache, with
asymmetric K/V bit-widths, routed through the kvScheme parameter. Two-phase
design: raw FP16 during prefill (zero overhead), batch-compress on the
first decode step, fused Metal kernels for encode and compressed-domain
attention afterwards.
Stacked on the kvScheme plumbing branch; resolveTurboScheme +
maybeTurboQuantizeKVCache convert eligible KVCacheSimple layers when a
turbo scheme is selected.
Changes from the earlier draft of this branch:
- f32 rotation matrices (bf16 rounding in the rotation compounds across
layers on larger models)
- norm correction applied on the WHT path too, matching the dense path
and the fused encode kernel (quantized unit vectors land ~5-10% short
without it)
- the codec setup crash this branch used to hit is fixed by computing
tokenCount from the leading dims instead of an out-of-range dim index,
so the external array-metadata guard is no longer needed
- raw-key schemes (turbo0v2/turbo0v4) are not exposed via kvScheme yet:
the rawKeyMode decode path produces NaN from the second decode step on
real checkpoints while staying unit-clean at every probed shape, dtype,
and magnitude — tracked for a dedicated debugging pass; the mode stays
reachable through the TurboQuantKVCache initializer
- CI-runnable test suite: rotation orthogonality, codec round trip,
cache offsets/trim, compressed-vs-FP16 attention parity (incl. GQA
head-mapping with head-distinguishable values and the kvScheme
conversion route), and an end-to-end synthetic-model generation test1 parent 4e799a4 commit d4ef67a
6 files changed
Lines changed: 2783 additions & 6 deletions
File tree
- Libraries/MLXLMCommon
- Tests/MLXLMTests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
51 | 51 | | |
52 | 52 | | |
53 | 53 | | |
54 | | - | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
| 68 | + | |
| 69 | + | |
| 70 | + | |
55 | 71 | | |
56 | 72 | | |
57 | 73 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
73 | 73 | | |
74 | 74 | | |
75 | 75 | | |
76 | | - | |
77 | | - | |
| 76 | + | |
| 77 | + | |
78 | 78 | | |
79 | 79 | | |
80 | 80 | | |
| |||
810 | 810 | | |
811 | 811 | | |
812 | 812 | | |
813 | | - | |
| 813 | + | |
| 814 | + | |
814 | 815 | | |
815 | 816 | | |
816 | 817 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1529 | 1529 | | |
1530 | 1530 | | |
1531 | 1531 | | |
| 1532 | + | |
1532 | 1533 | | |
1533 | 1534 | | |
1534 | 1535 | | |
| |||
1682 | 1683 | | |
1683 | 1684 | | |
1684 | 1685 | | |
| 1686 | + | |
| 1687 | + | |
| 1688 | + | |
| 1689 | + | |
| 1690 | + | |
| 1691 | + | |
| 1692 | + | |
| 1693 | + | |
| 1694 | + | |
| 1695 | + | |
| 1696 | + | |
| 1697 | + | |
| 1698 | + | |
| 1699 | + | |
| 1700 | + | |
| 1701 | + | |
1685 | 1702 | | |
1686 | 1703 | | |
1687 | 1704 | | |
| |||
1948 | 1965 | | |
1949 | 1966 | | |
1950 | 1967 | | |
1951 | | - | |
1952 | | - | |
| 1968 | + | |
| 1969 | + | |
| 1970 | + | |
1953 | 1971 | | |
1954 | 1972 | | |
1955 | 1973 | | |
1956 | 1974 | | |
1957 | 1975 | | |
1958 | 1976 | | |
1959 | 1977 | | |
| 1978 | + | |
| 1979 | + | |
| 1980 | + | |
| 1981 | + | |
| 1982 | + | |
| 1983 | + | |
| 1984 | + | |
| 1985 | + | |
| 1986 | + | |
| 1987 | + | |
| 1988 | + | |
| 1989 | + | |
1960 | 1990 | | |
1961 | 1991 | | |
1962 | 1992 | | |
| |||
0 commit comments