scripts.add_mtp_weights¶
Add MTP (Multi-Token Prediction) weights to an existing MLX Qwen3-Next model.
View the complete module source at #L1-L341.
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
¶
Add MTP (Multi-Token Prediction) weights to an existing MLX Qwen3-Next model.
This script: 1. Downloads the MTP shard from the original BF16 HuggingFace model 2. Extracts MTP weights (mtp.* keys) 3. Quantizes them to match the existing MLX model's quantization 4. Adds them to the MLX model's safetensors files 5. Updates config.json with num_nextn_predict_layers=1
Usage
python add_mtp_weights.py [--mlx-model-path PATH] [--source-model MODEL]
Requirements
pip install mlx
scripts.add_mtp_weights.DEFAULT_MLX_MODEL
module-attribute
¶
DEFAULT_MLX_MODEL = os.path.expanduser('~/.cache/huggingface/hub/models--mlx-community--Qwen3-Next-80B-A3B-Instruct-6bit')
scripts.add_mtp_weights.DEFAULT_SOURCE_MODEL
module-attribute
¶
scripts.add_mtp_weights.MTP_SHARD_NAME
module-attribute
¶
scripts.add_mtp_weights.MTP_SHARD_URL
module-attribute
¶
MTP_SHARD_URL = f'https://huggingface.co/{DEFAULT_SOURCE_MODEL}/resolve/main/{MTP_SHARD_NAME}'
scripts.add_mtp_weights.find_snapshot_dir
¶
Find the latest snapshot directory in HF cache structure.
Source code in scripts/add_mtp_weights.py
scripts.add_mtp_weights.download_mtp_shard
¶
Download the MTP shard using curl with resume support.
Source code in scripts/add_mtp_weights.py
scripts.add_mtp_weights.extract_and_quantize_mtp_weights
¶
Extract MTP weights, quantize, and save to MLX model directory.
Source code in scripts/add_mtp_weights.py
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 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | |
scripts.add_mtp_weights.update_model_index
¶
Update model.safetensors.index.json to include MTP weight keys.
Source code in scripts/add_mtp_weights.py
scripts.add_mtp_weights.update_config
¶
Update config.json to enable MTP.
Source code in scripts/add_mtp_weights.py
scripts.add_mtp_weights.main
¶
Source code in scripts/add_mtp_weights.py
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 | |
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.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.download_mtp_shard · function
Download the MTP shard using curl with resume support.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
dest_path |
Path |
yes |
none |
Required positional or keyword input. |
source_model |
str |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
Path - Direct return expressions:
shard_path
Exceptions and behavior
Function download_mtp_shard calls shard_path.exists, print, shard_path.stat, subprocess.run; can raise RuntimeError; returns shard_path.
Directly raised exceptions: RuntimeError.
scripts.add_mtp_weights.extract_and_quantize_mtp_weights · function
scripts.add_mtp_weights.extract_and_quantize_mtp_weights(shard_path: Path, snapshot_dir: Path, quantization_bits: int = 6) -> not annotated
Extract MTP weights, quantize, and save to MLX model directory.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
shard_path |
Path |
yes |
none |
Required positional or keyword input. |
snapshot_dir |
Path |
yes |
none |
Required positional or keyword input. |
quantization_bits |
int |
no |
6 |
Optional positional or keyword input; defaults to 6. |
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, print, mx.load, str; returns (mtp_output_file, list(quantized_weights.keys())).
No direct raise statement appears in this definition.
scripts.add_mtp_weights.extract_and_quantize_mtp_weights._quantize_one · nested function
scripts.add_mtp_weights.extract_and_quantize_mtp_weights._quantize_one(key, weight) -> not annotated
Quantize a single weight, apply norm adjustment, return dict entries.
Parameters
| Name | Type | Required | Default | Description |
|---|---|---|---|---|
key |
not annotated |
yes |
none |
Required positional or keyword input. |
weight |
not annotated |
yes |
none |
Required positional or keyword input. |
Returns
- Type:
not annotated - 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.update_model_index · function
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 |
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.update_config · function
Update config.json to enable MTP.
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, json.dump, print.
No direct raise statement appears in this definition.
scripts.add_mtp_weights.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. | #L38-L51 |
download_mtp_shard |
function | download_mtp_shard(dest_path: Path, source_model: str) -> Path |
Download the MTP shard using curl with resume support. | #L54-L82 |
extract_and_quantize_mtp_weights |
function | extract_and_quantize_mtp_weights(shard_path: Path, snapshot_dir: Path, quantization_bits: int = 6) -> not annotated |
Extract MTP weights, quantize, and save to MLX model directory. | #L85-L195 |
extract_and_quantize_mtp_weights._quantize_one |
nested function | extract_and_quantize_mtp_weights._quantize_one(key, weight) -> not annotated |
Quantize a single weight, apply norm adjustment, return dict entries. | #L137-L160 |
update_model_index |
function | update_model_index(snapshot_dir: Path, mtp_keys: list) -> not annotated |
Update model.safetensors.index.json to include MTP weight keys. | #L198-L221 |
update_config |
function | update_config(snapshot_dir: Path) -> not annotated |
Update config.json to enable MTP. | #L224-L236 |
main |
function | main() -> not annotated |
Function main calls argparse.ArgumentParser, parser.add_argument, parser.parse_args, print. |
#L239-L337 |