mirror of
https://github.com/ROCm/composable_kernel.git
synced 2026-05-03 21:21:22 +00:00
* updating codegen build for MIOpen access: adding .cmake for codegen component * updating CMake * adding in header guards for some headers due to issues with hiprtc compilation in MIOpen * some more header guards * putting env file in header guard * cleaning up some includes * updated types file for hiprtc purposes * fixed types file: bit-wise/memcpy issue * updating multiple utility files to deal with standard header inclusion for hiprtc * added some more header guards in the utility files, replacing some standard header functionality * added some more header guards * fixing some conflicts in utility files, another round of header guards * fixing errors in data type file * resolved conflict errors in a few utility files * added header guards/replicated functionality in device files * resolved issues with standard headers in device files: device_base and device_grouped_conv_fwd_multiple_abd * resolved issues with standard headers in device files: device_base.hpp, device_grouped_conv_fwd_multiple_abd.hpp, device_grouped_conv_fwd_multiple_abd_xdl_cshuffle.hpp * added header guards for gridwise gemm files: gridwise_gemm_multiple_abd_xdl_cshuffle.hpp and gridwise_gemm_multiple_d_xdl_cshuffle.hpp * fixed issue with numerics header, removed from transform_conv_fwd_to_gemm and added to device_column_to_image_impl, device_grouped_conv_fwd_multiple_abd_xdl_cshuffle, device_grouped_conv_fwd_multiple_abd_xdl_cshuffle_v3, device_image_to_column_impl * replaced standard header usage and added header guards in block to ctile map and gridwise_gemm_pipeline_selector * resolved errors in device_gemm_xdl_splitk_c_shuffle files in regards to replacement of standard headers in previous commit * added replicated functionality for standard header methods in utility files * replaced standard header functionality in threadwise tensor slice transfer files and added header guards in element_wise_operation.hpp * temp fix for namespace error in MIOpen * remove standard header usage in codegen device op * removed standard header usage in elementwise files, resolved namespace errors * formatting fix * changed codegen argument to ON for testing * temporarily removing codegen compiler flag for testing purposes * added codegen flag again, set default to ON * set codegen flag default back to OFF * replaced enable_if_t standard header usage in data_type.hpp * added some debug prints to pinpoint issues in MIOpen * added print outs to debug in MIOpen * removed debug print outs from device op * resolved stdexcept include error * formatting fix * adding includes to new fp8 file to resolve ck::enable_if_t errors * made changes to amd_wave_read_first_lane * updated functionality in type utility file * fixed end of file issue * resovled errors in type utility file, added functionality to array utility file * fixed standard header usage replication in data_type file, resolves error with failing examples on navi3x * formatting fix * replaced standard header usage in amd_ck_fp8 file * added include to random_gen file * removed and replicated standard header usage from data_type and type_convert files for fp8 changes * replicated standard unsigned integer types in random_gen * resolved comments from review: put calls to reinterpret_cast for size_t in header guards * updated/added copyright headers * removed duplicate header * fixed typo in header guard * updated copyright headers --------- Co-authored-by: Illia Silin <98187287+illsilin@users.noreply.github.com>
194 lines
6.2 KiB
C++
194 lines
6.2 KiB
C++
// SPDX-License-Identifier: MIT
|
|
// Copyright (c) 2024-2025, Advanced Micro Devices, Inc. All rights reserved.
|
|
|
|
#include "ck/host/device_gemm_multiple_d/problem.hpp"
|
|
#include "ck/host/device_gemm_multiple_d/operation.hpp"
|
|
#include "ck/host/headers.hpp"
|
|
#include "ck/host/stringutils.hpp"
|
|
#include "ck/host/utils.hpp"
|
|
#include <algorithm>
|
|
#include <cmath>
|
|
#include <iterator>
|
|
#include <random>
|
|
#include <test.hpp>
|
|
#include <rtc/compile_kernel.hpp>
|
|
#include <rtc/hip.hpp>
|
|
#include <fstream>
|
|
|
|
using half = _Float16;
|
|
// using half = __fp16;
|
|
|
|
std::vector<rtc::src_file> get_headers_for_test()
|
|
{
|
|
std::vector<rtc::src_file> result;
|
|
auto hs = ck::host::GetHeaders();
|
|
std::transform(
|
|
hs.begin(), hs.end(), std::back_inserter(result), [&](const auto& p) -> rtc::src_file {
|
|
return {p.first, p.second};
|
|
});
|
|
return result;
|
|
}
|
|
|
|
template <class T>
|
|
rtc::buffer<T> generate_buffer(std::size_t n, std::size_t seed = 0)
|
|
{
|
|
rtc::buffer<T> result(n);
|
|
std::mt19937 gen(seed);
|
|
std::uniform_real_distribution<double> dis(-1.0);
|
|
std::generate(result.begin(), result.end(), [&] { return dis(gen); });
|
|
return result;
|
|
}
|
|
|
|
template <class T, class U>
|
|
bool allclose(const T& a, const U& b, double atol = 0.01, double rtol = 0.01)
|
|
{
|
|
return std::equal(a.begin(), a.end(), b.begin(), b.end(), [&](double x, double y) {
|
|
return fabs(x - y) < atol + rtol * fabs(y);
|
|
});
|
|
}
|
|
|
|
std::string classify(double x)
|
|
{
|
|
switch(std::fpclassify(x))
|
|
{
|
|
case FP_INFINITE: return "inf";
|
|
case FP_NAN: return "nan";
|
|
case FP_NORMAL: return "normal";
|
|
case FP_SUBNORMAL: return "subnormal";
|
|
case FP_ZERO: return "zero";
|
|
default: return "unknown";
|
|
}
|
|
}
|
|
|
|
template <class Buffer>
|
|
void print_classification(const Buffer& x)
|
|
{
|
|
std::unordered_set<std::string> result;
|
|
for(const auto& i : x)
|
|
result.insert(classify(i));
|
|
for(const auto& c : result)
|
|
std::cout << c << ", ";
|
|
std::cout << std::endl;
|
|
}
|
|
|
|
template <class Buffer>
|
|
void print_statistics(const Buffer& x)
|
|
{
|
|
std::cout << "Min value: " << *std::min_element(x.begin(), x.end()) << ", ";
|
|
std::cout << "Max value: " << *std::max_element(x.begin(), x.end()) << ", ";
|
|
double num_elements = x.size();
|
|
auto mean =
|
|
std::accumulate(x.begin(), x.end(), double{0.0}, std::plus<double>{}) / num_elements;
|
|
auto stddev = std::sqrt(
|
|
std::accumulate(x.begin(),
|
|
x.end(),
|
|
double{0.0},
|
|
[&](double r, double v) { return r + std::pow((v - mean), 2.0); }) /
|
|
num_elements);
|
|
std::cout << "Mean: " << mean << ", ";
|
|
std::cout << "StdDev: " << stddev << "\n";
|
|
}
|
|
|
|
template <class Buffer>
|
|
void print_preview(const Buffer& x)
|
|
{
|
|
if(x.size() <= 10)
|
|
{
|
|
std::for_each(x.begin(), x.end(), [&](double i) { std::cout << i << ", "; });
|
|
}
|
|
else
|
|
{
|
|
std::for_each(x.begin(), x.begin() + 5, [&](double i) { std::cout << i << ", "; });
|
|
std::cout << "..., ";
|
|
std::for_each(x.end() - 5, x.end(), [&](double i) { std::cout << i << ", "; });
|
|
}
|
|
std::cout << std::endl;
|
|
}
|
|
|
|
template <class T>
|
|
struct check_all
|
|
{
|
|
rtc::buffer<T> data{};
|
|
bool operator()(const rtc::buffer<T>& x)
|
|
{
|
|
if(data.empty())
|
|
{
|
|
data = x;
|
|
return true;
|
|
}
|
|
if(std::any_of(x.begin(), x.end(), [](double y) { return std::isnan(y); }))
|
|
return false;
|
|
return allclose(data, x);
|
|
}
|
|
};
|
|
|
|
template <class Solution>
|
|
auto report(const Solution& solution, bool pass)
|
|
{
|
|
return test::make_predicate(solution.ToTemplateString(), [=] { return pass; });
|
|
}
|
|
|
|
const std::string gemm_compile_check = R"__ck__(
|
|
#include <${include}>
|
|
|
|
extern "C" __global__ void f(const ck::half_t* a, const ck::half_t* b, ck::half_t* c) {
|
|
using G = ${template};
|
|
constexpr auto desc = ${template}::make_descriptor(ck::make_naive_tensor_descriptor_packed(ck::make_tuple(${m}, ${k})),
|
|
ck::make_naive_tensor_descriptor(ck::make_tuple(${n}, ${k}), ck::make_tuple(1, ${n})),
|
|
ck::make_tuple(),
|
|
ck::make_naive_tensor_descriptor_packed(ck::make_tuple(${m}, ${n})));
|
|
|
|
static_assert(desc.IsValid(), "Invalid ck gemm.");
|
|
|
|
if constexpr(desc.IsValid())
|
|
{
|
|
${template}::Run(desc,
|
|
a,
|
|
b,
|
|
ck::make_tuple(),
|
|
c);
|
|
}
|
|
}
|
|
|
|
)__ck__";
|
|
|
|
TEST_CASE(test_problem_kernel)
|
|
{
|
|
ck::host::device_gemm_multiple_d::Problem prob;
|
|
prob.M = 1024;
|
|
prob.N = 1024;
|
|
prob.K = 1024;
|
|
check_all<half> check;
|
|
auto a = to_gpu(generate_buffer<half>(1024 * 1024, 0));
|
|
auto b = to_gpu(generate_buffer<half>(1024 * 1024, 1));
|
|
auto c = to_gpu(generate_buffer<half>(1024 * 1024, 2));
|
|
|
|
std::string epilogue = "";
|
|
std::string prologue = "";
|
|
|
|
for(auto solution : prob.GetSolutions("gfx90a", prologue, epilogue))
|
|
{
|
|
auto src = ck::host::InterpolateString(gemm_compile_check,
|
|
{{"include", prob.GetIncludeHeader()},
|
|
{"template", solution.ToTemplateString()},
|
|
{"m", std::to_string(prob.M)},
|
|
{"n", std::to_string(prob.N)},
|
|
{"k", std::to_string(prob.K)}});
|
|
auto srcs = get_headers_for_test();
|
|
srcs.push_back({"main.cpp", src});
|
|
rtc::compile_options options;
|
|
options.kernel_name = "f";
|
|
auto k = rtc::compile_kernel(srcs, options);
|
|
auto block_size = solution.GetTemplateParameter<std::size_t>("BlockSize");
|
|
auto m_per_block = solution.GetTemplateParameter<std::size_t>("MPerBlock");
|
|
auto n_per_block = solution.GetTemplateParameter<std::size_t>("NPerBlock");
|
|
auto grid_size = ck::host::integer_divide_ceil(prob.M, m_per_block) *
|
|
ck::host::integer_divide_ceil(prob.N, n_per_block);
|
|
k.launch(nullptr, grid_size * block_size, block_size)(a.data(), b.data(), c.data());
|
|
|
|
CHECK(report(solution, check(rtc::from_gpu(c))));
|
|
}
|
|
}
|
|
|
|
int main(int argc, const char* argv[]) { test::run(argc, argv); }
|