vllm_mlx.patches.qwen3_next_mtp¶
Runtime MTP (Multi-Token Prediction) support for Qwen3-Next models.
View the complete module source at #L1-L261.
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.patches.qwen3_next_mtp
¶
Runtime MTP (Multi-Token Prediction) support for Qwen3-Next models.
Qwen3-Next models may include a built-in MTP head that predicts token n+2 from hidden states + token n+1. MTP weights are added to the quantized MLX model via scripts/add_mtp_weights.py.
Since mlx_lm's qwen3_next.py does NOT define MTP module/methods, this module provides: - inject_mtp_support(): dynamically creates MTP module, loads weights, and monkey-patches the model class with return_hidden, mtp_forward, and make_mtp_cache - validate_mtp_support(): checks whether a loaded model has working MTP
The actual MTP scheduling logic lives in
- vllm_mlx/scheduler.py (_install_mtp, _mtp_step, _mtp_next)
vllm_mlx.patches.qwen3_next_mtp.inject_mtp_support
¶
Inject MTP module into a loaded Qwen3-Next model.
mlx_lm's qwen3_next.py does not define MTP layers, so we: 1. Create MTP module matching the weight structure 2. Quantize it to match the base model 3. Load MTP weights from model-mtp.safetensors 4. Monkey-patch Model with return_hidden, mtp_forward, make_mtp_cache
Parameters:
-
model(Any) –A model loaded via mlx_lm (strict=False, MTP weights ignored)
-
model_path–Path to model directory (contains model-mtp.safetensors)
-
config(dict) –Parsed config.json dict
Returns:
-
bool–True if MTP was successfully injected, False otherwise.
Source code in vllm_mlx/patches/qwen3_next_mtp.py
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 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 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 | |
vllm_mlx.patches.qwen3_next_mtp.validate_mtp_support
¶
Validate that a loaded model has working MTP support.
Checks: 1. model.mtp exists and is not None (MTP module instantiated) 2. model.mtp has layers with loaded weights 3. model has return_hidden support in call 4. model has mtp_forward method 5. model has make_mtp_cache method
Parameters:
-
model(Any) –A model loaded via mlx_lm.load()
Returns:
-
bool–True if MTP is fully functional, False otherwise.
Source code in vllm_mlx/patches/qwen3_next_mtp.py
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 | |
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.patches.qwen3_next_mtp.inject_mtp_support · function
Inject MTP module into a loaded Qwen3-Next model.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
model |
Any |
yes |
none |
A model loaded via mlx_lm (strict=False, MTP weights ignored) |
model_path |
not annotated |
yes |
none |
Path to model directory (contains model-mtp.safetensors) |
config |
dict |
yes |
none |
Parsed config.json dict |
Returns
- Type:
bool - Direct return expressions:
False;True
Exceptions and behavior
Function inject_mtp_support calls config.get, logger.info, Path, mtp_file.exists; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.patches.qwen3_next_mtp.inject_mtp_support._MTPModule · nested class
Nested Class inject_mtp_support._MTPModule derives from nn.Module and declares 1 direct member(s).
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
args |
not annotated |
yes |
none |
Required positional or keyword input. |
n_layers |
not annotated |
yes |
none |
Required positional or keyword input. |
Returns
- Constructs:
vllm_mlx.patches.qwen3_next_mtp.inject_mtp_support._MTPModule
Exceptions and behavior
Nested Class inject_mtp_support._MTPModule derives from nn.Module and declares 1 direct member(s).
No direct raise statement appears in this definition.
vllm_mlx.patches.qwen3_next_mtp.inject_mtp_support._MTPModule.__init__ · nested function
vllm_mlx.patches.qwen3_next_mtp.inject_mtp_support._MTPModule.__init__(args, n_layers) -> not annotated
Nested Function inject_mtp_support._MTPModule.__init__ updates self.pre_fc_norm_hidden, self.pre_fc_norm_embedding, self.fc, self.layers; calls super().__init__, super, nn.RMSNorm, nn.Linear.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
args |
not annotated |
yes |
none |
Required positional or keyword input. |
n_layers |
not annotated |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
not annotated
Exceptions and behavior
Nested Function inject_mtp_support._MTPModule.__init__ updates self.pre_fc_norm_hidden, self.pre_fc_norm_embedding, self.fc, self.layers; calls super().__init__, super, nn.RMSNorm, nn.Linear.
No direct raise statement appears in this definition.
vllm_mlx.patches.qwen3_next_mtp.inject_mtp_support._mtp_quant_pred · nested function
Nested Function inject_mtp_support._mtp_quant_pred calls isinstance, path.endswith; has 3 explicit return paths.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
path |
not annotated |
yes |
none |
Required positional or keyword input. |
module |
not annotated |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
not annotated - Direct return expressions:
False;{'group_size': 64, 'bits': 8};True
Exceptions and behavior
Nested Function inject_mtp_support._mtp_quant_pred calls isinstance, path.endswith; has 3 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.patches.qwen3_next_mtp.inject_mtp_support._Qwen3NextMTP · nested class
Qwen3-Next with MTP support (injected at runtime).
Parameters
This callable has no explicit inputs.
Returns
- Constructs:
vllm_mlx.patches.qwen3_next_mtp.inject_mtp_support._Qwen3NextMTP
Exceptions and behavior
Nested Class inject_mtp_support._Qwen3NextMTP derives from original_class and declares 3 direct member(s).
No direct raise statement appears in this definition.
vllm_mlx.patches.qwen3_next_mtp.inject_mtp_support._Qwen3NextMTP.__call__ · nested function
vllm_mlx.patches.qwen3_next_mtp.inject_mtp_support._Qwen3NextMTP.__call__(inputs, cache = None, return_hidden: bool = False) -> not annotated
Nested Function inject_mtp_support._Qwen3NextMTP.__call__ calls inner.embed_tokens, len, create_attention_mask, create_ssm_mask; has 2 explicit return paths.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
inputs |
not annotated |
yes |
none |
Required positional or keyword input. |
cache |
not annotated |
no |
None |
Optional positional or keyword input; defaults to None. |
return_hidden |
bool |
no |
False |
Optional positional or keyword input; defaults to False. |
Returns
- Type:
not annotated - Direct return expressions:
(out, hidden_states);out
Exceptions and behavior
Nested Function inject_mtp_support._Qwen3NextMTP.__call__ calls inner.embed_tokens, len, create_attention_mask, create_ssm_mask; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.patches.qwen3_next_mtp.inject_mtp_support._Qwen3NextMTP.mtp_forward · nested function
vllm_mlx.patches.qwen3_next_mtp.inject_mtp_support._Qwen3NextMTP.mtp_forward(hidden_states, next_token_ids, cache = None, mtp_cache = None) -> not annotated
Run MTP head: predict token n+2 from hidden states + token n+1.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
hidden_states |
not annotated |
yes |
none |
Required positional or keyword input. |
next_token_ids |
not annotated |
yes |
none |
Required positional or keyword input. |
cache |
not annotated |
no |
None |
Optional positional or keyword input; defaults to None. |
mtp_cache |
not annotated |
no |
None |
Optional positional or keyword input; defaults to None. |
Returns
- Type:
not annotated - Direct return expressions:
self.model.embed_tokens.as_linear(x);self.lm_head(x)
Exceptions and behavior
Nested Function inject_mtp_support._Qwen3NextMTP.mtp_forward calls self.model.embed_tokens, self.mtp.pre_fc_norm_hidden, self.mtp.pre_fc_norm_embedding, self.mtp.fc; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.patches.qwen3_next_mtp.inject_mtp_support._Qwen3NextMTP.make_mtp_cache · nested function
Create KV cache for MTP layers.
Parameters
This callable has no explicit inputs.
Returns
- Type:
not annotated - Direct return expressions:
None;[KVCache() for _ in self.mtp.layers]
Exceptions and behavior
Nested Function inject_mtp_support._Qwen3NextMTP.make_mtp_cache calls KVCache; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.patches.qwen3_next_mtp.validate_mtp_support · function
Validate that a loaded model has working MTP support.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
model |
Any |
yes |
none |
A model loaded via mlx_lm.load() |
Returns
- Type:
bool - Direct return expressions:
False;True
Exceptions and behavior
Function validate_mtp_support calls getattr, logger.warning, logger.info, inspect.signature; has 2 explicit return paths.
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 |
|---|---|---|---|---|
inject_mtp_support |
function | inject_mtp_support(model: Any, model_path, config: dict) -> bool |
Inject MTP module into a loaded Qwen3-Next model. | #L27-L181 |
inject_mtp_support._MTPModule |
nested class | inject_mtp_support._MTPModule(args, n_layers) |
Nested Class inject_mtp_support._MTPModule derives from nn.Module and declares 1 direct member(s). |
#L68-L83 |
inject_mtp_support._MTPModule.__init__ |
nested function | inject_mtp_support._MTPModule.__init__(args, n_layers) -> not annotated |
Nested Function inject_mtp_support._MTPModule.__init__ updates self.pre_fc_norm_hidden, self.pre_fc_norm_embedding, self.fc, self.layers; calls super().__init__, super, nn.RMSNorm, nn.Linear. |
#L69-L83 |
inject_mtp_support._mtp_quant_pred |
nested function | inject_mtp_support._mtp_quant_pred(path, module) -> not annotated |
Nested Function inject_mtp_support._mtp_quant_pred calls isinstance, path.endswith; has 3 explicit return paths. |
#L93-L103 |
inject_mtp_support._Qwen3NextMTP |
nested class | inject_mtp_support._Qwen3NextMTP() |
Qwen3-Next with MTP support (injected at runtime). | #L125-L177 |
inject_mtp_support._Qwen3NextMTP.__call__ |
nested function | inject_mtp_support._Qwen3NextMTP.__call__(inputs, cache = None, return_hidden: bool = False) -> not annotated |
Nested Function inject_mtp_support._Qwen3NextMTP.__call__ calls inner.embed_tokens, len, create_attention_mask, create_ssm_mask; has 2 explicit return paths. |
#L128-L150 |
inject_mtp_support._Qwen3NextMTP.mtp_forward |
nested function | inject_mtp_support._Qwen3NextMTP.mtp_forward(hidden_states, next_token_ids, cache = None, mtp_cache = None) -> not annotated |
Run MTP head: predict token n+2 from hidden states + token n+1. | #L152-L171 |
inject_mtp_support._Qwen3NextMTP.make_mtp_cache |
nested function | inject_mtp_support._Qwen3NextMTP.make_mtp_cache() -> not annotated |
Create KV cache for MTP layers. | #L173-L177 |
validate_mtp_support |
function | validate_mtp_support(model: Any) -> bool |
Validate that a loaded model has working MTP support. | #L184-L261 |