Skip to main content

ruda_tensor/api/
spatial_interpolate.rs

1use super::{Tensor, backend::Backend, ops::InterpolateOptions};
2use super::module::interpolate;
3use super::spatial_pool::{volume_planes, plane_depth_lines, depth_lines_volume};
4
5/// Resize native `[batch, channels, length]` activations.
6///
7/// Applies the existing interpolation kernel along the length axis, preserving
8/// its coordinate mapping, mode, storage and differentiable backward operation.
9pub fn interpolate1d<B: Backend>(
10    input: Tensor<B, 3>,
11    output_size: usize,
12    options: InterpolateOptions,
13) -> Tensor<B, 3> {
14    interpolate(input.unsqueeze_dim(2), [1, output_size], options).squeeze_dim(2)
15}
16
17/// Resize native `[batch, channels, depth, height, width]` activations.
18///
19/// Factors interpolation into native height/width and depth passes. `Bilinear`
20/// therefore applies linear interpolation in all three axes; cubic and Lanczos
21/// apply their existing filters separably. Both passes use the supplied corner
22/// alignment, without host resampling or detaching the gradient graph.
23pub fn interpolate3d<B: Backend>(
24    input: Tensor<B, 5>,
25    output_size: [usize; 3],
26    options: InterpolateOptions,
27) -> Tensor<B, 5> {
28    let [batch, channels, depth, _, _] = input.dims();
29    let planes = interpolate(volume_planes(input), [output_size[1], output_size[2]], options.clone());
30    let [_, _, height, width] = planes.dims();
31    let lines = interpolate1d(plane_depth_lines(planes, batch, depth), output_size[0], options);
32    depth_lines_volume(lines, batch, channels, height, width)
33}