use std::io::{self, Write};
use std::path::Path;
#[allow(clippy::too_many_arguments)]
pub fn write_model_file(
output_path: &Path,
name: &str,
card: &str,
config: &str,
program: &str,
program_format: &str,
graph: Option<&str>,
tensors_toml: &str,
vocab: &str,
eval: &str,
weights: &[u8],
) -> io::Result<()> {
let mut f = std::fs::File::create(output_path)?;
writeln!(f, "[cyb]")?;
writeln!(f, "types = [\"model\"]")?;
writeln!(f, "name = \"{name}\"")?;
writeln!(f, "format_version = 2")?;
writeln!(f)?;
for (section, format) in [
("card", "md"),
("config", "toml"),
("program", program_format),
] {
writeln!(f, "files")?;
writeln!(f, "name = \"{section}\"")?;
writeln!(f, "format = \"{format}\"")?;
writeln!(f)?;
}
if graph.is_some() {
writeln!(f, "files")?;
writeln!(f, "name = \"graph\"")?;
writeln!(f, "format = \"hex\"")?;
writeln!(f)?;
}
for (section, format) in [("tensors", "toml"), ("vocab", "toml"), ("eval", "toml")] {
writeln!(f, "files")?;
writeln!(f, "name = \"{section}\"")?;
writeln!(f, "format = \"{format}\"")?;
writeln!(f)?;
}
writeln!(f, "files")?;
writeln!(f, "name = \"weights\"")?;
writeln!(f, "format = \"tensors\"")?;
writeln!(f, "size = {}", weights.len())?;
for (marker, body) in [
("~~~card", card),
("~~~config", config),
("~~~program", program),
] {
writeln!(f, "{marker}")?;
f.write_all(body.as_bytes())?;
if !body.ends_with('\n') {
writeln!(f)?;
}
}
if let Some(hex) = graph {
writeln!(f, "~~~graph")?;
writeln!(f, "{hex}")?;
}
for (marker, body) in [("~~~tensors", tensors_toml), ("~~~vocab", vocab), ("~~~eval", eval)] {
writeln!(f, "{marker}")?;
f.write_all(body.as_bytes())?;
if !body.ends_with('\n') {
writeln!(f)?;
}
}
writeln!(f, "~~~weights")?;
f.write_all(weights)?;
Ok(())
}