coeus-ops 0.9.0

Numerical tensor operators for Coeus.
Documentation
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
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
use crate::ptr::{MutPtr, Ptr};
use coeus_core::{Backend, CpuAddressableStorage, CpuAddressableStorageMut, Layout, Scalar};

#[inline]
pub(crate) fn max_pool2d<T: Scalar, B: Backend>(
    backend: &B,
    input: &B::DeviceBuffer<T>,
    input_layout: &Layout,
    kernel_size: usize,
    stride: usize,
    padding: usize,
    dilation: usize,
    output: &mut B::DeviceBuffer<T>,
    output_layout: &Layout,
) where
    B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
    let n = input_layout.shape()[0];
    let c = input_layout.shape()[1];
    let h = input_layout.shape()[2];
    let w = input_layout.shape()[3];
    let h_out = output_layout.shape()[2];
    let w_out = output_layout.shape()[3];
    let out_numel = n * c * h_out * w_out;

    let input_slice = input.as_slice();
    let output_slice = output.as_mut_slice();

    let input_ptr = Ptr(input_slice.as_ptr());
    let output_ptr = MutPtr(output_slice.as_mut_ptr());

    let input_layout = input_layout.clone();
    let output_layout = output_layout.clone();

    let pad_s = padding as isize;
    let stride_s = stride as isize;
    let dil_s = dilation as isize;
    let k_size = kernel_size;

    backend.parallel_for(0, out_numel, move |i| {
        let ow = i % w_out;
        let temp1 = i / w_out;
        let oh = temp1 % h_out;
        let temp2 = temp1 / h_out;
        let ci = temp2 % c;
        let ni = temp2 / c;

        let mut max_val: Option<T> = None;

        for ikh in 0..k_size {
            let h_in = oh as isize * stride_s + ikh as isize * dil_s - pad_s;
            if h_in >= 0 && (h_in as usize) < h {
                for ikw in 0..k_size {
                    let w_in = ow as isize * stride_s + ikw as isize * dil_s - pad_s;
                    if w_in >= 0 && (w_in as usize) < w {
                        let input_idx =
                            input_layout.physical_index(&[ni, ci, h_in as usize, w_in as usize]);
                        let val = unsafe { input_ptr.read(input_idx) };
                        max_val = Some(match max_val {
                            None => val,
                            Some(m) => {
                                if val > m {
                                    val
                                } else {
                                    m
                                }
                            }
                        });
                    }
                }
            }
        }

        let output_idx = output_layout.physical_index(&[ni, ci, oh, ow]);
        unsafe {
            output_ptr.write(output_idx, max_val.unwrap_or(T::zero()));
        }
    });
}

#[inline]
pub(crate) fn max_pool2d_backward<T: Scalar, B: Backend>(
    backend: &B,
    grad_out: &B::DeviceBuffer<T>,
    grad_out_layout: &Layout,
    input: &B::DeviceBuffer<T>,
    input_layout: &Layout,
    kernel_size: usize,
    stride: usize,
    padding: usize,
    dilation: usize,
    grad_input: &mut B::DeviceBuffer<T>,
    grad_input_layout: &Layout,
) where
    B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
    let n = grad_input_layout.shape()[0];
    let c = grad_input_layout.shape()[1];
    let h = grad_input_layout.shape()[2];
    let w = grad_input_layout.shape()[3];
    let h_out = grad_out_layout.shape()[2];
    let w_out = grad_out_layout.shape()[3];
    let numel_in = n * c * h * w;

    let go_slice = grad_out.as_slice();
    let gi_slice = grad_input.as_mut_slice();
    let inp_slice = input.as_slice();

    let go_ptr = Ptr(go_slice.as_ptr());
    let gi_ptr = MutPtr(gi_slice.as_mut_ptr());
    let inp_ptr = Ptr(inp_slice.as_ptr());

    let go_layout = grad_out_layout.clone();
    let gi_layout = grad_input_layout.clone();
    let inp_layout = input_layout.clone();

    let pad_s = padding as isize;
    let stride_s = stride as isize;
    let dil_s = dilation as isize;
    let k_size = kernel_size;

    backend.parallel_for(0, numel_in, move |i| {
        let wi = i % w;
        let temp1 = i / w;
        let hi = temp1 % h;
        let temp2 = temp1 / h;
        let ci = temp2 % c;
        let ni = temp2 / c;

        let my_idx = inp_layout.physical_index(&[ni, ci, hi, wi]);
        let my_val = unsafe { inp_ptr.read(my_idx) };

        let mut sum = T::zero();

        for ikh in 0..k_size {
            let numer_h = hi as isize + pad_s - ikh as isize * dil_s;
            if numer_h >= 0 && numer_h % stride_s == 0 {
                let oh = (numer_h / stride_s) as usize;
                if oh < h_out {
                    for ikw in 0..k_size {
                        let numer_w = wi as isize + pad_s - ikw as isize * dil_s;
                        if numer_w >= 0 && numer_w % stride_s == 0 {
                            let ow = (numer_w / stride_s) as usize;
                            if ow < w_out {
                                let mut max_val: Option<T> = None;
                                let mut max_h: usize = 0;
                                let mut max_w: usize = 0;

                                for jkh in 0..k_size {
                                    let h_in =
                                        oh as isize * stride_s + jkh as isize * dil_s - pad_s;
                                    if h_in >= 0 && (h_in as usize) < h {
                                        for jkw in 0..k_size {
                                            let w_in = ow as isize * stride_s
                                                + jkw as isize * dil_s
                                                - pad_s;
                                            if w_in >= 0 && (w_in as usize) < w {
                                                let input_idx = inp_layout.physical_index(&[
                                                    ni,
                                                    ci,
                                                    h_in as usize,
                                                    w_in as usize,
                                                ]);
                                                let val = unsafe { inp_ptr.read(input_idx) };
                                                let is_new_max = match max_val {
                                                    None => true,
                                                    Some(m) => val > m,
                                                };
                                                if is_new_max {
                                                    max_val = Some(val);
                                                    max_h = h_in as usize;
                                                    max_w = w_in as usize;
                                                }
                                            }
                                        }
                                    }
                                }

                                if max_val.is_some()
                                    && max_h == hi
                                    && max_w == wi
                                    && my_val == max_val.unwrap()
                                {
                                    let go_idx = go_layout.physical_index(&[ni, ci, oh, ow]);
                                    let gval = unsafe { go_ptr.read(go_idx) };
                                    sum += gval;
                                }
                            }
                        }
                    }
                }
            }
        }

        let gi_idx = gi_layout.physical_index(&[ni, ci, hi, wi]);
        unsafe {
            let old = gi_ptr.read(gi_idx);
            gi_ptr.write(gi_idx, old + sum);
        }
    });
}

