* 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 {
struct MemoryPtrs {
uint8_t *zHost;
uint8_t *dyDevice;
uint8_t *yDevice;
uint8_t *zDevice;
uint8_t *paramDevice;
};
void InitializeData(int32_t eleNum, std::vector<float> &inputDy, std::vector<float> &inputY, std::vector<float> &golden)
{
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_real_distribution<float> dis(1.0f, 100.0f);
for (int i = 0; i < eleNum; ++i) {
inputDy[i] = (dis(gen));
inputY[i] = (dis(gen));
}
for (int i = 0; i < eleNum; ++i) {
golden[i] = (inputDy[i]) * (1 - inputY[i] * inputY[i]);
}
}
void CleanUp(uint8_t *&zHost, uint8_t *&dyDevice, uint8_t *&yDevice, uint8_t *&zDevice)
{
CHECK_ACL(aclrtFree(dyDevice));
CHECK_ACL(aclrtFree(yDevice));
CHECK_ACL(aclrtFree(zDevice));
CHECK_ACL(aclrtFreeHost(zHost));
}
void MallocHostDeviceMemory(MemoryPtrs& memoryPtrs, size_t byteSize, std::vector<float>& inputDy, std::vector<float>& inputY)
{
CHECK_ACL(aclrtMallocHost((void **)(&memoryPtrs.zHost), byteSize));
CHECK_ACL(aclrtMalloc((void **)&memoryPtrs.dyDevice, byteSize, ACL_MEM_MALLOC_HUGE_FIRST));
CHECK_ACL(aclrtMalloc((void **)&memoryPtrs.yDevice, byteSize, ACL_MEM_MALLOC_HUGE_FIRST));
CHECK_ACL(aclrtMalloc((void **)&memoryPtrs.zDevice, byteSize, ACL_MEM_MALLOC_HUGE_FIRST));
CHECK_ACL(aclrtMemcpy(memoryPtrs.dyDevice, byteSize, inputDy.data(), byteSize, ACL_MEMCPY_HOST_TO_DEVICE));
CHECK_ACL(aclrtMemcpy(memoryPtrs.yDevice, byteSize, inputY.data(), byteSize, ACL_MEMCPY_HOST_TO_DEVICE));
}
using AddOpTraits = ATVC::OpTraits<ATVC::OpInputs<float, float>, ATVC::OpOutputs<float>>;
template <typename Traits>
struct TanhGradComputeFunc {
* \brief: Compute operator of tanh: z = dy * (1 - y ^ 2)
* \param [in] dy, input local tensor
* \param [in] y, input local tensor
* \param [out] z, output local tensor
* \return void
*/
template <typename DTYPE_DY, typename DTYPE_X, typename DTYPE_Z>
__aicore__ inline void operator()(
AscendC::LocalTensor<DTYPE_DY> dy, AscendC::LocalTensor<DTYPE_X> y, AscendC::LocalTensor<DTYPE_Z> z)
{
auto length = y.GetSize();
AscendC::Mul(y, y, y, length);
AscendC::Mul(y, dy, y, length);
AscendC::Sub(z, dy, y, length);
}
};
}
template <class Traits>
* \brief: Kernel entry of Tanh, kernel func is: z = dy * (1 - y ^ 2)
* \param [in] dy, input global tensor
* \param [in] y, input global tensor
* \param [out] z, output global tensor
* \return void
*/
__global__ __aicore__ void TanhGrad(GM_ADDR dy, GM_ADDR y, GM_ADDR z, ATVC::EleWiseParam param)
{
KERNEL_TASK_TYPE_DEFAULT(KERNEL_TYPE_AIV_ONLY);
auto op = ATVC::Kernel::EleWiseOpTemplate<
TanhGradComputeFunc<Traits>>();
op.Run(dy, y, z, ¶m);
}
int main()
{
if (!ATVC::Host::DebugCheck<AddOpTraits, ATVC::TemplateType::ELE_WISE>()) {
printf("[ERROR]: ElementWise OpTraits check failed.\n");
return -1;
}
int32_t eleNum = 8 * 1024;
size_t byteSize = static_cast<size_t>(eleNum) * sizeof(float);
std::vector<float> inputDy(eleNum);
std::vector<float> inputY(eleNum);
std::vector<float> golden(eleNum);
InitializeData(eleNum, inputDy, inputY, golden);
ATVC::EleWiseParam param;
ATVC::Host::EleWiseTilingHyperParam hyperParam;
hyperParam.singleCoreBaseLine = 1024;
if (!ATVC::Host::CalcEleWiseTiling<AddOpTraits>(eleNum, param, hyperParam=hyperParam)) {
printf("[ERROR]: Calculate eleWise tiling failed.\n");
return -1;
};
aclrtContext context;
int32_t deviceId = 0;
aclrtStream stream = nullptr;
InitializeACL(context, stream, deviceId);
MemoryPtrs memoryPtrs;
MallocHostDeviceMemory(memoryPtrs, byteSize, inputDy, inputY);
uint32_t blockNum = param.tilingData.blockNum;
TanhGrad<AddOpTraits><<<blockNum, nullptr, stream>>>(memoryPtrs.dyDevice, memoryPtrs.yDevice, memoryPtrs.zDevice, param);
#if ATVC_DEBUG_MODE == 2
for (int32_t i = 0; i < 19; i++) {
TanhGrad<AddOpTraits><<<blockNum, nullptr, stream>>>(memoryPtrs.dyDevice, memoryPtrs.yDevice, memoryPtrs.zDevice, param);
}
#endif
CHECK_ACL(aclrtSynchronizeStream(stream));
CHECK_ACL(aclrtMemcpy(memoryPtrs.zHost, byteSize, memoryPtrs.zDevice, byteSize, ACL_MEMCPY_DEVICE_TO_HOST));
std::vector<float> outputZ(reinterpret_cast<float *>(memoryPtrs.zHost), reinterpret_cast<float *>(memoryPtrs.zHost) + eleNum);
CleanUp(memoryPtrs.zHost, memoryPtrs.dyDevice, memoryPtrs.yDevice, memoryPtrs.zDevice);
CleanACL(stream, context, deviceId);
if (!VerifyResults(golden, outputZ)) {
return -1;
}
printf("Accuracy verification passed.\n");
return 0;
}