test/reference/dwconv.cpp
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // | ||
| 2 | // SPDX-FileCopyrightText: Copyright 2025 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 <ostream> | ||
| 10 | |||
| 11 | namespace kai::test { | ||
| 12 | |||
| 13 | ✗ | std::ostream& operator<<(std::ostream& os, const Padding2D& pad) { | |
| 14 | ✗ | os << " [ " << pad.left << " , " << pad.right << " ," << pad.top << " , " << pad.bottom << " ] "; | |
| 15 | ✗ | return os; | |
| 16 | } | ||
| 17 | |||
| 18 | 360 | void PrintTo(const Padding2D& pad, std::ostream* os) { | |
| 19 | 360 | *os << "PAD_" << pad.left << "_" << pad.right << "_" << pad.bottom << "_" << pad.top; | |
| 20 | 360 | }; | |
| 21 | |||
| 22 | template <typename T> | ||
| 23 | 60 | Buffer depthwise_reference( | |
| 24 | const size_t batches, const size_t in_height, const size_t in_width, const size_t channels, | ||
| 25 | const size_t filter_height, const size_t filter_width, const void* feature_map, const void* weights, | ||
| 26 | const void* bias, const Padding2D& pad) { | ||
| 27 | // Calculate output dims according to padding and input params. | ||
| 28 | 60 | const size_t out_height = (in_height + pad.top + pad.bottom + 1 - filter_height); | |
| 29 | 60 | const size_t out_width = in_width + pad.left + pad.right + 1 - filter_width; | |
| 30 | 60 | const size_t out_size = out_height * out_width * batches * channels; | |
| 31 | |||
| 32 | // NOTE: We accumulate in datatype provided - this may need to change in the future. | ||
| 33 |
0/2✗ Branch 0 not taken.
✗ Branch 1 not taken.
|
60 | std::vector<T> acc(out_size, 0.0f); |
| 34 |
1/2✓ Branch 0 taken 60 times.
✗ Branch 1 not taken.
|
60 | Buffer dst(out_size * size_in_bits<T> / 8); |
| 35 | |||
| 36 |
2/2✓ Branch 0 taken 60 times.
✓ Branch 1 taken 60 times.
|
120 | for (size_t b = 0; b < batches; ++b) { |
| 37 |
2/2✓ Branch 0 taken 4083 times.
✓ Branch 1 taken 60 times.
|
4143 | for (size_t out_h = 0; out_h < out_height; ++out_h) { |
| 38 |
2/2✓ Branch 0 taken 4083 times.
✓ Branch 1 taken 273903 times.
|
277986 | for (size_t out_w = 0; out_w < out_width; ++out_w) { |
| 39 | 273903 | const size_t out_base = ((b * out_height + out_h) * out_width + out_w) * channels; | |
| 40 | |||
| 41 | // Apply filter to feature map. | ||
| 42 |
2/2✓ Branch 0 taken 28506438 times.
✓ Branch 1 taken 273903 times.
|
28780341 | for (size_t ic = 0; ic < channels; ++ic) { |
| 43 | 28506438 | float sum = 0.0f; | |
| 44 | |||
| 45 |
2/2✓ Branch 0 taken 85519314 times.
✓ Branch 1 taken 28506438 times.
|
114025752 | for (size_t kernel_h = 0; kernel_h < filter_height; ++kernel_h) { |
| 46 | // Determine if input height bounds. If not, then this is padding. | ||
| 47 | 85519314 | const int in_y = static_cast<int>(out_h + kernel_h) - static_cast<int>(pad.top); | |
| 48 |
4/4✓ Branch 0 taken 84287142 times.
✓ Branch 1 taken 1232172 times.
✓ Branch 2 taken 502728 times.
✓ Branch 3 taken 83784414 times.
|
85519314 | if (in_y < 0 || in_height <= static_cast<size_t>(in_y)) continue; |
| 49 | |||
| 50 |
2/2✓ Branch 0 taken 83784414 times.
✓ Branch 1 taken 251353242 times.
|
335137656 | for (size_t kernel_w = 0; kernel_w < filter_width; ++kernel_w) { |
| 51 | // Determine if in input width bounds, if not this is padding. | ||
| 52 | 251353242 | const int in_x = static_cast<int>(out_w + kernel_w) - static_cast<int>(pad.left); | |
| 53 |
4/4✓ Branch 0 taken 248445780 times.
✓ Branch 1 taken 2907462 times.
✓ Branch 2 taken 7156080 times.
✓ Branch 3 taken 241289700 times.
|
251353242 | if (in_x < 0 || in_width <= static_cast<size_t>(in_x)) continue; |
| 54 | |||
| 55 | 241289700 | auto in_idx = ((b * in_height + in_y) * in_width + in_x) * channels + ic; | |
| 56 | 241289700 | auto weights_idx = ((kernel_h * filter_width) + kernel_w) * channels + ic; | |
| 57 | |||
| 58 |
1/2✓ Branch 0 taken 241289700 times.
✗ Branch 1 not taken.
|
241289700 | auto wei_value = read_array<T>(weights, weights_idx); |
| 59 |
1/2✓ Branch 0 taken 241289700 times.
✗ Branch 1 not taken.
|
241289700 | auto in_value = read_array<T>(feature_map, in_idx); |
| 60 | |||
| 61 | // Perform actual accumulation and store in output vector | ||
| 62 | 241289700 | sum += in_value * wei_value; | |
| 63 | 251353242 | } | |
| 64 | 85519314 | } | |
| 65 | |||
| 66 | 28506438 | auto out_idx = out_base + ic; | |
| 67 |
1/2✓ Branch 0 taken 28506438 times.
✗ Branch 1 not taken.
|
28506438 | sum = sum + (T)read_array<T>(bias, ic); |
| 68 |
2/4✓ Branch 0 taken 28506438 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 28506438 times.
✗ Branch 3 not taken.
|
28506438 | write_array<T>(dst.data(), out_idx, sum); |
| 69 | 28506438 | } | |
| 70 | 273903 | } | |
| 71 | 4083 | } | |
| 72 | 60 | } | |
| 73 | 60 | return dst; | |
| 74 | 60 | } | |
| 75 | |||
| 76 | // Explicit template | ||
| 77 | template 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 | } // namespace kai::test | ||
| 83 |