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
use std::sync::{Arc, RwLock};
use crate::{
Device, Shape, Tensor, TensorError,
tensor::{TensorData, TensorInner},
};
impl Tensor {
/// Create a new tensor from the given data.
///
/// # Notes
/// * The type of data supported by this function includes scalar, vector, and slice.
/// * The element type of the data supported by this function includes `f32`, `f64`, `i64`, `u32`, `u8`.
///
/// # Arguments
/// * `data` - The data to create the tensor from.
/// * `device` - The device to place the tensor on.
/// * `grad_enabled` - Whether to enable gradient tracking for the tensor.
///
/// # Returns
/// * `Ok(tensor)` - The created tensor if successful.
/// * `Err(TensorError)` - The error when creating the tensor.
///
/// # Examples
/// ```
/// use nove::tensor::Device;
/// use nove::tensor::Tensor;
/// let device = Device::cpu();
///
/// // Create a tensor from a scalar
/// let tensor = Tensor::from_data(1.0f32, &device, false).unwrap();
/// println!("{:?}", tensor);
///
/// // Create a tensor from a 2D vector
/// let vec = vec![vec![1.0f32, 2.0f32, 3.0f32], vec![4.0f32, 5.0f32, 6.0f32]];
/// let tensor = Tensor::from_data(vec, &device, false).unwrap();
/// println!("{:?}", tensor);
///
/// // Create a tensor from a 2D slice
/// let slice = &[[1.0f32, 2.0f32, 3.0f32], [4.0f32, 5.0f32, 6.0f32]];
/// let tensor = Tensor::from_data(slice, &device, false).unwrap();
/// println!("{:?}", tensor);
/// ```
pub fn from_data<A>(data: A, device: &Device, grad_enabled: bool) -> Result<Self, TensorError>
where
A: candle_core::NdArray,
{
let inner = match grad_enabled {
true => TensorInner::Var(candle_core::Var::new(data, device)?),
false => TensorInner::Tensor(candle_core::Tensor::new(data, device)?),
};
Ok(Self {
data: Arc::new(RwLock::new(TensorData {
inner,
device: device.clone(),
parents: vec![],
grad: None,
name: None,
})),
})
}
/// Create a new tensor from a vector with the specified shape.
///
/// # Notes
/// * The element type of the data supported by this function includes `f32`, `f64`, `i64`, `u32`, `u8`.
/// * The total number of elements in the vector must match the product of the shape dimensions.
///
/// # Arguments
/// * `data` - The vector of data to create the tensor from.
/// * `shape` - The shape of the tensor.
/// * `device` - The device to place the tensor on.
/// * `grad_enabled` - Whether to enable gradient tracking for the tensor.
///
/// # Returns
/// * `Ok(tensor)` - The created tensor if successful.
/// * `Err(TensorError)` - The error when creating the tensor.
///
/// # Examples
/// ```
/// use nove::tensor::{Device, Shape, Tensor};
/// let device = Device::cpu();
///
/// let data = vec![1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0];
/// let shape = Shape::from(&[2, 3]);
/// let tensor = Tensor::from_vec(data, &shape, &device, false).unwrap();
/// println!("{:?}", tensor);
/// ```
pub fn from_vec<D>(
data: Vec<D>,
shape: &Shape,
device: &Device,
grad_enabled: bool,
) -> Result<Self, TensorError>
where
D: candle_core::WithDType,
{
let inner = match grad_enabled {
true => TensorInner::Var(candle_core::Var::from_vec(data, shape, device)?),
false => TensorInner::Tensor(candle_core::Tensor::from_vec(data, shape, device)?),
};
Ok(Self {
data: Arc::new(RwLock::new(TensorData {
inner,
device: device.clone(),
parents: vec![],
grad: None,
name: None,
})),
})
}
/// Create a new tensor from a slice with the specified shape.
///
/// # Notes
/// * The element type of the data supported by this function includes `f32`, `f64`, `i64`, `u32`, `u8`.
/// * The total number of elements in the slice must match the product of the shape dimensions.
///
/// # Arguments
/// * `data` - The slice of data to create the tensor from.
/// * `shape` - The shape of the tensor.
/// * `device` - The device to place the tensor on.
/// * `grad_enabled` - Whether to enable gradient tracking for the tensor.
///
/// # Returns
/// * `Ok(tensor)` - The created tensor if successful.
/// * `Err(TensorError)` - The error when creating the tensor.
///
/// # Examples
/// ```
/// use nove::tensor::{Device, Shape, Tensor};
/// let device = Device::cpu();
///
/// let data = [1.0f32, 2.0, 3.0, 4.0, 5.0, 6.0];
/// let shape = Shape::from(&[2, 3]);
/// let tensor = Tensor::from_slice(&data, &shape, &device, false).unwrap();
/// println!("{:?}", tensor);
/// ```
pub fn from_slice<D>(
data: &[D],
shape: &Shape,
device: &Device,
grad_enabled: bool,
) -> Result<Self, TensorError>
where
D: candle_core::WithDType,
{
let inner = match grad_enabled {
true => TensorInner::Var(candle_core::Var::from_slice(data, shape, device)?),
false => TensorInner::Tensor(candle_core::Tensor::from_slice(data, shape, device)?),
};
Ok(Self {
data: Arc::new(RwLock::new(TensorData {
inner,
device: device.clone(),
parents: vec![],
grad: None,
name: None,
})),
})
}
/// Create a new tensor from a scalar.
///
/// # Notes
/// * This function is an alias of `from_data` but accepts a scalar value with more explicit type.
/// * The type of the scalar supported by this function includes `f32`, `f64`, `i64`, `u32`, `u8`.
///
/// # Arguments
/// * `scalar` - The scalar to create the tensor from. It should be a single value not a vector or array.
/// * `device` - The device to place the tensor on.
/// * `grad_enabled` - Whether to enable gradient tracking for the tensor.
///
/// # Returns
/// * `Ok(tensor)` - The created tensor if successful.
/// * `Err(TensorError)` - The error when creating the tensor.
///
/// # Examples
/// ```
/// use nove::tensor::Device;
/// use nove::tensor::Tensor;
/// let device = Device::cpu();
///
/// let tensor = Tensor::from_scalar(1.0f32, &device, false).unwrap();
/// println!("{:?}", tensor);
/// ```
pub fn from_scalar<S>(
scalar: S,
device: &Device,
grad_enabled: bool,
) -> Result<Self, TensorError>
where
S: candle_core::NdArray + candle_core::WithDType,
{
Self::from_data(scalar, device, grad_enabled)
}
/// Convert the tensor to a scalar.
///
/// # Generic Type Parameters
/// * `S` - The element type of the scalar. It supports `f32`, `f64`, `i64`, `u32`, `u8`.
///
/// # Notes
/// * The tensor must only have one element.
///
/// # Returns
/// * `Ok(scalar)` - The scalar value if the tensor is a scalar.
/// * `Err(TensorError)` - The error when converting the tensor to a scalar.
///
/// # Examples
/// ```
/// use nove::tensor::Device;
/// use nove::tensor::Tensor;
/// let device = Device::cpu();
///
/// let tensor = Tensor::from_scalar(1.0f32, &device, false).unwrap();
/// println!("{:?}", tensor.to_scalar::<f32>().unwrap());
/// ```
pub fn to_scalar<S>(&self) -> Result<S, TensorError>
where
S: candle_core::WithDType,
{
let data = self.data.read()?;
let inner_tensor = match &data.inner {
TensorInner::Tensor(tensor) => tensor,
TensorInner::Var(var) => var,
};
// If the tensor dimension number is 0, it is a scalar tensor.
// We can directly convert it to a scalar.
// Otherwise, we need to squeeze the tensor to remove the dimension.
let num_dim = self.num_dim()?;
match num_dim {
0 => Ok(inner_tensor.to_scalar::<S>()?),
_ => {
let squeezed = inner_tensor.squeeze(0)?;
Ok(squeezed.to_scalar::<S>()?)
}
}
}
/// Convert the tensor to a one-dimensional vector.
///
/// # Generic Type Parameters
/// * `S` - The element type of the vector. It supports `f32`, `f64`, `i64`, `u32`, `u8`.
///
/// # Notes
/// * The tensor could be any shape, and it will be flattened to a one-dimensional vector.
///
/// # Returns
/// * `Ok(vec)` - The vector value if the tensor can be converted to a vector.
/// * `Err(TensorError)` - The error when converting the tensor to a vector.
///
/// # Examples
/// ```
/// use nove::tensor::Device;
/// use nove::tensor::Tensor;
/// let device = Device::cpu();
///
/// let tensor = Tensor::from_data(&[[1.0f64, 2.0f64, 3.0f64], [4.0f64, 5.0f64, 6.0f64]], &device, false).unwrap();
/// println!("{:?}", tensor.to_vec::<f64>().unwrap());
/// ```
///
pub fn to_vec<S>(&self) -> Result<Vec<S>, TensorError>
where
S: candle_core::WithDType,
{
let data = self.data.read()?;
let vec = match &data.inner {
TensorInner::Tensor(tensor) => tensor.flatten_all()?.to_vec1::<S>()?,
TensorInner::Var(var) => var.flatten_all()?.to_vec1::<S>()?,
};
Ok(vec)
}
/// Create a tensor from a candle tensor.
///
/// # Arguments
/// * `tensor` - The candle tensor to create the tensor from.
/// * `device` - The device to place the tensor on.
/// * `grad_enabled` - Whether to enable gradient tracking for the tensor.
///
/// # Returns
/// * `Ok(Self)` - The created tensor if successful.
/// * `Err(TensorError)` - The error when creating the tensor.
pub fn from_candle_tensor(
tensor: candle_core::Tensor,
device: &Device,
grad_enabled: bool,
) -> Result<Self, TensorError> {
let inner_tensor = tensor.copy()?.to_device(device)?;
let inner = match grad_enabled {
true => TensorInner::Var(candle_core::Var::from_tensor(&inner_tensor)?),
false => TensorInner::Tensor(inner_tensor.clone()),
};
Ok(Self {
data: Arc::new(RwLock::new(TensorData {
inner,
device: device.clone(),
parents: vec![],
grad: None,
name: None,
})),
})
}
/// Convert the tensor to a `candle_core::Tensor`.
///
/// # Returns
/// * `Ok(candle_core::Tensor)` - The `candle_core::Tensor` if successful.
/// * `Err(TensorError)` - The error when converting the tensor to a `candle_core::Tensor`.
pub fn to_candle_tensor(&self) -> Result<candle_core::Tensor, TensorError> {
let data = self.data.read()?;
let tensor = match &data.inner {
TensorInner::Tensor(tensor) => tensor.copy()?,
TensorInner::Var(var) => var.as_tensor().copy()?,
};
Ok(tensor)
}
/// Convert the tensor to a `candle_core::Var`.
///
/// # Returns
/// * `Ok(candle_core::Var)` - The `candle_core::Var` if successful.
/// * `Err(TensorError)` - The error when converting the tensor to a `candle_core::Var`.
pub fn to_candle_var(&self) -> Result<candle_core::Var, TensorError> {
let data = self.data.read()?;
match &data.inner {
TensorInner::Tensor(tensor) => Ok(candle_core::Var::from_tensor(&tensor.copy()?)?),
TensorInner::Var(var) => Ok(candle_core::Var::from_tensor(&var.as_tensor().copy()?)?),
}
}
}