File size: 3,270 Bytes
ce7fc72
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
// Copyright (c) 2026 Eoin Jordan
// SPDX-License-Identifier: Apache-2.0
//
// Registers the two Cortex-M kernels PoseMicro's int8 program calls with the ExecuTorch runtime of a PC, so
// train/export_pose.py can run the program before it goes on a board. build.sh compiles this file with
// ExecuTorch's own kernel sources and CMSIS-NN in plain C, the code the firmware builds for the ESP32-S3.
//
// The firmware gets this registration from ExecuTorch's code generator instead. The argument order below
// is that of backends/cortex_m/ops/operators.yaml in ExecuTorch v1.5.1.

#include <optional>

#include <executorch/runtime/core/evalue.h>
#include <executorch/runtime/core/exec_aten/exec_aten.h>
#include <executorch/runtime/kernel/kernel_runtime_context.h>
#include <executorch/runtime/kernel/operator_registry.h>

using executorch::aten::Tensor;
using executorch::runtime::EValue;
using executorch::runtime::Kernel;
using executorch::runtime::KernelRuntimeContext;
using executorch::runtime::Span;
using Int64ArrayRef = executorch::aten::ArrayRef<int64_t>;

namespace cortex_m {
namespace native {

Tensor& quantized_conv2d_out(
    KernelRuntimeContext& context, const Tensor& input, const Tensor& weight,
    const std::optional<Tensor>& bias, const Int64ArrayRef stride, const Int64ArrayRef padding,
    const Int64ArrayRef dilation, const int64_t input_offset, const int64_t output_offset,
    const Tensor& requantize_multipliers, const Tensor& requantize_shifts, const int64_t activation_min,
    const int64_t activation_max, const Tensor& scratch, Tensor& out);

Tensor& quantized_depthwise_conv2d_out(
    KernelRuntimeContext& context, const Tensor& input, const Tensor& weight,
    const std::optional<Tensor>& bias, const Int64ArrayRef stride, const Int64ArrayRef padding,
    const Int64ArrayRef dilation, const int64_t depth_multiplier, const int64_t input_offset,
    const int64_t output_offset, const Tensor& requantize_multipliers, const Tensor& requantize_shifts,
    const int64_t activation_min, const int64_t activation_max, const Tensor& scratch, Tensor& out);

}  // namespace native
}  // namespace cortex_m

namespace {

void conv2d(KernelRuntimeContext& context, Span<EValue*> s) {
  auto bias = s[2]->toOptional<Tensor>();
  cortex_m::native::quantized_conv2d_out(
      context, s[0]->toTensor(), s[1]->toTensor(), bias, s[3]->toIntList(), s[4]->toIntList(),
      s[5]->toIntList(), s[6]->toInt(), s[7]->toInt(), s[8]->toTensor(), s[9]->toTensor(), s[10]->toInt(),
      s[11]->toInt(), s[12]->toTensor(), s[13]->toTensor());
}

void depthwise_conv2d(KernelRuntimeContext& context, Span<EValue*> s) {
  auto bias = s[2]->toOptional<Tensor>();
  cortex_m::native::quantized_depthwise_conv2d_out(
      context, s[0]->toTensor(), s[1]->toTensor(), bias, s[3]->toIntList(), s[4]->toIntList(),
      s[5]->toIntList(), s[6]->toInt(), s[7]->toInt(), s[8]->toInt(), s[9]->toTensor(), s[10]->toTensor(),
      s[11]->toInt(), s[12]->toInt(), s[13]->toTensor(), s[14]->toTensor());
}

const Kernel kKernels[] = {
    Kernel("cortex_m::quantized_conv2d.out", conv2d),
    Kernel("cortex_m::quantized_depthwise_conv2d.out", depthwise_conv2d),
};

const auto kRegistered = executorch::runtime::register_kernels({kKernels, 2});

}  // namespace