Commit dffafd2a authored by Mateusz Ozga's avatar Mateusz Ozga
Browse files

Rewrite .sh test to Gtest

parent 9cac2827
File mode changed from 100644 to 100755
#!/bin/bash
## The following will be used for CI
set -x
## for float
bin/test_reduce_no_index -D 64,4,280,82 -R 0,1,2,3 0 2
bin/test_reduce_no_index -D 64,4,280,82 -R 0,1,2 0 2
bin/test_reduce_no_index -D 64,4,280,82 -R 0,1,3 0 2
bin/test_reduce_no_index -D 64,4,280,82 -R 0,2,3 0 2
bin/test_reduce_no_index -D 64,4,280,82 -R 1,2,3 0 2
bin/test_reduce_no_index -D 64,4,280,82 -R 0 0 2
bin/test_reduce_no_index -D 64,4,280,82 -R 1 0 2
bin/test_reduce_no_index -D 64,4,280,82 -R 2 0 2
bin/test_reduce_no_index -D 64,4,280,82 -R 3 0 2
## for float64
bin/test_reduce_no_index -D 64,4,280,82 -R 0,1,2,3 6 2
bin/test_reduce_no_index -D 64,4,280,82 -R 0,1,2 6 2
bin/test_reduce_no_index -D 64,4,280,82 -R 0,1,3 6 2
bin/test_reduce_no_index -D 64,4,280,82 -R 0,2,3 6 2
bin/test_reduce_no_index -D 64,4,280,82 -R 1,2,3 6 2
bin/test_reduce_no_index -D 64,4,280,82 -R 0 6 2
bin/test_reduce_no_index -D 64,4,280,82 -R 1 6 2
bin/test_reduce_no_index -D 64,4,280,82 -R 2 6 2
bin/test_reduce_no_index -D 64,4,280,82 -R 3 6 2
## for float16
bin/test_reduce_no_index -D 64,4,280,82 -R 0,1,2,3 1 2
bin/test_reduce_no_index -D 64,4,280,82 -R 0,1,2 1 2
bin/test_reduce_no_index -D 64,4,280,82 -R 0,1,3 1 2
bin/test_reduce_no_index -D 64,4,280,82 -R 0,2,3 1 2
bin/test_reduce_no_index -D 64,4,280,82 -R 1,2,3 1 2
bin/test_reduce_no_index -D 64,4,280,82 -R 0 1 2
bin/test_reduce_no_index -D 64,4,280,82 -R 1 1 2
bin/test_reduce_no_index -D 64,4,280,82 -R 2 1 2
bin/test_reduce_no_index -D 64,4,280,82 -R 3 1 2
## for int8_t
bin/test_reduce_no_index -D 64,4,280,82 -R 0,1,2,3 3 2
bin/test_reduce_no_index -D 64,4,280,82 -R 0,1,2 3 2
bin/test_reduce_no_index -D 64,4,280,82 -R 0,1,3 3 2
bin/test_reduce_no_index -D 64,4,280,82 -R 0,2,3 3 2
bin/test_reduce_no_index -D 64,4,280,82 -R 1,2,3 3 2
bin/test_reduce_no_index -D 64,4,280,82 -R 0 3 2
bin/test_reduce_no_index -D 64,4,280,82 -R 1 3 2
bin/test_reduce_no_index -D 64,4,280,82 -R 2 3 2
bin/test_reduce_no_index -D 64,4,280,82 -R 3 3 2
## for bfloat16
bin/test_reduce_no_index -D 64,4,280,82 -R 0,1,2,3 5 2
bin/test_reduce_no_index -D 64,4,280,82 -R 0,1,2 5 2
bin/test_reduce_no_index -D 64,4,280,82 -R 0,1,3 5 2
bin/test_reduce_no_index -D 64,4,280,82 -R 0,2,3 5 2
bin/test_reduce_no_index -D 64,4,280,82 -R 1,2,3 5 2
bin/test_reduce_no_index -D 64,4,280,82 -R 0 5 2
bin/test_reduce_no_index -D 64,4,280,82 -R 1 5 2
bin/test_reduce_no_index -D 64,4,280,82 -R 2 5 2
bin/test_reduce_no_index -D 64,4,280,82 -R 3 5 2
set +x
add_test_executable(test_reduce_no_index reduce_no_index.cpp) add_gtest_executable(test_reduce_no_index reduce_no_index.cpp)
add_test_executable(test_reduce_with_index reduce_with_index.cpp) add_test_executable(test_reduce_with_index reduce_with_index.cpp)
target_link_libraries(test_reduce_no_index PRIVATE utility device_reduce_instance) target_link_libraries(test_reduce_no_index PRIVATE utility device_reduce_instance)
target_link_libraries(test_reduce_with_index PRIVATE utility device_reduce_instance) target_link_libraries(test_reduce_with_index PRIVATE utility device_reduce_instance)
......
// SPDX-License-Identifier: MIT // SPDX-License-Identifier: MIT
// Copyright (c) 2018-2023, Advanced Micro Devices, Inc. All rights reserved. // Copyright (c) 2018-2024, Advanced Micro Devices, Inc. All rights reserved.
#include <getopt.h> #include <getopt.h>
#include "ck/library/utility/host_common_util.hpp" #include "ck/library/utility/host_common_util.hpp"
#include "profiler/profile_reduce_impl.hpp" #include "profiler/profile_reduce_impl.hpp"
#include <gtest/gtest.h>
using namespace ck; using namespace ck;
static struct option long_options[] = {{"inLengths", required_argument, nullptr, 'D'}, struct ReduceParam
{"reduceDimensions", required_argument, nullptr, 'R'},
{"scales", required_argument, nullptr, 'S'},
{"help", no_argument, nullptr, '?'},
{nullptr, 0, nullptr, 0}};
class SimpleAppArgs
{ {
private: bool do_verification;
int option_index = 0; bool propagateNan;
bool useIndex;
public: bool time_kernel;
bool do_dumpout;
int init_method;
float alpha;
float beta;
std::vector<size_t> inLengths; std::vector<size_t> inLengths;
std::vector<int> reduceDims; std::vector<int> reduceDims;
std::vector<float> scales; ReduceTensorOp reduceOpId;
};
int data_type; std::vector<std::vector<int>> settGenericReduceDim()
int init_method = 1; {
return {{0, 1, 2, 3}, {0, 1, 2}, {0, 1, 3}, {0, 2, 3}, {1, 2, 3}, {0}, {1}, {2}, {3}};
}
public: ReduceParam sett0()
void show_usage(const char* cmd) {
{ return {/*do_verification*/ true,
std::cout << "Usage of " << cmd << std::endl; /*propagateNan*/ false,
std::cout << "--inLengths or -D, comma separated list of input tensor dimension lengths " /*useIndex*/ false,
"(only 4-d tensor supported)" /*time_kernel*/ false,
<< std::endl; /*do_dumpout*/ false,
std::cout << "--reduceDimensions or -R comma seperated list of dimension indexes to reduce " /*init_method*/ 2,
"(only 1 or 3 or 4 dimensions supported)" /*alpha*/ 1.0f,
<< std::endl; /*beta*/ 0.0f,
std::cout << "--scales or -S, comma separated two float values for alpha and beta" /*inLengths*/ {64, 4, 280, 82},
<< std::endl; /*reduceDims*/ {0, 1, 2, 3},
std::cout << "Arg1 -- data type (0: fp16, 1: fp32, 3: int8, 5: bp16, 6: fp64)" << std::endl; /*reduceOpId*/ ReduceTensorOp::AMAX};
std::cout << "Arg2 -- init method(0=no init, 1=single integer value, 2=scope integer " }
"value, 3=decimal value)"
<< std::endl;
};
int processArgs(int argc, char* argv[]) template <typename T>
{ class ReduceNoIndexTest : public ::testing::Test
using ck::host_common::getTypeValuesFromString; {
protected:
using InDataType = std::tuple_element_t<0, T>;
using AccDataType = std::tuple_element_t<1, T>;
using OutDataType = std::tuple_element_t<2, T>;
int ch; static std::vector<ReduceParam> params;
while(1) static void SetUpTestSuite()
{ {
ch = getopt_long(argc, argv, "D:R:S:", long_options, &option_index); // set testcase variables
if(ch == -1) ReduceParam _sett0 = sett0();
break; // + reduce dims: Generic;
switch(ch) // set testcase variables
{ const auto settReduceDim = settGenericReduceDim();
case 'D':
if(!optarg) for(std::size_t i(0); i < settReduceDim.size(); ++i)
throw std::runtime_error("Invalid option format!");
inLengths = getTypeValuesFromString<size_t>(optarg);
break;
case 'R':
if(!optarg)
throw std::runtime_error("Invalid option format!");
reduceDims = getTypeValuesFromString<int>(optarg);
break;
case 'S':
if(!optarg)
throw std::runtime_error("Invalid option format!");
scales = getTypeValuesFromString<float>(optarg);
break;
case '?':
if(std::string(long_options[option_index].name) == "help")
{ {
show_usage(argv[0]); _sett0.reduceOpId = ReduceTensorOp::AMAX;
return (-1); _sett0.reduceDims = settReduceDim[i];
}; params.push_back(_sett0);
break; _sett0.reduceOpId = ReduceTensorOp::MIN;
default: show_usage(argv[0]); return (-1); }
};
}; };
if(optind + 2 > argc) void Run()
throw std::runtime_error("Invalid cmd-line arguments, more argumetns are needed!");
data_type = std::atoi(argv[optind++]);
init_method = std::atoi(argv[optind]);
if(scales.empty())
{ {
scales.push_back(1.0f); for(auto param : this->params)
scales.push_back(0.0f); {
}; bool success = ck::profiler::profile_reduce_impl<InDataType, AccDataType, OutDataType>(
param.do_verification,
param.init_method,
param.do_dumpout,
param.time_kernel,
param.inLengths,
param.reduceDims,
param.reduceOpId,
param.propagateNan,
param.useIndex,
param.alpha,
param.beta);
// EXPECT_TRUE
EXPECT_TRUE(success);
}
}
};
if(inLengths.size() != 4 || template <typename T>
(reduceDims.size() != 1 && reduceDims.size() != 3 && reduceDims.size() != 4)) std::vector<ReduceParam> ReduceNoIndexTest<T>::params = {};
return (-1);
if(data_type != 0 && data_type != 1 && data_type != 3 && data_type != 5 && data_type != 6) using Reduce_float_types = ::testing::Types<std::tuple<float, float, float>>;
return (-1); using Reduce_double_types = ::testing::Types<std::tuple<double, double, double>>;
using Reduce_int8t_types = ::testing::Types<std::tuple<int8_t, int8_t, int8_t>>;
using Reduce_half_types = ::testing::Types<std::tuple<ck::half_t, ck::half_t, ck::half_t>>;
using Reduce_bhalf_float_Types = ::testing::Types<std::tuple<ck::bhalf_t, float, ck::bhalf_t>>;
return (0); template <typename TType>
}; class ReduceNoIndexFloat : public ReduceNoIndexTest<TType>
{
}; };
bool test_reduce_no_index(int data_type, template <typename TType>
int init_method, class ReduceNoIndexDouble : public ReduceNoIndexTest<TType>
std::vector<int> reduceDims,
std::vector<size_t> inLengths,
ReduceTensorOp reduceOpId,
bool propagateNan,
float alpha,
float beta)
{ {
using ck::profiler::profile_reduce_impl; };
bool result = true; template <typename TType>
class ReduceNoIndexInt8 : public ReduceNoIndexTest<TType>
{
};
if(data_type == 0) template <typename TType>
{ class ReduceNoIndexHalf : public ReduceNoIndexTest<TType>
result = profile_reduce_impl<float, float, float>(true, {
init_method, };
false,
false,
inLengths,
reduceDims,
reduceOpId,
propagateNan,
false,
alpha,
beta);
}
else if(data_type == 1)
{
result = profile_reduce_impl<ck::half_t, float, ck::half_t>(true,
init_method,
false,
false,
inLengths,
reduceDims,
reduceOpId,
propagateNan,
false,
alpha,
beta);
}
else if(data_type == 3)
{
result = profile_reduce_impl<int8_t, int32_t, int8_t>(true,
init_method,
false,
false,
inLengths,
reduceDims,
reduceOpId,
propagateNan,
false,
alpha,
beta);
}
else if(data_type == 5)
{
result = profile_reduce_impl<ck::bhalf_t, float, ck::bhalf_t>(true,
init_method,
false,
false,
inLengths,
reduceDims,
reduceOpId,
propagateNan,
false,
alpha,
beta);
}
else if(data_type == 6)
{
result = profile_reduce_impl<double, double, double>(true,
init_method,
false,
false,
inLengths,
reduceDims,
reduceOpId,
propagateNan,
false,
alpha,
beta);
}
return (result); template <typename TType>
class ReduceNoIndexBHalfFloat : public ReduceNoIndexTest<TType>
{
}; };
constexpr ReduceTensorOp reduceOpId = ReduceTensorOp::AVG; TYPED_TEST_SUITE(ReduceNoIndexFloat, Reduce_float_types);
constexpr bool propagateNan = false; TYPED_TEST_SUITE(ReduceNoIndexDouble, Reduce_double_types);
TYPED_TEST_SUITE(ReduceNoIndexInt8, Reduce_int8t_types);
TYPED_TEST_SUITE(ReduceNoIndexHalf, Reduce_half_types);
TYPED_TEST_SUITE(ReduceNoIndexBHalfFloat, Reduce_bhalf_float_Types);
int main(int argc, char* argv[]) TYPED_TEST(ReduceNoIndexFloat, ReduceNoIndexTestFloat)
{ {
SimpleAppArgs args; // trigger Run() -> Generic
this->Run();
bool result = true; }
if(argc == 1) TYPED_TEST(ReduceNoIndexDouble, ReduceNoIndexTestDouble)
{ {
int data_type = 1; // trigger Run() -> Generic
int init_method = 2; this->Run();
std::vector<size_t> inLengths{64, 4, 280, 80}; }
std::vector<std::vector<int>> v_reduceDims{
{0, 1, 2, 3}, {0, 1, 2}, {1, 2, 3}, {0, 1, 3}, {0, 2, 3}, {0}, {1}, {2}, {3}};
for(auto& reduceDims : v_reduceDims)
result = result && test_reduce_no_index(data_type,
init_method,
reduceDims,
inLengths,
reduceOpId,
propagateNan,
1.0f,
0.0f);
}
else
{
if(args.processArgs(argc, argv) < 0)
{
throw std::runtime_error(
"Invalid input arguments, test_reduce_no_index could not be executed!");
};
result = test_reduce_no_index(args.data_type, TYPED_TEST(ReduceNoIndexInt8, ReduceNoIndexTestInt8)
args.init_method, {
args.reduceDims, // trigger Run() -> Generic
args.inLengths, this->Run();
reduceOpId, }
propagateNan,
args.scales[0],
args.scales[1]);
}
std::cout << "test_reduce_no_index ..... " << (result ? "SUCCESS" : "FAILURE") << std::endl; TYPED_TEST(ReduceNoIndexHalf, ReduceNoIndexTestHalf)
{
// trigger Run() -> Generic
this->Run();
}
return (result ? 0 : -1); TYPED_TEST(ReduceNoIndexBHalfFloat, ReduceNoIndexTestBHalfFloat)
{
// trigger Run() -> Generic
this->Run();
} }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment