abaddon 0.2.0-rc.2

LLM inference engine - The Destroyer renders judgment
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
//! HCT V3 Safetensors Loader
//!
//! Loads model weights from HCT V3 format safetensors files.
//! HCT V3 uses spectral compression (DCT) with zstd, achieving ~10x compression
//! on neural network weights while preserving model quality.
//!
//! ## Format
//!
//! HCT V3 files are standard safetensors with:
//! - `dtype: "hct_v3"` for compressed tensors
//! - Data is zstd-compressed spectral fragments
//!
//! ## Usage
//!
//! ```ignore
//! use abaddon::hct_v3::HctV3Loader;
//!
//! let loader = HctV3Loader::open("/path/to/model.safetensors")?;
//! let tensor = loader.load_tensor("model.layers.0.self_attn.q_proj.weight", &device)?;
//! ```

use std::collections::HashMap;
use std::fs::File;
use std::io::{BufReader, Read, Seek, SeekFrom};
use std::path::{Path, PathBuf};
use std::sync::Arc;

use candle_core::{Device, Tensor};
use haagenti::compressive::CompressiveSpectralDecoder;
use haagenti::holotensor::HoloFragment;
use parking_lot::RwLock;

/// Error type for HCT V3 loading.
#[derive(Debug, thiserror::Error)]
pub enum HctV3Error {
    #[error("IO error: {0}")]
    Io(#[from] std::io::Error),

    #[error("JSON parse error: {0}")]
    Json(#[from] serde_json::Error),

    #[error("Tensor not found: {name}")]
    TensorNotFound { name: String },

    #[error("Invalid HCT V3 format: {message}")]
    InvalidFormat { message: String },

    #[error("Decompression error: {message}")]
    Decompression { message: String },

    #[error("Tensor creation error: {0}")]
    Tensor(#[from] candle_core::Error),
}

/// Tensor metadata from safetensors header.
#[derive(Debug, Clone)]
pub struct TensorInfo {
    /// Tensor name.
    pub name: String,
    /// Original shape.
    pub shape: Vec<usize>,
    /// Data type (should be "hct_v3" for compressed).
    pub dtype: String,
    /// Start offset in data section.
    pub start_offset: usize,
    /// End offset in data section.
    pub end_offset: usize,
}

impl TensorInfo {
    /// Returns true if this tensor is HCT V3 compressed.
    pub fn is_hct_v3(&self) -> bool {
        self.dtype == "hct_v3"
    }

    /// Returns the compressed size in bytes.
    pub fn compressed_size(&self) -> usize {
        self.end_offset - self.start_offset
    }

    /// Returns the original size in bytes (assuming f32).
    pub fn original_size(&self) -> usize {
        self.shape.iter().product::<usize>() * 4
    }

    /// Returns the compression ratio.
    pub fn compression_ratio(&self) -> f64 {
        self.original_size() as f64 / self.compressed_size() as f64
    }
}

/// HCT V3 Safetensors loader.
///
/// Provides efficient loading and decompression of HCT V3 compressed tensors.
pub struct HctV3Loader {
    /// Path to the safetensors file.
    path: PathBuf,
    /// Header size in bytes.
    header_size: usize,
    /// Tensor metadata.
    tensors: HashMap<String, TensorInfo>,
    /// Cached decompressed tensors.
    cache: Arc<RwLock<HashMap<String, Tensor>>>,
}

impl HctV3Loader {
    /// Opens an HCT V3 safetensors file.
    ///
    /// # Errors
    ///
    /// Returns an error if the file cannot be opened or parsed.
    pub fn open(path: impl AsRef<Path>) -> Result<Self, HctV3Error> {
        let path = path.as_ref().to_path_buf();
        let mut file = BufReader::new(File::open(&path)?);

        // Read header size (8 bytes, little-endian)
        let mut header_size_bytes = [0u8; 8];
        file.read_exact(&mut header_size_bytes)?;
        let header_size = u64::from_le_bytes(header_size_bytes) as usize;

        // Read and parse header JSON
        let mut header_json = vec![0u8; header_size];
        file.read_exact(&mut header_json)?;
        let header_str = String::from_utf8_lossy(&header_json);
        let header: HashMap<String, serde_json::Value> = serde_json::from_str(&header_str)?;

        // Parse tensor metadata
        let mut tensors = HashMap::new();
        for (name, info) in header {
            if name == "__metadata__" {
                continue;
            }

            let dtype = info
                .get("dtype")
                .and_then(|v| v.as_str())
                .unwrap_or("unknown")
                .to_string();

            let shape: Vec<usize> = info
                .get("shape")
                .and_then(|v| v.as_array())
                .map(|arr| {
                    arr.iter()
                        .filter_map(|v| v.as_u64().map(|n| n as usize))
                        .collect()
                })
                .unwrap_or_default();

            let data_offsets = info.get("data_offsets").and_then(|v| v.as_array());
            let (start_offset, end_offset) = match data_offsets {
                Some(arr) if arr.len() >= 2 => {
                    let start = arr[0].as_u64().unwrap_or(0) as usize;
                    let end = arr[1].as_u64().unwrap_or(0) as usize;
                    (start, end)
                }
                _ => (0, 0),
            };

            tensors.insert(
                name.clone(),
                TensorInfo {
                    name,
                    shape,
                    dtype,
                    start_offset,
                    end_offset,
                },
            );
        }

        Ok(Self {
            path,
            header_size,
            tensors,
            cache: Arc::new(RwLock::new(HashMap::new())),
        })
    }

    /// Returns the path to the safetensors file.
    pub fn path(&self) -> &Path {
        &self.path
    }

    /// Returns the number of tensors in the file.
    pub fn num_tensors(&self) -> usize {
        self.tensors.len()
    }

    /// Returns all tensor names.
    pub fn tensor_names(&self) -> Vec<&str> {
        self.tensors.keys().map(|s| s.as_str()).collect()
    }

    /// Returns tensor info by name.
    pub fn tensor_info(&self, name: &str) -> Option<&TensorInfo> {
        self.tensors.get(name)
    }

    /// Returns iterator over all tensor info.
    pub fn tensors(&self) -> impl Iterator<Item = &TensorInfo> {
        self.tensors.values()
    }

    /// Returns total compressed size in bytes.
    pub fn total_compressed_size(&self) -> usize {
        self.tensors.values().map(|t| t.compressed_size()).sum()
    }

    /// Returns total original size in bytes.
    pub fn total_original_size(&self) -> usize {
        self.tensors.values().map(|t| t.original_size()).sum()
    }

    /// Returns average compression ratio.
    pub fn compression_ratio(&self) -> f64 {
        self.total_original_size() as f64 / self.total_compressed_size() as f64
    }

    /// Loads and decompresses a single tensor.
    ///
    /// Uses caching to avoid repeated decompression.
    ///
    /// # Errors
    ///
    /// Returns an error if the tensor is not found or decompression fails.
    pub fn load_tensor(&self, name: &str, device: &Device) -> Result<Tensor, HctV3Error> {
        // Check cache first
        {
            let cache = self.cache.read();
            if let Some(tensor) = cache.get(name) {
                return Ok(tensor.clone());
            }
        }

        // Get tensor info
        let info = self.tensors.get(name).ok_or_else(|| HctV3Error::TensorNotFound {
            name: name.to_string(),
        })?;

        // Decompress
        let data = self.decompress_tensor(info)?;

        // Create tensor
        let tensor = Tensor::from_vec(data, info.shape.as_slice(), device)?;

        // Cache the result
        {
            let mut cache = self.cache.write();
            cache.insert(name.to_string(), tensor.clone());
        }

        Ok(tensor)
    }

    /// Loads a tensor without caching (for memory-constrained scenarios).
    pub fn load_tensor_uncached(&self, name: &str, device: &Device) -> Result<Tensor, HctV3Error> {
        let info = self.tensors.get(name).ok_or_else(|| HctV3Error::TensorNotFound {
            name: name.to_string(),
        })?;

        let data = self.decompress_tensor(info)?;
        Ok(Tensor::from_vec(data, info.shape.as_slice(), device)?)
    }

    /// Decompresses a tensor to f32 data.
    fn decompress_tensor(&self, info: &TensorInfo) -> Result<Vec<f32>, HctV3Error> {
        if !info.is_hct_v3() {
            return Err(HctV3Error::InvalidFormat {
                message: format!("Expected dtype 'hct_v3', got '{}'", info.dtype),
            });
        }

        // Read compressed data from file
        let mut file = BufReader::new(File::open(&self.path)?);
        let data_start = 8 + self.header_size as u64;
        file.seek(SeekFrom::Start(data_start + info.start_offset as u64))?;

        let mut zstd_data = vec![0u8; info.compressed_size()];
        file.read_exact(&mut zstd_data)?;

        // Step 1: Decompress zstd (using standard zstd crate, matches turbo pipeline)
        let fragment_data = zstd::decode_all(&zstd_data[..]).map_err(|e| HctV3Error::Decompression {
            message: format!("zstd decompression failed: {}", e),
        })?;

        // Step 2: Parse fragments
        // Format: [num_fragments: u16] then per fragment: [index: u16][flags: u16][checksum: u64][data_len: u32][data]
        if fragment_data.len() < 2 {
            return Err(HctV3Error::InvalidFormat {
                message: "Fragment data too short".to_string(),
            });
        }

        let num_fragments = u16::from_le_bytes([fragment_data[0], fragment_data[1]]) as usize;
        let mut offset = 2;
        let mut fragments = Vec::with_capacity(num_fragments);

        for _ in 0..num_fragments {
            if offset + 16 > fragment_data.len() {
                return Err(HctV3Error::InvalidFormat {
                    message: "Truncated fragment header".to_string(),
                });
            }

            let index = u16::from_le_bytes([fragment_data[offset], fragment_data[offset + 1]]);
            offset += 2;
            let flags = u16::from_le_bytes([fragment_data[offset], fragment_data[offset + 1]]);
            offset += 2;
            let checksum = u64::from_le_bytes([
                fragment_data[offset],
                fragment_data[offset + 1],
                fragment_data[offset + 2],
                fragment_data[offset + 3],
                fragment_data[offset + 4],
                fragment_data[offset + 5],
                fragment_data[offset + 6],
                fragment_data[offset + 7],
            ]);
            offset += 8;
            let data_len = u32::from_le_bytes([
                fragment_data[offset],
                fragment_data[offset + 1],
                fragment_data[offset + 2],
                fragment_data[offset + 3],
            ]) as usize;
            offset += 4;

            if offset + data_len > fragment_data.len() {
                return Err(HctV3Error::InvalidFormat {
                    message: "Truncated fragment data".to_string(),
                });
            }

            let frag_data = fragment_data[offset..offset + data_len].to_vec();
            offset += data_len;

            fragments.push(HoloFragment {
                index,
                flags,
                checksum,
                data: frag_data,
            });
        }

        // Step 3: Decode using CompressiveSpectralDecoder
        let mut decoder = CompressiveSpectralDecoder::new();

        if fragments.is_empty() {
            return Err(HctV3Error::InvalidFormat {
                message: "No fragments to decode".to_string(),
            });
        }

        // Add essentials fragment (index 0)
        decoder.add_essentials(&fragments[0]).map_err(|e| HctV3Error::Decompression {
            message: format!("Failed to add essentials: {}", e),
        })?;

        // Add detail fragments
        for frag in &fragments[1..] {
            decoder.add_detail(frag).map_err(|e| HctV3Error::Decompression {
                message: format!("Failed to add detail fragment: {}", e),
            })?;
        }

        // Reconstruct
        let decoded = decoder.reconstruct().map_err(|e| HctV3Error::Decompression {
            message: format!("Reconstruction failed: {}", e),
        })?;

        // Verify size
        let expected_size: usize = info.shape.iter().product();
        if decoded.len() != expected_size {
            return Err(HctV3Error::InvalidFormat {
                message: format!(
                    "Size mismatch: decoded {} elements, expected {}",
                    decoded.len(),
                    expected_size
                ),
            });
        }

        Ok(decoded)
    }

    /// Loads all tensors into a map.
    ///
    /// This is useful for building a complete model from compressed weights.
    pub fn load_all(&self, device: &Device) -> Result<HashMap<String, Tensor>, HctV3Error> {
        let mut result = HashMap::new();
        for name in self.tensor_names() {
            let tensor = self.load_tensor(name, device)?;
            result.insert(name.to_string(), tensor);
        }
        Ok(result)
    }

    /// Loads tensors matching a prefix.
    ///
    /// Useful for loading a single layer at a time.
    pub fn load_prefix(
        &self,
        prefix: &str,
        device: &Device,
    ) -> Result<HashMap<String, Tensor>, HctV3Error> {
        let mut result = HashMap::new();
        for (name, _) in &self.tensors {
            if name.starts_with(prefix) {
                let tensor = self.load_tensor(name, device)?;
                result.insert(name.clone(), tensor);
            }
        }
        Ok(result)
    }

    /// Clears the tensor cache.
    pub fn clear_cache(&self) {
        self.cache.write().clear();
    }

    /// Returns cache statistics.
    pub fn cache_stats(&self) -> (usize, usize) {
        let cache = self.cache.read();
        let cached_count = cache.len();
        let cached_bytes: usize = cache
            .values()
            .map(|t| t.elem_count() * t.dtype().size_in_bytes())
            .sum();
        (cached_count, cached_bytes)
    }
}

impl std::fmt::Debug for HctV3Loader {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("HctV3Loader")
            .field("path", &self.path)
            .field("num_tensors", &self.tensors.len())
            .field("compression_ratio", &format!("{:.1}x", self.compression_ratio()))
            .finish()
    }
}

/// Convenience function to load a single tensor from an HCT V3 file.
pub fn load_hct_v3_tensor(
    path: impl AsRef<Path>,
    tensor_name: &str,
    device: &Device,
) -> Result<Tensor, HctV3Error> {
    let loader = HctV3Loader::open(path)?;
    loader.load_tensor(tensor_name, device)
}

/// Convenience function to load all tensors from an HCT V3 file.
pub fn load_hct_v3_all(
    path: impl AsRef<Path>,
    device: &Device,
) -> Result<HashMap<String, Tensor>, HctV3Error> {
    let loader = HctV3Loader::open(path)?;
    loader.load_all(device)
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tensor_info_compression_ratio() {
        let info = TensorInfo {
            name: "test".to_string(),
            shape: vec![1024, 1024],
            dtype: "hct_v3".to_string(),
            start_offset: 0,
            end_offset: 400_000, // ~10x compression
        };

        assert!(info.is_hct_v3());
        assert_eq!(info.original_size(), 1024 * 1024 * 4);
        assert_eq!(info.compressed_size(), 400_000);
        assert!((info.compression_ratio() - 10.49).abs() < 0.1);
    }
}