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
use std::sync::{Arc, RwLock};
use crate::{
DType, Device, Shape, Tensor, TensorError,
tensor::{TensorData, TensorInner},
};
impl Tensor {
/// Deep clone the data from this tensor to a new tensor.
///
/// # Notes
/// * Because the cloned tensor is a new tensor, it will not be connected to the previous computation graph.
///
/// # Returns
/// * `Ok(tensor)` - The cloned tensor if successful.
/// * `Err(TensorError)` - The error when cloning the tensor.
pub fn deep_clone(&self) -> Result<Self, TensorError> {
let inner = match &self.data.read()?.inner {
TensorInner::Tensor(tensor) => TensorInner::Tensor(tensor.copy()?),
TensorInner::Var(var) => TensorInner::Var(candle_core::Var::from_tensor(&var.copy()?)?),
};
let device = self.data.read()?.device.clone();
let grad = if let Some(grad) = self.data.read()?.grad.as_ref() {
Some(grad.deep_clone()?)
} else {
None
};
Ok(Self {
data: Arc::new(RwLock::new(TensorData {
inner,
device,
grad,
parents: vec![],
name: None,
})),
})
}
/// Create a new tensor with random values uniformly distributed in the specified range.
///
/// # Parameters
/// * `low` - The lower bound of the uniform distribution.
/// * `high` - The upper bound of the uniform distribution.
/// * `shape` - The shape of the tensor.
/// * `device` - The device to store the tensor.
/// * `grad_enabled` - Whether to enable gradient tracking for the tensor.
///
/// # Returns
/// * `Ok(Tensor)` - The new tensor if successful.
/// * `Err(TensorError)` - The error when creating the tensor.
pub fn rand<T>(
low: T,
high: T,
shape: &Shape,
device: &Device,
grad_enabled: bool,
) -> Result<Self, TensorError>
where
T: candle_core::FloatDType,
{
let inner = match grad_enabled {
true => TensorInner::Var(candle_core::Var::rand(low, high, shape, device)?),
false => TensorInner::Tensor(candle_core::Tensor::rand(low, high, shape, device)?),
};
Ok(Self {
data: Arc::new(RwLock::new(TensorData {
inner,
device: device.clone(),
parents: vec![],
grad: None,
name: None,
})),
})
}
/// Create a new tensor with random values normally distributed with mean `mean` and standard deviation `std`.
///
/// # Parameters
/// * `mean` - The mean of the normal distribution.
/// * `std` - The standard deviation of the normal distribution.
/// * `shape` - The shape of the tensor.
/// * `device` - The device to store the tensor.
/// * `grad_enabled` - Whether to enable gradient tracking for the tensor.
///
/// # Returns
/// * `Ok(Tensor)` - The new tensor if successful.
/// * `Err(TensorError)` - The error when creating the tensor.
pub fn randn<T>(
mean: T,
std: T,
shape: &Shape,
device: &Device,
grad_enabled: bool,
) -> Result<Self, TensorError>
where
T: candle_core::FloatDType,
{
let inner = match grad_enabled {
true => TensorInner::Var(candle_core::Var::randn(mean, std, shape, device)?),
false => TensorInner::Tensor(candle_core::Tensor::randn(mean, std, shape, device)?),
};
Ok(Self {
data: Arc::new(RwLock::new(TensorData {
inner,
device: device.clone(),
parents: vec![],
grad: None,
name: None,
})),
})
}
/// Create a new tensor filled with zeros.
///
/// # Parameters
/// * `shape` - The shape of the tensor.
/// * `dtype` - The data type of the tensor.
/// * `device` - The device to store the tensor.
/// * `grad_enabled` - Whether to enable gradient tracking for the tensor.
///
/// # Returns
/// * `Ok(Tensor)` - The new tensor if successful.
/// * `Err(TensorError)` - The error when creating the tensor.
pub fn zeros(
shape: &Shape,
dtype: &DType,
device: &Device,
grad_enabled: bool,
) -> Result<Self, TensorError> {
let inner = match grad_enabled {
true => TensorInner::Var(candle_core::Var::zeros(shape, *dtype, device)?),
false => TensorInner::Tensor(candle_core::Tensor::zeros(shape, *dtype, device)?),
};
Ok(Self {
data: Arc::new(RwLock::new(TensorData {
inner,
device: device.clone(),
parents: vec![],
grad: None,
name: None,
})),
})
}
}