* 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 <vector>
#include <cstdint>
#include <cmath>
#include <random>
#include <iostream>
#include <algorithm>
#include "acl/acl.h"
#include "elewise/host/elewise_host.h"
#include "elewise/kernel/elewise_kernel.h"
#include "example_common.h"
namespace {
using SinhOpTraits = ATVC::OpTraits<ATVC::OpInputs<float>, ATVC::OpOutputs<float>, ATVC::OpTemps<float, float>>;
template<class Traits>
struct SinhComputeFunc {
template<typename T, typename U>
__aicore__ inline void operator()(AscendC::LocalTensor<T> x,
AscendC::LocalTensor<T> y,
AscendC::LocalTensor<U> tempBuffer1,
AscendC::LocalTensor<U> tempBuffer2)
{
uint32_t tiledCnt = y.GetSize();
AscendC::Muls(tempBuffer1, x, static_cast<T>(-1), tiledCnt);
AscendC::Exp(tempBuffer1, tempBuffer1, tiledCnt);
AscendC::Exp(tempBuffer2, x, tiledCnt);
AscendC::Sub(y, tempBuffer2, tempBuffer1, tiledCnt);
AscendC::Muls(y, y, static_cast<T>(0.5), tiledCnt);
}
};
void InitializeData(int32_t eleNum, std::vector<float> &inputX, std::vector<float> &golden)
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<float> dis(1.0f, 10.0f);
for (int i = 0; i < eleNum; ++i) {
inputX[i] = dis(gen);
golden[i] = std::sinh(inputX[i]);
}
}
}
* 该函数为SinhCustom算子核函数入口
* x Device上的gm地址,指向SinhCustom算子第一个输入
* y Device上的gm地址,指向SinhCustom算子第一个输出
* param Device上的gm地址,指向运行态ATVC::EleWiseParam数据
*/
template<class OpTraits>
__global__ __aicore__ void SinhCustom(GM_ADDR x, GM_ADDR y, ATVC::EleWiseParam param)
{
KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_AIV_ONLY);
auto op = ATVC::Kernel::EleWiseOpTemplate<SinhComputeFunc<OpTraits>>();
op.Run(x, y, ¶m);
}
int main()
{
int32_t eleNum = 8 * 2048;
size_t inputByteSize = static_cast<size_t>(eleNum) * sizeof(float);
size_t outputByteSize = static_cast<size_t>(eleNum) * sizeof(float);
std::vector<float> inputX(eleNum);
std::vector<float> golden(eleNum);
InitializeData(eleNum, inputX, golden);
ATVC::EleWiseParam param;
if (!ATVC::Host::CalcEleWiseTiling<SinhOpTraits>(eleNum, param)) {
printf("Elewise tiling error.");
return -1;
};
aclrtContext context;
aclrtStream stream = nullptr;
int32_t deviceId = 0;
InitializeACL(context, stream, deviceId);
uint8_t *yHost;
uint8_t *xDevice;
uint8_t *yDevice;
CHECK_ACL(aclrtMalloc((void **)&xDevice, inputByteSize, ACL_MEM_MALLOC_HUGE_FIRST));
CHECK_ACL(aclrtMemcpy(xDevice, inputByteSize, inputX.data(), inputByteSize, ACL_MEMCPY_HOST_TO_DEVICE));
CHECK_ACL(aclrtMallocHost((void **)(&yHost), outputByteSize));
CHECK_ACL(aclrtMalloc((void **)&yDevice, outputByteSize, ACL_MEM_MALLOC_HUGE_FIRST));
SinhCustom<SinhOpTraits><<<param.tilingData.blockNum, nullptr, stream>>>(xDevice, yDevice, param);
CHECK_ACL(aclrtSynchronizeStream(stream));
CHECK_ACL(aclrtMemcpy(yHost, outputByteSize, yDevice, outputByteSize, ACL_MEMCPY_DEVICE_TO_HOST));
std::vector<float> outputY(reinterpret_cast<float*>(yHost), reinterpret_cast<float*>(yHost) + eleNum);
CHECK_ACL(aclrtFree(xDevice));
CHECK_ACL(aclrtFree(yDevice));
CHECK_ACL(aclrtFreeHost(yHost));
CleanACL(stream, context, deviceId);
if (!VerifyResults(golden, outputY)) {
return -1;
}
printf("Accuracy verification passed.\n");
return 0;
}