* Copyright (c) 2026 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 test_aclnn_inplace_attention_worker_scheduler.cpp
*/
#include <iostream>
#include <vector>
#include <cmath>
#include <limits>
#include "acl/acl.h"
#include "aclnnop/aclnn_attention_worker_scheduler.h"
#define CHECK_RET(cond, return_expr) \
do { \
if (!(cond)) { \
return_expr; \
} \
} while (0)
#define CHECK_FREE_RET(cond, return_expr) \
do { \
if (!(cond)) { \
Finalize(deviceId, stream); \
return_expr; \
} \
} while (0)
#define LOG_PRINT(message, ...) \
do { \
printf(message, ##__VA_ARGS__); \
} while (0)
int64_t GetShapeSize(const std::vector<int64_t>& shape) {
int64_t shapeSize = 1;
for (auto i : shape) {
shapeSize *= i;
}
return shapeSize;
}
int Init(int32_t deviceId, aclrtStream* stream) {
auto ret = aclInit(nullptr);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclInit failed. ERROR: %d\n", ret); return ret);
ret = aclrtSetDevice(deviceId);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetDevice failed. ERROR: %d\n", ret); return ret);
ret = aclrtCreateStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); return ret);
return 0;
}
int CreateAclTensor(const void *hostData, const std::vector<int64_t>& shape, void** deviceAddr,
aclDataType dataType, aclTensor** tensor) {
auto size = GetShapeSize(shape);
auto ret = aclrtMalloc(deviceAddr, size, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
ret = aclrtMemcpy(*deviceAddr, size, hostData, size, ACL_MEMCPY_HOST_TO_DEVICE);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret);
std::vector<int64_t> stride(shape.size(), 1);
for (int64_t i = shape.size() - 2; i >= 0; i--) {
stride[i] = shape[i + 1] * stride[i + 1];
}
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, stride.data(), 0, aclFormat::ACL_FORMAT_ND,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
void Finalize(int32_t deviceId, aclrtStream stream)
{
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
}
#pragma pack(push, 1)
struct AttentionDataDesc {
int32_t flag[0];
};
struct ScheduleContext {
struct CommonArea {
uint32_t session_num;
uint32_t micro_batch_num;
uint32_t micro_batch_size;
uint32_t selected_expert_num;
uint32_t expert_num;
uint32_t attn_to_ffn_token_size;
uint32_t ffn_to_attn_token_size;
int32_t schedule_mode;
int8_t reserve0[96];
};
struct ControlArea {
int32_t run_flag;
int8_t reserve2[124];
};
struct FfnArea {
uint64_t token_info_buf;
uint64_t token_info_buf_size;
uint64_t token_data_buf;
uint64_t token_data_buf_size;
uint64_t polling_index;
int8_t reserve3[88];
uint64_t layer_ids_buf;
uint64_t layer_ids_buf_size;
uint64_t session_ids_buf;
uint64_t session_ids_buf_size;
uint64_t micro_batch_ids_buf;
uint64_t micro_batch_ids_buf_size;
uint64_t expert_ids_buf;
uint64_t expert_ids_buf_size;
uint32_t out_num;
int8_t reserve4[60];
};
struct AttentionArea {
uint64_t token_info_buf;
uint64_t token_info_buf_size;
uint64_t token_data_buf;
uint64_t token_data_buf_size;
uint32_t micro_batch_id;
int8_t reserve5[92];
};
CommonArea common;
ControlArea control;
AttentionArea attention;
FfnArea ffn;
int8_t reserve6[384];
};
static_assert(sizeof(ScheduleContext) == 1024, "ScheduleContext size must be 1024 bytes");
#pragma pack(pop)
int aclnnAttentionWorkerSchedulerTest(int32_t deviceId, aclrtStream &stream) {
auto ret = Init(deviceId, &stream);
CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
ScheduleContext hostScheduleContext = {};
hostScheduleContext.common.session_num = 1;
hostScheduleContext.common.micro_batch_num = 2;
hostScheduleContext.common.micro_batch_size = 48;
hostScheduleContext.common.selected_expert_num = 9;
hostScheduleContext.common.expert_num = 16;
hostScheduleContext.common.attn_to_ffn_token_size = 512;
hostScheduleContext.common.ffn_to_attn_token_size = 512;
hostScheduleContext.common.schedule_mode = 1;
hostScheduleContext.control.run_flag = 1;
hostScheduleContext.attention.micro_batch_id = 1;
size_t per_data_desc_size = sizeof(AttentionDataDesc) + sizeof(int32_t) * hostScheduleContext.common.micro_batch_size * hostScheduleContext.common.selected_expert_num;
size_t expect_token_info_buf_size = static_cast<size_t>(hostScheduleContext.common.micro_batch_num) * per_data_desc_size;
void* tokenBufDeviceAddr = nullptr;
ret = aclrtMalloc(&tokenBufDeviceAddr, expect_token_info_buf_size, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
hostScheduleContext.attention.token_info_buf = reinterpret_cast<uint64_t>(tokenBufDeviceAddr);
hostScheduleContext.attention.token_info_buf_size = expect_token_info_buf_size;
int target_micro_batch_id = 0;
auto data_desc_ptr = reinterpret_cast<AttentionDataDesc *>(
reinterpret_cast<uint8_t *>(hostScheduleContext.attention.token_info_buf) + per_data_desc_size * target_micro_batch_id);
size_t flag_num =
static_cast<size_t>(hostScheduleContext.common.micro_batch_size) * hostScheduleContext.common.selected_expert_num;
std::vector<int32_t> host_flags(flag_num, 1);
ret = aclrtMemcpy(data_desc_ptr->flag, flag_num * sizeof(int32_t),
host_flags.data(), host_flags.size() * sizeof(int32_t),
ACL_MEMCPY_HOST_TO_DEVICE);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy flags from host to device failed. ERROR: %d\n", ret); return ret);
uint64_t token_data_buf_size = 100;
void* tokenDataDeviceAddr = nullptr;
ret = aclrtMalloc(&tokenDataDeviceAddr, token_data_buf_size, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMalloc failed. ERROR: %d\n", ret); return ret);
hostScheduleContext.attention.token_data_buf = reinterpret_cast<uint64_t>(tokenDataDeviceAddr);
std::vector<int64_t> scheduleContextShape = {1024};
void* scheduleContextDeviceAddr = nullptr;
aclTensor* scheduleContextRef = nullptr;
ret = CreateAclTensor(&hostScheduleContext, scheduleContextShape, &scheduleContextDeviceAddr, aclDataType::ACL_INT8, &scheduleContextRef);
CHECK_RET(ret == ACL_SUCCESS, return ret);
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
ret = aclnnInplaceAttentionWorkerSchedulerGetWorkspaceSize(scheduleContextRef, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceAttentionWorkerSchedulerGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
void* workspaceAddr = nullptr;
if (workspaceSize > 0) {
ret = aclrtMalloc(&workspaceAddr, workspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate workspace failed. ERROR: %d\n", ret); return ret;);
}
ret = aclnnInplaceAttentionWorkerScheduler(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceAttentionWorkerScheduler failed. ERROR: %d\n", ret); return ret);
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
auto size = GetShapeSize(scheduleContextShape);
std::vector<int8_t> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), scheduleContextDeviceAddr, size * sizeof(int8_t), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return ret);
ScheduleContext *out_schedule_context = reinterpret_cast<ScheduleContext *>(resultData.data());
LOG_PRINT("micro_batch_id = %u.\n", out_schedule_context->attention.micro_batch_id);
aclDestroyTensor(scheduleContextRef);
aclrtFree(scheduleContextDeviceAddr);
aclrtFree(tokenBufDeviceAddr);
aclrtFree(tokenDataDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
return ACL_SUCCESS;
}
int main() {
int32_t deviceId = 0;
aclrtStream stream;
auto ret = aclnnAttentionWorkerSchedulerTest(deviceId, stream);
CHECK_FREE_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAttentionWorkerSchedulerTest failed. ERROR: %d\n", ret); return ret);
Finalize(deviceId, stream);
return 0;
}