hf2q 0.1.9

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
use std::io::Write;
use std::os::unix::fs::FileExt;
use std::path::Path;

use crate::core::provenance::tensor_execution::ArtifactEvidence;
use crate::intelligence::calibration::{
    TeacherGreedyPromptReceipt, TeacherPredictionPointReceipt, VerifiedTeacherPredictionPlan,
};

use super::publication::RetainedTargetTemp;
use super::reservation::{
    reservation_contract_sha256, StructuralTeacherTargetReservationReceiptV1,
    UnpublishedStructuralTeacherTargetReservation,
};
use super::*;

impl UnpublishedStructuralTeacherTargetReservation {
    pub(crate) fn validate_private(&self) -> Result<(), ExactTeacherTargetError> {
        if reservation_contract_sha256(&self.receipt)? != self.receipt.reservation_contract_sha256 {
            return Err(ExactTeacherTargetError::Invalid(
                "teacher target reservation receipt does not reproduce".into(),
            ));
        }
        self.temporary
            .verify_private_and_absent(u64::try_from(TARGET_MAGIC.len()).unwrap())?;
        let mut magic = vec![0_u8; TARGET_MAGIC.len()];
        self.temporary
            .as_file()
            .read_exact_at(&mut magic, 0)
            .map_err(|error| ExactTeacherTargetError::io(self.temporary.output(), error))?;
        if magic != TARGET_MAGIC {
            return Err(ExactTeacherTargetError::Invalid(
                "teacher target reservation magic differs".into(),
            ));
        }
        Ok(())
    }

    pub(crate) fn begin<'a>(
        self,
        plan: &'a VerifiedTeacherPredictionPlan,
    ) -> Result<StructuralTeacherTargetStream<'a>, ExactTeacherTargetError> {
        self.validate_private()?;
        let preflight = preflight_structural_teacher_target(
            plan,
            self.receipt.vocabulary_size,
            self.receipt.limits,
        )?;
        if plan.manifest().manifest_sha256 != self.receipt.prediction_plan_sha256
            || plan.prediction_point_count() != self.receipt.prediction_point_count
            || plan.manifest().greedy_prompts.len() != self.receipt.generation_prompt_count
            || preflight.preflight_bytes != self.receipt.final_artifact_bytes
            || reservation_contract_sha256(&self.receipt)?
                != self.receipt.reservation_contract_sha256
        {
            return Err(ExactTeacherTargetError::Invalid(
                "teacher target reservation differs from its opaque prediction plan".into(),
            ));
        }
        Ok(StructuralTeacherTargetStream {
            plan,
            temporary: self.temporary,
            vocabulary_size: self.receipt.vocabulary_size,
            limits: self.receipt.limits,
            preflight_bytes: self.receipt.final_artifact_bytes,
            offset: u64::try_from(TARGET_MAGIC.len()).unwrap(),
            rows: Vec::with_capacity(self.receipt.prediction_point_count),
            trajectories: Vec::with_capacity(self.receipt.generation_prompt_count),
        })
    }
}

/// Checked target dimensions and token vocabulary closure. This remains a
/// structural capability, but it is intentionally produced before any family
/// runner allocates model weights or Metal buffers.
pub(crate) struct StructuralTeacherTargetPreflight<'a> {
    plan: &'a VerifiedTeacherPredictionPlan,
    vocabulary_size: usize,
    limits: TeacherTargetArtifactLimits,
    preflight_bytes: u64,
}

