use crate::runtime::cpu::CpuClient;
#[derive(Debug, Clone, Copy)]
pub enum GatherMap<'a> {
Shuffle(usize),
Unshuffle(usize),
Nearest {
rows: &'a [usize],
cols: &'a [usize],
},
}
#[derive(Debug, Clone, Copy)]
pub struct GatherGeometry {
pub out_dims: [usize; 4],
pub src_strides: [isize; 4],
pub dst_strides: [isize; 4],
pub channels_last: bool,
}
impl GatherGeometry {
fn rows(&self) -> (usize, usize) {
let [n, c, h, w] = self.out_dims;
if self.channels_last {
(n * h * w, c)
} else {
(n * c * h, w)
}
}
}
#[inline(always)]
fn dot(strides: [isize; 4], coords: [usize; 4]) -> isize {
coords[0] as isize * strides[0]
+ coords[1] as isize * strides[1]
+ coords[2] as isize * strides[2]
+ coords[3] as isize * strides[3]
}
#[inline(always)]
unsafe fn copy_row<T: Copy, F: Fn(usize, usize, usize) -> [usize; 3]>(
src: *const T,
dst: *mut T,
g: &GatherGeometry,
row: usize,
source: F,
) {
let [_, c_dim, h_dim, w_dim] = g.out_dims;
let emit = |n: usize, c: usize, h: usize, w: usize| {
let [sc, sh, sw] = source(c, h, w);
let s = dot(g.src_strides, [n, sc, sh, sw]);
let d = dot(g.dst_strides, [n, c, h, w]);
unsafe { *dst.offset(d) = *src.offset(s) };
};
if g.channels_last {
let (w, h, n) = (row % w_dim, (row / w_dim) % h_dim, row / (w_dim * h_dim));
for c in 0..c_dim {
emit(n, c, h, w);
}
} else {
let (h, c, n) = (row % h_dim, (row / h_dim) % c_dim, row / (h_dim * c_dim));
for w in 0..w_dim {
emit(n, c, h, w);
}
}
}
unsafe fn gather_row<T: Copy>(
src: *const T,
dst: *mut T,
g: &GatherGeometry,
map: GatherMap<'_>,
row: usize,
) {
unsafe {
match map {
GatherMap::Shuffle(r) => copy_row(src, dst, g, row, |c, h, w| {
[c * r * r + (h % r) * r + w % r, h / r, w / r]
}),
GatherMap::Unshuffle(r) => copy_row(src, dst, g, row, |c, h, w| {
let rem = c % (r * r);
[c / (r * r), h * r + rem / r, w * r + rem % r]
}),
GatherMap::Nearest { rows, cols } => {
copy_row(src, dst, g, row, |c, h, w| [c, rows[h], cols[w]])
}
}
}
}
pub unsafe fn image_gather_kernel<T: Copy>(
client: &CpuClient,
src: *const T,
dst: *mut T,
g: &GatherGeometry,
map: GatherMap<'_>,
) {
let (rows, run) = g.rows();
if rows == 0 || run == 0 {
return;
}
let src_addr = src as usize;
let dst_addr = dst as usize;
let geometry = *g;
client.par_for_each(rows, rows * run, move |row| {
unsafe {
gather_row(
src_addr as *const T,
dst_addr as *mut T,
&geometry,
map,
row,
)
}
});
}
#[cfg(test)]
mod tests {
use super::*;
use crate::runtime::Runtime;
use crate::runtime::cpu::{CpuDevice, CpuRuntime};
fn contiguous(dims: [usize; 4]) -> [isize; 4] {
let [_, c, h, w] = dims;
[(c * h * w) as isize, (h * w) as isize, w as isize, 1]
}
#[test]
fn shuffle_matches_the_index_formula() {
let client = CpuRuntime::default_client(&CpuDevice::new());
let r = 2;
let in_dims = [1, 8, 2, 3];
let out_dims = [1, 2, 4, 6];
let src: Vec<u32> = (0..48).collect();
let mut dst = vec![0u32; 48];
let g = GatherGeometry {
out_dims,
src_strides: contiguous(in_dims),
dst_strides: contiguous(out_dims),
channels_last: false,
};
unsafe {
image_gather_kernel(
&client,
src.as_ptr(),
dst.as_mut_ptr(),
&g,
GatherMap::Shuffle(r),
);
}
for c in 0..2 {
for oh in 0..4 {
for ow in 0..6 {
let ic = c * 4 + (oh % 2) * 2 + ow % 2;
let want = (ic * 6 + (oh / 2) * 3 + ow / 2) as u32;
assert_eq!(dst[(c * 4 + oh) * 6 + ow], want);
}
}
}
}
#[test]
fn unshuffle_inverts_shuffle_in_nhwc_order() {
let client = CpuRuntime::default_client(&CpuDevice::new());
let src: Vec<u16> = (0..48).collect();
let mut mid = vec![0u16; 48];
let mut back = vec![0u16; 48];
let nhwc = |n_h_w_c: [usize; 4]| {
let [_, h, w, c] = n_h_w_c;
[(h * w * c) as isize, 1, (w * c) as isize, c as isize]
};
let shuffle = GatherGeometry {
out_dims: [1, 2, 4, 6],
src_strides: nhwc([1, 2, 3, 8]),
dst_strides: nhwc([1, 4, 6, 2]),
channels_last: true,
};
let unshuffle = GatherGeometry {
out_dims: [1, 8, 2, 3],
src_strides: nhwc([1, 4, 6, 2]),
dst_strides: nhwc([1, 2, 3, 8]),
channels_last: true,
};
unsafe {
image_gather_kernel(
&client,
src.as_ptr(),
mid.as_mut_ptr(),
&shuffle,
GatherMap::Shuffle(2),
);
image_gather_kernel(
&client,
mid.as_ptr(),
back.as_mut_ptr(),
&unshuffle,
GatherMap::Unshuffle(2),
);
}
assert_eq!(back, src);
}
}