31template <
typename compute_attr_,
typename perf_tuning_knob_,
32 typename tile_shape_,
typename mem_desc_a_t_,
typename mem_desc_b_t_,
33 typename dtype_scale_,
typename dtype_zero_pt_,
int dequant_s_,
34 typename pre_processing_t_>
37 dtype_scale_, dtype_zero_pt_, dequant_s_,
gpu_arch::
Xe>,
49 perf_tuning_knob_, dtype_scale_, dtype_zero_pt_, dequant_s_,
51 static constexpr uint32_t k_stride = compute_policy::k_stride;
53 static constexpr uint32_t sg_tile_m = tile_shape::sg_tile_size_y;
54 static constexpr uint32_t sg_tile_n = tile_shape::sg_tile_size_x;
55 static constexpr uint32_t wg_size_x = tile_shape::wg_size_x;
56 static constexpr uint32_t wg_size_y = tile_shape::wg_size_y;
59 constexpr static gpu_arch arch_tag = compute_policy::arch_tag;
60 static constexpr uint32_t dequant_s = compute_policy::dequant_s;
61 using dtype_b =
typename mem_desc_b_t::dtype;
63 static constexpr uint32_t pack_ratio =
sizeof(
dtype_b) * 2;
65 static constexpr mem_layout mem_layout_a = mem_desc_a_t::layout;
66 static constexpr mem_layout mem_layout_b = mem_desc_b_t::layout;
67 static constexpr bool is_col_major_a
69 static constexpr bool is_col_major_b
74 using dtype_a =
typename mem_desc_a_t::dtype;
80 static_assert(std::is_same<remove_const_t<dtype_b>,
82 "this is for 4bit matB ");
83 static_assert(std::is_same<remove_const_t<dtype_zero_pt>,
85 "this is for 4bit zero_pt ");
88 static constexpr mem_space mem_space_a = mem_desc_a_t::space;
89 static constexpr mem_space mem_space_b = mem_desc_b_t::space;
99 static_assert(!is_col_major_b,
"only support MatB row-major for now");
100 static_assert((!is_local_a) && (!is_local_b),
101 "only support from global memory for now");
103 static constexpr uint32_t stages = compute_policy::stages;
104 static constexpr uint32_t sync_freq = compute_policy::sync_freq;
107 static constexpr uint32_t tile_size_x_a = k_stride;
108 static constexpr uint32_t tile_size_y_a = sg_tile_m;
109 static constexpr uint32_t tile_size_x_b = sg_tile_n;
110 static constexpr uint32_t tile_size_y_b = k_stride;
111 static constexpr uint32_t tile_size_x_c = sg_tile_n;
112 static constexpr uint32_t tile_size_y_c = sg_tile_m;
113 static constexpr uint32_t block_size_x_a
114 = compute_policy::block_bytes_x_a /
sizeof(dtype_mma_a);
115 static constexpr uint32_t block_size_y_a
116 = (compute_policy::block_size_y_a > tile_size_y_a)
120 static constexpr uint32_t block_size_x_b = compute_policy::block_size_x_b;
121 static constexpr uint32_t block_size_y_b
122 = compute_policy::block_bytes_y_b /
sizeof(dtype_mma_b);
125 static constexpr bool is_vnni_tiled_a
126 = (
sizeof(dtype_a) <
sizeof(uint32_t)) && is_col_major_a;
130 block_size_x_a, block_size_y_a, reg_layout_a>;
134 subgroup::msg_type_v<matA_tile_desc_t, mem_space_a>, arch_tag>;
138 wg_size_x, arch_tag>;
143 tile_size_y_b, block_size_x_b / pack_ratio, block_size_y_b,
148 subgroup::msg_type_v<matB_tile_desc_t, mem_space_b>, arch_tag>;
158 static_assert((k_stride % (block_size_y_b) == 0),
159 "k_stride%(block_size_y_b) == 0");
160 static_assert((dequant_s % (block_size_y_b) == 0),
161 "dequant_s%(block_size_y_b) == 0");
163 (k_stride % (dequant_s) == 0) || (dequant_s % (k_stride) == 0),
164 "k_stride should match with dequant_s");
167 static constexpr uint32_t block_size_y_scale
168 = (k_stride + dequant_s - 1) / dequant_s;
169 static constexpr uint32_t tile_size_y_scale = block_size_y_scale;
170 static constexpr uint32_t block_size_y_zero_pt
171 = (k_stride + dequant_s - 1) / dequant_s;
172 static constexpr uint32_t tile_size_y_zero_pt = block_size_y_zero_pt;
174 static constexpr uint32_t scale_addr_update_freq
175 = (k_stride < dequant_s) ? dequant_s / k_stride : 1;
193 subgroup::msg_type_v<scale_tile_desc_t, mem_space::global>,
197 tile_size_y_zero_pt, block_size_x_b / pack_ratio,
202 subgroup::msg_type_v<zero_pt_tile_desc_t, mem_space::global>,
213 static constexpr bool enable_periodic_sync = (sync_freq != 0);
214 static constexpr uint32_t barrier_count_x = wg_size_y > 1 ? wg_size_x : 0;
215 static constexpr uint32_t barrier_count_y = wg_size_x > 1 ? wg_size_y : 0;
218 static constexpr uint32_t barrier_count
219 = enable_periodic_sync ? barrier_count_x + barrier_count_y : 0;
221 static constexpr uint32_t slm_size = is_local_a
222 ? sg_tile_m * wg_size_y * k_stride *
sizeof(dtype_a)
225 static constexpr msg_type msg_type_a = matA_payload_t::message_type;
226 static constexpr msg_type msg_type_b = matB_payload_t::message_type;
248 : matA_base_desc(matA_desc)
249 , matB_base_desc(matB_desc)
250 , inner_loop_count(loop_count)
251 , scale_base_desc(scale_desc)
252 , zero_pt_base_desc(zero_pt_desc) {}
257 : matA_base_desc(args.matA_base_desc)
258 , matB_base_desc(args.matB_base_desc)
259 , inner_loop_count(args.inner_loop_count)
260 , scale_base_desc(args.scale_base_desc)
261 , zero_pt_base_desc(args.zero_pt_base_desc) {}
262 inline arguments_t &
operator=(
const arguments_t &args) {
263 this->matA_base_desc = args.matA_base_desc;
264 this->matB_base_desc = args.matB_base_desc;
265 this->inner_loop_count = args.inner_loop_count;
266 this->scale_base_desc = args.scale_base_desc;
267 this->zero_pt_base_desc = args.zero_pt_base_desc;
273 matA_base_desc = matA_desc;
274 matB_base_desc = matB_desc;
275 inner_loop_count = loop_count;
276 scale_base_desc = scale_desc;
277 zero_pt_base_desc = zero_pt_desc;
285 int32_t sg_idx = g.get_id() % wg_size_x;
286 return sg_idx * sg_tile_n;
293 int32_t sg_idy = g.get_id() / wg_size_x;
294 return sg_idy * sg_tile_m;
298 "This release function will wait until all the r/w and nbarrier "
299 "id used in this gemm have been committed. By default, it will "
300 "use barrier_id 0 to do the entire workgroup sync if wg_size > 1. "
301 "If you call this function, please set a free barrier id or make "
302 "sure barrier_id 0 is not being occupied and you need to allocate "
303 "one more barrier count in addition to the gemm barrier counts.")
304 __XETLA_API static
void release(uint8_t nbarrier_id = 0) {
305 static constexpr bool need_local_fence
308 if constexpr (need_local_fence) {
309 xetla_fence<memory_kind::shared_local>();
311 xetla_fence<memory_kind::untyped_global>();
312 static constexpr uint32_t wg_size = wg_size_x * wg_size_y;
313 if constexpr (wg_size > 1) {
329 arguments_t args, [[maybe_unused]] uint32_t slm_base = 0,
330 uint32_t nbarrier_base = 0) {
331 int32_t sg_idx = g.get_id() % wg_size_x;
332 int32_t sg_idy = g.get_id() / wg_size_x;
333 update_sg_tile_tdesc(args, sg_idx, sg_idy);
345 args.matA_base_desc, sg_idx);
347 args.matB_base_desc, sg_idy);
349 args.scale_base_desc, 0);
351 args.zero_pt_base_desc, 0);
357 nbarrier_b.
init_nbarrier(sg_idx + barrier_count_y + nbarrier_base,
360 int scale_prefetch_addr_i = 0;
361 int scale_load_addr_i = 0;
364 for (uint32_t i = 0; i < stages; i++) {
365 subgroup::tile_prefetch<cache_hint::cached, cache_hint::cached>(
366 matA_prefetch_payload);
367 subgroup::tile_prefetch<cache_hint::cached, cache_hint::cached>(
368 matB_prefetch_payload);
369 subgroup::tile_prefetch<cache_hint::cached, cache_hint::cached>(
370 scale_prefetch_payload);
371 subgroup::tile_prefetch<cache_hint::cached, cache_hint::cached>(
372 zero_pt_prefetch_payload);
373 scale_prefetch_addr_i++;
374 matA_prefetch_payload.template update_tdesc<update_dir_a>(
375 matA_t::tile_size_x);
376 matB_prefetch_payload.template update_tdesc<update_dir_b>(
377 matB_t::tile_size_y);
378 if ((scale_prefetch_addr_i % scale_addr_update_freq) == 0) {
379 scale_prefetch_payload
380 .template update_tdesc<tdesc_update_dir::y_dir>(
381 scale_t::tile_size_y);
382 zero_pt_prefetch_payload
383 .template update_tdesc<tdesc_update_dir::y_dir>(
384 zero_pt_t::tile_size_y);
388 for (uint32_t i = 0; i < args.inner_loop_count; i++) {
389 if constexpr (enable_periodic_sync) {
390 if ((i % sync_freq) == 0) {
391 if constexpr (wg_size_x > 1) { nbarrier_a.
arrive(); }
392 if constexpr (wg_size_y > 1) { nbarrier_b.
arrive(); }
395 subgroup::tile_load<cache_hint::cached, cache_hint::cached>(
397 subgroup::tile_load<cache_hint::cached, cache_hint::cached>(
399 subgroup::tile_load<cache_hint::cached, cache_hint::cached>(
400 scale, scale_payload);
401 subgroup::tile_load<cache_hint::cached, cache_hint::cached>(
402 zero_pt, zero_pt_payload);
405 if constexpr (stages != 0) {
406 subgroup::tile_prefetch<cache_hint::cached, cache_hint::cached>(
407 matA_prefetch_payload);
408 subgroup::tile_prefetch<cache_hint::cached, cache_hint::cached>(
409 matB_prefetch_payload);
410 subgroup::tile_prefetch<cache_hint::cached, cache_hint::cached>(
411 scale_prefetch_payload);
412 subgroup::tile_prefetch<cache_hint::cached, cache_hint::cached>(
413 zero_pt_prefetch_payload);
414 scale_prefetch_addr_i++;
417 matA_payload.template update_tdesc<update_dir_a>(
418 matA_t::tile_size_x);
419 matB_payload.template update_tdesc<update_dir_b>(
420 matB_t::tile_size_y);
421 if ((scale_load_addr_i % scale_addr_update_freq) == 0) {
422 scale_payload.template update_tdesc<tdesc_update_dir::y_dir>(
423 scale_t::tile_size_y);
424 zero_pt_payload.template update_tdesc<tdesc_update_dir::y_dir>(
425 zero_pt_t::tile_size_y);
427 if constexpr (stages != 0) {
428 matA_prefetch_payload.template update_tdesc<update_dir_a>(
429 matA_t::tile_size_x);
430 matB_prefetch_payload.template update_tdesc<update_dir_b>(
431 matB_t::tile_size_y);
432 if ((scale_prefetch_addr_i % scale_addr_update_freq) == 0) {
433 scale_prefetch_payload
434 .template update_tdesc<tdesc_update_dir::y_dir>(
435 scale_t::tile_size_y);
436 zero_pt_prefetch_payload
437 .template update_tdesc<tdesc_update_dir::y_dir>(
438 zero_pt_t::tile_size_y);
446 dequantize(matB_acc, matB, scale, zero_pt);
448 tile_mma::mma(matAcc, matAcc, matB_acc, matA_acc);
450 if constexpr (enable_periodic_sync) {
451 if ((i % sync_freq) == 0) {
452 if constexpr (wg_size_x > 1) { nbarrier_a.
wait(); }
453 if constexpr (wg_size_y > 1) { nbarrier_b.
wait(); }
461 inline void dequantize(matB_acc_t &matB_acc, matB_t &matB, scale_t &scale,
462 zero_pt_t &zero_pt) {
464 constexpr uint32_t num_block_x = tile_size_x_b / block_size_x_b;
465 constexpr uint32_t num_block_y = tile_size_y_b / block_size_y_b;
466 constexpr uint32_t vnni_rows =
sizeof(uint32_t) /
sizeof(dtype_mma_b);
467 constexpr uint32_t block_b_y_per_scale = dequant_s / block_size_y_b;
469 for (uint32_t i = 0; i < num_block_y; ++i) {
471 for (uint32_t j = 0; j < num_block_x; ++j) {
472 int block_id = (i * num_block_x + j);
473 auto matB_blk = matB.reg.xetla_select<matB_t::block_elems, 1>(
474 block_id * matB_t::block_elems)
475 .xetla_format<uint8_t>();
477 = (i / block_b_y_per_scale * num_block_x + j);
479 = scale.reg.xetla_select<scale_t::block_size_x, 1>(
480 scale_block_id * scale_t::block_size_x);
483 .xetla_select<zero_pt_t::block_size_x, 1>(
485 * zero_pt_t::block_size_x)
486 .xetla_format<uint8_t>();
489 = matB_acc.reg.xetla_select<matB_acc_t::block_elems, 1>(
490 block_id * matB_acc_t::block_elems);
494 zero_pt_sub.xetla_select<block_size_x_b / 2, 2>(0)
495 = zero_pt_vec & 0x0f;
496 zero_pt_sub.xetla_select<block_size_x_b / 2, 2>(1)
502 for (uint32_t row = 0; row < block_size_y_b; row++) {
504 .xetla_select<block_size_x_b, 1>(
505 row * block_size_x_b)
506 .xetla_format<int8_t>()
507 = zero_pt_sub.xetla_format<int8_t>() + int8_t(1);
511 cvt_blk.xetla_select<matB_t::block_elems, 2>(0)
513 cvt_blk.xetla_select<matB_t::block_elems, 2>(1) = matB_blk >> 4;
518 cvt_blk_i32 = (cvt_blk.xetla_format<int8_t>()
519 - zero_pt_blk.xetla_format<int8_t>());
523 temp_blk.xetla_select<matB_acc_t::block_elems, vnni_rows>(0)
527 for (uint32_t k = 0; k < block_size_y_b; k += vnni_rows) {
529 for (uint32_t row = 0; row < vnni_rows; row++) {
530 temp_blk.xetla_select<block_size_x_b, vnni_rows>(
531 row + block_size_x_b * k * vnni_rows)
532 = temp_blk.xetla_select<block_size_x_b,
534 (k + row) * block_size_x_b * vnni_rows);
538 xetla_vector<dtype_scale, block_size_x_b * vnni_rows> scale_blk;
540 for (uint32_t row = 0; row < vnni_rows; row++) {
541 scale_blk.xetla_select<block_size_x_b, vnni_rows>(row)
546 for (uint32_t k = 0; k < block_size_y_b; k += vnni_rows) {
547 dst_blk.xetla_select<block_size_x_b * vnni_rows, 1>(
549 = temp_blk.xetla_select<block_size_x_b * vnni_rows,
550 1>(k * block_size_x_b * vnni_rows)
558 arguments_t &args, int32_t sg_idx, int32_t sg_idy) {
559 int32_t tile_offset_n = sg_idx * sg_tile_n;
560 int32_t tile_offset_m = sg_idy * sg_tile_m;
562 args.matA_base_desc.update_coord_y(tile_offset_m);
563 args.matB_base_desc.update_coord_x(tile_offset_n / pack_ratio);
564 args.scale_base_desc.update_coord_x(tile_offset_n);
565 args.zero_pt_base_desc.update_coord_x(tile_offset_n / pack_ratio);
static __XETLA_API int get_matC_offset_y(work_group_t &g)
Gets the subgroup-level tile offset y.
Definition int4_dequantize_xmx_xe.hpp:292
typename mem_desc_b_t::dtype dtype_b
Definition int4_dequantize_xmx_xe.hpp:61
mem_desc_b_t_ mem_desc_b_t
Definition int4_dequantize_xmx_xe.hpp:45
static __XETLA_API int get_matC_offset_x(work_group_t &g)
Gets the subgroup-level tile offset x.
Definition int4_dequantize_xmx_xe.hpp:284
tile_shape_ tile_shape
Definition int4_dequantize_xmx_xe.hpp:46
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 int4_dequantize_xmx_xe.hpp:297
typename tile_shape::work_group_t work_group_t
Definition int4_dequantize_xmx_xe.hpp:57
typename compute_policy::dtype_zero_pt dtype_zero_pt
Definition int4_dequantize_xmx_xe.hpp:62
pre_processing_t_ pre_processing_t
Definition int4_dequantize_xmx_xe.hpp:47
mem_desc_a_t_ mem_desc_a_t
Definition int4_dequantize_xmx_xe.hpp:44
__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 int4_dequantize_xmx_xe.hpp:328
Gemm functor.
Definition api.hpp:52
typename std::remove_const< T >::type remove_const_t
Definition common.hpp:26
#define SW_BARRIER()
SW_BARRIER, insert software scheduling barrier, for better code control.
Definition common.hpp:227
#define __XETLA_API
Definition common.hpp:43
__ESIMD_NS::simd< native_type_t< Ty >, N > xetla_vector
wrapper for xetla_vector.
Definition base_types.hpp:149
#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< T::register_layout==reg_layout::tiled > vnni_reverse(T &mat_Acc)
Converts vnni_tiled layout format to tiled layout.
Definition op_function.hpp:196
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
Definition compute_policy.hpp:34
typename compute_attr::dtype_acc dtype_mma_acc
Definition compute_policy.hpp:41
typename compute_attr::dtype_b dtype_mma_b
Definition compute_policy.hpp:43
dtype_zero_pt_ dtype_zero_pt
Definition compute_policy.hpp:59
dtype_scale_ dtype_scale
Definition compute_policy.hpp:58
typename compute_attr::dtype_a dtype_mma_a
Definition compute_policy.hpp:42
Definition compute_policy.hpp:29
arguments_t(mem_desc_a_t matA_desc, mem_desc_b_t matB_desc, uint32_t loop_count, mem_desc_scale_t scale_desc, mem_desc_zero_pt_t zero_pt_desc)
Definition int4_dequantize_xmx_xe.hpp:245
mem_desc_scale_t scale_base_desc
Is the memory description of scale buffer. Scale size: (matrix_k/dequant_s)x(matrix_n)
Definition int4_dequantize_xmx_xe.hpp:238
void init(mem_desc_a_t matA_desc, mem_desc_b_t matB_desc, uint32_t loop_count, mem_desc_scale_t scale_desc, mem_desc_zero_pt_t zero_pt_desc)
Definition int4_dequantize_xmx_xe.hpp:270
arguments_t & operator=(const arguments_t &args)
Definition int4_dequantize_xmx_xe.hpp:262
arguments_t()=default
Default construct.
mem_desc_a_t matA_base_desc
Is the memory description of matA, including base, shape and coordinate.
Definition int4_dequantize_xmx_xe.hpp:232
mem_desc_b_t matB_base_desc
Is the memory description of matB, including base, shape and coordinate.
Definition int4_dequantize_xmx_xe.hpp:234
arguments_t(const arguments_t &args)
Definition int4_dequantize_xmx_xe.hpp:256
uint32_t inner_loop_count
Is the total inner loop count required to compute the entire K-dim.
Definition int4_dequantize_xmx_xe.hpp:236
mem_desc_zero_pt_t zero_pt_base_desc
Is the memory description of zero_pt buffer. Zero_pt size: (matrix_k/dequant_s)x(matrix_n/pack_ratio)
Definition int4_dequantize_xmx_xe.hpp:240
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