Files
leleka/crates/deploy/src/plan/model_steps.rs
T

210 lines
7.0 KiB
Rust

//! The plan steps that bring the model in: the shards, the KAR archive, the MTP pack, the tokenizer and the configs.
use super::*;
/// The archive is accepted as it is (`--trust-archive`): the engine verifies it at each load.
pub(super) fn trusts_archive(ctx: &Context, fs: &FsState) -> bool {
ctx.opts.trust_archive && fs.kar_present
}
pub(super) fn model_step(ctx: &Context, fs: &FsState, kar_runs_tool: bool) -> Step {
let windows = ctx.facts.os == "windows";
let paths = &ctx.paths;
let manifest = MANIFEST;
let sn = strata_next(ctx);
let commands = if windows {
Vec::new()
} else {
vec![
format!(
"{sn} model fetch {manifest} --output {}",
paths.model_dir().display()
),
format!(
"{sn} model verify {manifest} --output {}",
paths.model_dir().display()
),
]
};
step(
"model",
"fetch and verify the two IQ3_S GGUF shards",
model_state(ctx, fs, kar_runs_tool),
commands,
)
}
fn model_state(ctx: &Context, fs: &FsState, kar_runs_tool: bool) -> StepState {
let opts = &ctx.opts;
if ctx.facts.os == "windows" {
windows_model_state(ctx)
} else if fs.shards_stamp_ok && !opts.reverify {
StepState::Done(
"verified earlier; sizes and mtimes unchanged (--reverify hashes again)".into(),
)
} else if kar_runs_tool {
StepState::Todo(
"downloaded and verified inside the archive step \
(ensure-sharded-streamed fetches and verifies the shards first)"
.into(),
)
} else if !opts.fetch {
no_fetch_model_state(fs)
} else {
StepState::Todo(format!(
"{} still to download; resumable, SHA-256 of 83.6 GB",
gib_text(ctx.needs.shard_bytes_missing)
))
}
}
fn windows_model_state(ctx: &Context) -> StepState {
if ctx.needs.shard_bytes_missing == 0 && windows_shards_present(&ctx.paths) {
StepState::Done(
"both shards are present with the pinned sizes \
(the engine hashes them at each load, or skips it with a verification stamp)"
.into(),
)
} else {
StepState::Blocked(format!(
"the two shards must already be in {} with their pinned sizes \
(the Windows tool does not download or build; copy them from the WSL volume, see docs/windows.md)",
ctx.paths.model_dir().display()
))
}
}
fn no_fetch_model_state(fs: &FsState) -> StepState {
if fs.shards_stamp_ok {
StepState::Todo("--no-fetch: verify only".into())
} else {
StepState::Blocked(
"--no-fetch given but the shards are not verified yet; run without --no-fetch".into(),
)
}
}
/// The state of the `kar` step; `trusted` is [`trusts_archive`].
pub(super) fn kar_state(ctx: &Context, fs: &FsState, trusted: bool) -> StepState {
let opts = &ctx.opts;
let kar_unused = ctx
.profile
.as_ref()
.is_ok_and(|p| p.kar_source == KarSource::None);
if kar_unused {
StepState::Skip(
"kar_source none (experimental): no archive is used, the PLE rows are read from the GGUF shard".into(),
)
} else if ctx.facts.os == "windows" {
windows_kar_state(ctx, fs)
} else if fs.kar_stamp_ok && !opts.reverify {
StepState::Done(format!("{ARCHIVE_BYTES} bytes, verified earlier"))
} else if trusted {
StepState::Todo(
"accept the existing archive without the strict re-derivation (--trust-archive); \
the engine verifies its SHA-256 and every CRC at each load"
.into(),
)
} else if !opts.fetch {
StepState::Blocked("--no-fetch given but the archive is missing or unverified".into())
} else {
StepState::Todo(
"build the level-2 KAR archive (bounded memory, hours on a first run) \
or re-verify the existing one (strict, tens of minutes), then stamp it"
.into(),
)
}
}
fn windows_kar_state(ctx: &Context, fs: &FsState) -> StepState {
if fs.kar_present {
StepState::Done(format!(
"{ARCHIVE_BYTES} bytes with its provenance file (the engine checks SHA-256 and CRCs at each load)"
))
} else {
StepState::Blocked(format!(
"{} (pinned length {ARCHIVE_BYTES}) and its .provenance.json must already be on the model volume; \
the Windows tool does not build the archive (build it in WSL and copy it, see docs/windows.md)",
ctx.paths.kar().display()
))
}
}
pub(super) fn kar_step(ctx: &Context, state: StepState) -> Step {
let paths = &ctx.paths;
let commands = if ctx.facts.os == "windows" {
Vec::new()
} else {
vec![format!(
"{} model ensure-sharded-streamed {MANIFEST} --source-dir {} --output {} --levels 2",
strata_next(ctx),
paths.model_dir().display(),
paths.kar().display()
)]
};
step("kar", "KAR level-2 archive (+ provenance)", state, commands)
}
pub(super) fn mtp_pack_step(ctx: &Context, fs: &FsState) -> Step {
let pack = ctx.paths.mtp_pack();
let state = if let Some(src) = &ctx.opts.mtp_pack {
if fs.mtp_present {
StepState::Done("present; --mtp-pack ignored".into())
} else {
StepState::Todo(format!("import from {}", src.display()))
}
} else if fs.mtp_present {
StepState::Done(format!("{} present", pack.display()))
} else {
StepState::Blocked(format!(
"{} is missing and no --mtp-pack was given; the pack ({}) has no automated producer yet",
pack.display(),
MTP_FILES.join(", ")
))
};
step(
"mtp-pack",
"MTP draft pack (data/mtp-rt)",
state,
vec![format!("copy/unpack -> {}", pack.display())],
)
}
pub(super) fn tokenizer_step(ctx: &Context, fs: &FsState) -> Step {
let state = if fs.tokenizer_present {
StepState::Done("present".into())
} else {
StepState::Todo("extract from shard 1".into())
};
step(
"tokenizer",
"tokenizer assets for the API",
state,
vec![format!(
"{} model extract-tokenizer {} --output {}",
strata_next(ctx),
ctx.paths.shard(0).display(),
ctx.paths.tokenizer_root().display()
)],
)
}
pub(super) fn config_step(ctx: &Context, fs: &FsState) -> Step {
let state = if fs.configs_current {
StepState::Done("up to date".into())
} else {
StepState::Todo(
"write leleka.json, daemon.json, strata.json, profile.json, runtime.env".into(),
)
};
step(
"config",
"run configuration, gateway and API configs, environment file",
state,
vec![format!(
"write {}/{{leleka,daemon,strata,profile}}.json runtime.env",
ctx.opts.dirs.state.display()
)],
)
}