From a76956607c8efa841f9b009a65c9bb6a8c3385ed Mon Sep 17 00:00:00 2001 From: ROCm CI Service Account <66695075+rocm-ci@users.noreply.github.com> Date: Thu, 26 Jan 2023 21:50:05 +0530 Subject: [PATCH] SWDEV-366060 - Adding tests for __hmax/hmin device functions (#125) Change-Id: I4a34f15168bdedaaaaa247faa7a3e7fce34f89d5 --- catch/unit/deviceLib/CMakeLists.txt | 1 + catch/unit/deviceLib/hmax_hmin.cc | 264 ++++++++++++++++++++++++++++ 2 files changed, 265 insertions(+) create mode 100644 catch/unit/deviceLib/hmax_hmin.cc diff --git a/catch/unit/deviceLib/CMakeLists.txt b/catch/unit/deviceLib/CMakeLists.txt index c54cd68c08..02b542e71e 100644 --- a/catch/unit/deviceLib/CMakeLists.txt +++ b/catch/unit/deviceLib/CMakeLists.txt @@ -28,6 +28,7 @@ set(AMD_TEST_SRC bitInsert.cc floatTM.cc hipMathFunctions.cc + hmax_hmin.cc ) set(AMD_ARCH_SPEC_TEST_SRC AtomicAdd_Coherent_withunsafeflag.cc diff --git a/catch/unit/deviceLib/hmax_hmin.cc b/catch/unit/deviceLib/hmax_hmin.cc new file mode 100644 index 0000000000..4f642616c3 --- /dev/null +++ b/catch/unit/deviceLib/hmax_hmin.cc @@ -0,0 +1,264 @@ +/* +Copyright (c) 2022 Advanced Micro Devices, Inc. All rights reserved. + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in +all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN +THE SOFTWARE. +*/ + +#include +#include +#include +#include + +#define NElms 5 + + +// Kernel functions +__global__ void HMinMaxHalfOps(__half x, __half y, __half ExptdResult, + int *TstResult, int TstToRun) { + __half OutVal = 0; + if (TstToRun == 1) { + OutVal = __hmax(x, y); + if (!(__heq(OutVal, ExptdResult))) { + *TstResult = 0; // Indicates Test failed + } + } else if (TstToRun == 2) { + OutVal = __hmin(x, y); + if (!(__heq(OutVal, ExptdResult))) { + *TstResult = 0; // Indicates Test failed + } + } else if (TstToRun == 3) { + OutVal = __hmax_nan(x, y); + if ((__hisnan(ExptdResult))) { + if (!(__hisnan(OutVal))) { + *TstResult = 0; // Indicates Test failed + } + } else { + if (!(__heq(OutVal, ExptdResult))) { + *TstResult = 0; // Indicates Test failed + } + } + } else if (TstToRun == 4) { + OutVal = __hmin_nan(x, y); + if ((__hisnan(ExptdResult))) { + if (!(__hisnan(OutVal))) { + *TstResult = 0; // Indicates Test failed + } + } else { + if (!(__heq(OutVal, ExptdResult))) { + *TstResult = 0; // Indicates Test failed + } + } + } +} + +__global__ void HMinMaxHalfOpsArray(__half *x, __half *y, __half *ExptdResult, + int *TstResult, int TstToRun) { + size_t offset = (blockIdx.x * blockDim.x + threadIdx.x); + if (offset < NElms) { + __half OutVal = 0; + if (TstToRun == 1) { + OutVal = __hmax(x[offset], y[offset]); + if (!(__heq(OutVal, ExptdResult[offset]))) { + *TstResult = 0; // Indicates Test failed + } + } else if (TstToRun == 2) { + OutVal = __hmin(x[offset], y[offset]); + if (!(__heq(OutVal, ExptdResult[offset]))) { + *TstResult = 0; // Indicates Test failed + } + } + } +} + +// The following tests checks the basic functionality of __hmax(), __hmin() +// __hmax_nan() and __hmin_nan() +TEST_CASE("Unit_hmax_hmin_Tsts") { + int *Hptr = nullptr, *Dptr = nullptr; + HIP_CHECK(hipHostMalloc(&Hptr, sizeof(int))); + *Hptr = 1; + HIP_CHECK(hipHostGetDevicePointer(reinterpret_cast(&Dptr), Hptr, 0)); + + __half x = GENERATE(0, 0.1, 1.5); + __half y = GENERATE(1.5, 2, 10); + SECTION("Running test for __hmax()") { + HMinMaxHalfOps<<<1, 1>>>(x, y, y, Dptr, 1); + HIP_CHECK(hipStreamSynchronize(0)); + if (*Hptr == 0) { + REQUIRE(false); + } + HMinMaxHalfOps<<<1, 1>>>(y, x, y, Dptr, 1); + HIP_CHECK(hipStreamSynchronize(0)); + if (*Hptr == 0) { + REQUIRE(false); + } + } + + *Hptr = 1; + SECTION("Running test for __hmin()") { + HMinMaxHalfOps<<<1, 1>>>(x, y, x, Dptr, 2); + HIP_CHECK(hipStreamSynchronize(0)); + if (*Hptr == 0) { + REQUIRE(false); + } + HMinMaxHalfOps<<<1, 1>>>(y, x, x, Dptr, 2); + HIP_CHECK(hipStreamSynchronize(0)); + if (*Hptr == 0) { + REQUIRE(false); + } + } + + *Hptr = 1; + SECTION("Running test for __hmax_nan()") { + HMinMaxHalfOps<<<1, 1>>>(x, nan("1"), nan("1"), Dptr, 3); + HIP_CHECK(hipStreamSynchronize(0)); + + if (*Hptr == 0) { + REQUIRE(false); + } + + HMinMaxHalfOps<<<1, 1>>>(nan("1"), x, nan("1"), Dptr, 3); + HIP_CHECK(hipStreamSynchronize(0)); + + if (*Hptr == 0) { + REQUIRE(false); + } + + HMinMaxHalfOps<<<1, 1>>>(nan("1"), nan("1"), nan("1"), Dptr, 3); + HIP_CHECK(hipStreamSynchronize(0)); + + if (*Hptr == 0) { + REQUIRE(false); + } + + HMinMaxHalfOps<<<1, 1>>>(x, y, y, Dptr, 3); + HIP_CHECK(hipStreamSynchronize(0)); + + if (*Hptr == 0) { + REQUIRE(false); + } + + HMinMaxHalfOps<<<1, 1>>>(y, x, y, Dptr, 3); + HIP_CHECK(hipStreamSynchronize(0)); + + if (*Hptr == 0) { + REQUIRE(false); + } + } + + *Hptr = 1; + SECTION("Running test for __hmin_nan()") { + HMinMaxHalfOps<<<1, 1>>>(x, nan("1"), nan("1"), Dptr, 4); + HIP_CHECK(hipStreamSynchronize(0)); + + if (*Hptr == 0) { + REQUIRE(false); + } + + HMinMaxHalfOps<<<1, 1>>>(nan("1"), x, nan("1"), Dptr, 4); + HIP_CHECK(hipStreamSynchronize(0)); + + if (*Hptr == 0) { + REQUIRE(false); + } + + HMinMaxHalfOps<<<1, 1>>>(nan("1"), nan("1"), nan("1"), Dptr, 4); + HIP_CHECK(hipStreamSynchronize(0)); + + if (*Hptr == 0) { + REQUIRE(false); + } + + HMinMaxHalfOps<<<1, 1>>>(x, y, x, Dptr, 4); + HIP_CHECK(hipStreamSynchronize(0)); + + if (*Hptr == 0) { + REQUIRE(false); + } + + HMinMaxHalfOps<<<1, 1>>>(y, x, x, Dptr, 4); + HIP_CHECK(hipStreamSynchronize(0)); + + if (*Hptr == 0) { + REQUIRE(false); + } + } + HIP_CHECK(hipHostFree(Hptr)); +} + + +// The following Tests does negative testing by passing nan +TEST_CASE("Unit_hmax_hmin_Tsts_Negative") { + int *Hptr = nullptr, *Dptr = nullptr; + HIP_CHECK(hipHostMalloc(&Hptr, sizeof(int))); + *Hptr = 1; + HIP_CHECK(hipHostGetDevicePointer(reinterpret_cast(&Dptr), Hptr, 0)); + __half x = nan(""); + __half y = 1.5; + SECTION("Running Negative test for __hmax()") { + HMinMaxHalfOps<<<1, 1>>>(x, y, y, Dptr, 1); + HIP_CHECK(hipStreamSynchronize(0)); + if (*Hptr == 0) { + REQUIRE(false); + } + } + + SECTION("Running Negative test for __hmin()") { + HMinMaxHalfOps<<<1, 1>>>(x, y, y, Dptr, 2); + HIP_CHECK(hipStreamSynchronize(0)); + if (*Hptr == 0) { + REQUIRE(false); + } + } + + HIP_CHECK(hipHostFree(Hptr)); +} + +// The following tests the __hmax/min functions over array of memory +TEST_CASE("Unit_hmax_hmin_Tsts_With_Array") { + int *Hptr = nullptr, *Dptr = nullptr; + __half *Ad = nullptr, *Bd = nullptr; + HIP_CHECK(hipHostMalloc(&Hptr, sizeof(int))); + *Hptr = 1; + HIP_CHECK(hipHostGetDevicePointer(reinterpret_cast(&Dptr), Hptr, 0)); + __half x[NElms] = {1, 2, 3, 4, 5}; + __half y[NElms] = {7, 8, 9, 10, 11}; + HIP_CHECK(hipMalloc(&Ad, NElms * sizeof(__half))); + HIP_CHECK(hipMalloc(&Bd, NElms * sizeof(__half))); + HIP_CHECK(hipMemcpy(Ad, x, NElms * sizeof(__half), hipMemcpyHostToDevice)); + HIP_CHECK(hipMemcpy(Bd, y, NElms * sizeof(__half), hipMemcpyHostToDevice)); + SECTION("Running test for __hmax() with an array") { + HMinMaxHalfOpsArray<<<1, NElms>>>(Ad, Bd, Bd, Dptr, 1); + HIP_CHECK(hipStreamSynchronize(0)); + if (*Hptr == 0) { + REQUIRE(false); + } + } + + SECTION("Running test for __hmin() with an array") { + HMinMaxHalfOpsArray<<<1, NElms>>>(Ad, Bd, Ad, Dptr, 2); + HIP_CHECK(hipStreamSynchronize(0)); + if (*Hptr == 0) { + REQUIRE(false); + } + } + + HIP_CHECK(hipHostFree(Hptr)); + HIP_CHECK(hipFree(Ad)); + HIP_CHECK(hipFree(Bd)); +}