add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp Source File

add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp Source File#

Composable Kernel: add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp Source File
add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp
Go to the documentation of this file.
1// SPDX-License-Identifier: MIT
2// Copyright (c) 2018-2024, Advanced Micro Devices, Inc. All rights reserved.
3
4#pragma once
5
6#include "ck_tile/core.hpp"
8#include <string>
9#include <type_traits>
10
11namespace ck_tile {
12
13template <typename Problem_, typename Policy_ = AddRmsnorm2dRdquantFwdPipelineDefaultPolicy>
15{
18
26
27 static constexpr bool kHasGamma = !std::is_same_v<GammaDataType, ck_tile::null_type>;
28 static constexpr bool kSaveX = Problem::kSaveX;
29
30 static constexpr bool kNeedCrossWarpSync = Problem::kNeedCrossWarpSync;
31 static constexpr bool kPadM = false; // TODO - BlockAddRmsnorm2dRdquantFwdProblem::kPadM
32 static constexpr bool kPadN = Problem::kPadN;
33 static constexpr bool UseMax3 = true; // TODO - Move to trait
34
35 static constexpr const char* name = []() {
36 if constexpr(kNeedCrossWarpSync)
37 return "bpr_op"; // block per row
38 else
39 return "wpr_op"; // warp per row
40 }();
41
43 {
44 return Policy::template GetSmemSize<Problem>();
45 }
46
47 template <typename AWindow,
48 typename BWindow,
49 typename GammaWindow,
50 typename XWindow,
51 typename YScaleWindow,
52 typename QYWindow>
53 CK_TILE_DEVICE auto operator()(const AWindow& a_window_,
54 const BWindow& b_window_,
55 const GammaWindow& gamma_window_,
56 XWindow& x_window,
57 YScaleWindow& yscale_window,
58 QYWindow& qy_window,
59 ComputeDataType epsilon,
60 ck_tile::index_t row_size,
61 void* smem) const
62 {
63 const auto a_window =
64 make_tile_window(a_window_, Policy::template MakeABXBlockTileDistribution<Problem>());
65 const auto b_window =
66 make_tile_window(b_window_, Policy::template MakeABXBlockTileDistribution<Problem>());
67 const auto gamma_window = make_tile_window(
68 gamma_window_, Policy::template MakeGammaBlockTileDistribution<Problem>());
69
70 auto reduce_square_sum_func = ReduceOp::SquareAdd{};
71 auto reduce_sum_func = ReduceOp::Add{};
72 auto reduce_absmax_func = ReduceOp::AbsMax{};
73 auto reduce_absmax3_func = [](auto acc_, auto v_0_, auto v_1_) {
74 float rtn;
75 asm volatile("v_max3_f32 %0, %1, abs(%2), abs(%3)"
76 : "=v"(rtn)
77 : "v"(acc_), "v"(v_0_), "v"(v_1_));
78 return rtn;
79 };
80 auto reduce_max_func = ReduceOp::Max{};
81 auto block_reduce2d = Policy::template GetBlockReduce2d<Problem>();
82 auto block_reduce2d_sync = Policy::template GetBlockReduce2dSync<Problem>();
83 auto block_reduce2d_cross_warp_sync =
84 Policy::template GetBlockReduce2dCrossWarpSync<Problem>();
85
86 const auto a = load_tile(a_window);
87 const auto b = load_tile(b_window);
88 const auto gamma = load_tile(gamma_window);
89
90 auto x = tile_elementwise_in(
91 [&](const auto& a_, const auto& b_) {
93 },
94 a,
95 b);
96
97 if constexpr(kSaveX)
98 store_tile(x_window, cast_tile<XDataType>(x));
99
100 // compute mean square, each-thread->cross-lane->cross-warp
101 auto square_sum = block_reduce2d(
102 x, reduce_square_sum_func.GetIdentityValue<ComputeDataType>(), reduce_square_sum_func);
103 block_reduce2d_sync(square_sum, reduce_sum_func);
104 block_reduce2d_cross_warp_sync(square_sum, smem, reduce_sum_func);
105
106 auto inv_rms = tile_elementwise_in(
107 [&](const auto& v_) {
108 return type_convert<ComputeDataType>(1.0f) / (sqrt(v_ / row_size + epsilon));
109 },
110 square_sum);
111
112 // rmsnorm computation
113 auto y = make_static_distributed_tensor<ComputeDataType>(x.get_tile_distribution());
114 sweep_tile(y, [&, inv_rms_ = inv_rms](auto idx) {
115 constexpr auto i_idx = make_tuple(idx[number<0>{}]);
116 constexpr auto j_idx = make_tuple(idx[number<1>{}]);
117
118 const auto gamma_ = type_convert<ComputeDataType>(gamma[j_idx]);
119
120 const auto x_ = type_convert<ComputeDataType>(x[idx]);
121 auto y_ = x_ * inv_rms_[i_idx] * gamma_;
122
124 });
125
126 // compute absmax, each-thread->cross-lane->cross-warp
127 auto absmax = [&]() {
128 constexpr auto x_size_per_row =
129 x.get_tile_distribution().get_ys_to_d_descriptor().get_lengths().at(number<1>{});
130 if constexpr(UseMax3 && std::is_same_v<ComputeDataType, float> &&
131 x_size_per_row % 2 == 0)
132 {
133 return block_reduce2d(y,
134 reduce_absmax_func.GetIdentityValue<ComputeDataType>(),
135 reduce_absmax3_func,
137 }
138 else
139 {
140 return block_reduce2d(
141 y, reduce_absmax_func.GetIdentityValue<ComputeDataType>(), reduce_absmax_func);
142 }
143 }();
144 block_reduce2d_sync(absmax, reduce_max_func);
145 block_reduce2d_cross_warp_sync(absmax, smem, reduce_max_func);
146
147 // ex: yscale = absmax / 127 if int8
148 auto yscale = tile_elementwise_in(
149 [&](const auto& v_) {
151 },
152 absmax);
153 store_tile(yscale_window, cast_tile<YScaleDataType>(yscale));
154
155 // quantize y to qy
156 auto qy = make_static_distributed_tensor<QYDataType>(y.get_tile_distribution());
157 sweep_tile(qy, [&, yscale_ = yscale](auto idx) {
158 constexpr auto i_idx = make_tuple(idx[number<0>{}]);
159 auto qy_ = y[idx] / yscale_[i_idx];
161 });
162 store_tile(qy_window, qy);
163 }
164};
165} // namespace ck_tile
#define CK_TILE_DEVICE
Definition config.hpp:41
#define CK_TILE_HOST_DEVICE
Definition config.hpp:42
Definition tile/core/algorithm/cluster_descriptor.hpp:13
remove_cv_t< std::remove_reference_t< T > > remove_cvref_t
Definition type_traits.hpp:21
CK_TILE_DEVICE auto tile_elementwise_in(const InElementFunc &in_element_func, const InTensor &... in_dstr_tensors)
Definition tile_elementwise.hpp:40
CK_TILE_HOST_DEVICE constexpr void sweep_tile(const F &f, UnpacksPerXDim={})
Definition sweep_tile.hpp:231
CK_TILE_DEVICE bfloat16_t sqrt(bfloat16_t x)
Definition bfloat16.hpp:413
CK_TILE_HOST_DEVICE constexpr auto make_static_distributed_tensor(const StaticTileDistribution &)
Definition static_distributed_tensor.hpp:142
constant< v > number
Definition tile/core/numeric/integral_constant.hpp:37
CK_TILE_DEVICE constexpr auto make_tile_window(null_tensor_view, const WindowLengths &window_lengths, const multi_index< WindowLengths::size()> &, Ts &&...)
Definition null_tile_window.hpp:75
CK_TILE_DEVICE auto cast_tile(const SrcTensor &src_tensor)
Definition tile_elementwise.hpp:327
CK_TILE_DEVICE void store_tile(tile_window_with_static_lengths< BottomTensorView_, WindowLengths_ > &tile_window_tmp, const static_distributed_tensor< DataType_, TileDistribution_ > &dstr_tensor)
Definition store_tile.hpp:23
int32_t index_t
Definition integer.hpp:9
CK_TILE_HOST_DEVICE constexpr Y type_convert(X x)
Definition tile/core/numeric/type_convert.hpp:29
CK_TILE_DEVICE auto load_tile(const TileWindow_ &tile_window, number< i_access >={}, bool_constant< oob_conditional_check >={})
Definition load_tile.hpp:22
CK_TILE_HOST_DEVICE constexpr auto make_tuple(Xs &&... xs)
Definition tile/core/container/tuple.hpp:360
const GenericPointer< typename T::ValueType > T2 T::AllocatorType & a
Definition pointer.h:1517
Definition add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp:15
ck_tile::remove_cvref_t< Policy_ > Policy
Definition add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp:17
static CK_TILE_HOST_DEVICE constexpr index_t GetSmemSize()
Definition add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp:42
ck_tile::remove_cvref_t< typename Problem::ADataType > ADataType
Definition add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp:19
static constexpr bool kHasGamma
Definition add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp:27
ck_tile::remove_cvref_t< typename Problem::GammaDataType > GammaDataType
Definition add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp:21
static constexpr bool kPadM
Definition add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp:31
ck_tile::remove_cvref_t< typename Problem::ComputeDataType > ComputeDataType
Definition add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp:22
ck_tile::remove_cvref_t< typename Problem::QYDataType > QYDataType
Definition add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp:25
ck_tile::remove_cvref_t< typename Problem::XDataType > XDataType
Definition add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp:23
static constexpr const char * name
Definition add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp:35
static constexpr bool kNeedCrossWarpSync
Definition add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp:30
static constexpr bool UseMax3
Definition add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp:33
static constexpr bool kSaveX
Definition add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp:28
ck_tile::remove_cvref_t< typename Problem::YScaleDataType > YScaleDataType
Definition add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp:24
static constexpr bool kPadN
Definition add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp:32
CK_TILE_DEVICE auto operator()(const AWindow &a_window_, const BWindow &b_window_, const GammaWindow &gamma_window_, XWindow &x_window, YScaleWindow &yscale_window, QYWindow &qy_window, ComputeDataType epsilon, ck_tile::index_t row_size, void *smem) const
Definition add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp:53
ck_tile::remove_cvref_t< typename Problem::BDataType > BDataType
Definition add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp:20
ck_tile::remove_cvref_t< Problem_ > Problem
Definition add_rmsnorm2d_rdquant_fwd_pipeline_one_pass.hpp:16
Definition reduce_operator.hpp:101
Definition reduce_operator.hpp:14
Definition reduce_operator.hpp:65
Definition reduce_operator.hpp:40
static CK_TILE_HOST_DEVICE constexpr T max()
Definition tile/core/numeric/numeric.hpp:26
Definition unary_element_function.hpp:56
Definition tile/core/container/sequence.hpp:49