|
| 1 | +#pragma once |
| 2 | + |
| 3 | +#include <CL/sycl/detail/defines_elementary.hpp> |
| 4 | +#include <immintrin.h> |
| 5 | + |
| 6 | +__SYCL_INLINE_NAMESPACE(cl) { |
| 7 | +namespace sycl { |
| 8 | +namespace ext { |
| 9 | +namespace oneapi { |
| 10 | +namespace experimental::matrix { |
| 11 | + |
| 12 | +enum class matrix_use { a, b, accumulator }; |
| 13 | + |
| 14 | +enum class matrix_layout { row_major, col_major, packed_a, packed_b }; |
| 15 | + |
| 16 | +template <typename T, matrix_use MT, size_t Rows = sycl::dynamic_extent, |
| 17 | + size_t Cols = sycl::dynamic_extent, |
| 18 | + matrix_layout Layout = matrix_layout::row_major, |
| 19 | + typename Group = sycl::sub_group, typename Cond = void> |
| 20 | +struct joint_matrix { |
| 21 | + joint_matrix(Group g) {} |
| 22 | +}; |
| 23 | + |
| 24 | +// The enable_if_t usage in this file is used to disable the |
| 25 | +// matrix_layout::packed case which is not compatible with the Nvidia cuda |
| 26 | +// backend. |
| 27 | +template <matrix_layout Layout> |
| 28 | +struct joint_matrix< |
| 29 | + double, matrix_use::a, 8, 4, Layout, sycl::sub_group, |
| 30 | + typename std::enable_if_t<Layout == matrix_layout::row_major || |
| 31 | + Layout == matrix_layout::col_major>> { |
| 32 | + double data[1]; |
| 33 | +}; |
| 34 | + |
| 35 | +template <matrix_layout Layout> |
| 36 | +struct joint_matrix< |
| 37 | + double, matrix_use::b, 4, 8, Layout, sycl::sub_group, |
| 38 | + typename std::enable_if_t<(Layout == matrix_layout::row_major || |
| 39 | + Layout == matrix_layout::col_major)>> { |
| 40 | + double data[1]; |
| 41 | +}; |
| 42 | + |
| 43 | +template <matrix_layout Layout> |
| 44 | +struct joint_matrix< |
| 45 | + double, matrix_use::accumulator, 8, 8, Layout, sycl::sub_group, |
| 46 | + typename std::enable_if_t<Layout == matrix_layout::row_major || |
| 47 | + Layout == matrix_layout::col_major>> { |
| 48 | + double data[2]; |
| 49 | +}; |
| 50 | + |
| 51 | +} // namespace experimental::matrix |
| 52 | + |
| 53 | +namespace detail { |
| 54 | +using namespace experimental; |
| 55 | + |
| 56 | +template <typename T, matrix::matrix_use MT, size_t NumRows, size_t NumCols, |
| 57 | + matrix::matrix_layout Layout, access::address_space Space, |
| 58 | + typename Cond = void> |
| 59 | +struct joint_matrix_load_impl { |
| 60 | + void load(matrix::joint_matrix<T, MT, NumRows, NumCols, Layout> &res, |
| 61 | + multi_ptr<T, Space> src, size_t stride); |
| 62 | +}; |
| 63 | + |
| 64 | +template <matrix::matrix_layout Layout> constexpr int get_layout_id(); |
| 65 | + |
| 66 | +template <> constexpr int get_layout_id<matrix::matrix_layout::row_major>() { |
| 67 | + return 0; |
| 68 | +} |
| 69 | + |
| 70 | +template <> constexpr int get_layout_id<matrix::matrix_layout::col_major>() { |
| 71 | + return 1; |
| 72 | +} |
| 73 | + |
| 74 | +template <matrix::matrix_layout Layout, access::address_space Space> |
| 75 | +struct joint_matrix_load_impl< |
| 76 | + double, matrix::matrix_use::a, 8, 4, Layout, Space, |
| 77 | + typename std::enable_if_t<Layout == matrix::matrix_layout::row_major || |
| 78 | + Layout == matrix::matrix_layout::col_major>> { |
| 79 | + void |
| 80 | + load(matrix::joint_matrix<double, matrix::matrix_use::a, 8, 4, Layout> &res, |
| 81 | + multi_ptr<double, Space> src, size_t stride) { |
| 82 | + |
| 83 | +#ifdef __NVPTX__ |
| 84 | +#ifdef __SYCL_DEVICE_ONLY__ |
| 85 | + __dmma_m8n8k4_ld_a(res.data, src.get(), stride, get_layout_id<Layout>()); |
| 86 | +#endif |
| 87 | +#endif |
| 88 | + } |
| 89 | +}; |
| 90 | + |
| 91 | +template <matrix::matrix_layout Layout, access::address_space Space> |
| 92 | +struct joint_matrix_load_impl< |
| 93 | + double, matrix::matrix_use::b, 4, 8, Layout, Space, |
| 94 | + typename std::enable_if_t<Layout == matrix::matrix_layout::row_major || |
| 95 | + Layout == matrix::matrix_layout::col_major>> { |
| 96 | + void |
| 97 | + load(matrix::joint_matrix<double, matrix::matrix_use::b, 4, 8, Layout> &res, |
| 98 | + multi_ptr<double, Space> src, size_t stride) { |
| 99 | +#ifdef __NVPTX__ |
| 100 | +#ifdef __SYCL_DEVICE_ONLY__ |
| 101 | + __dmma_m8n8k4_ld_b(res.data, src.get(), stride, get_layout_id<Layout>()); |
| 102 | +#endif |
| 103 | +#endif |
| 104 | + } |
| 105 | +}; |
| 106 | + |
| 107 | +template <matrix::matrix_layout Layout, access::address_space Space> |
| 108 | +struct joint_matrix_load_impl< |
| 109 | + double, matrix::matrix_use::accumulator, 8, 8, Layout, Space, |
| 110 | + typename std::enable_if_t<Layout == matrix::matrix_layout::row_major || |
| 111 | + Layout == matrix::matrix_layout::col_major>> { |
| 112 | + void load(matrix::joint_matrix<double, matrix::matrix_use::accumulator, 8, 8, |
| 113 | + Layout> &res, |
| 114 | + multi_ptr<double, Space> src, size_t stride) { |
| 115 | + |
| 116 | +#ifdef __NVPTX__ |
| 117 | +#ifdef __SYCL_DEVICE_ONLY__ |
| 118 | + __dmma_m8n8k4_ld_c(res.data, src.get(), stride, get_layout_id<Layout>()); |
| 119 | +#endif |
| 120 | +#endif |
| 121 | + } |
| 122 | +}; |
| 123 | + |
| 124 | +template <typename T, size_t NumRows, size_t NumCols, |
| 125 | + matrix::matrix_layout Layout, access::address_space Space, |
| 126 | + typename Cond = void> |
| 127 | +struct joint_matrix_store_impl { |
| 128 | + void store(matrix::joint_matrix<T, matrix::matrix_use::accumulator, NumRows, |
| 129 | + NumCols, Layout> &src, |
| 130 | + multi_ptr<T, Space> dst, size_t stride); |
| 131 | +}; |
| 132 | + |
| 133 | +template <matrix::matrix_layout Layout, access::address_space Space> |
| 134 | +struct joint_matrix_store_impl< |
| 135 | + double, 8, 8, Layout, Space, |
| 136 | + typename std::enable_if_t<Layout == matrix::matrix_layout::row_major || |
| 137 | + Layout == matrix::matrix_layout::col_major>> { |
| 138 | + void store(matrix::joint_matrix<double, matrix::matrix_use::accumulator, 8, 8, |
| 139 | + Layout> &src, |
| 140 | + multi_ptr<double, Space> dst, size_t stride) { |
| 141 | + |
| 142 | +#ifdef __NVPTX__ |
| 143 | +#ifdef __SYCL_DEVICE_ONLY__ |
| 144 | + __dmma_m8n8k4_st_c_f64(dst.get(), src.data, stride, |
| 145 | + get_layout_id<Layout>()); |
| 146 | +#endif |
| 147 | +#endif |
| 148 | + } |
| 149 | +}; |
| 150 | + |
| 151 | +template <typename T1, typename T2, std::size_t M, std::size_t K, std::size_t N, |
| 152 | + matrix::matrix_layout LayoutA, matrix::matrix_layout LayoutB, |
| 153 | + matrix::matrix_layout LayoutC, typename Cond = void> |
| 154 | +struct joint_matrix_mad_impl { |
| 155 | + matrix::joint_matrix<T2, matrix::matrix_use::accumulator, M, N, LayoutC> |
| 156 | + mad(matrix::joint_matrix<T1, matrix::matrix_use::a, M, K, LayoutA> A, |
| 157 | + matrix::joint_matrix<T1, matrix::matrix_use::b, K, N, LayoutB> B, |
| 158 | + matrix::joint_matrix<T2, matrix::matrix_use::accumulator, M, N, LayoutC> |
| 159 | + C); |
| 160 | +}; |
| 161 | + |
| 162 | +template <matrix::matrix_layout LayoutA, matrix::matrix_layout LayoutB> |
| 163 | +constexpr int get_layout_pair_id(); |
| 164 | + |
| 165 | +template <> |
| 166 | +constexpr int get_layout_pair_id<matrix::matrix_layout::row_major, |
| 167 | + matrix::matrix_layout::row_major>() { |
| 168 | + return 0; |
| 169 | +} |
| 170 | + |
| 171 | +template <> |
| 172 | +constexpr int get_layout_pair_id<matrix::matrix_layout::row_major, |
| 173 | + matrix::matrix_layout::col_major>() { |
| 174 | + return 1; |
| 175 | +} |
| 176 | + |
| 177 | +template <> |
| 178 | +constexpr int get_layout_pair_id<matrix::matrix_layout::col_major, |
| 179 | + matrix::matrix_layout::row_major>() { |
| 180 | + return 2; |
| 181 | +} |
| 182 | + |
| 183 | +template <> |
| 184 | +constexpr int get_layout_pair_id<matrix::matrix_layout::col_major, |
| 185 | + matrix::matrix_layout::col_major>() { |
| 186 | + return 3; |
| 187 | +} |
| 188 | + |
| 189 | +template <matrix::matrix_layout LayoutA, matrix::matrix_layout LayoutB, |
| 190 | + matrix::matrix_layout LayoutC> |
| 191 | +struct joint_matrix_mad_impl< |
| 192 | + double, double, 8, 4, 8, LayoutA, LayoutB, LayoutC, |
| 193 | + typename std::enable_if_t<(LayoutA == matrix::matrix_layout::row_major || |
| 194 | + LayoutA == matrix::matrix_layout::col_major) && |
| 195 | + (LayoutB == matrix::matrix_layout::row_major || |
| 196 | + LayoutB == matrix::matrix_layout::col_major) && |
| 197 | + (LayoutC == matrix::matrix_layout::row_major || |
| 198 | + LayoutC == matrix::matrix_layout::col_major)>> { |
| 199 | + matrix::joint_matrix<double, matrix::matrix_use::accumulator, 8, 8, LayoutC> |
| 200 | + mad(matrix::joint_matrix<double, matrix::matrix_use::a, 8, 4, LayoutA> A, |
| 201 | + matrix::joint_matrix<double, matrix::matrix_use::b, 4, 8, LayoutB> B, |
| 202 | + matrix::joint_matrix<double, matrix::matrix_use::accumulator, 8, 8, |
| 203 | + LayoutC> |
| 204 | + C) { |
| 205 | + matrix::joint_matrix<double, matrix::matrix_use::accumulator, 8, 8, LayoutC> |
| 206 | + D; |
| 207 | + |
| 208 | +#ifdef __NVPTX__ |
| 209 | +#ifdef __SYCL_DEVICE_ONLY__ |
| 210 | + __dmma_m8n8k4_mma_f64(D.data, A.data, B.data, C.data, |
| 211 | + get_layout_pair_id<LayoutA, LayoutB>(), 0); |
| 212 | +#endif |
| 213 | +#endif |
| 214 | + |
| 215 | + return D; |
| 216 | + } |
| 217 | +}; |
| 218 | + |
| 219 | +} // namespace detail |
| 220 | + |
| 221 | +namespace experimental::matrix { |
| 222 | + |
| 223 | +template <typename Group, typename T, matrix_use MT, size_t NumRows, |
| 224 | + size_t NumCols, matrix_layout Layout, access::address_space Space> |
| 225 | +void joint_matrix_load( |
| 226 | + Group sg, joint_matrix<T, MT, NumRows, NumCols, Layout, Group> &res, |
| 227 | + multi_ptr<T, Space> src, size_t stride) { |
| 228 | + detail::joint_matrix_load_impl<T, MT, NumRows, NumCols, Layout, Space>{}.load( |
| 229 | + res, src, stride); |
| 230 | +} |
| 231 | + |
| 232 | +template <typename Group, typename T, size_t NumRows, size_t NumCols, |
| 233 | + matrix_layout Layout, access::address_space Space> |
| 234 | +void joint_matrix_store(Group sg, |
| 235 | + joint_matrix<T, matrix_use::accumulator, NumRows, |
| 236 | + NumCols, Layout, Group> &src, |
| 237 | + multi_ptr<T, Space> dst, size_t stride) { |
| 238 | + detail::joint_matrix_store_impl<T, NumRows, NumCols, Layout, Space>{}.store( |
| 239 | + src, dst, stride); |
| 240 | +} |
| 241 | + |
| 242 | +template <typename Group, typename T1, typename T2, std::size_t M, |
| 243 | + std::size_t K, std::size_t N, matrix_layout LayoutA, |
| 244 | + matrix_layout LayoutB, matrix_layout LayoutC> |
| 245 | +joint_matrix<T2, matrix_use::accumulator, M, N, LayoutC, Group> |
| 246 | +joint_matrix_mad( |
| 247 | + Group sg, joint_matrix<T1, matrix_use::a, M, K, LayoutA, Group> A, |
| 248 | + joint_matrix<T1, matrix_use::b, K, N, LayoutB, Group> B, |
| 249 | + joint_matrix<T2, matrix_use::accumulator, M, N, LayoutC, Group> C) { |
| 250 | + return detail::joint_matrix_mad_impl<T1, T2, M, K, N, LayoutA, LayoutB, |
| 251 | + LayoutC>{} |
| 252 | + .mad(A, B, C); |
| 253 | +} |
| 254 | + |
| 255 | +} // namespace experimental::matrix |
| 256 | +} // namespace oneapi |
| 257 | +} // namespace ext |
| 258 | +} // namespace sycl |
| 259 | +} // __SYCL_INLINE_NAMESPACE(cl) |
0 commit comments