hf2q 0.1.32

Pure Rust CLI for converting HuggingFace models to hardware-optimized formats and serving them over an OpenAI-compatible API on Apple Silicon
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
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
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
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
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
404
405
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
//! Runtime joins to independently reviewed build/artifact evidence. A packet
//! approval boolean is never a replacement for the separate review receipt.
use super::{protocol::Workload, receipt::*, store::hash};
use anyhow::{ensure, Context, Result};
use serde::{Deserialize, Serialize};
use sha2::{Digest, Sha256};
use std::{
    cell::RefCell,
    fs::File,
    io::Read,
    os::unix::fs::MetadataExt,
    path::{Path, PathBuf},
};
#[cfg(test)]
#[path = "acquisition/c2.rs"]
pub(crate) mod c2;
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct FileIdentity {
    pub device: u64,
    pub inode: u64,
    pub bytes: u64,
    pub mtime: i64,
    pub mtime_ns: i64,
    pub ctime: i64,
    pub ctime_ns: i64,
}
impl FileIdentity {
    pub fn observed(file: &File) -> Result<Self> {
        let m = file.metadata()?;
        ensure!(m.is_file(), "bound model is not a regular file");
        Ok(Self {
            device: m.dev(),
            inode: m.ino(),
            bytes: m.len(),
            mtime: m.mtime(),
            mtime_ns: m.mtime_nsec(),
            ctime: m.ctime(),
            ctime_ns: m.ctime_nsec(),
        })
    }
}
fn same_opened_file(expected: &FileIdentity, observed: &FileIdentity) -> Result<()> {
    ensure!(expected == observed, "opened model FD identity changed");
    Ok(())
}
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields)]
pub struct FileBinding {
    pub path: PathBuf,
    pub sha256: String,
}
#[derive(Clone, Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Binding {
    pub composition: FileBinding,
    pub independent_review: FileBinding,
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Composition {
    schema: String,
    comparison_key: ComparisonKey,
    model_file: FileIdentity,
    full_content_receipt: FileBinding,
}
#[derive(Deserialize)]
#[serde(deny_unknown_fields)]
struct Review {
    schema: String,
    verdict: String,
    composition_sha256: String,
}

