leto_ops/application/convolution/transposed/
backward.rs1use super::plan::TransposedConvolutionPlan;
2use crate::application::convolution::coordinates::decode_index;
3use crate::domain::scalar::Scalar;
4use leto::{ArrayView, ArrayViewMut, Result, TransposedConvolutionParameters};
5
6pub struct TransposedConvolutionGradients<'target, 'view, T, const R: usize> {
11 grad_input: Option<&'target mut ArrayViewMut<'view, T, R>>,
12 grad_weight: Option<&'target mut ArrayViewMut<'view, T, R>>,
13 grad_bias: Option<&'target mut ArrayViewMut<'view, T, 1>>,
14}
15
16impl<'target, 'view, T, const R: usize> TransposedConvolutionGradients<'target, 'view, T, R> {
17 #[must_use]
19 pub const fn new(
20 grad_input: Option<&'target mut ArrayViewMut<'view, T, R>>,
21 grad_weight: Option<&'target mut ArrayViewMut<'view, T, R>>,
22 grad_bias: Option<&'target mut ArrayViewMut<'view, T, 1>>,
23 ) -> Self {
24 Self {
25 grad_input,
26 grad_weight,
27 grad_bias,
28 }
29 }
30}
31
32pub fn convolution_transposed_backward_accumulate<T: Scalar, const R: usize, const D: usize>(
48 input: &ArrayView<'_, T, R>,
49 weight: &ArrayView<'_, T, R>,
50 grad_output: &ArrayView<'_, T, R>,
51 parameters: TransposedConvolutionParameters<D>,
52 mut gradients: TransposedConvolutionGradients<'_, '_, T, R>,
53) -> Result<()> {
54 let plan = TransposedConvolutionPlan::validate_backward(
55 input,
56 weight,
57 grad_output,
58 parameters,
59 gradients.grad_input.as_deref(),
60 gradients.grad_weight.as_deref(),
61 gradients.grad_bias.as_deref(),
62 )?;
63
64 if let Some(target) = gradients.grad_input.as_mut() {
65 accumulate_input_gradient(weight, grad_output, &plan, target);
66 }
67 if let Some(target) = gradients.grad_weight.as_mut() {
68 accumulate_weight_gradient(input, grad_output, &plan, target);
69 }
70 if let Some(target) = gradients.grad_bias.as_mut() {
71 accumulate_bias_gradient(grad_output, &plan, target);
72 }
73 Ok(())
74}
75
76fn accumulate_input_gradient<T: Scalar, const R: usize, const D: usize>(
77 weight: &ArrayView<'_, T, R>,
78 grad_output: &ArrayView<'_, T, R>,
79 plan: &TransposedConvolutionPlan<D>,
80 grad_input: &mut ArrayViewMut<'_, T, R>,
81) {
82 let mut input_index = [0; R];
83 let mut weight_index = [0; R];
84 let mut output_index = [0; R];
85 let mut kernel_spatial = [0; D];
86
87 for flat_input in 0..plan.input_elements {
88 decode_index(flat_input, &grad_input.shape(), &mut input_index);
89 let mut sum = T::ZERO;
90 for output_channel in 0..plan.output_channels {
91 weight_index[0] = input_index[1];
92 weight_index[1] = output_channel;
93 for flat_kernel in 0..plan.kernel_elements {
94 decode_index(flat_kernel, &plan.kernel_spatial, &mut kernel_spatial);
95 if map_output_position(
96 &input_index,
97 &kernel_spatial,
98 plan,
99 &mut output_index,
100 &mut weight_index,
101 ) {
102 output_index[0] = input_index[0];
103 output_index[1] = output_channel;
104 sum += grad_output[output_index] * weight[weight_index];
105 }
106 }
107 }
108 grad_input[input_index] += sum;
109 }
110}
111
112fn accumulate_weight_gradient<T: Scalar, const R: usize, const D: usize>(
113 input: &ArrayView<'_, T, R>,
114 grad_output: &ArrayView<'_, T, R>,
115 plan: &TransposedConvolutionPlan<D>,
116 grad_weight: &mut ArrayViewMut<'_, T, R>,
117) {
118 let mut weight_index = [0; R];
119 let mut input_index = [0; R];
120 let mut output_index = [0; R];
121 let mut kernel_spatial = [0; D];
122 let mut input_spatial = [0; D];
123
124 for flat_weight in 0..plan.weight_elements {
125 decode_index(flat_weight, &grad_weight.shape(), &mut weight_index);
126 kernel_spatial.copy_from_slice(&weight_index[2..]);
127 let mut sum = T::ZERO;
128 for batch in 0..plan.batch {
129 input_index[0] = batch;
130 input_index[1] = weight_index[0];
131 for flat_input in 0..plan.input_spatial_elements {
132 decode_index(flat_input, &plan.input_spatial, &mut input_spatial);
133 input_index[2..].copy_from_slice(&input_spatial);
134 if map_output_position(
135 &input_index,
136 &kernel_spatial,
137 plan,
138 &mut output_index,
139 &mut weight_index,
140 ) {
141 output_index[0] = batch;
142 output_index[1] = weight_index[1];
143 sum += input[input_index] * grad_output[output_index];
144 }
145 }
146 }
147 grad_weight[weight_index] += sum;
148 }
149}
150
151fn accumulate_bias_gradient<T: Scalar, const R: usize, const D: usize>(
152 grad_output: &ArrayView<'_, T, R>,
153 plan: &TransposedConvolutionPlan<D>,
154 grad_bias: &mut ArrayViewMut<'_, T, 1>,
155) {
156 let mut output_index = [0; R];
157 let mut output_spatial = [0; D];
158 for output_channel in 0..plan.output_channels {
159 let mut sum = T::ZERO;
160 for batch in 0..plan.batch {
161 for flat_output in 0..plan.output_spatial_elements {
162 decode_index(flat_output, &plan.output_spatial, &mut output_spatial);
163 output_index[0] = batch;
164 output_index[1] = output_channel;
165 output_index[2..].copy_from_slice(&output_spatial);
166 sum += grad_output[output_index];
167 }
168 }
169 grad_bias[output_channel] += sum;
170 }
171}
172
173fn map_output_position<const R: usize, const D: usize>(
174 input_index: &[usize; R],
175 kernel_spatial: &[usize; D],
176 plan: &TransposedConvolutionPlan<D>,
177 output_index: &mut [usize; R],
178 weight_index: &mut [usize; R],
179) -> bool {
180 for axis in 0..D {
181 let padded_position = input_index[axis + 2] * plan.parameters.stride()[axis]
184 + kernel_spatial[axis] * plan.parameters.dilation()[axis];
185 if padded_position < plan.parameters.padding()[axis] {
186 return false;
187 }
188 let output_position = padded_position - plan.parameters.padding()[axis];
189 if output_position >= plan.output_spatial[axis] {
190 return false;
191 }
192 output_index[axis + 2] = output_position;
193 weight_index[axis + 2] = kernel_spatial[axis];
194 }
195 true
196}