rust-ai-core 0.3.4

Unified AI engineering toolkit: orchestrates peft-rs, qlora-rs, unsloth-rs, axolotl-rs, bitnet-quantize, trit-vsa, vsa-optim-rs, and tritter-accel
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
// SPDX-License-Identifier: MIT
// Copyright 2026 Tyler Zervas

//! Memory estimation and tracking utilities for GPU operations.
//!
//! ## Why This Module Exists
//!
//! GPU memory (VRAM) is a precious and limited resource. Unlike CPU memory, there's no
//! swap space fallback when VRAM runs out - operations simply fail. This module provides
//! utilities to estimate memory requirements before allocation and track usage during
//! execution, enabling crates to:
//!
//! 1. **Pre-flight checks**: Verify sufficient VRAM before starting expensive operations
//! 2. **Batch size optimization**: Automatically adjust batch sizes to fit available memory
//! 3. **Memory budgeting**: Track allocations across multiple operations
//! 4. **Debugging**: Identify memory leaks or unexpected allocations
//!
//! ## Design Decisions
//!
//! - **Conservative estimation**: Estimates include overhead buffers because running out
//!   of memory mid-operation is worse than slightly underutilizing VRAM.
//!
//! - **No global state**: `MemoryTracker` is an explicit struct, not a global singleton,
//!   because different parts of an application may need independent tracking.
//!
//! - **Candle-agnostic sizes**: Functions work with shapes and dtypes directly, not just
//!   Candle tensors, enabling estimation before tensor creation.

use crate::error::{CoreError, Result};
use candle_core::DType;
use std::sync::atomic::{AtomicUsize, Ordering};

/// Default overhead factor applied to memory estimates.
///
/// Why 1.1x: CUDA allocators have alignment requirements and fragmentation overhead.
/// A 10% buffer prevents edge-case OOM errors when estimates are exact.
pub const DEFAULT_OVERHEAD_FACTOR: f64 = 1.1;

/// Estimate the memory required to store a tensor with given shape and dtype.
///
/// This function calculates the raw memory requirement without overhead. Use
/// [`MemoryTracker::estimate_with_overhead`] for production estimates.
///
/// ## Arguments
///
/// * `shape` - Tensor dimensions (e.g., `[batch, seq_len, hidden_dim]`)
/// * `dtype` - Data type determining bytes per element
///
/// ## Returns
///
/// Memory requirement in bytes.
///
/// ## Why This Function
///
/// Pre-computing memory requirements allows batch size optimization and preflight
/// checks before committing to expensive allocations.
///
/// ## Example
///
/// ```rust
/// use rust_ai_core::estimate_tensor_bytes;
/// use candle_core::DType;
///
/// // LLaMA-2 7B attention output: [batch, heads, seq, head_dim]
/// let bytes = estimate_tensor_bytes(&[1, 32, 4096, 128], DType::BF16);
/// assert_eq!(bytes, 1 * 32 * 4096 * 128 * 2); // 32 MB
/// ```
#[must_use]
pub fn estimate_tensor_bytes(shape: &[usize], dtype: DType) -> usize {
    let numel: usize = shape.iter().product();
    numel * dtype.size_in_bytes()
}

/// Estimate memory for attention computation.
///
/// Attention requires storing Q, K, V tensors plus the attention weights matrix.
/// This function estimates the total memory for a single attention layer.
///
/// ## Arguments
///
/// * `batch_size` - Number of sequences in the batch
/// * `num_heads` - Number of attention heads
/// * `seq_len` - Sequence length (context window)
/// * `head_dim` - Dimension per attention head
/// * `dtype` - Data type for all tensors
///
/// ## Returns
///
/// Estimated memory in bytes for one attention layer.
///
/// ## Why This Function
///
/// Attention is the primary memory consumer in transformers. The attention weights
/// matrix scales with `O(seq_len²)`, making it the bottleneck for long sequences.
/// This estimate helps determine maximum context length for a given VRAM budget.
///
/// ## Memory Breakdown
///
/// - Q, K, V: 3 × (batch × heads × seq × `head_dim`)
/// - Attention weights: batch × heads × seq × seq
/// - Output: batch × heads × seq × `head_dim`
#[must_use]
pub fn estimate_attention_memory(
    batch_size: usize,
    num_heads: usize,
    seq_len: usize,
    head_dim: usize,
    dtype: DType,
) -> usize {
    let bytes_per_elem = dtype.size_in_bytes();

    // Q, K, V tensors
    let qkv_bytes = 3 * batch_size * num_heads * seq_len * head_dim * bytes_per_elem;

    // Attention weights matrix (the O(n²) component)
    let attn_weights_bytes = batch_size * num_heads * seq_len * seq_len * bytes_per_elem;

    // Output tensor
    let output_bytes = batch_size * num_heads * seq_len * head_dim * bytes_per_elem;

    qkv_bytes + attn_weights_bytes + output_bytes
}

