Elffuss BraidHub

kikocis/kleidiai public

main · 27.0 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 <gtest/gtest.h>
10
11#include <array>
12#include <cstddef>
13#include <cstring>
14#include <iostream>
15#include <optional>
16#include <string_view>
17#include <tuple>
18#include <unordered_map>
19#include <variant>
20
21#include "kai/ukernels/dwconv/dwconv_f16_f16_f16p/kai_dwconv_clamp_f16_f16_f16p1vlx1b_3x3_s1_4x4_sme2_mla.h"
22#include "kai/ukernels/dwconv/dwconv_f16_f16_f16p/kai_dwconv_clamp_f16_f16_f16p_interface.h"
23#include "kai/ukernels/dwconv/dwconv_f32_f32_f32p/kai_dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla.h"
24#include "kai/ukernels/dwconv/dwconv_f32_f32_f32p/kai_dwconv_clamp_f32_f32_f32p_interface.h"
25#include "kai/ukernels/dwconv/pack/kai_rhs_dwconv_pack_x16p1vlx1b_x16_x16_sme.h"
26#include "kai/ukernels/dwconv/pack/kai_rhs_dwconv_pack_x32p1vlx1b_x32_x32_sme.h"
27#include "test/common/abi_checker.hpp"
28#include "test/common/buffer.hpp"
29#include "test/common/compare.hpp"
30#include "test/common/cpu_info.hpp"
31#include "test/common/float16.hpp"
32#include "test/common/matmul_test_common.hpp"
33#include "test/common/matrix_portion.hpp"
34#include "test/common/seed.hpp"
35#include "test/reference/clamp.hpp"
36#include "test/reference/fill.hpp"
37
38namespace kai::test {
39
40namespace {
41
42// Interface for depthfirst kernel.
43struct DepthwiseDepthfirstKernel {
44 std::function<size_t(size_t m, size_t n, size_t k)> get_dst_size;
45 std::function<size_t(void)> get_filter_height;
46 std::function<size_t(void)> get_filter_width;
47 std::function<void(
48 const void* src, const void* rhs_packed, void* dst, size_t n_channels, size_t src_rows, size_t src_cols,
49 size_t dst_rows, size_t dst_cols, size_t pad_left, size_t pad_top, size_t in_stride_row, size_t in_stride_col,
50 size_t out_stride_row, size_t out_stride_col, float clamp_min, float clamp_max)>
51 conv;
52};
53
54/// Interface for depthwise kernel.
55struct DepthwisePlanarKernel {
56 std::function<size_t(size_t m, size_t n, size_t k)> get_dst_size;
57 std::function<size_t(size_t m, size_t n)> get_dst_offset;
58 std::function<size_t(void)> get_m_step;
59 std::function<void(
60 const void* inptr, const void* packed_rhs, void* outptr_start, size_t stride_in_row, size_t stride_in_col,
61 size_t dst_stride_row, size_t dst_stride_col, unsigned int valid_input_rows, unsigned int valid_out_rows,
62 unsigned int pad_left, unsigned int pad_top, float pad_value, float clamp_min, float clamp_max)>
63 conv;
64};
65
66// Rhs packing micro-kernel.
67struct RhsPackDepthwiseKernel {
68 std::function<size_t(size_t fh, size_t fw, size_t nc)> get_rhs_packed_size;
69 std::function<void(
70 size_t filter_height, size_t filter_width, size_t height, size_t width, size_t num_channels, const void* rhs,
71 const void* bias, void* rhs_packed)>
72 pack;
73};
74
75using DepthwiseKernel = std::variant<DepthwisePlanarKernel, DepthwiseDepthfirstKernel>;
76
77/// Description of a Depthwise kernel set
78struct Depthwise {
79 std::string_view name;
80 std::function<bool(void)> is_supported;
81 std::pair<size_t, size_t> filter;
82 DataType data_type;
83 DataType acc_type;
84 RhsPackDepthwiseKernel rhs;
85 DepthwiseKernel depthwise;
86};
87
88/// Convenience types for testing.
89using DepthwiseArray = std::array<Depthwise, 1>;
90using DepthwiseTestParams = std::tuple<Depthwise, MatMulShape, Padding2D, std::optional<float>>;
91using DepthwiseF32PlanarKernelTest = testing::TestWithParam<DepthwiseTestParams>;
92using DepthwiseF16DepthfirstKernelTest = testing::TestWithParam<DepthwiseTestParams>;
93
94struct DepthwiseF16DepthfirstPartialParams {
95 MatMulShape in_shape;
96 Padding2D padding;
97 size_t out_row;
98 size_t out_col;
99 size_t tile_rows;
100 size_t tile_cols;
101 float clamp_keep_ratio;
102};
103
104using DepthwiseF16DepthfirstPartialKernelTest = testing::TestWithParam<DepthwiseF16DepthfirstPartialParams>;
105
106/// Use interface for depthwise kernel
107const kai_dwconv_clamp_f32_f32_f32p_planar_ukernel& get_dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla() {
108 static kai_dwconv_clamp_f32_f32_f32p_planar_ukernel ukernel;
109 ukernel.get_m_step = kai_get_m_step_dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla;
110 ukernel.get_dst_offset = kai_get_dst_offset_dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla;
111 ukernel.get_dst_size = kai_get_dst_size_dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla;
112 ukernel.run_dwconv = kai_run_dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla;
113 return ukernel;
114}
115
116const kai_dwconv_clamp_f16_f16_f16p_depthfirst_ukernel& get_dwconv_clamp_f16_f16_f16p1vlx1b_3x3_s1_4x4_sme2_mla() {
117 static kai_dwconv_clamp_f16_f16_f16p_depthfirst_ukernel ukernel;
118 ukernel.get_filter_height = kai_get_filter_height_dwconv_clamp_f16_f16_f16p1vlx1b_3x3_s1_4x4_sme2_mla;
119 ukernel.get_filter_width = kai_get_filter_width_dwconv_clamp_f16_f16_f16p1vlx1b_3x3_s1_4x4_sme2_mla;
120 ukernel.get_dst_size = kai_get_dst_size_dwconv_clamp_f16_f16_f16p1vlx1b_3x3_s1_4x4_sme2_mla;
121 ukernel.run_dwconv = kai_run_dwconv_clamp_f16_f16_f16p1vlx1b_3x3_s1_4x4_sme2_mla;
122 return ukernel;
123}
124
125const DepthwiseArray& get_depthwise_f32_planar_methods() {
126 static DepthwiseArray depthwise_methods{};
127 Depthwise& method = depthwise_methods[0];
128
129 method.name = "dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla";
130 method.rhs.get_rhs_packed_size = kai_rhs_get_dst_size_dwconv_pack_x32p1vlx1b_x32_x32_sme;
131 method.rhs.pack = kai_run_rhs_dwconv_pack_x32p1vlx1b_x32_x32_sme;
132 method.is_supported = cpu_has_sme2;
133 method.filter = {3, 3};
134
135 const kai_dwconv_clamp_f32_f32_f32p_planar_ukernel& ukernel_f32 =
136 get_dwconv_clamp_f32_f32_f32p1vlx1b_3x3_s1_4xc_sme2_mla();
137 method.data_type = DataType::FP32;
138 method.acc_type = DataType::FP32;
139 method.depthwise = DepthwisePlanarKernel{
140 .get_dst_size = ukernel_f32.get_dst_size,
141 .get_dst_offset = ukernel_f32.get_dst_offset,
142 .get_m_step = ukernel_f32.get_m_step,
143 .conv = ukernel_f32.run_dwconv};
144
145 return depthwise_methods;
146}
147
148const DepthwiseArray& get_depthwise_f16_depthfirst_methods() {
149 static DepthwiseArray depthwise_methods{};
150 Depthwise& method = depthwise_methods[0];
151
152 method.name = "dwconv_clamp_f16_f16_f16p1vlx1b_3x3_s1_4x4_sme2_mla";
153 method.rhs.get_rhs_packed_size = kai_rhs_get_dst_size_dwconv_pack_x16p1vlx1b_x16_x16_sme;
154 method.rhs.pack = kai_run_rhs_dwconv_pack_x16p1vlx1b_x16_x16_sme;
155 method.is_supported = cpu_has_sme2;
156
157 const kai_dwconv_clamp_f16_f16_f16p_depthfirst_ukernel& ukernel_f16 =
158 get_dwconv_clamp_f16_f16_f16p1vlx1b_3x3_s1_4x4_sme2_mla();
159 method.filter = {ukernel_f16.get_filter_height(), ukernel_f16.get_filter_width()};
160 method.data_type = DataType::FP16;
161 method.acc_type = DataType::FP16;
162 method.depthwise = DepthwiseDepthfirstKernel{
163 .get_dst_size = ukernel_f16.get_dst_size,
164 .get_filter_height = ukernel_f16.get_filter_height,
165 .get_filter_width = ukernel_f16.get_filter_width,
166 .conv = ukernel_f16.run_dwconv};
167
168 return depthwise_methods;
169}
170
171const std::array<MatMulShape, 11>& get_depthwise_shapes() {
172 static constexpr std::array<MatMulShape, 11> shapes{{
173 // IN_HEIGHT, IN_WIDTH, IN_CHANNELS
174 MatMulShape{4, 4, 1},
175 MatMulShape{5, 5, 8},
176 MatMulShape{7, 9, 17},
177 MatMulShape{8, 4, 16},
178 MatMulShape{17, 13, 31},
179 MatMulShape{96, 33, 37},
180 MatMulShape{99, 22, 51},
181 MatMulShape{127, 127, 127},
182 MatMulShape{6, 6, 32},
183 MatMulShape{10, 10, 2},
184 MatMulShape{258, 258, 32},
185 }};
186 return shapes;
187}
188
189const std::array<Padding2D, 4>& get_depthwise_paddings() {
190 static constexpr std::array<Padding2D, 4> paddings{{
191 // pad_left, pad_right, pad_top, pad_bottom
192 Padding2D{0, 0, 0, 0},
193 Padding2D{0, 1, 0, 1},
194 Padding2D{1, 1, 1, 1},
195 Padding2D{5, 11, 7, 3},
196 }};
197 return paddings;
198}
199
200const std::array<std::optional<float>, 4>& get_depthwise_clamp_keep_ratios() {
201 static constexpr std::array<std::optional<float>, 4> clamp_keep_ratios{{std::nullopt, 1.0F, 0.9F, 0.5F}};
202 return clamp_keep_ratios;
203}
204
205/// Test reference identification.
206struct TestDataId {
207 using DT = std::underlying_type_t<DataType>;
208 MatMulShape in_shape;
209 MatMulShape rhs_shape;
210 Padding2D pad;
211 DataType dt;
212 DataType dt_acc;
213 std::optional<float> clamp_keep_ratio;
214
215 struct Hash {
216 size_t operator()(const TestDataId& test_id) const {
217 return //
218 (MatMulShape::Hash{}(test_id.in_shape) << 0) ^ //
219 (MatMulShape::Hash{}(test_id.rhs_shape) << 1) ^ //
220 (Padding2D::Hash{}(test_id.pad) << 2) ^ //
221 (std::hash<DT>{}(static_cast<DT>(test_id.dt)) << 3) ^ //
222 (std::hash<DT>{}(static_cast<DT>(test_id.dt_acc)) << 4) ^ //
223 (std::hash<float>{}(test_id.clamp_keep_ratio.value_or(0.0)) << 5); //
224 }
225 };
226
227private:
228 friend bool operator==(const TestDataId& lhs, const TestDataId& rhs) {
229 return //
230 lhs.in_shape == rhs.in_shape && //
231 lhs.rhs_shape == rhs.rhs_shape && //
232 lhs.pad == rhs.pad && //
233 lhs.dt == rhs.dt && //
234 lhs.dt_acc == rhs.dt_acc && //
235 lhs.clamp_keep_ratio == rhs.clamp_keep_ratio; //
236 }
237};
238
239/// Test reference data
240struct TestData {
241 Buffer lhs; ///< LHS input matrix
242 Buffer rhs; ///< RHS input matrix
243 Buffer bias; ///< Bias vector
244 Buffer out; ///< Reference depthwise result
245 Buffer padding; ///< Padding buffer
246 Range<float> clamp_range; ///< Clamp range
247};
248
249/// Generate reference data, caches it.
250struct ReferenceGenerator {
251 /// Retrieve reference data for the provided test identification
252 static const TestData& get_test_reference(const TestDataId test_id, const MatMulShape& out_shape) {
253 static std::unordered_map<TestDataId, TestData, TestDataId::Hash> m_data;
254 if (const auto itr = m_data.find(test_id); itr != end(m_data)) {
255 return itr->second;
256 }
257
258 return m_data[test_id] = generate_reference(test_id, out_shape);
259 }
260
261private:
262 /// Generate reference data.
263 // NOTE : This block is currently FP32/FP16 specific - it is not datatype generic
264 static TestData generate_reference(const TestDataId& test_id, const MatMulShape& out_shape) {
265 const auto& [in_shape, rhs_shape, pad, dt, acc_dt, clamp_keep_ratio] = test_id;
266
267 // Stable key derived from the cache identifier.
268 const auto key_hash = static_cast<std::uint32_t>(TestDataId::Hash{}(test_id));
269 const auto key = std::string("dwconv_cache:") + std::to_string(key_hash);
270 auto& feed = seed_stream(key);
271
272 // Generate random input data
273 Buffer lhs = fill_matrix_random(in_shape.m, in_shape.n * in_shape.k, DataFormat(dt), feed());
274 Buffer rhs = fill_matrix_random(rhs_shape.m, rhs_shape.n * rhs_shape.k, DataFormat(dt), feed());
275 Buffer bias = fill_matrix_random(1, out_shape.k, DataFormat(dt), feed());
276
277 // Call reference function
278 Buffer out = (acc_dt == DataType::FP16) ? depthwise_reference<Float16>(
279 1, in_shape.m, in_shape.n, in_shape.k, rhs_shape.m, rhs_shape.n,
280 lhs.data(), rhs.data(), bias.data(), pad)
281 : depthwise_reference<float>(
282 1, in_shape.m, in_shape.n, in_shape.k, rhs_shape.m, rhs_shape.n,
283 lhs.data(), rhs.data(), bias.data(), pad);
284
285 const auto [min, max] =
286 find_clamp_range(dt, out.data(), out_shape.m * out_shape.n * out_shape.k, clamp_keep_ratio);
287 Buffer out_clamped = clamp(dt, out.data(), out_shape.m * out_shape.n * out_shape.k, min, max);
288
289 // Populate reference data
290 TestData test_reference;
291 test_reference.lhs = std::move(lhs);
292 test_reference.rhs = std::move(rhs);
293 test_reference.bias = std::move(bias);
294 test_reference.out = std::move(out_clamped);
295 test_reference.clamp_range = {min, max};
296 return test_reference;
297 };
298};
299
300/// Perform RHS packing for depthwise
301Buffer pack_rhs(const RhsPackDepthwiseKernel& kernel, const MatMulShape& shape, const TestData& reference) {
302 // Calculate size, and allocate buffer
303 const size_t dst_size = kernel.get_rhs_packed_size(shape.m, shape.n, shape.k);
304 Buffer dst(dst_size);
305
306 // RHS Pack API is subject to change.
307 abi_check(
308 kernel.pack, shape.m, shape.n, shape.m, shape.n, shape.k, reference.rhs.data(), reference.bias.data(),
309 dst.data());
310 return dst;
311}
312
313/// Perform Depthwise Operation using planar kernel.
314Buffer dwconv(
315 const DepthwisePlanarKernel& kernel, const Rect& portion, const MatMulShape& in_shape, const MatMulShape& out_shape,
316 const Padding2D pad, const TestData& reference, const Buffer& rhs_packed, Range<float> clamp_range, DataType type) {
317 const size_t dst_size = kernel.get_dst_size(out_shape.m, out_shape.n, out_shape.k);
318 Buffer dst(dst_size);
319
320 const size_t dt_size_bytes = data_type_size_in_bits(type) / 8;
321 const size_t stride_in_row = in_shape.n * in_shape.k * dt_size_bytes;
322 const size_t dst_stride_row = out_shape.n * out_shape.k * dt_size_bytes;
323 const size_t stride_col = out_shape.k * dt_size_bytes;
324
325 // Loop the following. M-Step rows are handled at a time.
326 for (size_t out_row = portion.start_row(); out_row < portion.end_row(); out_row += kernel.get_m_step()) {
327 const int start_in_row = out_row - pad.top;
328 const size_t pad_top = (start_in_row < 0) ? (-start_in_row) : 0;
329 const size_t in_row = (start_in_row < 0) ? 0 : start_in_row;
330
331 const size_t valid_input_rows = (in_row < in_shape.m) ? (in_shape.m - in_row) : 0;
332 const size_t valid_out_rows = (out_shape.m - out_row);
333
334 abi_check(
335 kernel.conv, reference.lhs.data() + (in_row * stride_in_row), rhs_packed.data(),
336 dst.data() + (out_row * dst_stride_row), stride_in_row, stride_col, dst_stride_row, stride_col,
337 valid_input_rows, valid_out_rows, pad.left, pad_top, 0.f, clamp_range.min, clamp_range.max);
338 }
339
340 return dst;
341}
342
343/// Perform Depthwise Operation using a depth-first kernel
344Buffer dwconv(
345 const DepthwiseDepthfirstKernel& kernel, const Rect& portion, const MatMulShape& in_shape,
346 const MatMulShape& out_shape, const Padding2D pad, const TestData& reference, const Buffer& rhs_packed,
347 Range<float> clamp_range, DataType type) {
348 KAI_UNUSED(portion);
349 KAI_UNUSED(pad);
350
351 const size_t dst_size = kernel.get_dst_size(out_shape.m, out_shape.n, out_shape.k);
352 Buffer dst(dst_size);
353
354 const size_t dt_size_bytes = data_type_size_in_bits(type) / 8;
355 const size_t dst_stride_row = out_shape.n * out_shape.k * dt_size_bytes;
356 const size_t stride_in_row = in_shape.n * in_shape.k * dt_size_bytes;
357 const size_t stride_col = out_shape.k * dt_size_bytes;
358
359 // NOTE: Currently stride is taken in elements rather than bytes.
360 // Unlike planar, depth-first can handle all input data at once.
361 abi_check(
362 kernel.conv, reference.lhs.data(), rhs_packed.data(), dst.data(), in_shape.k, in_shape.m, in_shape.n,
363 out_shape.m, out_shape.n, pad.left, pad.top, stride_in_row, stride_col, dst_stride_row, stride_col,
364 clamp_range.min, clamp_range.max);
365
366 return dst;
367}
368
369Buffer dwconv_partial(
370 const DepthwiseDepthfirstKernel& kernel, const MatMulShape& in_shape, size_t out_row, size_t out_col,
371 size_t tile_rows, size_t tile_cols, const Padding2D pad, const TestData& reference, const Buffer& rhs_packed,
372 Range<float> clamp_range, DataType type) {
373 const size_t dst_size = kernel.get_dst_size(tile_rows, tile_cols, in_shape.k);
374 Buffer dst(dst_size);
375
376 const size_t dt_size_bytes = data_type_size_in_bits(type) / 8;
377 const size_t in_stride_row = in_shape.n * in_shape.k * dt_size_bytes;
378 const size_t stride_col = in_shape.k * dt_size_bytes;
379 const size_t dst_stride_row = tile_cols * in_shape.k * dt_size_bytes;
380
381 const int start_in_row = static_cast<int>(out_row) - static_cast<int>(pad.top);
382 const int start_in_col = static_cast<int>(out_col) - static_cast<int>(pad.left);
383
384 const size_t pad_top = (start_in_row < 0) ? static_cast<size_t>(-start_in_row) : 0;
385 const size_t pad_left = (start_in_col < 0) ? static_cast<size_t>(-start_in_col) : 0;
386
387 size_t in_row = (start_in_row < 0) ? 0 : static_cast<size_t>(start_in_row);
388 size_t in_col = (start_in_col < 0) ? 0 : static_cast<size_t>(start_in_col);
389
390 if (in_row > in_shape.m) {
391 in_row = in_shape.m;
392 }
393 if (in_col > in_shape.n) {
394 in_col = in_shape.n;
395 }
396
397 const size_t src_rows = in_shape.m - in_row;
398 const size_t src_cols = in_shape.n - in_col;
399 const std::byte* src_base = reference.lhs.data();
400 const void* src_tile = (src_rows == 0 || src_cols == 0)
401 ? static_cast<const void*>(src_base)
402 : static_cast<const void*>(src_base + (in_row * in_stride_row) + (in_col * stride_col));
403
404 abi_check(
405 kernel.conv, src_tile, rhs_packed.data(), dst.data(), in_shape.k, static_cast<size_t>(src_rows),
406 static_cast<size_t>(src_cols), static_cast<size_t>(tile_rows), static_cast<size_t>(tile_cols),
407 static_cast<size_t>(pad_left), static_cast<size_t>(pad_top), in_stride_row, stride_col, dst_stride_row,
408 stride_col, clamp_range.min, clamp_range.max);
409
410 return dst;
411}
412
413Buffer copy_reference_output_region(
414 const TestData& reference, const MatMulShape& out_shape, DataType type, size_t out_row, size_t out_col,
415 size_t tile_rows, size_t tile_cols) {
416 const size_t dt_size_bytes = data_type_size_in_bits(type) / 8;
417 const size_t src_stride_row = out_shape.n * out_shape.k * dt_size_bytes;
418 const size_t dst_stride_row = tile_cols * out_shape.k * dt_size_bytes;
419 const size_t src_col_offset = out_col * out_shape.k * dt_size_bytes;
420
421 Buffer dst(tile_rows * dst_stride_row);
422
423 const std::byte* src = reference.out.data();
424 std::byte* dst_ptr = dst.data();
425
426 for (size_t row = 0; row < tile_rows; ++row) {
427 std::memcpy(
428 dst_ptr + (row * dst_stride_row), src + ((out_row + row) * src_stride_row) + src_col_offset,
429 dst_stride_row);
430 }
431
432 return dst;
433}
434
435const std::array<DepthwiseF16DepthfirstPartialParams, 9>& get_depthwise_f16_depthfirst_partial_params() {
436 static constexpr std::array<DepthwiseF16DepthfirstPartialParams, 9> params{{
437 // input shape, padding, output row, output col, tile rows, tile cols, clamp keep ratio
438 {MatMulShape{4, 4, 1}, Padding2D{1, 1, 1, 1}, 0, 0, 3, 3, 1.0F},
439 {MatMulShape{4, 4, 1}, Padding2D{1, 1, 1, 1}, 3, 3, 1, 1, 0.9F},
440 {MatMulShape{10, 10, 2}, Padding2D{1, 1, 1, 1}, 0, 0, 3, 3, 1.0F},
441 {MatMulShape{10, 10, 2}, Padding2D{1, 1, 1, 1}, 4, 4, 4, 4, 0.5F},
442 {MatMulShape{10, 10, 2}, Padding2D{1, 1, 1, 1}, 8, 4, 2, 4, 0.9F},
443 {MatMulShape{10, 10, 2}, Padding2D{1, 1, 1, 1}, 4, 8, 4, 2, 0.9F},
444 {MatMulShape{10, 10, 2}, Padding2D{1, 1, 1, 1}, 8, 8, 2, 2, 0.5F},
445 {MatMulShape{6, 6, 32}, Padding2D{0, 0, 0, 0}, 3, 3, 1, 1, 1.0F},
446 {MatMulShape{96, 33, 37}, Padding2D{1, 1, 1, 1}, 92, 29, 4, 4, 0.5F},
447 }};
448 return params;
449}
450
451void test_depthwise_output(const DepthwiseTestParams& params) {
452 const auto& method = std::get<0>(params);
453 const auto& in_shape = std::get<1>(params);
454 const auto& padding = std::get<2>(params);
455 const auto clamp_keep_ratio = std::get<3>(params);
456
457 if (not method.is_supported()) {
458 GTEST_SKIP() << "Unsupported CPU feature";
459 }
460
461 // Calculate Shapes.
462 const int out_height = (in_shape.m + padding.top + padding.bottom + 1 - method.filter.first);
463 const int out_width = (in_shape.n + padding.left + padding.right + 1 - method.filter.second);
464 ASSERT_TRUE(out_height > 0 && out_width > 0);
465
466 const size_t dt_size_bytes = data_type_size_in_bits(method.data_type) / 8;
467 MatMulShape rhs_shape = {method.filter.first, method.filter.second, in_shape.k};
468 MatMulShape out_shape = {static_cast<size_t>(out_height), static_cast<size_t>(out_width), (in_shape.k)};
469
470 // 1. Calculate reference.
471 const TestData& test_data = ReferenceGenerator::get_test_reference(
472 {in_shape, rhs_shape, padding, method.data_type, method.acc_type, clamp_keep_ratio}, out_shape);
473
474 // 2. Pack RHS (Weights+Bias)
475 Buffer rhs_packed = pack_rhs(method.rhs, rhs_shape, test_data);
476 const MatrixPortion out_portion{0, 0, 1, 1};
477
478 const Rect portion = out_portion.compute_portion(
479 out_shape.m, out_shape.n * out_shape.k, out_shape.m, (rhs_packed.size() / dt_size_bytes));
480
481 // 3. Run Depthwise Kernel.
482 Buffer out = std::visit(
483 [&](auto&& k) {
484 return dwconv(
485 k, portion, in_shape, out_shape, padding, test_data, rhs_packed, test_data.clamp_range,
486 method.data_type);
487 },
488 method.depthwise);
489
490 // 4. Compare with reference result.
491 // Use appropriate tolerances depending on datatype.
492 DefaultMismatchHandler handler = (method.data_type == DataType::FP32) ? DefaultMismatchHandler(0, 0.0001, 0, 0.0001)
493 : DefaultMismatchHandler(0, 0.01, 0, 0.01);
494 const auto success = compare(
495 out.data(), test_data.out.data(), method.data_type, out_shape.m, out_shape.n * out_shape.k, portion, handler);
496 ASSERT_TRUE(success);
497}
498
499static std::string get_depthwise_f16_depthfirst_partial_test_name(
500 const testing::TestParamInfo<DepthwiseF16DepthfirstPartialParams>& info) {
501 const auto& param = info.param;
502 return "M_" + std::to_string(param.in_shape.m) + "__N_" + std::to_string(param.in_shape.n) + "__K_" +
503 std::to_string(param.in_shape.k) + "__Padding_" + std::to_string(param.padding.left) + "_" +
504 std::to_string(param.padding.right) + "_" + std::to_string(param.padding.top) + "_" +
505 std::to_string(param.padding.bottom) + "__OutRow_" + std::to_string(param.out_row) + "__OutCol_" +
506 std::to_string(param.out_col) + "__TileRows_" + std::to_string(param.tile_rows) + "__TileCols_" +
507 std::to_string(param.tile_cols) + "__ClampKeepRatio_" +
508 std::to_string(static_cast<int>(param.clamp_keep_ratio * 100));
509}
510
511} // namespace
512
513/// End-to-end test for FP32 planar depthwise kernels.
514TEST_P(DepthwiseF32PlanarKernelTest, Output) {
515 test_depthwise_output(GetParam());
516}
517
518/// End-to-end test for FP16 depth-first depthwise kernels.
519TEST_P(DepthwiseF16DepthfirstKernelTest, Output) {
520 test_depthwise_output(GetParam());
521}
522
523/// Partial-output test for FP16 depth-first depthwise kernels.
524TEST_P(DepthwiseF16DepthfirstPartialKernelTest, Output) {
525 const auto& params = GetParam();
526 const Depthwise& method = get_depthwise_f16_depthfirst_methods()[0];
527 const auto& kernel = std::get<DepthwiseDepthfirstKernel>(method.depthwise);
528
529 if (not method.is_supported()) {
530 GTEST_SKIP() << "Unsupported CPU feature";
531 }
532
533 const int out_height = (params.in_shape.m + params.padding.top + params.padding.bottom + 1 - method.filter.first);
534 const int out_width = (params.in_shape.n + params.padding.left + params.padding.right + 1 - method.filter.second);
535 ASSERT_TRUE(out_height > 0 && out_width > 0);
536
537 MatMulShape rhs_shape = {method.filter.first, method.filter.second, params.in_shape.k};
538 MatMulShape out_shape = {static_cast<size_t>(out_height), static_cast<size_t>(out_width), params.in_shape.k};
539
540 ASSERT_LE(params.out_row + params.tile_rows, out_shape.m);
541 ASSERT_LE(params.out_col + params.tile_cols, out_shape.n);
542
543 const TestData& test_data = ReferenceGenerator::get_test_reference(
544 {params.in_shape, rhs_shape, params.padding, method.data_type, method.acc_type, params.clamp_keep_ratio},
545 out_shape);
546 Buffer rhs_packed = pack_rhs(method.rhs, rhs_shape, test_data);
547
548 Buffer out = dwconv_partial(
549 kernel, params.in_shape, params.out_row, params.out_col, params.tile_rows, params.tile_cols, params.padding,
550 test_data, rhs_packed, test_data.clamp_range, method.data_type);
551 Buffer expected = copy_reference_output_region(
552 test_data, out_shape, method.data_type, params.out_row, params.out_col, params.tile_rows, params.tile_cols);
553
554 DefaultMismatchHandler handler(0, 0.01, 0, 0.01);
555 const Rect full_tile{0, 0, params.tile_rows, params.tile_cols * out_shape.k};
556 const auto success = compare(
557 out.data(), expected.data(), method.data_type, params.tile_rows, params.tile_cols * out_shape.k, full_tile,
558 handler);
559 ASSERT_TRUE(success);
560}
561
562TEST(DepthwiseF16DepthfirstKernelTest, SupportsMoreThanOneThousandChannels) {
563 const Depthwise& method = get_depthwise_f16_depthfirst_methods()[0];
564 test_depthwise_output({method, MatMulShape{5, 5, 1001}, Padding2D{1, 1, 1, 1}, 0.9F});
565}
566
567/// Name generator for test case
568[[maybe_unused]] static void PrintTo(const DepthwiseTestParams& param, std::ostream* os) {
569 const auto& [method, shape, padding, clamp_keep_ratio] = param;
570 *os << method.name << "__";
571 PrintTo(shape, os);
572 *os << "__";
573 PrintTo(padding, os);
574 *os << "__";
575 *os << "__clamp_keep_ratio_"
576 << (clamp_keep_ratio.has_value() ? std::to_string(static_cast<int>(clamp_keep_ratio.value() * 100))
577 : "noclamp");
578}
579
580/// Name generator for FP16 depth-first partial-output test case.
581[[maybe_unused]] static void PrintTo(const DepthwiseF16DepthfirstPartialParams& param, std::ostream* os) {
582 PrintTo(param.in_shape, os);
583 *os << "__";
584 PrintTo(param.padding, os);
585 *os << "__out_row_" << param.out_row;
586 *os << "__out_col_" << param.out_col;
587 *os << "__tile_rows_" << param.tile_rows;
588 *os << "__tile_cols_" << param.tile_cols;
589 *os << "__clamp_keep_ratio_" << static_cast<int>(param.clamp_keep_ratio * 100);
590}
591
592/// Test parameter listing
593INSTANTIATE_TEST_SUITE_P(
594 Fp32DepthwisePlanar, DepthwiseF32PlanarKernelTest,
595 testing::Combine(
596 testing::ValuesIn(get_depthwise_f32_planar_methods()), //
597 testing::ValuesIn(get_depthwise_shapes()), testing::ValuesIn(get_depthwise_paddings()),
598 testing::ValuesIn(get_depthwise_clamp_keep_ratios())),
599 testing::PrintToStringParamName());
600
601INSTANTIATE_TEST_SUITE_P(
602 Fp16DepthwiseDepthfirst, DepthwiseF16DepthfirstKernelTest,
603 testing::Combine(
604 testing::ValuesIn(get_depthwise_f16_depthfirst_methods()), //
605 testing::ValuesIn(get_depthwise_shapes()), testing::ValuesIn(get_depthwise_paddings()),
606 testing::ValuesIn(get_depthwise_clamp_keep_ratios())),
607 testing::PrintToStringParamName());
608
609INSTANTIATE_TEST_SUITE_P(
610 Fp16DepthwiseDepthfirst, DepthwiseF16DepthfirstPartialKernelTest,
611 testing::ValuesIn(get_depthwise_f16_depthfirst_partial_params()), get_depthwise_f16_depthfirst_partial_test_name);
612
613} // namespace kai::test