aclnnRingAttentionUpdate
Applicable Products
Ascend 950PR&950DT products : Supported
Atlas A3 products : Supported
Atlas A2 products : Supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Not supported
Atlas training products : Not supported
Function
Description: The RingAttentionUpdate operator updates the outputs of two FlashAttention operations based on their different softmax max and sum values.
Formula:
Function Prototype
Each operator uses a two-phase API. Call "aclnnRingAttentionUpdateGetWorkspaceSize" to obtain the workspace size required for computation and the executor that contains the operator computation process, and then call "aclnnRingAttentionUpdate" to perform the computation.
aclnnStatus aclnnRingAttentionUpdateGetWorkspaceSize(
const aclTensor *prevAttnOut,
const aclTensor *prevSoftmaxMax,
const aclTensor *prevSoftmaxSum,
const aclTensor *curAttnOut,
const aclTensor *curSoftmaxMax,
const aclTensor *curSoftmaxSum,
const aclTensor *actualSeqQlenOptional,
char *inputLayoutOptional,
const aclTensor *attnOutOut,
const aclTensor *softmaxMaxOut,
const aclTensor *softmaxSumOut,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnRingAttentionUpdate(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnRingAttentionUpdateGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor prevAttnOut (aclTensor*) Input prev_attn_out in the formula, which is the output of the first FlashAttention. The input shape must keep consistent with the inputLayoutOptional attribute. FLOAT16, FLOAT, BFLOAT16 ND [T,N,D], [S,B,H] √ prevSoftmaxMax (aclTensor*) Input prev_softmax_max in the formula, which is the max result of the softmax of the first FlashAttention. The 8 values in the last dimension must be the same and must be positive numbers. FLOAT ND [B,N,S,8], [T,N,8] √ prevSoftmaxSum (aclTensor*) Input prev_softmax_sum in the formula, the sum result of softmax of the first FlashAttention. The input shape must keep consistent with that of prevSoftmaxMax. The 8 values in the last dimension must be the same and must be positive numbers. FLOAT ND [B,N,S,8], [T,N,8] √ curAttnOut (aclTensor*) Input cur_attn_out in the formula, which is the output of the second FlashAttention. The data type and input shape must keep consistent with those of prevAttnOut. FLOAT16, FLOAT, BFLOAT16 ND [T,N,D], [S,B,H] √ curSoftmaxMax (aclTensor*) Input cur_softmax_max in the formula, which indicates the max result of the softmax in the second FlashAttention. The input shape must keep consistent with that of prevSoftmaxMax, the 8 numbers in the last dimension must be the same, and all values must be positive numbers. FLOAT ND [B,N,S,8], [T,N,8] √ curSoftmaxSum (aclTensor*) Input cur_softmax_sum in the formula, which is the sum result of softmax of the second FlashAttention. The input shape must keep consistent with prevSoftmaxMax. The 8 numbers in the last dimension are the same and must be positive numbers. FLOAT ND [B,N,S,8], [T,N,8] √ actualSeqQlenOptional (aclTensor*) Input Accumulation of sequence lengths starting from 0. This parameter is required when inputLayoutOptional is TND. It is an aclTensor of integers increasing from 0 to T. INT64 - - - inputLayoutOptional (char*) Input Data layout of the inputs related to attn_out. char* constant on the Host. Currently, "TND" and "SBH" are supported. - - - - attnOutOut (aclTensor*) Output attn_out in the formula, the output updated from the two results. The data type and output shape keep consistent with prevAttnOut. FLOAT16, FLOAT, BFLOAT16 ND [T,N,D], [S,B,H] √ softmaxMaxOut (aclTensor*) Output softmax_max in the formula, the max of softmax updated through the two results. The output shape keeps consistent with prevSoftmaxMax. FLOAT ND [B,N,S,8], [T,N,8] √ softmaxSumOut (aclTensor*) Output softmax_sum in the formula, that is, the sum of softmax updated based on the two results. The output shape keeps consistent with prevSoftmaxMax. FLOAT ND [B,N,S,8], [T,N,8] √ workspaceSize (uint64_t*) Output Size of the workspace to be allocated on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the op executor, containing the operator computation flow. - - - - - Atlas A2 products ,Atlas A3 products : If the input data layout inputLayoutOptional is TND, D must be a multiple of 64.
Return Value
aclnnStatus: Returns the status code. For details, see aclnn return codes.
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 When the input prevAttnOut, prevSoftmaxMax, prevSoftmaxSum, curAttnOut, curSoftmaxMax, curSoftmaxSum, attnOutOut, softmaxMaxOut, or softmaxSumOut is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of prevAttnOut, prevSoftmaxMax, prevSoftmaxSum, curAttnOut, curSoftmaxMax, curSoftmaxSum, attnOutOut, softmaxMaxOut, or softmaxSumOut is out of the supported range. ACLNN_ERR_PARAM_INVALID 561002 When actualSeqQlenOptional has an input, the input data format is out of the supported range. The shape of prevAttnOut, prevSoftmaxMax, prevSoftmaxSum, curAttnOut, curSoftmaxMax, curSoftmaxSum, attnOutOut, softmaxMaxOut, or softmaxSumOut is empty. The shape of prevAttnOut, prevSoftmaxMax, prevSoftmaxSum, curAttnOut, curSoftmaxMax, curSoftmaxSum, attnOutOut, softmaxMaxOut, or softmaxSumOut is not supported.
aclnnRingAttentionUpdate
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address allocated on the device. workspaceSize Input Workspace size allocated on the device, obtained by the first-phase API aclnnRingAttentionUpdateGetWorkspaceSize. executor Input Executor of an op, which contains the operator calculation flow. stream Input Specifies the Stream for task execution. Return value:
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
- Deterministic computation:
- aclnnRingAttentionUpdate defaults to a deterministic implementation.
- When inputLayoutOptional is set to "TND", actualSeqQlenOptional is mandatory.
- When inputLayoutOptional is set to "TND":
N:
Atlas A2 products ,Atlas A3 products : N<=256.
D:
Atlas A2 products ,Atlas A3 products : D<=768 and D must be a multiple of 64.
Example
The following sample code is for reference only. For details about compilation and execution, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_ring_attention_update.h"
#define CHECK_RET(cond, return_expr) \
do { \
if (!(cond)) { \
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 shape_size = 1;
for (auto i : shape) {
shape_size *= i;
}
return shape_size;
}
int Init(int32_t deviceId, aclrtStream* stream) {
// Boilerplate: resource initialization.
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;
}
template <typename T>
int CreateAclTensor(const std::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr,
aclDataType dataType, aclTensor** tensor) {
auto size = GetShapeSize(shape) * sizeof(T);
// 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 host data to the device memory.
ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret);
// Calculate the strides of the contiguous tensor.
std::vector<int64_t> strides(shape.size(), 1);
for (int64_t i = shape.size() - 2; i >= 0; i--) {
strides[i] = shape[i + 1] * strides[i + 1];
}
// Call the aclCreateTensor API to create an aclTensor.
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
int main() {
// 1. (Boilerplate) Device/stream initialization. For details, see the acl API documentation.
// Set deviceId based on the actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
// Handle check as needed.
CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct the inputs and outputs. Custom construction is required based on the API.
int64_t batchNum = 1;
int64_t headNum = 1;
int64_t seqSize = 2;
int64_t headDim = 4;
int64_t headSize = headNum * headDim;
std::vector<int64_t> prevAttnOutShape = {seqSize, batchNum, headSize};
std::vector<int64_t> prevSoftmaxMaxShape = {batchNum, headNum, seqSize, 8};
std::vector<int64_t> prevSoftmaxSumShape = {batchNum, headNum, seqSize, 8};
std::vector<int64_t> curAttnOutShape = {seqSize, batchNum, headSize};
std::vector<int64_t> curSoftmaxMaxShape = {batchNum, headNum, seqSize, 8};
std::vector<int64_t> curSoftmaxSumShape = {batchNum, headNum, seqSize, 8};
std::vector<int64_t> actualSeqQlenOptionalShape = {batchNum, headNum};
std::vector<int64_t> attnOutShape = {seqSize, batchNum, headSize};
std::vector<int64_t> softmaxMaxShape = {batchNum, headNum, seqSize, 8};
std::vector<int64_t> softmaxSumShape = {batchNum, headNum, seqSize, 8};
void* prevAttnOutDeviceAddr = nullptr;
void* prevSoftmaxMaxDeviceAddr = nullptr;
void* prevSoftmaxSumDeviceAddr = nullptr;
void* curAttnOutDeviceAddr = nullptr;
void* curSoftmaxMaxDeviceAddr = nullptr;
void* curSoftmaxSumDeviceAddr = nullptr;
void* actualSeqQlenOptionalDeviceAddr = nullptr;
void* attnOutDeviceAddr = nullptr;
void* softmaxMaxDeviceAddr = nullptr;
void* softmaxSumDeviceAddr = nullptr;
aclTensor* prevAttnOut = nullptr;
aclTensor* prevSoftmaxMax = nullptr;
aclTensor* prevSoftmaxSum = nullptr;
aclTensor* curAttnOut = nullptr;
aclTensor* curSoftmaxMax = nullptr;
aclTensor* curSoftmaxSum = nullptr;
aclTensor* actualSeqQlenOptional = nullptr;
aclTensor* attnOut = nullptr;
aclTensor* softmaxMax = nullptr;
aclTensor* softmaxSum = nullptr;
std::vector<float> prevAttnOutHostData(seqSize * batchNum * headSize, 1);
std::vector<float> prevSoftmaxMaxHostData(batchNum * headNum * seqSize * 8, 1);
std::vector<float> prevSoftmaxSumHostData(batchNum * headNum * seqSize * 8, 1);
std::vector<float> curAttnOutHostData(seqSize * batchNum * headSize, 1);
std::vector<float> curSoftmaxMaxHostData(batchNum * headNum * seqSize * 8, 1);
std::vector<float> curSoftmaxSumHostData(batchNum * headNum * seqSize * 8, 1);
std::vector<float> actualSeqQlenOptionalHostData(batchNum * headNum, 1);
std::vector<float> attnOutHostData(seqSize * batchNum * headSize, 1);
std::vector<float> softmaxMaxHostData(batchNum * headNum * seqSize * 8, 1);
std::vector<float> softmaxSumHostData(batchNum * headNum * seqSize * 8, 1);
char* inputLayoutOptional = "SBH";
// Create the prevAttnOut aclTensor.
ret = CreateAclTensor(prevAttnOutHostData, prevAttnOutShape, &prevAttnOutDeviceAddr, aclDataType::ACL_FLOAT, &prevAttnOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the prevSoftmaxMax aclTensor.
ret = CreateAclTensor(prevSoftmaxMaxHostData, prevSoftmaxMaxShape, &prevSoftmaxMaxDeviceAddr, aclDataType::ACL_FLOAT, &prevSoftmaxMax);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the prevSoftmaxSum aclTensor.
ret = CreateAclTensor(prevSoftmaxSumHostData, prevSoftmaxSumShape, &prevSoftmaxSumDeviceAddr, aclDataType::ACL_FLOAT, &prevSoftmaxSum);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the curAttnOut aclTensor.
ret = CreateAclTensor(curAttnOutHostData, curAttnOutShape, &curAttnOutDeviceAddr, aclDataType::ACL_FLOAT, &curAttnOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the curSoftmaxMax aclTensor.
ret = CreateAclTensor(curSoftmaxMaxHostData, curSoftmaxMaxShape, &curSoftmaxMaxDeviceAddr, aclDataType::ACL_FLOAT, &curSoftmaxMax);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the curSoftmaxSum aclTensor.
ret = CreateAclTensor(curSoftmaxSumHostData, curSoftmaxSumShape, &curSoftmaxSumDeviceAddr, aclDataType::ACL_FLOAT, &curSoftmaxSum);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the actualSeqQlenOptional aclTensor.
ret = CreateAclTensor(actualSeqQlenOptionalHostData, actualSeqQlenOptionalShape, &actualSeqQlenOptionalDeviceAddr, aclDataType::ACL_INT64, &actualSeqQlenOptional);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the attnOut aclTensor.
ret = CreateAclTensor(attnOutHostData, attnOutShape, &attnOutDeviceAddr, aclDataType::ACL_FLOAT, &attnOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create softmaxMax aclTensor.
ret = CreateAclTensor(softmaxMaxHostData, softmaxMaxShape, &softmaxMaxDeviceAddr, aclDataType::ACL_FLOAT, &softmaxMax);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create softmaxSum aclTensor.
ret = CreateAclTensor(softmaxSumHostData, softmaxSumShape, &softmaxSumDeviceAddr, aclDataType::ACL_FLOAT, &softmaxSum);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library APIs. Modify them to the specific APIs.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnRingAttentionUpdate.
ret = aclnnRingAttentionUpdateGetWorkspaceSize(prevAttnOut, prevSoftmaxMax, prevSoftmaxSum,
curAttnOut, curSoftmaxMax, curSoftmaxSum,
actualSeqQlenOptional, inputLayoutOptional,
attnOut, softmaxMax, softmaxSum, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnRingAttentionUpdateGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory based on the workspaceSize calculated 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 aclnnRingAttentionUpdate.
ret = aclnnRingAttentionUpdate(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnRingAttentionUpdate failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Synchronize and wait until the task execution is 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 device memory to host memory. Modify it based on the specific API definition.
auto attnOutSize = GetShapeSize(attnOutShape);
std::vector<float> attnOutResultData(attnOutSize, 0);
ret = aclrtMemcpy(attnOutResultData.data(), attnOutResultData.size() * sizeof(attnOutResultData[0]), attnOutDeviceAddr, attnOutSize * sizeof(float),
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);
for (int64_t i = 0; i < attnOutSize; i++) {
LOG_PRINT("attnOutResultData[%ld] is: %f\n", i, attnOutResultData[i]);
}
auto softmaxMaxSize = GetShapeSize(softmaxMaxShape);
std::vector<float> softmaxMaxResultData(softmaxMaxSize, 0);
ret = aclrtMemcpy(softmaxMaxResultData.data(), softmaxMaxResultData.size() * sizeof(softmaxMaxResultData[0]), softmaxMaxDeviceAddr, softmaxMaxSize * sizeof(float),
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);
for (int64_t i = 0; i < softmaxMaxSize; i++) {
LOG_PRINT("softmaxMaxResultData[%ld] is: %f\n", i, softmaxMaxResultData[i]);
}
auto softmaxSumSize = GetShapeSize(softmaxSumShape);
std::vector<float> softmaxSumResultData(softmaxSumSize, 0);
ret = aclrtMemcpy(softmaxSumResultData.data(), softmaxSumResultData.size() * sizeof(softmaxSumResultData[0]), softmaxSumDeviceAddr, softmaxSumSize * sizeof(float),
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);
for (int64_t i = 0; i < softmaxSumSize; i++) {
LOG_PRINT("softmaxSumResultData[%ld] is: %f\n", i, softmaxSumResultData[i]);
}
// 6. Release aclTensor and aclScalar. Modify it based on the specific API definition.
aclDestroyTensor(prevAttnOut);
aclDestroyTensor(prevSoftmaxMax);
aclDestroyTensor(prevSoftmaxSum);
aclDestroyTensor(curAttnOut);
aclDestroyTensor(curSoftmaxMax);
aclDestroyTensor(curSoftmaxSum);
aclDestroyTensor(actualSeqQlenOptional);
aclDestroyTensor(attnOut);
aclDestroyTensor(softmaxMax);
aclDestroyTensor(softmaxSum);
// 7. Release device resources. Modify it based on the specific API definition.
aclrtFree(prevAttnOutDeviceAddr);
aclrtFree(prevSoftmaxMaxDeviceAddr);
aclrtFree(prevSoftmaxSumDeviceAddr);
aclrtFree(curAttnOutDeviceAddr);
aclrtFree(curSoftmaxMaxDeviceAddr);
aclrtFree(curSoftmaxSumDeviceAddr);
aclrtFree(actualSeqQlenOptionalDeviceAddr);
aclrtFree(attnOutDeviceAddr);
aclrtFree(softmaxMaxDeviceAddr);
aclrtFree(softmaxSumDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}