| 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 |
#include "gtest/gtest.h" |
| 17 |
#include "Kokkos_Core.hpp" |
| 18 |
#include "Kokkos_Random.hpp" |
| 19 |
|
| 20 |
#include "KokkosBatched_Gemm_Decl.hpp" |
| 21 |
#include "KokkosBatched_Gemm_TeamVector_Impl.hpp" |
| 22 |
|
| 23 |
#include "KokkosKernels_TestUtils.hpp" |
| 24 |
|
| 25 |
using namespace KokkosBatched; |
| 26 |
|
| 27 |
namespace Test { |
| 28 |
namespace TeamVectorGemm { |
| 29 |
|
| 30 |
template <typename TA, typename TB> |
| 31 |
struct ParamTag { |
| 32 |
typedef TA transA; |
| 33 |
typedef TB transB; |
| 34 |
}; |
| 35 |
|
| 36 |
template <typename DeviceType, typename ViewType, typename ScalarType, typename ParamTagType, typename AlgoTagType> |
| 37 |
struct Functor_TestBatchedTeamVector { |
| 38 |
using execution_space = typename DeviceType::execution_space; |
| 39 |
ViewType _a, _b, _c; |
| 40 |
|
| 41 |
ScalarType _alpha, _beta; |
| 42 |
|
| 43 |
KOKKOS_INLINE_FUNCTION |
| 44 |
Functor_TestBatchedTeamVector(const ScalarType alpha, const ViewType &a, const ViewType &b, const ScalarType beta, |
| 45 |
const ViewType &c) |
| 46 |
: _a(a), _b(b), _c(c), _alpha(alpha), _beta(beta) {} |
| 47 |
|
| 48 |
template <typename MemberType> |
| 49 |
KOKKOS_INLINE_FUNCTION void operator()(const ParamTagType &, const MemberType &member) const { |
| 50 |
const int k = member.league_rank(); |
| 51 |
|
| 52 |
auto aa = Kokkos::subview(_a, k, Kokkos::ALL(), Kokkos::ALL()); |
| 53 |
auto bb = Kokkos::subview(_b, k, Kokkos::ALL(), Kokkos::ALL()); |
| 54 |
auto cc = Kokkos::subview(_c, k, Kokkos::ALL(), Kokkos::ALL()); |
| 55 |
|
| 56 |
KokkosBatched::TeamVectorGemm<MemberType, typename ParamTagType::transA, typename ParamTagType::transB, |
| 57 |
AlgoTagType>::invoke(member, _alpha, aa, bb, _beta, cc); |
| 58 |
} |
| 59 |
|
| 60 |
inline void run() { |
| 61 |
typedef typename ViewType::value_type value_type; |
| 62 |
std::string name_region("KokkosBatched::Test::TeamVector"); |
| 63 |
const std::string name_value_type = Test::value_type_name<value_type>(); |
| 64 |
std::string name = name_region + name_value_type; |
| 65 |
Kokkos::Profiling::pushRegion(name.c_str()); |
| 66 |
const int league_size = _c.extent(0); |
| 67 |
Kokkos::TeamPolicy<execution_space, ParamTagType> policy(league_size, Kokkos::AUTO); |
| 68 |
Kokkos::parallel_for(name.c_str(), policy, *this); |
| 69 |
Kokkos::Profiling::popRegion(); |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
template <typename DeviceType, typename ViewType, typename ScalarType, typename ParamTagType, typename AlgoTagType> |
| 74 |
void impl_test_batched_teamvectorgemm(const int N, const int matAdim1, const int matAdim2, const int matBdim1, |
| 75 |
const int matBdim2, const int matCdim1, const int matCdim2) { |
| 76 |
using transA = typename ParamTagType::transA; |
| 77 |
using transB = typename ParamTagType::transB; |
| 78 |
using execution_space = typename DeviceType::execution_space; |
| 79 |
using value_type = typename ViewType::value_type; |
| 80 |
using ats = Kokkos::ArithTraits<value_type>; |
| 81 |
|
| 82 |
/// randomized input testing views |
| 83 |
ScalarType alpha = ScalarType(1.5), beta = ScalarType(3.0); |
| 84 |
|
| 85 |
ViewType a_expected("a_expected", N, matAdim1, matAdim2), a_actual("a_actual", N, matAdim1, matAdim2), |
| 86 |
b_expected("b_expected", N, matBdim1, matBdim2), b_actual("b_actual", N, matBdim1, matBdim2), |
| 87 |
c_expected("c_expected", N, matCdim1, matCdim2), c_actual("c_actual", N, matCdim1, matCdim2); |
| 88 |
|
| 89 |
Kokkos::Random_XorShift64_Pool<typename DeviceType::execution_space> random(13718); |
| 90 |
|
| 91 |
Kokkos::fill_random(a_expected, random, value_type(1.0)); |
| 92 |
Kokkos::fill_random(b_expected, random, value_type(1.0)); |
| 93 |
Kokkos::fill_random(c_expected, random, value_type(1.0)); |
| 94 |
|
| 95 |
Kokkos::fence(); |
| 96 |
|
| 97 |
Kokkos::deep_copy(a_actual, a_expected); |
| 98 |
Kokkos::deep_copy(b_actual, b_expected); |
| 99 |
Kokkos::deep_copy(c_actual, c_expected); |
| 100 |
|
| 101 |
// Functor_TestBatchedTeamVector<DeviceType,ViewType,ScalarType, |
| 102 |
// ParamTagType,Algo::Gemm::Unblocked>(alpha, a_expected, b_expected, |
| 103 |
// beta, c_expected).run(); |
| 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 |
|
| 115 |
Functor_TestBatchedTeamVector<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>(alpha, a_actual, b_actual, |
| 116 |
beta, c_actual) |
| 117 |
.run(); |
| 118 |
|
| 119 |
Kokkos::fence(); |
| 120 |
|
| 121 |
typename ViewType::HostMirror c_expected_host = Kokkos::create_mirror_view(c_expected); |
| 122 |
typename ViewType::HostMirror c_actual_host = Kokkos::create_mirror_view(c_actual); |
| 123 |
|
| 124 |
// Copy to host for comparison |
| 125 |
Kokkos::deep_copy(c_expected_host, c_expected); |
| 126 |
Kokkos::deep_copy(c_actual_host, c_actual); |
| 127 |
|
| 128 |
using mag_type = typename ats::mag_type; |
| 129 |
mag_type sum(1), diff(0); |
| 130 |
|
| 131 |
mag_type eps = ats::epsilon(); |
| 132 |
|
| 133 |
eps *= std::is_same<value_type, Kokkos::Experimental::half_t>::value || |
| 134 |
std::is_same<value_type, Kokkos::Experimental::bhalf_t>::value |
| 135 |
? 4 |
| 136 |
: 1e3; |
| 137 |
|
| 138 |
for (int k = 0; k < N; ++k) |
| 139 |
for (int i = 0; i < matCdim1; ++i) |
| 140 |
for (int j = 0; j < matCdim2; ++j) { |
| 141 |
sum += ats::abs(c_expected_host(k, i, j)); |
| 142 |
diff += ats::abs(c_expected_host(k, i, j) - c_actual_host(k, i, j)); |
| 143 |
} |
| 144 |
EXPECT_NEAR_KK(diff / sum, 0, eps); |
| 145 |
} |
| 146 |
} // namespace TeamVectorGemm |
| 147 |
} // namespace Test |
| 148 |
|
| 149 |
// void (*impl_test)(const int, const int, const int, const int, const int, |
| 150 |
// const int, const int) |
| 151 |
template <typename DeviceType, typename ValueType, typename ScalarType, typename ParamTagType, typename AlgoTagType> |
| 152 |
int test_batched_teamvectorgemm() { |
| 153 |
#if defined(KOKKOSKERNELS_INST_LAYOUTLEFT) |
| 154 |
{ |
| 155 |
typedef Kokkos::View<ValueType ***, Kokkos::LayoutLeft, DeviceType> ViewType; |
| 156 |
Test::TeamVectorGemm::impl_test_batched_teamvectorgemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>( |
| 157 |
0, 10, 10, 10, 10, 10, 10); |
| 158 |
for (int i = 0; i < 10; ++i) { |
| 159 |
// printf("Testing: LayoutLeft, Blksize %d\n", i); |
| 160 |
Test::TeamVectorGemm::impl_test_batched_teamvectorgemm<DeviceType, ViewType, ScalarType, ParamTagType, |
| 161 |
AlgoTagType>(1024, i, i, i, i, i, i); |
| 162 |
} |
| 163 |
for (int i = 0; i < 10; ++i) { |
| 164 |
// printf("Testing: LayoutLeft, Blksize %d\n", i); |
| 165 |
int dimM = i; |
| 166 |
int dimN = 2 * i; |
| 167 |
int dimK = 3 * i; |
| 168 |
if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::NoTranspose>::value) && |
| 169 |
(std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::NoTranspose>::value)) { |
| 170 |
Test::TeamVectorGemm::impl_test_batched_teamvectorgemm<DeviceType, ViewType, ScalarType, ParamTagType, |
| 171 |
AlgoTagType>(1024, dimM, dimK, dimK, dimN, dimM, dimN); |
| 172 |
} |
| 173 |
if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::NoTranspose>::value) && |
| 174 |
(std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::Transpose>::value)) { |
| 175 |
Test::TeamVectorGemm::impl_test_batched_teamvectorgemm<DeviceType, ViewType, ScalarType, ParamTagType, |
| 176 |
AlgoTagType>(1024, dimM, dimK, dimN, dimK, dimM, dimN); |
| 177 |
} |
| 178 |
if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::Transpose>::value) && |
| 179 |
(std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::NoTranspose>::value)) { |
| 180 |
Test::TeamVectorGemm::impl_test_batched_teamvectorgemm<DeviceType, ViewType, ScalarType, ParamTagType, |
| 181 |
AlgoTagType>(1024, dimK, dimM, dimK, dimN, dimM, dimN); |
| 182 |
} |
| 183 |
if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::Transpose>::value) && |
| 184 |
(std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::Transpose>::value)) { |
| 185 |
Test::TeamVectorGemm::impl_test_batched_teamvectorgemm<DeviceType, ViewType, ScalarType, ParamTagType, |
| 186 |
AlgoTagType>(1024, dimK, dimM, dimN, dimK, dimM, dimN); |
| 187 |
} |
| 188 |
} |
| 189 |
} |
| 190 |
#endif |
| 191 |
#if defined(KOKKOSKERNELS_INST_LAYOUTRIGHT) |
| 192 |
{ |
| 193 |
typedef Kokkos::View<ValueType ***, Kokkos::LayoutRight, DeviceType> ViewType; |
| 194 |
Test::TeamVectorGemm::impl_test_batched_teamvectorgemm<DeviceType, ViewType, ScalarType, ParamTagType, AlgoTagType>( |
| 195 |
0, 10, 10, 10, 10, 10, 10); |
| 196 |
for (int i = 0; i < 10; ++i) { |
| 197 |
// printf("Testing: LayoutRight, Blksize %d\n", i); |
| 198 |
Test::TeamVectorGemm::impl_test_batched_teamvectorgemm<DeviceType, ViewType, ScalarType, ParamTagType, |
| 199 |
AlgoTagType>(1024, i, i, i, i, i, i); |
| 200 |
} |
| 201 |
for (int i = 0; i < 10; ++i) { |
| 202 |
// printf("Testing: LayoutLeft, Blksize %d\n", i); |
| 203 |
int dimM = i; |
| 204 |
int dimN = 2 * i; |
| 205 |
int dimK = 3 * i; |
| 206 |
if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::NoTranspose>::value) && |
| 207 |
(std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::NoTranspose>::value)) { |
| 208 |
Test::TeamVectorGemm::impl_test_batched_teamvectorgemm<DeviceType, ViewType, ScalarType, ParamTagType, |
| 209 |
AlgoTagType>(1024, dimM, dimK, dimK, dimN, dimM, dimN); |
| 210 |
} |
| 211 |
if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::NoTranspose>::value) && |
| 212 |
(std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::Transpose>::value)) { |
| 213 |
Test::TeamVectorGemm::impl_test_batched_teamvectorgemm<DeviceType, ViewType, ScalarType, ParamTagType, |
| 214 |
AlgoTagType>(1024, dimM, dimK, dimN, dimK, dimM, dimN); |
| 215 |
} |
| 216 |
if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::Transpose>::value) && |
| 217 |
(std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::NoTranspose>::value)) { |
| 218 |
Test::TeamVectorGemm::impl_test_batched_teamvectorgemm<DeviceType, ViewType, ScalarType, ParamTagType, |
| 219 |
AlgoTagType>(1024, dimK, dimM, dimK, dimN, dimM, dimN); |
| 220 |
} |
| 221 |
if ((std::is_same<typename ParamTagType::transA, KokkosBatched::Trans::Transpose>::value) && |
| 222 |
(std::is_same<typename ParamTagType::transB, KokkosBatched::Trans::Transpose>::value)) { |
| 223 |
Test::TeamVectorGemm::impl_test_batched_teamvectorgemm<DeviceType, ViewType, ScalarType, ParamTagType, |
| 224 |
AlgoTagType>(1024, dimK, dimM, dimN, dimK, dimM, dimN); |
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
#endif |
| 229 |
|
| 230 |
return 0; |
| 231 |
} |