vllm.v1.attention.ops.turboquant_soa.triton_turboquant_unified_attention ¶
Unified (prefill + decode) Triton attention kernel for TurboQuant.
Structure is ported from vllm/v1/attention/ops/triton_unified_attention.py (the AITER unified attention kernel already upstreamed into vLLM). Only the K and V load sites are replaced: instead of reading raw fp16 keys/values from two contiguous caches, this kernel reads TurboQuant-packed bytes from a single combined cache and dequantizes on the fly inside the tile loop.
Benefits over the v1/v2 decode-only kernels:
- GQA heads are stacked into
BLOCK_Mand the Q·K and P·V ops are propertl.dottensor-core operations (MFMA on MI300X). - The same kernel handles decode (
BLOCK_Q=1) and prefill (BLOCK_Q>1) -- no more Python per-request for-loop for continuation chunks. - Only the subset of features exercised by the current TQ paths is kept (causal, GQA). Sinks / softcap / ALiBi / sliding-window / qq-bias / mm-prefix are deferred to follow-ups.
This is an opt-in v3 path behind VLLM_TQ_DECODE_V3.
Functions:
-
triton_turboquant_decode_attention_soa–Decode-only convenience wrapper around
triton_turboquant_unified_attention. -
triton_turboquant_unified_attention–Launch unified TQ attention (v3).
_get_pair_lut(centroids) ¶
Return a fresh pair-LUT for centroids on each call.
Previously cached by (data_ptr, device) — that key is unsafe because the CUDA allocator can reuse freed addresses for different centroid tensors, yielding a stale (wrong-values) LUT. The LUT is tiny (NN2 fp32, ~2KB at N=16) so unconditional rebuild is essentially free compared to attention work. If this shows up on a profile, replace with a hash-of-values fingerprint, not data_ptr.
Source code in vllm/v1/attention/ops/turboquant_soa/triton_turboquant_unified_attention.py
_tq_fuse_q_rotation(Q, PiT_ptr, PiT_stride_0, PiT_stride_1, dim_mask, HEAD_SIZE_PADDED) ¶
Fused Q @ PiT prologue. Called once per program for the MSE-key path when the launcher has passed the raw (un-rotated) query. Returns the rotated Q in the original dtype.
Source code in vllm/v1/attention/ops/turboquant_soa/triton_turboquant_unified_attention.py
_tq_load_k_tile(KV_cache_ptr, KV_cache_u16_ptr, data_bases, knorm_u16_addrs, d_offs, d_mask, tile_mask, Centroids_ptr, Pair_lut_ptr, OUT_DTYPE, HEAD_DIM, BLOCK_D, MSE_BITS, N_CENTROIDS, KEY_FP8, USE_PAIR_LUT, NORM_CORRECTION, FP8_E4B15, TILE_SIZE) ¶
Load + dequantize a TILE_SIZE × HEAD_SIZE block of keys and return the transposed tile K_T : [HEAD_SIZE_PADDED, TILE_SIZE].
Opt#3 SoA layout: packed K data is at data_bases[t] + [0, MSE_BYTES) for MSE keys (or [0, D) for FP8). The per-token K-norm lives in the per-block SoA metadata region; knorm_u16_addrs already encodes its u16 element index. For decode tiles aligned with blocks, these addresses are contiguous → one coalesced wide load replaces TILE_SIZE scattered loads (the whole point of Opt#3).
Source code in vllm/v1/attention/ops/turboquant_soa/triton_turboquant_unified_attention.py
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 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 | |
_tq_load_v_tile(KV_cache_ptr, KV_cache_u16_ptr, val_bases, vscale_u16_addrs, vzero_u16_addrs, d_offs, d_mask, tile_mask, OUT_DTYPE, HEAD_DIM, VQB) ¶
Load + dequantize a TILE_SIZE × HEAD_SIZE block of values.
Opt#3 SoA layout: packed V data is at val_bases[t] + [0, VAL_DATA_BYTES) (V-data immediately follows K-data within the slot's data region, and val_bases is precomputed by the caller as data_base + KEY_DATA_BYTES). V-scale / V-zero live in the per-block SoA metadata region at indices vscale_u16_addrs / vzero_u16_addrs. For tiles aligned to block boundaries, those addresses are contiguous → one coalesced wide load per field instead of TILE_SIZE scattered 2-byte loads.
Source code in vllm/v1/attention/ops/turboquant_soa/triton_turboquant_unified_attention.py
triton_turboquant_decode_attention_soa(query, kv_cache, block_table, seq_lens, Pi, centroids, scale, mse_bits, key_packed_size, value_quant_bits, value_packed_size, key_fp8=False, norm_correction=False, PiT=None, max_seq_len=0, mid_o_buf=None, output_buf=None, lse_buf=None, buf_holder=None, max_num_kv_splits=32, sinks=None) ¶
Decode-only convenience wrapper around triton_turboquant_unified_attention.
Treats a rank-3 [B, Hq, D] query as one token per request (query_len = 1) and synthesizes a query_start_loc of [0, 1, 2, ..., B]. max_num_kv_splits is forwarded to the unified launcher as the 3D split-KV segment count (capped per-call against max_seq_len). sinks (optional [Hq] fp32) are forwarded to the kernel which folds them into the softmax denominator via the init-time trick.
Source code in vllm/v1/attention/ops/turboquant_soa/triton_turboquant_unified_attention.py
triton_turboquant_unified_attention(query, kv_cache, block_table, seq_lens, query_start_loc, Pi, centroids, scale, mse_bits, key_packed_size, value_quant_bits, value_packed_size, key_fp8=False, norm_correction=False, PiT=None, output=None, tile_size=None, max_query_len=None, max_seq_len=None, num_kv_splits=None, force_2d=False, fuse_q_rot=True, sinks=None) ¶
Launch unified TQ attention (v3).
query carries raw query vectors. For the MSE-key path the query has to be rotated by PiT before it can be multiplied against the (already-rotated) stored K. By default (fuse_q_rot=True) that rotation is done inside the attention kernel prologue as a single small MFMA — no extra dispatch, no HBM round-trip. Setting fuse_q_rot=False restores the original launcher path (fp32 rocBLAS GEMM + casts + .contiguous()), which is kept as an A/B toggle for bench harnesses; numerical results match the fused path to within a few ulp of fp32 round-off. The FP8-key path never rotates Q regardless of this flag.
tile_size defaults to 32 for prefill (max_query_len > 1) and 16 for pure decode (max_query_len == 1). Callers may override.
Dispatch rule
- Prefill / chunked-prefill (any query block with
BLOCK_Q > 1) andnum_tokens > num_seqs: 2D kernel (plenty of CTAs already). - Pure decode with long KV: 3D split-KV kernel +
reduce_segmentsfor extra parallelism; this is the same pattern v2 uses. force_2d=True: force the 2D path regardless (used by the apples-to-apples bench to isolate pure TQ dequant overhead).
num_kv_splits controls the 3D-split segment count (default 16).
Returns output of shape [num_tokens, Hq, D] in query.dtype.
Source code in vllm/v1/attention/ops/turboquant_soa/triton_turboquant_unified_attention.py
894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282 | |