#[inline]
pub(crate) fn max_pool3d<T: Scalar, B: Backend>(
    backend: &B,
    input: &B::DeviceBuffer<T>,
    input_layout: &Layout,
    kernel_size: usize,
    stride: usize,
    padding: usize,
    dilation: usize,
    output: &mut B::DeviceBuffer<T>,
    output_layout: &Layout,
) where
    B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
    let n = input_layout.shape()[0];
    let c = input_layout.shape()[1];
    let d = input_layout.shape()[2];
    let h = input_layout.shape()[3];
    let w = input_layout.shape()[4];
    let d_out = output_layout.shape()[2];
    let h_out = output_layout.shape()[3];
    let w_out = output_layout.shape()[4];
    let out_numel = n * c * d_out * h_out * w_out;

    let input_slice = input.as_slice();
    let output_slice = output.as_mut_slice();

    let input_ptr = Ptr(input_slice.as_ptr());
    let output_ptr = MutPtr(output_slice.as_mut_ptr());

    let input_layout = input_layout.clone();
    let output_layout = output_layout.clone();

    let pad_s = padding as isize;
    let stride_s = stride as isize;
    let dil_s = dilation as isize;
    let k_size = kernel_size;

    backend.parallel_for(0, out_numel, move |i| {
        let ow = i % w_out;
        let temp1 = i / w_out;
        let oh = temp1 % h_out;
        let temp2 = temp1 / h_out;
        let od = temp2 % d_out;
        let temp3 = temp2 / d_out;
        let ci = temp3 % c;
        let ni = temp3 / c;

        let mut max_val: Option<T> = None;

        for ikd in 0..k_size {
            let d_in = od as isize * stride_s + ikd as isize * dil_s - pad_s;
            if d_in >= 0 && (d_in as usize) < d {
                for ikh in 0..k_size {
                    let h_in = oh as isize * stride_s + ikh as isize * dil_s - pad_s;
                    if h_in >= 0 && (h_in as usize) < h {
                        for ikw in 0..k_size {
                            let w_in = ow as isize * stride_s + ikw as isize * dil_s - pad_s;
                            if w_in >= 0 && (w_in as usize) < w {
                                let input_idx = input_layout.physical_index(&[
                                    ni,
                                    ci,
                                    d_in as usize,
                                    h_in as usize,
                                    w_in as usize,
                                ]);
                                let val = unsafe { input_ptr.read(input_idx) };
                                max_val = Some(match max_val {
                                    None => val,
                                    Some(m) => {
                                        if val > m {
                                            val
                                        } else {
                                            m
                                        }
                                    }
                                });
                            }
                        }
                    }
                }
            }
        }

        let output_idx = output_layout.physical_index(&[ni, ci, od, oh, ow]);
        unsafe {
            output_ptr.write(output_idx, max_val.unwrap_or(T::zero()));
        }
    });
}

