/**
* 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.
*/
/* !
* \file clampmax.asc
* \brief
*/
#include "acl/acl.h"
#include "data_utils.h"
#include "kernel_operator.h"
constexpr uint32_t CLAMP_CALC_FAC = 1;
constexpr uint32_t CLAMP_FLOAT_ELE = 64;
constexpr uint32_t CLAMP_HALF_ELE = 128;
__aicore__ inline void GetClampMaxMinTmpSize(const AscendC::ShapeInfo srcShape, const uint32_t typeSize,
const bool isReuseSource, uint32_t& maxValue, uint32_t& minValue)
{
(void)isReuseSource;
const uint32_t inputSize = srcShape.shape[0];
if (typeSize == sizeof(float)) {
minValue = CLAMP_FLOAT_ELE * sizeof(uint8_t);
if ((inputSize * sizeof(uint8_t)) > (CLAMP_FLOAT_ELE * sizeof(uint8_t))) {
maxValue = inputSize * sizeof(uint8_t);
} else {
maxValue = CLAMP_FLOAT_ELE * sizeof(uint8_t);
}
} else {
minValue = CLAMP_HALF_ELE * sizeof(uint8_t);
if ((inputSize * sizeof(uint8_t)) > (CLAMP_HALF_ELE * sizeof(uint8_t))) {
maxValue = inputSize * sizeof(uint8_t);
} else {
maxValue = CLAMP_HALF_ELE * sizeof(uint8_t);
}
}
}
template <typename T>
class KernelClamp {
public:
__aicore__ inline KernelClamp() {}
__aicore__ inline void Init(GM_ADDR srcGm, GM_ADDR dstGm, uint32_t srcSize, uint32_t delCount, uint32_t clampType,
T scalar, AscendC::TPipe* pipeIn)
{
pipe = pipeIn;
srcGlobal.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(srcGm), srcSize);
dstGlobal.SetGlobalBuffer(reinterpret_cast<__gm__ T*>(dstGm), srcSize);
pipe->InitBuffer(inQueueX, 1, srcSize * sizeof(T));
pipe->InitBuffer(outQueue, 1, srcSize * sizeof(T));
bufferSize = srcSize;
delSize = delCount;
clampMode = clampType;
clampScalar = scalar;
}
__aicore__ inline void Process()
{
AscendC::AscendCUtils::SetOverflow(1);
CopyIn();
Compute();
CopyOut();
AscendC::AscendCUtils::SetOverflow(0);
}
__aicore__ inline void CopyIn()
{
AscendC::LocalTensor<T> srcLocal = inQueueX.AllocTensor<T>();
AscendC::DataCopy(srcLocal, srcGlobal, bufferSize);
inQueueX.EnQue(srcLocal);
}
__aicore__ inline void Compute()
{
AscendC::LocalTensor<T> dstLocal = outQueue.AllocTensor<T>();
AscendC::LocalTensor<T> srcLocal = inQueueX.DeQue<T>();
AscendC::LocalTensor<uint8_t> stackBuffer;
bool ans = AscendC::PopStackBuffer<uint8_t, AscendC::TPosition::LCM>(stackBuffer);
stackBufferSize = stackBuffer.GetSize();
uint32_t inputShape[1] = {bufferSize};
AscendC::ShapeInfo shapeInfo{1, inputShape, 1, inputShape, AscendC::DataFormat::ND};
uint32_t maxValue = 0;
uint32_t minValue = 0;
GetClampMaxMinTmpSize(shapeInfo, sizeof(T), false, maxValue, minValue);
uint64_t medianValue = (maxValue + minValue) / 2;
uint32_t calcount = bufferSize - delSize;
if (clampMode == 0) {
AscendC::ClampMin<T, false>(dstLocal, srcLocal, stackBuffer, clampScalar, calcount);
} else {
AscendC::ClampMax<T, false>(dstLocal, srcLocal, stackBuffer, clampScalar, calcount);
}
outQueue.EnQue<T>(dstLocal);
inQueueX.FreeTensor(srcLocal);
}
__aicore__ inline void CopyOut()
{
AscendC::LocalTensor<T> dstLocal = outQueue.DeQue<T>();
AscendC::DataCopy(dstGlobal, dstLocal, bufferSize);
outQueue.FreeTensor(dstLocal);
}
private:
AscendC::TPipe* pipe;
AscendC::TQue<AscendC::QuePosition::VECIN, 1> inQueueX;
AscendC::TQue<AscendC::QuePosition::VECOUT, 1> outQueue;
AscendC::GlobalTensor<T> srcGlobal;
AscendC::GlobalTensor<T> dstGlobal;
uint32_t bufferSize = 0;
uint32_t delSize = 0;
uint32_t stackBufferSize = 0;
uint32_t clampMode = 0;
T clampScalar = 0;
};
__global__ __vector__ void clampmax_custom(GM_ADDR srcGm, GM_ADDR dstGm)
{
AscendC::TPipe pipe;
constexpr uint32_t srcSize = 256;
constexpr uint32_t delSize = 0;
constexpr uint32_t clampType = 1;
constexpr float scalar = 65502;
KernelClamp<float> op;
op.Init(srcGm, dstGm, srcSize, delSize, clampType, scalar, &pipe);
op.Process();
}
static bool CompareResult(const void* outputData, uint32_t outSize)
{
void* goldenData;
aclrtMallocHost((void**)(&goldenData), outSize);
size_t goldenSize = outSize;
bool ret = ReadFile("./output/golden.bin", goldenSize, goldenData, goldenSize);
if (ret) {
printf("ReadFile golden.bin success!\n");
} else {
printf("test failed!\n");
return false;
}
constexpr float EPS = 1e-4;
const float epsilon = 1e-8f;
int64_t wrongNum = 0;
for (size_t i = 0; i < outSize / sizeof(float); i++) {
float a = (reinterpret_cast<const float*>(outputData))[i];
float b = (reinterpret_cast<const float*>(goldenData))[i];
float ae = std::abs(a - b);
float re = ae / (std::abs(b) + epsilon);
if (ae > EPS && re > EPS) {
printf("CompareResult golden.bin failed output is %lf, golden is %lf\n", a, b);
wrongNum++;
}
}
aclrtFreeHost(goldenData);
if (wrongNum != 0) {
return false;
} else {
printf("CompareResult golden.bin success!\n");
return true;
}
}
int32_t main(int32_t argc, char* argv[])
{
size_t param1FileSize = 256 * sizeof(float);
size_t param2FileSize = 256 * sizeof(float);
uint32_t numBlocks = 1;
aclInit(nullptr);
aclrtContext context;
int32_t deviceId = 0;
aclrtSetDevice(deviceId);
aclrtCreateContext(&context, deviceId);
aclrtStream stream = nullptr;
aclrtCreateStream(&stream);
uint8_t* param1Host;
uint8_t* param1Device;
aclrtMallocHost((void**)(¶m1Host), param1FileSize);
aclrtMalloc((void**)¶m1Device, param1FileSize, ACL_MEM_MALLOC_HUGE_FIRST);
ReadFile("./input/input_x.bin", param1FileSize, param1Host, param1FileSize);
aclrtMemcpy(param1Device, param1FileSize, param1Host, param1FileSize, ACL_MEMCPY_HOST_TO_DEVICE);
uint8_t* param2Host;
uint8_t* param2Device;
aclrtMallocHost((void**)(¶m2Host), param2FileSize);
aclrtMalloc((void**)¶m2Device, param2FileSize, ACL_MEM_MALLOC_HUGE_FIRST);
clampmax_custom<<<numBlocks, nullptr, stream>>>(param1Device, param2Device);
aclrtSynchronizeStream(stream);
aclrtFree(param1Device);
aclrtFreeHost(param1Host);
aclrtMemcpy(param2Host, param2FileSize, param2Device, param2FileSize, ACL_MEMCPY_DEVICE_TO_HOST);
WriteFile("./output/output.bin", param2Host, param2FileSize);
bool goldenResult = true;
goldenResult = CompareResult(param2Host, param2FileSize);
if (goldenResult) {
printf("test pass!\n");
} else {
printf("test failed!\n");
}
aclrtFree(param2Device);
aclrtFreeHost(param2Host);
aclrtDestroyStream(stream);
aclrtDestroyContext(context);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}