pub(crate) fn reviewed_model_identity(binding: &Binding, identity: &Identity) -> Result<FileIdentity> {
    let composition: Composition =
        serde_json::from_slice(&checked_file(&binding.composition, 1024 * 1024)?)?;
    let review: Review =
        serde_json::from_slice(&checked_file(&binding.independent_review, 65536)?)?;
    ensure!(
        composition.schema == "hf2q.q1.acquisition-composition.v1"
            && review.schema == "hf2q.q1.acquisition-review.v1"
            && review.verdict == "Accepted"
            && review.composition_sha256 == binding.composition.sha256,
        "independent acquisition composition is unverified"
    );
    ensure!(
        composition.comparison_key == ComparisonKey::from(identity),
        "composition immutable comparison key mismatch"
    );
    let content = checked_file(&composition.full_content_receipt, 16 * 1024 * 1024)?;
    full_content_receipt_has_model(&content, &identity.model_sha256)?;
    Ok(composition.model_file)
}
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct ComparisonKey {
    pub family: Family,
    pub source_commit: String,
    pub binary_sha256: String,
    pub native_version: String,
    pub native_checksum: String,
    pub model_sha256: String,
    pub tokenizer_sha256: String,
    pub template_sha256: String,
    pub config_sha256: String,
    pub token_vectors_sha256: String,
}
impl From<&Identity> for ComparisonKey {
    fn from(i: &Identity) -> Self {
        Self {
            family: i.family,
            source_commit: i.source_commit.clone(),
            binary_sha256: i.binary_sha256.clone(),
            native_version: i.native_version.clone(),
            native_checksum: i.native_checksum.clone(),
            model_sha256: i.model_sha256.clone(),
            tokenizer_sha256: i.tokenizer_sha256.clone(),
            template_sha256: i.template_sha256.clone(),
            config_sha256: i.config_sha256.clone(),
            token_vectors_sha256: i.token_vectors_sha256.clone(),
        }
    }
}
#[derive(Clone, Debug, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct Acquisition {
    pub packet_sha256: String,
    pub nonce: String,
    pub output_root: PathBuf,
    pub actual_binary_sha256: String,
    pub composition_sha256: String,
    pub independent_review_sha256: String,
    pub opened_model: FileIdentity,
    pub observed_model_opens: u64,
    pub required_model_opens: u64,
    pub device_registry_id: u64,
    pub queue_identity: u64,
    pub model_instance: u64,
    pub provenance: String,
}
#[derive(Clone, Debug, Serialize)]
pub struct ConfigRecipe<'a> {
    pub family: Family,
    pub capacity: u64,
    pub slots: u32,
    pub output_allowance: u64,
    pub cache_mode: &'a str,
    pub route: &'a str,
    pub actual_model_config_sha256: String,
}
pub fn tokens_digest(work: &Workload) -> Result<String> {
    work.validate().map_err(anyhow::Error::msg)?;
    let mut digest = Sha256::new();
    digest.update(b"hf2q.q1.tokens.le-u32.v1\0");
    digest.update((work.lanes.len() as u32).to_le_bytes());
    for lane in &work.lanes {
        digest.update((lane.len() as u32).to_le_bytes());
        for token in lane {
            digest.update(token.to_le_bytes());
        }
    }
    Ok(hex::encode(digest.finalize()))
}
pub fn config_digest(recipe: &ConfigRecipe<'_>) -> Result<String> {
    Ok(hash(&serde_json::to_vec(recipe)?))
}
pub(crate) fn checked_file(binding: &FileBinding, max: u64) -> Result<Vec<u8>> {
    ensure!(
        binding.sha256.len() == 64 && binding.path.is_absolute(),
        "invalid external proof binding"
    );
    let meta = std::fs::symlink_metadata(&binding.path)?;
    ensure!(
        meta.is_file() && !meta.file_type().is_symlink() && meta.len() <= max,
        "external proof extent/type"
    );
    let mut bytes = Vec::new();
    File::open(&binding.path)?
        .take(max + 1)
        .read_to_end(&mut bytes)?;
    ensure!(
        bytes.len() as u64 <= max && hash(&bytes) == binding.sha256,
        "external proof hash differs"
    );
    Ok(bytes)
}
pub(crate) fn full_content_receipt_has_model(bytes: &[u8], model_sha: &str) -> Result<()> {
    fn has(value: &serde_json::Value, sha: &str) -> bool {
        match value {
            serde_json::Value::String(s) => s == sha,
            serde_json::Value::Array(values) => values.iter().any(|v| has(v, sha)),
            serde_json::Value::Object(values) => values.values().any(|v| has(v, sha)),
            _ => false,
        }
    }
    let value: serde_json::Value = serde_json::from_slice(bytes)?;
    ensure!(
        has(&value, model_sha),
        "retained full-content receipt does not name model digest"
    );
    Ok(())
}
pub(crate) fn file_digest(path: &Path) -> Result<String> {
    use std::os::unix::fs::OpenOptionsExt;
    let mut file = std::fs::OpenOptions::new().read(true).custom_flags(libc::O_NONBLOCK).open(path)?;
    ensure!(file.metadata()?.is_file(), "digest source is not a regular file");
    let mut digest = Sha256::new();
    let mut page = [0u8; 65536];
    loop {
        let n = file.read(&mut page)?;
        if n == 0 {
            break;
        }
        digest.update(&page[..n]);
    }
    Ok(hex::encode(digest.finalize()))
}
pub(crate) fn executable_digest() -> Result<String> {
    file_digest(&std::env::current_exe()?)
}
struct Active {
    file: File,
    expected: FileIdentity,
    opens: u64,
    nonce: String,
}
thread_local! {static ACTIVE:RefCell<Option<Active>>=const{RefCell::new(None)};}
pub struct Scope {
    acquisition: Acquisition,
    config_checked: std::cell::Cell<bool>,
    _same_thread: std::marker::PhantomData<std::rc::Rc<()>>,
}
impl Scope {
    /// Before any model load/GPU work: verify actual opened FD and executable
    /// against a separate root-reviewed composition, not a packet assertion.
    pub fn begin(
        binding: &Binding,
        identity: &Identity,
        work: &Workload,
        model: &Path,
        output: &Path,
        packet: &[u8],
    ) -> Result<Self> {
        identity.validate().map_err(anyhow::Error::msg)?;
        ensure!(
            identity.family == work.family && tokens_digest(work)? == identity.token_vectors_sha256,
            "actual workload digest/family mismatch"
        );
        Self::begin_bound(binding,identity,model,output,packet)
    }
    fn begin_bound(binding:&Binding,identity:&Identity,model:&Path,output:&Path,packet:&[u8])->Result<Self>{
        let reviewed_model = reviewed_model_identity(binding, identity)?;
        ensure!(
            option_env!("HF2Q_BUILD_GIT_SHA") == Some(identity.source_commit.as_str()),
            "compiled source stamp absent/wrong"
        );
        let actual_binary_sha256 = executable_digest()?;
        ensure!(
            actual_binary_sha256 == identity.binary_sha256,
            "actual executable differs from reviewed build"
        );
        let file = File::open(model)?;
        let opened = FileIdentity::observed(&file)?;
        same_opened_file(&reviewed_model, &opened)?;
        ensure!(
            output.is_absolute()
                && output.parent().context("output parent")?.canonicalize()?
                    == output.parent().unwrap(),
            "canonical acquisition output parent required"
        );
        let mut nonce = [0u8; 32];
        File::open("/dev/urandom")?.read_exact(&mut nonce)?;
        let nonce = hex::encode(nonce);
        ACTIVE.with(|slot| -> Result<()> {
            ensure!(slot.borrow().is_none(), "nested acquisition");
            *slot.borrow_mut() = Some(Active {
                file,
                expected: opened.clone(),
                opens: 0,
                nonce: nonce.clone(),
            });
            Ok(())
        })?;
        Ok(Self {
            acquisition: Acquisition {
                packet_sha256: hash(packet),
                nonce,
                output_root: output.to_path_buf(),
                actual_binary_sha256,
                composition_sha256: binding.composition.sha256.clone(),
                independent_review_sha256: binding.independent_review.sha256.clone(),
                opened_model: opened,
                observed_model_opens: 0,
                required_model_opens: 0,
                device_registry_id: 0,
                queue_identity: 0,
                model_instance: 0,
                provenance: "IndependentRootReviewedComposition".into(),
            },
            config_checked: std::cell::Cell::new(false),
            _same_thread: std::marker::PhantomData,
        })
    }
    pub fn check_config(&self, identity: &Identity, recipe: &ConfigRecipe<'_>) -> Result<()> {
        ensure!(
            config_digest(recipe)? == identity.config_sha256,
            "actual model/cache/route config differs from immutable comparison key"
        );
        self.config_checked.set(true);
        Ok(())
    }
    pub fn check_unchanged(&self) -> Result<()> {
        ACTIVE.with(|slot| -> Result<()> {
            let slot = slot.borrow();
            let active = slot.as_ref().context("acquisition disappeared")?;
            ensure!(
                active.nonce == self.acquisition.nonce
                    && FileIdentity::observed(&active.file)? == active.expected,
                "model FD mutated during acquisition"
            );
            Ok(())
        })
    }
    pub fn observed(
        &self,
        device: &mlx_native::MlxDevice,
        model_instance: u64,
        required_model_opens: u64,
    ) -> Result<Acquisition> {
        use mlx_native::metal::foreign_types::ForeignType;
        ensure!(self.acquisition.provenance=="IndependentRootReviewedComposition","preparation cannot attest a comparable acquisition");
        ensure!(
            self.config_checked.get(),
            "actual config recipe was not checked"
        );
        let mut result = self.acquisition.clone();
        ACTIVE.with(|slot| -> Result<()> {
            let slot = slot.borrow();
            let active = slot.as_ref().context("acquisition disappeared")?;
            ensure!(
                active.nonce == result.nonce
                    && FileIdentity::observed(&active.file)? == active.expected
                    && required_model_opens > 0
                    && active.opens >= required_model_opens,
                "model FD changed or actual loader did not consume bound descriptor"
            );
            result.observed_model_opens = active.opens;
            result.required_model_opens = required_model_opens;
            Ok(())
        })?;
        result.device_registry_id = device.registry_id();
        result.queue_identity = device.metal_queue().as_ptr() as usize as u64;
        result.model_instance = model_instance;
        ensure!(
            result.device_registry_id != 0 && result.queue_identity != 0 && model_instance != 0,
            "missing observed runtime identity"
        );
        Ok(result)
    }
}
impl Drop for Scope {
    fn drop(&mut self) {
        ACTIVE.with(|slot| {
            let mut slot = slot.borrow_mut();
            if slot
                .as_ref()
                .is_some_and(|a| a.nonce == self.acquisition.nonce)
            {
                slot.take();
            }
        });
    }
}
/// Test-only loader seam. The exact File checked by fstat is consumed by the
/// production parser; ordinary tests without a scope keep the existing open.
pub fn open_model(path: &Path) -> Result<mlx_native::gguf::GgufFile> {
    let file = File::open(path)?;
    ACTIVE.with(|slot| -> Result<()> {
        if let Some(active) = slot.borrow_mut().as_mut() {
            same_opened_file(&active.expected, &FileIdentity::observed(&file)?)?;
            active.opens = active
                .opens
                .checked_add(1)
                .context("model open count overflow")?;
        }
        Ok(())
    })?;
    Ok(mlx_native::gguf::GgufFile::from_file(file)?)
}

