Copyright (c) 2025 Huawei Technologies Co., Ltd.
This program is free software, you can redistribute it and/or modify it under the terms and conditions of
CANN Open Software License Agreement Version 2.0 (the "License").
Please refer to the License for details. You may not use this file except in compliance with the License.
THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND, EITHER EXPRESS OR IMPLIED,
INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT, MERCHANTABILITY, OR FITNESS FOR A PARTICULAR PURPOSE.
See LICENSE in the root of the software repository for the full text of the License.
*/
#include "test_common.h"
#include "acl/acl.h"
#include <gtest/gtest.h>
using namespace std;
using namespace PtoTestCommon;
class TANDSTest : public testing::Test {
protected:
void SetUp() override
{}
void TearDown() override
{}
};
std::string GetGoldenDir()
{
const testing::TestInfo *testInfo = testing::UnitTest::GetInstance()->current_test_info();
const std::string caseName = testInfo->name();
std::string suiteName = testInfo->test_suite_name();
std::string fullPath = "../" + suiteName + "." + caseName;
return fullPath;
}
template <typename T, int kTRows_, int kTCols_, int vRows, int vCols>
void LaunchTAnds(T *out, T *src0, T src1, void *stream);
template <typename T, int kTRows_, int kTCols_, int vRows, int vCols>
void test_tands()
{
size_t fileSize = kTRows_ * kTCols_ * sizeof(T);
aclInit(nullptr);
aclrtSetDevice(0);
aclrtStream stream;
aclrtCreateStream(&stream);
T *dstHost, *src0Host;
T *dstDevice, *src0Device;
T src1;
aclrtMallocHost((void **)(&dstHost), fileSize);
aclrtMallocHost((void **)(&src0Host), fileSize);
aclrtMalloc((void **)&dstDevice, fileSize, ACL_MEM_MALLOC_HUGE_FIRST);
aclrtMalloc((void **)&src0Device, fileSize, ACL_MEM_MALLOC_HUGE_FIRST);
ReadFile(GetGoldenDir() + "/input1.bin", fileSize, src0Host, fileSize);
std::string scalar_file = GetGoldenDir() + "/input2.bin";
std::ifstream file(scalar_file, std::ios::binary);
file.read(reinterpret_cast<char *>(&src1), sizeof(T));
file.close();
aclrtMemcpy(src0Device, fileSize, src0Host, fileSize, ACL_MEMCPY_HOST_TO_DEVICE);
LaunchTAnds<T, kTRows_, kTCols_, vRows, vCols>(dstDevice, src0Device, src1, stream);
aclrtSynchronizeStream(stream);
aclrtMemcpy(dstHost, fileSize, dstDevice, fileSize, ACL_MEMCPY_DEVICE_TO_HOST);
WriteFile(GetGoldenDir() + "/output.bin", dstHost, fileSize);
aclrtFree(dstDevice);
aclrtFree(src0Device);
aclrtFreeHost(dstHost);
aclrtFreeHost(src0Host);
aclrtDestroyStream(stream);
aclrtResetDevice(0);
aclFinalize();
std::vector<T> golden(fileSize);
std::vector<T> devFinal(fileSize);
ReadFile(GetGoldenDir() + "/golden.bin", fileSize, golden.data(), fileSize);
ReadFile(GetGoldenDir() + "/output.bin", fileSize, devFinal.data(), fileSize);
bool ret = ResultCmp<T>(golden, devFinal, 0.001f);
EXPECT_TRUE(ret);
}
TEST_F(TANDSTest, case1)
{
test_tands<uint16_t, 64, 64, 64, 64>();
}
TEST_F(TANDSTest, case2)
{
test_tands<uint16_t, 64, 64, 63, 63>();
}
TEST_F(TANDSTest, case3)
{
test_tands<uint16_t, 1, 16384, 1, 16384>();
}
TEST_F(TANDSTest, case4)
{
test_tands<uint16_t, 2048, 16, 2048, 16>();
}
TEST_F(TANDSTest, case5)
{
test_tands<uint8_t, 32, 32, 32, 32>();
}
TEST_F(TANDSTest, case6)
{
test_tands<uint32_t, 8, 8, 8, 8>();
}
TEST_F(TANDSTest, case7)
{
test_tands<int8_t, 32, 32, 32, 32>();
}
TEST_F(TANDSTest, case8)
{
test_tands<int16_t, 16, 16, 16, 16>();
}
TEST_F(TANDSTest, case9)
{
test_tands<int32_t, 8, 8, 8, 8>();
}
TEST_F(TANDSTest, case10)
{
test_tands<int32_t, 1, 8, 1, 8>();
}