66import triton
77import triton .language as tl
88
9+ from .kvcache import dequant_group , h32
10+
911CHUNK = 512
1012TILE = 64
1113
@@ -25,9 +27,9 @@ def _tile(q, k, v, m, l, o, valid, scale: tl.constexpr):
2527
2628
2729@triton .jit
28- def _chunks (Q , KC , VC , POS0 , PO , PM , PL , IDS , NKR , SPR ,
30+ def _chunks (Q , KC , VC , KSC , VSC , POS0 , PO , PM , PL , IDS , NKR , SPR ,
2931 H : tl .constexpr , HK : tl .constexpr , D : tl .constexpr , G : tl .constexpr , CH : tl .constexpr ,
30- NCH : tl .constexpr , SCALE : tl .constexpr , IDW : tl .constexpr , QSA : tl .constexpr ):
32+ NCH : tl .constexpr , SCALE : tl .constexpr , IDW : tl .constexpr , QSA : tl .constexpr , KVQ : tl . constexpr ):
3133 r = tl .program_id (0 )
3234 hk = tl .program_id (1 )
3335 c = tl .program_id (2 )
@@ -51,8 +53,21 @@ def _chunks(Q, KC, VC, POS0, PO, PM, PL, IDS, NKR, SPR,
5153 if QSA :
5254 if sparse :
5355 ki = tl .load (IDS + r * IDW + ki , mask = valid , other = 0 )
54- kk = tl .load (KC + (ki [:, None ].to (tl .int64 ) * HK + hk ) * D + d [None , :], mask = valid [:, None ], other = 0.0 )
55- vv = tl .load (VC + (ki [:, None ].to (tl .int64 ) * HK + hk ) * D + d [None , :], mask = valid [:, None ], other = 0.0 )
56+ off = (ki [:, None ].to (tl .int64 ) * HK + hk ) * D + d [None , :]
57+ if KVQ :
58+ # int8 codes + one fp16 scale per 32 values: dequantize the tile before the dot (the keys are
59+ # stored rotated; the query is rotated by the write path, so no rotation is undone here)
60+ gs = tl .arange (0 , D // 32 )
61+ sgc = (ki [:, None ].to (tl .int64 ) * HK + hk ) * (D // 32 ) + gs [None , :]
62+ kc = tl .load (KC + off , mask = valid [:, None ], other = 0 )
63+ sc = tl .load (KSC + sgc , mask = valid [:, None ], other = 0.0 )
64+ kk = dequant_group (tl .reshape (kc , (64 , D )), sc , M = 64 , W = D )
65+ vc = tl .load (VC + off , mask = valid [:, None ], other = 0 )
66+ sv = tl .load (VSC + sgc , mask = valid [:, None ], other = 0.0 )
67+ vv = dequant_group (tl .reshape (vc , (64 , D )), sv , M = 64 , W = D )
68+ else :
69+ kk = tl .load (KC + off , mask = valid [:, None ], other = 0.0 )
70+ vv = tl .load (VC + off , mask = valid [:, None ], other = 0.0 )
5671 m , l , o = _tile (q , kk , vv , m , l , o , valid , SCALE )
5772 base = (r * NCH + c ) * H + hk * G + gg
5873 tl .store (PO + base [:, None ] * D + d [None , :], o , mask = gg [:, None ] < G )
@@ -62,7 +77,7 @@ def _chunks(Q, KC, VC, POS0, PO, PM, PL, IDS, NKR, SPR,
6277
6378@triton .jit
6479def _merge (PO , PM , PL , POS0 , OUT , NKR , SPR , H : tl .constexpr , HK : tl .constexpr , D : tl .constexpr , G : tl .constexpr ,
65- CH : tl .constexpr , NCH : tl .constexpr , QSA : tl .constexpr ):
80+ CH : tl .constexpr , NCH : tl .constexpr , QSA : tl .constexpr , KVQ : tl . constexpr ):
6681 r = tl .program_id (0 )
6782 hk = tl .program_id (1 )
6883 n = tl .load (POS0 ) + r + 1
@@ -87,6 +102,10 @@ def _merge(PO, PM, PL, POS0, OUT, NKR, SPR, H: tl.constexpr, HK: tl.constexpr, D
87102 l = l * a + cl * b
88103 m = next_m
89104 result = o / l [:, None ]
105+ if KVQ :
106+ # the values are stored rotated: o = p . (H v) = H (p . v), so one rotation a row puts the merged
107+ # output back, whatever windows and chunks it came from
108+ result = tl .reshape (h32 (tl .reshape (result , (16 * D // 32 , 32 )), M = 16 * D // 32 ), (16 , D ))
90109 tl .store (OUT + (r * H + head [:, None ]) * D + d [None , :], result .to (tl .bfloat16 ), mask = gg [:, None ] < G )
91110
92111
@@ -112,9 +131,18 @@ def __init__(self, rows: int, heads: int, head_dim: int, capacity: int, device,
112131
113132def attention (q : torch .Tensor , kc : torch .Tensor , vc : torch .Tensor , pos0 : torch .Tensor , scratch : AttnScratch ,
114133 rows : int , scale : float , out : torch .Tensor | None = None , * ,
115- context : int | None = None ) -> torch .Tensor :
116- """q [R, H, D] against caches holding [0, P0 + R) -> [R, H, D] bf16; sparse rows read ``scratch.ids``."""
117-
134+ context : int | None = None , ks : torch .Tensor | None = None , vs : torch .Tensor | None = None ,
135+ kvq : bool = False ) -> torch .Tensor :
136+ """q [R, H, D] against caches holding [0, P0 + R) -> [R, H, D] bf16; sparse rows read ``scratch.ids``.
137+
138+ With ``kvq`` the caches hold int8 codes and one fp16 scale per 32 values (``ks``/``vs``), and both ``q``
139+ and the returned output are in the caches' H32 rotation: ``q`` was rotated by ``glue.attn_prep``, and the
140+ output is rotated back in ``_merge``, so the caller sees the same arithmetic either way."""
141+
142+ if kvq and (ks is None or vs is None ):
143+ raise ValueError ("an int8 KV cache needs its scale tensors" )
144+ if ks is None :
145+ ks = vs = kc
118146 _ , h , d = q .shape
119147 hk = kc .shape [1 ]
120148 g = h // hk
@@ -124,11 +152,11 @@ def attention(q: torch.Tensor, kc: torch.Tensor, vc: torch.Tensor, pos0: torch.T
124152 keys = min (keys , (scratch .budget // scratch .ratio + 1 ) * scratch .ratio - 1 )
125153 chunks = min (nch , triton .cdiv (keys , CHUNK ))
126154 out = scratch .out if out is None else out
127- _chunks [(rows , hk , chunks )](q , kc , vc , pos0 , scratch .po , scratch .pm , scratch .pl , scratch .ids , scratch .nk ,
155+ _chunks [(rows , hk , chunks )](q , kc , vc , ks , vs , pos0 , scratch .po , scratch .pm , scratch .pl , scratch .ids , scratch .nk ,
128156 scratch .sparse , H = h , HK = hk , D = d , G = g , CH = CHUNK , NCH = nch , SCALE = scale ,
129- IDW = scratch .idw , QSA = scratch .qsa , num_warps = 4 , num_stages = 1 )
157+ IDW = scratch .idw , QSA = scratch .qsa , KVQ = kvq , num_warps = 4 , num_stages = 1 )
130158 _merge [(rows , hk )](scratch .po , scratch .pm , scratch .pl , pos0 , out , scratch .nk , scratch .sparse , H = h ,
131- HK = hk , D = d , G = g , CH = CHUNK , NCH = nch , QSA = scratch .qsa , num_warps = 4 )
159+ HK = hk , D = d , G = g , CH = CHUNK , NCH = nch , QSA = scratch .qsa , KVQ = kvq , num_warps = 4 )
132160 return out
133161
134162
0 commit comments