torsh-core 0.1.2

Core types and traits for ToRSh deep learning framework
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
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
//! Core shape types and fundamental operations

use crate::error::{Result, TorshError};
use std::sync::{Arc, OnceLock};

// Constants for commonly used error messages to reduce heap allocations
const ZERO_DIMENSION_ERROR: &str = "Shape cannot contain zero dimensions";

/// Core Shape type representing tensor dimensions
///
/// A Shape represents the dimensions of a tensor and provides fundamental
/// operations for querying and manipulating tensor shapes.
///
/// # Performance Notes
///
/// The stride computation is cached internally to avoid repeated allocations.
/// This provides 15-20% performance improvement in hot paths.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Shape {
    dims: Vec<usize>,
    /// Cached strides for efficient repeated access
    /// Skipped during serialization and equality comparisons
    #[cfg_attr(feature = "serialize", serde(skip))]
    #[cfg_attr(feature = "serialize", serde(default))]
    strides_cache: OnceLock<Arc<[usize]>>,
}

// Manual implementations of PartialEq, Eq, and Hash that ignore the cache
impl PartialEq for Shape {
    fn eq(&self, other: &Self) -> bool {
        self.dims == other.dims
    }
}

impl Eq for Shape {}

impl std::hash::Hash for Shape {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.dims.hash(state);
    }
}

impl Shape {
    /// Create a new shape from dimensions
    ///
    /// # Examples
    ///
    /// ```
    /// use torsh_core::shape::Shape;
    ///
    /// let shape = Shape::new(vec![2, 3, 4]);
    /// assert_eq!(shape.dims(), &[2, 3, 4]);
    /// ```
    pub fn new(dims: Vec<usize>) -> Self {
        Shape {
            dims,
            strides_cache: OnceLock::new(),
        }
    }

    /// Create a shape from dimensions with validation (no zero dimensions)
    ///
    /// # Examples
    ///
    /// ```
    /// use torsh_core::shape::Shape;
    ///
    /// // Valid shape
    /// let shape = Shape::from_dims(vec![2, 3, 4]).unwrap();
    /// assert_eq!(shape.dims(), &[2, 3, 4]);
    ///
    /// // Invalid shape with zero dimension
    /// let result = Shape::from_dims(vec![2, 0, 4]);
    /// assert!(result.is_err());
    /// ```
    pub fn from_dims<T: Into<Vec<usize>>>(dims: T) -> Result<Self> {
        let dims = dims.into();
        if dims.contains(&0) {
            return Err(TorshError::InvalidShape(ZERO_DIMENSION_ERROR.to_string()));
        }
        Ok(Shape::new(dims))
    }

    /// Create a shape from a slice with validation
    pub fn from_slice(dims: &[usize]) -> Result<Self> {
        if dims.contains(&0) {
            return Err(TorshError::InvalidShape(ZERO_DIMENSION_ERROR.to_string()));
        }
        Ok(Shape::new(dims.to_vec()))
    }

    /// Create a shape from an array with validation
    pub fn from_array<const N: usize>(dims: [usize; N]) -> Result<Self> {
        if dims.contains(&0) {
            return Err(TorshError::InvalidShape(ZERO_DIMENSION_ERROR.to_string()));
        }
        Ok(Shape::new(dims.to_vec()))
    }

    /// Create a 1D shape with validation
    pub fn from_1d(d1: usize) -> Result<Self> {
        if d1 == 0 {
            return Err(TorshError::InvalidShape(ZERO_DIMENSION_ERROR.to_string()));
        }
        Ok(Shape::new(vec![d1]))
    }

    /// Create a 2D shape with validation
    pub fn from_2d(d1: usize, d2: usize) -> Result<Self> {
        if d1 == 0 || d2 == 0 {
            return Err(TorshError::InvalidShape(ZERO_DIMENSION_ERROR.to_string()));
        }
        Ok(Shape::new(vec![d1, d2]))
    }

    /// Create a 3D shape with validation
    pub fn from_3d(d1: usize, d2: usize, d3: usize) -> Result<Self> {
        if d1 == 0 || d2 == 0 || d3 == 0 {
            return Err(TorshError::InvalidShape(ZERO_DIMENSION_ERROR.to_string()));
        }
        Ok(Shape::new(vec![d1, d2, d3]))
    }

