aclnnInplaceAttentionWorkerScheduler
Applicable Products
Ascend 950PR&950DT products : Supported
Atlas A3 products : Supported
Atlas A2 products : Not supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Not supported
Atlas training products : Not supported
Function
Description: In use cases where Attention and FFN are deployed separately, this operator scans data on the Attention side. The operator receives the output data of the FFNToAttention operator and scans the data step by step to ensure that the data is ready.
This operator is not recommended for standalone use. Use it together with the FFNToAttention and AttentionWorkerCombine operators to form a complete workflow.
Receives the data sent by the FFNToAttention operator. The data is stored in the ScheduleContext structure. For the detailed definition, see Example. The structure contains the CommonArea, ControlArea, AttentionArea, and FfnArea fields. This API involves the following fields: CommonArea (which stores configuration information such as session_num, micro_batch_num, micro_batch_size, and selected_expert_num), ControlArea (which the upper layer uses to control whether the process exits), and AttentionArea (which manages the core data buffers and status information required during operator computation, where token_info_buf stores input-related data).
Read the flag information stored in token_info_buf in the ScheduleContext.AttentionArea field to check whether the communication data is ready.
After all data is ready, it can be used by the AttentionWorkerCombine operator.
Formula:
$$ \text{Initialize:} \quad \text{ready\_count} = 0, \quad \text{flag\_num} = \text{micro\_batch\_size} \times \text{selected\_expert\_num} $$$$ \text{Check if run\_flag is 0:} \quad \text{if run\_flag} = 0, \quad \text{exit and log} $$$$ \text{Loop:} \quad \text{while run\_flag} \neq 0: \quad \text{ready\_count} = \sum_{i=1}^{\text{flag\_num}} \mathbf{1}\_{\{ \text{flag}[i] = 1 \}}; \quad \text{if ready\_count} = \text{flag\_num}, \quad \text{break} $$$$ \text{Reset flags:} \quad \text{flag}[i] = 0 \quad \text{for} \quad i = 1, 2, \dots, \text{flag\_num} $$$$ \text{Set micro\_batch\_id:} \quad \text{micro\_batch\_id} = (\text{micro\_batch\_id} + 1) \% \text{micro\_batch\_num} $$NOTE: micro_batch_size, selected_expert_num, run_flag, and micro_batch_id are parameters of the input ScheduleContext structure. For details about the structure, see Example.
Function Prototype
Each operator uses a two-phase API. You must first call the "aclnnInplaceAttentionWorkerSchedulerGetWorkspaceSize" API to obtain the workspace size required for the computation and the executor that contains the computation flow. Then call the "aclnnInplaceAttentionWorkerScheduler" API to execute the computation.
aclnnStatus aclnnInplaceAttentionWorkerSchedulerGetWorkspaceSize(
aclTensor* scheduleContextRef,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnInplaceAttentionWorkerScheduler(
void* workspace,
uint64_t workspaceSize,
aclOpExecutor* executor,
aclrtStream stream)aclnnInplaceAttentionWorkerSchedulerGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor scheduleContextRef Input/Output Data to be processed received on the Attention side, indicating the input scheduleContext information. For details, see Example. Empty tensors are not supported. INT8 ND 1D, with the shape fixed to (1024) × workspaceSize Output Returns the workspace size to be allocated on the Device side. - - - - - executor Output Returns the op executor that contains the operator computation flow. - - - - - Return Value
aclnnStatus: Return status code. For details, see aclnn return code.
The first-phase API performs input parameter validation and returns an error in the following scenarios:
Return Value Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 The parameter scheduleContextRef is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The dimension of the parameter scheduleContextRef is not 1. The parameter scheduleContextRef is an empty tensor.
aclnnInplaceAttentionWorkerScheduler
Parameters
Parameter Input/Output Description workspace Input The workspace memory address allocated on the device. workspaceSize Input The workspace size allocated on the device, obtained by the first-phase API aclnnInplaceAttentionWorkerSchedulerGetWorkspaceSize. executor Input op executor that contains the operator computation flow. stream Input Stream for executing tasks. Return Value
aclnnStatus: Return status code. For details, see aclnn return code.
Constraints
- aclnnInplaceAttentionWorkerScheduler defaults to a deterministic implementation. Non-deterministic implementation is not supported yet, and the deterministic computation configuration does not take effect.
Example
The sample code is as follows and is for reference only. For details about the compilation and execution process, see Compile and Run Samples.
#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) {
// Initialize. (boilerplate)
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);
// Call aclrtMalloc to allocate memory on the device.
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);
// Call aclrtMemcpy to copy data from the host to the memory on the device.
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);
// Calculate the stride of the contiguous tensor.
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];
}
// Call the aclCreateTensor API to create an aclTensor.
*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; // Number of attention nodes
uint32_t micro_batch_num;
uint32_t micro_batch_size;
uint32_t selected_expert_num;
uint32_t expert_num; // Number of experts per layer, including routing experts and shared experts.
uint32_t attn_to_ffn_token_size; // Each token in the Ffn window data area has a space size aligned to 512 bytes.
uint32_t ffn_to_attn_token_size; // Each token in the Attention window data area has a space size aligned to 512 bytes.
int32_t schedule_mode; // 0: Ffn only 1: Attention only
int8_t reserve0[96];
};
struct ControlArea {
int32_t run_flag; // 0 : exited 1 : running
int8_t reserve2[124];
};
struct FfnArea {
// ffn area
uint64_t token_info_buf; // Points to device memory.
uint64_t token_info_buf_size;
uint64_t token_data_buf; // Points to device memory.
uint64_t token_data_buf_size;
uint64_t polling_index; // For synchronous computation only:records the micro-batch ids to be processed internally by the ffn worker scheduler.
int8_t reserve3[88];
// ffn out area
uint64_t layer_ids_buf; // Points to a device memory region that stores the organized layer ids, with an array size of session_num.
uint64_t layer_ids_buf_size; // Total size: session_num * sizeof(int32_t)
uint64_t session_ids_buf; // Points to a device memory region that stores the organized session ids. The array size is session_num.
uint64_t session_ids_buf_size; // Total size: session_num * sizeof(int32_t)
uint64_t micro_batch_ids_buf; // Points to a device memory region that stores the organized micro batch ids. The array size is session_num.
uint64_t micro_batch_ids_buf_size; // Total size: session_num * sizeof(int32_t)
uint64_t expert_ids_buf; // Points to a device memory region that stores the organized expert ids. The tensor dimensions are [session_num, batch_size, selected_expert_num].
uint64_t expert_ids_buf_size; // Total memory size: session_num * batch_size * selected_expert_num * sizeof(int32_t)
uint32_t out_num; // Indicates the number of sessions that have been processed.
int8_t reserve4[60];
};
struct AttentionArea {
// attention area
uint64_t token_info_buf; // Points to device memory.
uint64_t token_info_buf_size;
uint64_t token_data_buf; // Points to device memory.
uint64_t token_data_buf_size;
uint32_t micro_batch_id; // Records the latest ready micro batch id.
int8_t reserve5[92];
};
// common area
CommonArea common;
ControlArea control;
AttentionArea attention;
FfnArea ffn;
// reserve area
int8_t reserve6[384]; // Padding to 1024 Byte.
};
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);
// 2. Construct the input and output. Customize the construction based on the API definition.
ScheduleContext hostScheduleContext = {};
// Initialize CommonArea
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; // attention
// Initialize ControlArea
hostScheduleContext.control.run_flag = 1; // running
// Initialize AttentionArea
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;
// Set all flags to 1.
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);
// Create the scheduleContext aclTensor.
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);
// 3. Call the CANN operator library API. Replace it with the specific API.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnInplaceAttentionWorkerScheduler.
ret = aclnnInplaceAttentionWorkerSchedulerGetWorkspaceSize(scheduleContextRef, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceAttentionWorkerSchedulerGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate memory on the device based on the workspaceSize computed by the first-phase API.
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;);
}
// Call the second-phase API of aclnnInplaceAttentionWorkerScheduler.
ret = aclnnInplaceAttentionWorkerScheduler(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnInplaceAttentionWorkerScheduler failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Synchronize and wait for the task execution to complete.
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
// 5. Obtain the output value and copy the result from the memory on the device to the host. Modify the code based on the specific API definition.
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);
// Print the output result.
ScheduleContext *out_schedule_context = reinterpret_cast<ScheduleContext *>(resultData.data());
LOG_PRINT("micro_batch_id = %u.\n", out_schedule_context->attention.micro_batch_id);
// 6. Release the aclTensor. Modify the code based on the specific API definition.
aclDestroyTensor(scheduleContextRef);
// 7. Release device resources. Modify the code based on the specific API definition.
aclrtFree(scheduleContextDeviceAddr);
aclrtFree(tokenBufDeviceAddr);
aclrtFree(tokenDataDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
return ACL_SUCCESS;
}
int main() {
// 1. (Boilerplate) Initialize the device/stream. See the external API list.
// Set deviceId based on the actual device.
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;
}