Desktop foundation / core (ubuntu-latest) (push) Failing after 28s
Desktop foundation / core (macos-latest) (push) Canceled after 0s
Desktop foundation / core (windows-latest) (push) Canceled after 0s
Linux / verify (push) Canceled after 0s
Linux / keyless-cuda (push) Canceled after 0s
Linux / static-cuda-package (push) Canceled after 0s
Linux / hosted-cuda-package (push) Canceled after 0s
Linux / linux-static-cli (push) Canceled after 0s
Linux / linux-cli-container (push) Canceled after 0s
Linux / release-cli (push) Canceled after 0s
187 lines
6.7 KiB
Rust
187 lines
6.7 KiB
Rust
//! Experimental identity for a virtual F32 stream decoded from one all-IQ3_S
|
|
//! GGUF. The bytes are never materialized on disk. This is a preparation aid,
|
|
//! not an approval to serve a lossy KAR archive.
|
|
|
|
use std::{
|
|
fs::File,
|
|
io::{Read, Seek, SeekFrom},
|
|
path::Path,
|
|
};
|
|
|
|
use sha2::{Digest, Sha256};
|
|
|
|
use crate::{gguf, iq3_s};
|
|
|
|
const BLOCKS_PER_BATCH: usize = 256;
|
|
|
|
#[derive(Debug)]
|
|
pub struct TensorIdentity {
|
|
pub name: String,
|
|
pub derived_offset: u64,
|
|
pub derived_bytes: u64,
|
|
pub iq3_sha256: [u8; 32],
|
|
pub f32_sha256: [u8; 32],
|
|
}
|
|
|
|
#[derive(Debug)]
|
|
pub struct Identity {
|
|
pub gguf_sha256: [u8; 32],
|
|
pub derived_sha256: [u8; 32],
|
|
pub derived_bytes: u64,
|
|
pub tensors: Vec<TensorIdentity>,
|
|
}
|
|
|
|
/// The virtual source is the concatenation, in GGUF tensor-directory order,
|
|
/// of each tensor's dequantized IEEE-754 little-endian F32 values. No GGUF
|
|
/// header, alignment padding, tensor name, or framing appears in that stream.
|
|
/// Tensor offsets and digests refer to this stream, never to IQ3_S file bytes.
|
|
pub fn fingerprint(path: &Path) -> Result<Identity, String> {
|
|
let index = gguf::tensor_index(path).map_err(|error| error.to_string())?;
|
|
if index.tensors.is_empty() || index.tensors.iter().any(|tensor| tensor.kind != 21) {
|
|
return Err("IQ3 derivation currently requires a nonempty all-IQ3_S GGUF".into());
|
|
}
|
|
let gguf_sha256 = hash_file(path)?;
|
|
let mut source = File::open(path).map_err(|error| error.to_string())?;
|
|
let mut derived = Sha256::new();
|
|
let mut derived_bytes = 0u64;
|
|
let mut tensors = Vec::with_capacity(index.tensors.len());
|
|
let mut block_buffer = vec![0u8; BLOCKS_PER_BATCH * iq3_s::BLOCK_BYTES];
|
|
for tensor in &index.tensors {
|
|
if !tensor.bytes.is_multiple_of(iq3_s::BLOCK_BYTES as u64) {
|
|
return Err(format!(
|
|
"{}: IQ3_S tensor is not block-aligned",
|
|
tensor.name
|
|
));
|
|
}
|
|
let absolute = index
|
|
.summary
|
|
.data_start
|
|
.checked_add(tensor.offset)
|
|
.ok_or("GGUF tensor offset overflow")?;
|
|
source
|
|
.seek(SeekFrom::Start(absolute))
|
|
.map_err(|error| error.to_string())?;
|
|
let offset = derived_bytes;
|
|
let mut iq3_hash = Sha256::new();
|
|
let mut f32_hash = Sha256::new();
|
|
let mut remaining = tensor.bytes;
|
|
while remaining != 0 {
|
|
let count = remaining.min(block_buffer.len() as u64) as usize;
|
|
source
|
|
.read_exact(&mut block_buffer[..count])
|
|
.map_err(|error| error.to_string())?;
|
|
iq3_hash.update(&block_buffer[..count]);
|
|
for block in block_buffer[..count]
|
|
.as_chunks::<{ iq3_s::BLOCK_BYTES }>()
|
|
.0
|
|
{
|
|
let values = iq3_s::decode_block(block)?;
|
|
for value in values {
|
|
let bytes = value.to_le_bytes();
|
|
f32_hash.update(bytes);
|
|
derived.update(bytes);
|
|
}
|
|
}
|
|
let expanded = (count / iq3_s::BLOCK_BYTES)
|
|
.checked_mul(iq3_s::BLOCK_ELEMENTS * 4)
|
|
.ok_or("derived IQ3 byte count overflow")?;
|
|
derived_bytes = derived_bytes
|
|
.checked_add(expanded as u64)
|
|
.ok_or("derived IQ3 byte count overflow")?;
|
|
remaining -= count as u64;
|
|
}
|
|
tensors.push(TensorIdentity {
|
|
name: tensor.name.clone(),
|
|
derived_offset: offset,
|
|
derived_bytes: derived_bytes - offset,
|
|
iq3_sha256: iq3_hash.finalize().into(),
|
|
f32_sha256: f32_hash.finalize().into(),
|
|
});
|
|
}
|
|
if source.metadata().map_err(|error| error.to_string())?.len() != index.summary.file_size
|
|
|| hash_file(path)? != gguf_sha256
|
|
{
|
|
return Err("GGUF source changed during IQ3 derivation".into());
|
|
}
|
|
Ok(Identity {
|
|
gguf_sha256,
|
|
derived_sha256: derived.finalize().into(),
|
|
derived_bytes,
|
|
tensors,
|
|
})
|
|
}
|
|
|
|
fn hash_file(path: &Path) -> Result<[u8; 32], String> {
|
|
let mut source = File::open(path).map_err(|error| error.to_string())?;
|
|
let mut digest = Sha256::new();
|
|
let mut buffer = [0u8; 1024 * 1024];
|
|
loop {
|
|
let count = source
|
|
.read(&mut buffer)
|
|
.map_err(|error| error.to_string())?;
|
|
if count == 0 {
|
|
break;
|
|
}
|
|
digest.update(&buffer[..count]);
|
|
}
|
|
Ok(digest.finalize().into())
|
|
}
|
|
|
|
#[cfg(test)]
|
|
mod tests {
|
|
use super::*;
|
|
use std::fs;
|
|
|
|
fn string(out: &mut Vec<u8>, value: &str) {
|
|
out.extend_from_slice(&(value.len() as u64).to_le_bytes());
|
|
out.extend_from_slice(value.as_bytes());
|
|
}
|
|
|
|
#[test]
|
|
fn virtual_f32_identity_uses_tensor_directory_order_and_rejects_bad_source() {
|
|
let mut gguf = b"GGUF".to_vec();
|
|
gguf.extend_from_slice(&3u32.to_le_bytes());
|
|
gguf.extend_from_slice(&2u64.to_le_bytes());
|
|
gguf.extend_from_slice(&0u64.to_le_bytes());
|
|
for (name, offset) in [("second", 128u64), ("first", 0)] {
|
|
string(&mut gguf, name);
|
|
gguf.extend_from_slice(&1u32.to_le_bytes());
|
|
gguf.extend_from_slice(&256u64.to_le_bytes());
|
|
gguf.extend_from_slice(&21u32.to_le_bytes());
|
|
gguf.extend_from_slice(&offset.to_le_bytes());
|
|
}
|
|
gguf.resize(gguf.len().div_ceil(32) * 32, 0);
|
|
let data_start = gguf.len();
|
|
let mut first = [0u8; iq3_s::BLOCK_BYTES];
|
|
first[..2].copy_from_slice(&0x3c00u16.to_le_bytes());
|
|
let mut second = first;
|
|
second[..2].copy_from_slice(&0x4000u16.to_le_bytes());
|
|
gguf.extend_from_slice(&first);
|
|
gguf.resize(data_start + 128, 0);
|
|
gguf.extend_from_slice(&second);
|
|
let path =
|
|
std::env::temp_dir().join(format!("strata-iq3-identity-{}.gguf", std::process::id()));
|
|
fs::write(&path, &gguf).unwrap();
|
|
let identity = fingerprint(&path).unwrap();
|
|
assert_eq!(
|
|
identity.gguf_sha256.as_slice(),
|
|
Sha256::digest(&gguf).as_slice()
|
|
);
|
|
assert_eq!(identity.derived_bytes, 2048);
|
|
assert_eq!(identity.tensors[0].name, "second");
|
|
assert_eq!(identity.tensors[1].name, "first");
|
|
assert_eq!(identity.tensors[1].derived_offset, 1024);
|
|
let mut expected = Sha256::new();
|
|
expected.update([2.0f32.to_le_bytes(); 256].concat());
|
|
expected.update([1.0f32.to_le_bytes(); 256].concat());
|
|
assert_eq!(
|
|
identity.derived_sha256.as_slice(),
|
|
expected.finalize().as_slice()
|
|
);
|
|
gguf[0] = b'X';
|
|
fs::write(&path, gguf).unwrap();
|
|
assert!(fingerprint(&path).is_err());
|
|
fs::remove_file(path).unwrap();
|
|
}
|
|
}
|