tokitai-operator 0.1.0

Verified DL kernel compiler: formally-checked GEMM, p-adic, sheaf, contract-carrying ops. Paper-artifact grade.
Documentation
//! Architecture save (JSON + SHA-256).
//!
//! Writes the `Architecture` descriptor to `arch.json` and the
//! SHA-256 of the topology to `arch.json.sha256`. The pair is
//! loaded by `load.rs`; the SHA-256 is used to detect tampering.
//!
// Write a `ModelArch` to disk as pretty-printed JSON.
//
// Pretty-printing (vs. compact) costs a few extra KB but makes the
// file diffable in code review and easy to hand-edit for one-off
// arch experiments.

use std::fs::File;
use std::io::BufWriter;
use std::path::Path;

use crate::{Error, Result};

use super::arch::ModelArch;

/// Write `arch` to `path` as pretty-printed JSON. Creates the file
/// (truncating if it already exists). The parent directory must
/// already exist.
pub fn save_arch(path: &Path, arch: &ModelArch) -> Result<()> {
    let f = File::create(path)
        .map_err(|e| Error::backend(format!("arch save: create {}: {e}", path.display())))?;
    let mut w = BufWriter::new(f);
    serde_json::to_writer_pretty(&mut w, arch)
        .map_err(|e| Error::backend(format!("arch save: serialize {}: {e}", path.display())))?;
    std::io::Write::flush(&mut w)
        .map_err(|e| Error::backend(format!("arch save: flush {}: {e}", path.display())))?;
    Ok(())
}