vllm.model_executor.layers.mamba.mamba_utils
¶
Classes:
-
MambaCopySpec–Data class specifying the memory-copy parameters for Mamba states used for
-
MambaStateDtypeCalculator– -
MambaStateShapeCalculator–
Functions:
-
get_conv_copy_spec–Return a MambaCopySpec for copying a convolutional state slice.
-
get_conv_state_layout–Return the SSM conv state layout.
-
get_temporal_copy_spec–Return a MambaCopySpec for copying a temporal state slice.
-
is_conv_state_dim_first–True when the conv state is stored as (dim, state_len) per block.
Attributes:
-
MambaStateCopyFunc(TypeAlias) –Type alias for a function that computes a MambaCopySpec for copying state slices.
MambaStateCopyFunc = Callable[[torch.Tensor, list[int], int, int], MambaCopySpec]
module-attribute
¶
Type alias for a function that computes a MambaCopySpec for copying state slices.
Parameters:
state: torch.Tensor - the Mamba state tensor (e.g., conv or temporal states).
block_ids: list[int] - the list of block indices for the state to copy.
cur_block_idx: int - current block index within block_ids to copy from.
num_accepted_tokens: int - number of accepted tokens used to compute the copy offset.
Range: 1 .. 1 + num_speculative_tokens (inclusive).
MambaCopySpec
dataclass
¶
Data class specifying the memory-copy parameters for Mamba states used for prefix caching in align mode.
Attributes:
-
start_addr(int) –Starting address for the memory copy operation.
-
num_elements(int) –Number of elements to copy from the starting address.
Source code in vllm/model_executor/layers/mamba/mamba_utils.py
MambaStateDtypeCalculator
¶
Methods:
-
append_replayssm_ring–Append the ReplaySSM ring dtypes to a base
(conv, ssm)tuple:
Source code in vllm/model_executor/layers/mamba/mamba_utils.py
54 55 56 57 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 | |
append_replayssm_ring(base_dtypes, model_dtype)
classmethod
¶
Append the ReplaySSM ring dtypes to a base (conv, ssm) tuple:
(x_cache, dt_cache, B_cache) = (activation, fp32, activation).
Source code in vllm/model_executor/layers/mamba/mamba_utils.py
MambaStateShapeCalculator
¶
Methods:
-
append_replayssm_ring–Append the physical ReplaySSM ring shapes.
-
extra_groups_for_head_shards–Compute the increase in group numbers to account for
-
gated_delta_net_state_splits–How TP splits
gated_delta_net_state_shape's states: conv -
kda_state_splits–How TP splits
kda_state_shape's states: conv [Q, K, V], and the -
linear_attention_state_splits–How TP splits
linear_attention_state_shape's state: by head. -
mamba1_state_splits–How TP splits
mamba1_state_shape's states: conv [x], and the -
mamba2_state_splits–How TP splits
mamba2_state_shape's states: conv [x, B, C], and -
short_conv_state_splits–How TP splits
short_conv_state_shape's conv state: by channel.
Source code in vllm/model_executor/layers/mamba/mamba_utils.py
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 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 | |
_orient_conv_shape(dim, state_len)
staticmethod
¶
Return (dim, state_len) for DS layout, (state_len, dim) for SD.
Source code in vllm/model_executor/layers/mamba/mamba_utils.py
append_replayssm_ring(base_shapes, n_groups, tp_world_size, logical_window, backend, num_speculative_tokens=0)
classmethod
¶
Append the physical ReplaySSM ring shapes.
base_shapes[1] is (nheads // tp, head_dim, state_size);
B_cache extends n_groups for head shards as in mamba2_state_shape.
Source code in vllm/model_executor/layers/mamba/mamba_utils.py
extra_groups_for_head_shards(ngroups, tp_size)
classmethod
¶
Compute the increase in group numbers to account for replication in order to accompany the head shards.
Source code in vllm/model_executor/layers/mamba/mamba_utils.py
gated_delta_net_state_splits(tp_world_size, num_k_heads, num_v_heads, head_k_dim, head_v_dim)
classmethod
¶
How TP splits gated_delta_net_state_shape's states: conv
[Q, K, V], and the temporal state by value head.
Source code in vllm/model_executor/layers/mamba/mamba_utils.py
kda_state_splits(tp_world_size, num_heads, head_dim, num_k_heads=None, head_k_dim=None)
classmethod
¶
How TP splits kda_state_shape's states: conv [Q, K, V], and the
recurrent state by head. The same layout as gated delta net's.
Source code in vllm/model_executor/layers/mamba/mamba_utils.py
linear_attention_state_splits(tp_size, num_heads)
classmethod
¶
How TP splits linear_attention_state_shape's state: by head.
Source code in vllm/model_executor/layers/mamba/mamba_utils.py
mamba1_state_splits(tp_world_size, intermediate_size)
classmethod
¶
How TP splits mamba1_state_shape's states: conv [x], and the
temporal state by channel.
Source code in vllm/model_executor/layers/mamba/mamba_utils.py
mamba2_state_splits(tp_world_size, intermediate_size, n_groups, num_heads, state_size)
classmethod
¶
How TP splits mamba2_state_shape's states: conv [x, B, C], and
the temporal state by head. A single group that TP does not divide is
held whole by every rank (see extra_groups_for_head_shards).
Source code in vllm/model_executor/layers/mamba/mamba_utils.py
short_conv_state_splits(tp_world_size, intermediate_size)
classmethod
¶
How TP splits short_conv_state_shape's conv state: by channel.
Source code in vllm/model_executor/layers/mamba/mamba_utils.py
get_conv_copy_spec(state, block_ids, cur_block_idx, num_accepted_tokens)
¶
Return a MambaCopySpec for copying a convolutional state slice.
Works for both SD layout (num_blocks, state_len, dim) and
DS layout (num_blocks, dim, state_len).
Source code in vllm/model_executor/layers/mamba/mamba_utils.py
get_conv_state_layout()
cached
¶
Return the SSM conv state layout.
SD = (state_len, dim) — dim is the innermost contiguous dimension. DS = (dim, state_len) — TP-sharded dim is on dim-1 (like HND for KV cache), consistent with SSM temporal state layout.
Source code in vllm/model_executor/layers/mamba/mamba_utils.py
get_temporal_copy_spec(state, block_ids, cur_block_idx, num_accepted_tokens)
¶
Return a MambaCopySpec for copying a temporal state slice.