/* * Copyright (c) Advanced Micro Devices, Inc., or its affiliates. * * SPDX-License-Identifier: MIT */ #pragma once #include "unary_common.hh" #include namespace cg = cooperative_groups; #define CAST_KERNEL_DEF(func_name, T1, T2) \ __global__ void func_name##_kernel(T1* const ys, const size_t num_xs, T2* const xs) { \ 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) { \ ys[i] = func_name(xs[i]); \ } \ } #define CAST_BINARY_KERNEL_DEF(func_name, T1, T2) \ __global__ void func_name##_kernel(T1* const ys, const size_t num_xs, T2* const x1s, \ T2* 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) { \ ys[i] = func_name(x1s[i], x2s[i]); \ } \ } #define CAST_F2I_REF_DEF(func_name, T1, T2, ref_func) \ T1 func_name##_ref(T2 arg) { \ if (arg >= static_cast(std::numeric_limits::max())) \ return std::numeric_limits::max(); \ else if (arg <= static_cast(std::numeric_limits::min())) \ return std::numeric_limits::min(); \ T2 result = ref_func(arg); \ return result; \ } #define CAST_F2I_RZ_REF_DEF(func_name, T1, T2) \ T1 func_name##_ref(T2 arg) { \ if (arg >= static_cast(std::numeric_limits::max())) \ return std::numeric_limits::max(); \ else if (arg <= static_cast(std::numeric_limits::min())) \ return std::numeric_limits::min(); \ T1 result = static_cast(arg); \ return result; \ } #define CAST_RND_REF_DEF(func_name, T1, T2, round_dir) \ T1 func_name##_ref(T2 arg) { \ int curr_direction = fegetround(); \ fesetround(round_dir); \ T1 result = static_cast(arg); \ fesetround(curr_direction); \ return result; \ } #define CAST_REF_DEF(func_name, T1, T2) \ T1 func_name##_ref(T2 arg) { \ T1 result = static_cast(arg); \ return result; \ } template T1 type2_as_type1_ref(T2 arg) { T1 tmp; memcpy(&tmp, &arg, sizeof(tmp)); return tmp; } template void CastUnaryHalfPrecisionBruteForceTest(kernel_sig kernel, ref_sig ref_func, const ValidatorBuilder& validator_builder) { const auto [grid_size, block_size] = GetOccupancyMaxPotentialBlockSize(kernel); const auto reduction_factor = GetTestReductionFactor(); const auto inv_reduction_factor = 1 / reduction_factor; const auto stop = static_cast( std::ceil((std::numeric_limits::max() + 1ul) * reduction_factor)); const auto max_batch_size = std::min(GetMaxAllowedDeviceMemoryUsage() / (sizeof(Float16) + sizeof(T)), stop); LinearAllocGuard values{LinearAllocs::hipHostMalloc, max_batch_size * sizeof(Float16)}; MathTest math_test(kernel, max_batch_size); auto batch_size = max_batch_size; const auto num_threads = thread_pool.thread_count(); for (uint64_t v = 0u; v < stop;) { batch_size = std::min(max_batch_size, stop - v); 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([=, &values] { auto t = v * inv_reduction_factor; uint16_t val; for (auto j = 0u; j < sub_batch_size; ++j) { val = static_cast(std::floor(t)); t += inv_reduction_factor; values.ptr()[base_idx + j] = *reinterpret_cast(&val); if (std::isnan(values.ptr()[base_idx + j]) || std::isinf(values.ptr()[base_idx + j])) { values.ptr()[base_idx + j] = 0; } } }); v += sub_batch_size; base_idx += sub_batch_size; } thread_pool.Wait(); math_test.Run(validator_builder, grid_size, block_size, ref_func, batch_size, values.ptr()); } } template void CastUnaryHalfPrecisionTest(kernel_sig kernel, ref_sig ref, const ValidatorBuilder& validator_builder) { SECTION("Brute force") { CastUnaryHalfPrecisionBruteForceTest(kernel, ref, validator_builder); } } template void CastDoublePrecisionSpecialValuesTest(kernel_sig kernel, ref_sig ref_func, const ValidatorBuilder& validator_builder) { const auto [grid_size, block_size] = GetOccupancyMaxPotentialBlockSize(kernel); const auto values = std::get>(kSpecialValRegistry); std::vector spec_values; if (!std::is_same_v && !std::is_same_v && !std::is_same_v) { for (int i = 0; i < values.size; i++) { if (!std::isnan(values.data[i]) && !std::isinf(values.data[i])) { spec_values.push_back(values.data[i]); } } } MathTest math_test(kernel, spec_values.size()); math_test.template Run(validator_builder, grid_size, block_size, ref_func, spec_values.size(), spec_values.data()); } template void CastDoublePrecisionTest(kernel_sig kernel, ref_sig ref, const ValidatorBuilder& validator_builder) { SECTION("Special values") { CastDoublePrecisionSpecialValuesTest(kernel, ref, validator_builder); } SECTION("Brute force") { UnaryDoublePrecisionBruteForceTest(kernel, ref, validator_builder); } } template void CastIntRangeTest(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 auto reduction_factor = GetTestReductionFactor(); const auto inv_reduction_factor = 1 / reduction_factor; const auto max_batch_size = GetMaxAllowedDeviceMemoryUsage() / (sizeof(T) + sizeof(TArg)); LinearAllocGuard values{LinearAllocs::hipHostMalloc, max_batch_size * sizeof(TArg)}; MathTest math_test(kernel, max_batch_size); bool running = true; size_t inserted = 0u; auto v = static_cast(a); while (running) { if (std::floor(v) > b) { running = false; } else { values.ptr()[inserted++] = static_cast(std::floor(v)); v += inv_reduction_factor; if (inserted < max_batch_size) continue; } math_test.Run(validator_builder, grid_size, block_size, ref_func, inserted, values.ptr()); inserted = 0u; } } template void CastIntBruteForceTest(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(T) + sizeof(TArg)), num_iterations); LinearAllocGuard values{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([=, &values] { const auto generator = [=] { static thread_local std::mt19937 rng(std::random_device{}()); std::uniform_int_distribution unif_dist(a, b); return static_cast(unif_dist(rng)); }; std::generate(values.ptr() + base_idx, values.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, values.ptr()); } } template void CastBinaryIntRangeTest(kernel_sig kernel, ref_sig ref_func, const ValidatorBuilder& validator_builder, const T2 a = std::numeric_limits::lowest(), const T2 b = std::numeric_limits::max()) { const auto [grid_size, block_size] = GetOccupancyMaxPotentialBlockSize(kernel); const auto reduction_factor = GetTestReductionFactor(); const auto inv_reduction_factor = 1 / reduction_factor; const auto max_batch_size = GetMaxAllowedDeviceMemoryUsage() / (sizeof(T1) + 2 * sizeof(T2)); LinearAllocGuard values1{LinearAllocs::hipHostMalloc, max_batch_size * sizeof(T2)}; LinearAllocGuard values2{LinearAllocs::hipHostMalloc, max_batch_size * sizeof(T2)}; MathTest math_test(kernel, max_batch_size); bool running = true; size_t inserted = 0u; auto v = static_cast(a); while (running) { if (std::floor(v) > b) { running = false; } else { const auto t = static_cast(std::floor(v)); values1.ptr()[inserted] = t; values2.ptr()[inserted++] = b - t; v += inv_reduction_factor; if (inserted < max_batch_size) continue; } math_test.Run(validator_builder, grid_size, block_size, ref_func, inserted, values1.ptr(), values2.ptr()); inserted = 0u; } }