infotheory 1.1.1

The algorithmic information theory library.
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
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
//! Simple aligned tensor types for SIMD operations.
//!
//! These are minimal, no-frills tensor implementations designed for:
//! - 32-byte aligned memory for portable SIMD kernels
//! - Direct access to underlying data
//! - Zero-copy views for weights

use std::alloc::{Layout, alloc_zeroed, dealloc};
use std::mem::size_of;
use std::ops::{Index, IndexMut};
use std::ptr::NonNull;

/// 32-byte alignment for SIMD-friendly access.
const ALIGNMENT: usize = 32;

#[inline]
fn dangling_aligned_f32() -> NonNull<f32> {
    debug_assert_eq!(ALIGNMENT % std::mem::align_of::<f32>(), 0);
    NonNull::new(ALIGNMENT as *mut u8)
        .expect("aligned dangling pointer must be non-null")
        .cast()
}

#[inline]
fn layout_for_f32_elems(len: usize) -> Layout {
    let bytes = len
        .checked_mul(size_of::<f32>())
        .expect("tensor allocation overflow");
    Layout::from_size_align(bytes, ALIGNMENT).expect("Invalid layout")
}

#[inline]
fn alloc_f32_buffer(len: usize) -> NonNull<f32> {
    if len == 0 {
        return dangling_aligned_f32();
    }
    let layout = layout_for_f32_elems(len);
    let ptr = unsafe { alloc_zeroed(layout) };
    NonNull::new(ptr).expect("Allocation failed").cast()
}

#[inline]
unsafe fn dealloc_f32_buffer(ptr: NonNull<f32>, len: usize) {
    if len == 0 {
        return;
    }
    let layout = layout_for_f32_elems(len);
    unsafe {
        dealloc(ptr.as_ptr() as *mut u8, layout);
    }
}

#[inline]
fn padded_stride(cols: usize) -> usize {
    cols.checked_add(7).expect("tensor stride overflow") & !7
}

/// Owned 1D tensor with aligned memory.
#[repr(C)]
pub struct Tensor1D {
    data: NonNull<f32>,
    len: usize,
}

impl Tensor1D {
    /// Create a new zero-initialized tensor.
    pub fn zeros(len: usize) -> Self {
        Self {
            data: alloc_f32_buffer(len),
            len,
        }
    }

    /// Create from an existing `Vec<f32>` (may copy if not aligned).
    pub fn from_vec(v: Vec<f32>) -> Self {
        let mut t = Self::zeros(v.len());
        t.as_mut_slice().copy_from_slice(&v);
        t
    }

    #[inline]
    /// Number of logical elements.
    pub fn len(&self) -> usize {
        self.len
    }

    #[inline]
    /// Returns `true` when `len() == 0`.
    pub fn is_empty(&self) -> bool {
        self.len == 0
    }

    #[inline]
    /// Raw pointer to the aligned backing buffer.
    pub fn as_ptr(&self) -> *const f32 {
        self.data.as_ptr()
    }

    #[inline]
    /// Mutable raw pointer to the aligned backing buffer.
    pub fn as_mut_ptr(&mut self) -> *mut f32 {
        self.data.as_ptr()
    }

    #[inline]
    /// Immutable slice over logical elements.
    pub fn as_slice(&self) -> &[f32] {
        unsafe { std::slice::from_raw_parts(self.data.as_ptr(), self.len) }
    }

    #[inline]
    /// Mutable slice over logical elements.
    pub fn as_mut_slice(&mut self) -> &mut [f32] {
        unsafe { std::slice::from_raw_parts_mut(self.data.as_ptr(), self.len) }
    }

    /// Fill with zeros.
    #[inline]
    pub fn zero(&mut self) {
        unsafe {
            std::ptr::write_bytes(self.data.as_ptr(), 0, self.len);
        }
    }

    /// Copy from another tensor.
    #[inline]
    pub fn copy_from(&mut self, other: &Tensor1D) {
        debug_assert_eq!(self.len, other.len);
        self.as_mut_slice().copy_from_slice(other.as_slice());
    }

    /// Copy from slice.
    #[inline]
    pub fn copy_from_slice(&mut self, slice: &[f32]) {
        debug_assert_eq!(self.len, slice.len());
        self.as_mut_slice().copy_from_slice(slice);
    }
}

impl Clone for Tensor1D {
    fn clone(&self) -> Self {
        let mut new = Self::zeros(self.len);
        new.as_mut_slice().copy_from_slice(self.as_slice());
        new
    }
}

impl Drop for Tensor1D {
    fn drop(&mut self) {
        unsafe {
            dealloc_f32_buffer(self.data, self.len);
        }
    }
}

// Safety: Tensor1D owns its data
unsafe impl Send for Tensor1D {}
unsafe impl Sync for Tensor1D {}

