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
//! Tensor implementation
//!
//! This module provides the core `Tensor` type, which is an N-dimensional array
//! with automatic backend selection for optimal performance.
use std::fmt;
use num_traits::Num;
use serde::{Deserialize, Serialize};
use crate::error::{RealizarError, Result};
/// N-dimensional tensor with automatic backend dispatch
///
/// The tensor automatically selects the optimal execution backend (SIMD, GPU, WASM)
/// based on operation type, data size, and available hardware.
///
/// # Examples
///
/// ```
/// use realizar::Tensor;
///
/// // Create a 2×3 tensor
/// let t = Tensor::from_vec(vec![2, 3], vec![
/// 1.0, 2.0, 3.0,
/// 4.0, 5.0, 6.0,
/// ]).unwrap();
///
/// assert_eq!(t.shape(), &[2, 3]);
/// assert_eq!(t.ndim(), 2);
/// assert_eq!(t.size(), 6);
/// ```
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Tensor<T: Num> {
/// Flattened data in row-major order
data: Vec<T>,
/// Shape of the tensor
shape: Vec<usize>,
}
impl<T: Num + Clone> Tensor<T> {
/// Create a new tensor from a vector and shape
///
/// # Arguments
///
/// * `shape` - Dimensions of the tensor
/// * `data` - Flattened data in row-major order
///
/// # Errors
///
/// Returns `Err` if:
/// - Shape is empty
/// - Data size doesn't match shape
/// - Shape contains zero
///
/// # Examples
///
/// ```
/// use realizar::Tensor;
///
/// let t = Tensor::from_vec(vec![2, 2], vec![1.0, 2.0, 3.0, 4.0]).unwrap();
/// assert_eq!(t.shape(), &[2, 2]);
/// ```
pub fn from_vec(shape: Vec<usize>, data: Vec<T>) -> Result<Self> {
// Validate shape
if shape.is_empty() {
return Err(RealizarError::InvalidShape {
reason: "Shape cannot be empty".to_string(),
});
}
if shape.contains(&0) {
return Err(RealizarError::InvalidShape {
reason: "Shape dimensions cannot be zero".to_string(),
});
}
// Calculate expected size
let expected_size = shape.iter().product();
// Validate data size
if data.len() != expected_size {
return Err(RealizarError::DataShapeMismatch {
data_size: data.len(),
shape,
expected: expected_size,
});
}
Ok(Self { data, shape })
}
/// Get the shape of the tensor
///
/// # Examples
///
/// ```
/// use realizar::Tensor;
///
/// let t = Tensor::from_vec(vec![3, 4], vec![0.0; 12]).unwrap();
/// assert_eq!(t.shape(), &[3, 4]);
/// ```
#[must_use]
pub fn shape(&self) -> &[usize] {
&self.shape
}
/// Get the number of dimensions
///
/// # Examples
///
/// ```
/// use realizar::Tensor;
///
/// let t = Tensor::from_vec(vec![2, 3, 4], vec![0.0; 24]).unwrap();
/// assert_eq!(t.ndim(), 3);
/// ```
#[must_use]
pub fn ndim(&self) -> usize {
self.shape.len()
}
/// Get the total number of elements
///
/// # Examples
///
/// ```
/// use realizar::Tensor;
///
/// let t = Tensor::from_vec(vec![2, 3], vec![0.0; 6]).unwrap();
/// assert_eq!(t.size(), 6);
/// ```
#[must_use]
pub fn size(&self) -> usize {
self.data.len()
}
/// Get a reference to the underlying data
///
/// # Examples
///
/// ```
/// use realizar::Tensor;
///
/// let t = Tensor::from_vec(vec![2], vec![1.0, 2.0]).unwrap();
/// assert_eq!(t.data(), &[1.0, 2.0]);
/// ```
#[must_use]
pub fn data(&self) -> &[T] {
&self.data
}
}
impl<T: Num + Clone + fmt::Display> fmt::Display for Tensor<T> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Tensor(shape={:?}, data=[", self.shape)?;
for (i, val) in self.data.iter().enumerate() {
if i > 0 {
write!(f, ", ")?;
}
write!(f, "{val}")?;
}
write!(f, "])")
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_create_tensor() {
let t = Tensor::from_vec(vec![2, 3], vec![1.0, 2.0, 3.0, 4.0, 5.0, 6.0]).unwrap();
assert_eq!(t.shape(), &[2, 3]);
assert_eq!(t.ndim(), 2);
assert_eq!(t.size(), 6);
}
#[test]
fn test_empty_shape_error() {
let result = Tensor::from_vec(vec![], vec![1.0, 2.0]);
assert!(result.is_err());
assert!(matches!(
result.unwrap_err(),
RealizarError::InvalidShape { .. }
));
}
#[test]
fn test_zero_dimension_error() {
let result = Tensor::<f32>::from_vec(vec![2, 0], vec![]);
assert!(result.is_err());
}
#[test]
fn test_size_mismatch_error() {
let result = Tensor::from_vec(vec![2, 3], vec![1.0, 2.0]);
assert!(result.is_err());
assert!(matches!(
result.unwrap_err(),
RealizarError::DataShapeMismatch { .. }
));
}
#[test]
fn test_display() {
let t = Tensor::from_vec(vec![2], vec![1.0, 2.0]).unwrap();
let display = format!("{t}");
assert!(display.contains("shape=[2]"));
assert!(display.contains('1'));
assert!(display.contains('2'));
}
}