use crate::backend_ops::BackendOps;
use coeus_core::Float;
use coeus_tensor::Tensor;
#[inline]
pub fn conv_transpose1d_output_len(
l: usize,
kernel: usize,
stride: usize,
padding: usize,
output_padding: usize,
dilation: usize,
) -> usize {
(l - 1) * stride + dilation * (kernel - 1) + output_padding + 1 - 2 * padding
}
#[expect(
clippy::too_many_arguments,
reason = "ratchet ATLAS-COEUS-LINT-RATCHET-097"
)]
#[inline]
pub fn conv_transpose2d_output_dims(
h: usize,
w: usize,
kh: usize,
kw: usize,
stride: usize,
padding: usize,
output_padding: usize,
dilation: usize,
) -> (usize, usize) {
let h_out = (h - 1) * stride + dilation * (kh - 1) + output_padding + 1 - 2 * padding;
let w_out = (w - 1) * stride + dilation * (kw - 1) + output_padding + 1 - 2 * padding;
(h_out, w_out)
}
#[expect(
clippy::too_many_arguments,
reason = "ratchet ATLAS-COEUS-LINT-RATCHET-097"
)]
#[inline]
pub fn conv_transpose1d<T: Float, B: BackendOps<T> + Default>(
input: &Tensor<T, B>,
weight: &Tensor<T, B>,
bias: Option<&Tensor<T, B>>,
stride: usize,
padding: usize,
output_padding: usize,
dilation: usize,
backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
let n = input.shape()[0];
let c_out = weight.shape()[1];
let l = input.shape()[2];
let k = weight.shape()[2];
let l_out = conv_transpose1d_output_len(l, k, stride, padding, output_padding, dilation);
let mut output = Tensor::zeros_on([n, c_out, l_out], backend)?;
let (out_storage, out_layout) = output.storage_mut_and_layout()?;
backend.conv_transpose1d(
input.storage(),
input.layout(),
weight.storage(),
weight.layout(),
bias.map(|b| b.storage()),
stride,
padding,
output_padding,
dilation,
out_storage,
out_layout,
)?;
Ok(output)
}
#[expect(
clippy::too_many_arguments,
reason = "ratchet ATLAS-COEUS-LINT-RATCHET-097"
)]
#[inline]
pub fn conv_transpose2d<T: Float, B: BackendOps<T> + Default>(
input: &Tensor<T, B>,
weight: &Tensor<T, B>,
bias: Option<&Tensor<T, B>>,
stride: usize,
padding: usize,
output_padding: usize,
dilation: usize,
backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
let n = input.shape()[0];
let c_out = weight.shape()[1];
let h = input.shape()[2];
let w = input.shape()[3];
let kh = weight.shape()[2];
let kw = weight.shape()[3];
let (h_out, w_out) =
conv_transpose2d_output_dims(h, w, kh, kw, stride, padding, output_padding, dilation);
let mut output = Tensor::zeros_on([n, c_out, h_out, w_out], backend)?;
let (out_storage, out_layout) = output.storage_mut_and_layout()?;
backend.conv_transpose2d(
input.storage(),
input.layout(),
weight.storage(),
weight.layout(),
bias.map(|b| b.storage()),
stride,
padding,
output_padding,
dilation,
out_storage,
out_layout,
)?;
Ok(output)
}
#[expect(
clippy::too_many_arguments,
reason = "ratchet ATLAS-COEUS-LINT-RATCHET-097"
)]
#[inline]
pub fn conv_transpose3d_output_dims(
d: usize,
h: usize,
w: usize,
kd: usize,
kh: usize,
kw: usize,
stride: usize,
padding: usize,
output_padding: usize,
dilation: usize,
) -> (usize, usize, usize) {
let d_out = (d - 1) * stride + dilation * (kd - 1) + output_padding + 1 - 2 * padding;
let h_out = (h - 1) * stride + dilation * (kh - 1) + output_padding + 1 - 2 * padding;
let w_out = (w - 1) * stride + dilation * (kw - 1) + output_padding + 1 - 2 * padding;
(d_out, h_out, w_out)
}
#[expect(
clippy::too_many_arguments,
reason = "ratchet ATLAS-COEUS-LINT-RATCHET-097"
)]
#[inline]
pub fn conv_transpose3d<T: Float, B: BackendOps<T> + Default>(
input: &Tensor<T, B>,
weight: &Tensor<T, B>,
bias: Option<&Tensor<T, B>>,
stride: usize,
padding: usize,
output_padding: usize,
dilation: usize,
backend: &B,
) -> Result<Tensor<T, B>, B::Error> {
let n = input.shape()[0];
let c_out = weight.shape()[1];
let d = input.shape()[2];
let h = input.shape()[3];
let w = input.shape()[4];
let kd = weight.shape()[2];
let kh = weight.shape()[3];
let kw = weight.shape()[4];
let (d_out, h_out, w_out) = conv_transpose3d_output_dims(
d,
h,
w,
kd,
kh,
kw,
stride,
padding,
output_padding,
dilation,
);
let mut output = Tensor::zeros_on([n, c_out, d_out, h_out, w_out], backend)?;
let (out_storage, out_layout) = output.storage_mut_and_layout()?;
backend.conv_transpose3d(
input.storage(),
input.layout(),
weight.storage(),
weight.layout(),
bias.map(|b| b.storage()),
stride,
padding,
output_padding,
dilation,
out_storage,
out_layout,
)?;
Ok(output)
}
#[cfg(test)]
mod tests {
use super::*;
use coeus_core::SequentialBackend;
use coeus_tensor::Tensor;
#[test]
fn conv_transpose1d_identity_kernel() {
let b = SequentialBackend::new();
let input = Tensor::from_slice(vec![1, 1, 4], &[1.0f32, 2.0, 3.0, 4.0])
.expect("invariant: test backend operation succeeds");
let weight = Tensor::from_slice(vec![1, 1, 1], &[1.0f32])
.expect("invariant: test backend operation succeeds");
let out = conv_transpose1d(&input, &weight, None, 1, 0, 0, 1, &b)
.expect("identity transposed convolution must succeed");
assert_eq!(out.shape(), &[1, 1, 4]);
assert_eq!(out.as_slice(), &[1.0, 2.0, 3.0, 4.0]);
}
#[test]
fn conv_transpose1d_stride2_upsample() {
let b = SequentialBackend::new();
let input = Tensor::from_slice(vec![1, 1, 3], &[1.0f32, 2.0, 3.0])
.expect("invariant: test backend operation succeeds");
let weight = Tensor::from_slice(vec![1, 1, 1], &[1.0f32])
.expect("invariant: test backend operation succeeds");
let out = conv_transpose1d(&input, &weight, None, 2, 0, 0, 1, &b)
.expect("strided transposed convolution must succeed");
assert_eq!(out.shape(), &[1, 1, 5]);
assert_eq!(out.as_slice(), &[1.0, 0.0, 2.0, 0.0, 3.0]);
}
#[test]
fn conv_transpose2d_identity_kernel() {
let b = SequentialBackend::new();
let input = Tensor::from_slice(vec![1, 1, 2, 2], &[1.0f32, 2.0, 3.0, 4.0])
.expect("invariant: test backend operation succeeds");
let weight = Tensor::from_slice(vec![1, 1, 1, 1], &[1.0f32])
.expect("invariant: test backend operation succeeds");
let out = conv_transpose2d(&input, &weight, None, 1, 0, 0, 1, &b)
.expect("identity transposed convolution must succeed");
assert_eq!(out.shape(), &[1, 1, 2, 2]);
assert_eq!(out.as_slice(), &[1.0, 2.0, 3.0, 4.0]);
}
}