pub(crate) fn preflight_structural_teacher_target(
    plan: &VerifiedTeacherPredictionPlan,
    vocabulary_size: usize,
    limits: TeacherTargetArtifactLimits,
) -> Result<StructuralTeacherTargetPreflight<'_>, ExactTeacherTargetError> {
    if vocabulary_size == 0
        || vocabulary_size > limits.max_vocabulary_size
        || plan.prediction_point_count() == 0
        || plan.prediction_point_count() > limits.max_prediction_rows
        || limits.top_k == 0
        || limits.top_k > vocabulary_size
        || limits.max_vocabulary_size > MAX_TARGET_VOCABULARY_SIZE
        || limits.max_prediction_rows > MAX_TARGET_PREDICTION_ROWS
        || limits.max_target_bytes > MAX_TARGET_ARTIFACT_BYTES
        || limits.top_k > MAX_TARGET_TOP_K
        || plan
            .prediction_point_count()
            .checked_mul(limits.top_k)
            .is_none_or(|entries| entries > MAX_TARGET_SUMMARY_ENTRIES)
    {
        return Err(ExactTeacherTargetError::Invalid(
            "teacher target dimensions exceed their declared bounds".into(),
        ));
    }
    let preflight_bytes = checked_target_bytes(plan.prediction_point_count(), vocabulary_size)?;
    if preflight_bytes > limits.max_target_bytes {
        return Err(ExactTeacherTargetError::Invalid(
            "teacher target artifact exceeds its preflight byte bound".into(),
        ));
    }
    plan.visit_examples(|_receipt, token_ids, _points, _greedy| {
        if token_ids
            .iter()
            .any(|token_id| usize::try_from(*token_id).unwrap_or(usize::MAX) >= vocabulary_size)
        {
            return Err(ExactTeacherTargetError::Invalid(
                "teacher prediction example contains a token outside the declared vocabulary"
                    .into(),
            ));
        }
        Ok(())
    })?;
    Ok(StructuralTeacherTargetPreflight {
        plan,
        vocabulary_size,
        limits,
        preflight_bytes,
    })
}

/// Canonical row-at-a-time target writer used by the future family-owned
/// source teacher. It proves framing and plan closure only; callers cannot
/// promote its result into execution or allocator authority.
pub(crate) struct StructuralTeacherTargetStream<'a> {
    plan: &'a VerifiedTeacherPredictionPlan,
    temporary: RetainedTargetTemp,
    vocabulary_size: usize,
    limits: TeacherTargetArtifactLimits,
    preflight_bytes: u64,
    offset: u64,
    rows: Vec<TeacherTargetRowReceipt>,
    trajectories: Vec<TeacherGreedyTrajectoryReceipt>,
}

impl<'a> StructuralTeacherTargetPreflight<'a> {
    pub(crate) fn preflight_bytes(&self) -> u64 {
        self.preflight_bytes
    }

    #[cfg(test)]
    pub(crate) fn begin(
        self,
        output: &Path,
    ) -> Result<StructuralTeacherTargetStream<'a>, ExactTeacherTargetError> {
        let plan = self.plan;
        self.reserve(output)?.begin(plan)
    }

    pub(crate) fn reserve(
        self,
        output: &Path,
    ) -> Result<UnpublishedStructuralTeacherTargetReservation, ExactTeacherTargetError> {
        let Self {
            plan,
            vocabulary_size,
            limits,
            preflight_bytes,
        } = self;

        let mut temporary = RetainedTargetTemp::create(output)?;
        temporary
            .as_file_mut()
            .write_all(TARGET_MAGIC)
            .map_err(|error| ExactTeacherTargetError::io(output, error))?;

        let receipt = StructuralTeacherTargetReservationReceiptV1::new(
            plan.manifest().manifest_sha256.clone(),
            limits,
            vocabulary_size,
            plan.prediction_point_count(),
            plan.manifest().greedy_prompts.len(),
            preflight_bytes,
        )?;
        Ok(UnpublishedStructuralTeacherTargetReservation { receipt, temporary })
    }
}

