Files
leleka/crates/offline-tools/src/cli.rs
T
Pavlo RatushnyiandClaude Sonnet 5.5 3485221865 feat(offline-tools): port embd_bf16_pack, ple_fp8_pack, draft_vocab, make_profile, mtp_rt
New crate leleka-offline-tools wired as 'strata-next tools ...'. Every output is byte-identical to the unmodified
Python tool (scripts/offline/diff_tools.py: packs incl. refusal cases, 14 draft-vocab subsets x out/stats, make_profile
at 512/256/8 experts with ranked bases and traces, mtp_rt at the real H/FF geometry). Committed tiny goldens +
integration tests; mtp_rt's multi-MB input is regenerated from a shared splitmix64 rule.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
2026-10-14 15:40:02 +02:00

114 lines
4.4 KiB
Rust

//! `strata-next tools <name> ...`: the argument parsing for the ports in this crate. Each subcommand mirrors its
//! Python tool's flags.
use std::path::{Path, PathBuf};
use crate::{Result, draft_vocab, make_profile, mtp_rt, packs};
pub const USAGE: &str = "tools embd-bf16-pack --model DIR --out FILE.gguf|tools ple-fp8-pack --model DIR --out FILE.gguf|tools draft-vocab --gguf FIRST.gguf [--base IDS.bin] [--add han,kana,hangul,cjk_punct,cjk,cyrillic] [--out IDS.bin] [--stats]|tools make-profile [TRACE ...] (--base PROFILE.bin | --no-base) --out PROFILE.bin [--n-expert N]|tools mtp-rt --gguf mtp-q2_0.gguf --out DIR [--n-expert N]";
struct Flags<'a> {
args: &'a [String],
}
impl<'a> Flags<'a> {
fn value(&self, name: &str) -> Result<Option<&'a str>> {
match self.args.iter().position(|a| a == name) {
None => Ok(None),
Some(at) => self
.args
.get(at + 1)
.map(|v| Some(v.as_str()))
.ok_or_else(|| format!("{name} needs a value").into()),
}
}
fn required(&self, name: &str) -> Result<&'a str> {
self.value(name)?
.ok_or_else(|| format!("{name} is required").into())
}
fn has(&self, name: &str) -> bool {
self.args.iter().any(|a| a == name)
}
}
pub fn run(args: &[String]) -> Result<String> {
let [command, rest @ ..] = args else {
return Err(format!("usage: strata-next {USAGE}").into());
};
let flags = Flags { args: rest };
match command.as_str() {
"embd-bf16-pack" => packs::embd_bf16_pack(
Path::new(flags.required("--model")?),
Path::new(flags.required("--out")?),
),
"ple-fp8-pack" => packs::ple_fp8_pack(
Path::new(flags.required("--model")?),
Path::new(flags.required("--out")?),
),
"draft-vocab" => {
let base = flags.value("--base")?.map(Path::new);
let out = flags.value("--out")?.map(Path::new);
draft_vocab::run(&draft_vocab::Options {
gguf: Path::new(flags.required("--gguf")?),
base,
add: flags.value("--add")?.unwrap_or(""),
out,
stats: flags.has("--stats"),
})
}
"make-profile" => {
// positional arguments are the traces: everything that is not a flag or a flag's value
let mut traces: Vec<PathBuf> = Vec::new();
let mut skip = false;
for arg in rest {
if std::mem::take(&mut skip) {
continue;
}
match arg.as_str() {
"--base" | "--out" | "--n-expert" => skip = true,
"--no-base" => {}
flag if flag.starts_with("--") => {
return Err(format!("unknown option {flag}").into());
}
trace => traces.push(trace.into()),
}
}
let trace_refs: Vec<&Path> = traces.iter().map(PathBuf::as_path).collect();
let base = match (flags.value("--base")?, flags.has("--no-base")) {
(Some(_), true) => return Err("--base and --no-base are exclusive".into()),
(Some(path), false) => Some(Path::new(path)),
(None, true) => None,
(None, false) => return Err("give --base PROFILE.bin or --no-base".into()),
};
let n_expert = match flags.value("--n-expert")? {
Some(n) => n.parse()?,
None => make_profile::N_EXPERT,
};
make_profile::run(&make_profile::Options {
traces: &trace_refs,
base,
out: Path::new(flags.required("--out")?),
n_expert,
})
}
"mtp-rt" => {
let geometry = mtp_rt::Geometry {
n_expert: match flags.value("--n-expert")? {
Some(n) => n.parse()?,
None => mtp_rt::NE,
},
..Default::default()
};
mtp_rt::run(
Path::new(flags.required("--gguf")?),
Path::new(flags.required("--out")?),
&geometry,
)
}
other => {
Err(format!("unknown tools subcommand {other}; usage: strata-next {USAGE}").into())
}
}
}