use crate::{Error, Matrix, Result, matrix};
use super::super::{Module, ModuleRegistry};
pub struct PixelShuffle {
upscale_factor: usize,
registry: ModuleRegistry,
}
impl PixelShuffle {
pub fn new(upscale_factor: usize) -> Result<Self> {
if upscale_factor < 2 {
return Err(Error::invalid_argument(
"pixel shuffle requires an upscale factor of at least 2",
));
}
Ok(Self {
upscale_factor,
registry: ModuleRegistry::new(),
})
}
pub const fn upscale_factor(&self) -> usize {
self.upscale_factor
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
let r = self.upscale_factor;
let &[batch, packed, height, width] = input.shape() else {
return Err(Error::invalid_argument(format!(
"pixel shuffle requires [N, C*r^2, H, W]; found {:?}",
input.shape(),
)));
};
let squared = r
.checked_mul(r)
.ok_or_else(|| Error::invalid_argument("pixel shuffle factor overflows usize"))?;
if packed == 0 || !packed.is_multiple_of(squared) {
return Err(Error::invalid_argument(format!(
"pixel shuffle channels {packed} must be a nonzero multiple of r^2 = {squared}"
)));
}
let channels = packed / squared;
let out_height = height * r;
let out_width = width * r;
let mut indices = Vec::with_capacity(batch * channels * out_height * out_width);
for _ in 0..batch {
for c in 0..channels {
for h_out in 0..out_height {
for w_out in 0..out_width {
let channel = c * squared + (h_out % r) * r + (w_out % r);
indices.push((channel * height + h_out / r) as i32 * width as i32 + (w_out / r) as i32);
}
}
}
}
let flat = input.reshape([batch, packed * height * width])?;
let index_matrix = Matrix::new_from_slice_handle(
input.runtime(),
vec![batch, channels * out_height * out_width],
&indices,
)?;
let gathered = matrix::gather_last_dim(&flat, &index_matrix)?;
gathered.reshape([batch, channels, out_height, out_width])
}
}
impl Module for PixelShuffle {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
PixelShuffle::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}
pub struct PixelUnshuffle {
downscale_factor: usize,
registry: ModuleRegistry,
}
impl PixelUnshuffle {
pub fn new(downscale_factor: usize) -> Result<Self> {
if downscale_factor < 2 {
return Err(Error::invalid_argument(
"pixel unshuffle requires a downscale factor of at least 2",
));
}
Ok(Self {
downscale_factor,
registry: ModuleRegistry::new(),
})
}
pub const fn downscale_factor(&self) -> usize {
self.downscale_factor
}
pub fn forward(&self, input: &Matrix) -> Result<Matrix> {
let r = self.downscale_factor;
let &[batch, channels, height, width] = input.shape() else {
return Err(Error::invalid_argument(format!(
"pixel unshuffle requires [N, C, H, W]; found {:?}",
input.shape(),
)));
};
if height == 0 || !height.is_multiple_of(r) || width == 0 || !width.is_multiple_of(r) {
return Err(Error::invalid_argument(format!(
"pixel unshuffle spatial extents {height}x{width} must be nonzero multiples of r = {r}"
)));
}
let squared = r
.checked_mul(r)
.ok_or_else(|| Error::invalid_argument("pixel unshuffle factor overflows usize"))?;
let out_height = height / r;
let out_width = width / r;
let mut indices = Vec::with_capacity(batch * channels * squared * out_height * out_width);
for _ in 0..batch {
for c in 0..channels {
for i in 0..r {
for j in 0..r {
for h in 0..out_height {
for w in 0..out_width {
indices.push((c * height + h * r + i) as i32 * width as i32 + (w * r + j) as i32);
}
}
}
}
}
}
let flat = input.reshape([batch, channels * height * width])?;
let index_matrix = Matrix::new_from_slice_handle(
input.runtime(),
vec![batch, channels * squared * out_height * out_width],
&indices,
)?;
let gathered = matrix::gather_last_dim(&flat, &index_matrix)?;
gathered.reshape([batch, channels * squared, out_height, out_width])
}
}
impl Module for PixelUnshuffle {
fn forward(&self, input: &Matrix) -> Result<Matrix> {
PixelUnshuffle::forward(self, input)
}
fn registry(&self) -> &ModuleRegistry {
&self.registry
}
}