mirror of
https://github.com/ROCm/composable_kernel.git
synced 2026-05-14 02:02:46 +00:00
Batchnorm-forward and Batchnorm-infer Implemented using generic kernels (#320)
* Implement multiple-reduction in one kernel (kernels, device ops, examples)
* Add generic elementwise kernel and device interface
* Add generator for normal-distributed data initialization
* Add host refer implementation of batchnorm-forward and batchnorm-infer
* Add examples for implementing batchnorm-forward and batchnorm-infer using generic kernels
* Remove un-needed including in batchnorm example
* Renaming generic_elementwise to elementiwise in kernel and device classes/functions
* Change in gemm_layernorm examples to use DeviceElementwise instead of Device5AryElementwise
* Change in exampe 19_binary_elementwise to use DeviceElementwise instead of DeviceBinaryElementwise
* Change in device_cgemm_4gemm_xdl_cshuffle.hpp to use kernel_elementwise instead of kernel_binary_elementwise
* Add DeviceElementwiseBase and use it in device_normalize_instance.cpp
* Removing and renaming files
* Update to synchronize gemm_layernorm client example to the generic element-wise device op API
* Update to synchronize with the latest headers directory and HostTensorDescriptor interface renaming
* Merge two static member functions in device_elementwise.hpp
* Remove unary_elementwise_1d kernel and device
[ROCm/composable_kernel commit: 53ea4713af]
This commit is contained in:
@@ -6,7 +6,7 @@
|
||||
|
||||
#include "ck/ck.hpp"
|
||||
#include "ck/tensor_operation/gpu/element/binary_element_wise_operation.hpp"
|
||||
#include "ck/tensor_operation/gpu/device/device_binary_elementwise.hpp"
|
||||
#include "ck/tensor_operation/gpu/device/device_elementwise.hpp"
|
||||
|
||||
#include "ck/library/utility/check_err.hpp"
|
||||
#include "ck/library/utility/device_memory.hpp"
|
||||
@@ -16,28 +16,23 @@
|
||||
using F16 = ck::half_t;
|
||||
using F32 = float;
|
||||
|
||||
using ABDataType = F16;
|
||||
using CDataType = F16;
|
||||
using EltwiseComputeDataType = F32;
|
||||
using ABDataType = F16;
|
||||
using CDataType = F16;
|
||||
|
||||
using Add = ck::tensor_operation::element_wise::Add;
|
||||
|
||||
using DeviceElementwiseAddInstance =
|
||||
ck::tensor_operation::device::DeviceBinaryElementwise<ABDataType,
|
||||
ABDataType,
|
||||
CDataType,
|
||||
EltwiseComputeDataType,
|
||||
Add,
|
||||
2,
|
||||
8,
|
||||
8,
|
||||
8,
|
||||
8>;
|
||||
ck::tensor_operation::device::DeviceElementwise<ck::Tuple<ABDataType, ABDataType>,
|
||||
ck::Tuple<CDataType>,
|
||||
Add,
|
||||
2,
|
||||
8,
|
||||
ck::Sequence<8, 8>,
|
||||
ck::Sequence<8>>;
|
||||
|
||||
template <typename HostTensorA,
|
||||
typename HostTensorB,
|
||||
typename HostTensorC,
|
||||
typename ComputeDataType,
|
||||
typename Functor,
|
||||
int broadcastDim>
|
||||
void host_broadcast2D(
|
||||
@@ -49,19 +44,19 @@ void host_broadcast2D(
|
||||
{
|
||||
for(int n = 0; n < N; ++n)
|
||||
{
|
||||
ComputeDataType Amn = ck::type_convert<ComputeDataType>(A(m, n));
|
||||
ComputeDataType Cmn = 0;
|
||||
auto Amn = A(m, n);
|
||||
ctype Cmn = 0;
|
||||
if constexpr(broadcastDim == 0)
|
||||
{
|
||||
ComputeDataType Bn = ck::type_convert<ComputeDataType>(B(n));
|
||||
auto Bn = B(n);
|
||||
functor(Cmn, Amn, Bn);
|
||||
}
|
||||
else
|
||||
{
|
||||
ComputeDataType Bm = ck::type_convert<ComputeDataType>(B(m));
|
||||
auto Bm = B(m);
|
||||
functor(Cmn, Amn, Bm);
|
||||
}
|
||||
C(m, n) = ck::type_convert<ctype>(Cmn);
|
||||
C(m, n) = Cmn;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -103,18 +98,19 @@ int main()
|
||||
b_n_device_buf.GetDeviceBuffer()};
|
||||
std::array<void*, 1> output = {c_m_n_device_buf.GetDeviceBuffer()};
|
||||
|
||||
std::vector<ck::index_t> a_strides = {Stride, 1};
|
||||
std::vector<ck::index_t> b_strides = {0, 1};
|
||||
std::vector<ck::index_t> c_strides = {Stride, 1};
|
||||
std::array<ck::index_t, 2> abc_lengths = {M, N};
|
||||
std::array<ck::index_t, 2> a_strides = {Stride, 1};
|
||||
std::array<ck::index_t, 2> b_strides = {0, 1};
|
||||
std::array<ck::index_t, 2> c_strides = {Stride, 1};
|
||||
|
||||
auto broadcastAdd = DeviceElementwiseAddInstance{};
|
||||
auto argument = broadcastAdd.MakeArgumentPointer(
|
||||
input, output, {M, N}, {a_strides, b_strides}, {c_strides}, Add{});
|
||||
abc_lengths, {a_strides, b_strides}, {c_strides}, input, output, Add{});
|
||||
|
||||
if(!broadcastAdd.IsSupportedArgument(argument.get()))
|
||||
{
|
||||
throw std::runtime_error("The runtime parameters seems not supported by the "
|
||||
"DeviceBinaryElementwise instance, exiting!");
|
||||
throw std::runtime_error(
|
||||
"The runtime parameters seems not supported by the device instance, exiting!");
|
||||
};
|
||||
|
||||
auto broadcastAdd_invoker_ptr = broadcastAdd.MakeInvokerPointer();
|
||||
@@ -129,12 +125,8 @@ int main()
|
||||
c_m_n_device_buf.FromDevice(c_m_n.mData.data());
|
||||
Tensor<CDataType> host_c_m_n(f_host_tensor_descriptor2d(M, N, Stride));
|
||||
|
||||
host_broadcast2D<Tensor<ABDataType>,
|
||||
Tensor<ABDataType>,
|
||||
Tensor<CDataType>,
|
||||
EltwiseComputeDataType,
|
||||
Add,
|
||||
0>(host_c_m_n, a_m_n, b_n, M, N, Add{});
|
||||
host_broadcast2D<Tensor<ABDataType>, Tensor<ABDataType>, Tensor<CDataType>, Add, 0>(
|
||||
host_c_m_n, a_m_n, b_n, M, N, Add{});
|
||||
|
||||
pass &= ck::utils::check_err(
|
||||
c_m_n.mData, host_c_m_n.mData, "Error: Incorrect results c", 1e-3, 1e-3);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include "ck/ck.hpp"
|
||||
#include "ck/tensor_operation/gpu/element/binary_element_wise_operation.hpp"
|
||||
#include "ck/tensor_operation/gpu/device/device_binary_elementwise.hpp"
|
||||
#include "ck/tensor_operation/gpu/device/device_elementwise.hpp"
|
||||
|
||||
#include "ck/library/utility/check_err.hpp"
|
||||
#include "ck/library/utility/device_memory.hpp"
|
||||
@@ -16,29 +16,21 @@
|
||||
using F16 = ck::half_t;
|
||||
using F32 = float;
|
||||
|
||||
using ABDataType = F16;
|
||||
using CDataType = F16;
|
||||
using EltwiseComputeDataType = F32;
|
||||
using ABDataType = F16;
|
||||
using CDataType = F16;
|
||||
|
||||
using Add = ck::tensor_operation::element_wise::Add;
|
||||
|
||||
using DeviceElementwiseAddInstance =
|
||||
ck::tensor_operation::device::DeviceBinaryElementwise<ABDataType,
|
||||
ABDataType,
|
||||
CDataType,
|
||||
EltwiseComputeDataType,
|
||||
Add,
|
||||
3,
|
||||
8,
|
||||
1,
|
||||
8,
|
||||
8>;
|
||||
ck::tensor_operation::device::DeviceElementwise<ck::Tuple<ABDataType, ABDataType>,
|
||||
ck::Tuple<CDataType>,
|
||||
Add,
|
||||
3,
|
||||
8,
|
||||
ck::Sequence<1, 8>,
|
||||
ck::Sequence<8>>;
|
||||
|
||||
template <typename HostTensorA,
|
||||
typename HostTensorB,
|
||||
typename HostTensorC,
|
||||
typename ComputeDataType,
|
||||
typename Functor>
|
||||
template <typename HostTensorA, typename HostTensorB, typename HostTensorC, typename Functor>
|
||||
void host_broadcast3D_am_bmnk(HostTensorC& C,
|
||||
const HostTensorA& A,
|
||||
const HostTensorB& B,
|
||||
@@ -51,11 +43,11 @@ void host_broadcast3D_am_bmnk(HostTensorC& C,
|
||||
for(std::size_t n = 0; n < shape[1]; ++n)
|
||||
for(std::size_t k = 0; k < shape[2]; ++k)
|
||||
{
|
||||
ComputeDataType a_val = ck::type_convert<ComputeDataType>(A(m));
|
||||
ComputeDataType b_val = ck::type_convert<ComputeDataType>(B(m, n, k));
|
||||
ComputeDataType c_val = 0;
|
||||
auto a_val = A(m);
|
||||
auto b_val = B(m, n, k);
|
||||
ctype c_val = 0;
|
||||
functor(c_val, a_val, b_val);
|
||||
C(m, n, k) = ck::type_convert<ctype>(c_val);
|
||||
C(m, n, k) = c_val;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,25 +77,25 @@ int main()
|
||||
b_m_n_k_device_buf.GetDeviceBuffer()};
|
||||
std::array<void*, 1> output = {c_m_n_k_device_buf.GetDeviceBuffer()};
|
||||
|
||||
std::vector<ck::index_t> a_strides = {1, 0, 0};
|
||||
std::vector<ck::index_t> b_strides{b_m_n_k.mDesc.GetStrides().begin(),
|
||||
b_m_n_k.mDesc.GetStrides().end()};
|
||||
std::vector<ck::index_t> c_strides{c_m_n_k.mDesc.GetStrides().begin(),
|
||||
c_m_n_k.mDesc.GetStrides().end()};
|
||||
std::array<ck::index_t, 3> abc_lengths;
|
||||
std::array<ck::index_t, 3> a_strides = {1, 0, 0};
|
||||
std::array<ck::index_t, 3> b_strides;
|
||||
std::array<ck::index_t, 3> c_strides;
|
||||
|
||||
std::copy(mnk.begin(), mnk.end(), abc_lengths.begin());
|
||||
std::copy(
|
||||
b_m_n_k.mDesc.GetStrides().begin(), b_m_n_k.mDesc.GetStrides().end(), b_strides.begin());
|
||||
std::copy(
|
||||
c_m_n_k.mDesc.GetStrides().begin(), c_m_n_k.mDesc.GetStrides().end(), c_strides.begin());
|
||||
|
||||
auto broadcastAdd = DeviceElementwiseAddInstance{};
|
||||
auto argument =
|
||||
broadcastAdd.MakeArgumentPointer(input,
|
||||
output,
|
||||
std::vector<ck::index_t>{mnk.begin(), mnk.end()},
|
||||
{a_strides, b_strides},
|
||||
{c_strides},
|
||||
Add{});
|
||||
auto argument = broadcastAdd.MakeArgumentPointer(
|
||||
abc_lengths, {a_strides, b_strides}, {c_strides}, input, output, Add{});
|
||||
|
||||
if(!broadcastAdd.IsSupportedArgument(argument.get()))
|
||||
{
|
||||
throw std::runtime_error("The runtime parameters seems not supported by the "
|
||||
"DeviceBinaryElementwise instance, exiting!");
|
||||
throw std::runtime_error(
|
||||
"The runtime parameters seems not supported by the device instance, exiting!");
|
||||
};
|
||||
|
||||
auto broadcastAdd_invoker_ptr = broadcastAdd.MakeInvokerPointer();
|
||||
@@ -118,11 +110,8 @@ int main()
|
||||
c_m_n_k_device_buf.FromDevice(c_m_n_k.mData.data());
|
||||
Tensor<CDataType> host_c_m_n_k(mnk);
|
||||
|
||||
host_broadcast3D_am_bmnk<Tensor<ABDataType>,
|
||||
Tensor<ABDataType>,
|
||||
Tensor<CDataType>,
|
||||
EltwiseComputeDataType,
|
||||
Add>(host_c_m_n_k, a_m, b_m_n_k, mnk, Add{});
|
||||
host_broadcast3D_am_bmnk<Tensor<ABDataType>, Tensor<ABDataType>, Tensor<CDataType>, Add>(
|
||||
host_c_m_n_k, a_m, b_m_n_k, mnk, Add{});
|
||||
|
||||
pass &= ck::utils::check_err(
|
||||
c_m_n_k.mData, host_c_m_n_k.mData, "Error: Incorrect results c", 1e-3, 1e-3);
|
||||
|
||||
@@ -5,7 +5,7 @@
|
||||
#include <cstdlib>
|
||||
|
||||
#include "ck/ck.hpp"
|
||||
#include "ck/tensor_operation/gpu/device/device_binary_elementwise.hpp"
|
||||
#include "ck/tensor_operation/gpu/device/device_elementwise.hpp"
|
||||
#include "ck/tensor_operation/gpu/element/binary_element_wise_operation.hpp"
|
||||
#include "ck/library/utility/check_err.hpp"
|
||||
#include "ck/library/utility/device_memory.hpp"
|
||||
@@ -15,29 +15,21 @@
|
||||
using F16 = ck::half_t;
|
||||
using F32 = float;
|
||||
|
||||
using ABDataType = F16;
|
||||
using CDataType = F16;
|
||||
using EltwiseComputeDataType = F32;
|
||||
using ABDataType = F16;
|
||||
using CDataType = F16;
|
||||
|
||||
using Add = ck::tensor_operation::element_wise::Add;
|
||||
|
||||
using DeviceElementwiseAddInstance =
|
||||
ck::tensor_operation::device::DeviceBinaryElementwise<ABDataType,
|
||||
ABDataType,
|
||||
CDataType,
|
||||
EltwiseComputeDataType,
|
||||
Add,
|
||||
1,
|
||||
8,
|
||||
8,
|
||||
8,
|
||||
8>;
|
||||
ck::tensor_operation::device::DeviceElementwise<ck::Tuple<ABDataType, ABDataType>,
|
||||
ck::Tuple<CDataType>,
|
||||
Add,
|
||||
1,
|
||||
8,
|
||||
ck::Sequence<8, 8>,
|
||||
ck::Sequence<8>>;
|
||||
|
||||
template <typename HostTensorA,
|
||||
typename HostTensorB,
|
||||
typename HostTensorC,
|
||||
typename ComputeDataType,
|
||||
typename Functor>
|
||||
template <typename HostTensorA, typename HostTensorB, typename HostTensorC, typename Functor>
|
||||
void host_elementwise1D(
|
||||
HostTensorC& C, const HostTensorA& A, const HostTensorB& B, int M, Functor functor)
|
||||
{
|
||||
@@ -45,11 +37,11 @@ void host_elementwise1D(
|
||||
|
||||
for(int m = 0; m < M; ++m)
|
||||
{
|
||||
ComputeDataType Am = ck::type_convert<ComputeDataType>(A(m));
|
||||
ComputeDataType Bm = ck::type_convert<ComputeDataType>(B(m));
|
||||
ComputeDataType Cm = 0;
|
||||
auto Am = A(m);
|
||||
auto Bm = B(m);
|
||||
ctype Cm = 0;
|
||||
functor(Cm, Am, Bm);
|
||||
C(m) = ck::type_convert<ctype>(Cm);
|
||||
C(m) = Cm;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -83,18 +75,19 @@ int main()
|
||||
b_m_device_buf.GetDeviceBuffer()};
|
||||
std::array<void*, 1> output = {c_m_device_buf.GetDeviceBuffer()};
|
||||
|
||||
std::vector<ck::index_t> a_strides = {1};
|
||||
std::vector<ck::index_t> b_strides = {1};
|
||||
std::vector<ck::index_t> c_strides = {1};
|
||||
std::array<ck::index_t, 1> abc_lengths = {M};
|
||||
std::array<ck::index_t, 1> a_strides = {1};
|
||||
std::array<ck::index_t, 1> b_strides = {1};
|
||||
std::array<ck::index_t, 1> c_strides = {1};
|
||||
|
||||
auto broadcastAdd = DeviceElementwiseAddInstance{};
|
||||
auto argument = broadcastAdd.MakeArgumentPointer(
|
||||
input, output, {M}, {{a_strides}, b_strides}, {c_strides}, Add{});
|
||||
abc_lengths, {a_strides, b_strides}, {c_strides}, input, output, Add{});
|
||||
|
||||
if(!broadcastAdd.IsSupportedArgument(argument.get()))
|
||||
{
|
||||
throw std::runtime_error("The runtime parameters seems not supported by the "
|
||||
"DeviceBinaryElementwise instance, exiting!");
|
||||
throw std::runtime_error(
|
||||
"The runtime parameters seems not supported by the device instance, exiting!");
|
||||
};
|
||||
|
||||
auto broadcastAdd_invoker_ptr = broadcastAdd.MakeInvokerPointer();
|
||||
@@ -109,11 +102,8 @@ int main()
|
||||
c_m_device_buf.FromDevice(c_m.mData.data());
|
||||
Tensor<CDataType> host_c_m(f_host_tensor_descriptor1d(M, 1));
|
||||
|
||||
host_elementwise1D<Tensor<ABDataType>,
|
||||
Tensor<ABDataType>,
|
||||
Tensor<CDataType>,
|
||||
EltwiseComputeDataType,
|
||||
Add>(host_c_m, a_m, b_m, M, Add{});
|
||||
host_elementwise1D<Tensor<ABDataType>, Tensor<ABDataType>, Tensor<CDataType>, Add>(
|
||||
host_c_m, a_m, b_m, M, Add{});
|
||||
|
||||
pass &= ck::utils::check_err(
|
||||
c_m.mData, host_c_m.mData, "Error: Incorrect results c", 1e-3, 1e-3);
|
||||
|
||||
@@ -6,7 +6,7 @@
|
||||
|
||||
#include "ck/ck.hpp"
|
||||
#include "ck/tensor_operation/gpu/element/binary_element_wise_operation.hpp"
|
||||
#include "ck/tensor_operation/gpu/device/device_binary_elementwise.hpp"
|
||||
#include "ck/tensor_operation/gpu/device/device_elementwise.hpp"
|
||||
|
||||
#include "ck/library/utility/check_err.hpp"
|
||||
#include "ck/library/utility/device_memory.hpp"
|
||||
@@ -16,29 +16,21 @@
|
||||
using F16 = ck::half_t;
|
||||
using F32 = float;
|
||||
|
||||
using ABDataType = F16;
|
||||
using CDataType = F16;
|
||||
using EltwiseComputeDataType = F32;
|
||||
using ABDataType = F16;
|
||||
using CDataType = F16;
|
||||
|
||||
using Add = ck::tensor_operation::element_wise::Add;
|
||||
|
||||
using DeviceElementwiseAddInstance =
|
||||
ck::tensor_operation::device::DeviceBinaryElementwise<ABDataType,
|
||||
ABDataType,
|
||||
CDataType,
|
||||
EltwiseComputeDataType,
|
||||
Add,
|
||||
4,
|
||||
8,
|
||||
8,
|
||||
8,
|
||||
8>;
|
||||
ck::tensor_operation::device::DeviceElementwise<ck::Tuple<ABDataType, ABDataType>,
|
||||
ck::Tuple<CDataType>,
|
||||
Add,
|
||||
4,
|
||||
8,
|
||||
ck::Sequence<8, 8>,
|
||||
ck::Sequence<8>>;
|
||||
|
||||
template <typename HostTensorA,
|
||||
typename HostTensorB,
|
||||
typename HostTensorC,
|
||||
typename ComputeDataType,
|
||||
typename Functor>
|
||||
template <typename HostTensorA, typename HostTensorB, typename HostTensorC, typename Functor>
|
||||
void host_elementwise4D(HostTensorC& C,
|
||||
const HostTensorA& A,
|
||||
const HostTensorB& B,
|
||||
@@ -52,11 +44,11 @@ void host_elementwise4D(HostTensorC& C,
|
||||
for(std::size_t h = 0; h < shape[2]; ++h)
|
||||
for(std::size_t w = 0; w < shape[3]; ++w)
|
||||
{
|
||||
ComputeDataType a_val = ck::type_convert<ComputeDataType>(A(n, c, h, w));
|
||||
ComputeDataType b_val = ck::type_convert<ComputeDataType>(B(n, c, h, w));
|
||||
ComputeDataType c_val = 0;
|
||||
auto a_val = A(n, c, h, w);
|
||||
auto b_val = B(n, c, h, w);
|
||||
ctype c_val = 0;
|
||||
functor(c_val, a_val, b_val);
|
||||
C(n, c, h, w) = ck::type_convert<ctype>(c_val);
|
||||
C(n, c, h, w) = c_val;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -85,23 +77,24 @@ int main()
|
||||
b_device_buf.GetDeviceBuffer()};
|
||||
std::array<void*, 1> output = {c_device_buf.GetDeviceBuffer()};
|
||||
|
||||
std::vector<ck::index_t> a_strides{a.mDesc.GetStrides().begin(), a.mDesc.GetStrides().end()};
|
||||
std::vector<ck::index_t> b_strides{b.mDesc.GetStrides().begin(), b.mDesc.GetStrides().end()};
|
||||
std::vector<ck::index_t> c_strides{c.mDesc.GetStrides().begin(), c.mDesc.GetStrides().end()};
|
||||
std::array<ck::index_t, 4> abc_lengths;
|
||||
std::array<ck::index_t, 4> a_strides;
|
||||
std::array<ck::index_t, 4> b_strides;
|
||||
std::array<ck::index_t, 4> c_strides;
|
||||
|
||||
std::copy(nchw.begin(), nchw.end(), abc_lengths.begin());
|
||||
std::copy(a.mDesc.GetStrides().begin(), a.mDesc.GetStrides().end(), a_strides.begin());
|
||||
std::copy(b.mDesc.GetStrides().begin(), b.mDesc.GetStrides().end(), b_strides.begin());
|
||||
std::copy(c.mDesc.GetStrides().begin(), c.mDesc.GetStrides().end(), c_strides.begin());
|
||||
|
||||
auto broadcastAdd = DeviceElementwiseAddInstance{};
|
||||
auto argument =
|
||||
broadcastAdd.MakeArgumentPointer(input,
|
||||
output,
|
||||
std::vector<ck::index_t>{nchw.begin(), nchw.end()},
|
||||
{{a_strides}, b_strides},
|
||||
{c_strides},
|
||||
Add{});
|
||||
auto argument = broadcastAdd.MakeArgumentPointer(
|
||||
abc_lengths, {a_strides, b_strides}, {c_strides}, input, output, Add{});
|
||||
|
||||
if(!broadcastAdd.IsSupportedArgument(argument.get()))
|
||||
{
|
||||
throw std::runtime_error("The runtime parameters seems not supported by the "
|
||||
"DeviceBinaryElementwise instance, exiting!");
|
||||
throw std::runtime_error(
|
||||
"The runtime parameters seems not supported by the device instance, exiting!");
|
||||
};
|
||||
|
||||
auto broadcastAdd_invoker_ptr = broadcastAdd.MakeInvokerPointer();
|
||||
@@ -116,11 +109,8 @@ int main()
|
||||
c_device_buf.FromDevice(c.mData.data());
|
||||
Tensor<CDataType> host_c(nchw);
|
||||
|
||||
host_elementwise4D<Tensor<ABDataType>,
|
||||
Tensor<ABDataType>,
|
||||
Tensor<CDataType>,
|
||||
EltwiseComputeDataType,
|
||||
Add>(host_c, a, b, nchw, Add{});
|
||||
host_elementwise4D<Tensor<ABDataType>, Tensor<ABDataType>, Tensor<CDataType>, Add>(
|
||||
host_c, a, b, nchw, Add{});
|
||||
|
||||
pass &=
|
||||
ck::utils::check_err(c.mData, host_c.mData, "Error: Incorrect results c", 1e-3, 1e-3);
|
||||
|
||||
Reference in New Issue
Block a user