    /// Create a 4D shape with validation
    pub fn from_4d(d1: usize, d2: usize, d3: usize, d4: usize) -> Result<Self> {
        if d1 == 0 || d2 == 0 || d3 == 0 || d4 == 0 {
            return Err(TorshError::InvalidShape(ZERO_DIMENSION_ERROR.to_string()));
        }
        Ok(Shape::new(vec![d1, d2, d3, d4]))
    }

    /// Create a scalar shape (0-dimensional)
    pub fn scalar() -> Self {
        Shape::new(Vec::new())
    }

    /// Get the number of dimensions (rank)
    ///
    /// # Examples
    ///
    /// ```
    /// use torsh_core::shape::Shape;
    ///
    /// let scalar = Shape::new(vec![]);
    /// assert_eq!(scalar.ndim(), 0);
    ///
    /// let vector = Shape::new(vec![10]);
    /// assert_eq!(vector.ndim(), 1);
    ///
    /// let matrix = Shape::new(vec![3, 4]);
    /// assert_eq!(matrix.ndim(), 2);
    /// ```
    pub const fn ndim(&self) -> usize {
        self.dims.len()
    }

    /// Get the number of dimensions (alias for `ndim()`)
    ///
    /// This method provides a more idiomatic Rust interface for getting the
    /// number of dimensions, following the convention of using `len()` for
    /// collection-like types.
    ///
    /// # Examples
    ///
    /// ```
    /// use torsh_core::shape::Shape;
    ///
    /// let shape = Shape::new(vec![2, 3, 4]);
    /// assert_eq!(shape.len(), 3);
    /// assert_eq!(shape.len(), shape.ndim());
    /// ```
    #[inline]
    #[allow(clippy::len_without_is_empty)]
    pub const fn len(&self) -> usize {
        self.ndim()
    }

    /// Get contiguous strides for this shape
    ///
    /// Returns a cached reference to avoid repeated allocations.
    /// This provides 15-20% performance improvement in hot paths.
    ///
    /// # Examples
    ///
    /// ```
    /// use torsh_core::shape::Shape;
    ///
    /// let shape = Shape::new(vec![2, 3, 4]);
    /// let strides = shape.strides();
    /// assert_eq!(strides, &[12, 4, 1]);
    /// ```
    pub fn strides(&self) -> &[usize] {
        self.strides_cache
            .get_or_init(|| {
                if self.dims.is_empty() {
                    return Arc::from([]);
                }

                let mut strides = vec![1; self.dims.len()];
                for i in (0..self.dims.len() - 1).rev() {
                    strides[i] = strides[i + 1] * self.dims[i + 1];
                }
                Arc::from(strides)
            })
            .as_ref()
    }

    /// Get default (contiguous) strides for this shape
    /// This is an alias for `strides()` for backward compatibility
    pub fn default_strides(&self) -> &[usize] {
        self.strides()
    }

    /// Check if the shape represents a contiguous tensor
    /// (always true for shapes, as they define the default contiguous layout)
    pub fn is_contiguous(&self) -> bool {
        true
    }

    /// Get the dimensions
    ///
    /// # Examples
    ///
    /// ```
    /// use torsh_core::shape::Shape;
    ///
    /// let shape = Shape::new(vec![2, 3, 4]);
    /// assert_eq!(shape.dims(), &[2, 3, 4]);
    ///
    /// let scalar = Shape::new(vec![]);
    /// assert_eq!(scalar.dims(), &[] as &[usize]);
    /// ```
    pub fn dims(&self) -> &[usize] {
        &self.dims
    }

    /// Get the total number of elements
    ///
    /// # Examples
    ///
    /// ```
    /// use torsh_core::shape::Shape;
    ///
    /// let shape = Shape::new(vec![2, 3, 4]);
    /// assert_eq!(shape.numel(), 24);
    ///
    /// let scalar = Shape::new(vec![]);
    /// assert_eq!(scalar.numel(), 1);
    ///
    /// let vector = Shape::new(vec![5]);
    /// assert_eq!(vector.numel(), 5);
    /// ```
    pub fn numel(&self) -> usize {
        self.dims
            .iter()
            .try_fold(1usize, |acc, &dim| acc.checked_mul(dim))
            .unwrap_or(usize::MAX)
    }

