From 8f36cf01c4227ea0dce8ffad60aadfb4f3be4cdd Mon Sep 17 00:00:00 2001 From: Chuang-Yu Cheng Date: Thu, 20 Jun 2024 07:33:11 +0100 Subject: [PATCH 1/4] spirv_new: fix test_decorate to use the device's default rounding mode instead of CL_HALF_RTE for half conversion. --- test_conformance/spirv_new/test_decorate.cpp | 28 +++++++++++++++++--- 1 file changed, 25 insertions(+), 3 deletions(-) diff --git a/test_conformance/spirv_new/test_decorate.cpp b/test_conformance/spirv_new/test_decorate.cpp index 3a1f422aff..ca4d55c431 100644 --- a/test_conformance/spirv_new/test_decorate.cpp +++ b/test_conformance/spirv_new/test_decorate.cpp @@ -216,7 +216,8 @@ static inline Ti generate_saturated_rhs_input(RandomSeed &seed) } template -static inline To compute_saturated_output(Ti lhs, Ti rhs) +static inline To compute_saturated_output(Ti lhs, Ti rhs, + cl_half_rounding_mode half_rounding) { constexpr auto loVal = std::numeric_limits::min(); constexpr auto hiVal = std::numeric_limits::max(); @@ -226,7 +227,7 @@ static inline To compute_saturated_output(Ti lhs, Ti rhs) cl_float f = cl_half_to_float(lhs) * cl_half_to_float(rhs); // Quantize to fp16: - f = cl_half_to_float(cl_half_from_float(f, CL_HALF_RTE)); + f = cl_half_to_float(cl_half_from_float(f, half_rounding)); To val = (To)std::min(std::max(f, loVal), hiVal); if (isnan(cl_half_to_float(rhs))) @@ -246,6 +247,26 @@ static inline To compute_saturated_output(Ti lhs, Ti rhs) return val; } +static cl_half_rounding_mode get_half_rounding_mode(cl_device_id deviceID) +{ + const cl_device_fp_config fpConfigHalf = + get_default_rounding_mode(deviceID, CL_DEVICE_HALF_FP_CONFIG); + + if (fpConfigHalf == CL_FP_ROUND_TO_NEAREST) + { + return CL_HALF_RTE; + } + else if (fpConfigHalf == CL_FP_ROUND_TO_ZERO) + { + return CL_HALF_RTZ; + } + else + { + log_error("Error while acquiring half rounding mode"); + } + return CL_HALF_RTE; +} + template int verify_saturated_results(cl_device_id deviceID, cl_context context, cl_command_queue queue, const char *kname, @@ -305,7 +326,8 @@ int verify_saturated_results(cl_device_id deviceID, cl_context context, for (int i = 0; i < num; i++) { - To val = compute_saturated_output(h_lhs[i], h_rhs[i]); + To val = compute_saturated_output(h_lhs[i], h_rhs[i], + get_half_rounding_mode(deviceID)); if (val != h_res[i]) { From 48e696ce80ad00393daf6f8a4cd55e7baefb1732 Mon Sep 17 00:00:00 2001 From: Chuang-Yu Cheng Date: Thu, 20 Jun 2024 08:01:34 +0100 Subject: [PATCH 2/4] NFC: spirv_new - test_decorate refactoring. --- test_conformance/spirv_new/test_decorate.cpp | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/test_conformance/spirv_new/test_decorate.cpp b/test_conformance/spirv_new/test_decorate.cpp index ca4d55c431..b8012691e6 100644 --- a/test_conformance/spirv_new/test_decorate.cpp +++ b/test_conformance/spirv_new/test_decorate.cpp @@ -324,10 +324,11 @@ int verify_saturated_results(cl_device_id deviceID, cl_context context, err = clEnqueueReadBuffer(queue, res, CL_TRUE, 0, out_bytes, &h_res[0], 0, NULL, NULL); SPIRV_CHECK_ERROR(err, "Failed to read to output"); + cl_half_rounding_mode half_rounding = get_half_rounding_mode(deviceID); + for (int i = 0; i < num; i++) { - To val = compute_saturated_output(h_lhs[i], h_rhs[i], - get_half_rounding_mode(deviceID)); + To val = compute_saturated_output(h_lhs[i], h_rhs[i], half_rounding); if (val != h_res[i]) { From adf1ab6142f97ba85b93f4f2ffe62c485d5c9653 Mon Sep 17 00:00:00 2001 From: Chuang-Yu Cheng Date: Thu, 20 Jun 2024 08:23:38 +0100 Subject: [PATCH 3/4] NFC: spirv_new - test_decorate: fix format. --- test_conformance/spirv_new/test_decorate.cpp | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/test_conformance/spirv_new/test_decorate.cpp b/test_conformance/spirv_new/test_decorate.cpp index b8012691e6..b4b9b20237 100644 --- a/test_conformance/spirv_new/test_decorate.cpp +++ b/test_conformance/spirv_new/test_decorate.cpp @@ -328,7 +328,8 @@ int verify_saturated_results(cl_device_id deviceID, cl_context context, for (int i = 0; i < num; i++) { - To val = compute_saturated_output(h_lhs[i], h_rhs[i], half_rounding); + To val = compute_saturated_output(h_lhs[i], h_rhs[i], + half_rounding); if (val != h_res[i]) { From d5adddb67a31486073a4b55d583b3d1d96958d2d Mon Sep 17 00:00:00 2001 From: Chuang-Yu Cheng Date: Thu, 27 Jun 2024 02:14:34 +0100 Subject: [PATCH 4/4] NFC: spirv_new - test_decorate: only get_half_rounding_mode when Ti is half. --- test_conformance/spirv_new/test_decorate.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/test_conformance/spirv_new/test_decorate.cpp b/test_conformance/spirv_new/test_decorate.cpp index b4b9b20237..b85419300d 100644 --- a/test_conformance/spirv_new/test_decorate.cpp +++ b/test_conformance/spirv_new/test_decorate.cpp @@ -324,7 +324,11 @@ int verify_saturated_results(cl_device_id deviceID, cl_context context, err = clEnqueueReadBuffer(queue, res, CL_TRUE, 0, out_bytes, &h_res[0], 0, NULL, NULL); SPIRV_CHECK_ERROR(err, "Failed to read to output"); - cl_half_rounding_mode half_rounding = get_half_rounding_mode(deviceID); + cl_half_rounding_mode half_rounding = CL_HALF_RTE; + if (std::is_same::value) + { + half_rounding = get_half_rounding_mode(deviceID); + } for (int i = 0; i < num; i++) {