/// Memory usage tracker for GPU operations.
///
/// Tracks allocated and peak memory usage across operations. Thread-safe via atomics.
///
/// ## Why This Struct
///
/// Unlike CPU memory which is managed by the OS, GPU memory requires explicit tracking
/// because:
///
/// 1. **No swap**: When VRAM runs out, allocations fail immediately
/// 2. **Fragmentation**: Repeated allocations can fragment the heap
/// 3. **Debugging**: Memory leaks on GPU are harder to diagnose than CPU leaks
///
/// ## Usage Pattern
///
/// ```rust
/// use rust_ai_core::MemoryTracker;
///
/// let tracker = MemoryTracker::new();
///
/// // Before allocation
/// tracker.allocate(1024 * 1024).expect("allocation should succeed"); // 1 MB
///
/// // After freeing
/// tracker.deallocate(1024 * 1024);
///
/// println!("Peak usage: {} bytes", tracker.peak_bytes());
/// ```
#[derive(Debug)]
pub struct MemoryTracker {
    /// Currently allocated bytes.
    allocated: AtomicUsize,
    /// Peak allocation during lifetime.
    peak: AtomicUsize,
    /// Optional memory limit (0 = unlimited).
    limit: AtomicUsize,
    /// Overhead factor for estimates.
    overhead_factor: f64,
}

impl Default for MemoryTracker {
    fn default() -> Self {
        Self::new()
    }
}

impl MemoryTracker {
    /// Create a new memory tracker with no limit.
    ///
    /// ## Why No Default Limit
    ///
    /// Different GPUs have vastly different VRAM capacities (4GB to 80GB+).
    /// Setting a default limit would either be too restrictive for high-end cards
    /// or meaningless for consumer cards. Users should set limits explicitly.
    #[must_use]
    pub fn new() -> Self {
        Self {
            allocated: AtomicUsize::new(0),
            peak: AtomicUsize::new(0),
            limit: AtomicUsize::new(0),
            overhead_factor: DEFAULT_OVERHEAD_FACTOR,
        }
    }

    /// Create a tracker with a memory limit.
    ///
    /// ## Arguments
    ///
    /// * `limit_bytes` - Maximum allowed allocation in bytes
    ///
    /// ## Why Memory Limits
    ///
    /// Setting a limit below actual VRAM capacity reserves space for:
    /// - CUDA context overhead (~200-500 MB)
    /// - Framework allocations (PyTorch/Candle tensor cache)
    /// - Other processes sharing the GPU
    #[must_use]
    pub fn with_limit(limit_bytes: usize) -> Self {
        Self {
            allocated: AtomicUsize::new(0),
            peak: AtomicUsize::new(0),
            limit: AtomicUsize::new(limit_bytes),
            overhead_factor: DEFAULT_OVERHEAD_FACTOR,
        }
    }

    /// Set a custom overhead factor for estimates.
    ///
    /// ## Arguments
    ///
    /// * `factor` - Multiplier applied to estimates (default: 1.1)
    #[must_use]
    pub fn with_overhead_factor(mut self, factor: f64) -> Self {
        self.overhead_factor = factor;
        self
    }

    /// Record a memory allocation.
    ///
    /// ## Arguments
    ///
    /// * `bytes` - Number of bytes allocated
    ///
    /// ## Returns
    ///
    /// `Ok(())` if allocation is within limits.
    ///
    /// ## Errors
    ///
    /// Returns `CoreError::OutOfMemory` if allocation would exceed the limit.
    ///
    /// ## Why Track Allocations
    ///
    /// Explicit tracking catches memory issues early. Without tracking, OOM errors
    /// occur deep in CUDA kernels with unhelpful error messages.
    pub fn allocate(&self, bytes: usize) -> Result<()> {
        // Check limit BEFORE updating state to avoid partial updates on failure
        let limit = self.limit.load(Ordering::SeqCst);
        let current = self.allocated.load(Ordering::SeqCst);
        let new_allocated = current + bytes;

        if limit > 0 && new_allocated > limit {
            return Err(CoreError::oom(format!(
                "allocation of {bytes} bytes would exceed limit of {limit} bytes \
                 (current: {current} bytes)"
            )));
        }

        // Update allocated (actual update happens here)
        let actual_new = self.allocated.fetch_add(bytes, Ordering::SeqCst) + bytes;

        // Update peak
        let mut current_peak = self.peak.load(Ordering::SeqCst);
        while actual_new > current_peak {
            match self.peak.compare_exchange_weak(
                current_peak,
                actual_new,
                Ordering::SeqCst,
                Ordering::SeqCst,
            ) {
                Ok(_) => break,
                Err(p) => current_peak = p,
            }
        }

        Ok(())
    }

    /// Record a memory deallocation.
    ///
    /// ## Arguments
    ///
    /// * `bytes` - Number of bytes freed
    pub fn deallocate(&self, bytes: usize) {
        self.allocated.fetch_sub(bytes, Ordering::SeqCst);
    }