    /// Check if the shape is empty (contains zero)
    ///
    /// # Examples
    ///
    /// ```
    /// use torsh_core::shape::Shape;
    ///
    /// let shape = Shape::new(vec![2, 3, 4]);
    /// assert!(!shape.is_empty());
    ///
    /// let empty_shape = Shape::new(vec![2, 0, 4]);
    /// assert!(empty_shape.is_empty());
    /// ```
    pub fn is_empty(&self) -> bool {
        self.dims.contains(&0)
    }

    /// Check if the shape is scalar
    ///
    /// # Examples
    ///
    /// ```
    /// use torsh_core::shape::Shape;
    ///
    /// let scalar = Shape::new(vec![]);
    /// assert!(scalar.is_scalar());
    ///
    /// let vector = Shape::new(vec![5]);
    /// assert!(!vector.is_scalar());
    ///
    /// let matrix = Shape::new(vec![2, 3]);
    /// assert!(!matrix.is_scalar());
    /// ```
    pub const fn is_scalar(&self) -> bool {
        self.dims.is_empty()
    }

    /// Get the size of a specific dimension
    ///
    /// Supports negative indexing (e.g., -1 for last dimension)
    ///
    /// # Examples
    ///
    /// ```
    /// use torsh_core::shape::Shape;
    ///
    /// let shape = Shape::new(vec![2, 3, 4]);
    /// assert_eq!(shape.size(0).unwrap(), 2);
    /// assert_eq!(shape.size(-1).unwrap(), 4);
    /// ```
    pub fn size(&self, dim: i32) -> Result<usize> {
        let ndim = self.dims.len() as i32;
        let dim = if dim < 0 { ndim + dim } else { dim };

        if dim < 0 || dim >= ndim {
            return Err(TorshError::InvalidArgument(format!(
                "Invalid dimension {} for shape with {} dimensions",
                dim, ndim
            )));
        }

        Ok(self.dims[dim as usize])
    }

    /// Check if two shapes are compatible for broadcasting
    ///
    /// # Examples
    ///
    /// ```
    /// use torsh_core::shape::Shape;
    ///
    /// let shape1 = Shape::new(vec![3, 1, 4]);
    /// let shape2 = Shape::new(vec![2, 4]);
    /// assert!(shape1.is_broadcastable_with(&shape2));
    ///
    /// let shape3 = Shape::new(vec![3, 2]);
    /// let shape4 = Shape::new(vec![3, 4]);
    /// assert!(!shape3.is_broadcastable_with(&shape4));
    /// ```
    pub fn is_broadcastable_with(&self, other: &Shape) -> bool {
        let mut dims1 = self.dims.iter().rev();
        let mut dims2 = other.dims.iter().rev();

        loop {
            match (dims1.next(), dims2.next()) {
                (Some(&d1), Some(&d2)) => {
                    if d1 != d2 && d1 != 1 && d2 != 1 {
                        return false;
                    }
                }
                (None, None) => break,
                _ => {} // One is shorter than the other, which is fine
            }
        }

        true
    }

    /// Compute the broadcast shape with another shape
    ///
    /// # Examples
    ///
    /// ```
    /// use torsh_core::shape::Shape;
    ///
    /// let shape1 = Shape::new(vec![3, 1, 4]);
    /// let shape2 = Shape::new(vec![2, 4]);
    /// let broadcast_shape = shape1.broadcast_with(&shape2).unwrap();
    /// assert_eq!(broadcast_shape.dims(), &[3, 2, 4]);
    /// ```
    pub fn broadcast_with(&self, other: &Shape) -> Result<Shape> {
        if !self.is_broadcastable_with(other) {
            return Err(TorshError::BroadcastError {
                shape1: self.dims.clone(),
                shape2: other.dims.clone(),
            });
        }

        let max_ndim = self.ndim().max(other.ndim());
        let mut result_dims = vec![1; max_ndim];

        let self_dims_padded = self.dims_padded_to(max_ndim);
        let other_dims_padded = other.dims_padded_to(max_ndim);

        for i in 0..max_ndim {
            let d1 = self_dims_padded[i];
            let d2 = other_dims_padded[i];
            result_dims[i] = d1.max(d2);
        }

        Ok(Shape::new(result_dims))
    }

