mirror of
https://github.com/ROCm/composable_kernel.git
synced 2026-04-20 06:49:15 +00:00
Common forward convolution utility refactor. (#141)
* Convolution ND * Code unification across dimensions for generating tensor descriptors. * Example * Instances * Move convnd f32 instance file to comply with repo structure. * Conv 1D tensor layouts. * Formatting and use ReferenceConv * Reference ConvFwd supporting 1D and 2D convolution. * Debug printing TensorLayout name. * Conv fwd 1D instance f32 * Refactor conv ND example. Needed to support various conv dimensio. Needed to support various conv dimensions * Rename conv nd example director to prevent conflicts. * Refactor some common utility to single file. Plus some tests. * Refactor GetHostTensorDescriptor + UT. * Add 1D test case. * Test reference convolution 1d/2d * Remove some leftovers. * Fix convolution example error for 1D * Refactor test check errors utility function. * Test Conv2D Fwd XDL * More UT for 1D case. * Parameterize input & weight initializers. * Rename example to prevent conflicts. * Split convnd instance into separate files for 1d/2d * Address review comments. * Fix data type for flops/gbytes calculations. * Assign example number 11. * 3D cases for convolution utility functions. * 3D reference convolution. * Add support for 3D convolution. * Check for inputs bigger than 2GB. * Formatting * Support for bf16/f16/f32/i8 - conv instances + UT. * Use check_err from test_util.hpp. * Split convnd test into separate files for each dim. * Fix data generation and use proper instances. * Formatting * Skip tensor initialization if not necessary. * Fix CMakefiles. * Remove redundant conv2d_fwd test. * Lower problem size for conv3D UT. * 3D case for convnd example. * Remove leftovers after merge. * Add Conv Specialization string to GetTypeString * Skip instance causing numerical errors. * Small fixes. * Remove redundant includes. * Fix namespace name error. * Script for automatic testing and logging convolution fwd UTs * Comment out numactl cmd. * Refine weights initalization and relax rtol for fp16 * Move test_util.hpp to check_err.hpp * Refine weights initalization and relax rtol for fp16 * Refactor common part of test conv utils. * Move utility function to single common place. * Add additional common functions to utility. * Refactor convnd_fwd_xdl examples. * Remove redundant files. * Unify structure. * Add constructor to ConvParams. * And add input parameters validation. * Modify conv examples to use single utility file. * Remove check_error from host_tensor.hpp * Get rid of check_indices function. * Remove bf16_to_f32 function overload for scalars. * Fix namespace. * Add half_float::half for check_err. * Fix conv params size in UT. * Fix weights initialization for int8. * Fix weights initialization for int8. * Add type_convert when store output in ref conv 1D. * Get back old conv2d_fwd_xdl operation. * Silence conv debug print. * format * clean * clean * Fix merge. * Fix namespace for check_err * Formatting. * Fix merge artifacts. * Remove deleted header. * Fix some includes and use ck::utils::check_err. * Remove unused check_indices restored by previous merge. * Fix namespaces after merge. * Fix compilation error. * Small fixes. * Use common functions. * Fix filename * Fix namespaces. * Fix merge artifact - retrieve removed by accident fun. * Fix ConvForwardSpecialization. * Adhere to coding style rules. * Fix merge artifacts. Co-authored-by: Adam Osewski <aosewski@amd.com> Co-authored-by: Chao Liu <chao.liu2@amd.com>
This commit is contained in:
@@ -6,13 +6,13 @@
|
||||
#include <type_traits>
|
||||
#include <vector>
|
||||
|
||||
#include "check_err.hpp"
|
||||
#include "config.hpp"
|
||||
#include "conv_utils.hpp"
|
||||
#include "conv_fwd_util.hpp"
|
||||
#include "element_wise_operation.hpp"
|
||||
#include "host_tensor.hpp"
|
||||
#include "reference_conv_fwd.hpp"
|
||||
#include "tensor_layout.hpp"
|
||||
#include "test_util.hpp"
|
||||
|
||||
namespace {
|
||||
using InElementOp = ck::tensor_operation::element_wise::PassThrough;
|
||||
@@ -57,9 +57,10 @@ template <ck::index_t NDim,
|
||||
typename OutLayout = ck::tensor_layout::convolution::NHWK,
|
||||
typename FillInputOp = FillMonotonicSeq<InDataType>,
|
||||
typename FillWeightsOp = FillConstant<WeiDataType>>
|
||||
Tensor<OutDataType> RunReferenceConv(const ck::conv_util::ConvParams& params,
|
||||
const FillInputOp& fill_input_op = FillInputOp{},
|
||||
const FillWeightsOp& fill_weights_op = FillWeightsOp{0.5f})
|
||||
Tensor<OutDataType>
|
||||
run_reference_convolution_forward(const ck::utils::conv::ConvParams& params,
|
||||
const FillInputOp& fill_input_op = FillInputOp{},
|
||||
const FillWeightsOp& fill_weights_op = FillWeightsOp{0.5f})
|
||||
{
|
||||
std::vector<std::size_t> input_dims{static_cast<std::size_t>(params.N),
|
||||
static_cast<std::size_t>(params.C)};
|
||||
@@ -80,18 +81,16 @@ Tensor<OutDataType> RunReferenceConv(const ck::conv_util::ConvParams& params,
|
||||
std::begin(output_spatial_lengths),
|
||||
std::end(output_spatial_lengths));
|
||||
|
||||
Tensor<InDataType> input(ck::conv_util::GetHostTensorDescriptor(input_dims, InLayout{}));
|
||||
Tensor<WeiDataType> weights(ck::conv_util::GetHostTensorDescriptor(filter_dims, WeiLayout{}));
|
||||
Tensor<InDataType> input(ck::utils::conv::get_host_tensor_descriptor(input_dims, InLayout{}));
|
||||
Tensor<WeiDataType> weights(
|
||||
ck::utils::conv::get_host_tensor_descriptor(filter_dims, WeiLayout{}));
|
||||
Tensor<OutDataType> host_output(
|
||||
ck::conv_util::GetHostTensorDescriptor(output_dims, OutLayout{}));
|
||||
ck::utils::conv::get_host_tensor_descriptor(output_dims, OutLayout{}));
|
||||
|
||||
fill_input_op(input.begin(), input.end());
|
||||
fill_weights_op(weights.begin(), weights.end());
|
||||
std::fill(host_output.begin(), host_output.end(), OutDataType(0.f));
|
||||
|
||||
// std::cout <<"input: " << input.mDesc << std::endl << input.mData << std::endl;
|
||||
// std::cout <<"weight: " << weights.mDesc << std::endl << weights.mData << std::endl;
|
||||
|
||||
auto ref_conv = ck::tensor_operation::host::ReferenceConvFwd<InDataType,
|
||||
WeiDataType,
|
||||
OutDataType,
|
||||
@@ -116,10 +115,10 @@ Tensor<OutDataType> RunReferenceConv(const ck::conv_util::ConvParams& params,
|
||||
return host_output;
|
||||
}
|
||||
|
||||
bool TestConv2DNHWC()
|
||||
bool test_conv2d_nhwc()
|
||||
{
|
||||
bool res{true};
|
||||
ck::conv_util::ConvParams params;
|
||||
ck::utils::conv::ConvParams params;
|
||||
params.N = 1;
|
||||
params.K = 1;
|
||||
params.C = 2;
|
||||
@@ -130,7 +129,7 @@ bool TestConv2DNHWC()
|
||||
params.input_left_pads = std::vector<ck::index_t>{0, 0};
|
||||
params.input_right_pads = std::vector<ck::index_t>{0, 0};
|
||||
|
||||
auto out_tensor = RunReferenceConv<2>(params);
|
||||
auto out_tensor = run_reference_convolution_forward<2>(params);
|
||||
std::vector<std::size_t> ref_dims{1, 1, 4, 4};
|
||||
std::vector<float> ref_data{130.5,
|
||||
148.5,
|
||||
@@ -148,10 +147,10 @@ bool TestConv2DNHWC()
|
||||
472.5,
|
||||
490.5,
|
||||
508.5};
|
||||
res = res && test::check_err(out_tensor.mDesc.GetLengths(),
|
||||
ref_dims,
|
||||
"Error: wrong output tensor dimensions!");
|
||||
res = res && test::check_err(out_tensor.mData, ref_data, "Error: incorrect results!");
|
||||
res = res && ck::utils::check_err(out_tensor.mDesc.GetLengths(),
|
||||
ref_dims,
|
||||
"Error: wrong output tensor dimensions!");
|
||||
res = res && ck::utils::check_err(out_tensor.mData, ref_data, "Error: incorrect results!");
|
||||
|
||||
params.N = 1;
|
||||
params.K = 2;
|
||||
@@ -163,7 +162,7 @@ bool TestConv2DNHWC()
|
||||
params.input_left_pads = std::vector<ck::index_t>{1, 1};
|
||||
params.input_right_pads = std::vector<ck::index_t>{1, 1};
|
||||
|
||||
out_tensor = RunReferenceConv<2>(params);
|
||||
out_tensor = run_reference_convolution_forward<2>(params);
|
||||
ref_dims = std::vector<std::size_t>{1, 2, 5, 5};
|
||||
ref_data = std::vector<float>{
|
||||
210., 210., 327., 327., 351., 351., 375., 375., 399., 399.,
|
||||
@@ -171,18 +170,18 @@ bool TestConv2DNHWC()
|
||||
747., 747., 1138.5, 1138.5, 1174.5, 1174.5, 1210.5, 1210.5, 1246.5, 1246.5,
|
||||
1035., 1035., 1570.5, 1570.5, 1606.5, 1606.5, 1642.5, 1642.5, 1678.5, 1678.5,
|
||||
1323., 1323., 2002.5, 2002.5, 2038.5, 2038.5, 2074.5, 2074.5, 2110.5, 2110.5};
|
||||
res = res && test::check_err(out_tensor.mDesc.GetLengths(),
|
||||
ref_dims,
|
||||
"Error: wrong output tensor dimensions!");
|
||||
res = res && test::check_err(out_tensor.mData, ref_data, "Error: incorrect results!");
|
||||
res = res && ck::utils::check_err(out_tensor.mDesc.GetLengths(),
|
||||
ref_dims,
|
||||
"Error: wrong output tensor dimensions!");
|
||||
res = res && ck::utils::check_err(out_tensor.mData, ref_data, "Error: incorrect results!");
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
bool TestConv1DNWC()
|
||||
bool test_conv1d_nwc()
|
||||
{
|
||||
bool res{true};
|
||||
ck::conv_util::ConvParams params;
|
||||
ck::utils::conv::ConvParams params;
|
||||
params.num_dim_spatial = 1;
|
||||
params.N = 1;
|
||||
params.K = 1;
|
||||
@@ -194,19 +193,20 @@ bool TestConv1DNWC()
|
||||
params.input_left_pads = std::vector<ck::index_t>{0};
|
||||
params.input_right_pads = std::vector<ck::index_t>{0};
|
||||
|
||||
auto out_tensor = RunReferenceConv<1,
|
||||
float,
|
||||
float,
|
||||
float,
|
||||
ck::tensor_layout::convolution::NWC,
|
||||
ck::tensor_layout::convolution::KXC,
|
||||
ck::tensor_layout::convolution::NWK>(params);
|
||||
auto out_tensor =
|
||||
run_reference_convolution_forward<1,
|
||||
float,
|
||||
float,
|
||||
float,
|
||||
ck::tensor_layout::convolution::NWC,
|
||||
ck::tensor_layout::convolution::KXC,
|
||||
ck::tensor_layout::convolution::NWK>(params);
|
||||
std::vector<std::size_t> ref_dims{1, 1, 4};
|
||||
std::vector<float> ref_data{7.5, 13.5, 19.5, 25.5};
|
||||
res = res && test::check_err(out_tensor.mDesc.GetLengths(),
|
||||
ref_dims,
|
||||
"Error: wrong output tensor dimensions!");
|
||||
res = res && test::check_err(out_tensor.mData, ref_data, "Error: incorrect results!");
|
||||
res = res && ck::utils::check_err(out_tensor.mDesc.GetLengths(),
|
||||
ref_dims,
|
||||
"Error: wrong output tensor dimensions!");
|
||||
res = res && ck::utils::check_err(out_tensor.mData, ref_data, "Error: incorrect results!");
|
||||
|
||||
params.num_dim_spatial = 1;
|
||||
params.N = 1;
|
||||
@@ -219,19 +219,19 @@ bool TestConv1DNWC()
|
||||
params.input_left_pads = std::vector<ck::index_t>{1};
|
||||
params.input_right_pads = std::vector<ck::index_t>{1};
|
||||
|
||||
out_tensor = RunReferenceConv<1,
|
||||
float,
|
||||
float,
|
||||
float,
|
||||
ck::tensor_layout::convolution::NWC,
|
||||
ck::tensor_layout::convolution::KXC,
|
||||
ck::tensor_layout::convolution::NWK>(params);
|
||||
out_tensor = run_reference_convolution_forward<1,
|
||||
float,
|
||||
float,
|
||||
float,
|
||||
ck::tensor_layout::convolution::NWC,
|
||||
ck::tensor_layout::convolution::KXC,
|
||||
ck::tensor_layout::convolution::NWK>(params);
|
||||
ref_dims = std::vector<std::size_t>{1, 2, 5};
|
||||
ref_data = std::vector<float>{9., 9., 19.5, 19.5, 31.5, 31.5, 43.5, 43.5, 55.5, 55.5};
|
||||
res = res && test::check_err(out_tensor.mDesc.GetLengths(),
|
||||
ref_dims,
|
||||
"Error: wrong output tensor dimensions!");
|
||||
res = res && test::check_err(out_tensor.mData, ref_data, "Error: incorrect results!");
|
||||
res = res && ck::utils::check_err(out_tensor.mDesc.GetLengths(),
|
||||
ref_dims,
|
||||
"Error: wrong output tensor dimensions!");
|
||||
res = res && ck::utils::check_err(out_tensor.mData, ref_data, "Error: incorrect results!");
|
||||
|
||||
params.num_dim_spatial = 1;
|
||||
params.N = 2;
|
||||
@@ -244,13 +244,13 @@ bool TestConv1DNWC()
|
||||
params.input_left_pads = std::vector<ck::index_t>{1};
|
||||
params.input_right_pads = std::vector<ck::index_t>{1};
|
||||
|
||||
auto out_tensor2 = RunReferenceConv<1,
|
||||
float,
|
||||
float,
|
||||
float,
|
||||
ck::tensor_layout::convolution::NWC,
|
||||
ck::tensor_layout::convolution::KXC,
|
||||
ck::tensor_layout::convolution::NWK>(
|
||||
auto out_tensor2 = run_reference_convolution_forward<1,
|
||||
float,
|
||||
float,
|
||||
float,
|
||||
ck::tensor_layout::convolution::NWC,
|
||||
ck::tensor_layout::convolution::KXC,
|
||||
ck::tensor_layout::convolution::NWK>(
|
||||
params, FillMonotonicSeq<float>{0.f, 0.1f});
|
||||
|
||||
ref_dims = std::vector<std::size_t>{2, 16, 16};
|
||||
@@ -319,18 +319,18 @@ bool TestConv1DNWC()
|
||||
72.9, 72.9, 72.9, 72.9, 72.9, 72.9, 72.9, 72.9,
|
||||
49.4, 49.4, 49.4, 49.4, 49.4, 49.4, 49.4, 49.4,
|
||||
49.4, 49.4, 49.4, 49.4, 49.4, 49.4, 49.4, 49.4};
|
||||
res = res && test::check_err(out_tensor2.mDesc.GetLengths(),
|
||||
ref_dims,
|
||||
"Error: wrong output tensor dimensions!");
|
||||
res = res && test::check_err(out_tensor2.mData, ref_data, "Error: incorrect results!");
|
||||
res = res && ck::utils::check_err(out_tensor2.mDesc.GetLengths(),
|
||||
ref_dims,
|
||||
"Error: wrong output tensor dimensions!");
|
||||
res = res && ck::utils::check_err(out_tensor2.mData, ref_data, "Error: incorrect results!");
|
||||
|
||||
return res;
|
||||
}
|
||||
|
||||
bool TestConv3DNCDHW()
|
||||
bool test_conv3d_ncdhw()
|
||||
{
|
||||
bool res{true};
|
||||
ck::conv_util::ConvParams params;
|
||||
ck::utils::conv::ConvParams params;
|
||||
params.num_dim_spatial = 3;
|
||||
params.N = 1;
|
||||
params.K = 1;
|
||||
@@ -342,13 +342,13 @@ bool TestConv3DNCDHW()
|
||||
params.input_left_pads = std::vector<ck::index_t>{0, 0, 0};
|
||||
params.input_right_pads = std::vector<ck::index_t>{0, 0, 0};
|
||||
|
||||
auto out_tensor = RunReferenceConv<3,
|
||||
float,
|
||||
float,
|
||||
float,
|
||||
ck::tensor_layout::convolution::NCDHW,
|
||||
ck::tensor_layout::convolution::KCZYX,
|
||||
ck::tensor_layout::convolution::NKDHW>(
|
||||
auto out_tensor = run_reference_convolution_forward<3,
|
||||
float,
|
||||
float,
|
||||
float,
|
||||
ck::tensor_layout::convolution::NCDHW,
|
||||
ck::tensor_layout::convolution::KCZYX,
|
||||
ck::tensor_layout::convolution::NKDHW>(
|
||||
params, FillMonotonicSeq<float>{0.f, 0.1f});
|
||||
std::vector<std::size_t> ref_dims{1, 1, 4, 4, 4};
|
||||
std::vector<float> ref_data{
|
||||
@@ -360,10 +360,11 @@ bool TestConv3DNCDHW()
|
||||
634.5, 637.2, 639.9, 642.60004, 650.7, 653.4, 656.10004, 658.8,
|
||||
699.3, 702., 704.7, 707.4, 715.5, 718.2, 720.9, 723.60004,
|
||||
731.7, 734.4001, 737.10004, 739.8, 747.9001, 750.60004, 753.3, 756.};
|
||||
res = res && test::check_err(out_tensor.mDesc.GetLengths(),
|
||||
ref_dims,
|
||||
"Error [case 1]: wrong output tensor dimensions!");
|
||||
res = res && test::check_err(out_tensor.mData, ref_data, "Error [case 1]: incorrect results!");
|
||||
res = res && ck::utils::check_err(out_tensor.mDesc.GetLengths(),
|
||||
ref_dims,
|
||||
"Error [case 1]: wrong output tensor dimensions!");
|
||||
res = res &&
|
||||
ck::utils::check_err(out_tensor.mData, ref_data, "Error [case 1]: incorrect results!");
|
||||
|
||||
params.N = 1;
|
||||
params.K = 2;
|
||||
@@ -375,13 +376,13 @@ bool TestConv3DNCDHW()
|
||||
params.input_left_pads = std::vector<ck::index_t>{0, 0, 0};
|
||||
params.input_right_pads = std::vector<ck::index_t>{0, 0, 0};
|
||||
|
||||
out_tensor = RunReferenceConv<3,
|
||||
float,
|
||||
float,
|
||||
float,
|
||||
ck::tensor_layout::convolution::NCDHW,
|
||||
ck::tensor_layout::convolution::KCZYX,
|
||||
ck::tensor_layout::convolution::NKDHW>(
|
||||
out_tensor = run_reference_convolution_forward<3,
|
||||
float,
|
||||
float,
|
||||
float,
|
||||
ck::tensor_layout::convolution::NCDHW,
|
||||
ck::tensor_layout::convolution::KCZYX,
|
||||
ck::tensor_layout::convolution::NKDHW>(
|
||||
params, FillMonotonicSeq<float>{0.f, 0.1f});
|
||||
ref_dims = std::vector<std::size_t>{1, 2, 4, 4, 4};
|
||||
ref_data = std::vector<float>{
|
||||
@@ -401,11 +402,11 @@ bool TestConv3DNCDHW()
|
||||
5283.9004, 5292., 5300.0996, 5308.2, 5381.0996, 5389.2, 5397.3, 5405.4004,
|
||||
6255.9004, 6264.0005, 6272.1, 6280.2, 6353.1, 6361.2, 6369.301, 6377.4,
|
||||
6450.301, 6458.4, 6466.5, 6474.6, 6547.5, 6555.6, 6563.699, 6571.801};
|
||||
res = res && test::check_err(out_tensor.mDesc.GetLengths(),
|
||||
ref_dims,
|
||||
"Error [case 2]: wrong output tensor dimensions!");
|
||||
res = res && ck::utils::check_err(out_tensor.mDesc.GetLengths(),
|
||||
ref_dims,
|
||||
"Error [case 2]: wrong output tensor dimensions!");
|
||||
res =
|
||||
res && test::check_err(
|
||||
res && ck::utils::check_err(
|
||||
out_tensor.mData, ref_data, "Error [case 2]: incorrect results!", 1e-4f, 1e-6f);
|
||||
|
||||
return res;
|
||||
@@ -416,11 +417,11 @@ bool TestConv3DNCDHW()
|
||||
int main(void)
|
||||
{
|
||||
bool res{true};
|
||||
res = TestConv2DNHWC();
|
||||
std::cout << "TestConv2DNHWC ..... " << (res ? "SUCCESS" : "FAILURE") << std::endl;
|
||||
res = TestConv1DNWC();
|
||||
res = test_conv2d_nhwc();
|
||||
std::cout << "test_conv2d_nhwc ..... " << (res ? "SUCCESS" : "FAILURE") << std::endl;
|
||||
res = test_conv1d_nwc();
|
||||
std::cout << "TestConv1DNHWC ..... " << (res ? "SUCCESS" : "FAILURE") << std::endl;
|
||||
res = TestConv3DNCDHW();
|
||||
std::cout << "TestConv3DNCDHW ..... " << (res ? "SUCCESS" : "FAILURE") << std::endl;
|
||||
res = test_conv3d_ncdhw();
|
||||
std::cout << "test_conv3d_ncdhw ..... " << (res ? "SUCCESS" : "FAILURE") << std::endl;
|
||||
return res ? 0 : 1;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user