use crate::error::Error;
use crate::neural_network::layers::ParamCounts;
use crate::neural_network::layers::upsampling::Interpolation;
use crate::neural_network::layers::upsampling::resize_engine::{
upsample_backward, upsample_forward, upsample_output_shape, validate_factors,
};
use crate::neural_network::layers::validation::start_build;
use crate::neural_network::layers::{
built_layer_shape_functions, no_trainable_parameters_layer_functions,
};
use crate::neural_network::traits::{LayerBase, UnaryLayer};
use crate::neural_network::{Ctx, Shape, Tensor};
#[derive(Debug)]
pub struct UpSampling1D {
size: usize,
built: Option<Shape>,
}
impl UpSampling1D {
pub fn new(size: usize) -> Result<Self, Error> {
validate_factors(&[size])?;
Ok(UpSampling1D { size, built: None })
}
}
impl LayerBase for UpSampling1D {
fn layer_type(&self) -> &str {
"UpSampling1D"
}
built_layer_shape_functions!();
no_trainable_parameters_layer_functions!();
}
impl UnaryLayer for UpSampling1D {
fn build(&mut self, input: &Shape) -> Result<(), Error> {
let Some(built) = start_build(&self.built, "UpSampling1D", input)? else {
return Ok(());
};
self.compute_output_shape(&built)?;
self.built = Some(built);
Ok(())
}
fn forward(&self, input: &Tensor, ctx: &mut Ctx) -> Result<Tensor, Error> {
let output = upsample_forward(
input,
&[self.size],
Interpolation::Nearest,
3,
"UpSampling1D",
)?;
if ctx.is_training() {
ctx.push_cache("UpSampling1D", input.shape().to_vec());
}
Ok(output)
}
fn backward(&self, grad_output: &Tensor, ctx: &mut Ctx) -> Result<Tensor, Error> {
let input_shape: Vec<usize> = ctx.pop_cache("UpSampling1D")?;
upsample_backward(
grad_output,
Some(input_shape.as_slice()),
&[self.size],
Interpolation::Nearest,
"UpSampling1D",
)
}
fn compute_output_shape(&self, input: &Shape) -> Result<Shape, Error> {
upsample_output_shape(input, &[self.size], "UpSampling1D")
}
}