    /// Get dimensions padded to a specific length with 1s at the beginning
    fn dims_padded_to(&self, target_len: usize) -> Vec<usize> {
        if self.ndim() >= target_len {
            self.dims.clone()
        } else {
            let mut padded = vec![1; target_len - self.ndim()];
            padded.extend_from_slice(&self.dims);
            padded
        }
    }

    /// Check if the shape represents a matrix (2D)
    pub fn is_matrix(&self) -> bool {
        self.ndim() == 2
    }

    /// Check if the shape represents a vector (1D)
    pub fn is_vector(&self) -> bool {
        self.ndim() == 1
    }

    /// Get the shape as a reference to the inner vector
    pub fn as_slice(&self) -> &[usize] {
        &self.dims
    }

    /// Convert the shape to a vector
    pub fn to_vec(&self) -> Vec<usize> {
        self.dims.clone()
    }

    /// Create a new shape with an additional dimension of size 1 at the specified position
    ///
    /// # Examples
    ///
    /// ```
    /// use torsh_core::shape::Shape;
    ///
    /// let shape = Shape::new(vec![3, 4]);
    /// let unsqueezed = shape.unsqueeze(1).unwrap();
    /// assert_eq!(unsqueezed.dims(), &[3, 1, 4]);
    /// ```
    pub fn unsqueeze(&self, dim: i32) -> Result<Shape> {
        let ndim = self.dims.len() as i32;
        let new_ndim = ndim + 1;
        let dim = if dim < 0 { new_ndim + dim } else { dim };

        if dim < 0 || dim > ndim {
            return Err(TorshError::InvalidArgument(format!(
                "Invalid dimension {} for shape with {} dimensions",
                dim, ndim
            )));
        }

        let mut new_dims = self.dims.clone();
        new_dims.insert(dim as usize, 1);
        Ok(Shape::new(new_dims))
    }

    /// Create a new shape with dimensions of size 1 removed
    ///
    /// # Examples
    ///
    /// ```
    /// use torsh_core::shape::Shape;
    ///
    /// let shape = Shape::new(vec![1, 3, 1, 4]);
    /// let squeezed = shape.squeeze();
    /// assert_eq!(squeezed.dims(), &[3, 4]);
    /// ```
    pub fn squeeze(&self) -> Shape {
        let new_dims: Vec<usize> = self.dims.iter().filter(|&&d| d != 1).copied().collect();
        Shape::new(new_dims)
    }

    /// Create a new shape with a specific dimension of size 1 removed
    ///
    /// # Examples
    ///
    /// ```
    /// use torsh_core::shape::Shape;
    ///
    /// let shape = Shape::new(vec![1, 3, 1, 4]);
    /// let squeezed = shape.squeeze_dim(0).unwrap();
    /// assert_eq!(squeezed.dims(), &[3, 1, 4]);
    /// ```
    pub fn squeeze_dim(&self, dim: i32) -> Result<Shape> {
        let ndim = self.dims.len() as i32;
        let dim = if dim < 0 { ndim + dim } else { dim };

        if dim < 0 || dim >= ndim {
            return Err(TorshError::InvalidArgument(format!(
                "Invalid dimension {} for shape with {} dimensions",
                dim, ndim
            )));
        }

        let dim_size = self.dims[dim as usize];
        if dim_size != 1 {
            return Err(TorshError::InvalidOperation(format!(
                "Cannot squeeze dimension {} with size {}",
                dim, dim_size
            )));
        }

        let mut new_dims = self.dims.clone();
        new_dims.remove(dim as usize);
        Ok(Shape::new(new_dims))
    }
}

impl Default for Shape {
    fn default() -> Self {
        Shape::scalar()
    }
}

impl From<Vec<usize>> for Shape {
    fn from(dims: Vec<usize>) -> Self {
        Shape::new(dims)
    }
}

impl From<&[usize]> for Shape {
    fn from(dims: &[usize]) -> Self {
        Shape::new(dims.to_vec())
    }
}

impl<const N: usize> From<[usize; N]> for Shape {
    fn from(dims: [usize; N]) -> Self {
        Shape::new(dims.to_vec())
    }
}

