Files
leleka/control-plane/src/mtp_fetch.rs
T

220 lines
7.5 KiB
Rust

//! `tools/mtp_fetch.py`: the MTP block of the BF16 checkpoint without the checkpoint.
//!
//! The GSQ-RCO GGUF ships no MTP head; the BF16 checkpoint (Qwen/Qwen3.8-Flash-Next, 131 shards) has 31 `mtp.*`
//! tensors scattered over 28 shards. Safetensors puts a JSON header (name -> dtype, shape, byte range) at the start of
//! each shard, so HTTP range requests read the headers and then only the MTP tensors.
//!
//! * `inventory --out DIR`: headers only; writes `mtp-inventory.json` and `.md`.
//! * `fetch --out DIR [--only SUBSTR]`: the tensors themselves, resumable; one raw file per tensor plus
//! `mtp-manifest.json`.
//! * `verify --out DIR`: the fetched tensors against SHA-256 (exit 3: some are missing or wrong).
//!
//! #327: a mirror or proxy that ignores the Range header answers 200 with the whole shard, and a proxy may cut that to
//! the requested length, so neither the size nor a hash of what was downloaded catches it. A range read therefore needs
//! a 206 whose Content-Range is the range asked for, and every tensor of the pinned revision is checked against the
//! SHA-256 table below.
use std::{
collections::{BTreeMap, BTreeSet},
fs::{self, File, OpenOptions},
io::{Read, Write},
path::{Path, PathBuf},
time::Duration,
};
use reqwest::{
Client, StatusCode,
header::{CONTENT_RANGE, RANGE, USER_AGENT},
};
use serde::Serialize;
use serde_json::{Map, Value};
use sha2::{Digest, Sha256};
type Result<T> = std::result::Result<T, crate::BoxError>;
/// A fixed commit of the checkpoint (its `sha` on 2026-09-30), so every install reads the same tensors;
/// `STRATA_MTP_REVISION` overrides it (e.g. `main`).
pub const PINNED_REVISION: &str = "de4b8e4d43b917e7706784d8bb445c9af86a3540";
/// `verify`'s exit code: a tensor is missing or corrupt.
pub const BAD: i32 = 3;
const CHUNK: u64 = 64 << 20;
const HEADERS_ATTEMPTS: u32 = 4;
/// SHA-256 of every MTP tensor's bytes at [`PINNED_REVISION`] (verbatim from `mtp_fetch.py`).
pub const SHA256: &[(&str, &str)] = &[
(
"mtp.fc_embedding.weight",
"241d6e87ded317a61b1aeb3ca4e28c53f1caba09ad5be66092e91fe5e3e14751",
),
(
"mtp.fc_hidden.weight",
"97c8e427ad0177e2c713ed24a397d815b523eda15ef453f1ae6463f6c70a8f96",
),
(
"mtp.hyper_connection_mixer.hc_norm.weight",
"ce948a398e15aeaa403c424cda34e6eed296ab2570d591bb5ff3da642b255d64",
),
(
"mtp.hyper_connection_mixer.input_mix_weight_down.weight",
"c633d2841fda463e82045e0b93fb8cb8d511ec0c9eda616020f01aca18d71947",
),
(
"mtp.hyper_connection_mixer.input_mix_weight_up.weight",
"6565285f0ba2923703252763cfc11d8a9bda40ab42e7548e12464b04dbdbec13",
),
(
"mtp.layers.0.attn_hyper_connection.block_inject_weight.weight",
"c8afff64e7f54412d02120097c4255033c55245e1c1eed84cc630d428ccb864c",
),
(
"mtp.layers.0.attn_hyper_connection.hc_norm.weight",
"1fb619aeaaf637ccd70f46f1f8f8116e6a46448cb43026b246de8ee8286ed809",
),
(
"mtp.layers.0.attn_hyper_connection.input_mix_weight_down.weight",
"e80ac8bf3e270e9d5a949ec7c913329dc8de017d69851334820fae7b8c86a7f5",
),
(
"mtp.layers.0.attn_hyper_connection.input_mix_weight_up.weight",
"f80f1fd44e1183e7764ea496b11ff89ead9dd758f861d4e79a770bf54956b1a5",
),
(
"mtp.layers.0.mlp.experts.down_proj",
"539c46234f73bac70450f731f40e715f77ac32faf77def0f3f800e58aaabbbc0",
),
(
"mtp.layers.0.mlp.experts.gate_up_proj",
"839e98f9642ebc884b178abd2090b6dc8b0bcd81ed92d7f7a08314c6a68f96d5",
),
(
"mtp.layers.0.mlp.gate.weight",
"f0d35fa41b360a08bc0c5b1e5766aa42df960291716ee189208b5d2599462c20",
),
(
"mtp.layers.0.mlp.shared_expert.down_proj.weight",
"d2ced8c81367cc494599d1a8cdc279c3dd6579b628ded05acd7495e99cee01d8",
),
(
"mtp.layers.0.mlp.shared_expert.gate_proj.weight",
"f4a81109165853b3b64f36d809f98094b599763364070f5fa079200e1c386d4c",
),
(
"mtp.layers.0.mlp.shared_expert.up_proj.weight",
"172586861eea53efbff10afc43ded5b6e49c83879e9efc01e089c652bbb0a156",
),
(
"mtp.layers.0.mlp.shared_expert_gate.weight",
"acdd306d9996d356e4667a16ff63af6fef086fc7f570f1bc00bbe882a2a93f6e",
),
(
"mtp.layers.0.mlp_hyper_connection.block_inject_weight.weight",
"c5782dd92b479a1f8ef43d683fe9c917fd9e8fba81f23c1aae8817bfe4b476a6",
),
(
"mtp.layers.0.mlp_hyper_connection.hc_norm.weight",
"21a504876b5e7b788301557c68dfb757999430eba36c66ec8ab96a63604dbfd1",
),
(
"mtp.layers.0.mlp_hyper_connection.input_mix_weight_down.weight",
"b71240036dea954e034d5ae04e097bcd7ac51cb28a44e4c93d198af3447278a1",
),
(
"mtp.layers.0.mlp_hyper_connection.input_mix_weight_up.weight",
"40756866bf2a5e82c28e407f717b6fa07614d593f78e79c7ceb2cd4ffe6dda94",
),
(
"mtp.layers.0.self_attn.indexer.index_qk_proj.weight",
"2befafae6e944c1fc651fd73d4d527eb6f138a6c1679065b003128ee3150e859",
),
(
"mtp.layers.0.self_attn.indexer.k_layernorm.weight",
"2db621f6a08b4085756fc69d60a4a1bd1d7fbf5d406456d2477075842efaae54",
),
(
"mtp.layers.0.self_attn.indexer.q_layernorm.weight",
"afaf59f68e251939970ffdeacad0667dffc657a522bea62cdc44a52816fc49d6",
),
(
"mtp.layers.0.self_attn.k_norm.weight",
"8f924b0357cf936e14fc34518205d000e1423aa60626827df81b0804fae8cdfd",
),
(
"mtp.layers.0.self_attn.k_proj.weight",
"1a295dc2d6e633e808aeb19e00c97856a1817bbfcc83f5b610e2bfcacfcb8c38",
),
(
"mtp.layers.0.self_attn.o_proj.weight",
"9d2fcf0ac6ac19de87f638f710cfd1af6ad14141e75b7d3d5d29fbd06f374219",
),
(
"mtp.layers.0.self_attn.q_norm.weight",
"57957aa1e7c5a3ac628bff0ec5522ad55a39b8c0b8bd2d324ae640e3771617ef",
),
(
"mtp.layers.0.self_attn.q_proj.weight",
"5220aef3d288f40759f7356af9ef453e5ac0256a38749a9bb803a4e9db58150d",
),
(
"mtp.layers.0.self_attn.v_proj.weight",
"39cb12f5424e965fa71fb81db0ee230c701b7b1e0b72c36641fde7e0e41bff41",
),
(
"mtp.pre_fc_norm_embedding.weight",
"04c4a570850e06f2d8913da8220d54d4c7f87db6eb6d45480b938e8ba41d6a86",
),
(
"mtp.pre_fc_norm_hidden.weight",
"b933b44559879449b1baf2af8385c5c237347307e3fd452394870cb53e6eab6d",
),
];
/// One MTP tensor of the checkpoint: where its bytes are in which shard.
#[derive(Serialize, Clone, Debug, PartialEq, Eq)]
pub struct Row {
pub name: String,
pub shard: String,
pub dtype: String,
pub shape: Vec<u64>,
pub start: u64,
pub end: u64,
pub bytes: u64,
}
#[derive(Serialize)]
struct Inventory<'a> {
repo: &'a str,
total_bytes: u64,
tensors: &'a [Row],
}
#[derive(Serialize)]
struct ManifestRow<'a> {
#[serde(flatten)]
row: &'a Row,
file: String,
sha256: String,
}
pub struct Fetcher {
client: Client,
/// Where the shards are read from (ends with `/`).
repo: String,
/// The revision the hashes belong to.
pinned: String,
sha256: BTreeMap<String, String>,
backoff: fn(u32) -> Duration,
}
mod repo_url;
#[allow(unused_imports)]
pub use repo_url::*;
mod fetcher;
#[allow(unused_imports)]
pub use fetcher::*;
mod mtime_ns;
#[allow(unused_imports)]
pub use mtime_ns::*;
mod fetch_steps;
use fetch_steps::{drop_foreign_file, write_manifest};
mod inventory_steps;
use inventory_steps::{check_all_found, inventory_markdown};