#[derive(Debug, Clone, Copy, PartialEq)]
pub enum NearestAxis {
Identity,
Half,
Scaled(f32),
}
impl NearestAxis {
pub fn new(in_size: usize, out_size: usize, scale_factor: Option<f64>) -> Self {
if out_size == in_size {
return Self::Identity;
}
if Some(out_size) == in_size.checked_mul(2) {
return Self::Half;
}
let scale = match scale_factor {
Some(s) => (1.0 / s) as f32,
None => in_size as f32 / out_size as f32,
};
Self::Scaled(scale)
}
#[inline]
pub fn source_index(self, dst: usize, in_size: usize) -> usize {
match self {
Self::Identity => dst,
Self::Half => dst >> 1,
Self::Scaled(scale) => {
let src = (dst as f32 * scale).floor() as usize;
src.min(in_size - 1)
}
}
}
}
#[cfg(any(feature = "cuda", feature = "wgpu"))]
impl NearestAxis {
pub const MODE_IDENTITY: u32 = 0;
pub const MODE_HALF: u32 = 1;
pub const MODE_SCALED: u32 = 2;
pub fn kernel_args(self) -> (u32, f32) {
match self {
Self::Identity => (Self::MODE_IDENTITY, 0.0),
Self::Half => (Self::MODE_HALF, 0.0),
Self::Scaled(scale) => (Self::MODE_SCALED, scale),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
fn table(in_size: usize, out_size: usize, scale_factor: Option<f64>) -> Vec<usize> {
let axis = NearestAxis::new(in_size, out_size, scale_factor);
(0..out_size)
.map(|dst| axis.source_index(dst, in_size))
.collect()
}
#[test]
fn size_mode_matches_pytorch_tables() {
assert_eq!(table(3, 7, None), vec![0, 0, 0, 1, 1, 2, 2]);
assert_eq!(table(7, 3, None), vec![0, 2, 4]);
assert_eq!(table(5, 8, None), vec![0, 0, 1, 1, 2, 3, 3, 4]);
assert_eq!(table(5, 3, None), vec![0, 1, 3]);
}
#[test]
fn scale_mode_matches_pytorch_tables() {
assert_eq!(table(5, 8, Some(1.7)), vec![0, 0, 1, 1, 2, 2, 3, 4]);
assert_eq!(table(4, 6, Some(1.5)), vec![0, 0, 1, 2, 2, 3]);
assert_eq!(table(5, 3, Some(0.75)), vec![0, 1, 2]);
assert_eq!(table(3, 7, Some(2.5)), vec![0, 0, 0, 1, 1, 2, 2]);
}
#[test]
fn twice_the_input_size_shifts_even_with_another_scale_factor() {
assert_eq!(NearestAxis::new(3, 6, Some(2.3)), NearestAxis::Half);
assert_eq!(table(3, 6, Some(2.3)), vec![0, 0, 1, 1, 2, 2]);
}
#[test]
fn equal_sizes_copy_even_with_another_scale_factor() {
assert_eq!(NearestAxis::new(3, 3, Some(1.2)), NearestAxis::Identity);
assert_eq!(table(3, 3, Some(1.2)), vec![0, 1, 2]);
}
#[test]
fn integer_factor_repeats_each_index() {
assert_eq!(table(2, 6, Some(3.0)), vec![0, 0, 0, 1, 1, 1]);
assert_eq!(table(2, 6, None), vec![0, 0, 0, 1, 1, 1]);
}
#[cfg(any(feature = "cuda", feature = "wgpu"))]
#[test]
fn kernel_args_carry_the_scale_only_when_scaled() {
assert_eq!(NearestAxis::Identity.kernel_args(), (0, 0.0));
assert_eq!(NearestAxis::Half.kernel_args(), (1, 0.0));
assert_eq!(NearestAxis::Scaled(0.5).kernel_args(), (2, 0.5));
}
}