use crate::dtype::DType;
use crate::error::Result;
use crate::ops::{BinaryOps, PaddingMode, ReduceOps, ScalarOps, TensorOps};
use crate::runtime::Runtime;
use super::super::conv_common::compute_padding_2d;
pub(super) fn conv2d_input_backward<R, C>(
client: &C,
grad_output: &crate::tensor::Tensor<R>,
weight: &crate::tensor::Tensor<R>,
input_shape: &[usize],
stride: (usize, usize),
padding: PaddingMode,
dilation: (usize, usize),
groups: usize,
) -> Result<crate::tensor::Tensor<R>>
where
R: Runtime<DType = DType>,
C: TensorOps<R> + BinaryOps<R> + ReduceOps<R> + ScalarOps<R>,
{
let batch = input_shape[0];
let _c_in = input_shape[1];
let input_h = input_shape[2];
let input_w = input_shape[3];
let c_out = weight.shape()[0];
let c_in_per_group = weight.shape()[1];
let kernel_h = weight.shape()[2];
let kernel_w = weight.shape()[3];
let output_h = grad_output.shape()[2];
let output_w = grad_output.shape()[3];
let c_out_per_group = c_out / groups;
let (pad_top, _pad_bottom, pad_left, _pad_right) =
compute_padding_2d(padding, kernel_h, kernel_w, dilation.0, dilation.1);
let device = grad_output.device();
let dtype = grad_output.dtype();
let mut d_input = crate::tensor::Tensor::<R>::zeros(input_shape, dtype, device)?;
for kh in 0..kernel_h {
for kw in 0..kernel_w {
let weight_kh = weight.narrow(2, kh, 1)?;
let weight_khkw = weight_kh.narrow(3, kw, 1)?;
let weight_2d = weight_khkw.squeeze(Some(3)).squeeze(Some(2));
for oh in 0..output_h {
let ih_pos = oh * stride.0 + kh * dilation.0;
if ih_pos < pad_top || ih_pos >= pad_top + input_h {
continue;
}
let ih = ih_pos - pad_top;
for ow in 0..output_w {
let iw_pos = ow * stride.1 + kw * dilation.1;
if iw_pos < pad_left || iw_pos >= pad_left + input_w {
continue;
}
let iw = iw_pos - pad_left;
let grad_o = grad_output.narrow(2, oh, 1)?.narrow(3, ow, 1)?;
let grad_o_2d = grad_o.squeeze(Some(3)).squeeze(Some(2));
for g in 0..groups {
let c_in_start = g * c_in_per_group;
let c_out_start = g * c_out_per_group;
let grad_g = grad_o_2d.narrow(1, c_out_start, c_out_per_group)?;
let weight_g = weight_2d.narrow(0, c_out_start, c_out_per_group)?;
let contrib_g = client.matmul(&grad_g, &weight_g)?;
let contrib_4d = contrib_g.reshape(&[batch, c_in_per_group, 1, 1])?;
let mut d_input_at = d_input.narrow(2, ih, 1)?.narrow(3, iw, 1)?;
let d_input_group = d_input_at.narrow(1, c_in_start, c_in_per_group)?;
let updated_group = client.add(&d_input_group, &contrib_4d)?;
d_input_at =
client.slice_assign(&d_input_at, &updated_group, 1, c_in_start)?;
let mut d_input_h = d_input.narrow(2, ih, 1)?;
d_input_h = client.slice_assign(&d_input_h, &d_input_at, 3, iw)?;
d_input = client.slice_assign(&d_input, &d_input_h, 2, ih)?;
}
}
}
}
}
Ok(d_input)
}
pub(super) fn conv2d_weight_backward<R, C>(
client: &C,
grad_output: &crate::tensor::Tensor<R>,
input: &crate::tensor::Tensor<R>,
weight_shape: &[usize],
stride: (usize, usize),
padding: PaddingMode,
dilation: (usize, usize),
groups: usize,
) -> Result<crate::tensor::Tensor<R>>
where
R: Runtime<DType = DType>,
C: TensorOps<R> + BinaryOps<R> + ReduceOps<R> + ScalarOps<R>,
{
let _batch = input.shape()[0];
let _c_in = input.shape()[1];
let input_h = input.shape()[2];
let input_w = input.shape()[3];
let c_out = weight_shape[0];
let c_in_per_group = weight_shape[1];
let kernel_h = weight_shape[2];
let kernel_w = weight_shape[3];
let output_h = grad_output.shape()[2];
let output_w = grad_output.shape()[3];
let c_out_per_group = c_out / groups;
let (pad_top, _pad_bottom, pad_left, _pad_right) =
compute_padding_2d(padding, kernel_h, kernel_w, dilation.0, dilation.1);
let device = grad_output.device();
let dtype = grad_output.dtype();
let mut d_weight = crate::tensor::Tensor::<R>::zeros(weight_shape, dtype, device)?;
for oh in 0..output_h {
for ow in 0..output_w {
let grad_o = grad_output.narrow(2, oh, 1)?.narrow(3, ow, 1)?;
let grad_o_2d = grad_o.squeeze(Some(3)).squeeze(Some(2));
for kh in 0..kernel_h {
let ih_pos = oh * stride.0 + kh * dilation.0;
if ih_pos < pad_top || ih_pos >= pad_top + input_h {
continue;
}
let ih = ih_pos - pad_top;
for kw in 0..kernel_w {
let iw_pos = ow * stride.1 + kw * dilation.1;
if iw_pos < pad_left || iw_pos >= pad_left + input_w {
continue;
}
let iw = iw_pos - pad_left;
let input_hw = input.narrow(2, ih, 1)?.narrow(3, iw, 1)?;
let input_2d = input_hw.squeeze(Some(3)).squeeze(Some(2));
for g in 0..groups {
let c_in_start = g * c_in_per_group;
let c_out_start = g * c_out_per_group;
let input_g = input_2d.narrow(1, c_in_start, c_in_per_group)?;
let grad_g = grad_o_2d.narrow(1, c_out_start, c_out_per_group)?;
let contrib_2d = client.matmul(&grad_g.transpose(0, 1)?, &input_g)?;
let contrib_4d =
contrib_2d.reshape(&[c_out_per_group, c_in_per_group, 1, 1])?;
let mut d_weight_at = d_weight.narrow(2, kh, 1)?.narrow(3, kw, 1)?;
let d_weight_group = d_weight_at.narrow(0, c_out_start, c_out_per_group)?;
let updated_group = client.add(&d_weight_group, &contrib_4d)?;
d_weight_at =
client.slice_assign(&d_weight_at, &updated_group, 0, c_out_start)?;
let mut d_weight_kh = d_weight.narrow(2, kh, 1)?;
d_weight_kh = client.slice_assign(&d_weight_kh, &d_weight_at, 3, kw)?;
d_weight = client.slice_assign(&d_weight, &d_weight_kh, 2, kh)?;
}
}
}
}
}
Ok(d_weight)
}