vllm.attention.backends.utils
Attention backend utils
STR_NOT_IMPL_ENC_DEC_ROCM_HIP
module-attribute
¶
TAttentionMetadata
module-attribute
¶
TAttentionMetadata = TypeVar(
"TAttentionMetadata", bound="AttentionMetadata"
)
CommonAttentionState
¶
Bases: AttentionState
Source code in vllm/attention/backends/utils.py
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 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 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 |
|
__init__
¶
__init__(runner: ModelRunnerBase)
_add_additional_input_buffers_for_enc_dec_model
¶
Saves additional input buffers specific to the encoder-decoder model from the attention metadata.
This method extracts and stores encoder-decoder related input buffers
from the attn_metadata
into the input_buffers
dictionary. The
buffers include encoder sequence lengths, cross-slot mappings, and
cross-block tables, which are essential for the encoder-decoder model
during CUDA graph replay.
Source code in vllm/attention/backends/utils.py
_prepare_input_buffers_for_enc_dec_model
¶
Populates input buffers with data from the encoder-decoder model's attention metadata.
This method fills the input buffers with encoder-decoder specific
tensors. It copies data from the attn_metadata
and keyword arguments
(kwargs
) into corresponding buffers in the input_buffers
dictionary.
The copied data includes attention-related metadata as well as input
IDs and positional information for the encoder.
Source code in vllm/attention/backends/utils.py
_update_captured_metadata_for_enc_dec_model
¶
_update_captured_metadata_for_enc_dec_model(
batch_size: int, attn_metadata
)
Updates the attention metadata parameters for CUDA graph capture in an encoder-decoder model.
This method modifies attention-related tensors and metadata required for CUDA graph capture in encoder-decoder models. Specifically, it updates the cross-attention and encoder sequence tensors in the AttentionMetadata object.
Source code in vllm/attention/backends/utils.py
begin_forward
¶
get_graph_input_buffers
¶
Source code in vllm/attention/backends/utils.py
graph_capture
¶
graph_capture(max_batch_size: int)
Source code in vllm/attention/backends/utils.py
graph_capture_get_metadata_for_batch
¶
Source code in vllm/attention/backends/utils.py
graph_clone
¶
graph_clone(batch_size: int) -> CommonAttentionState
prepare_graph_input_buffers
¶
prepare_graph_input_buffers(
input_buffers,
attn_metadata,
is_encoder_decoder_model: bool = False,
) -> None
Source code in vllm/attention/backends/utils.py
CommonMetadataBuilder
¶
Bases: AttentionMetadataBuilder[TAttentionMetadata]
Source code in vllm/attention/backends/utils.py
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 |
|
__init__
¶
__init__(input_builder: ModelInputForGPUBuilder)
_add_seq_group
¶
_add_seq_group(
inter_data: InterDataForSeqGroup,
chunked_prefill_enabled: bool,
)
Source code in vllm/attention/backends/utils.py
build
¶
Build attention metadata with on-device tensors.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
seq_lens
|
List[int]
|
The maybe padded sequence lengths of the input sequences. |
required |
query_lens
|
List[int]
|
The query lengths of the input sequences. |
required |
cuda_graph_pad_size
|
int
|
The padding size for cuda graph. -1 if cuda graph is not used. |
required |
batch_size
|
int
|
The maybe padded batch size. |
required |
Source code in vllm/attention/backends/utils.py
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 |
|
prepare
¶
Source code in vllm/attention/backends/utils.py
MLADims
dataclass
¶
Source code in vllm/attention/backends/utils.py
_compute_slot_mapping_numpy
¶
_compute_slot_mapping_numpy(
slot_mapping: List[int],
block_table: List[int],
range_start: int,
range_end: int,
block_size: int,
)
Source code in vllm/attention/backends/utils.py
_compute_slot_mapping_python
¶
_compute_slot_mapping_python(
slot_mapping: List[int],
block_table: List[int],
range_start: int,
range_end: int,
block_size: int,
)
Source code in vllm/attention/backends/utils.py
compute_slot_mapping
¶
compute_slot_mapping(
is_profile_run: bool,
slot_mapping: List[int],
seq_id: int,
seq_len: int,
context_len: int,
start_idx: int,
block_size: int,
block_tables: Dict[int, List[int]],
)
Compute slot mapping.
Source code in vllm/attention/backends/utils.py
compute_slot_mapping_start_idx
¶
compute_slot_mapping_start_idx(
is_prompt: bool,
query_len: int,
context_len: int,
sliding_window: int,
)
Compute the start index of slot mapping.
Source code in vllm/attention/backends/utils.py
get_mla_dims
¶
get_mla_dims(model_config: ModelConfig) -> MLADims
Source code in vllm/attention/backends/utils.py
get_num_prefill_decode_query_kv_tokens
¶
Calculate the number of prefill and decode tokens for query, key/value based on the attention metadata and the specified attention type.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
attn_metadata
|
AttentionMetadata
|
Attention Metadata object. |
required |
attn_type
|
AttentionType
|
The type of attention being used. |
required |
Returns: Tuple[int, int, int]: A tuple containing three integers: - The number of prefill query tokens. - The number of prefill key/value tokens. - The number of decode query tokens.
Raises:
Type | Description |
---|---|
AssertionError
|
If the number of encoder tokens in |
Source code in vllm/attention/backends/utils.py
get_seq_len_block_table_args
¶
The particular choice of sequence-length- and block-table-related attributes which should be extracted from attn_metadata is dependent on the type of attention operation.
Decoder attn -> select entirely decoder self-attention-related fields Encoder/decoder cross-attn -> select encoder sequence lengths & cross-attn block-tables fields Encoder attn -> select encoder sequence lengths fields & no block tables
Arguments:
- attn_metadata: Attention metadata structure associated with attention op
- is_prompt: True if prefill, False otherwise
- attn_type: encoder attention, decoder self-attention, encoder/decoder cross-attention
Returns:
- Appropriate sequence-lengths tensor
- Appropriate max sequence-length scalar
- Appropriate block tables (or None)
Source code in vllm/attention/backends/utils.py
is_all_cross_attn_metadata_set
¶
All attention metadata required for enc/dec cross-attention is set.
Superset of encoder attention required metadata.
Source code in vllm/attention/backends/utils.py
is_all_encoder_attn_metadata_set
¶
All attention metadata required for encoder attention is set.
Source code in vllm/attention/backends/utils.py
is_block_tables_empty
¶
Check if block_tables is None or a dictionary with all None values.