//! `model` commands that read a GGUF file: metadata, tensor index and the derived identities. use std::path::Path; use strata_control::{BoxError, derived_iq3, derived_mixed, gguf, model}; /// Run the command in `args` if this group owns it; `Ok(false)` when it does not. pub async fn run(args: &[String]) -> Result { let handled: Result<(), BoxError> = match args { [model, action, path] if model == "model" && action == "inspect-metadata" => { // Every key/value as one JSON object; non-finite floats are the strings "nan", "inf", "-inf". let entries = gguf::metadata_entries(Path::new(path))?; println!("{}", strata_control::gguf_json::metadata_json(entries)); Ok(()) } [model, action, path] if model == "model" && action == "inspect" => { let summary = gguf::inspect(Path::new(path))?; println!("GGUF v3: {path}"); println!("size: {} bytes", summary.file_size); println!( "tensors: {}; metadata: {}", summary.tensors, summary.metadata ); println!("data starts at byte {}", summary.data_start); if let Some(architecture) = summary.architecture { println!("architecture: {architecture}"); } if let (Some(no), Some(count)) = (summary.split_no, summary.split_count) { println!("split: {}/{}", no + 1, count); } println!("tensor types: {:?}", summary.types); Ok(()) } [model, action, path] if model == "model" && action == "index" => { print_tensor_index(Path::new(path), false) } [model, action, path, hash_flag] if model == "model" && action == "index" && hash_flag == "--hash" => { print_tensor_index(Path::new(path), true) } [model, action, path] if model == "model" && action == "derive-iq3-identity" => { let identity = derived_iq3::fingerprint(Path::new(path))?; println!( "{}", serde_json::to_string_pretty(&serde_json::json!({ "derivation": "strata-next/iq3_s-to-f32-le-v1", "gguf_sha256": hex::encode(identity.gguf_sha256), "derived_f32_sha256": hex::encode(identity.derived_sha256), "derived_f32_bytes": identity.derived_bytes, "tensors": identity.tensors.iter().map(|tensor| serde_json::json!({ "name": tensor.name, "derived_offset": tensor.derived_offset, "derived_bytes": tensor.derived_bytes, "iq3_sha256": hex::encode(tensor.iq3_sha256), "f32_sha256": hex::encode(tensor.f32_sha256), })).collect::>(), }))? ); Ok(()) } [model, action, path] if model == "model" && action == "derive-mixed-identity" => { let identity = derived_mixed::fingerprint(Path::new(path))?; println!( "{}", serde_json::to_string_pretty(&serde_json::json!({ "derivation": "strata-next/gguf-mixed-to-f32-le-v1", "gguf_sha256": hex::encode(identity.gguf_sha256), "derived_f32_sha256": hex::encode(identity.derived_sha256), "derived_f32_bytes": identity.derived_bytes, "tensors": identity.tensors.iter().map(|tensor| serde_json::json!({ "name": tensor.name, "source_kind": tensor.source_kind, "source_sha256": hex::encode(tensor.source_sha256), "derived_offset": tensor.derived_offset, "derived_bytes": tensor.derived_bytes, "f32_sha256": hex::encode(tensor.f32_sha256), })).collect::>(), }))? ); Ok(()) } [model, action, manifest_spec, source_flag, source_dir] if model == "model" && action == "derive-sharded-identity" && source_flag == "--source-dir" => { let manifest = model::Manifest::from_spec(manifest_spec).await?; let identity = derived_mixed::fingerprint_shards(&manifest, Path::new(source_dir))?; println!( "{}", serde_json::to_string_pretty(&serde_json::json!({ "derivation": "strata-next/gguf-shards-to-f32-le-v1", "derived_f32_sha256": hex::encode(identity.derived_sha256), "derived_f32_bytes": identity.derived_bytes, "shards": identity.shards.iter().map(|shard| serde_json::json!({ "name": shard.name, "gguf_sha256": hex::encode(shard.gguf_sha256), "gguf_bytes": shard.gguf_bytes, "tensor_count": shard.tensor_count, })).collect::>(), "tensors": identity.tensors.iter().map(|tensor| serde_json::json!({ "name": tensor.name, "source_kind": tensor.source_kind, "source_sha256": hex::encode(tensor.source_sha256), "derived_offset": tensor.derived_offset, "derived_bytes": tensor.derived_bytes, "f32_sha256": hex::encode(tensor.f32_sha256), })).collect::>(), }))? ); Ok(()) } _ => return Ok(false), }; handled.map(|()| true) } fn print_tensor_index(path: &Path, hash: bool) -> Result<(), BoxError> { let index = gguf::tensor_index(path)?; for (number, tensor) in index.tensors.iter().enumerate() { let mut row = serde_json::json!({ "name": tensor.name, "dimensions": tensor.dimensions, "type": tensor.kind, "offset": index.summary.data_start + tensor.offset, "bytes": tensor.bytes, }); if hash { row["sha256"] = hex::encode(gguf::tensor_sha256(path, &index, number)?).into(); } println!("{row}"); } Ok(()) }