vllm.attention ¶
Modules:
Name | Description |
---|---|
backends | |
layer | Attention layer. |
layers | |
ops | |
selector | |
utils | |
__all__ module-attribute
¶
__all__ = [
"Attention",
"AttentionBackend",
"AttentionMetadata",
"AttentionType",
"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
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 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 |
|
attn_backend instance-attribute
¶
attn_backend = get_attn_backend(
head_size,
dtype,
kv_cache_dtype,
block_size,
use_mla=use_mla,
has_sink=has_sink,
use_sparse=use_sparse,
)
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,
use_sparse: 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
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 |
|
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
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 |
|
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
supports_quant_query_input class-attribute
instance-attribute
¶
supports_quant_query_input: bool = False
full_cls_name classmethod
¶
get_builder_cls abstractmethod
staticmethod
¶
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]
make_metadata classmethod
¶
make_metadata(*args, **kwargs) -> AttentionMetadata
AttentionMetadata ¶
AttentionType ¶
Attention type. Use string to be compatible with torch.compile
.
Source code in vllm/attention/backends/abstract.py
DECODER class-attribute
instance-attribute
¶
Decoder attention between previous layer Q/K/V.
ENCODER class-attribute
instance-attribute
¶
Encoder attention between previous layer Q/K/V for encoder-decoder.
ENCODER_DECODER class-attribute
instance-attribute
¶
Attention between dec. Q and enc. K/V for encoder-decoder.
ENCODER_ONLY class-attribute
instance-attribute
¶
Encoder attention between previous layer Q/K/V.
get_attn_backend ¶
get_attn_backend(
head_size: int,
dtype: dtype,
kv_cache_dtype: Optional[str],
block_size: int,
use_mla: bool = False,
has_sink: bool = False,
use_sparse: bool = False,
) -> type[AttentionBackend]
Selects which attention backend to use and lazily imports it.