vllm.model_executor.layers.attention.attention ¶
Attention ¶
Bases: Module, AttentionLayerBase
Attention layer.
This class takes query, key, and value tensors as input. The input tensors can either contain prompt tokens or generation tokens. The class does the following:
- Store the input key and value tensors in the KV cache.
- Perform (multi-head/multi-query/grouped-query) attention.
- Return the output tensor.
Source code in vllm/model_executor/layers/attention/attention.py
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 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 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 | |
attn_backend instance-attribute ¶
attn_backend = get_attn_backend(
head_size,
dtype,
kv_cache_dtype,
block_size,
use_mla=False,
has_sink=has_sink,
use_mm_prefix=use_mm_prefix,
attn_type=attn_type,
)
head_size_v instance-attribute ¶
impl instance-attribute ¶
impl = impl_cls(
num_heads,
head_size,
scale,
num_kv_heads,
alibi_slopes,
sliding_window,
kv_cache_dtype,
logits_soft_cap,
attn_type,
kv_sharing_target_layer_name,
**extra_impl_args,
)
kv_cache_torch_dtype instance-attribute ¶
kv_cache_torch_dtype = kv_cache_dtype_str_to_dtype(
kv_cache_dtype, model_config
)
kv_sharing_target_layer_name instance-attribute ¶
__init__ ¶
__init__(
num_heads: int,
head_size: int,
scale: float,
num_kv_heads: int | None = None,
alibi_slopes: list[float] | None = None,
use_alibi_sqrt: bool | None = None,
cache_config: CacheConfig | None = None,
quant_config: QuantizationConfig | None = None,
logits_soft_cap: float | None = None,
per_layer_sliding_window: int | None = None,
prefix: str = "",
attn_type: str = DECODER,
kv_sharing_target_layer_name: str | None = None,
attn_backend: type[AttentionBackend] | None = None,
head_size_v: int | None = None,
**extra_impl_args,
) -> None
The KV cache is stored inside this class and is accessed via self.kv_cache.
Source code in vllm/model_executor/layers/attention/attention.py
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 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 | |
calc_kv_scales ¶
Source code in vllm/model_executor/layers/attention/attention.py
extra_repr ¶
extra_repr() -> str
Source code in vllm/model_executor/layers/attention/attention.py
forward ¶
The KV cache is stored inside this class and is accessed via self.kv_cache.
Attention metadata (attn_metadata) is set using a context manager in the model runner's execute_model method. It is accessed via forward context using vllm.forward_context.get_forward_context().attn_metadata.
Source code in vllm/model_executor/layers/attention/attention.py
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 | |
get_attn_backend ¶
get_attn_backend() -> type[AttentionBackend]
get_kv_cache_spec ¶
get_kv_cache_spec(vllm_config: VllmConfig) -> KVCacheSpec
Source code in vllm/model_executor/layers/attention/attention.py
process_weights_after_loading ¶
process_weights_after_loading(act_dtype: dtype)
Source code in vllm/model_executor/layers/attention/attention.py
_init_kv_cache_quant ¶
_init_kv_cache_quant(
layer: Module,
quant_config: QuantizationConfig | None,
prefix: str,
) -> None
Initializes KV cache scaling factors and quantization method.
This helper function sets up the KV cache quantization attributes that are shared between Attention and MLAAttention layers. It initializes scale tensors for query, key, value, and probability, and configures the quantization method if applicable.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer | Module | The attention layer instance to initialize. | required |
quant_config | QuantizationConfig | None | Optional quantization configuration. | required |
prefix | str | Layer name prefix for quantization method lookup. | required |
Source code in vllm/model_executor/layers/attention/attention.py
get_attention_context ¶
Extract attention context for a given layer.
This helper function extracts the attention metadata, attention layer instance, and KV cache tensor for a specific layer.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
layer_name | str | The name/identifier of the attention layer. | required |
Returns:
| Name | Type | Description |
|---|---|---|
Any | A tuple containing: | |
Attention | MLAAttention |
| |
Tensor |
| |
tuple[Any, Attention | MLAAttention, Tensor] |
| |
Note | tuple[Any, Attention | MLAAttention, Tensor] | attn_metadata may be None, but attn_layer and kv_cache are always |
tuple[Any, Attention | MLAAttention, Tensor] | extracted from the forward context. |
Source code in vllm/model_executor/layers/attention/attention.py
maybe_calc_kv_scales ¶
Source code in vllm/model_executor/layers/attention/attention.py
maybe_calc_kv_scales_fake ¶
set_default_quant_scales ¶
Sets default quantization scales for the layer.
Source code in vllm/model_executor/layers/attention/attention.py
should_load_quant_weights ¶
should_load_quant_weights(
quant_method: QuantizeMethodBase | None,
) -> bool
Returns whether the quantization method should load quantized weights.
Source code in vllm/model_executor/layers/attention/attention.py
unified_attention ¶
Source code in vllm/model_executor/layers/attention/attention.py
unified_attention_fake ¶
unified_attention_with_output ¶
unified_attention_with_output(
query: Tensor,
key: Tensor,
value: Tensor,
output: Tensor,
layer_name: str,
output_scale: Tensor | None = None,
output_block_scale: Tensor | None = None,
kv_cache_dummy_dep: Tensor | None = None,
) -> None
Source code in vllm/model_executor/layers/attention/attention.py
unified_attention_with_output_fake ¶
unified_attention_with_output_fake(
query: Tensor,
key: Tensor,
value: Tensor,
output: Tensor,
layer_name: str,
output_scale: Tensor | None = None,
output_block_scale: Tensor | None = None,
kv_cache_dummy_dep: Tensor | None = None,
) -> None
Source code in vllm/model_executor/layers/attention/attention.py
unified_kv_cache_update ¶
Returns a dummy that is passed to unified_attention to signal a side effect and the data dependency between them to ensure torch.compile preserves ordering.