mirror of
https://github.com/ROCm/composable_kernel.git
synced 2026-05-01 20:21:23 +00:00
Ck tile/layernorm: implement naive reduce, opt performance (#1784)
* add no welford * enable output raw * raw of int8 * fix build * fix smoke test err * [ck_tile]layernorm: fix welford ok, set int8 and bf16 small N as default and others open by generate * [cktile]layernorm, fix err commit files and remove uselss * fix quant 8192 err & change norm_reduce class and file name --------- Co-authored-by: coderfeli <coderfeli@163.com> Co-authored-by: carlushuang <carlus.huang@amd.com>
This commit is contained in:
57
include/ck_tile/ops/norm_reduce/thread/thread_welford.hpp
Normal file
57
include/ck_tile/ops/norm_reduce/thread/thread_welford.hpp
Normal file
@@ -0,0 +1,57 @@
|
||||
// SPDX-License-Identifier: MIT
|
||||
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved.
|
||||
|
||||
#pragma once
|
||||
|
||||
#include "ck_tile/core.hpp"
|
||||
|
||||
namespace ck_tile {
|
||||
|
||||
template <typename T, bool kFastFDiv = false>
|
||||
CK_TILE_DEVICE void welford_update(T& mean, T& var, T x, int count, bool_constant<kFastFDiv> = {})
|
||||
{
|
||||
// TODO: check nan? maybe no
|
||||
T delta = x - mean;
|
||||
if(kFastFDiv && std::is_same_v<T, float>)
|
||||
{
|
||||
mean += delta * __builtin_amdgcn_rcpf(count);
|
||||
}
|
||||
else
|
||||
{
|
||||
mean += delta / count;
|
||||
}
|
||||
T delta2 = x - mean;
|
||||
var += delta * delta2;
|
||||
}
|
||||
|
||||
template <typename T, bool kFastFDiv = false>
|
||||
CK_TILE_DEVICE static void welford_merge(T& mean_a,
|
||||
T& var_a,
|
||||
int& count_a,
|
||||
T mean_b,
|
||||
T var_b,
|
||||
int count_b,
|
||||
bool_constant<kFastFDiv> = {})
|
||||
{
|
||||
int count = count_a + count_b;
|
||||
T count_ = type_convert<T>(count);
|
||||
T count_a_ = type_convert<T>(count_a);
|
||||
T count_b_ = type_convert<T>(count_b);
|
||||
T count_b_over_count;
|
||||
if(kFastFDiv && std::is_same_v<T, float>)
|
||||
{
|
||||
count_b_over_count =
|
||||
count == 0 ? type_convert<T>(0) : count_b_ * __builtin_amdgcn_rcpf(count_);
|
||||
}
|
||||
else
|
||||
{
|
||||
count_b_over_count = count == 0 ? type_convert<T>(0) : count_b_ / count_;
|
||||
}
|
||||
|
||||
T delta = mean_b - mean_a;
|
||||
mean_a += delta * count_b_over_count;
|
||||
var_a += var_b + delta * delta * count_a_ * count_b_over_count;
|
||||
count_a = count;
|
||||
}
|
||||
|
||||
} // namespace ck_tile
|
||||
Reference in New Issue
Block a user