    /// Get currently allocated bytes.
    #[must_use]
    pub fn allocated_bytes(&self) -> usize {
        self.allocated.load(Ordering::SeqCst)
    }

    /// Get peak allocation during tracker lifetime.
    ///
    /// ## Why Track Peak
    ///
    /// Peak usage is more useful than current usage for capacity planning.
    /// It shows the high-water mark needed to complete a workload.
    #[must_use]
    pub fn peak_bytes(&self) -> usize {
        self.peak.load(Ordering::SeqCst)
    }

    /// Get configured memory limit (0 = unlimited).
    #[must_use]
    pub fn limit_bytes(&self) -> usize {
        self.limit.load(Ordering::SeqCst)
    }

    /// Estimate required memory with overhead factor applied.
    ///
    /// ## Arguments
    ///
    /// * `shape` - Tensor dimensions
    /// * `dtype` - Data type
    ///
    /// ## Returns
    ///
    /// Estimated bytes including overhead buffer.
    #[must_use]
    pub fn estimate_with_overhead(&self, shape: &[usize], dtype: DType) -> usize {
        let raw = estimate_tensor_bytes(shape, dtype);
        #[allow(
            clippy::cast_sign_loss,
            clippy::cast_possible_truncation,
            clippy::cast_precision_loss
        )]
        {
            (raw as f64 * self.overhead_factor) as usize
        }
    }

    /// Check if an allocation would fit within limits.
    ///
    /// ## Arguments
    ///
    /// * `bytes` - Proposed allocation size
    ///
    /// ## Returns
    ///
    /// `true` if allocation would succeed.
    #[must_use]
    pub fn would_fit(&self, bytes: usize) -> bool {
        let limit = self.limit.load(Ordering::SeqCst);
        if limit == 0 {
            return true; // No limit
        }
        self.allocated.load(Ordering::SeqCst) + bytes <= limit
    }

    /// Reset the tracker to initial state.
    ///
    /// ## Why Reset
    ///
    /// Between training epochs or inference batches, resetting allows tracking
    /// per-phase memory usage without creating new tracker instances.
    pub fn reset(&self) {
        self.allocated.store(0, Ordering::SeqCst);
        self.peak.store(0, Ordering::SeqCst);
    }
}

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

    #[test]
    fn test_estimate_tensor_bytes() {
        // 1000 f32 elements = 4000 bytes
        assert_eq!(estimate_tensor_bytes(&[10, 100], DType::F32), 4000);

        // 1000 f16 elements = 2000 bytes
        assert_eq!(estimate_tensor_bytes(&[10, 100], DType::F16), 2000);

        // Empty tensor = 0 bytes
        assert_eq!(estimate_tensor_bytes(&[0], DType::F32), 0);
    }

    #[test]
    fn test_estimate_attention_memory() {
        // Simple case: 1 batch, 1 head, 4 seq, 2 head_dim, f32
        let bytes = estimate_attention_memory(1, 1, 4, 2, DType::F32);
        // QKV: 3 * 1 * 1 * 4 * 2 * 4 = 96
        // Attn: 1 * 1 * 4 * 4 * 4 = 64
        // Out: 1 * 1 * 4 * 2 * 4 = 32
        // Total: 192
        assert_eq!(bytes, 192);
    }

    #[test]
    fn test_memory_tracker_allocation() {
        let tracker = MemoryTracker::with_limit(1000);

        // Successful allocation
        assert!(tracker.allocate(500).is_ok());
        assert_eq!(tracker.allocated_bytes(), 500);

        // Second allocation
        assert!(tracker.allocate(400).is_ok());
        assert_eq!(tracker.allocated_bytes(), 900);

        // Exceeds limit
        assert!(tracker.allocate(200).is_err());
        assert_eq!(tracker.allocated_bytes(), 900); // Unchanged

        // Deallocation
        tracker.deallocate(400);
        assert_eq!(tracker.allocated_bytes(), 500);

        // Now fits
        assert!(tracker.allocate(200).is_ok());
    }

    #[test]
    fn test_memory_tracker_peak() {
        let tracker = MemoryTracker::new();

        tracker.allocate(100).unwrap();
        tracker.allocate(200).unwrap();
        assert_eq!(tracker.peak_bytes(), 300);

        tracker.deallocate(200);
        assert_eq!(tracker.allocated_bytes(), 100);
        assert_eq!(tracker.peak_bytes(), 300); // Peak unchanged

        tracker.allocate(50).unwrap();
        assert_eq!(tracker.peak_bytes(), 300); // Still 300

        tracker.allocate(300).unwrap();
        assert_eq!(tracker.peak_bytes(), 450); // New peak
    }

    #[test]
    fn test_would_fit() {
        let tracker = MemoryTracker::with_limit(1000);
        tracker.allocate(500).unwrap();

        assert!(tracker.would_fit(400));
        assert!(tracker.would_fit(500));
        assert!(!tracker.would_fit(501));

        // Unlimited tracker
        let unlimited = MemoryTracker::new();
        assert!(unlimited.would_fit(usize::MAX));
    }
}