impl Index<usize> for Tensor1D {
    type Output = f32;

    #[inline]
    fn index(&self, i: usize) -> &f32 {
        debug_assert!(i < self.len);
        unsafe { &*self.data.as_ptr().add(i) }
    }
}

impl IndexMut<usize> for Tensor1D {
    #[inline]
    fn index_mut(&mut self, i: usize) -> &mut f32 {
        debug_assert!(i < self.len);
        unsafe { &mut *self.data.as_ptr().add(i) }
    }
}

/// Owned 2D tensor with aligned memory (row-major).
#[repr(C)]
pub struct Tensor2D {
    data: NonNull<f32>,
    rows: usize,
    cols: usize,
    stride: usize, // stride in elements (rounded up for alignment)
}

impl Tensor2D {
    /// Create a new zero-initialized 2D tensor.
    pub fn zeros(rows: usize, cols: usize) -> Self {
        // Pad cols to a multiple of 8 f32 lanes.
        let stride = padded_stride(cols);
        let total = rows
            .checked_mul(stride)
            .expect("tensor allocation overflow");

        Self {
            data: alloc_f32_buffer(total),
            rows,
            cols,
            stride,
        }
    }

    /// Create from Vec with shape.
    pub fn from_vec(v: Vec<f32>, rows: usize, cols: usize) -> Self {
        assert_eq!(v.len(), rows * cols);
        let mut t = Self::zeros(rows, cols);

        // Copy row by row to handle stride
        for r in 0..rows {
            let src_start = r * cols;
            let src_end = src_start + cols;
            t.row_mut(r).copy_from_slice(&v[src_start..src_end]);
        }
        t
    }

    #[inline]
    /// Number of matrix rows.
    pub fn rows(&self) -> usize {
        self.rows
    }

    #[inline]
    /// Number of logical columns (excluding stride padding).
    pub fn cols(&self) -> usize {
        self.cols
    }

    #[inline]
    /// Row stride in elements (includes alignment padding).
    pub fn stride(&self) -> usize {
        self.stride
    }

    #[inline]
    /// Raw pointer to matrix storage.
    pub fn as_ptr(&self) -> *const f32 {
        self.data.as_ptr()
    }

    #[inline]
    /// Mutable raw pointer to matrix storage.
    pub fn as_mut_ptr(&mut self) -> *mut f32 {
        self.data.as_ptr()
    }

    /// Get a row slice.
    #[inline]
    pub fn row(&self, r: usize) -> &[f32] {
        debug_assert!(r < self.rows);
        unsafe {
            let ptr = self.data.as_ptr().add(r * self.stride);
            std::slice::from_raw_parts(ptr, self.cols)
        }
    }

    /// Get a mutable row slice.
    #[inline]
    pub fn row_mut(&mut self, r: usize) -> &mut [f32] {
        debug_assert!(r < self.rows);
        unsafe {
            let ptr = self.data.as_ptr().add(r * self.stride);
            std::slice::from_raw_parts_mut(ptr, self.cols)
        }
    }

    /// Get raw row pointer (includes stride padding).
    #[inline]
    pub fn row_ptr(&self, r: usize) -> *const f32 {
        debug_assert!(r < self.rows);
        unsafe { self.data.as_ptr().add(r * self.stride) }
    }

    /// Get raw mutable row pointer.
    #[inline]
    pub fn row_ptr_mut(&mut self, r: usize) -> *mut f32 {
        debug_assert!(r < self.rows);
        unsafe { self.data.as_ptr().add(r * self.stride) }
    }

    /// Fill with zeros.
    pub fn zero(&mut self) {
        let total = self
            .rows
            .checked_mul(self.stride)
            .expect("tensor allocation overflow");
        unsafe {
            std::ptr::write_bytes(self.data.as_ptr(), 0, total);
        }
    }
}

impl Clone for Tensor2D {
    fn clone(&self) -> Self {
        let total = self
            .rows
            .checked_mul(self.stride)
            .expect("tensor allocation overflow");
        let data = alloc_f32_buffer(total);

        unsafe {
            std::ptr::copy_nonoverlapping(self.data.as_ptr(), data.as_ptr(), total);
        }

        Self {
            data,
            rows: self.rows,
            cols: self.cols,
            stride: self.stride,
        }
    }
}

impl Drop for Tensor2D {
    fn drop(&mut self) {
        let total = self
            .rows
            .checked_mul(self.stride)
            .expect("tensor allocation overflow");
        unsafe {
            dealloc_f32_buffer(self.data, total);
        }
    }
}

// Safety: Tensor2D owns its data
unsafe impl Send for Tensor2D {}
unsafe impl Sync for Tensor2D {}

