|
| 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/runtime/kernel/kernel_includes.h> |
| 10 | + |
| 11 | +#include <sdkconfig.h> |
| 12 | + |
| 13 | +#include <esp_nn.h> |
| 14 | + |
| 15 | +#include <algorithm> |
| 16 | +#include <cstdint> |
| 17 | +#include <limits> |
| 18 | + |
| 19 | +namespace espressif::native { |
| 20 | + |
| 21 | +using executorch::aten::IntArrayRef; |
| 22 | +using executorch::aten::Tensor; |
| 23 | +using executorch::runtime::KernelRuntimeContext; |
| 24 | + |
| 25 | +// cppcheck-suppress unusedFunction |
| 26 | +Tensor& quantized_conv2d_int8_out( |
| 27 | + KernelRuntimeContext& ctx, |
| 28 | + const Tensor& input, |
| 29 | + const Tensor& weight, |
| 30 | + const std::optional<Tensor>& bias, |
| 31 | + IntArrayRef stride, |
| 32 | + IntArrayRef padding, |
| 33 | + const Tensor& output_multiplier, |
| 34 | + const Tensor& output_shift, |
| 35 | + int64_t input_offset, |
| 36 | + int64_t output_offset, |
| 37 | + const Tensor& scratch, |
| 38 | + Tensor& out) { |
| 39 | + ET_KERNEL_CHECK( |
| 40 | + ctx, |
| 41 | + input.dim() == 4 && weight.dim() == 4 && out.dim() == 4 && |
| 42 | + stride.size() == 2 && padding.size() == 2, |
| 43 | + InvalidArgument, |
| 44 | + out); |
| 45 | + const auto batches = input.size(0); |
| 46 | + const auto height = input.size(1); |
| 47 | + const auto width = input.size(2); |
| 48 | + const auto channels = input.size(3); |
| 49 | + const auto kernel_h = weight.size(1); |
| 50 | + const auto kernel_w = weight.size(2); |
| 51 | + const auto output_h = out.size(1); |
| 52 | + const auto output_w = out.size(2); |
| 53 | + const auto outputs = out.size(3); |
| 54 | + const int64_t dimensions[] = { |
| 55 | + height, |
| 56 | + width, |
| 57 | + channels, |
| 58 | + kernel_h, |
| 59 | + kernel_w, |
| 60 | + output_h, |
| 61 | + output_w, |
| 62 | + outputs, |
| 63 | + stride[0], |
| 64 | + stride[1]}; |
| 65 | + for (const auto value : dimensions) { |
| 66 | + ET_KERNEL_CHECK( |
| 67 | + ctx, |
| 68 | + value > 0 && value <= std::numeric_limits<uint16_t>::max(), |
| 69 | + InvalidArgument, |
| 70 | + out); |
| 71 | + } |
| 72 | + for (const auto value : padding) { |
| 73 | + ET_KERNEL_CHECK( |
| 74 | + ctx, |
| 75 | + value >= 0 && value <= std::numeric_limits<uint16_t>::max(), |
| 76 | + InvalidArgument, |
| 77 | + out); |
| 78 | + } |
| 79 | + const uint64_t input_bytes = uint64_t(height) * width * channels; |
| 80 | + const uint64_t output_bytes = uint64_t(output_h) * output_w * outputs; |
| 81 | + const uint64_t filter_bytes = |
| 82 | + uint64_t(kernel_h) * kernel_w * channels * outputs; |
| 83 | + const uint64_t padded_h = std::max<int64_t>( |
| 84 | + height + padding[0], (output_h - 1) * stride[0] + kernel_h); |
| 85 | + const uint64_t padded_w = std::max<int64_t>( |
| 86 | + width + padding[1], (output_w - 1) * stride[1] + kernel_w); |
| 87 | + const uint64_t aligned_channels = (uint64_t(channels) + 15) & ~uint64_t(15); |
| 88 | + const uint64_t aligned_filter_bytes = |
| 89 | + (uint64_t(kernel_w) * aligned_channels + 15) * kernel_h * outputs; |
| 90 | + const uint64_t staging_bytes = |
| 91 | + 40 * ((uint64_t(kernel_h) * kernel_w * channels + 15) & ~uint64_t(15)); |
| 92 | + constexpr auto limit = std::numeric_limits<int32_t>::max(); |
| 93 | + ET_KERNEL_CHECK( |
| 94 | + ctx, |
| 95 | + batches >= 0 && input_bytes <= limit && output_bytes <= limit && |
| 96 | + filter_bytes <= weight.nbytes() && aligned_filter_bytes <= limit && |
| 97 | + staging_bytes <= limit && |
| 98 | + padded_h <= limit / (padded_w * aligned_channels) && |
| 99 | + uint64_t(batches) <= input.nbytes() / input_bytes && |
| 100 | + uint64_t(batches) <= out.nbytes() / output_bytes && |
| 101 | + uint64_t(outputs) <= output_multiplier.nbytes() / sizeof(int32_t) && |
| 102 | + uint64_t(outputs) <= output_shift.nbytes() / sizeof(int32_t) && |
| 103 | + (!bias || uint64_t(outputs) <= bias->nbytes() / sizeof(int32_t)), |
| 104 | + InvalidArgument, |
| 105 | + out); |
| 106 | + ET_KERNEL_CHECK( |
| 107 | + ctx, |
| 108 | + padded_h * padded_w * aligned_channels + aligned_filter_bytes + |
| 109 | + staging_bytes + 8 * outputs + 16 * 1024 + 256 <= |
| 110 | + limit, |
| 111 | + InvalidArgument, |
| 112 | + out); |
| 113 | + |
| 114 | + const data_dims_t input_dims{ |
| 115 | + int32_t(width), int32_t(height), int32_t(channels), 1}; |
| 116 | + const data_dims_t filter_dims{ |
| 117 | + int32_t(kernel_w), int32_t(kernel_h), int32_t(channels), 1}; |
| 118 | + const data_dims_t output_dims{ |
| 119 | + int32_t(output_w), int32_t(output_h), int32_t(outputs), 1}; |
| 120 | + const conv_params_t params{ |
| 121 | + int32_t(input_offset), |
| 122 | + int32_t(output_offset), |
| 123 | + {int32_t(stride[1]), int32_t(stride[0])}, |
| 124 | + {int32_t(padding[1]), int32_t(padding[0])}, |
| 125 | + {1, 1}, |
| 126 | + {-128, 127}}; |
| 127 | + const quant_data_t quant{ |
| 128 | + const_cast<int32_t*>(output_shift.const_data_ptr<int32_t>()), |
| 129 | + const_cast<int32_t*>(output_multiplier.const_data_ptr<int32_t>())}; |
| 130 | + const auto required = esp_nn_get_conv_scratch_size( |
| 131 | + &input_dims, &filter_dims, &output_dims, ¶ms); |
| 132 | + ET_KERNEL_CHECK( |
| 133 | + ctx, |
| 134 | + required >= 0 && size_t(required) <= scratch.nbytes(), |
| 135 | + InvalidArgument, |
| 136 | + out); |
| 137 | + esp_nn_set_conv_scratch_buf(scratch.mutable_data_ptr<uint8_t>()); |
| 138 | + for (int64_t batch = 0; batch < batches; ++batch) { |
| 139 | + esp_nn_conv_s8( |
| 140 | + &input_dims, |
| 141 | + input.const_data_ptr<int8_t>() + batch * input_bytes, |
| 142 | + &filter_dims, |
| 143 | + weight.const_data_ptr<int8_t>(), |
| 144 | + bias ? bias->const_data_ptr<int32_t>() : nullptr, |
| 145 | + &output_dims, |
| 146 | + out.mutable_data_ptr<int8_t>() + batch * output_bytes, |
| 147 | + ¶ms, |
| 148 | + &quant); |
| 149 | + } |
| 150 | + esp_nn_set_conv_scratch_buf(nullptr); |
| 151 | + return out; |
| 152 | +} |
| 153 | + |
| 154 | +} // namespace espressif::native |
0 commit comments