Ascend C算子开发指南
Ascend C的特点
C/C++原生编程:Ascend C原生支持C和C++标准规范。
屏蔽硬件差异:编程模型屏蔽了硬件差异,提高了代码的通用性。
API封装:类库API封装,既保证易用性,又兼顾高效性。
孪生调试:支持在CPU侧模拟NPU侧的行为,便于调试。
开发基本流程
环境准备:
安装CANN开发套件包,根据机器CPU架构下载对应的版本。
示例(AArch64架构):
bash
复制代码
wget -O Ascend-cann-toolkit_8.0.RC1.alpha002_linux-aarch64.run <下载链接>
chmod +x Ascend-cann-toolkit_8.0.RC1.alpha002_linux-x86_64.run
./Ascend-cann-toolkit_8.0.RC1.alpha002_linux-x86_64.run --check
sudo ./Ascend-cann-toolkit_8.0.RC1.alpha002_linux-x86_64.run --install
source /usr/local/Ascend/ascend-toolkit/set_env.sh
算子分析:
分析算子的数学表达式、输入输出数据类型和计算逻辑。
例如,Add算子的数学表达式为 $z = x + y$,输入输出数据类型为half(float16),支持的shape为(8, 2048)。
核函数开发(以Add算子为例):
获取样例代码目录quick-start,依次开发add_custom.cpp、main.cpp、gen_data.py三个文件。
核函数实现(add_custom.cpp):
cpp
复制代码
extern "C" global aicore void add_custom(GM_ADDR x, GM_ADDR y, GM_ADDR z) {
KernelAdd op;
op.Init(x, y, z);
op.Process();
}
void add_custom_do(uint32_t blockDim, void* l2ctrl, void* stream, uint8_t* x, uint8_t* y, uint8_t* z) {
add_custom<<<blockDim, l2ctrl, stream>>>(x, y, z);
}
算子类实现(KernelAdd):
cpp
复制代码
class KernelAdd {
public:
aicore inline KernelAdd() {}
aicore inline void Init(GM_ADDR x, GM_ADDR y, GM_ADDR z) {
// 初始化代码
}
aicore inline void Process() {
// 核心处理函数
}
private:
// 各阶段函数定义
aicore inline void CopyIn(int32_t progress) {}
aicore inline void Compute(int32_t progress) {}
aicore inline void CopyOut(int32_t progress) {}
private:
TPipe pipe;
TQue<QuePosition::VECIN, BUFFER_NUM> inQueueX, inQueueY;
TQue<QuePosition::VECOUT, BUFFER_NUM> outQueueZ;
GlobalTensor
};
Process函数:
cpp
复制代码
aicore inline void Process() {
constexpr int32_t loopCount = TILE_NUM * BUFFER_NUM;
for (int32_t i = 0; i < loopCount; i++) {
CopyIn(i);
Compute(i);
CopyOut(i);
}
}
CopyIn函数:
cpp
复制代码
aicore inline void CopyIn(int32_t progress) {
LocalTensor
LocalTensor
DataCopy(xLocal, xGm[progress * TILE_LENGTH], TILE_LENGTH);
DataCopy(yLocal, yGm[progress * TILE_LENGTH], TILE_LENGTH);
inQueueX.EnQue(xLocal);
inQueueY.EnQue(yLocal);
}
Compute函数:
cpp
复制代码
aicore inline void Compute(int32_t progress) {
LocalTensor
LocalTensor
LocalTensor
Add(zLocal, xLocal, yLocal, TILE_LENGTH);
outQueueZ.EnQue
inQueueX.FreeTensor(xLocal);
inQueueY.FreeTensor(yLocal);
}
CopyOut函数:
cpp
复制代码
aicore inline void CopyOut(int32_t progress) {
LocalTensor
DataCopy(zGm[progress * TILE_LENGTH], zLocal, TILE_LENGTH);
outQueueZ.FreeTensor(zLocal);
}
运行验证(main.cpp):
CPU侧验证:
cpp
复制代码
// 初始化内存并调用核函数
uint8_t* x = (uint8_t)AscendC::GmAlloc(inputByteSize);
uint8_t y = (uint8_t)AscendC::GmAlloc(inputByteSize);
uint8_t z = (uint8_t*)AscendC::GmAlloc(outputByteSize);
ReadFile("./input/input_x.bin", inputByteSize, x, inputByteSize);
ReadFile("./input/input_y.bin", inputByteSize, y, inputByteSize);
AscendC::SetKernelMode(KernelMode::AIV_MODE);
ICPU_RUN_KF(add_custom, blockDim, x, y, z);
WriteFile("./output/output_z.bin", z, outputByteSize);
AscendC::GmFree((void *)x);
AscendC::GmFree((void *)y);
AscendC::GmFree((void *)z);
NPU侧验证:
cpp
复制代码
// 初始化AscendCL
CHECK_ACL(aclInit(nullptr));
aclrtContext context;
int32_t deviceId = 0;
CHECK_ACL(aclrtSetDevice(deviceId));
CHECK_ACL(aclrtCreateContext(&context, deviceId));
aclrtStream stream = nullptr;
CHECK_ACL(aclrtCreateStream(&stream));
// 分配内存并进行数据拷贝
uint8_t *xHost, *yHost, *zHost;
uint8_t *xDevice, *yDevice, *zDevice;
CHECK_ACL(aclrtMallocHost((void)(&xHost), inputByteSize));
CHECK_ACL(aclrtMallocHost((void)(&yHost), inputByteSize));
CHECK_ACL(aclrtMallocHost((void)(&zHost), outputByteSize));
CHECK_ACL(aclrtMalloc((void)&xDevice, inputByteSize, ACL_MEM_MALLOC_HUGE_FIRST));
CHECK_ACL(aclrtMalloc((void)&yDevice, inputByteSize, ACL_MEM_MALLOC_HUGE_FIRST));
CHECK_ACL(aclrtMalloc((void)&zDevice, outputByteSize, ACL_MEM_MALLOC_HUGE_FIRST));
ReadFile("./input/input_x.bin", inputByteSize, xHost, inputByteSize);
ReadFile("./input/input_y.bin", inputByteSize, yHost, inputByteSize);
CHECK_ACL(aclrtMemcpy(xDevice, inputByteSize, xHost, inputByteSize, ACL_MEMCPY_HOST_TO_DEVICE));
CHECK_ACL(aclrtMemcpy(yDevice, inputByteSize, yHost, inputByteSize, ACL_MEMCPY_HOST_TO_DEVICE));
add_custom_do(blockDim, nullptr, stream, xDevice, yDevice, zDevice);
CHECK_ACL(aclrtSynchronizeStream(stream));
CHECK_ACL(aclrtMemcpy(zHost, outputByteSize, zDevice, outputByteSize, ACL_MEMCPY_DEVICE_TO_HOST));
WriteFile("./output/output_z.bin", zHost, outputByteSize);
CHECK_ACL(aclrtFree(xDevice));
CHECK_ACL(aclrtFree(yDevice));
CHECK_ACL(aclrtFree(zDevice));
CHECK_ACL(aclrtFreeHost(xHost));
CHECK_ACL(aclrtFreeHost(yHost));
CHECK_ACL(aclrtFreeHost(zHost));
CHECK_ACL(aclrtDestroyStream(stream));
CHECK_ACL(aclrtDestroyContext(context));
CHECK_ACL(aclrtResetDevice(deviceId));
CHECK_ACL(aclFinalize());
数据生成(gen_data.py):
python
复制代码
import numpy as np
def gen_golden_data_simple():
input_x = np.random.uniform(-100, 100, [8, 2048]).astype(np.float16)
input_y = np.random.uniform(-100, 100, [8, 2048]).astype(np.float16)
golden = (input_x + input_y).astype(np.float16)
input_x.tofile("./input/input_x.bin")
input_y.tofile("./input/input_y.bin")
golden.tofile("./output/golden.bin")
if name == "main":
gen_golden_data_simple()
运行验证:
设置环境变量:
bash
复制代码
export ASCEND_HOME_DIR=/usr/local/Ascend/ascend-toolkit/latest
执行脚本:
bash
复制代码
bash run.sh <soc_version> <run_mode>
通过以上步骤,即可完成Ascend C算子的开发和验证。