vllm_mlx.utils.mamba_cache¶
BatchMambaCache implementation for continuous batching with Mamba models.
View the complete module source at #L1-L215.
API details¶
Each callable below includes its exact signature, type annotations, inputs, defaults, return contract, documented exceptions, implementation source, and parsed docstring sections when the source provides them.
vllm_mlx.utils.mamba_cache
¶
BatchMambaCache implementation for continuous batching with Mamba models.
mlx-lm's BatchGenerator requires cache objects to have an extract method,
but MambaCache (which extends ArraysCache) doesn't have one. This module
provides a BatchMambaCache wrapper that adds batching support.
vllm_mlx.utils.mamba_cache.BatchMambaCache
¶
Bases: ArraysCache
Batch-aware MambaCache for continuous batching.
This extends MambaCache to support batch operations required by
mlx-lm's BatchGenerator, specifically the extract method.
Initialize BatchMambaCache.
Parameters:
-
left_padding(Optional[List[int]], default:None) –Amount of left padding for each sequence in batch
-
size(int, default:2) –Number of state arrays (default 2 for Mamba models)
Source code in vllm_mlx/utils/mamba_cache.py
vllm_mlx.utils.mamba_cache.BatchMambaCache._batch_size
instance-attribute
¶
vllm_mlx.utils.mamba_cache.BatchMambaCache.extract
¶
Extract a single cache from the batch.
Parameters:
-
idx(int) –Index of the sequence to extract
Returns:
-
ArraysCache–A new MambaCache with the extracted state
Source code in vllm_mlx/utils/mamba_cache.py
vllm_mlx.utils.mamba_cache.BatchMambaCache.merge
classmethod
¶
merge(caches: List[ArraysCache]) -> BatchMambaCache
Merge multiple MambaCache objects into a BatchMambaCache.
Parameters:
-
caches(List[ArraysCache]) –List of MambaCache objects to merge
Returns:
-
BatchMambaCache–A new BatchMambaCache containing all caches
Source code in vllm_mlx/utils/mamba_cache.py
vllm_mlx.utils.mamba_cache.patch_mlx_lm_for_mamba
¶
Patch mlx-lm to support MambaCache in BatchGenerator.
This modifies the _make_cache function to handle MambaCache by converting it to BatchMambaCache.
Source code in vllm_mlx/utils/mamba_cache.py
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 | |
vllm_mlx.utils.mamba_cache.ensure_mamba_support
¶
Ensure MambaCache batching support is enabled.
NOTE: Disabled for mlx-lm >= 0.30.6 where ArraysCache natively supports all batch operations (extract, merge, filter, prepare). The old patch replaced ArraysCache with BatchMambaCache, which broke hybrid models (Qwen3.5) that mix ArraysCache + KVCache layers.
Source code in vllm_mlx/utils/mamba_cache.py
Complete contract reference¶
Expand any definition for its exact inputs, annotations, defaults, return contract, directly raised exceptions, source-grounded behavior, and immutable line link. This section includes private and nested definitions that ordinary API generators omit.
vllm_mlx.utils.mamba_cache.BatchMambaCache · class
Batch-aware MambaCache for continuous batching.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
left_padding |
Optional[List[int]] |
no |
None |
Amount of left padding for each sequence in batch |
size |
int |
no |
2 |
Number of state arrays (default 2 for Mamba models) |
Returns
- Constructs:
vllm_mlx.utils.mamba_cache.BatchMambaCache
Exceptions and behavior
Class BatchMambaCache derives from MambaCache and declares 3 direct member(s).
No direct raise statement appears in this definition.
vllm_mlx.utils.mamba_cache.BatchMambaCache.__init__ · method
vllm_mlx.utils.mamba_cache.BatchMambaCache.__init__(left_padding: Optional[List[int]] = None, size: int = 2) -> not annotated
Initialize BatchMambaCache.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
left_padding |
Optional[List[int]] |
no |
None |
Amount of left padding for each sequence in batch |
size |
int |
no |
2 |
Number of state arrays (default 2 for Mamba models) |
Returns
- Type:
not annotated
Exceptions and behavior
Method BatchMambaCache.__init__ updates self._batch_size; calls super().__init__, super, len.
No direct raise statement appears in this definition.
vllm_mlx.utils.mamba_cache.BatchMambaCache.extract · method
Extract a single cache from the batch.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
idx |
int |
yes |
none |
Index of the sequence to extract |
Returns
- Type:
MambaCache - Direct return expressions:
cache
Exceptions and behavior
Method BatchMambaCache.extract calls len, MambaCache, mx.contiguous; returns cache.
No direct raise statement appears in this definition.
vllm_mlx.utils.mamba_cache.BatchMambaCache.merge · method
Merge multiple MambaCache objects into a BatchMambaCache.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
caches |
List[MambaCache] |
yes |
none |
List of MambaCache objects to merge |
Returns
- Type:
'BatchMambaCache' - Direct return expressions:
cls([]);merged_cache
Exceptions and behavior
Method BatchMambaCache.merge calls cls, len, range, merged_cache.cache.append; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.utils.mamba_cache.patch_mlx_lm_for_mamba · function
Patch mlx-lm to support MambaCache in BatchGenerator.
Parameters
This callable has no explicit inputs.
Returns
- Type:
not annotated
Exceptions and behavior
Function patch_mlx_lm_for_mamba calls importlib.import_module, logger.info.
No direct raise statement appears in this definition.
vllm_mlx.utils.mamba_cache.patch_mlx_lm_for_mamba._patched_make_cache · nested function
vllm_mlx.utils.mamba_cache.patch_mlx_lm_for_mamba._patched_make_cache(model, left_padding, max_kv_size = None) -> not annotated
Convert a list of regular caches into their corresponding batch-aware caches, with support for MambaCache.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
model |
not annotated |
yes |
none |
The model to create cache for |
left_padding |
not annotated |
yes |
none |
Left padding for batch |
max_kv_size |
not annotated |
no |
None |
Maximum KV cache size (mlx-lm 0.30.6+) |
Returns
- Type:
not annotated - Direct return expressions:
[to_batch_cache(c) for c in cache];[BatchRotatingKVCache(max_kv_size, left_padding) for _ in model.layers];[BatchKVCache(left_padding) for _ in model.layers]
Exceptions and behavior
Nested Function patch_mlx_lm_for_mamba._patched_make_cache calls hasattr, model.make_cache, to_batch_cache, BatchRotatingKVCache; has 3 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.utils.mamba_cache.patch_mlx_lm_for_mamba._patched_make_cache.to_batch_cache · nested function
vllm_mlx.utils.mamba_cache.patch_mlx_lm_for_mamba._patched_make_cache.to_batch_cache(c) -> not annotated
Nested Function patch_mlx_lm_for_mamba._patched_make_cache.to_batch_cache calls isinstance, BatchKVCache, BatchMambaCache, mx.array; can raise ValueError; has 5 explicit return paths.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
c |
not annotated |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
not annotated - Direct return expressions:
BatchKVCache(left_padding);BatchMambaCache(left_padding);c;BatchRotatingKVCache(c.max_size, left_padding);CacheList(*(to_batch_cache(sub_c) for sub_c in c.caches))
Exceptions and behavior
Nested Function patch_mlx_lm_for_mamba._patched_make_cache.to_batch_cache calls isinstance, BatchKVCache, BatchMambaCache, mx.array; can raise ValueError; has 5 explicit return paths.
Directly raised exceptions: ValueError.
vllm_mlx.utils.mamba_cache.patch_mlx_lm_for_mamba._patched_merge_caches · nested function
Merge caches with MambaCache support.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
caches |
not annotated |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
not annotated - Direct return expressions:
batch_cache
Exceptions and behavior
Nested Function patch_mlx_lm_for_mamba._patched_merge_caches calls range, len, isinstance, BatchKVCache.merge; can raise ValueError; returns batch_cache.
Directly raised exceptions: ValueError.
vllm_mlx.utils.mamba_cache.ensure_mamba_support · function
Ensure MambaCache batching support is enabled.
Parameters
This callable has no explicit inputs.
Returns
- Type:
not annotated
Exceptions and behavior
Function ensure_mamba_support calls logger.info.
No direct raise statement appears in this definition.
Complete symbol map¶
This map also includes private definitions and nested helpers. The signature column exposes every explicit input even when an internal helper has no dedicated parameter prose.
| Symbol | Kind | Signature and inputs | What it does | Source |
|---|---|---|---|---|
BatchMambaCache |
class | BatchMambaCache(left_padding: Optional[List[int]] = None, size: int = 2) |
Batch-aware MambaCache for continuous batching. | #L24-L96 |
BatchMambaCache.__init__ |
method | BatchMambaCache.__init__(left_padding: Optional[List[int]] = None, size: int = 2) -> not annotated |
Initialize BatchMambaCache. | #L32-L43 |
BatchMambaCache.extract |
method | BatchMambaCache.extract(idx: int) -> MambaCache |
Extract a single cache from the batch. | #L45-L63 |
BatchMambaCache.merge |
method | BatchMambaCache.merge(caches: List[MambaCache]) -> 'BatchMambaCache' |
Merge multiple MambaCache objects into a BatchMambaCache. | #L66-L96 |
patch_mlx_lm_for_mamba |
function | patch_mlx_lm_for_mamba() -> not annotated |
Patch mlx-lm to support MambaCache in BatchGenerator. | #L99-L194 |
patch_mlx_lm_for_mamba._patched_make_cache |
nested function | patch_mlx_lm_for_mamba._patched_make_cache(model, left_padding, max_kv_size = None) -> not annotated |
Convert a list of regular caches into their corresponding batch-aware caches, with support for MambaCache. | #L126-L166 |
patch_mlx_lm_for_mamba._patched_make_cache.to_batch_cache |
nested function | patch_mlx_lm_for_mamba._patched_make_cache.to_batch_cache(c) -> not annotated |
Nested Function patch_mlx_lm_for_mamba._patched_make_cache.to_batch_cache calls isinstance, BatchKVCache, BatchMambaCache, mx.array; can raise ValueError; has 5 explicit return paths. |
#L137-L155 |
patch_mlx_lm_for_mamba._patched_merge_caches |
nested function | patch_mlx_lm_for_mamba._patched_merge_caches(caches) -> not annotated |
Merge caches with MambaCache support. | #L174-L190 |
ensure_mamba_support |
function | ensure_mamba_support() -> not annotated |
Ensure MambaCache batching support is enabled. | #L201-L215 |