vllm_mlx.patches.qwen3_5_mtp¶
Runtime MTP (Multi-Token Prediction) support for Qwen3.5 models.
View the complete module source at #L1-L512.
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_5_mtp
¶
Runtime MTP (Multi-Token Prediction) support for Qwen3.5 models.
Qwen3.5 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_qwen35.py.
Since mlx_lm's qwen3_5.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
Supports both Dense (27B) and MoE (122B-A10B, 35B-A3B) architectures.
The actual MTP scheduling logic lives in
- vllm_mlx/scheduler.py (_install_mtp, _mtp_step, _mtp_next)
vllm_mlx.patches.qwen3_5_mtp._MTP_KEY_PREFIXES
module-attribute
¶
vllm_mlx.patches.qwen3_5_mtp._QWEN_MTP_RMSNORM_WEIGHT_SUFFIXES
module-attribute
¶
_QWEN_MTP_RMSNORM_WEIGHT_SUFFIXES = ('input_layernorm.weight', 'post_attention_layernorm.weight', 'q_norm.weight', 'k_norm.weight', 'pre_fc_norm_hidden.weight', 'pre_fc_norm_embedding.weight', 'norm.weight')
vllm_mlx.patches.qwen3_5_mtp._QWEN_MTP_HIDDEN_STATE_MODES
module-attribute
¶
vllm_mlx.patches.qwen3_5_mtp._strip_mtp_key_prefix
¶
Return an MTP-relative key for supported standalone shard layouts.
vllm_mlx.patches.qwen3_5_mtp._resolve_qwen_mtp_hidden_state_mode
¶
Resolve the checkpoint's MTP hidden-state contract safely.
Source code in vllm_mlx/patches/qwen3_5_mtp.py
vllm_mlx.patches.qwen3_5_mtp._select_qwen_mtp_hidden_state
¶
Select the representation expected by the checkpoint's MTP head.
vllm_mlx.patches.qwen3_5_mtp._is_qwen_mtp_rmsnorm_weight
¶
Return True for MTP RMSNorm weights that use Qwen's offset convention.
Source code in vllm_mlx/patches/qwen3_5_mtp.py
vllm_mlx.patches.qwen3_5_mtp._apply_qwen_mtp_rmsnorm_offset_fixups
¶
Apply Qwen raw-offset RMSNorm fixups without double-shifting MLX weights.
Source code in vllm_mlx/patches/qwen3_5_mtp.py
vllm_mlx.patches.qwen3_5_mtp._fixup_moe_mtp
¶
Fix missing weights in MoE MTP module.
MoE MTP checkpoints (122B, 35B) only contain: fc, q_proj, o_proj, shared_expert.*, and per-expert weights. Missing: - k_proj, v_proj → zero out (attention becomes no-op) - gate, shared_expert_gate → copy from main model's last full-attn layer - norms → already at identity (weight=1.0), no action needed
Source code in vllm_mlx/patches/qwen3_5_mtp.py
vllm_mlx.patches.qwen3_5_mtp.inject_mtp_support
¶
Inject MTP module into a loaded Qwen3.5 model.
mlx_lm's qwen3_5.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_5_mtp.py
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 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 | |
vllm_mlx.patches.qwen3_5_mtp.validate_mtp_support
¶
Validate that a loaded model has working MTP support.
Checks: 1. model.mtp exists and is not None 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_5_mtp.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.patches.qwen3_5_mtp._strip_mtp_key_prefix · function
Return an MTP-relative key for supported standalone shard layouts.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
key |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
str | None - Direct return expressions:
key.removeprefix(prefix);None
Exceptions and behavior
Function _strip_mtp_key_prefix calls key.startswith, key.removeprefix; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.patches.qwen3_5_mtp._is_qwen_mtp_rmsnorm_weight · function
Return True for MTP RMSNorm weights that use Qwen's offset convention.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
key |
str |
yes |
none |
Required positional or keyword input. |
weight |
not annotated |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
bool - Direct return expressions:
weight.ndim == 1 and any((key.endswith(suffix) for suffix in _QWEN_MTP_RMSNORM_WEIGHT_SUFFIXES))
Exceptions and behavior
Function _is_qwen_mtp_rmsnorm_weight calls any, key.endswith; returns weight.ndim == 1 and any((key.endswith(suffix) for suffix in _QWEN_MTP_RMSNORM_WEIGHT_SUFFIXES)).
No direct raise statement appears in this definition.
vllm_mlx.patches.qwen3_5_mtp._apply_qwen_mtp_rmsnorm_offset_fixups · function
Apply Qwen raw-offset RMSNorm fixups without double-shifting MLX weights.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
mtp_weights |
dict |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
int - Direct return expressions:
norm_fixup_count
Exceptions and behavior
Function _apply_qwen_mtp_rmsnorm_offset_fixups calls list, mtp_weights.items, _is_qwen_mtp_rmsnorm_weight, weight.mean().item; returns norm_fixup_count.
No direct raise statement appears in this definition.
vllm_mlx.patches.qwen3_5_mtp._fixup_moe_mtp · function
Fix missing weights in MoE MTP module.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
mtp |
not annotated |
yes |
none |
Required positional or keyword input. |
inner_model |
not annotated |
yes |
none |
Required positional or keyword input. |
loaded_keys |
set |
yes |
none |
Required positional or keyword input. |
mx |
not annotated |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
None - Direct return expressions:
None
Exceptions and behavior
Function _fixup_moe_mtp calls reversed, logger.warning, getattr, mlx.utils.tree_flatten; returns None.
No direct raise statement appears in this definition.
vllm_mlx.patches.qwen3_5_mtp.inject_mtp_support · function
Inject MTP module into a loaded Qwen3.5 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, _resolve_qwen_mtp_hidden_state_mode, text_config.get, logger.info; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.patches.qwen3_5_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_5_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_5_mtp.inject_mtp_support._MTPModule.__init__ · nested function
vllm_mlx.patches.qwen3_5_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_5_mtp.inject_mtp_support._Qwen3_5MTP · nested class
Qwen3.5 with MTP support (injected at runtime).
Parameters
This callable has no explicit inputs.
Returns
- Constructs:
vllm_mlx.patches.qwen3_5_mtp.inject_mtp_support._Qwen3_5MTP
Exceptions and behavior
Nested Class inject_mtp_support._Qwen3_5MTP derives from original_class and declares 3 direct member(s).
No direct raise statement appears in this definition.
vllm_mlx.patches.qwen3_5_mtp.inject_mtp_support._Qwen3_5MTP.__call__ · nested function
vllm_mlx.patches.qwen3_5_mtp.inject_mtp_support._Qwen3_5MTP.__call__(inputs, cache = None, return_hidden: bool = False, input_embeddings = None, **kwargs) -> not annotated
Nested Function inject_mtp_support._Qwen3_5MTP.__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. |
input_embeddings |
not annotated |
no |
None |
Optional positional or keyword input; defaults to None. |
**kwargs |
not annotated |
no |
none |
Additional variadic keyword inputs accepted by this callable. |
Returns
- Type:
not annotated - Direct return expressions:
(out, _select_qwen_mtp_hidden_state(hidden_state_mode, hidden_states, normed));out
Exceptions and behavior
Nested Function inject_mtp_support._Qwen3_5MTP.__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_5_mtp.inject_mtp_support._Qwen3_5MTP.mtp_forward · nested function
vllm_mlx.patches.qwen3_5_mtp.inject_mtp_support._Qwen3_5MTP.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._Qwen3_5MTP.mtp_forward calls self.model.embed_tokens, self.mtp.pre_fc_norm_embedding, self.mtp.pre_fc_norm_hidden, self.mtp.fc; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.patches.qwen3_5_mtp.inject_mtp_support._Qwen3_5MTP.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._Qwen3_5MTP.make_mtp_cache calls KVCache; has 2 explicit return paths.
No direct raise statement appears in this definition.
vllm_mlx.patches.qwen3_5_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 hasattr, getattr, logger.warning, 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 |
|---|---|---|---|---|
_strip_mtp_key_prefix |
function | _strip_mtp_key_prefix(key: str) -> str \| None |
Return an MTP-relative key for supported standalone shard layouts. | #L31-L36 |
_resolve_qwen_mtp_hidden_state_mode |
function | _resolve_qwen_mtp_hidden_state_mode(config: dict) -> str |
Resolve the checkpoint's MTP hidden-state contract safely. | #L52-L65 |
_select_qwen_mtp_hidden_state |
function | _select_qwen_mtp_hidden_state(mode: str, hidden_states, normed) -> not annotated |
Select the representation expected by the checkpoint's MTP head. | #L68-L70 |
_is_qwen_mtp_rmsnorm_weight |
function | _is_qwen_mtp_rmsnorm_weight(key: str, weight) -> bool |
Return True for MTP RMSNorm weights that use Qwen's offset convention. | #L73-L77 |
_apply_qwen_mtp_rmsnorm_offset_fixups |
function | _apply_qwen_mtp_rmsnorm_offset_fixups(mtp_weights: dict) -> int |
Apply Qwen raw-offset RMSNorm fixups without double-shifting MLX weights. | #L80-L90 |
_fixup_moe_mtp |
function | _fixup_moe_mtp(mtp, inner_model, loaded_keys: set, mx) -> None |
Fix missing weights in MoE MTP module. | #L93-L157 |
inject_mtp_support |
function | inject_mtp_support(model: Any, model_path, config: dict) -> bool |
Inject MTP module into a loaded Qwen3.5 model. | #L160-L447 |
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). |
#L239-L252 |
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. |
#L240-L252 |
inject_mtp_support._Qwen3_5MTP |
nested class | inject_mtp_support._Qwen3_5MTP() |
Qwen3.5 with MTP support (injected at runtime). | #L368-L438 |
inject_mtp_support._Qwen3_5MTP.__call__ |
nested function | inject_mtp_support._Qwen3_5MTP.__call__(inputs, cache = None, return_hidden: bool = False, input_embeddings = None, **kwargs) -> not annotated |
Nested Function inject_mtp_support._Qwen3_5MTP.__call__ calls inner.embed_tokens, len, create_attention_mask, create_ssm_mask; has 2 explicit return paths. |
#L371-L408 |
inject_mtp_support._Qwen3_5MTP.mtp_forward |
nested function | inject_mtp_support._Qwen3_5MTP.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. | #L410-L432 |
inject_mtp_support._Qwen3_5MTP.make_mtp_cache |
nested function | inject_mtp_support._Qwen3_5MTP.make_mtp_cache() -> not annotated |
Create KV cache for MTP layers. | #L434-L438 |
validate_mtp_support |
function | validate_mtp_support(model: Any) -> bool |
Validate that a loaded model has working MTP support. | #L450-L512 |