pub fn validate_pair_for(
    left_root: &Path,
    left: &Trace,
    right_root: &Path,
    right: &Trace,
    recovery: bool,
) -> std::result::Result<bool, String> {
    let l = left
        .acquisition
        .as_ref()
        .ok_or("left acquisition unverified")?;
    let r = right
        .acquisition
        .as_ref()
        .ok_or("right acquisition unverified")?;
    if (l.nonce == r.nonce) != recovery {
        return Err("acquisition nonce does not match comparison purpose".into());
    }
    let synthetic =
        l.provenance == "SyntheticOracleControl" && r.provenance == "SyntheticOracleControl";
    if !synthetic && !recovery && l.packet_sha256 == r.packet_sha256 {
        return Err("same acquisition packet cannot be relabelled across trajectories".into());
    }
    for (root, trace, a) in [(left_root, left, l), (right_root, right, r)] {
        let hex = |s: &str| {
            s.len() == 64
                && s.bytes()
                    .all(|b| b.is_ascii_hexdigit() && !b.is_ascii_uppercase())
        };
        if a.output_root != root
            || !hex(&a.nonce)
            || !hex(&a.packet_sha256)
            || a.actual_binary_sha256 != trace.comparison_key.binary_sha256
        {
            return Err("acquisition output/nonce/packet/build binding mismatch".into());
        }
        if !synthetic
            && (a.provenance != "IndependentRootReviewedComposition"
                || !hex(&a.composition_sha256)
                || !hex(&a.independent_review_sha256)
                || (a.required_model_opens == 0 || a.observed_model_opens < a.required_model_opens)
                || a.opened_model.inode == 0
                || a.opened_model.bytes == 0
                || a.device_registry_id == 0
                || a.queue_identity == 0
                || a.model_instance == 0)
        {
            return Err("unverified actual acquisition provenance".into());
        }
    }
    Ok(!synthetic)
}
#[cfg(test)]
pub(crate) fn synthetic(root: &Path, identity: &Identity) -> Acquisition {
    Acquisition {
        packet_sha256: hash(b"synthetic-oracle-fixture"),
        nonce: hash(root.as_os_str().as_encoded_bytes()),
        output_root: root.into(),
        actual_binary_sha256: identity.binary_sha256.clone(),
        composition_sha256: String::new(),
        independent_review_sha256: String::new(),
        opened_model: FileIdentity {
            device: 0,
            inode: 0,
            bytes: 0,
            mtime: 0,
            mtime_ns: 0,
            ctime: 0,
            ctime_ns: 0,
        },
        observed_model_opens: 0,
        required_model_opens: 0,
        device_registry_id: 0,
        queue_identity: 0,
        model_instance: 0,
        provenance: "SyntheticOracleControl".into(),
    }
}

#[cfg(test)]
#[path = "acquisition_controls.rs"]
mod controls;

#[path="acquisition/preparation.rs"]
mod preparation;
pub(crate) use preparation::preparation_prefix_digest;