scripts.add_mtp_weights_qwen35¶
Add MTP (Multi-Token Prediction) weights to an existing MLX Qwen3.5 model.
View the complete module source at #L1-L470.
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.
scripts.add_mtp_weights_qwen35
¶
Add MTP (Multi-Token Prediction) weights to an existing MLX Qwen3.5 model.
This script: 1. Fetches the safetensors index from the original BF16 HuggingFace model 2. Identifies shards containing MTP weights (mtp.* keys) 3. Downloads only those shards via curl -C - 4. Extracts MTP weights 5. For MoE models: stacks expert weights (256×) into switch_mlp format 6. Applies norm shift (HF weight → MLX weight+1.0) for RMSNorm keys 7. Quantizes to match the MLX model's quantization scheme 8. Saves as mtp/weights.safetensors (subdirectory avoids mlx_vlm glob)
Supports both: - MoE models (Qwen3.5-122B-A10B, 35B-A3B): 256 experts, sparse MTP attention - Dense models (Qwen3.5-27B): full MTP with k/v projections and norms
Usage
python add_mtp_weights_qwen35.py --mlx-model-path PATH --source-model MODEL
Requirements
pip install mlx
scripts.add_mtp_weights_qwen35.MODEL_CONFIGS
module-attribute
¶
MODEL_CONFIGS = {'Qwen/Qwen3.5-122B-A10B': {'num_experts': 256, 'hidden_size': 3072, 'is_moe': True}, 'Qwen/Qwen3.5-35B-A3B': {'num_experts': 256, 'hidden_size': 2048, 'is_moe': True}, 'Qwen/Qwen3.5-27B': {'num_experts': 0, 'hidden_size': 5120, 'is_moe': False}}
scripts.add_mtp_weights_qwen35.find_snapshot_dir
¶
Find the latest snapshot directory in HF cache structure.
Source code in scripts/add_mtp_weights_qwen35.py
scripts.add_mtp_weights_qwen35.fetch_shard_index
¶
Fetch model.safetensors.index.json from HuggingFace.
Source code in scripts/add_mtp_weights_qwen35.py
scripts.add_mtp_weights_qwen35.identify_mtp_shards
¶
Identify which shards contain MTP weights.
Returns:
-
tuple[dict[str, str], set[str]]–Tuple of (mtp_key_to_shard mapping, set of shard filenames to download)
Source code in scripts/add_mtp_weights_qwen35.py
scripts.add_mtp_weights_qwen35.download_shards
¶
Download required shards using curl with resume support.
Source code in scripts/add_mtp_weights_qwen35.py
scripts.add_mtp_weights_qwen35.extract_and_quantize_mtp_weights
¶
extract_and_quantize_mtp_weights(mtp_keys: dict[str, str], shard_paths: dict[str, Path], snapshot_dir: Path, is_moe: bool, num_experts: int, no_quantize: bool = False)
Extract MTP weights from BF16 shards, optionally quantize, and save.
Source code in scripts/add_mtp_weights_qwen35.py
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 | |
scripts.add_mtp_weights_qwen35.update_model_index
¶
Update model.safetensors.index.json to include MTP weight keys.
Source code in scripts/add_mtp_weights_qwen35.py
scripts.add_mtp_weights_qwen35.update_config
¶
Update config.json to signal MTP availability.
For Qwen3.5, mtp_num_hidden_layers already exists in text_config. We add num_nextn_predict_layers at top level for vllm-mlx compatibility.
Source code in scripts/add_mtp_weights_qwen35.py
scripts.add_mtp_weights_qwen35.main
¶
Source code in scripts/add_mtp_weights_qwen35.py
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 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 | |
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.
scripts.add_mtp_weights_qwen35.find_snapshot_dir · function
Find the latest snapshot directory in HF cache structure.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
model_path |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
Path - Direct return expressions:
Path(model_path);snapshots[-1]
Exceptions and behavior
Function find_snapshot_dir calls Path, snapshots_dir.exists, (Path(model_path) / 'config.json').exists, FileNotFoundError; can raise FileNotFoundError; has 2 explicit return paths.
Directly raised exceptions: FileNotFoundError.
scripts.add_mtp_weights_qwen35.fetch_shard_index · function
Fetch model.safetensors.index.json from HuggingFace.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
source_model |
str |
yes |
none |
Required positional or keyword input. |
download_dir |
Path |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
dict - Direct return expressions:
json.load(f)
Exceptions and behavior
Function fetch_shard_index calls print, subprocess.run, str, RuntimeError; can raise RuntimeError; returns json.load(f).
Directly raised exceptions: RuntimeError.
scripts.add_mtp_weights_qwen35.identify_mtp_shards · function
Identify which shards contain MTP weights.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
index |
dict |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
tuple[dict[str, str], set[str]] - Direct return expressions:
(mtp_keys, shards_needed)
Exceptions and behavior
Function identify_mtp_shards calls index.get, set, weight_map.items, key.startswith; returns (mtp_keys, shards_needed).
No direct raise statement appears in this definition.
scripts.add_mtp_weights_qwen35.download_shards · function
scripts.add_mtp_weights_qwen35.download_shards(shards: set[str], source_model: str, download_dir: Path) -> dict[str, Path]
Download required shards using curl with resume support.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
shards |
set[str] |
yes |
none |
Required positional or keyword input. |
source_model |
str |
yes |
none |
Required positional or keyword input. |
download_dir |
Path |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
dict[str, Path] - Direct return expressions:
shard_paths
Exceptions and behavior
Function download_shards calls sorted, shard_path.exists, shard_path.stat, print; can raise RuntimeError; returns shard_paths.
Directly raised exceptions: RuntimeError.
scripts.add_mtp_weights_qwen35.extract_and_quantize_mtp_weights · function
scripts.add_mtp_weights_qwen35.extract_and_quantize_mtp_weights(mtp_keys: dict[str, str], shard_paths: dict[str, Path], snapshot_dir: Path, is_moe: bool, num_experts: int, no_quantize: bool = False) -> not annotated
Extract MTP weights from BF16 shards, optionally quantize, and save.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
mtp_keys |
dict[str, str] |
yes |
none |
Required positional or keyword input. |
shard_paths |
dict[str, Path] |
yes |
none |
Required positional or keyword input. |
snapshot_dir |
Path |
yes |
none |
Required positional or keyword input. |
is_moe |
bool |
yes |
none |
Required positional or keyword input. |
num_experts |
int |
yes |
none |
Required positional or keyword input. |
no_quantize |
bool |
no |
False |
Optional positional or keyword input; defaults to False. |
Returns
- Type:
not annotated - Direct return expressions:
(mtp_output_file, list(quantized_weights.keys()))
Exceptions and behavior
Function extract_and_quantize_mtp_weights calls mx.set_default_device, open, json.load, config.get; returns (mtp_output_file, list(quantized_weights.keys())).
No direct raise statement appears in this definition.
scripts.add_mtp_weights_qwen35.extract_and_quantize_mtp_weights._quantize_one · nested function
scripts.add_mtp_weights_qwen35.extract_and_quantize_mtp_weights._quantize_one(key: str, weight: mx.array) -> dict[str, mx.array]
Quantize a single weight, apply norm adjustment.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
key |
str |
yes |
none |
Required positional or keyword input. |
weight |
mx.array |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
dict[str, mx.array] - Direct return expressions:
{key: weight};{key: q_w, key.replace('.weight', '.scales'): q_s, key.replace('.weight', '.biases'): q_b}
Exceptions and behavior
Nested Function extract_and_quantize_mtp_weights._quantize_one calls any, key.endswith, mx.eval, print; has 2 explicit return paths.
No direct raise statement appears in this definition.
scripts.add_mtp_weights_qwen35.update_model_index · function
scripts.add_mtp_weights_qwen35.update_model_index(snapshot_dir: Path, mtp_keys: list[str]) -> not annotated
Update model.safetensors.index.json to include MTP weight keys.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
snapshot_dir |
Path |
yes |
none |
Required positional or keyword input. |
mtp_keys |
list[str] |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
not annotated - Direct return expressions:
None
Exceptions and behavior
Function update_model_index calls index_path.exists, print, open, json.load; returns None.
No direct raise statement appears in this definition.
scripts.add_mtp_weights_qwen35.update_config · function
Update config.json to signal MTP availability.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
snapshot_dir |
Path |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
not annotated
Exceptions and behavior
Function update_config calls open, json.load, config.get, text_config.get.
No direct raise statement appears in this definition.
scripts.add_mtp_weights_qwen35.main · function
Function main calls argparse.ArgumentParser, parser.add_argument, parser.parse_args, print.
Parameters
This callable has no explicit inputs.
Returns
- Type:
not annotated
Exceptions and behavior
Function main calls argparse.ArgumentParser, parser.add_argument, parser.parse_args, print.
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 |
|---|---|---|---|---|
find_snapshot_dir |
function | find_snapshot_dir(model_path: str) -> Path |
Find the latest snapshot directory in HF cache structure. | #L53-L63 |
fetch_shard_index |
function | fetch_shard_index(source_model: str, download_dir: Path) -> dict |
Fetch model.safetensors.index.json from HuggingFace. | #L66-L80 |
identify_mtp_shards |
function | identify_mtp_shards(index: dict) -> tuple[dict[str, str], set[str]] |
Identify which shards contain MTP weights. | #L83-L98 |
download_shards |
function | download_shards(shards: set[str], source_model: str, download_dir: Path) -> dict[str, Path] |
Download required shards using curl with resume support. | #L101-L130 |
extract_and_quantize_mtp_weights |
function | extract_and_quantize_mtp_weights(mtp_keys: dict[str, str], shard_paths: dict[str, Path], snapshot_dir: Path, is_moe: bool, num_experts: int, no_quantize: bool = False) -> not annotated |
Extract MTP weights from BF16 shards, optionally quantize, and save. | #L133-L272 |
extract_and_quantize_mtp_weights._quantize_one |
nested function | extract_and_quantize_mtp_weights._quantize_one(key: str, weight: mx.array) -> dict[str, mx.array] |
Quantize a single weight, apply norm adjustment. | #L204-L229 |
update_model_index |
function | update_model_index(snapshot_dir: Path, mtp_keys: list[str]) -> not annotated |
Update model.safetensors.index.json to include MTP weight keys. | #L275-L294 |
update_config |
function | update_config(snapshot_dir: Path) -> not annotated |
Update config.json to signal MTP availability. | #L297-L322 |
main |
function | main() -> not annotated |
Function main calls argparse.ArgumentParser, parser.add_argument, parser.parse_args, print. |
#L325-L466 |