-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfactory.rs
More file actions
290 lines (258 loc) · 9.95 KB
/
Copy pathfactory.rs
File metadata and controls
290 lines (258 loc) · 9.95 KB
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
//! Factory methods to create [`Tensor`].
//!
//! Pytorch puts these in the module, as torch.zeros(), I chose to put them as static methods on Tensor.
use crate::{StableTorchResult, Tensor, TensorAccess, dtype::DType};
use torch_stable::aoti_torch::{aoti_torch_zero_, AtenTensorHandle};
use torch_stable::headeronly::core::{Layout, MemoryFormat};
use torch_stable::stable::device::Device;
use torch_stable::{
aoti_torch::StableIValue, stable::tensor::Tensor as StableTensor, unsafe_call_bail,
unsafe_call_dispatch_bail,
};
/// Options for the `to` operation.
///
/// The types [`Device`], [`DType`], [`Layout`] and [`MemoryFormat`] implement [`std::convert::From`] for this struct.
///
/// This means that you can do:
/// ```rust
/// # use flash_powder::prelude::*;
/// # use flash_powder::{StableTorchResult, Tensor};
/// # use flash_powder as fp;
/// # fn foo() -> StableTorchResult<()>{
/// let t = Tensor::zeros(&[3,3], &fp::DType::U8.into())?;
/// # Ok(())
/// # }
/// ```
/// If you want to populate two fields at the same time you still need to create the struct manually.
#[derive(Copy, Clone, Debug, Default)]
pub struct ToOptions {
pub dtype: Option<DType>,
pub layout: Option<Layout>,
pub device: Option<Device>,
pub pin_memory: Option<bool>,
pub memory_format: Option<MemoryFormat>,
pub non_blocking: bool,
pub copy: bool,
}
macro_rules! impl_conversion {
($t:ty, $dest:ty, $v:ident) => {
impl std::convert::From<$t> for $dest {
fn from(value: $t) -> Self {
Self{$v: Some(value), ..Default::default()}
}
}
};
}
impl_conversion!(Device, ToOptions, device);
impl_conversion!(DType, ToOptions, dtype);
impl_conversion!(Layout, ToOptions, layout);
impl_conversion!(MemoryFormat, ToOptions, memory_format);
/// Options for empty.
///
/// The types [`Device`], [`DType`], [`Layout`] and [`MemoryFormat`] implement [`std::convert::From`] for this struct.
#[derive(Copy, Clone, Debug, Default)]
pub struct EmptyOptions {
pub dtype: Option<DType>,
pub layout: Option<Layout>,
pub device: Option<Device>,
pub pin_memory: Option<bool>,
pub memory_format: Option<MemoryFormat>,
}
impl_conversion!(Device, EmptyOptions, device);
impl_conversion!(DType, EmptyOptions, dtype);
impl_conversion!(Layout, EmptyOptions, layout);
impl_conversion!(MemoryFormat, EmptyOptions, memory_format);
/// Options to create various tensors.
///
/// The types [`Device`], [`DType`], [`Layout`] implement [`std::convert::From`] for this struct.
#[derive(Copy, Clone, Debug, Default)]
pub struct TensorOptions {
pub dtype: Option<DType>,
pub layout: Option<Layout>,
pub device: Option<Device>,
pub pin_memory: Option<bool>,
}
impl_conversion!(Device, TensorOptions, device);
impl_conversion!(DType, TensorOptions, dtype);
impl_conversion!(Layout, TensorOptions, layout);
/// Native functions that produce owned tensors.
///
/// See the [`factory`][crate::factory] module for description of this trait's functionality.
/// This trait is only implemented for [`Tensor`].
///
/// ```
/// # use flash_powder::prelude::*;
/// # use flash_powder::Tensor;
/// let a = Tensor::empty(&[5, 5], &Default::default()).unwrap();
/// assert_eq!(a.sizes(), &[5, 5]);
/// ```
pub trait TensorFactory {
/// A new empty vector
///
///
/// - [native_functions.yaml](https://github.com/pytorch/pytorch/blob/v2.12.0-rc2/aten/src/ATen/native/native_functions.yaml#L2425)
/// - [pytorch equivalent](https://docs.pytorch.org/docs/2.11/generated/torch.empty.html#torch.empty)
///
fn empty(dimensions: &[usize], options: &EmptyOptions) -> StableTorchResult<Tensor> {
let mut stack: [StableIValue; 6] = [
(dimensions).into(),
(&options.dtype).into(),
(&options.layout).into(),
(&options.device).into(),
(&options.pin_memory).into(),
(&options.memory_format).into(),
];
// https://github.com/pytorch/pytorch/blob/v2.11.0/aten/src/ATen/native/native_functions.yaml#L2424
unsafe_call_dispatch_bail!("aten::empty", "memory_format", stack.as_mut_slice());
let r: StableTensor = stack[0].try_into()?;
unsafe_call_bail!(aoti_torch_zero_(r.get()));
Ok(Tensor::new(r))
}
/// A new zeros vector
///
///
///
/// ```rust
/// # use flash_powder::prelude::*;
/// # use flash_powder::{StableTorchResult, Tensor};
/// # use flash_powder as fp;
/// # fn foo() -> StableTorchResult<()>{
/// let t = Tensor::zeros(&[3,3], &fp::DType::U8.into())?;
/// # Ok(())
/// # }
/// ```
///
/// - [native_functions.yaml](https://github.com/pytorch/pytorch/blob/v2.12.0-rc2/aten/src/ATen/native/native_functions.yaml#L6837)
/// - [pytorch equivalent](https://docs.pytorch.org/docs/2.11/generated/torch.zeros.html)
///
//
// https://github.com/pytorch/pytorch/blob/v2.11.0/aten/src/ATen/native/native_functions.yaml#L6800
fn zeros(dimensions: &[usize], options: &TensorOptions) -> StableTorchResult<Tensor> {
let mut stack: [StableIValue; 5] = [
(dimensions).into(),
(&options.dtype).into(),
(&options.layout).into(),
(&options.device).into(),
(&options.pin_memory).into(),
];
unsafe_call_dispatch_bail!("aten::zeros", "", stack.as_mut_slice());
let r: StableTensor = stack[0].try_into()?;
Ok(Tensor::new(r))
}
/// A new ones vector
///
///
///
/// ```rust
/// # use flash_powder::prelude::*;
/// # use flash_powder::{StableTorchResult, Tensor};
/// # use flash_powder as fp;
/// # fn foo() -> StableTorchResult<()>{
/// let t = Tensor::ones(&[3,3], &fp::DType::U8.into())?;
/// # Ok(())
/// # }
/// ```
///
/// - [native_functions.yaml](https://github.com/pytorch/pytorch/blob/v2.12.0/aten/src/ATen/native/native_functions.yaml#L4621)
/// - [pytorch equivalent](https://docs.pytorch.org/docs/2.12/generated/torch.ones.html)
///
//
// https://github.com/pytorch/pytorch/blob/v2.11.0/aten/src/ATen/native/native_functions.yaml#L6800
fn ones(dimensions: &[usize], options: &TensorOptions) -> StableTorchResult<Tensor> {
let mut stack: [StableIValue; 5] = [
(dimensions).into(),
(&options.dtype).into(),
(&options.layout).into(),
(&options.device).into(),
(&options.pin_memory).into(),
];
unsafe_call_dispatch_bail!("aten::ones", "", stack.as_mut_slice());
let r: StableTensor = stack[0].try_into()?;
Ok(Tensor::new(r))
}
/// A new randn tensor
///
///
/// - [native_functions.yaml](https://github.com/pytorch/pytorch/blob/v2.12.0-rc2/aten/src/ATen/native/native_functions.yaml#L4963)
/// - [pytorch equivalent](https://docs.pytorch.org/docs/2.12/generated/torch.randn.html)
///
fn randn(dimensions: &[usize], options: &TensorOptions) -> StableTorchResult<Tensor> {
let mut stack: [StableIValue; 5] = [
(dimensions).into(),
(&options.dtype).into(),
(&options.layout).into(),
(&options.device).into(),
(&options.pin_memory).into(),
];
unsafe_call_dispatch_bail!("aten::randn", "", stack.as_mut_slice());
let r: StableTensor = stack[0].try_into()?;
Ok(Tensor::new(r))
}
fn from_f32(value: f32) -> StableTorchResult<Tensor> {
let mut handle_res: AtenTensorHandle = std::ptr::null_mut();
unsafe_call_bail!(
torch_stable::aoti_torch::aoti_torch_scalar_to_tensor_float32(value, &mut handle_res)
);
Ok(Tensor::new(StableTensor::from_handle(handle_res)))
}
/// Concatenates the given sequence of tensors in tensors in the given dimension
///
/// - [native_functions.yaml](https://github.com/pytorch/pytorch/blob/v2.12.0-rc2/aten/src/ATen/native/native_functions.yaml#L1433)
/// - [pytorch equivalent](https://docs.pytorch.org/docs/2.11/generated/torch.cat.html)
fn cat<T>(tensors: &[&T], dim: usize) -> StableTorchResult<Tensor>
where
T: TensorAccess,
{
let mut stack: [StableIValue; 2] =
[tensors.iter().map(|z| z.get_tensor()).collect(), dim.into()];
unsafe_call_dispatch_bail!("aten::cat", "", stack.as_mut_slice());
let r: StableTensor = stack[0].try_into()?;
Ok(Tensor::new(r))
}
}
impl TensorFactory for Tensor {}
#[cfg(test)]
mod test {
use super::*;
use crate::prelude::*;
#[test]
fn test_flash_powder_randn() -> StableTorchResult<()> {
let d = Tensor::randn(&[1000, 1000], &Default::default())?;
assert_eq!(d.sizes(), &[1000, 1000]);
let mean = d.mean(&Default::default())?;
let value = mean.f32s_ref()?[0];
assert!(value.abs() < 0.01);
Ok(())
}
#[test]
fn test_flash_powder_cat() -> StableTorchResult<()> {
/*
#|PYTHON
x = torch.tensor([[1.0, 2.0],[3.0, 4.0]], dtype=torch.float)
*/
let d = Tensor::from([[1.0f32, 2.0], [3.0, 4.0]])?;
assert_eq!(d.sizes(), &[2, 2]); // #PYTHON list(x.shape)
assert_eq!(d.f32s_ref()?, &[1.0f32, 2.0, 3.0, 4.0]); // #PYTHON list(x.view(-1).tolist())
/*
#|PYTHON
a = torch.cat([x,x,x], 0)
*/
let a = Tensor::cat(&[&d, &d, &d], 0)?;
assert_eq!(a.sizes(), &[6, 2]); // #PYTHON list(a.shape)
assert_eq!(
a.f32s_ref()?,
&[1.0f32, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0, 4.0, 1.0, 2.0, 3.0, 4.0]
); // #PYTHON list(a.view(-1).tolist())
/*
#|PYTHON
b = torch.cat([x,x,x], 1)
*/
let b = Tensor::cat(&[&d, &d, &d], 1)?;
assert_eq!(b.sizes(), &[2, 6]); // #PYTHON list(b.shape)
assert_eq!(
b.f32s_ref()?,
&[1.0f32, 2.0, 1.0, 2.0, 1.0, 2.0, 3.0, 4.0, 3.0, 4.0, 3.0, 4.0]
); // #PYTHON list(b.view(-1).tolist())
Ok(())
}
}