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
//! RAM cache for warm tensors using pinned memory.
//!
//! Stores layer weights in CPU memory for fast upload to GPU.
//! Uses CUDA pinned (page-locked) memory when available for ~2x faster transfers.

use std::collections::HashMap;

use super::error::TieredError;
use super::lru::LruTracker;
use super::stats::TieredStats;
use std::sync::Arc;

/// CPU-side layer weights stored in RAM.
///
/// Weights are stored as contiguous f16 buffers for efficient GPU upload.
/// Can use pinned (page-locked) memory for faster PCIe transfers.
#[derive(Debug)]
pub struct CpuLayerWeights {
    /// Weight data as contiguous bytes (f16 format).
    pub data: Vec<u8>,

    /// Offsets and sizes for each weight tensor within data.
    pub layout: LayerLayout,

    /// Total size in bytes.
    pub size_bytes: usize,

    /// Whether this buffer uses pinned memory.
    pub is_pinned: bool,
}

/// Layout information for layer weights within a contiguous buffer.
#[derive(Debug, Clone)]
pub struct LayerLayout {
    /// Q projection offset and shape.
    pub q_proj: TensorLayout,
    /// K projection offset and shape.
    pub k_proj: TensorLayout,
    /// V projection offset and shape.
    pub v_proj: TensorLayout,
    /// O projection offset and shape.
    pub o_proj: TensorLayout,
    /// Gate projection offset and shape.
    pub gate_proj: TensorLayout,
    /// Up projection offset and shape.
    pub up_proj: TensorLayout,
    /// Down projection offset and shape.
    pub down_proj: TensorLayout,
    /// Input layernorm offset and shape.
    pub input_norm: TensorLayout,
    /// Post-attention layernorm offset and shape.
    pub post_attn_norm: TensorLayout,
}

/// Layout for a single tensor within the buffer.
#[derive(Debug, Clone, Copy)]
pub struct TensorLayout {
    /// Byte offset from start of buffer.
    pub offset: usize,
    /// Size in bytes.
    pub size: usize,
    /// Shape dimensions.
    pub shape: [usize; 2],
}

impl TensorLayout {
    /// Create a new tensor layout.
    pub fn new(offset: usize, shape: [usize; 2]) -> Self {
        // Assuming f16 (2 bytes per element)
        let size = shape[0] * shape[1] * 2;
        Self {
            offset,
            size,
            shape,
        }
    }

    /// Create layout for a 1D tensor (e.g., layernorm).
    pub fn new_1d(offset: usize, size: usize) -> Self {
        Self {
            offset,
            size: size * 2, // f16
            shape: [size, 1],
        }
    }
}

impl CpuLayerWeights {
    /// Get a slice of the weight data for a specific tensor.
    pub fn tensor_data(&self, layout: &TensorLayout) -> &[u8] {
        &self.data[layout.offset..layout.offset + layout.size]
    }

    /// Get Q projection data.
    pub fn q_proj_data(&self) -> &[u8] {
        self.tensor_data(&self.layout.q_proj)
    }

    /// Get K projection data.
    pub fn k_proj_data(&self) -> &[u8] {
        self.tensor_data(&self.layout.k_proj)
    }

    /// Get V projection data.
    pub fn v_proj_data(&self) -> &[u8] {
        self.tensor_data(&self.layout.v_proj)
    }

    /// Get O projection data.
    pub fn o_proj_data(&self) -> &[u8] {
        self.tensor_data(&self.layout.o_proj)
    }

    /// Get gate projection data.
    pub fn gate_proj_data(&self) -> &[u8] {
        self.tensor_data(&self.layout.gate_proj)
    }

    /// Get up projection data.
    pub fn up_proj_data(&self) -> &[u8] {
        self.tensor_data(&self.layout.up_proj)
    }

    /// Get down projection data.
    pub fn down_proj_data(&self) -> &[u8] {
        self.tensor_data(&self.layout.down_proj)
    }

    /// Get input norm data.
    pub fn input_norm_data(&self) -> &[u8] {
        self.tensor_data(&self.layout.input_norm)
    }

    /// Get post-attention norm data.
    pub fn post_attn_norm_data(&self) -> &[u8] {
        self.tensor_data(&self.layout.post_attn_norm)
    }
}

/// RAM cache for layer weights.
///
/// Stores layers in CPU memory with optional pinned memory for fast GPU transfers.
/// Uses LRU eviction when capacity is exceeded.
pub struct RamCache {
    /// Cached layers (layer_idx -> weights).
    layers: HashMap<usize, CpuLayerWeights>,

