vllm.attention
Modules:
Name | Description |
---|---|
backends | |
layer | Attention layer. |
layers | |
ops | |
selector | |
utils | |
__all__ module-attribute
¶
__all__ = [
"Attention",
"AttentionBackend",
"AttentionMetadata",
"AttentionType",
"AttentionMetadataBuilder",
"AttentionState",
"get_attn_backend",
]
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/attention/layer.py
58 59 60 61 62 63 64 65 66 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 327 328 329 |
|
attn_backend instance-attribute
¶
attn_backend = get_attn_backend(
head_size,
dtype,
kv_cache_dtype,
block_size,
is_attention_free,
use_mla=use_mla,
has_sink=has_sink,
)
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_sharing_target_layer_name instance-attribute
¶
__init__ ¶
__init__(
num_heads: int,
head_size: int,
scale: float,
num_kv_heads: Optional[int] = None,
alibi_slopes: Optional[List[float]] = None,
cache_config: Optional[CacheConfig] = None,
quant_config: Optional[QuantizationConfig] = None,
logits_soft_cap: Optional[float] = None,
per_layer_sliding_window: Optional[int] = None,
use_mla: bool = False,
prefix: str = "",
attn_type: str = DECODER,
kv_sharing_target_layer_name: Optional[str] = None,
attn_backend: Optional[type[AttentionBackend]] = None,
**extra_impl_args,
) -> None
The KV cache is stored inside this class and is accessed via self.kv_cache
.
Source code in vllm/attention/layer.py
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 |
|
calc_kv_scales ¶
Source code in vllm/attention/layer.py
extra_repr ¶
extra_repr() -> str
Source code in vllm/attention/layer.py
forward ¶
forward(
query: Tensor,
key: Tensor,
value: Tensor,
output_shape: Optional[Size] = None,
) -> Tensor
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/attention/layer.py
get_attn_backend ¶
get_attn_backend() -> type[AttentionBackend]
process_weights_after_loading ¶
process_weights_after_loading(act_dtype: dtype)
Source code in vllm/attention/layer.py
AttentionBackend ¶
Bases: ABC
Abstract class for attention backends.
Source code in vllm/attention/backends/abstract.py
copy_blocks abstractmethod
staticmethod
¶
full_cls_name classmethod
¶
get_builder_cls abstractmethod
staticmethod
¶
get_builder_cls() -> Type[AttentionMetadataBuilder]
get_impl_cls abstractmethod
staticmethod
¶
get_impl_cls() -> Type[AttentionImpl]
get_kv_cache_shape abstractmethod
staticmethod
¶
get_kv_cache_stride_order staticmethod
¶
get_metadata_cls abstractmethod
staticmethod
¶
get_metadata_cls() -> Type[AttentionMetadata]
get_state_cls abstractmethod
staticmethod
¶
get_state_cls() -> Type[AttentionState]
make_metadata classmethod
¶
make_metadata(*args, **kwargs) -> AttentionMetadata
swap_blocks abstractmethod
staticmethod
¶
AttentionMetadata dataclass
¶
Attention metadata for prefill and decode batched together.
Source code in vllm/attention/backends/abstract.py
decode_metadata abstractmethod
property
¶
decode_metadata: Optional[AttentionMetadata]
Return the attention metadata that's required to run decode attention.
multi_modal_placeholder_index_maps instance-attribute
¶
prefill_metadata abstractmethod
property
¶
prefill_metadata: Optional[AttentionMetadata]
Return the attention metadata that's required to run prefill attention.
__init__ ¶
__init__(
num_prefills: int,
num_prefill_tokens: int,
num_decode_tokens: int,
slot_mapping: Tensor,
multi_modal_placeholder_index_maps: Optional[
Dict[str, IndexMap]
],
enable_kv_scales_calculation: bool,
) -> None
asdict_zerocopy ¶
Similar to dataclasses.asdict, but avoids deepcopying.
Source code in vllm/attention/backends/abstract.py
AttentionMetadataBuilder ¶
Abstract class for attention metadata builders.
Source code in vllm/attention/backends/abstract.py
__init__ abstractmethod
¶
__init__(
input_builder: ModelRunnerInputBuilderBase,
) -> None
Create the builder, remember some configuration and parameters.
AttentionState ¶
Holds attention backend-specific objects reused during the lifetime of the model runner.
Source code in vllm/attention/backends/abstract.py
__init__ abstractmethod
¶
__init__(runner: ModelRunnerBase)
begin_forward abstractmethod
¶
begin_forward(model_input: ModelRunnerInputBase) -> None
get_graph_input_buffers abstractmethod
¶
get_graph_input_buffers(
attn_metadata: T,
is_encoder_decoder_model: bool = False,
) -> Dict[str, Any]
Get attention-specific input buffers for CUDA graph capture.
graph_capture_get_metadata_for_batch abstractmethod
¶
graph_capture_get_metadata_for_batch(
batch_size: int, is_encoder_decoder_model: bool = False
) -> T
Get attention metadata for CUDA graph capture of batch_size.
graph_clone abstractmethod
¶
graph_clone(batch_size: int) -> AttentionState[T]
AttentionType ¶
Attention type. Use string to be compatible with torch.compile
.
Source code in vllm/attention/backends/abstract.py
get_attn_backend ¶
get_attn_backend(
head_size: int,
dtype: dtype,
kv_cache_dtype: Optional[str],
block_size: int,
is_attention_free: bool = False,
use_mla: bool = False,
has_sink: bool = False,
) -> type[AttentionBackend]
Selects which attention backend to use and lazily imports it.