|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +#include <executorch/kernels/portable/cpu/util/kernel_ops_util.h> |
| 10 | +#include <executorch/runtime/kernel/kernel_includes.h> |
| 11 | + |
| 12 | +namespace torch { |
| 13 | +namespace executor { |
| 14 | +namespace native { |
| 15 | + |
| 16 | +using Tensor = executorch::aten::Tensor; |
| 17 | +using ScalarType = executorch::aten::ScalarType; |
| 18 | +using IntArrayRef = executorch::aten::ArrayRef<int64_t>; |
| 19 | + |
| 20 | +namespace { |
| 21 | + |
| 22 | +bool check_max_pool2d_backward_args( |
| 23 | + const Tensor& grad_output, |
| 24 | + const Tensor& input, |
| 25 | + IntArrayRef kernel_size, |
| 26 | + IntArrayRef stride, |
| 27 | + IntArrayRef padding, |
| 28 | + IntArrayRef dilation, |
| 29 | + bool ceil_mode, |
| 30 | + const Tensor& indices, |
| 31 | + const Tensor& grad_input) { |
| 32 | + ET_LOG_AND_RETURN_IF_FALSE(tensors_have_same_dtype(grad_output, input)); |
| 33 | + ET_LOG_AND_RETURN_IF_FALSE(tensors_have_same_dtype(grad_input, input)); |
| 34 | + |
| 35 | + ET_CHECK_OR_RETURN_FALSE( |
| 36 | + check_max_pool2d_with_indices_args( |
| 37 | + input, |
| 38 | + kernel_size, |
| 39 | + stride, |
| 40 | + padding, |
| 41 | + dilation, |
| 42 | + ceil_mode, |
| 43 | + grad_output, |
| 44 | + indices), |
| 45 | + "Invalid max_pool_2d arguments"); |
| 46 | + |
| 47 | + size_t output_ndim = 0; |
| 48 | + executorch::aten::SizesType output_sizes[kTensorDimensionLimit]; |
| 49 | + get_max_pool2d_with_indices_out_target_size( |
| 50 | + input, |
| 51 | + kernel_size, |
| 52 | + stride, |
| 53 | + padding, |
| 54 | + dilation, |
| 55 | + ceil_mode, |
| 56 | + output_sizes, |
| 57 | + &output_ndim); |
| 58 | + |
| 59 | + ET_LOG_AND_RETURN_IF_FALSE( |
| 60 | + output_size_is_valid({output_sizes, output_ndim}, 2)); |
| 61 | + |
| 62 | + ET_CHECK_OR_RETURN_FALSE( |
| 63 | + grad_output.dim() == input.dim(), |
| 64 | + "grad_output should have same number of dimensions as input"); |
| 65 | + |
| 66 | + ET_LOG_AND_RETURN_IF_FALSE( |
| 67 | + tensor_has_expected_size(grad_output, {output_sizes, output_ndim})); |
| 68 | + |
| 69 | + return true; |
| 70 | +} |
| 71 | + |
| 72 | +template <typename CTYPE, bool is_3d> |
| 73 | +void max_pool_backward_impl( |
| 74 | + const Tensor& grad_input, |
| 75 | + const Tensor& grad_output, |
| 76 | + const Tensor& indices) { |
| 77 | + const CTYPE* grad_output_data = grad_output.const_data_ptr<CTYPE>(); |
| 78 | + const int64_t* indices_data = indices.const_data_ptr<int64_t>(); |
| 79 | + CTYPE* grad_input_data = grad_input.mutable_data_ptr<CTYPE>(); |
| 80 | + |
| 81 | + // treat batch size and channels as one dimension |
| 82 | + // |
| 83 | + // MaxPool2d: |
| 84 | + // ndim == 3: CHW |
| 85 | + // ndim == 4: NCHW |
| 86 | + // |
| 87 | + // MaxPool3d: |
| 88 | + // ndim == 4: CDHW |
| 89 | + // ndim == 5: NCDHW |
| 90 | + int64_t ndim = grad_output.dim(); |
| 91 | + int64_t channels; |
| 92 | + if (is_3d) { |
| 93 | + channels = ndim == 4 ? grad_output.size(0) |
| 94 | + : grad_output.size(0) * grad_output.size(1); |
| 95 | + } else { |
| 96 | + channels = ndim == 3 ? grad_output.size(0) |
| 97 | + : grad_output.size(0) * grad_output.size(1); |
| 98 | + } |
| 99 | + int64_t input_depth = is_3d ? grad_input.size(-3) : 1; |
| 100 | + |
| 101 | + int64_t input_height = grad_input.size(ndim - 2); |
| 102 | + int64_t input_width = grad_input.size(ndim - 1); |
| 103 | + int64_t output_depth = is_3d ? grad_output.size(ndim - 3) : 1; |
| 104 | + int64_t output_height = grad_output.size(ndim - 2); |
| 105 | + int64_t output_width = grad_output.size(ndim - 1); |
| 106 | + |
| 107 | + for (int64_t c = 0; c < channels; ++c) { |
| 108 | + CTYPE* grad_input_ptr = |
| 109 | + grad_input_data + c * input_depth * input_height * input_width; |
| 110 | + const CTYPE* grad_output_ptr = |
| 111 | + grad_output_data + c * output_depth * output_height * output_width; |
| 112 | + const int64_t * indices_ptr = |
| 113 | + indices_data + c * output_depth * output_height * output_width; |
| 114 | + |
| 115 | + for (int64_t od = 0; od < output_depth; od++) { |
| 116 | + for (int64_t oh = 0; oh < output_height; oh++) { |
| 117 | + for (int64_t ow = 0; ow < output_width; ow++) { |
| 118 | + // retrieve position of max |
| 119 | + int64_t index = |
| 120 | + od * output_height * output_width + oh * output_width + ow; |
| 121 | + int64_t maxindex = indices_ptr[index]; |
| 122 | + if (maxindex != -1) { |
| 123 | + // update gradient |
| 124 | + grad_input_ptr[maxindex] += grad_output_ptr[index]; |
| 125 | + } |
| 126 | + } |
| 127 | + } |
| 128 | + } |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +} // namespace |
| 133 | + |
| 134 | +Tensor& max_pool2d_with_indices_backward_out( |
| 135 | + KernelRuntimeContext& ctx, |
| 136 | + const Tensor& grad_output, |
| 137 | + const Tensor& input, |
| 138 | + ET_UNUSED IntArrayRef kernel_size, |
| 139 | + ET_UNUSED IntArrayRef stride, |
| 140 | + ET_UNUSED IntArrayRef padding, |
| 141 | + ET_UNUSED IntArrayRef dilation, |
| 142 | + ET_UNUSED bool ceil_mode, |
| 143 | + const Tensor& indices, |
| 144 | + Tensor& grad_input) { |
| 145 | + (void)ctx; |
| 146 | + |
| 147 | + ET_KERNEL_CHECK( |
| 148 | + ctx, |
| 149 | + check_max_pool2d_backward_args( |
| 150 | + grad_output, |
| 151 | + input, |
| 152 | + kernel_size, |
| 153 | + stride, |
| 154 | + padding, |
| 155 | + dilation, |
| 156 | + ceil_mode, |
| 157 | + indices, |
| 158 | + grad_input), |
| 159 | + InvalidArgument, |
| 160 | + grad_input); |
| 161 | + |
| 162 | + ET_KERNEL_CHECK( |
| 163 | + ctx, |
| 164 | + resize_tensor(grad_input, input.sizes()) == Error::Ok, |
| 165 | + InvalidArgument, |
| 166 | + grad_input); |
| 167 | + |
| 168 | + constexpr auto name = "max_pool2d_with_indices_backward.grad_input"; |
| 169 | + |
| 170 | + ET_SWITCH_FLOATHBF16_TYPES(input.scalar_type(), ctx, name, CTYPE, [&]() { |
| 171 | + max_pool_backward_impl<CTYPE, false>(grad_input, grad_output, indices); |
| 172 | + }); |
| 173 | + |
| 174 | + return grad_input; |
| 175 | +} |
| 176 | + |
| 177 | +} // namespace native |
| 178 | +} // namespace executor |
| 179 | +} // namespace torch |
0 commit comments