Elffuss BraidHub

kikocis/kleidiai public

main · 3.9 KiBRawPermalink
1//
2// SPDX-FileCopyrightText: Copyright 2025-2026 Arm Limited and/or its affiliates <open-source-office@arm.com>
3//
4// SPDX-License-Identifier: Apache-2.0
5//
6
7#include "test/reference/dwconv.hpp"
8
9#include <cmath>
10#include <ostream>
11
12#include "test/common/float16.hpp"
13
14namespace kai::test {
15
16std::ostream& operator<<(std::ostream& os, const Padding2D& pad) {
17 os << " [ " << pad.left << " , " << pad.right << " ," << pad.top << " , " << pad.bottom << " ] ";
18 return os;
19}
20
21void PrintTo(const Padding2D& pad, std::ostream* os) {
22 *os << "PAD_" << pad.left << "_" << pad.right << "_" << pad.bottom << "_" << pad.top;
23};
24
25template <typename T>
26Buffer depthwise_reference(
27 const size_t batches, const size_t in_height, const size_t in_width, const size_t channels,
28 const size_t filter_height, const size_t filter_width, const void* feature_map, const void* weights,
29 const void* bias, const Padding2D& pad) {
30 // Calculate output dims according to padding and input params.
31 const size_t out_height = (in_height + pad.top + pad.bottom + 1 - filter_height);
32 const size_t out_width = in_width + pad.left + pad.right + 1 - filter_width;
33 const size_t out_size = out_height * out_width * batches * channels;
34
35 Buffer dst(out_size * size_in_bits<T> / 8);
36
37 for (size_t b = 0; b < batches; ++b) {
38 for (size_t out_h = 0; out_h < out_height; ++out_h) {
39 for (size_t out_w = 0; out_w < out_width; ++out_w) {
40 const size_t out_base = ((b * out_height + out_h) * out_width + out_w) * channels;
41
42 // Apply filter to feature map.
43 for (size_t ic = 0; ic < channels; ++ic) {
44 float sum = static_cast<float>(read_array<T>(bias, ic));
45
46 for (size_t kernel_w = 0; kernel_w < filter_width; ++kernel_w) {
47 // Determine if in input width bounds, if not this is padding.
48 const int in_x = static_cast<int>(out_w + kernel_w) - static_cast<int>(pad.left);
49 if (in_x < 0 || in_width <= static_cast<size_t>(in_x)) continue;
50
51 for (size_t kernel_h = 0; kernel_h < filter_height; ++kernel_h) {
52 // Determine if input height bounds. If not, then this is padding.
53 const int in_y = static_cast<int>(out_h + kernel_h) - static_cast<int>(pad.top);
54 if (in_y < 0 || in_height <= static_cast<size_t>(in_y)) continue;
55
56 auto in_idx = ((b * in_height + in_y) * in_width + in_x) * channels + ic;
57 auto weights_idx = ((kernel_h * filter_width) + kernel_w) * channels + ic;
58
59 const float wei_value = static_cast<float>(read_array<T>(weights, weights_idx));
60 const float in_value = static_cast<float>(read_array<T>(feature_map, in_idx));
61
62 // Perform actual accumulation and store in output vector
63 sum = std::fma(in_value, wei_value, sum);
64 }
65 }
66
67 auto out_idx = out_base + ic;
68 write_array<T>(dst.data(), out_idx, static_cast<T>(sum));
69 }
70 }
71 }
72 }
73 return dst;
74}
75
76// Explicit template
77template Buffer depthwise_reference<float>(
78 const size_t batches, const size_t in_height, const size_t in_width, const size_t channels,
79 const size_t filter_height, const size_t filter_width, const void* feature_map, const void* weights,
80 const void* bias, const Padding2D& pad);
81
82// Explicit template
83template Buffer depthwise_reference<Float16>(
84 const size_t batches, const size_t in_height, const size_t in_width, const size_t channels,
85 const size_t filter_height, const size_t filter_width, const void* feature_map, const void* weights,
86 const void* bias, const Padding2D& pad);
87
88} // namespace kai::test