/* * Copyright (c) Advanced Micro Devices, Inc., or its affiliates. * * SPDX-License-Identifier: MIT */ #pragma once #include "math_common.hh" #include "math_special_values.hh" #include namespace cg = cooperative_groups; #define MATH_BINARY_KERNEL_DEF(func_name) \ template \ __global__ void func_name##_kernel(RT* const ys, const size_t num_xs, T* const x1s, \ T* const x2s) { \ const auto tid = cg::this_grid().thread_rank(); \ const auto stride = cg::this_grid().size(); \ \ for (size_t i = tid; i < num_xs; i += stride) { \ if constexpr (std::is_same_v) { \ ys[i] = func_name##f(x1s[i], x2s[i]); \ } else if constexpr (std::is_same_v) { \ ys[i] = func_name(x1s[i], x2s[i]); \ } \ } \ } template void BinaryFloatingPointBruteForceTest(kernel_sig kernel, ref_sig ref_func, const ValidatorBuilder& validator_builder, const TArg a = std::numeric_limits::lowest(), const TArg b = std::numeric_limits::max()) { const auto [grid_size, block_size] = GetOccupancyMaxPotentialBlockSize(kernel); const uint64_t num_iterations = GetTestIterationCount(); const auto max_batch_size = std::min(GetMaxAllowedDeviceMemoryUsage() / (sizeof(TArg) * 2 + sizeof(T)), num_iterations); LinearAllocGuard x1s{LinearAllocs::hipHostMalloc, max_batch_size * sizeof(TArg)}; LinearAllocGuard x2s{LinearAllocs::hipHostMalloc, max_batch_size * sizeof(TArg)}; MathTest math_test(kernel, max_batch_size); auto batch_size = max_batch_size; const auto num_threads = thread_pool.thread_count(); for (uint64_t i = 0ul; i < num_iterations; i += batch_size) { batch_size = std::min(max_batch_size, num_iterations - i); const auto min_sub_batch_size = batch_size / num_threads; const auto tail = batch_size % num_threads; auto base_idx = 0u; for (auto i = 0u; i < num_threads; ++i) { const auto sub_batch_size = min_sub_batch_size + (i < tail); thread_pool.Post([=, &x1s, &x2s] { const auto generator = [=] { static thread_local std::mt19937 rng(std::random_device{}()); if constexpr (std::is_same_v) { std::uniform_real_distribution> unif_dist(-FLOAT16_MAX, FLOAT16_MAX); return static_cast(unif_dist(rng)); } else { std::uniform_real_distribution> unif_dist(a, b); return static_cast(unif_dist(rng)); } }; std::generate(x1s.ptr() + base_idx, x1s.ptr() + base_idx + sub_batch_size, generator); std::generate(x2s.ptr() + base_idx, x2s.ptr() + base_idx + sub_batch_size, generator); }); base_idx += sub_batch_size; } thread_pool.Wait(); math_test.Run(validator_builder, grid_size, block_size, ref_func, batch_size, x1s.ptr(), x2s.ptr()); } } template void BinaryFloatingPointSpecialValuesTest(kernel_sig kernel, ref_sig ref_func, const ValidatorBuilder& validator_builder) { const auto [grid_size, block_size] = GetOccupancyMaxPotentialBlockSize(kernel); using SpecialValsType = std::conditional_t, float, TArg>; const auto values = std::get>(kSpecialValRegistry); const auto size = values.size * values.size; LinearAllocGuard x1s{LinearAllocs::hipHostMalloc, size * sizeof(TArg)}; LinearAllocGuard x2s{LinearAllocs::hipHostMalloc, size * sizeof(TArg)}; for (auto i = 0u; i < values.size; ++i) { for (auto j = 0u; j < values.size; ++j) { x1s.ptr()[i * values.size + j] = values.data[i]; x2s.ptr()[i * values.size + j] = values.data[j]; } } MathTest math_test(kernel, size); math_test.template Run(validator_builder, grid_size, block_size, ref_func, size, x1s.ptr(), x2s.ptr()); } template void BinaryFloatingPointTest(kernel_sig kernel, ref_sig ref_func, const ValidatorBuilder& validator_builder) { SECTION("Special values") { BinaryFloatingPointSpecialValuesTest(kernel, ref_func, validator_builder); } SECTION("Brute force") { BinaryFloatingPointBruteForceTest(kernel, ref_func, validator_builder); } } #define MATH_BINARY_WITHIN_ULP_TEST_DEF(kern_name, ref_func, sp_ulp, dp_ulp) \ MATH_BINARY_KERNEL_DEF(kern_name) \ \ HIP_TEMPLATE_TEST_CASE(Unit_Device_##kern_name##_Accuracy_Positive, float, double) { \ using RT = RefType_t; \ RT (*ref)(RT, RT) = ref_func; \ const auto ulp = std::is_same_v ? sp_ulp : dp_ulp; \ \ BinaryFloatingPointTest(kern_name##_kernel, ref, \ ULPValidatorBuilderFactory(ulp)); \ }