use crate::{Error, Matrix, Result, matrix};
use super::super::{Module, ModuleRegistry};
pub struct ChannelShuffle {
groups: usize,
registry: ModuleRegistry,
}
impl ChannelShuffle {
pub fn new(groups: usize) -> Result<Self> {
if groups == 0 {
return Err(Error::invalid_argument(
"channel shuffle requires at least one group",
));
}
Ok(Self {
groups,
registry: ModuleRegistry::new(),
})
}
pub const fn groups(&self) -> usize {
self.groups
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
let operation = "ChannelShuffle";
let shape = input.shape();
if shape.len() < 2 {
return Err(Error::invalid_argument(format!(
"{operation} requires an input of rank at least two"
)));
}
let (batch, channels) = (shape[0], shape[1]);
if !channels.is_multiple_of(self.groups) {
return Err(Error::invalid_argument(format!(
"{operation} channel count {channels} is not divisible by {}",
self.groups
)));
}
let per_group = channels / self.groups;
let spatial: usize = shape[2..].iter().product();
let flattened = input.reshape([batch, channels, spatial])?;
let rows = matrix::transpose(
&matrix::transpose(&flattened, 1, 2)?.reshape([batch * spatial, channels])?,
0,
1,
)?;
let indices: Vec<i32> = (0..per_group)
.flat_map(|within| (0..self.groups).map(move |group| (group * per_group + within) as i32))
.collect();
let index = Matrix::new_from_slice_handle(input.runtime(), vec![channels], &indices)?;
let shuffled = matrix::transpose(&matrix::gather(&rows, &index)?, 0, 1)?
.reshape([batch, spatial, channels])?;
matrix::transpose(&shuffled, 1, 2)?.reshape(shape.to_vec())
}
}
impl Module for ChannelShuffle {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
ChannelShuffle::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}