XeTLA v0.3.6
IntelĀ® Xe Templates for Linear Algebra - API Definition Document
 
Loading...
Searching...
No Matches
default_xmx_xe.hpp
Go to the documentation of this file.
1/*******************************************************************************
2* Copyright (c) 2022-2023 Intel Corporation
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
19
20#pragma once
21
22#include "group/gemm/api.hpp"
23#include "group/gemm/compute_policy.hpp"
24
25namespace gpu::xetla::group {
26
29
31template <typename compute_attr_, typename perf_tuning_knob_,
32 typename tile_shape_, typename mem_desc_a_t_, typename mem_desc_b_t_,
33 typename pre_processing_t_, gpu_arch arch_tag_>
34class gemm_t<
35 compute_policy_default_xmx<compute_attr_, perf_tuning_knob_, arch_tag_>,
36 tile_shape_, // tile shape of workgroup-level gemm
37 mem_desc_a_t_, // memory attribute of matA
38 mem_desc_b_t_, // memory attribute of matB
39 pre_processing_t_, // pre_processing functor
40 std::enable_if_t<(arch_tag_ == gpu_arch::Xe)>> {
41public:
42 using mem_desc_a_t = mem_desc_a_t_;
43 using mem_desc_b_t = mem_desc_b_t_;
44 using tile_shape = tile_shape_;
45 using pre_processing_t = pre_processing_t_;
47 perf_tuning_knob_, arch_tag_>;
48 static constexpr uint32_t k_stride = compute_policy::k_stride;
49 static constexpr uint32_t sg_tile_m = tile_shape::sg_tile_size_y;
50 static constexpr uint32_t sg_tile_n = tile_shape::sg_tile_size_x;
51 static constexpr uint32_t wg_size_x = tile_shape::wg_size_x;
52 static constexpr uint32_t wg_size_y = tile_shape::wg_size_y;
53 using work_group_t = typename tile_shape::work_group_t;
54
55 constexpr static gpu_arch arch_tag = compute_policy::arch_tag;
56
57 static constexpr mem_layout mem_layout_a = mem_desc_a_t::layout;
58 static constexpr mem_layout mem_layout_b = mem_desc_b_t::layout;
59 static constexpr bool is_col_major_a
60 = mem_layout_a == mem_layout::col_major;
61 static constexpr bool is_col_major_b
62 = mem_layout_b == mem_layout::col_major;
63
64private:
65 /******** set data type **********/
66 using dtype_a = typename mem_desc_a_t::dtype;
67 using dtype_b = typename mem_desc_b_t::dtype;
68 using dtype_mma_acc = typename compute_policy::dtype_mma_acc;
69 using dtype_mma_a = typename compute_policy::dtype_mma_a;
70 using dtype_mma_b = typename compute_policy::dtype_mma_b;
71
72 using check_dtype
74 dtype_a, dtype_b, dtype_mma_a, dtype_mma_b>;
75
76 /******** set memory attribute **********/
77 static constexpr mem_space mem_space_a = mem_desc_a_t::space;
78 static constexpr mem_space mem_space_b = mem_desc_b_t::space;
79
80 static constexpr bool is_local_a = mem_space_a == mem_space::local;
81 static constexpr bool is_local_b = mem_space_b == mem_space::local;
82 static constexpr tdesc_update_dir update_dir_a = is_col_major_a
85 static constexpr tdesc_update_dir update_dir_b = is_col_major_b
88
89 using check_memory
91 mem_layout_a, mem_layout_b, mem_space_a, mem_space_b>;
92
93 static constexpr uint32_t stages = compute_policy::stages;
94 static constexpr uint32_t sync_freq = compute_policy::sync_freq;
95
96 /******** set tile layout && worker scope **********/
97 static constexpr uint32_t tile_size_x_a = k_stride;
98 static constexpr uint32_t tile_size_y_a = sg_tile_m;
99 static constexpr uint32_t tile_size_x_b = sg_tile_n;
100 static constexpr uint32_t tile_size_y_b = k_stride;
101 static constexpr uint32_t tile_size_x_c = sg_tile_n;
102 static constexpr uint32_t tile_size_y_c = sg_tile_m;
103 static constexpr uint32_t block_size_x_a = compute_policy::block_size_x_a;
104 static constexpr uint32_t block_size_y_a
105 = (compute_policy::block_size_y_a > tile_size_y_a)
106 ? tile_size_y_a
107 : compute_policy::block_size_y_a;
108 static constexpr uint32_t block_size_x_b = compute_policy::block_size_x_b;
109 static constexpr uint32_t block_size_y_b = compute_policy::block_size_y_b;
110
111 using check_tile_size = group::gemm<
112 gpu_arch::Xe>::default_xmx::check_tile_size_default<dtype_mma_a,
113 tile_size_x_a, tile_size_y_a, block_size_x_a, block_size_y_a,
114 tile_size_x_b, tile_size_y_b, block_size_x_b, block_size_y_b>;
115
116 /******** set tile **********/
117 static constexpr reg_layout reg_layout_a = reg_layout::tiled;
118 using matA_tile_desc_t = subgroup::tile_desc_t<tile_size_x_a, tile_size_y_a,
119 block_size_x_a, block_size_y_a, reg_layout_a>;
123 is_local_a ? msg_type::scatter : msg_type::block_2d, arch_tag>;
127 wg_size_x, arch_tag>;
128 static constexpr reg_layout reg_layout_b
129 = sizeof(dtype_b) < sizeof(uint32_t) ? reg_layout::vnni_tiled
131 using matB_tile_desc_t = subgroup::tile_desc_t<tile_size_x_b, tile_size_y_b,
132 block_size_x_b, block_size_y_b, reg_layout_b>;
136 is_local_b ? msg_type::scatter : msg_type::block_2d, arch_tag>;
140 wg_size_y, arch_tag>;
141
142public:
144 tile_size_y_c, block_size_x_b, block_size_y_a, reg_layout::tiled>;
146
147private:
149 matA_acc_t, mma_engine::xmx, arch_tag>;
150 static constexpr bool enable_periodic_sync = (sync_freq != 0);
151 static constexpr uint32_t barrier_count_x = wg_size_y > 1 ? wg_size_x : 0;
152 static constexpr uint32_t barrier_count_y = wg_size_x > 1 ? wg_size_y : 0;
153
154public:
155 static constexpr uint32_t barrier_count
156 = enable_periodic_sync ? barrier_count_x + barrier_count_y : 0;
157
158 static constexpr uint32_t slm_size = 0;
159
160 static constexpr msg_type msg_type_a = matA_payload_t::message_type;
161 static constexpr msg_type msg_type_b = matB_payload_t::message_type;
162
163 using pre_processing_arg_t = typename pre_processing_t::arguments_t;
164
167 struct arguments_t {
176
178 inline arguments_t() = default;
179
185 inline arguments_t(mem_desc_a_t matA_desc, mem_desc_b_t matB_desc,
186 uint32_t loop_count, pre_processing_arg_t args = {})
187 : matA_base_desc(matA_desc)
188 , matB_base_desc(matB_desc)
189 , inner_loop_count(loop_count)
190 , pre_processing_args(args) {}
191 // Be aware of the risks: Rule of three (copy constructor, copy assignment, destructor)
192 // Please check if you need to add self-define destructor
193 // inline ~arguments_t(){}
194 inline arguments_t(const arguments_t &args)
195 : matA_base_desc(args.matA_base_desc)
196 , matB_base_desc(args.matB_base_desc)
197 , inner_loop_count(args.inner_loop_count)
198 , pre_processing_args(args.pre_processing_args) {}
199 inline arguments_t &operator=(const arguments_t &args) {
200 this->matA_base_desc = args.matA_base_desc;
201 this->matB_base_desc = args.matB_base_desc;
202 this->inner_loop_count = args.inner_loop_count;
203 this->pre_processing_args = args.pre_processing_args;
204 return *this;
205 }
206
212 inline void init(mem_desc_a_t matA_desc, mem_desc_b_t matB_desc,
213 uint32_t loop_count, pre_processing_arg_t args = {}) {
214 matA_base_desc = matA_desc;
215 matB_base_desc = matB_desc;
216 inner_loop_count = loop_count;
217 pre_processing_args = args;
218 }
219 };
220
225 int32_t sg_idx = g.get_id() % wg_size_x;
226 return sg_idx * sg_tile_n;
227 }
228
233 int32_t sg_idy = g.get_id() / wg_size_x;
234 return sg_idy * sg_tile_m;
235 }
236
238 "This release function will wait until all the r/w and nbarrier "
239 "id used in this gemm have been committed. By default, it will "
240 "use barrier_id 0 to do the entire workgroup sync if wg_size > 1. "
241 "If you call this function, please set a free barrier id or make "
242 "sure barrier_id 0 is not being occupied and you need to allocate "
243 "one more barrier count in addition to the gemm barrier counts.")
244 __XETLA_API static void release(uint8_t nbarrier_id = 0) {
245 static constexpr bool need_local_fence
246 = (mem_space_a == mem_space::local)
247 || (mem_space_b == mem_space::local);
248 if constexpr (need_local_fence) {
249 xetla_fence<memory_kind::shared_local>();
250 }
251 xetla_fence<memory_kind::untyped_global>();
252 static constexpr uint32_t wg_size = wg_size_x * wg_size_y;
253 if constexpr (wg_size > 1) {
255 nbarrier.init_nbarrier(
257 nbarrier.arrive_wait();
258 }
259 }
260
269 arguments_t args, [[maybe_unused]] uint32_t slm_base = 0,
270 uint32_t nbarrier_base = 0) {
271 int32_t sg_idx = g.get_id() % wg_size_x;
272 int32_t sg_idy = g.get_id() / wg_size_x;
273
274 XETLA_ASSERT(g.get_id() < (wg_size_x * wg_size_y),
275 "Thread id(%d) should less than wg_size(%d)", g.get_id(),
276 wg_size_x * wg_size_y);
277
278 update_sg_tile_tdesc(args, sg_idx, sg_idy);
280 matA_t matA;
281 matB_t matB;
282 // >>>>>>>>>>>>>>>>>> pre_processing init
283 pre_processing.init(g, args.pre_processing_args);
284 matA_payload_t matA_payload(args.matA_base_desc);
285 matB_payload_t matB_payload(args.matB_base_desc);
286 matA_prefetch_payload_t matA_prefetch_payload(
287 args.matA_base_desc, sg_idx);
288 matB_prefetch_payload_t matB_prefetch_payload(
289 args.matB_base_desc, sg_idy);
291 nbarrier_a.init_nbarrier(
292 sg_idy + nbarrier_base, nbarrier_role::producer_consumer);
294 nbarrier_b.init_nbarrier(sg_idx + barrier_count_y + nbarrier_base,
296
297#pragma unroll
298 for (uint32_t i = 0; i < stages; i++) {
299 subgroup::tile_prefetch<cache_hint::cached, cache_hint::cached>(
300 matA_prefetch_payload);
301 subgroup::tile_prefetch<cache_hint::cached, cache_hint::cached>(
302 matB_prefetch_payload);
303 matA_prefetch_payload.template update_tdesc<update_dir_a>(
304 matA_t::tile_size_x);
305 matB_prefetch_payload.template update_tdesc<update_dir_b>(
306 matB_t::tile_size_y);
307 }
308
309 for (uint32_t i = 0; i < args.inner_loop_count; i++) {
310 if constexpr (enable_periodic_sync) {
311 if ((i % sync_freq) == 0) {
312 if constexpr (wg_size_x > 1) { nbarrier_a.arrive(); }
313 if constexpr (wg_size_y > 1) { nbarrier_b.arrive(); }
314 }
315 }
316 subgroup::tile_load<cache_hint::cached, cache_hint::cached>(
317 matB, matB_payload);
318 subgroup::tile_load<cache_hint::cached, cache_hint::cached>(
319 matA, matA_payload);
320 if constexpr (stages != 0) {
321 subgroup::tile_prefetch<cache_hint::cached, cache_hint::cached>(
322 matA_prefetch_payload);
323 subgroup::tile_prefetch<cache_hint::cached, cache_hint::cached>(
324 matB_prefetch_payload);
325 }
326 SW_BARRIER();
327 matA_payload.template update_tdesc<update_dir_a>(
328 matA_t::tile_size_x);
329 matB_payload.template update_tdesc<update_dir_b>(
330 matB_t::tile_size_y);
331 if constexpr (stages != 0) {
332 matA_prefetch_payload.template update_tdesc<update_dir_a>(
333 matA_t::tile_size_x);
334 matB_prefetch_payload.template update_tdesc<update_dir_b>(
335 matB_t::tile_size_y);
336 }
337 SW_BARRIER();
338 matA_acc_t matA_acc;
339 matB_acc_t matB_acc;
340 subgroup::elemwise_cvt(matA_acc, matA);
341 subgroup::vnni_transform(matB_acc, matB);
342 pre_processing(matA_acc, matB_acc, matA, matB);
343 SW_BARRIER();
344 tile_mma::mma(matAcc, matAcc, matB_acc, matA_acc);
345 SW_BARRIER();
346 if constexpr (enable_periodic_sync) {
347 if ((i % sync_freq) == 0) {
348 if constexpr (wg_size_x > 1) { nbarrier_a.wait(); }
349 if constexpr (wg_size_y > 1) { nbarrier_b.wait(); }
350 }
351 }
352 }
353 SW_BARRIER();
354 }
355
356private:
358 __XETLA_API static void update_sg_tile_tdesc(
359 arguments_t &args, int32_t sg_idx, int32_t sg_idy) {
360 int32_t tile_offset_n = sg_idx * sg_tile_n;
361 int32_t tile_offset_m = sg_idy * sg_tile_m;
362
363 args.matA_base_desc.update_coord_y(tile_offset_m);
364 args.matB_base_desc.update_coord_x(tile_offset_n);
365 }
366};
367
369
370} // namespace gpu::xetla::group
XETLA_MARKER("This release function will wait until all the r/w and nbarrier " "id used in this gemm have been committed. By default, it will " "use barrier_id 0 to do the entire workgroup sync if wg_size > 1. " "If you call this function, please set a free barrier id or make " "sure barrier_id 0 is not being occupied and you need to allocate " "one more barrier count in addition to the gemm barrier counts.") __XETLA_API static void release(uint8_t nbarrier_id=0)
Definition default_xmx_xe.hpp:237
__XETLA_API KERNEL_FUNC void operator()(work_group_t &g, matAcc_t &matAcc, arguments_t args, uint32_t slm_base=0, uint32_t nbarrier_base=0)
Main execution function for gemm.
Definition default_xmx_xe.hpp:268
Gemm functor.
Definition api.hpp:52
#define SW_BARRIER()
SW_BARRIER, insert software scheduling barrier, for better code control.
Definition common.hpp:227
#define __XETLA_API
Definition common.hpp:43
#define XETLA_ASSERT(c, s,...)
Definition debug.hpp:158
C++ API.
#define KERNEL_FUNC
KERNEL_FUNC macro.
Definition common.hpp:39
Definition limitation.hpp:607
__XETLA_API std::enable_if_t<(T_src::register_layout !=reg_layout::linear) &&(T_dst::register_layout !=reg_layout::linear) &&is_same_layout< T_dst, T_src >::value &&(!is_floating_to_integer< T_dst, T_src >::value)> elemwise_cvt(T_dst &dst, T_src &src)
Is the element wise data conversion, the src and dst tile should have the same layout.
Definition op_function.hpp:40
__XETLA_API std::enable_if_t< is_same_layout< T_dst, T_src >::value > vnni_transform(T_dst &dst, T_src &src)
Changes vnni layout.
Definition op_function.hpp:355
reg_layout
tile layout in register linear: linear layout with one tile tiled: 2d block stacked in raster order v...
Definition common.hpp:209
mem_space
Definition common.hpp:77
gpu_arch
Definition common.hpp:73
msg_type
Definition common.hpp:78
tdesc_update_dir
Definition common.hpp:228
mem_layout
Definition common.hpp:76
Compute policy for xmx engine.
Definition compute_policy.hpp:35
arguments_t(mem_desc_a_t matA_desc, mem_desc_b_t matB_desc, uint32_t loop_count, pre_processing_arg_t args={})
Constructs a new arguments t object.
Definition default_xmx_xe.hpp:185
void init(mem_desc_a_t matA_desc, mem_desc_b_t matB_desc, uint32_t loop_count, pre_processing_arg_t args={})
Explicit initialization function.
Definition default_xmx_xe.hpp:212
Definition limitation.hpp:609
Is to illustrate the memory information.
Definition api.hpp:44
Is to illustrate the memory information to prefetch data to cache.
Definition api.hpp:53
Is to illustrate the tile information about a sub matrix.
Definition api.hpp:64
Is the xetla tile mma operation definition API.
Definition api.hpp:36
Is a struct contains some register file.
Definition api.hpp:99
xetla nbarrier definition API.
Definition raw_send_nbarrier.hpp:43
__XETLA_API void arrive()
named barrier signal from subgroup.
Definition raw_send_nbarrier.hpp:65
__XETLA_API void arrive_wait()
named barrier signal from subgroup.
Definition raw_send_nbarrier.hpp:80
__XETLA_API void init_nbarrier(uint8_t nbarrier_id, nbarrier_role role=nbarrier_role::producer_consumer)
Definition raw_send_nbarrier.hpp:55
__XETLA_API void wait()
named barrier wait within subgroup.
Definition raw_send_nbarrier.hpp:76