vllm.model_executor.layers.attention.attn_capture ¶
Post-hoc Attention Capture for vLLM
Captures and analyzes attention patterns after request completion with minimal overhead. Attention scores (Q*K) are computed on the GPU at request-free time with query buffers and delivered via shared memory.
Module-level functions handle stateless operations (encoding, slot math, attention computation). The AttentionCapture class manages only per-worker mutable state (Q buffer, capture slots).
AttentionCapture ¶
Per-worker attention capture state.
Instantiated once per ModelRunner. Buffers Q vectors during inference and computes attention scores at request completion time.
Source code in vllm/model_executor/layers/attention/attn_capture.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 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 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 | |
_collect_query ¶
Collect Q tensors from buffer in deterministic slot order. Falls back to q_cache for prefix-cached tokens whose Q was not re-computed this request (prefix caching ON). Returns (q_list, q_slot_ids, tok_idx).
Source code in vllm/model_executor/layers/attention/attn_capture.py
_filter_compatible_qk staticmethod ¶
_filter_compatible_qk(
tok_idx: list[int],
q_list: list[Tensor],
k_list: list[Tensor],
) -> tuple[list[int], list[Tensor], list[Tensor]]
Filter Q/K pairs to those with matching shape and device.
Source code in vllm/model_executor/layers/attention/attn_capture.py
buffer_query ¶
Buffer Q tokens at attention-computation time. K is NOT buffered — it is read from KV cache at capture time (request completion).
Source code in vllm/model_executor/layers/attention/attn_capture.py
capture ¶
Capture attention scores for a completed request.
Called after request completion. Computes Q*K attention on GPU and writes results to shared memory.
Source code in vllm/model_executor/layers/attention/attn_capture.py
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 | |
cleanup_request_buffers ¶
Remove buffered Q vectors for a finished request. Called for ALL requests to prevent stale data leaking into future requests. q_cache is evicted only when the underlying KV block is reclaimed.
Source code in vllm/model_executor/layers/attention/attn_capture.py
extract_layer_idx ¶
Parse layer index from attention layer name, with caching.
Source code in vllm/model_executor/layers/attention/attn_capture.py
CaptureConfig dataclass ¶
_shm_name ¶
_shm_read ¶
Read snapshot list from shared-memory, polling until available.
Source code in vllm/model_executor/layers/attention/attn_capture.py
_shm_write ¶
Write snapshot list to a named shared-memory segment. Protocol: size header is written LAST so readers treat size==0 as "write in progress" and keep polling.
Source code in vllm/model_executor/layers/attention/attn_capture.py
build_token_meta ¶
build_token_meta(
req_state,
token_idx: list[int],
*,
ordered_slots_len: int | None = None,
) -> dict[str, Any]
Build token mapping metadata for post-hoc client-side alignment.
Source code in vllm/model_executor/layers/attention/attn_capture.py
compute_qk_attention ¶
Compute scaled dot-product attention probabilities. Handles GQA by expanding K heads to match Q heads. Args: q_tensor: [T, num_q_heads, head_dim] k_tensor: [T, num_kv_heads, head_dim] scale: 1/sqrt(head_dim) Returns: [T, num_q_heads, T] attention probabilities, or None on mismatch.
Source code in vllm/model_executor/layers/attention/attn_capture.py
encode_snapshot ¶
Encode attention tensor to gzip+base64 wire format.
Source code in vllm/model_executor/layers/attention/attn_capture.py
extract_k_from_kv_cache ¶
Extract K(key) vectors from paged KV cache at given slot positions.
auto-detects layout from tensor shape.
- FlashInfer: [num_blocks, 2, page_size, num_kv_heads, head_dim]
- FlashAttention:[2, num_blocks, page_size, num_kv_heads, head_dim]
Returns: Tensor of shape [len(slot_ids), num_kv_heads, head_dim]
Source code in vllm/model_executor/layers/attention/attn_capture.py
get_attn_capture ¶
get_attn_capture() -> Optional[AttentionCapture]
load_attn_snapshot ¶
Load attention snapshot(s) via shared memory (cross-process).
ordered_slots_for_group ¶
Build deduplicated, token-order slot list from a block group.
Source code in vllm/model_executor/layers/attention/attn_capture.py
resolve_target_layers ¶
Determine target layers: per-request override or server default.
Source code in vllm/model_executor/layers/attention/attn_capture.py
set_attn_capture ¶
set_attn_capture(
instance: Optional[AttentionCapture],
) -> None
slots_from_blocks ¶
Return the set of all slot IDs covered by the given blocks.