Debian Codegraph @

packages/kokkos-kernels/batched/dense/unit_test/Test_Batched_SerialGemm.hpp

raw
1 //@HEADER
2 // ************************************************************************
3 //
4 // Kokkos v. 4.0
5 // Copyright (2022) National Technology & Engineering
6 // Solutions of Sandia, LLC (NTESS).
7 //
8 // Under the terms of Contract DE-NA0003525 with NTESS,
9 // the U.S. Government retains certain rights in this software.
10 //
11 // Part of Kokkos, under the Apache License v2.0 with LLVM Exceptions.
12 // See https://kokkos.org/LICENSE for license information.
13 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
14 //
15 //@HEADER
16 /// \author Kyungjoo Kim ([email protected])
17
18 #include "gtest/gtest.h"
19 #include "Kokkos_Core.hpp"
20 #include "Kokkos_Random.hpp"
21
22 // #include "KokkosBatched_Vector.hpp"
23
24 #include "KokkosBatched_Gemm_Decl.hpp"
25 #include "KokkosBatched_Gemm_Serial_Impl.hpp"
26
27 #include "KokkosKernels_TestUtils.hpp"
28 #include "KokkosKernels_TestVanilla.hpp"
29
30 using namespace KokkosBatched;
31
32 namespace Test {
33 namespace Gemm {
34
35 template <typename TA, typename TB>
36 struct ParamTag {
37 typedef TA transA;
38 typedef TB transB;
39 };
40
41 template <typename DeviceType, typename ViewType, typename ScalarType, typename ParamTagType, typename AlgoTagType>
42 struct Functor_TestBatchedSerialGemm {
43 using execution_space = typename DeviceType::execution_space;
44 ViewType _a, _b, _c;
45
46 ScalarType _alpha, _beta;
47
48 KOKKOS_INLINE_FUNCTION
49 Functor_TestBatchedSerialGemm(const ScalarType alpha, const ViewType &a, const ViewType &b, const ScalarType beta,
50 const ViewType &c)
51 : _a(a), _b(b), _c(c), _alpha(alpha), _beta(beta) {}
52
53 KOKKOS_INLINE_FUNCTION
54 void operator()(const ParamTagType &, const int k) const {
55 auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL());
56 auto bb = Kokkos::subview(_b, k, Kokkos::ALL(), Kokkos::ALL());
57 auto cc = Kokkos::subview(_c, k, Kokkos::ALL(), Kokkos::ALL());
58
59 SerialGemm<typename ParamTagType::transA, typename ParamTagType::transB, AlgoTagType>::invoke(_alpha, aa, bb, _beta,
60 cc);
61 }
62
63 inline void run() {
64 typedef typename ViewType::value_type value_type;
65 std::string name_region("KokkosBatched::Test::SerialGemm");
66 const std::string name_value_type = Test::value_type_name<value_type>();
67 std::string name = name_region + name_value_type;
68 Kokkos::Profiling::pushRegion(name.c_str());
69 Kokkos::RangePolicy<execution_space, ParamTagType> policy(0, _c.extent(0));
70 Kokkos::parallel_for(name.c_str(), policy, *this);
71 Kokkos::Profiling::popRegion();
72 }
73 };
74
75 template <typename DeviceType, typename ViewType, typename ScalarType, typename ParamTagType, typename AlgoTagType>
76 void impl_test_batched_gemm(const int N, const int matAdim1, const int matAdim2, const int matBdim1, const int matBdim2,
77 const int matCdim1, const int matCdim2) {
78 using execution_space = typename DeviceType::execution_space;
79 using transA = typename ParamTagType::transA;
80 using transB = typename ParamTagType::transB;
81 using value_type = typename ViewType::value_type;
82 using ats = Kokkos::ArithTraits<value_type>;
83
84 /// randomized input testing views
85 ScalarType alpha = ScalarType(1.5);
86 ScalarType beta = ScalarType(3.0);
87
88 ViewType a_expected("a_expected", N, matAdim1, matAdim2), a_actual("a_actual", N, matAdim1, matAdim2),
89 b_expected("b_expected", N, matBdim1, matBdim2), b_actual("b_actual", N, matBdim1, matBdim2),
90 c_expected("c_expected", N, matCdim1, matCdim2), c_actual("c_actual", N, matCdim1, matCdim2);
91
92 Kokkos::Random_XorShift64_Pool<execution_space> random(13718);
93
94 Kokkos::fill_random(a_expected, random, value_type(1.0));
95 Kokkos::fill_random(b_expected, random, value_type(1.0));
96 Kokkos::fill_random(c_expected, random, value_type(1.0));
97
98 Kokkos::fence();
99
100 Kokkos::deep_copy(a_actual, a_expected);
101 Kokkos::deep_copy(b_actual, b_expected);
102 Kokkos::deep_copy(c_actual, c_expected);
103
104 Functor_BatchedVanillaGEMM<ViewType, ViewType, ViewType, execution_space> vgemm;
105 vgemm.A_t = std::is_same<transA, Trans::Transpose>::value;
106 vgemm.B_t = std::is_same<transB, Trans::Transpose>::value;
107 vgemm.A_c = vgemm.B_c = false;
108 vgemm.A = a_expected;
109 vgemm.B = b_expected;
110 vgemm.C = c_expected;
111 vgemm.alpha = alpha;
112 vgemm.beta = beta;
113 vgemm.run(); // Compute c_expected
114 Functor_TestBatchedSerialGemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(alpha, a_actual, b_actual,
115 beta, c_actual)
116 .run();
117
118 typename ViewType::HostMirror c_expected_host = Kokkos::create_mirror_view(c_expected);
119 typename ViewType::HostMirror c_actual_host = Kokkos::create_mirror_view(c_actual);
120
121 // Copy to host for comparison
122 Kokkos::deep_copy(c_expected_host, c_expected);
123 Kokkos::deep_copy(c_actual_host, c_actual);
124
125 Kokkos::fence();
126
127 // check c_expected = c_actual
128 // std::conditional<, float,
129 using mag_type = typename ats::mag_type;
130 mag_type sum(1), diff(0);
131
132 mag_type eps = ats::epsilon();
133
134 eps *= std::is_same<value_type, Kokkos::Experimental::half_t>::value ||
135 std::is_same<value_type, Kokkos::Experimental::bhalf_t>::value
136 ? 4
137 : 1e3;
138
139 for (int k = 0; k < N; ++k)
140 for (int i = 0; i < matCdim1; ++i)
141 for (int j = 0; j < matCdim2; ++j) {
142 sum += ats::abs(c_expected_host(k, i, j));
143 diff += ats::abs(c_expected_host(k, i, j) - c_actual_host(k, i, j));
144 }
145 EXPECT_NEAR_KK(diff / sum, 0, eps);
146 }
147 } // namespace Gemm
148 } // namespace Test
149
150 template <typename DeviceType, typename ValueType, typename ScalarType, typename ParamTagType, typename AlgoTagType>
151 int test_batched_gemm() {
152 #if defined(KOKKOSKERNELS_INST_LAYOUTLEFT)
153 {
154 typedef Kokkos::View<ValueType ***, Kokkos::LayoutLeft, DeviceType> ViewType;
155 Test::Gemm::impl_test_batched_gemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(0, 10, 10, 10, 10,
156 10, 10);
157 for (int i = 0; i < 10; ++i) {
158 // printf("Testing: LayoutLeft, Blksize %d\n", i);
159 Test::Gemm::impl_test_batched_gemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(1024, i, i, i, i,
160 i, i);
161 }
162 for (int i = 0; i < 10; ++i) {
163 // printf("Testing: LayoutLeft, Blksize %d\n", i);
164 int dimM = i;
165 int dimN = 2 * i;
166 int dimK = 3 * i;
167 if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::NoTranspose>::value) &&
168 (std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::NoTranspose>::value)) {
169 Test::Gemm::impl_test_batched_gemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(
170 1024, dimM, dimK, dimK, dimN, dimM, dimN);
171 }
172 if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::NoTranspose>::value) &&
173 (std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::Transpose>::value)) {
174 Test::Gemm::impl_test_batched_gemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(
175 1024, dimM, dimK, dimN, dimK, dimM, dimN);
176 }
177 if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::Transpose>::value) &&
178 (std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::NoTranspose>::value)) {
179 Test::Gemm::impl_test_batched_gemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(
180 1024, dimK, dimM, dimK, dimN, dimM, dimN);
181 }
182 if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::Transpose>::value) &&
183 (std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::Transpose>::value)) {
184 Test::Gemm::impl_test_batched_gemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(
185 1024, dimK, dimM, dimN, dimK, dimM, dimN);
186 }
187 }
188 }
189 #endif
190 #if defined(KOKKOSKERNELS_INST_LAYOUTRIGHT)
191 {
192 typedef Kokkos::View<ValueType ***, Kokkos::LayoutRight, DeviceType> ViewType;
193 Test::Gemm::impl_test_batched_gemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(0, 10, 10, 10, 10,
194 10, 10);
195 for (int i = 0; i < 10; ++i) {
196 // printf("Testing: LayoutRight, Blksize %d\n", i);
197 Test::Gemm::impl_test_batched_gemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(1024, i, i, i, i,
198 i, i);
199 }
200 for (int i = 0; i < 10; ++i) {
201 // printf("Testing: LayoutLeft, Blksize %d\n", i);
202 int dimM = i;
203 int dimN = 2 * i;
204 int dimK = 3 * i;
205 if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::NoTranspose>::value) &&
206 (std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::NoTranspose>::value)) {
207 Test::Gemm::impl_test_batched_gemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(
208 1024, dimM, dimK, dimK, dimN, dimM, dimN);
209 }
210 if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::NoTranspose>::value) &&
211 (std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::Transpose>::value)) {
212 Test::Gemm::impl_test_batched_gemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(
213 1024, dimM, dimK, dimN, dimK, dimM, dimN);
214 }
215 if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::Transpose>::value) &&
216 (std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::NoTranspose>::value)) {
217 Test::Gemm::impl_test_batched_gemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(
218 1024, dimK, dimM, dimK, dimN, dimM, dimN);
219 }
220 if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::Transpose>::value) &&
221 (std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::Transpose>::value)) {
222 Test::Gemm::impl_test_batched_gemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(
223 1024, dimK, dimM, dimN, dimK, dimM, dimN);
224 }
225 }
226 }
227 #endif
228
229 return 0;
230 }