XeTLA v0.3.6
IntelĀ® Xe Templates for Linear Algebra - API Definition Document
 
Loading...
Searching...
No Matches
softmax_bwd_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/reduction/reduction.hpp"
23#include "group/softmax/api.hpp"
26
27namespace gpu::xetla::group {
28
29template <typename dtype_in_, typename dtype_acc_, typename tile_shape_>
30class softmax_t<softmax_policy_bwd<dtype_in_, dtype_acc_, gpu_arch::Xe>,
31 tile_shape_> {
32
33public:
34 using tile_shape = tile_shape_;
35 using dtype_in = dtype_in_;
36 using dtype_acc = dtype_acc_;
37 static constexpr gpu_arch arch_tag = gpu_arch::Xe;
38
39private:
40 using mem_desc_in_t
42 using shape_t = typename mem_desc_in_t::shape_t;
43 using coord_t = typename mem_desc_in_t::coord_t;
44 using base_t = typename mem_desc_in_t::base_t;
45 using work_group_t = typename tile_shape::work_group_t;
46 static constexpr uint32_t sg_tile_m = tile_shape::sg_tile_size_y;
47 static constexpr uint32_t sg_tile_n = tile_shape::sg_tile_size_x;
48 static constexpr uint32_t wg_size_x = tile_shape::wg_size_x;
49 static constexpr uint32_t wg_size_y = tile_shape::wg_size_y;
50
51 using wg_reduce_sum_t = group_reduce_t<dtype_acc, 1, sg_tile_m,
52 reduce_op::sum, wg_size_x, true, gpu_arch::Xe>;
53
54public:
55 struct arguments_t {
56 shape_t shape;
57 base_t base;
59 inline arguments_t() = default;
60 inline arguments_t(base_t base_, shape_t shape_, dtype_acc sqrt_dk_inv_)
61 : shape(shape_), base(base_), sqrt_dk_inv(sqrt_dk_inv_) {}
62 };
63 struct get_barrier_count {
64 static constexpr uint32_t count = (wg_size_x > 1) ? wg_size_y : 0;
65 };
66
67 struct get_slm_size {
68 static constexpr uint32_t size = (wg_size_x > 1)
69 ? wg_size_y * wg_size_x * sg_tile_m * sizeof(dtype_acc)
70 : 0;
71 };
72
73 template <typename matAcc_t>
74 __XETLA_API KERNEL_FUNC void operator()(work_group_t &g, matAcc_t &matAcc,
75 coord_t coord, const arguments_t &args, uint32_t slm_base = 0,
76 uint32_t nbarrier_base = 0) {
77 static_assert(std::is_same<typename matAcc_t::dtype, dtype_acc>::value,
78 "matAcc dtype should match with dtype_acc");
79
80 static constexpr uint32_t tile_size_x = matAcc_t::tile_size_x;
81 static constexpr uint32_t tile_size_y = matAcc_t::tile_size_y;
82 static constexpr uint32_t block_size_x = matAcc_t::block_size_x;
83 static constexpr uint32_t block_size_y = matAcc_t::block_size_y;
84 static_assert((sg_tile_m == tile_size_y) && (sg_tile_n == tile_size_x),
85 "tile size should match");
86 using mat_in_tile_desc_t = subgroup::tile_desc_t<tile_size_x,
87 tile_size_y, block_size_x, block_size_y, reg_layout::tiled>;
89 using mat_in_payload_t = subgroup::mem_payload_t<mem_desc_in_t,
90 mat_in_tile_desc_t,
91 subgroup::msg_type_v<mat_in_tile_desc_t, mem_desc_in_t::space>,
93
94 int32_t sg_idx = g.get_id() % wg_size_x;
95 int32_t sg_idy = g.get_id() / wg_size_x;
96 int32_t tile_offset_n = sg_idx * sg_tile_n;
97 int32_t tile_offset_m = sg_idy * sg_tile_m;
98 coord.x += tile_offset_n;
99 coord.y += tile_offset_m;
100 uint32_t nbarrier_id = nbarrier_base + sg_idy;
101 uint32_t slm_base_addr
102 = slm_base + sg_idy * wg_size_x * sg_tile_m * sizeof(dtype_acc);
103
104 mem_desc_in_t mem_desc_in(args.base, args.shape, coord);
105 mat_in_t mat_in;
106 mat_in_payload_t mat_in_payload(mem_desc_in);
107 subgroup::tile_load<cache_hint::cached, cache_hint::cached>(
108 mat_in, mat_in_payload);
109 matAcc_t mat_in_acc;
110 subgroup::elemwise_cvt(mat_in_acc, mat_in);
111 matAcc.reg = matAcc.reg * mat_in_acc.reg;
114 1>(matAcc);
115 wg_reduce_sum_t wg_reduce_sum(sg_idx, nbarrier_id, slm_base_addr);
116 xetla_vector<dtype_acc, sg_tile_m> group_sum = wg_reduce_sum(local_sum);
117 subgroup::tile_broadcast_op<subgroup::tile_minus, matAcc_t>(
118 matAcc, group_sum);
119 matAcc.reg = matAcc.reg * mat_in_acc.reg * args.sqrt_dk_inv;
120 }
121};
122
123} // namespace gpu::xetla::group
__XETLA_API KERNEL_FUNC void operator()(work_group_t &g, matAcc_t &matAcc, coord_t coord, const arguments_t &args, uint32_t slm_base=0, uint32_t nbarrier_base=0)
Definition softmax_bwd_xe.hpp:74
Definition api.hpp:27
#define __XETLA_API
Definition common.hpp:43
C++ API.
C++ API.
__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<(dim==1), xetla_vector< dtype_out, mat_t::tile_size_y > > tile_reduce(mat_t &src)
Definition reduction.hpp:33
gpu_arch
Definition common.hpp:73
This is the group reduction.
Definition reduction_api.hpp:36
Definition softmax_policy.hpp:31
arguments_t(base_t base_, shape_t shape_, dtype_acc sqrt_dk_inv_)
Definition softmax_bwd_xe.hpp:60
Definition dict.hpp:59
Is to illustrate the memory information.
Definition api.hpp:44
Is to illustrate the tile information about a sub matrix.
Definition api.hpp:64
Is a struct contains some register file.
Definition api.hpp:99