    /// LRU tracking for eviction.
    lru: LruTracker<usize>,

    /// Current usage in bytes.
    usage: usize,

    /// Budget in bytes.
    budget: u64,

    /// Whether to use pinned memory.
    use_pinned: bool,

    /// Statistics tracker.
    stats: Arc<TieredStats>,
}

impl RamCache {
    /// Create a new RAM cache.
    ///
    /// # Arguments
    /// * `budget` - Maximum RAM usage in bytes
    /// * `use_pinned` - Whether to allocate pinned (page-locked) memory
    /// * `stats` - Statistics tracker
    pub fn new(budget: u64, use_pinned: bool, stats: Arc<TieredStats>) -> Self {
        Self {
            layers: HashMap::new(),
            lru: LruTracker::new(),
            usage: 0,
            budget,
            use_pinned,
            stats,
        }
    }

    /// Check if a layer is cached.
    pub fn contains(&self, layer_idx: usize) -> bool {
        self.layers.contains_key(&layer_idx)
    }

    /// Get a cached layer without updating LRU.
    pub fn peek(&self, layer_idx: usize) -> Option<&CpuLayerWeights> {
        self.layers.get(&layer_idx)
    }

    /// Get a cached layer, updating LRU tracking.
    pub fn get(&mut self, layer_idx: usize) -> Option<&CpuLayerWeights> {
        if self.layers.contains_key(&layer_idx) {
            self.lru.touch(layer_idx);
            self.stats.record_ram_hit();
            self.layers.get(&layer_idx)
        } else {
            None
        }
    }

    /// Insert a layer into the cache.
    ///
    /// # Arguments
    /// * `layer_idx` - Layer index
    /// * `weights` - CPU layer weights
    /// * `priority` - Eviction priority (higher = less likely to evict)
    pub fn insert(
        &mut self,
        layer_idx: usize,
        weights: CpuLayerWeights,
        priority: f32,
    ) -> Result<(), TieredError> {
        let size = weights.size_bytes;

        // Check budget
        if self.usage + size > self.budget as usize {
            return Err(TieredError::ram_alloc(
                format!("layer {} doesn't fit", layer_idx),
                size as u64,
                (self.budget as usize - self.usage) as u64,
            ));
        }

        // Remove old entry if exists
        if let Some(old) = self.layers.remove(&layer_idx) {
            self.usage -= old.size_bytes;
            self.lru.remove(&layer_idx);
        }

        // Insert new entry
        self.layers.insert(layer_idx, weights);
        self.lru.touch_with_priority(layer_idx, priority);
        self.usage += size;

        Ok(())
    }

    /// Remove a layer from the cache.
    pub fn remove(&mut self, layer_idx: usize) -> Option<CpuLayerWeights> {
        if let Some(layer) = self.layers.remove(&layer_idx) {
            self.usage -= layer.size_bytes;
            self.lru.remove(&layer_idx);
            Some(layer)
        } else {
            None
        }
    }

    /// Get current usage in bytes.
    pub fn usage(&self) -> usize {
        self.usage
    }

    /// Get available space in bytes.
    pub fn available(&self) -> usize {
        (self.budget as usize).saturating_sub(self.usage)
    }

    /// Get number of cached layers.
    pub fn num_layers(&self) -> usize {
        self.layers.len()
    }

    /// Check if there's room for a layer of given size.
    pub fn has_room_for(&self, size: usize) -> bool {
        self.usage + size <= self.budget as usize
    }

    /// Evict layers to free at least `bytes_needed`.
    ///
    /// Returns the number of bytes freed.
    pub fn evict_for_space(&mut self, bytes_needed: u64) -> u64 {
        let mut freed = 0u64;

        // Get sizes for eviction calculation
        let sizes: HashMap<usize, usize> = self
            .layers
            .iter()
            .map(|(&idx, layer)| (idx, layer.size_bytes))
            .collect();

        // Get eviction order
        let candidates = self.lru.eviction_candidates_for_size(bytes_needed, |idx| {
            sizes.get(idx).copied().unwrap_or(0) as u64
        });

        for layer_idx in candidates {
            if freed >= bytes_needed {
                break;
            }

            if let Some(layer) = self.layers.remove(&layer_idx) {
                freed += layer.size_bytes as u64;
                self.usage -= layer.size_bytes;
                self.lru.remove(&layer_idx);
                self.stats.record_ram_eviction(layer.size_bytes as u64);

                tracing::debug!(
                    layer_idx,
                    freed_mb = layer.size_bytes / (1024 * 1024),
                    "Evicted layer from RAM"
                );
            }
        }

        freed
    }

