use tch::{Tensor, nn::Init};
use super::{Module, ParameterPath, functional, layers::validate_parameter_kind};
use crate::{Result, RustTorchError, device::ensure_device};
fn invalid(field: &'static str, reason: &str) -> RustTorchError {
RustTorchError::InvalidConfiguration {
field,
reason: reason.to_owned(),
}
}
fn spatial_input<const D: usize>(input: &Tensor) -> Result<Vec<i64>> {
if !input.defined() {
return Err(invalid("spatial input", "must be a defined tensor"));
}
let shape = input.size();
if !(1..=3).contains(&D) || ![D + 1, D + 2].contains(&shape.len()) {
return Err(invalid(
"spatial input",
"requires one, two, or three spatial axes, preceded by channels and an optional batch",
));
}
Ok(shape)
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct ConvTransposeConfig<const D: usize> {
in_channels: i64,
out_channels: i64,
kernel_size: [i64; D],
stride: [i64; D],
padding: [i64; D],
output_padding: [i64; D],
dilation: [i64; D],
groups: i64,
bias: bool,
}
impl<const D: usize> ConvTransposeConfig<D> {
pub const fn new(in_channels: i64, out_channels: i64, kernel_size: [i64; D]) -> Self {
Self {
in_channels,
out_channels,
kernel_size,
stride: [1; D],
padding: [0; D],
output_padding: [0; D],
dilation: [1; D],
groups: 1,
bias: true,
}
}
#[must_use]
pub const fn stride(mut self, stride: [i64; D]) -> Self {
self.stride = stride;
self
}
#[must_use]
pub const fn padding(mut self, padding: [i64; D]) -> Self {
self.padding = padding;
self
}
#[must_use]
pub const fn output_padding(mut self, output_padding: [i64; D]) -> Self {
self.output_padding = output_padding;
self
}
#[must_use]
pub const fn dilation(mut self, dilation: [i64; D]) -> Self {
self.dilation = dilation;
self
}
#[must_use]
pub const fn groups(mut self, groups: i64) -> Self {
self.groups = groups;
self
}
#[must_use]
pub const fn bias(mut self, bias: bool) -> Self {
self.bias = bias;
self
}
pub fn build(self, path: &ParameterPath<'_>) -> Result<ConvTranspose<D>> {
validate_parameter_kind(path)?;
functional::validate_conv_config(
self.in_channels,
self.out_channels,
&self.kernel_size,
&self.stride,
&self.padding,
&self.dilation,
self.groups,
)?;
for axis in 0..D {
if self.output_padding[axis] < 0
|| (self.output_padding[axis] >= self.stride[axis]
&& self.output_padding[axis] >= self.dilation[axis])
{
return Err(invalid(
"output_padding",
"must be nonnegative and smaller than either stride or dilation per axis",
));
}
}
let fan_in = self
.kernel_size
.iter()
.try_fold(self.out_channels / self.groups, |n, k| n.checked_mul(*k))
.ok_or_else(|| invalid("kernel_size", "fan-in exceeds i64"))?;
let bound = 1.0 / (fan_in as f64).sqrt();
let init = Init::Uniform {
lo: -bound,
up: bound,
};
let mut shape = vec![self.in_channels, self.out_channels / self.groups];
shape.extend(self.kernel_size);
let weight = path.f_var("weight", &shape, init)?;
let bias = self
.bias
.then(|| path.f_var("bias", &[self.out_channels], init))
.transpose()?;
Ok(ConvTranspose {
weight,
bias,
config: self,
})
}
}
#[derive(Debug)]
pub struct ConvTranspose<const D: usize> {
weight: Tensor,
bias: Option<Tensor>,
config: ConvTransposeConfig<D>,
}
impl<const D: usize> ConvTranspose<D> {
pub const fn weight(&self) -> &Tensor {
&self.weight
}
pub const fn bias(&self) -> Option<&Tensor> {
self.bias.as_ref()
}
pub fn forward_with_output_size(&self, input: &Tensor, output_size: &[i64]) -> Result<Tensor> {
let shape = spatial_input::<D>(input)?;
let sizes = if output_size.len() == shape.len() {
&output_size[shape.len() - D..]
} else {
output_size
};
if sizes.len() != D {
return Err(invalid(
"output_size",
"must contain spatial sizes or the full output shape",
));
}
let mut output_padding = [0; D];
for axis in 0..D {
let minimum = (shape[shape.len() - D + axis] as i128 - 1)
* self.config.stride[axis] as i128
- 2 * self.config.padding[axis] as i128
+ self.config.dilation[axis] as i128 * (self.config.kernel_size[axis] as i128 - 1)
+ 1;
let extra = sizes[axis] as i128 - minimum;
if extra < 0 || extra >= self.config.stride[axis] as i128 {
return Err(invalid(
"output_size",
"is outside the range allowed by kernel, stride, padding, and dilation",
));
}
output_padding[axis] = extra as i64;
}
self.apply(input, &output_padding)
}
fn apply(&self, input: &Tensor, output_padding: &[i64; D]) -> Result<Tensor> {
let shape = spatial_input::<D>(input)?;
ensure_device(
"transposed convolution weight",
&self.weight,
input.device(),
)?;
if shape[shape.len() - D - 1] != self.config.in_channels {
return Err(invalid(
"input channels",
"must match the transposed convolution configuration",
));
}
if let Some(bias) = &self.bias {
ensure_device("transposed convolution bias", bias, input.device())?;
}
let c = self.config;
Ok(match D {
1 => input.f_conv_transpose1d(
&self.weight,
self.bias.as_ref(),
c.stride.as_slice(),
c.padding.as_slice(),
output_padding.as_slice(),
c.groups,
c.dilation.as_slice(),
)?,
2 => input.f_conv_transpose2d(
&self.weight,
self.bias.as_ref(),
c.stride.as_slice(),
c.padding.as_slice(),
output_padding.as_slice(),
c.groups,
c.dilation.as_slice(),
)?,
3 => input.f_conv_transpose3d(
&self.weight,
self.bias.as_ref(),
c.stride.as_slice(),
c.padding.as_slice(),
output_padding.as_slice(),
c.groups,
c.dilation.as_slice(),
)?,
_ => {
return Err(invalid(
"convolution dimensions",
"must be one, two, or three",
));
}
})
}
}
impl<const D: usize> Module for ConvTranspose<D> {
fn forward(&self, input: &Tensor) -> Result<Tensor> {
self.apply(input, &self.config.output_padding)
}
}
#[derive(Debug, Clone, Copy)]
struct PoolOptions<const D: usize> {
kernel: [i64; D],
stride: [i64; D],
padding: [i64; D],
ceil_mode: bool,
}
impl<const D: usize> PoolOptions<D> {
fn new(kernel: [i64; D]) -> Result<Self> {
let options = Self {
kernel,
stride: kernel,
padding: [0; D],
ceil_mode: false,
};
options.validate()?;
Ok(options)
}
fn validate(&self) -> Result<()> {
if !(1..=3).contains(&D) {
return Err(invalid("pool dimensions", "must be one, two, or three"));
}
if self
.kernel
.iter()
.chain(self.stride.iter())
.any(|v| *v <= 0)
{
return Err(invalid("pool kernel/stride", "must be positive per axis"));
}
if self
.padding
.iter()
.zip(self.kernel)
.any(|(p, k)| *p < 0 || *p > k / 2)
{
return Err(invalid(
"pool padding",
"must be nonnegative and at most half the kernel size per axis",
));
}
Ok(())
}
}
#[derive(Debug, Clone, Copy)]
pub struct MaxPool<const D: usize> {
options: PoolOptions<D>,
dilation: [i64; D],
}
#[derive(Debug, Clone, Copy)]
pub struct AvgPool<const D: usize> {
options: PoolOptions<D>,
count_include_pad: bool,
divisor_override: Option<i64>,
}
macro_rules! pool_setters {
($pool:ident) => {
impl<const D: usize> $pool<D> {
#[must_use]
pub const fn stride(mut self, stride: [i64; D]) -> Self {
self.options.stride = stride;
self
}
#[must_use]
pub const fn padding(mut self, padding: [i64; D]) -> Self {
self.options.padding = padding;
self
}
#[must_use]
pub const fn ceil_mode(mut self, ceil: bool) -> Self {
self.options.ceil_mode = ceil;
self
}
}
};
}
pool_setters!(MaxPool);
pool_setters!(AvgPool);
impl<const D: usize> MaxPool<D> {
pub fn new(kernel_size: [i64; D]) -> Result<Self> {
Ok(Self {
options: PoolOptions::new(kernel_size)?,
dilation: [1; D],
})
}
#[must_use]
pub const fn dilation(mut self, dilation: [i64; D]) -> Self {
self.dilation = dilation;
self
}
pub fn forward_with_indices(&self, input: &Tensor) -> Result<(Tensor, Tensor)> {
spatial_input::<D>(input)?;
self.options.validate()?;
if self.dilation.iter().any(|d| *d <= 0) {
return Err(invalid("pool dilation", "must be positive per axis"));
}
let o = self.options;
Ok(match D {
1 => input.f_max_pool1d_with_indices(
o.kernel.as_slice(),
o.stride.as_slice(),
o.padding.as_slice(),
self.dilation.as_slice(),
o.ceil_mode,
)?,
2 => input.f_max_pool2d_with_indices(
o.kernel.as_slice(),
o.stride.as_slice(),
o.padding.as_slice(),
self.dilation.as_slice(),
o.ceil_mode,
)?,
3 => input.f_max_pool3d_with_indices(
o.kernel.as_slice(),
o.stride.as_slice(),
o.padding.as_slice(),
self.dilation.as_slice(),
o.ceil_mode,
)?,
_ => return Err(invalid("pool dimensions", "must be one, two, or three")),
})
}
}
impl<const D: usize> Module for MaxPool<D> {
fn forward(&self, input: &Tensor) -> Result<Tensor> {
Ok(self.forward_with_indices(input)?.0)
}
}
impl<const D: usize> AvgPool<D> {
pub fn new(kernel_size: [i64; D]) -> Result<Self> {
Ok(Self {
options: PoolOptions::new(kernel_size)?,
count_include_pad: true,
divisor_override: None,
})
}
#[must_use]
pub const fn count_include_pad(mut self, count: bool) -> Self {
self.count_include_pad = count;
self
}
#[must_use]
pub const fn divisor_override(mut self, divisor: Option<i64>) -> Self {
self.divisor_override = divisor;
self
}
}
impl<const D: usize> Module for AvgPool<D> {
fn forward(&self, input: &Tensor) -> Result<Tensor> {
spatial_input::<D>(input)?;
self.options.validate()?;
if self.divisor_override == Some(0) || D == 1 && self.divisor_override.is_some() {
return Err(invalid(
"divisor_override",
"must be nonzero and is supported only for 2-D/3-D average pooling",
));
}
let o = self.options;
Ok(match D {
1 => input.f_avg_pool1d(
o.kernel.as_slice(),
o.stride.as_slice(),
o.padding.as_slice(),
o.ceil_mode,
self.count_include_pad,
)?,
2 => input.f_avg_pool2d(
o.kernel.as_slice(),
o.stride.as_slice(),
o.padding.as_slice(),
o.ceil_mode,
self.count_include_pad,
self.divisor_override,
)?,
3 => input.f_avg_pool3d(
o.kernel.as_slice(),
o.stride.as_slice(),
o.padding.as_slice(),
o.ceil_mode,
self.count_include_pad,
self.divisor_override,
)?,
_ => return Err(invalid("pool dimensions", "must be one, two, or three")),
})
}
}
#[derive(Debug, Clone, Copy)]
pub struct AdaptiveAvgPool<const D: usize> {
output_size: [i64; D],
}
#[derive(Debug, Clone, Copy)]
pub struct AdaptiveMaxPool<const D: usize> {
output_size: [i64; D],
}
macro_rules! adaptive_constructor {
($pool:ident) => {
impl<const D: usize> $pool<D> {
pub fn new(output_size: [i64; D]) -> Result<Self> {
if !(1..=3).contains(&D) || output_size.iter().any(|s| *s <= 0) {
return Err(invalid(
"adaptive output_size",
"must contain one, two, or three positive spatial sizes",
));
}
Ok(Self { output_size })
}
}
};
}
adaptive_constructor!(AdaptiveAvgPool);
adaptive_constructor!(AdaptiveMaxPool);
impl<const D: usize> Module for AdaptiveAvgPool<D> {
fn forward(&self, input: &Tensor) -> Result<Tensor> {
spatial_input::<D>(input)?;
Ok(match D {
1 => input.f_adaptive_avg_pool1d(self.output_size.as_slice())?,
2 => input.f_adaptive_avg_pool2d(self.output_size.as_slice())?,
3 => input.f_adaptive_avg_pool3d(self.output_size.as_slice())?,
_ => return Err(invalid("pool dimensions", "must be one, two, or three")),
})
}
}
impl<const D: usize> AdaptiveMaxPool<D> {
pub fn forward_with_indices(&self, input: &Tensor) -> Result<(Tensor, Tensor)> {
spatial_input::<D>(input)?;
Ok(match D {
1 => input.f_adaptive_max_pool1d(self.output_size.as_slice())?,
2 => input.f_adaptive_max_pool2d(self.output_size.as_slice())?,
3 => input.f_adaptive_max_pool3d(self.output_size.as_slice())?,
_ => return Err(invalid("pool dimensions", "must be one, two, or three")),
})
}
}
impl<const D: usize> Module for AdaptiveMaxPool<D> {
fn forward(&self, input: &Tensor) -> Result<Tensor> {
Ok(self.forward_with_indices(input)?.0)
}
}
macro_rules! spatial_aliases {
($layer:ident, $one:ident, $two:ident, $three:ident) => {
#[doc = concat!("Sequence variant of [`", stringify!($layer), "`]. See its example for construction and use.")]
pub type $one = $layer<1>;
#[doc = concat!("Image variant of [`", stringify!($layer), "`]. See its example for construction and use.")]
pub type $two = $layer<2>;
#[doc = concat!("Volume variant of [`", stringify!($layer), "`]. See its example for construction and use.")]
pub type $three = $layer<3>;
};
}
spatial_aliases!(
ConvTranspose,
ConvTranspose1d,
ConvTranspose2d,
ConvTranspose3d
);
spatial_aliases!(MaxPool, MaxPool1d, MaxPool2d, MaxPool3d);
spatial_aliases!(AvgPool, AvgPool1d, AvgPool2d, AvgPool3d);
spatial_aliases!(
AdaptiveAvgPool,
AdaptiveAvgPool1d,
AdaptiveAvgPool2d,
AdaptiveAvgPool3d
);
spatial_aliases!(
AdaptiveMaxPool,
AdaptiveMaxPool1d,
AdaptiveMaxPool2d,
AdaptiveMaxPool3d
);