Skip to content

Commit 11fffd1

Browse files
committed
feat: CompressedTensor::read_from + read_bgz7_file
Deserializer for bgz7 files — reads back the per-tensor Base17 rows written by stream_index_gguf/stream_index_gguf_bf16. Required by causal_diff to load and compare indexed models.
1 parent 3769a31 commit 11fffd1

1 file changed

Lines changed: 69 additions & 3 deletions

File tree

src/hpc/gguf_indexer.rs

Lines changed: 69 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -738,11 +738,77 @@ impl CompressedTensor {
738738
}
739739
Ok(())
740740
}
741+
742+
/// Deserialize from bytes: [name_len:u32][name][layer_type:u8][n_rows:u32][n_cols:u32][base17 × n_rows]
743+
pub fn read_from<R: Read>(r: &mut R) -> Result<Self, String> {
744+
let mut u32_buf = [0u8; 4];
745+
746+
r.read_exact(&mut u32_buf).map_err(|e| e.to_string())?;
747+
let name_len = u32::from_le_bytes(u32_buf) as usize;
748+
749+
let mut name_bytes = vec![0u8; name_len];
750+
r.read_exact(&mut name_bytes).map_err(|e| e.to_string())?;
751+
let name = String::from_utf8(name_bytes).map_err(|e| e.to_string())?;
752+
753+
let mut lt_buf = [0u8; 1];
754+
r.read_exact(&mut lt_buf).map_err(|e| e.to_string())?;
755+
let layer_type = match lt_buf[0] {
756+
0 => LayerType::Attention,
757+
1 => LayerType::FeedForward,
758+
2 => LayerType::Conv2D,
759+
3 => LayerType::Norm,
760+
4 => LayerType::Embedding,
761+
_ => LayerType::Skip,
762+
};
763+
764+
r.read_exact(&mut u32_buf).map_err(|e| e.to_string())?;
765+
let n_rows = u32::from_le_bytes(u32_buf) as usize;
766+
767+
r.read_exact(&mut u32_buf).map_err(|e| e.to_string())?;
768+
let n_cols = u32::from_le_bytes(u32_buf) as usize;
769+
770+
let mut rows = Vec::with_capacity(n_rows);
771+
let mut b17_buf = [0u8; Base17::BYTE_SIZE];
772+
for _ in 0..n_rows {
773+
r.read_exact(&mut b17_buf).map_err(|e| e.to_string())?;
774+
rows.push(Base17::from_bytes(&b17_buf));
775+
}
776+
777+
Ok(CompressedTensor {
778+
name,
779+
layer_type,
780+
original_shape: vec![], // not stored in bgz7
781+
n_rows,
782+
n_cols,
783+
rows,
784+
})
785+
}
741786
}
742787

743-
// ============================================================================
744-
// Reshape helpers
745-
// ============================================================================
788+
/// Read all tensors from a bgz7 file.
789+
///
790+
/// Returns Vec of (name, layer_type, rows) tuples.
791+
pub fn read_bgz7_file(path: &str) -> Result<Vec<CompressedTensor>, String> {
792+
let file = std::fs::File::open(path).map_err(|e| format!("{}: {}", path, e))?;
793+
let mut reader = std::io::BufReader::new(file);
794+
795+
let mut magic = [0u8; 4];
796+
reader.read_exact(&mut magic).map_err(|e| e.to_string())?;
797+
if &magic != b"BGZ7" {
798+
return Err(format!("bad magic: {:?}", magic));
799+
}
800+
801+
let mut u32_buf = [0u8; 4];
802+
reader.read_exact(&mut u32_buf).map_err(|e| e.to_string())?;
803+
let n_tensors = u32::from_le_bytes(u32_buf) as usize;
804+
805+
let mut tensors = Vec::with_capacity(n_tensors);
806+
for _ in 0..n_tensors {
807+
tensors.push(CompressedTensor::read_from(&mut reader)?);
808+
}
809+
810+
Ok(tensors)
811+
}
746812

747813
/// Reshape a flat f32 tensor into rows × cols based on layer type.
748814
///

0 commit comments

Comments
 (0)