impl StructuralTeacherTargetStream<'_> {
    pub(crate) fn write_row(
        &mut self,
        point: &TeacherPredictionPointReceipt,
        logits: &[f32],
    ) -> Result<u32, ExactTeacherTargetError> {
        let expected = self
            .plan
            .manifest()
            .prediction_points
            .get(self.rows.len())
            .ok_or_else(|| {
                ExactTeacherTargetError::Invalid(
                    "teacher target contains an extra prediction row".into(),
                )
            })?;
        if point != expected {
            return Err(ExactTeacherTargetError::Invalid(
                "teacher target row differs from canonical prediction-plan order".into(),
            ));
        }
        if logits.len() != self.vocabulary_size {
            return Err(ExactTeacherTargetError::Invalid(format!(
                "teacher row {} has vocabulary {}, expected {}",
                point.point_ordinal,
                logits.len(),
                self.vocabulary_size
            )));
        }
        let payload = row_bytes(logits)?;
        let (argmax_token_id, top_k, logsumexp_f64_bits) = row_summary(logits, self.limits.top_k)?;
        let payload_offset = self
            .offset
            .checked_add(ROW_FRAME_BYTES)
            .ok_or_else(|| ExactTeacherTargetError::Invalid("row offset overflow".into()))?;
        let prefix_digest = digest_to_array(&point.prefix_token_ids_sha256)?;
        let point_ordinal = u64::try_from(point.point_ordinal).map_err(|_| {
            ExactTeacherTargetError::Invalid("prediction point ordinal overflow".into())
        })?;
        let vocabulary_size = u64::try_from(self.vocabulary_size).map_err(|_| {
            ExactTeacherTargetError::Invalid("target vocabulary size overflow".into())
        })?;
        let payload_bytes = u64::try_from(payload.len()).map_err(|_| {
            ExactTeacherTargetError::Invalid("target row byte length overflow".into())
        })?;
        self.temporary
            .as_file_mut()
            .write_all(ROW_MAGIC)
            .and_then(|_| {
                self.temporary
                    .as_file_mut()
                    .write_all(&point_ordinal.to_le_bytes())
            })
            .and_then(|_| {
                self.temporary
                    .as_file_mut()
                    .write_all(&vocabulary_size.to_le_bytes())
            })
            .and_then(|_| self.temporary.as_file_mut().write_all(&prefix_digest))
            .and_then(|_| {
                self.temporary
                    .as_file_mut()
                    .write_all(&payload_bytes.to_le_bytes())
            })
            .and_then(|_| self.temporary.as_file_mut().write_all(&payload))
            .map_err(|error| ExactTeacherTargetError::io(self.temporary.output(), error))?;
        self.offset = payload_offset
            .checked_add(payload_bytes)
            .ok_or_else(|| ExactTeacherTargetError::Invalid("row end overflow".into()))?;
        self.rows.push(TeacherTargetRowReceipt {
            point_ordinal: point.point_ordinal,
            stable_id: point.stable_id.clone(),
            point_kind: point.kind,
            prefix_token_count: point.prefix_token_count,
            prefix_token_ids_sha256: point.prefix_token_ids_sha256.clone(),
            vocabulary_size: self.vocabulary_size,
            payload_offset,
            payload_bytes,
            payload_sha256: hash_bytes(&payload),
            argmax_token_id,
            top_k,
            logsumexp_f64_bits,
        });
        Ok(argmax_token_id)
    }

    pub(crate) fn write_trajectory(
        &mut self,
        prompt: &TeacherGreedyPromptReceipt,
        token_ids: &[u32],
    ) -> Result<(), ExactTeacherTargetError> {
        let expected = self
            .plan
            .manifest()
            .greedy_prompts
            .get(self.trajectories.len())
            .ok_or_else(|| {
                ExactTeacherTargetError::Invalid(
                    "teacher target contains an extra greedy trajectory".into(),
                )
            })?;
        if prompt != expected {
            return Err(ExactTeacherTargetError::Invalid(
                "teacher greedy trajectory differs from canonical prediction-plan order".into(),
            ));
        }
        if token_ids.len() != EXACT_TEACHER_GREEDY_TOKEN_COUNT {
            return Err(ExactTeacherTargetError::Invalid(
                "teacher greedy trajectory must contain exactly 32 tokens".into(),
            ));
        }
        if token_ids.iter().any(|token_id| {
            usize::try_from(*token_id).unwrap_or(usize::MAX) >= self.vocabulary_size
        }) {
            return Err(ExactTeacherTargetError::Invalid(
                "teacher greedy trajectory contains a token outside the declared vocabulary".into(),
            ));
        }
        self.trajectories.push(TeacherGreedyTrajectoryReceipt {
            stable_id: prompt.stable_id.clone(),
            prompt_token_ids_sha256: prompt.prefix_token_ids_sha256.clone(),
            token_ids: token_ids.to_vec(),
            token_ids_sha256: trajectory_sha256(token_ids)?,
        });
        Ok(())
    }

    /// Seal and independently verify the exact temporary inode without
    /// publishing it at the requested destination.
    pub(crate) fn finish_unpublished(
        mut self,
    ) -> Result<UnpublishedStructuralTeacherTargetArtifact, ExactTeacherTargetError> {
        if self.rows.len() != self.plan.prediction_point_count()
            || self.trajectories.len() != self.plan.manifest().greedy_prompts.len()
        {
            return Err(ExactTeacherTargetError::Invalid(
                "teacher target is missing prediction rows or greedy trajectories".into(),
            ));
        }
        if self.offset != self.preflight_bytes {
            return Err(ExactTeacherTargetError::Invalid(
                "written teacher target size differs from preflight".into(),
            ));
        }
        self.temporary
            .as_file_mut()
            .flush()
            .and_then(|_| self.temporary.as_file().sync_all())
            .map_err(|error| ExactTeacherTargetError::io(self.temporary.output(), error))?;
        let artifact_sha256 =
            hash_open_file_bounded(self.temporary.as_file_mut(), self.preflight_bytes)?;
        let mut receipt = ExactTeacherTargetReceipt {
            schema_version: EXACT_TEACHER_TARGET_SCHEMA_VERSION,
            semantics: TARGET_SEMANTICS.into(),
            prediction_plan_sha256: self.plan.manifest().manifest_sha256.clone(),
            limits: self.limits,
            vocabulary_size: self.vocabulary_size,
            prediction_point_count: self.plan.prediction_point_count(),
            generation_prompt_count: self.plan.manifest().greedy_prompts.len(),
            target_artifact: ArtifactEvidence {
                artifact_id: "exact_teacher_logits".into(),
                role: "structural_full_vocabulary_f32_target_rows".into(),
                byte_len: self.preflight_bytes,
                sha256: artifact_sha256,
            },
            rows: self.rows,
            greedy_trajectories: self.trajectories,
            receipt_sha256: String::new(),
        };
        receipt.receipt_sha256 = receipt_sha256(&receipt)?;
        verify::verify_structural_teacher_target_artifact(self.temporary.as_file_mut(), &receipt)?;
        Ok(UnpublishedStructuralTeacherTargetArtifact {
            receipt,
            temporary: self.temporary,
        })
    }

    /// Compatibility transition for structural-only callers. Family-owned
    /// execution builds its completion receipt between `finish_unpublished`
    /// and `publish_noclobber` instead.
    #[cfg(test)]
    pub(crate) fn finish(
        self,
    ) -> Result<StructurallyVerifiedTeacherTargetArtifact, ExactTeacherTargetError> {
        self.finish_unpublished()?.publish_noclobber()
    }
}