    /// Evict the least recently used layer.
    pub fn evict_lru(&mut self) -> Option<(usize, CpuLayerWeights)> {
        if let Some(layer_idx) = self.lru.pop_lru() {
            if let Some(layer) = self.layers.remove(&layer_idx) {
                self.usage -= layer.size_bytes;
                self.stats.record_ram_eviction(layer.size_bytes as u64);
                return Some((layer_idx, layer));
            }
        }
        None
    }

    /// Clear all cached layers.
    pub fn clear(&mut self) {
        self.layers.clear();
        self.lru.clear();
        self.usage = 0;
    }

    /// Get budget.
    pub fn budget(&self) -> u64 {
        self.budget
    }

    /// Check if using pinned memory.
    pub fn uses_pinned_memory(&self) -> bool {
        self.use_pinned
    }

    /// Get iterator over cached layer indices.
    pub fn layer_indices(&self) -> impl Iterator<Item = usize> + '_ {
        self.layers.keys().copied()
    }
}

impl std::fmt::Debug for RamCache {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("RamCache")
            .field("num_layers", &self.layers.len())
            .field("usage_mb", &(self.usage / (1024 * 1024)))
            .field("budget_mb", &(self.budget / (1024 * 1024)))
            .field("use_pinned", &self.use_pinned)
            .finish()
    }
}

/// Allocate pinned memory buffer.
///
/// Pinned memory is page-locked and can be transferred to GPU ~2x faster.
/// Falls back to regular allocation if pinning fails.
#[allow(dead_code)]
pub fn allocate_pinned(size: usize) -> Result<Vec<u8>, TieredError> {
    // For now, just use regular allocation
    // Full implementation would use cuMemAllocHost or similar
    Ok(vec![0u8; size])
}

/// Free pinned memory buffer.
///
/// Must be used for memory allocated with `allocate_pinned`.
#[allow(dead_code)]
pub fn free_pinned(_data: Vec<u8>) {
    // For now, just let it drop
    // Full implementation would use cuMemFreeHost
}

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

    fn create_test_cache() -> RamCache {
        RamCache::new(1024 * 1024 * 1024, false, Arc::new(TieredStats::new()))
    }

    fn create_dummy_weights(size: usize) -> CpuLayerWeights {
        let layout = LayerLayout {
            q_proj: TensorLayout::new(0, [4096, 4096]),
            k_proj: TensorLayout::new(0, [1024, 4096]),
            v_proj: TensorLayout::new(0, [1024, 4096]),
            o_proj: TensorLayout::new(0, [4096, 4096]),
            gate_proj: TensorLayout::new(0, [11008, 4096]),
            up_proj: TensorLayout::new(0, [11008, 4096]),
            down_proj: TensorLayout::new(0, [4096, 11008]),
            input_norm: TensorLayout::new_1d(0, 4096),
            post_attn_norm: TensorLayout::new_1d(0, 4096),
        };

        CpuLayerWeights {
            data: vec![0u8; size],
            layout,
            size_bytes: size,
            is_pinned: false,
        }
    }

    #[test]
    fn test_ram_cache_basic() {
        let mut cache = create_test_cache();

        let weights = create_dummy_weights(1024);
        cache.insert(0, weights, 0.5).unwrap();

        assert!(cache.contains(0));
        assert_eq!(cache.num_layers(), 1);
        assert_eq!(cache.usage(), 1024);
    }

    #[test]
    fn test_ram_cache_eviction() {
        let mut cache = RamCache::new(2048, false, Arc::new(TieredStats::new()));

        // Insert two layers
        cache.insert(0, create_dummy_weights(1024), 0.5).unwrap();
        cache.insert(1, create_dummy_weights(1024), 0.3).unwrap(); // Lower priority

        // Evict for space
        let freed = cache.evict_for_space(1024);
        assert_eq!(freed, 1024);

        // Lower priority layer should be evicted
        assert!(cache.contains(0));
        assert!(!cache.contains(1));
    }

    #[test]
    fn test_ram_cache_budget() {
        let mut cache = RamCache::new(1024, false, Arc::new(TieredStats::new()));

        // First insert succeeds
        cache.insert(0, create_dummy_weights(512), 0.5).unwrap();

        // Second insert would exceed budget
        let result = cache.insert(1, create_dummy_weights(1024), 0.5);
        assert!(result.is_err());
    }
}