llm.cli — CLI Tools¶
Command-line utilities registered under pyproject.toml [project.scripts]
(llm-train lives in llm.training.train; the llm-serve entry point
lives in llm.serving.api). Each module below is a thin Typer app.
Checkpoint migration¶
migrate_ckpt
¶
llm-migrate-ckpt — convert legacy v0.0.5 .pt checkpoints to v2.
This CLI closes the gap introduced by the v2 split-layout format
(ADR-006, commit 4b9cf68). Training checkpoints in v0.0.5 and
earlier are a single torch.save blob; v2 splits them into three
sidecars (<stem>.safetensors + <stem>.meta.json +
<stem>.extra_state.pt).
The :class:~llm.training.core.checkpoint.CheckpointManager loader
already auto-detects both layouts — legacy .pt files keep
loading with a :class:DeprecationWarning. This CLI exists so users
with a large fleet of v0.0.5 checkpoints can convert them once and
silence the warning permanently.
Usage:
llm-migrate-ckpt path/to/latest.pt # convert (keep legacy)
llm-migrate-ckpt path/to/latest # looks for latest.pt
llm-migrate-ckpt path/to/latest.pt --in-place # delete legacy after success
llm-migrate-ckpt path/to/latest.pt --verify # round-trip check (load new, compare)
llm-migrate-ckpt path/to/latest.pt --dry-run # print plan, write nothing
llm-migrate-ckpt path/to/latest.pt --overwrite # replace an existing split layout
Exit codes:
0 — conversion succeeded (or --dry-run plan was printed)
1 — conversion failed (legacy missing, split layout already present, etc.)
2 — verification failed (the new layout does not round-trip)
main
¶
main(path=typer.Argument(..., help='Path to the legacy checkpoint — either `<name>.pt` or the stem `<name>` (the function appends `.pt`).', exists=False), in_place=typer.Option(False, '--in-place', help='Delete the legacy `.pt` file after a successful conversion. Off by default.'), verify=typer.Option(False, '--verify', help='Reload the new split layout and compare against the legacy blob. Exits 2 on mismatch.'), dry_run=typer.Option(False, '--dry-run', help='Print the conversion plan and exit without writing anything.'), overwrite=typer.Option(False, '--overwrite', help='Replace an existing split-layout trio at the same stem. Off by default.'))
Convert a legacy v0.0.5 .pt checkpoint to the v2 split layout.
源代码位于: src/llm/cli/migrate_ckpt.py
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 | |
Quantization¶
quantize
¶
llm-quantize CLI — currently supports the gptq subcommand.
This CLI closes the gap between the Python API
(:func:llm.quantization.gptq.quantize_model_gptq) and the command line.
The same validation rules that the Python API enforces (GPTQConfig
__post_init__) are also enforced here so users get early, clear errors
on bad input rather than a stack trace halfway through Hessian
accumulation.
Subcommand surface (matches
docs/superpowers/plans/2026-07-22-gptq-integration.md § Task 10):
llm-quantize gptq \
--model PATH # torch.save blob with a DecoderModel \
--output PATH # where to write the quantized model \
--calib-data PATH # raw text (one sample per line) — needs --tokenizer \
--calib-data-tokens PATH # pre-tokenized tensor file — mutually exclusive with --calib-data \
--tokenizer PATH # HF tokenizer dir; required with --calib-data \
--bits {4,8} # default 4 \
--group-size N|-1 # default 128; -1 = per-channel \
[--sym|--asym] # default sym (--asym rejected: not implemented) \
[--act-order|--no-act-order] # default off \
--percdamp F # default 0.01 \
--blocksize N # default 128 \
--target-modules m1,m2,... # default: all nn.Linear layers
Exit codes:
0 — quantization succeeded
1 — argument validation failed (bad bits, missing tokenizer, etc.)
2 — runtime failure (model load, tokenization, etc.)
(Note: typer itself exits 2 — the same as a runtime failure — for
argparse-level usage errors like a missing required --model/--output,
so a bare mis-typed invocation is not distinguishable from an in-run
crash by exit code alone; the stderr message differs.)
gptq
¶
gptq(model=typer.Option(..., '--model', help='Path to model checkpoint (.pt with DecoderModel state_dict).'), output=typer.Option(..., '--output', help='Output path for quantized model (torch.save blob).'), calib_data=typer.Option(None, '--calib-data', help='Path to raw text file (one sample per line). Requires --tokenizer.'), calib_data_tokens=typer.Option(None, '--calib-data-tokens', help='Path to pre-tokenized .pt file (tensor or list of tensors). Mutually exclusive with --calib-data.'), tokenizer=typer.Option(None, '--tokenizer', help='Path to HF tokenizer (required when --calib-data is set).'), bits=typer.Option(4, '--bits', help='Quantization bit width (4 or 8).'), group_size=typer.Option(128, '--group-size', help='Group size for per-group scales (-1 = per-channel).'), sym=typer.Option(True, '--sym/--asym', help='Symmetric (default) vs asymmetric quantization.'), percdamp=typer.Option(0.01, '--percdamp', help='Hessian damping fraction (must be in (0, 1)).'), blocksize=typer.Option(128, '--blocksize', help='Column block size for the GPTQ outer loop.'), act_order=typer.Option(False, '--act-order/--no-act-order', help='Sort weight columns by diag(H) descending (better accuracy, slower).'), target_modules=typer.Option(None, '--target-modules', help='Comma-separated layer names to quantize (default: all nn.Linear).'))
Quantize a model with GPTQ (Frantar 2022, arXiv:2210.17323).
源代码位于: src/llm/cli/quantize.py
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 | |
fp8
¶
fp8(model=typer.Option(..., '--model', help='Path to model checkpoint (.pt with DecoderModel state_dict).'), output=typer.Option(..., '--output', help='Output path for quantized model (torch.save blob).'), weight_dtype=typer.Option('e4m3', '--weight-dtype', help="FP8 weight format: 'e4m3' (E4M3FN, default) or 'e5m2' (wider range)."), per_channel=typer.Option(True, '--per-channel/--per-tensor', help='Per-output-row (default) vs per-tensor weight scaling.'), activation=typer.Option('dynamic', '--activation', help="Activation scaling: 'dynamic' (per forward; default, no calibration needed) or 'static' (from calibration)."), calib_data=typer.Option(None, '--calib-data', help='Path to raw text file (one sample per line). Requires --tokenizer. Needed only for static activation.'), calib_data_tokens=typer.Option(None, '--calib-data-tokens', help='Path to pre-tokenized .pt file (tensor or list of tensors). Needed only for static activation.'), tokenizer=typer.Option(None, '--tokenizer', help='Path to HF tokenizer (required when --calib-data is set).'), target_modules=typer.Option(None, '--target-modules', help='Comma-separated layer names to quantize (default: all nn.Linear).'))
Quantize a model's Linear weights + activations with FP8 (E4M3/E5M2).
Stores real float8 weights (1 byte/weight) and simulates the fp8 matmul in fp32. Static activation scaling captures per-layer abs-max over the calibration batches; dynamic computes it per forward and needs no calibration data.
源代码位于: src/llm/cli/quantize.py
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 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 | |
Pruning¶
prune
¶
llm-prune — weight-prune a pretrained model checkpoint.
Thin wrapper over :func:llm.quantization.prune.prune_model (the sibling of the
llm-quantize CLI). Loads a bare torch.save model blob (a DecoderModel,
plain or already quantized), zeroes a fraction of each nn.Linear's weights via
a persistent weight_mask, and writes a new pruned blob atomically.
Exit codes:
0 — success (sparsity reported)
1 — argument validation failed (bad ratio / method / clobbered output)
2 — runtime failure (model load, pruning, save)
(Note: typer itself exits 2 — the same as a runtime failure — for
argparse-level usage errors like a missing required --model/--output,
so a bare mis-typed invocation is not distinguishable from an in-run
crash by exit code alone; the stderr message differs.)
prune
¶
prune(model=typer.Option(..., '--model', help='Path to model blob (.pt torch.save of a DecoderModel).'), output=typer.Option(..., '--output', help='Output path for the pruned model blob.'), ratio=typer.Option(0.5, '--ratio', help="Fraction of each Linear's weights to zero (0 < ratio < 1)."), method=typer.Option('magnitude', '--method', help="'magnitude' (keep largest |W|) or 'random'."), target_modules=typer.Option(None, '--target-modules', help='Comma-separated module-name substrings to prune (default: all Linear).'), seed=typer.Option(None, '--seed', help="Seed for 'random' pruning (reproducibility)."))
Prune a pretrained model's linear weights and save a new blob.
源代码位于: src/llm/cli/prune.py
Low-rank decomposition¶
decompose
¶
llm-decompose — low-rank (SVD U-V) factorize a pretrained model.
Thin wrapper over :func:llm.quantization.lowrank.decompose_model (the sibling
of llm-prune in the compression family). Loads a bare torch.save model
blob, factorizes each nn.Linear weight into u @ v at the requested rank,
and writes a new blob atomically, reporting rank / compression ratio / Frobenius
reconstruction error.
Exit codes:
0 — success
1 — argument validation failed (bad/conflicting rank, clobbered output)
2 — runtime failure (model load, decomposition, save)
(Note: typer itself exits 2 — the same as a runtime failure — for
argparse-level usage errors like a missing required --model/--output,
so a bare mis-typed invocation is not distinguishable from an in-run
crash by exit code alone; the stderr message differs.)
decompose
¶
decompose(model=typer.Option(..., '--model', help='Path to model blob (.pt torch.save of a DecoderModel).'), output=typer.Option(..., '--output', help='Output path for the low-rank model blob.'), rank=typer.Option(None, '--rank', help='Explicit rank r (mutually exclusive with --rank-ratio).'), rank_ratio=typer.Option(None, '--rank-ratio', help='Auto rank = ratio * min(out, in) (mutually exclusive with --rank).'), target_modules=typer.Option(None, '--target-modules', help='Comma-separated module-name substrings to decompose (default: all Linear).'))
Low-rank factorize a pretrained model's linear weights.