forked from coreylowman/dfdx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathselect.rs
291 lines (254 loc) · 9.05 KB
/
select.rs
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
//! Implementations of selecting either 1 or Z elements from an axis of an nd array.
//!
//! # Implementation Details
//! There are four cases to handle:
//!
//! ## Selecting 1 element from the 0th axis [select_modes::Index]
//!
//! Just index into input using the single index and assign to output.
//!
//! ## Selecting Z elements from the 0th axis [select_modes::Index]
//!
//! Just index into input for each index and assing to `output[z]`
//!
//! ## Selecting either 1 or Z elements from a non-zero axis [select_modes::Recurse]
//!
//! Then all three arrays with have the same dimension as the 0th axis.
//! Do a for loop over the 0th axis and recurse!
//!
//! ## Broadcasted select [select_modes::Broadcast]
//!
//! In this case only the indices & output are indexed. The input is broadcasted by
//! not indexing into it.
use super::{Cpu, ForEachElement};
use crate::arrays::CountElements;
/// Used to disambiguate trait implementations. Callees
/// must specify what kind of selection is occurring.
pub(crate) mod select_modes {
use std::marker::PhantomData;
/// Select the current axis.
pub struct Index;
/// Recurse the current axis.
pub struct Recurse<M>(PhantomData<*const M>);
/// Broadcast the current axis of input and recurse the indices.
pub struct Broadcast<M>(PhantomData<*const M>);
}
use select_modes::{Broadcast, Index, Recurse};
pub(crate) type SelectAx0 = select_modes::Index;
pub(crate) type SelectAx1 = select_modes::Recurse<SelectAx0>;
pub(crate) type SelectAx2 = select_modes::Recurse<SelectAx1>;
pub(crate) type SelectAx3 = select_modes::Recurse<SelectAx2>;
pub(crate) type BSelectAx1 = select_modes::Broadcast<SelectAx0>;
/// Select values from `T` using indices `I`. `Mode` is used to disambiguate the impl.
pub trait DeviceSelect<T, I, Mode> {
type Result;
/// Equivalent to psuedocode `out = inp[indices]`
fn select_axis(inp: &T, indices: &I, out: &mut Self::Result);
/// `inp[indices] += out`
fn select_add(inp: &mut T, indices: &I, out: &Self::Result);
}
// Select 1 element from 0th axis.
impl<T, const M: usize> DeviceSelect<[T; M], usize, Index> for Cpu
where
Self: ForEachElement<T>,
T: Copy + CountElements,
T::Dtype: for<'a> std::ops::AddAssign<&'a T::Dtype>,
{
type Result = T;
fn select_axis(inp: &[T; M], indices: &usize, out: &mut Self::Result) {
*out = inp[*indices];
}
fn select_add(inp: &mut [T; M], indices: &usize, out: &Self::Result) {
Self::foreach_mr(&mut inp[*indices], out, &mut |a, b| *a += b);
}
}
// Select Z elements from 0th axis.
impl<T, const M: usize, const Z: usize> DeviceSelect<[T; M], [usize; Z], Index> for Cpu
where
Self: ForEachElement<T>,
T: Copy + CountElements,
T::Dtype: for<'a> std::ops::AddAssign<&'a T::Dtype>,
{
type Result = [T; Z];
fn select_axis(inp: &[T; M], indices: &[usize; Z], out: &mut Self::Result) {
for z in 0..Z {
out[z] = inp[indices[z]];
}
}
fn select_add(inp: &mut [T; M], indices: &[usize; Z], out: &Self::Result) {
for z in 0..Z {
Self::foreach_mr(&mut inp[indices[z]], &out[z], &mut |a, b| *a += b);
}
}
}
// Select elements from non-zero axis
impl<T, I, const M: usize, SubMode> DeviceSelect<[T; M], [I; M], Recurse<SubMode>> for Cpu
where
Self: DeviceSelect<T, I, SubMode>,
{
type Result = [<Self as DeviceSelect<T, I, SubMode>>::Result; M];
fn select_axis(inp: &[T; M], indices: &[I; M], out: &mut Self::Result) {
for m in 0..M {
Self::select_axis(&inp[m], &indices[m], &mut out[m]);
}
}
fn select_add(inp: &mut [T; M], indices: &[I; M], out: &Self::Result) {
for m in 0..M {
Self::select_add(&mut inp[m], &indices[m], &out[m]);
}
}
}
// Broadcast select elements from non-zero axis.
impl<T, I, const M: usize, SubMode> DeviceSelect<T, [I; M], Broadcast<SubMode>> for Cpu
where
Self: DeviceSelect<T, I, SubMode>,
{
type Result = [<Self as DeviceSelect<T, I, SubMode>>::Result; M];
fn select_axis(inp: &T, indices: &[I; M], out: &mut Self::Result) {
for m in 0..M {
Self::select_axis(inp, &indices[m], &mut out[m]);
}
}
fn select_add(inp: &mut T, indices: &[I; M], out: &Self::Result) {
for m in 0..M {
Self::select_add(inp, &indices[m], &out[m]);
}
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::arrays::ZeroElements;
#[test]
fn test_select_1d_0() {
let a: [f32; 3] = [1.0, 2.0, 3.0];
let mut b: f32 = ZeroElements::ZEROS;
Cpu::select_axis(&a, &1, &mut b);
assert_eq!(b, 2.0);
}
#[test]
fn test_select_1d_0z() {
let a: [f32; 3] = [1.0f32, 2.0, 3.0];
let mut b: [f32; 6] = ZeroElements::ZEROS;
<Cpu as DeviceSelect<_, _, Index>>::select_axis(&a, &[0, 1, 2, 2, 1, 0], &mut b);
assert_eq!(b, [1.0, 2.0, 3.0, 3.0, 2.0, 1.0]);
}
const A_2D: [[f32; 3]; 2] = [[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]];
#[test]
fn test_select_2d_0() {
let a = A_2D;
let mut b: [f32; 3] = ZeroElements::ZEROS;
Cpu::select_axis(&a, &0, &mut b);
assert_eq!(b, [1.0, 2.0, 3.0]);
}
#[test]
fn test_select_2d_0z() {
let a = A_2D;
let mut b: [[f32; 3]; 3] = ZeroElements::ZEROS;
<Cpu as DeviceSelect<_, _, Index>>::select_axis(&a, &[0, 0, 1], &mut b);
assert_eq!(b, [a[0], a[0], a[1]]);
}
#[test]
fn test_select_2d_1() {
let a = A_2D;
let mut b: [f32; 2] = ZeroElements::ZEROS;
<Cpu as DeviceSelect<_, _, Recurse<Index>>>::select_axis(&a, &[0, 1], &mut b);
assert_eq!(b, [1.0, 5.0]);
}
#[test]
fn test_select_2d_1z() {
let a = A_2D;
let mut b: [[f32; 2]; 2] = ZeroElements::ZEROS;
<Cpu as DeviceSelect<_, _, Recurse<Index>>>::select_axis(&a, &[[0, 2], [1, 1]], &mut b);
assert_eq!(b, [[1.0, 3.0], [5.0, 5.0]]);
}
#[test]
fn test_select_broadcast_2d() {
let a = [[1.0], [2.0]];
let i: [[usize; 3]; 4] = [[0, 1, 0], [1, 1, 1], [0, 0, 0], [1, 0, 1]];
let mut b: [[[f32; 1]; 3]; 4] = ZeroElements::ZEROS;
<Cpu as DeviceSelect<_, _, Broadcast<Index>>>::select_axis(&a, &i, &mut b);
#[rustfmt::skip]
assert_eq!(b, [[[1.], [2.], [1.]], [[2.], [2.], [2.]], [[1.], [1.], [1.]], [[2.], [1.], [2.]]]);
}
#[test]
fn test_select_add_2d() {
let mut a = [[0.0; 3]; 2];
let b = [[1.0, 3.0], [5.0, 5.0]];
let i = [[0, 2], [1, 1]];
<Cpu as DeviceSelect<_, _, Recurse<Index>>>::select_add(&mut a, &i, &b);
assert_eq!(a, [[1.0, 0.0, 3.0], [0.0, 10.0, 0.0]]);
}
const A_3D: [[[f32; 3]; 2]; 4] = [
[[1.0, 2.0, 3.0], [4.0, 5.0, 6.0]],
[[-1.0, 2.0, 3.0], [4.0, -5.0, 6.0]],
[[1.0, -2.0, 3.0], [-4.0, 5.0, -6.0]],
[[1.0, 2.0, -3.0], [-4.0, -5.0, -6.0]],
];
#[test]
fn test_select_3d_0() {
let a = A_3D;
let mut b: [[f32; 3]; 2] = ZeroElements::ZEROS;
Cpu::select_axis(&a, &0, &mut b);
assert_eq!(b, A_3D[0]);
}
#[test]
fn test_select_3d_0z() {
let a = A_3D;
let mut b: [[[f32; 3]; 2]; 6] = ZeroElements::ZEROS;
<Cpu as DeviceSelect<_, _, Index>>::select_axis(&a, &[0, 0, 1, 2, 3, 3], &mut b);
assert_eq!(b, [A_3D[0], A_3D[0], A_3D[1], A_3D[2], A_3D[3], A_3D[3]]);
}
#[test]
fn test_select_3d_1() {
let a = A_3D;
let mut b: [[f32; 3]; 4] = ZeroElements::ZEROS;
<Cpu as DeviceSelect<_, _, Recurse<Index>>>::select_axis(&a, &[0, 0, 1, 1], &mut b);
assert_eq!(b, [A_3D[0][0], A_3D[1][0], A_3D[2][1], A_3D[3][1]]);
}
#[test]
fn test_select_3d_1z() {
let a = A_3D;
let mut b: [[[f32; 3]; 1]; 4] = ZeroElements::ZEROS;
<Cpu as DeviceSelect<_, _, Recurse<Index>>>::select_axis(&a, &[[0], [0], [1], [1]], &mut b);
assert_eq!(b, [[A_3D[0][0]], [A_3D[1][0]], [A_3D[2][1]], [A_3D[3][1]]]);
}
#[test]
fn test_select_3d_2() {
let a = A_3D;
let mut b: [[f32; 2]; 4] = ZeroElements::ZEROS;
<Cpu as DeviceSelect<_, _, Recurse<Recurse<Index>>>>::select_axis(
&a,
&[[1, 0], [0, 1], [0, 0], [1, 1]],
&mut b,
);
assert_eq!(
b,
[
[A_3D[0][0][1], A_3D[0][1][0]],
[A_3D[1][0][0], A_3D[1][1][1]],
[A_3D[2][0][0], A_3D[2][1][0]],
[A_3D[3][0][1], A_3D[3][1][1]],
]
);
}
#[test]
fn test_select_3d_2z() {
let a = A_3D;
let mut b: [[[f32; 1]; 2]; 4] = ZeroElements::ZEROS;
<Cpu as DeviceSelect<_, _, Recurse<Recurse<Index>>>>::select_axis(
&a,
&[[[1], [0]], [[0], [1]], [[0], [0]], [[1], [1]]],
&mut b,
);
assert_eq!(
b,
[
[[A_3D[0][0][1]], [A_3D[0][1][0]]],
[[A_3D[1][0][0]], [A_3D[1][1][1]]],
[[A_3D[2][0][0]], [A_3D[2][1][0]]],
[[A_3D[3][0][1]], [A_3D[3][1][1]]],
]
);
}
}