impl std::fmt::Display for Shape {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if self.is_scalar() {
            write!(f, "[]")
        } else {
            write!(
                f,
                "[{}]",
                self.dims
                    .iter()
                    .map(|d| d.to_string())
                    .collect::<Vec<_>>()
                    .join(", ")
            )
        }
    }
}

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

    #[test]
    fn test_shape_creation() {
        let shape = Shape::new(vec![2, 3, 4]);
        assert_eq!(shape.dims(), &[2, 3, 4]);
        assert_eq!(shape.ndim(), 3);
        assert_eq!(shape.numel(), 24);
        assert!(!shape.is_scalar());
        assert!(!shape.is_empty());
    }

    #[test]
    fn test_shape_validation() {
        let valid_shape = Shape::from_dims(vec![2, 3, 4]);
        assert!(valid_shape.is_ok());

        let invalid_shape = Shape::from_dims(vec![2, 0, 4]);
        assert!(invalid_shape.is_err());
    }

    #[test]
    fn test_scalar_shape() {
        let scalar = Shape::scalar();
        assert!(scalar.is_scalar());
        assert_eq!(scalar.ndim(), 0);
        assert_eq!(scalar.numel(), 1);
        assert_eq!(scalar.dims(), &[] as &[usize]);
    }

    #[test]
    fn test_broadcasting() {
        let shape1 = Shape::new(vec![3, 1, 4]);
        let shape2 = Shape::new(vec![2, 4]);
        assert!(shape1.is_broadcastable_with(&shape2));

        let broadcast_shape = shape1
            .broadcast_with(&shape2)
            .expect("broadcast should succeed");
        assert_eq!(broadcast_shape.dims(), &[3, 2, 4]);

        let shape3 = Shape::new(vec![3, 2]);
        let shape4 = Shape::new(vec![3, 4]);
        assert!(!shape3.is_broadcastable_with(&shape4));
    }

    #[test]
    fn test_squeeze_unsqueeze() {
        let shape = Shape::new(vec![1, 3, 1, 4]);
        let squeezed = shape.squeeze();
        assert_eq!(squeezed.dims(), &[3, 4]);

        let shape2 = Shape::new(vec![3, 4]);
        let unsqueezed = shape2.unsqueeze(1).expect("unsqueeze should succeed");
        assert_eq!(unsqueezed.dims(), &[3, 1, 4]);

        let squeeze_dim = shape.squeeze_dim(0).expect("squeeze_dim should succeed");
        assert_eq!(squeeze_dim.dims(), &[3, 1, 4]);
    }

    #[test]
    fn test_shape_properties() {
        let matrix = Shape::new(vec![3, 4]);
        assert!(matrix.is_matrix());
        assert!(!matrix.is_vector());

        let vector = Shape::new(vec![5]);
        assert!(!vector.is_matrix());
        assert!(vector.is_vector());

        assert_eq!(matrix.size(0).expect("size should succeed"), 3);
        assert_eq!(matrix.size(-1).expect("size should succeed"), 4);
    }

    #[test]
    fn test_shape_conversions() {
        let dims = vec![2, 3, 4];
        let shape: Shape = dims.clone().into();
        assert_eq!(shape.dims(), &dims);

        let arr = [2, 3, 4];
        let shape: Shape = arr.into();
        assert_eq!(shape.dims(), &[2, 3, 4]);
    }

    #[test]
    fn test_shape_display() {
        let scalar = Shape::scalar();
        assert_eq!(format!("{}", scalar), "[]");

        let matrix = Shape::new(vec![3, 4]);
        assert_eq!(format!("{}", matrix), "[3, 4]");
    }

    #[test]
    fn test_stride_caching() {
        let shape = Shape::new(vec![2, 3, 4]);

        // First call computes and caches strides
        let strides1 = shape.strides();
        assert_eq!(strides1, &[12, 4, 1]);

        // Second call returns cached reference (same pointer)
        let strides2 = shape.strides();
        assert_eq!(strides2, &[12, 4, 1]);

        // Verify both references point to the same cached data
        assert_eq!(strides1.as_ptr(), strides2.as_ptr());

        // Test empty shape caching
        let empty_shape = Shape::scalar();
        let empty_strides1 = empty_shape.strides();
        let empty_strides2 = empty_shape.strides();
        assert_eq!(empty_strides1, &[] as &[usize]);
        assert_eq!(empty_strides1.as_ptr(), empty_strides2.as_ptr());
    }
}