impl UnpublishedStructuralTeacherTargetArtifact {
    /// Reverify the retained temporary inode and publish it without replacing
    /// an existing destination. This is the final fallible transition: after
    /// successful publication no additional receipt construction is needed.
    pub(crate) fn publish_noclobber(
        self,
    ) -> Result<StructurallyVerifiedTeacherTargetArtifact, ExactTeacherTargetError> {
        let receipt = self.receipt;
        let output = self.temporary.output().to_owned();
        let expected_len = receipt.target_artifact.byte_len;
        let file = self.temporary.publish_noclobber(expected_len, |file| {
            verify::verify_structural_teacher_target_artifact(file, &receipt)
        })?;
        Ok(StructurallyVerifiedTeacherTargetArtifact {
            receipt,
            _file: file,
            path: output,
        })
    }
}

/// A byte-verified target retained under a private temporary name until a
/// family-owned completion receipt is ready.
pub(crate) struct UnpublishedStructuralTeacherTargetArtifact {
    receipt: ExactTeacherTargetReceipt,
    temporary: RetainedTargetTemp,
}

impl UnpublishedStructuralTeacherTargetArtifact {
    pub(crate) fn receipt(&self) -> &ExactTeacherTargetReceipt {
        &self.receipt
    }

    #[cfg(test)]
    pub(in crate::intelligence::exact_teacher) fn retained_file_for_test(&mut self) -> &mut File {
        self.temporary.as_file_mut()
    }
}