Debian Codegraph @

packages/kokkos-kernels/batched/dense/unit_test/Test_Batched_TeamGemm.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 #include "KokkosBatched_Gemm_Team_Impl.hpp"
27
28 #include "KokkosKernels_TestUtils.hpp"
29
30 using namespace KokkosBatched;
31
32 namespace Test {
33 namespace TeamGemm {
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_TestBatchedTeamGemm {
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_TestBatchedTeamGemm(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 template <typename MemberType>
54 KOKKOS_INLINE_FUNCTION void operator()(const ParamTagType &, const MemberType &member) const {
55 const int k = member.league_rank();
56
57 auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL());
58 auto bb = Kokkos::subview(_b, k, Kokkos::ALL(), Kokkos::ALL());
59 auto cc = Kokkos::subview(_c, k, Kokkos::ALL(), Kokkos::ALL());
60
61 KokkosBatched::TeamGemm<MemberType, typename ParamTagType::transA, typename ParamTagType::transB,
62 AlgoTagType>::invoke(member, _alpha, aa, bb, _beta, cc);
63 }
64
65 inline void run() {
66 typedef typename ViewType::value_type value_type;
67 std::string name_region("KokkosBatched::Test::TeamGemm");
68 const std::string name_value_type = Test::value_type_name<value_type>();
69 std::string name = name_region + name_value_type;
70 Kokkos::Profiling::pushRegion(name.c_str());
71 const int league_size = _c.extent(0);
72 Kokkos::TeamPolicy<execution_space, ParamTagType> policy(league_size, Kokkos::AUTO);
73 Kokkos::parallel_for(name.c_str(), policy, *this);
74 Kokkos::Profiling::popRegion();
75 }
76 };
77
78 template <typename DeviceType, typename ViewType, typename ScalarType, typename ParamTagType, typename AlgoTagType>
79 void impl_test_batched_teamgemm(const int N, const int matAdim1, const int matAdim2, const int matBdim1,
80 const int matBdim2, const int matCdim1, const int matCdim2) {
81 using transA = typename ParamTagType::transA;
82 using transB = typename ParamTagType::transB;
83 using execution_space = typename DeviceType::execution_space;
84 using value_type = typename ViewType::value_type;
85 using ats = Kokkos::ArithTraits<value_type>;
86
87 /// randomized input testing views
88 ScalarType alpha = ScalarType(1.5), beta = ScalarType(3.0);
89
90 ViewType a_expected("a_expected", N, matAdim1, matAdim2), a_actual("a_actual", N, matAdim1, matAdim2),
91 b_expected("b_expected", N, matBdim1, matBdim2), b_actual("b_actual", N, matBdim1, matBdim2),
92 c_expected("c_expected", N, matCdim1, matCdim2), c_actual("c_actual", N, matCdim1, matCdim2);
93
94 Kokkos::Random_XorShift64_Pool<typename DeviceType::execution_space> random(13718);
95
96 Kokkos::fill_random(a_expected, random, value_type(1.0));
97 Kokkos::fill_random(b_expected, random, value_type(1.0));
98 Kokkos::fill_random(c_expected, random, value_type(1.0));
99
100 Kokkos::fence();
101
102 Kokkos::deep_copy(a_actual, a_expected);
103 Kokkos::deep_copy(b_actual, b_expected);
104 Kokkos::deep_copy(c_actual, c_expected);
105
106 Functor_BatchedVanillaGEMM<ViewType, ViewType, ViewType, execution_space> vgemm;
107 vgemm.A_t = std::is_same<transA, Trans::Transpose>::value;
108 vgemm.B_t = std::is_same<transB, Trans::Transpose>::value;
109 vgemm.A_c = vgemm.B_c = false;
110 vgemm.A = a_expected;
111 vgemm.B = b_expected;
112 vgemm.C = c_expected;
113 vgemm.alpha = alpha;
114 vgemm.beta = beta;
115 vgemm.run(); // Compute c_expected
116
117 Functor_TestBatchedTeamGemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(alpha, a_actual, b_actual,
118 beta, c_actual)
119 .run();
120
121 Kokkos::fence();
122
123 typename ViewType::HostMirror c_expected_host = Kokkos::create_mirror_view(c_expected);
124 typename ViewType::HostMirror c_actual_host = Kokkos::create_mirror_view(c_actual);
125
126 // Copy to host for comparision
127 Kokkos::deep_copy(c_expected_host, c_expected);
128 Kokkos::deep_copy(c_actual_host, c_actual);
129
130 using mag_type = typename ats::mag_type;
131 mag_type sum(1), diff(0);
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 TeamGemm
148 } // namespace Test
149
150 // void (*impl_test)(const int, const int, const int, const int, const int,
151 // const int, const int)
152 template <typename DeviceType, typename ValueType, typename ScalarType, typename ParamTagType, typename AlgoTagType>
153 int test_batched_teamgemm() {
154 #if defined(KOKKOSKERNELS_INST_LAYOUTLEFT)
155 {
156 typedef Kokkos::View<ValueType ***, Kokkos::LayoutLeft, DeviceType> ViewType;
157 Test::TeamGemm::impl_test_batched_teamgemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(
158 0, 10, 10, 10, 10, 10, 10);
159 for (int i = 0; i < 10; ++i) {
160 // printf("Testing: LayoutLeft, Blksize %d\n", i);
161 Test::TeamGemm::impl_test_batched_teamgemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(
162 1024, i, i, i, i, i, i);
163 }
164 for (int i = 0; i < 10; ++i) {
165 // printf("Testing: LayoutLeft, Blksize %d\n", i);
166 int dimM = i;
167 int dimN = 2 * i;
168 int dimK = 3 * i;
169 if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::NoTranspose>::value) &&
170 (std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::NoTranspose>::value)) {
171 Test::TeamGemm::impl_test_batched_teamgemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(
172 1024, dimM, dimK, dimK, dimN, dimM, dimN);
173 }
174 if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::NoTranspose>::value) &&
175 (std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::Transpose>::value)) {
176 Test::TeamGemm::impl_test_batched_teamgemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(
177 1024, dimM, dimK, dimN, dimK, dimM, dimN);
178 }
179 if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::Transpose>::value) &&
180 (std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::NoTranspose>::value)) {
181 Test::TeamGemm::impl_test_batched_teamgemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(
182 1024, dimK, dimM, dimK, dimN, dimM, dimN);
183 }
184 if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::Transpose>::value) &&
185 (std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::Transpose>::value)) {
186 Test::TeamGemm::impl_test_batched_teamgemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(
187 1024, dimK, dimM, dimN, dimK, dimM, dimN);
188 }
189 }
190 }
191 #endif
192 #if defined(KOKKOSKERNELS_INST_LAYOUTRIGHT)
193 {
194 typedef Kokkos::View<ValueType ***, Kokkos::LayoutRight, DeviceType> ViewType;
195 Test::TeamGemm::impl_test_batched_teamgemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(
196 0, 10, 10, 10, 10, 10, 10);
197 for (int i = 0; i < 10; ++i) {
198 // printf("Testing: LayoutRight, Blksize %d\n", i);
199 Test::TeamGemm::impl_test_batched_teamgemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(
200 1024, i, i, i, i, i, i);
201 }
202 for (int i = 0; i < 10; ++i) {
203 // printf("Testing: LayoutLeft, Blksize %d\n", i);
204 int dimM = i;
205 int dimN = 2 * i;
206 int dimK = 3 * i;
207 if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::NoTranspose>::value) &&
208 (std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::NoTranspose>::value)) {
209 Test::TeamGemm::impl_test_batched_teamgemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(
210 1024, dimM, dimK, dimK, dimN, dimM, dimN);
211 }
212 if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::NoTranspose>::value) &&
213 (std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::Transpose>::value)) {
214 Test::TeamGemm::impl_test_batched_teamgemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(
215 1024, dimM, dimK, dimN, dimK, dimM, dimN);
216 }
217 if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::Transpose>::value) &&
218 (std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::NoTranspose>::value)) {
219 Test::TeamGemm::impl_test_batched_teamgemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(
220 1024, dimK, dimM, dimK, dimN, dimM, dimN);
221 }
222 if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::Transpose>::value) &&
223 (std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::Transpose>::value)) {
224 Test::TeamGemm::impl_test_batched_teamgemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(
225 1024, dimK, dimM, dimN, dimK, dimM, dimN);
226 }
227 }
228 }
229 #endif
230
231 return 0;
232 }