Debian Codegraph @

src/lib/core/highway/hwy/contrib/dot/dot_test.cc

raw symbols SCIP
1 // Copyright 2021 Google LLC
2 // SPDX-License-Identifier: Apache-2.0
3 //
4 // Licensed under the Apache License, Version 2.0 (the "License");
5 // you may not use this file except in compliance with the License.
6 // You may obtain a copy of the License at
7 //
8 // http://www.apache.org/licenses/LICENSE-2.0
9 //
10 // Unless required by applicable law or agreed to in writing, software
11 // distributed under the License is distributed on an "AS IS" BASIS,
12 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 // See the License for the specific language governing permissions and
14 // limitations under the License.
15
16 #include <stdint.h>
17 #include <stdio.h>
18 #include <stdlib.h>
19
20 #include "hwy/aligned_allocator.h"
21
22 // clang-format off
23 #undef HWY_TARGET_INCLUDE
24 #define HWY_TARGET_INCLUDE "hwy/contrib/dot/dot_test.cc"
25 #include "hwy/foreach_target.h" // IWYU pragma: keep
26
27 #include "hwy/contrib/dot/dot-inl.h"
28 #include "hwy/tests/test_util-inl.h"
29 // clang-format on
30
31 HWY_BEFORE_NAMESPACE();
32 namespace hwy {
33 namespace HWY_NAMESPACE {
34
35 template <typename T>
36 HWY_NOINLINE T SimpleDot(const T* pa, const T* pb, size_t num) {
37 double sum = 0.0;
38 for (size_t i = 0; i < num; ++i) {
39 sum += pa[i] * pb[i];
40 }
41 return static_cast<T>(sum);
42 }
43
44 HWY_NOINLINE float SimpleDot(const bfloat16_t* pa, const bfloat16_t* pb,
45 size_t num) {
46 float sum = 0.0f;
47 for (size_t i = 0; i < num; ++i) {
48 sum += F32FromBF16(pa[i]) * F32FromBF16(pb[i]);
49 }
50 return sum;
51 }
52
53 template <typename T>
54 void SetValue(const float value, T* HWY_RESTRICT ptr) {
55 *ptr = static_cast<T>(value);
56 }
57 void SetValue(const float value, bfloat16_t* HWY_RESTRICT ptr) {
58 *ptr = BF16FromF32(value);
59 }
60
61 class TestDot {
62 // Computes/verifies one dot product.
63 template <int kAssumptions, class D>
64 void Test(D d, size_t num, size_t misalign_a, size_t misalign_b,
65 RandomState& rng) {
66 using T = TFromD<D>;
67 const size_t N = Lanes(d);
68 const auto random_t = [&rng]() {
69 const int32_t bits = static_cast<int32_t>(Random32(&rng)) & 1023;
70 return static_cast<float>(bits - 512) * (1.0f / 64);
71 };
72
73 const size_t padded =
74 (kAssumptions & Dot::kPaddedToVector) ? RoundUpTo(num, N) : num;
75 AlignedFreeUniquePtr<T[]> pa = AllocateAligned<T>(misalign_a + padded);
76 AlignedFreeUniquePtr<T[]> pb = AllocateAligned<T>(misalign_b + padded);
77 T* a = pa.get() + misalign_a;
78 T* b = pb.get() + misalign_b;
79 size_t i = 0;
80 for (; i < num; ++i) {
81 SetValue(random_t(), a + i);
82 SetValue(random_t(), b + i);
83 }
84 // Fill padding with NaN - the values are not used, but avoids MSAN errors.
85 for (; i < padded; ++i) {
86 ScalableTag<float> df1;
87 SetValue(GetLane(NaN(df1)), a + i);
88 SetValue(GetLane(NaN(df1)), b + i);
89 }
90
91 const auto expected = SimpleDot(a, b, num);
92 const auto actual = Dot::Compute<kAssumptions>(d, a, b, num);
93 const auto max = static_cast<decltype(actual)>(8 * 8 * num);
94 HWY_ASSERT(-max <= actual && actual <= max);
95 HWY_ASSERT(expected - 1E-4 <= actual && actual <= expected + 1E-4);
96 }
97
98 // Runs tests with various alignments.
99 template <int kAssumptions, class D>
100 void ForeachMisalign(D d, size_t num, RandomState& rng) {
101 const size_t N = Lanes(d);
102 const size_t misalignments[3] = {0, N / 4, 3 * N / 5};
103 for (size_t ma : misalignments) {
104 for (size_t mb : misalignments) {
105 Test<kAssumptions>(d, num, ma, mb, rng);
106 }
107 }
108 }
109
110 // Runs tests with various lengths compatible with the given assumptions.
111 template <int kAssumptions, class D>
112 void ForeachCount(D d, RandomState& rng) {
113 const size_t N = Lanes(d);
114 const size_t counts[] = {1,
115 3,
116 7,
117 16,
118 HWY_MAX(N / 2, 1),
119 HWY_MAX(2 * N / 3, 1),
120 N,
121 N + 1,
122 4 * N / 3,
123 3 * N,
124 8 * N,
125 8 * N + 2};
126 for (size_t num : counts) {
127 if ((kAssumptions & Dot::kAtLeastOneVector) && num < N) continue;
128 if ((kAssumptions & Dot::kMultipleOfVector) && (num % N) != 0) continue;
129 ForeachMisalign<kAssumptions>(d, num, rng);
130 }
131 }
132
133 public:
134 template <class T, class D>
135 HWY_NOINLINE void operator()(T /*unused*/, D d) {
136 RandomState rng;
137
138 // All 8 combinations of the three length-related flags:
139 ForeachCount<0>(d, rng);
140 ForeachCount<Dot::kAtLeastOneVector>(d, rng);
141 ForeachCount<Dot::kMultipleOfVector>(d, rng);
142 ForeachCount<Dot::kMultipleOfVector | Dot::kAtLeastOneVector>(d, rng);
143 ForeachCount<Dot::kPaddedToVector>(d, rng);
144 ForeachCount<Dot::kPaddedToVector | Dot::kAtLeastOneVector>(d, rng);
145 ForeachCount<Dot::kPaddedToVector | Dot::kMultipleOfVector>(d, rng);
146 ForeachCount<Dot::kPaddedToVector | Dot::kMultipleOfVector |
147 Dot::kAtLeastOneVector>(d, rng);
148 }
149 };
150
151 void TestAllDot() { ForFloatTypes(ForPartialVectors<TestDot>()); }
152 void TestAllDotBF16() { ForShrinkableVectors<TestDot>()(bfloat16_t()); }
153
154 // NOLINTNEXTLINE(google-readability-namespace-comments)
155 } // namespace HWY_NAMESPACE
156 } // namespace hwy
157 HWY_AFTER_NAMESPACE();
158
159 #if HWY_ONCE
160
161 namespace hwy {
162 HWY_BEFORE_TEST(DotTest);
163 HWY_EXPORT_AND_TEST_P(DotTest, TestAllDot);
164 HWY_EXPORT_AND_TEST_P(DotTest, TestAllDotBF16);
165 } // namespace hwy
166
167 #endif