|
| 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 "cortex_m_ops_common.h" |
| 10 | + |
| 11 | +extern "C" { |
| 12 | +#include "arm_nnfunctions.h" |
| 13 | +} |
| 14 | + |
| 15 | +namespace cortex_m { |
| 16 | +namespace native { |
| 17 | + |
| 18 | +using KernelRuntimeContext = torch::executor::KernelRuntimeContext; |
| 19 | + |
| 20 | +namespace { |
| 21 | +constexpr int64_t kConvDim = 4; |
| 22 | + |
| 23 | +bool validate_depthwise_conv2d_arguments( |
| 24 | + KernelRuntimeContext& context, |
| 25 | + const Tensor& input, |
| 26 | + const Tensor& weight, |
| 27 | + const torch::executor::optional<Tensor>& bias, |
| 28 | + const Tensor& output, |
| 29 | + const IntArrayRef& stride, |
| 30 | + const IntArrayRef& padding, |
| 31 | + const IntArrayRef& dilation, |
| 32 | + const int64_t depth_multiplier, |
| 33 | + const Tensor& requantize_multipliers, |
| 34 | + const Tensor& requantize_shifts) { |
| 35 | + if (input.dim() != kConvDim || weight.dim() != kConvDim || |
| 36 | + output.dim() != kConvDim) { |
| 37 | + ET_LOG(Error, "quantized_depthwise_conv2d_out: tensors must be 4-D"); |
| 38 | + context.fail(Error::InvalidArgument); |
| 39 | + return false; |
| 40 | + } |
| 41 | + |
| 42 | + // CMSIS-NN depthwise convolution only supports batch size of 1 |
| 43 | + if (input.size(0) != 1) { |
| 44 | + ET_LOG( |
| 45 | + Error, |
| 46 | + "quantized_depthwise_conv2d_out: batch size must be 1, got %zd", |
| 47 | + input.size(0)); |
| 48 | + context.fail(Error::InvalidArgument); |
| 49 | + return false; |
| 50 | + } |
| 51 | + |
| 52 | + // Validate weight is in IHWO layout: [1, H, W, C_OUT] |
| 53 | + if (weight.size(0) != 1) { |
| 54 | + ET_LOG( |
| 55 | + Error, |
| 56 | + "quantized_depthwise_conv2d_out: weight dim 0 must be 1, got %zd", |
| 57 | + weight.size(0)); |
| 58 | + context.fail(Error::InvalidArgument); |
| 59 | + return false; |
| 60 | + } |
| 61 | + |
| 62 | + const int64_t weight_output_channels = weight.size(3); |
| 63 | + const int64_t output_channels = output.size(1); |
| 64 | + if (weight_output_channels != output_channels) { |
| 65 | + ET_LOG( |
| 66 | + Error, |
| 67 | + "quantized_depthwise_conv2d_out: weight out_ch (%zd) != out_ch (%zd)", |
| 68 | + weight_output_channels, |
| 69 | + output_channels); |
| 70 | + context.fail(Error::InvalidArgument); |
| 71 | + return false; |
| 72 | + } |
| 73 | + |
| 74 | + if (!is_channels_last_tensor(input)) { |
| 75 | + ET_LOG( |
| 76 | + Error, "quantized_depthwise_conv2d_out: input must be channels_last"); |
| 77 | + context.fail(Error::InvalidArgument); |
| 78 | + return false; |
| 79 | + } |
| 80 | + |
| 81 | + if (!is_channels_last_tensor(output)) { |
| 82 | + ET_LOG( |
| 83 | + Error, "quantized_depthwise_conv2d_out: output must be channels_last"); |
| 84 | + context.fail(Error::InvalidArgument); |
| 85 | + return false; |
| 86 | + } |
| 87 | + |
| 88 | + if (input.scalar_type() != ScalarType::Char || |
| 89 | + output.scalar_type() != ScalarType::Char) { |
| 90 | + ET_LOG( |
| 91 | + Error, "quantized_depthwise_conv2d_out: input and output must be int8"); |
| 92 | + context.fail(Error::InvalidArgument); |
| 93 | + return false; |
| 94 | + } |
| 95 | + |
| 96 | + if (weight.scalar_type() != ScalarType::Char) { |
| 97 | + ET_LOG(Error, "quantized_depthwise_conv2d_out: weight must be int8"); |
| 98 | + context.fail(Error::InvalidArgument); |
| 99 | + return false; |
| 100 | + } |
| 101 | + |
| 102 | + if (bias.has_value() && bias.value().scalar_type() != ScalarType::Int) { |
| 103 | + ET_LOG(Error, "quantized_depthwise_conv2d_out: bias must be int32"); |
| 104 | + context.fail(Error::InvalidArgument); |
| 105 | + return false; |
| 106 | + } |
| 107 | + |
| 108 | + if (stride.size() != 2 || padding.size() != 2 || dilation.size() != 2) { |
| 109 | + ET_LOG( |
| 110 | + Error, |
| 111 | + "quantized_depthwise_conv2d_out: stride/padding/dilation must have length 2"); |
| 112 | + context.fail(Error::InvalidArgument); |
| 113 | + return false; |
| 114 | + } |
| 115 | + |
| 116 | + const int64_t input_channels = input.size(1); |
| 117 | + // output_channels already extracted above for weight validation |
| 118 | + if (output_channels != input_channels * depth_multiplier) { |
| 119 | + ET_LOG( |
| 120 | + Error, |
| 121 | + "quantized_depthwise_conv2d_out: out_ch (%zd) != in_ch (%zd) * depth_mult (%zd)", |
| 122 | + output_channels, |
| 123 | + input_channels, |
| 124 | + depth_multiplier); |
| 125 | + context.fail(Error::InvalidArgument); |
| 126 | + return false; |
| 127 | + } |
| 128 | + |
| 129 | + if (requantize_multipliers.size(0) != output_channels || |
| 130 | + requantize_shifts.size(0) != output_channels) { |
| 131 | + ET_LOG( |
| 132 | + Error, |
| 133 | + "quantized_depthwise_conv2d_out: per-ch params size != out_ch (%zd)", |
| 134 | + output_channels); |
| 135 | + context.fail(Error::InvalidArgument); |
| 136 | + return false; |
| 137 | + } |
| 138 | + |
| 139 | + return true; |
| 140 | +} |
| 141 | +} // namespace |
| 142 | + |
| 143 | +Tensor& quantized_depthwise_conv2d_out( |
| 144 | + KernelRuntimeContext& context, |
| 145 | + const Tensor& input, |
| 146 | + const Tensor& weight, |
| 147 | + const torch::executor::optional<Tensor>& bias, |
| 148 | + const IntArrayRef stride, |
| 149 | + const IntArrayRef padding, |
| 150 | + const IntArrayRef dilation, |
| 151 | + const int64_t depth_multiplier, |
| 152 | + const int64_t input_offset, |
| 153 | + const int64_t output_offset, |
| 154 | + const Tensor& requantize_multipliers, |
| 155 | + const Tensor& requantize_shifts, |
| 156 | + const int64_t activation_min, |
| 157 | + const int64_t activation_max, |
| 158 | + Tensor& out) { |
| 159 | + if (!validate_depthwise_conv2d_arguments( |
| 160 | + context, |
| 161 | + input, |
| 162 | + weight, |
| 163 | + bias, |
| 164 | + out, |
| 165 | + stride, |
| 166 | + padding, |
| 167 | + dilation, |
| 168 | + depth_multiplier, |
| 169 | + requantize_multipliers, |
| 170 | + requantize_shifts)) { |
| 171 | + return out; |
| 172 | + } |
| 173 | + |
| 174 | + const int32_t batch = static_cast<int32_t>(input.size(0)); |
| 175 | + const int32_t input_channels = static_cast<int32_t>(input.size(1)); |
| 176 | + const int32_t input_height = static_cast<int32_t>(input.size(2)); |
| 177 | + const int32_t input_width = static_cast<int32_t>(input.size(3)); |
| 178 | + |
| 179 | + // Weight is in IHWO layout after permutation in the pass: [1, H, W, C_OUT] |
| 180 | + // For depthwise conv, this matches CMSIS-NN's expected format |
| 181 | + const int32_t kernel_height = static_cast<int32_t>(weight.size(1)); |
| 182 | + const int32_t kernel_width = static_cast<int32_t>(weight.size(2)); |
| 183 | + |
| 184 | + const int32_t output_channels = static_cast<int32_t>(out.size(1)); |
| 185 | + const int32_t output_height = static_cast<int32_t>(out.size(2)); |
| 186 | + const int32_t output_width = static_cast<int32_t>(out.size(3)); |
| 187 | + |
| 188 | + const int32_t depth_multiplier_val = static_cast<int32_t>(depth_multiplier); |
| 189 | + |
| 190 | + const int32_t input_offset_val = static_cast<int32_t>(input_offset); |
| 191 | + const int32_t output_offset_val = static_cast<int32_t>(output_offset); |
| 192 | + const int32_t activation_min_val = static_cast<int32_t>(activation_min); |
| 193 | + const int32_t activation_max_val = static_cast<int32_t>(activation_max); |
| 194 | + |
| 195 | + const cmsis_nn_dims input_dims{ |
| 196 | + batch, input_height, input_width, input_channels}; |
| 197 | + const cmsis_nn_dims filter_dims{ |
| 198 | + 1, kernel_height, kernel_width, output_channels}; |
| 199 | + const cmsis_nn_dims output_dims{ |
| 200 | + batch, output_height, output_width, output_channels}; |
| 201 | + const cmsis_nn_dims bias_dims{1, 1, 1, output_channels}; |
| 202 | + |
| 203 | + cmsis_nn_dw_conv_params dw_conv_params; |
| 204 | + dw_conv_params.input_offset = input_offset_val; |
| 205 | + dw_conv_params.output_offset = output_offset_val; |
| 206 | + dw_conv_params.ch_mult = depth_multiplier_val; |
| 207 | + dw_conv_params.stride.h = static_cast<const int32_t>(stride[0]); |
| 208 | + dw_conv_params.stride.w = static_cast<const int32_t>(stride[1]); |
| 209 | + dw_conv_params.padding.h = static_cast<const int32_t>(padding[0]); |
| 210 | + dw_conv_params.padding.w = static_cast<const int32_t>(padding[1]); |
| 211 | + dw_conv_params.dilation.h = static_cast<const int32_t>(dilation[0]); |
| 212 | + dw_conv_params.dilation.w = static_cast<const int32_t>(dilation[1]); |
| 213 | + dw_conv_params.activation.min = activation_min_val; |
| 214 | + dw_conv_params.activation.max = activation_max_val; |
| 215 | + |
| 216 | + cmsis_nn_per_channel_quant_params quant_params; |
| 217 | + quant_params.multiplier = requantize_multipliers.data_ptr<int32_t>(); |
| 218 | + quant_params.shift = requantize_shifts.data_ptr<int32_t>(); |
| 219 | + |
| 220 | + const int8_t* input_data = input.const_data_ptr<int8_t>(); |
| 221 | + const int8_t* weight_data = weight.const_data_ptr<int8_t>(); |
| 222 | + int8_t* output_data = out.mutable_data_ptr<int8_t>(); |
| 223 | + const int32_t* bias_data = |
| 224 | + bias.has_value() ? bias.value().const_data_ptr<int32_t>() : nullptr; |
| 225 | + |
| 226 | + cmsis_nn_context cmsis_context; |
| 227 | + cmsis_context.buf = nullptr; |
| 228 | + cmsis_context.size = 0; |
| 229 | + |
| 230 | + const int32_t buffer_bytes = arm_depthwise_conv_wrapper_s8_get_buffer_size( |
| 231 | + &dw_conv_params, &input_dims, &filter_dims, &output_dims); |
| 232 | + if (buffer_bytes < 0) { |
| 233 | + ET_LOG( |
| 234 | + Error, |
| 235 | + "quantized_depthwise_conv2d_out: CMSIS-NN buffer size calculation failed"); |
| 236 | + context.fail(Error::Internal); |
| 237 | + return out; |
| 238 | + } |
| 239 | + |
| 240 | + auto buffer_or_error = context.allocate_temp( |
| 241 | + static_cast<size_t>(buffer_bytes), alignof(int16_t)); |
| 242 | + if (!buffer_or_error.ok()) { |
| 243 | + ET_LOG( |
| 244 | + Error, |
| 245 | + "quantized_depthwise_conv2d_out: failed to allocate scratch buffer (%d bytes, error %d)", |
| 246 | + static_cast<int>(buffer_bytes), |
| 247 | + static_cast<int>(buffer_or_error.error())); |
| 248 | + context.fail(buffer_or_error.error()); |
| 249 | + return out; |
| 250 | + } |
| 251 | + cmsis_context.buf = buffer_or_error.get(); |
| 252 | + cmsis_context.size = buffer_bytes; |
| 253 | + |
| 254 | + const arm_cmsis_nn_status status = arm_depthwise_conv_wrapper_s8( |
| 255 | + &cmsis_context, |
| 256 | + &dw_conv_params, |
| 257 | + &quant_params, |
| 258 | + &input_dims, |
| 259 | + input_data, |
| 260 | + &filter_dims, |
| 261 | + weight_data, |
| 262 | + &bias_dims, |
| 263 | + bias_data, |
| 264 | + &output_dims, |
| 265 | + output_data); |
| 266 | + |
| 267 | + if (status != ARM_CMSIS_NN_SUCCESS) { |
| 268 | + ET_LOG( |
| 269 | + Error, |
| 270 | + "quantized_depthwise_conv2d_out: arm_depthwise_conv_wrapper_s8 failed with status %d", |
| 271 | + status); |
| 272 | + context.fail(Error::Internal); |
| 273 | + } |
| 274 | + |
| 275 | + return out; |
| 276 | +} |
| 277 | + |
| 278 | +} // namespace native |
| 279 | +} // namespace cortex_m |
0 commit comments