#[inline]
pub(crate) fn max_pool3d_backward<T: Scalar, B: Backend>(
    backend: &B,
    grad_out: &B::DeviceBuffer<T>,
    grad_out_layout: &Layout,
    input: &B::DeviceBuffer<T>,
    input_layout: &Layout,
    kernel_size: usize,
    stride: usize,
    padding: usize,
    dilation: usize,
    grad_input: &mut B::DeviceBuffer<T>,
    grad_input_layout: &Layout,
) where
    B::DeviceBuffer<T>: CpuAddressableStorageMut<T>,
{
    let n = grad_input_layout.shape()[0];
    let c = grad_input_layout.shape()[1];
    let d = grad_input_layout.shape()[2];
    let h = grad_input_layout.shape()[3];
    let w = grad_input_layout.shape()[4];
    let d_out = grad_out_layout.shape()[2];
    let h_out = grad_out_layout.shape()[3];
    let w_out = grad_out_layout.shape()[4];
    let numel_in = n * c * d * h * w;

    let go_slice = grad_out.as_slice();
    let gi_slice = grad_input.as_mut_slice();
    let inp_slice = input.as_slice();

    let go_ptr = Ptr(go_slice.as_ptr());
    let gi_ptr = MutPtr(gi_slice.as_mut_ptr());
    let inp_ptr = Ptr(inp_slice.as_ptr());

    let go_layout = grad_out_layout.clone();
    let gi_layout = grad_input_layout.clone();
    let inp_layout = input_layout.clone();

    let pad_s = padding as isize;
    let stride_s = stride as isize;
    let dil_s = dilation as isize;
    let k_size = kernel_size;

    backend.parallel_for(0, numel_in, move |i| {
        let wi = i % w;
        let temp1 = i / w;
        let hi = temp1 % h;
        let temp2 = temp1 / h;
        let di = temp2 % d;
        let temp3 = temp2 / d;
        let ci = temp3 % c;
        let ni = temp3 / c;

        let my_idx = inp_layout.physical_index(&[ni, ci, di, hi, wi]);
        let my_val = unsafe { inp_ptr.read(my_idx) };

        let mut sum = T::zero();

        for ikd in 0..k_size {
            let numer_d = di as isize + pad_s - ikd as isize * dil_s;
            if numer_d >= 0 && numer_d % stride_s == 0 {
                let od = (numer_d / stride_s) as usize;
                if od < d_out {
                    for ikh in 0..k_size {
                        let numer_h = hi as isize + pad_s - ikh as isize * dil_s;
                        if numer_h >= 0 && numer_h % stride_s == 0 {
                            let oh = (numer_h / stride_s) as usize;
                            if oh < h_out {
                                for ikw in 0..k_size {
                                    let numer_w = wi as isize + pad_s - ikw as isize * dil_s;
                                    if numer_w >= 0 && numer_w % stride_s == 0 {
                                        let ow = (numer_w / stride_s) as usize;
                                        if ow < w_out {
                                            let mut max_val: Option<T> = None;
                                            let mut max_d: usize = 0;
                                            let mut max_h: usize = 0;
                                            let mut max_w: usize = 0;

                                            for jkd in 0..k_size {
                                                let d_in = od as isize * stride_s
                                                    + jkd as isize * dil_s
                                                    - pad_s;
                                                if d_in >= 0 && (d_in as usize) < d {
                                                    for jkh in 0..k_size {
                                                        let h_in = oh as isize * stride_s
                                                            + jkh as isize * dil_s
                                                            - pad_s;
                                                        if h_in >= 0 && (h_in as usize) < h {
                                                            for jkw in 0..k_size {
                                                                let w_in = ow as isize * stride_s
                                                                    + jkw as isize * dil_s
                                                                    - pad_s;
                                                                if w_in >= 0 && (w_in as usize) < w
                                                                {
                                                                    let input_idx = inp_layout
                                                                        .physical_index(&[
                                                                            ni,
                                                                            ci,
                                                                            d_in as usize,
                                                                            h_in as usize,
                                                                            w_in as usize,
                                                                        ]);
                                                                    let val = unsafe {
                                                                        inp_ptr.read(input_idx)
                                                                    };
                                                                    let is_new_max = match max_val {
                                                                        None => true,
                                                                        Some(m) => val > m,
                                                                    };
                                                                    if is_new_max {
                                                                        max_val = Some(val);
                                                                        max_d = d_in as usize;
                                                                        max_h = h_in as usize;
                                                                        max_w = w_in as usize;
                                                                    }
                                                                }
                                                            }
                                                        }
                                                    }
                                                }
                                            }

                                            if max_val.is_some()
                                                && max_d == di
                                                && max_h == hi
                                                && max_w == wi
                                                && my_val == max_val.unwrap()
                                            {
                                                let go_idx =
                                                    go_layout.physical_index(&[ni, ci, od, oh, ow]);
                                                let gval = unsafe { go_ptr.read(go_idx) };
                                                sum += gval;
                                            }
                                        }
                                    }
                                }
                            }
                        }
                    }
                }
            }
        }

        let gi_idx = gi_layout.physical_index(&[ni, ci, di, hi, wi]);
        unsafe {
            let old = gi_ptr.read(gi_idx);
            gi_ptr.write(gi_idx, old + sum);
        }
    });
}