/// View into external f32 data (for weights).
#[derive(Clone, Copy)]
pub struct TensorView1D<'a> {
    data: &'a [f32],
}

impl<'a> TensorView1D<'a> {
    #[inline]
    /// Wrap an immutable 1D slice.
    pub fn new(data: &'a [f32]) -> Self {
        Self { data }
    }

    #[inline]
    /// Number of elements in the view.
    pub fn len(&self) -> usize {
        self.data.len()
    }

    #[inline]
    /// Returns `true` when the view is empty.
    pub fn is_empty(&self) -> bool {
        self.data.is_empty()
    }

    #[inline]
    /// Raw pointer to the first element.
    pub fn as_ptr(&self) -> *const f32 {
        self.data.as_ptr()
    }

    #[inline]
    /// Borrow the underlying immutable slice.
    pub fn as_slice(&self) -> &[f32] {
        self.data
    }
}

impl<'a> Index<usize> for TensorView1D<'a> {
    type Output = f32;

    #[inline]
    fn index(&self, i: usize) -> &f32 {
        &self.data[i]
    }
}

/// View into external f32 data (for weights), row-major.
#[derive(Clone, Copy)]
pub struct TensorView2D<'a> {
    data: &'a [f32],
    rows: usize,
    cols: usize,
}

impl<'a> TensorView2D<'a> {
    #[inline]
    /// Wrap row-major data with explicit `(rows, cols)` logical shape.
    pub fn new(data: &'a [f32], rows: usize, cols: usize) -> Self {
        debug_assert_eq!(data.len(), rows * cols);
        Self { data, rows, cols }
    }

    #[inline]
    /// Number of rows in the view.
    pub fn rows(&self) -> usize {
        self.rows
    }

    #[inline]
    /// Number of columns in the view.
    pub fn cols(&self) -> usize {
        self.cols
    }

    #[inline]
    /// Raw pointer to the first element.
    pub fn as_ptr(&self) -> *const f32 {
        self.data.as_ptr()
    }

    #[inline]
    /// Borrow row `r`.
    pub fn row(&self, r: usize) -> &[f32] {
        debug_assert!(r < self.rows);
        let start = r * self.cols;
        &self.data[start..start + self.cols]
    }

    #[inline]
    /// Pointer to the first element of row `r`.
    pub fn row_ptr(&self, r: usize) -> *const f32 {
        debug_assert!(r < self.rows);
        unsafe { self.data.as_ptr().add(r * self.cols) }
    }

    /// Transpose view (returns new TensorView with swapped dims).
    /// Note: This is a logical transpose - data is still row-major of original.
    /// Use only for matmuls that handle transposed right operand.
    pub fn t(&self) -> TransposedView2D<'a> {
        TransposedView2D {
            data: self.data,
            rows: self.cols, // swapped
            cols: self.rows, // swapped
            orig_cols: self.cols,
        }
    }
}

/// Transposed view (for efficient transpose-multiply).
#[derive(Clone, Copy)]
pub struct TransposedView2D<'a> {
    data: &'a [f32],
    rows: usize,
    cols: usize,
    orig_cols: usize,
}

impl<'a> TransposedView2D<'a> {
    #[inline]
    pub fn rows(&self) -> usize {
        self.rows
    }

    #[inline]
    pub fn cols(&self) -> usize {
        self.cols
    }

    /// Get element at (r, c) in transposed view.
    #[inline]
    pub fn get(&self, r: usize, c: usize) -> f32 {
        // In transposed view, (r, c) maps to original (c, r)
        self.data[c * self.orig_cols + r]
    }

    /// Get original row (which is a column in transposed view).
    #[inline]
    pub fn orig_row(&self, r: usize) -> &[f32] {
        let start = r * self.orig_cols;
        &self.data[start..start + self.orig_cols]
    }
}

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

    #[test]
    fn zero_len_tensor1d_uses_aligned_non_allocating_sentinel() {
        let mut t = Tensor1D::zeros(0);
        assert_eq!(t.len(), 0);
        assert!(t.is_empty());
        assert!(t.as_slice().is_empty());
        assert!(t.as_mut_slice().is_empty());
        assert_eq!((t.as_ptr() as usize) % ALIGNMENT, 0);
        t.zero();
    }

    #[test]
    fn zero_sized_tensor2d_is_safe() {
        let mut t = Tensor2D::zeros(3, 0);
        assert_eq!(t.rows(), 3);
        assert_eq!(t.cols(), 0);
        assert_eq!(t.stride(), 0);
        assert_eq!((t.as_ptr() as usize) % ALIGNMENT, 0);
        for row in 0..t.rows() {
            assert!(t.row(row).is_empty());
            assert!(t.row_mut(row).is_empty());
        }
        t.zero();
    }
}