aclnnRingAttentionUpdateV2
Applicable Products
| Product | Supported |
|---|---|
| √ | |
| √ | |
| √ | |
| × | |
| × | |
| × |
Function
Description: Updates the outputs of two FlashAttention operations based on their different softmax max and sum values. The difference from the RingAttentionUpdate API is: in scenarios where the input layout is TND, the softmax-related inputs in the original RingAttentionUpdate API use the BNS8 data layout, whereas the RingAttentionUpdateV2 API supports passing a string parameter inputSoftmaxLayout to control whether the data layout of softmax-related inputs is consistent with that of attention (i.e., using the TND layout).
Formula:
Function Prototype
Each operator is divided into a two-phase API. You must first call the "aclnnRingAttentionUpdateV2GetWorkspaceSize" API to obtain the workspace size required for computation and the executor that contains the operator computation process, and then call the "aclnnRingAttentionUpdateV2" API to perform the computation.
aclnnStatus aclnnRingAttentionUpdateV2GetWorkspaceSize(
const aclTensor *prevAttnOut,
const aclTensor *prevSoftmaxMax,
const aclTensor *prevSoftmaxSum,
const aclTensor *curAttnOut,
const aclTensor *curSoftmaxMax,
const aclTensor *curSoftmaxSum,
const aclTensor *actualSeqQlenOptional,
char *inputLayoutOptional,
char *inputSoftmaxLayoutOptional,
const aclTensor *attnOutOut,
const aclTensor *softmaxMaxOut,
const aclTensor *softmaxSumOut,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnRingAttentionUpdateV2(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnRingAttentionUpdateV2GetWorkspaceSize
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 be 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 softmax max result from the first FlashAttention. The eight numbers in the last dimension are identical and must be positive. FLOAT ND [B,N,S,8]、[T,N,8] √ prevSoftmaxSum (aclTensor*) Input prev_softmax_sum in the formula, which is the softmax sum result of the first FlashAttention. The input shape must be consistent with prevSoftmaxMax, with the last eight numbers in the last dimension being identical and positive. 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 are 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 is the max result of softmax from the second FlashAttention. The input shape must be consistent with prevSoftmaxMax, with the last eight numbers in the last dimension being identical and positive. FLOAT ND [B,N,S,8], [T,N,8] √ curSoftmaxSum (aclTensor*) Input. The cur_softmax_sum in the formula, which is the softmax sum result of the second FlashAttention. The input shape must be consistent with prevSoftmaxMax, with the last dimension having eight identical numbers, and must be positive. FLOAT ND [B,N,S,8], [T,N,8] √ actualSeqQlenOptional (aclTensor*) Input Cumulative sum of sequence lengths starting from 0. When the data layout inputLayoutOptional is TND, this parameter must be passed. It is an integer aclTensor incrementing from 0 to T. INT64 - - - inputLayoutOptional (char*) Input Data layout of prevAttnOut and curAttnOut. Currently supports "TND" and "SBH". - - - - inputSoftmaxLayoutOptional (char*) Input Data layout of prevSoftmaxMax, prevSoftmaxSum, curSoftmaxMax, and curSoftmaxSum. Takes effect when inputLayoutOptional is TND. This switch controls whether to transpose the softmaxMax-related inputs. Currently supports an empty string, "SBH", and "TND". - - - - attnOutOut (aclTensor*) Output attn_out in the formula, the output updated by the two results. The data type and output shape are consistent with prevAttnOut. FLOAT16, FLOAT, BFLOAT16 ND [T,N,D], [S,B,H] √ softmaxMaxOut (aclTensor*) Output softmax_max in the formula, which is the max of the softmax after two result updates. The output shape is consistent with prevSoftmaxMax. FLOAT ND [B,N,S,8], [T,N,8] √ softmaxSumOut (aclTensor*) Output softmax_sum in the formula, representing the sum of softmax updated by the two results. The output shape is consistent with prevSoftmaxMax. FLOAT ND [B,N,S,8]、[T,N,8] √ workspaceSize (uint64_t*) Output Size of the workspace to be applied for on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the operator computation process. - - - - - Atlas A2 training products/Atlas A2 inference products ,Atlas A3 training products/Atlas A3 inference products : When the input data layout inputLayoutOptional is TND, D is constrained to be a multiple of 64.
Return Value
aclnnStatus: return 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 passed prevAttnOut, prevSoftmaxMax, prevSoftmaxSum, curAttnOut, curSoftmaxMax, curSoftmaxSum, attnOutOut, softmaxMaxOut, or softmaxSumOut is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data types of prevAttnOut, prevSoftmaxMax, prevSoftmaxSum, curAttnOut, curSoftmaxMax, curSoftmaxSum, attnOutOut, softmaxMaxOut, and softmaxSumOut are not within the supported range. The shapes of prevAttnOut, prevSoftmaxMax, prevSoftmaxSum, curAttnOut, curSoftmaxMax, curSoftmaxSum, attnOutOut, softmaxMaxOut, and softmaxSumOut are not supported. ACLNN_ERR_INNER_TILING_ERROR 561002 When actualSeqQlenOptional is provided, the input data format is not within the supported range. The inputSoftmaxLayoutOptional input value is not within the supported range.
aclnnRingAttentionUpdateV2
Parameters
Parameter Input/Output Description workspace Input Workspace memory address applied on the Device side. workspaceSize Input Workspace size applied on the Device side, obtained from the first-phase API aclnnRingAttentionUpdateV2GetWorkspaceSize. executor Input Operator executor that contains the operator computation process. stream Input Specifies the stream for task execution. Return Value
aclnnStatus: return code. For details, see aclnn Return Code.
Constraints
Deterministic computation:
- aclnnRingAttentionUpdateV2 defaults to a deterministic implementation.
When inputLayoutOptional is set to "TND", actualSeqQlenOptional is mandatory.
When inputLayoutOptional is set to "TND":
N:
Atlas A2 training products/Atlas A2 inference products ,Atlas A3 training products/Atlas A3 inference products : N<=256.
D:
Atlas A2 training products/Atlas A2 inference products ,Atlas A3 training products/Atlas A3 inference products : D<=768 and D is a multiple of 64.
inputSoftmaxLayoutOptional takes effect only when inputLayoutOptional is set to "TND". inputSoftmaxLayoutOptional supports only three inputs: an empty string, "SBH", and "TND".
Example
The following provides example code for reference only. For details about compilation and running, 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 shapeSize = 1;
for (auto i : shape) {
shapeSize *= i;
}
return shapeSize;
}
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 apply for device-side memory.
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-side data to device-side 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 a 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) Initialize the device/stream. See the ACL API manual.
// Enter the deviceId based on your actual device.
int32_t deviceId = 0;
aclrtStream stream;
auto ret = Init(deviceId, &stream);
CHECK_RET(ret == ACL_SUCCESS, 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 = 64;
int64_t headSize = headNum * headDim;
std::vector<int64_t> prevAttnOutShape = {seqSize, batchNum, headSize};
std::vector<int64_t> prevSoftmaxMaxShape = {batchNum * seqSize, headNum, 8};
std::vector<int64_t> prevSoftmaxSumShape = {batchNum * seqSize, headNum, 8};
std::vector<int64_t> curAttnOutShape = {seqSize, batchNum, headSize};
std::vector<int64_t> curSoftmaxMaxShape = {batchNum * seqSize, headNum, 8};
std::vector<int64_t> curSoftmaxSumShape = {batchNum * seqSize, headNum, 8};
std::vector<int64_t> actualSeqQlenOptionalShape = {batchNum, headNum};
std::vector<int64_t> attnOutShape = {seqSize, batchNum, headSize};
std::vector<int64_t> softmaxMaxShape = {batchNum * seqSize, headNum, 8};
std::vector<int64_t> softmaxSumShape = {batchNum * seqSize, headNum, 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 = "TND";
char* inputSoftmaxLayoutOptional = "TND";
// Create prevAttnOut aclTensor.
ret = CreateAclTensor(prevAttnOutHostData, prevAttnOutShape, &prevAttnOutDeviceAddr, aclDataType::ACL_FLOAT, &prevAttnOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create prevSoftmaxMax aclTensor.
ret = CreateAclTensor(prevSoftmaxMaxHostData, prevSoftmaxMaxShape, &prevSoftmaxMaxDeviceAddr, aclDataType::ACL_FLOAT, &prevSoftmaxMax);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create prevSoftmaxSum aclTensor.
ret = CreateAclTensor(prevSoftmaxSumHostData, prevSoftmaxSumShape, &prevSoftmaxSumDeviceAddr, aclDataType::ACL_FLOAT, &prevSoftmaxSum);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create curAttnOut aclTensor.
ret = CreateAclTensor(curAttnOutHostData, curAttnOutShape, &curAttnOutDeviceAddr, aclDataType::ACL_FLOAT, &curAttnOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create curSoftmaxMax aclTensor.
ret = CreateAclTensor(curSoftmaxMaxHostData, curSoftmaxMaxShape, &curSoftmaxMaxDeviceAddr, aclDataType::ACL_FLOAT, &curSoftmaxMax);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create curSoftmaxSum aclTensor.
ret = CreateAclTensor(curSoftmaxSumHostData, curSoftmaxSumShape, &curSoftmaxSumDeviceAddr, aclDataType::ACL_FLOAT, &curSoftmaxSum);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create actualSeqQlenOptional aclTensor.
ret = CreateAclTensor(actualSeqQlenOptionalHostData, actualSeqQlenOptionalShape, &actualSeqQlenOptionalDeviceAddr, aclDataType::ACL_FLOAT, &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 the softmaxMax aclTensor.
ret = CreateAclTensor(softmaxMaxHostData, softmaxMaxShape, &softmaxMaxDeviceAddr, aclDataType::ACL_FLOAT, &softmaxMax);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the softmaxSum aclTensor.
ret = CreateAclTensor(softmaxSumHostData, softmaxSumShape, &softmaxSumDeviceAddr, aclDataType::ACL_FLOAT, &softmaxSum);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Replace with the actual API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnRingAttentionUpdateV2.
ret = aclnnRingAttentionUpdateV2GetWorkspaceSize(prevAttnOut, prevSoftmaxMax, prevSoftmaxSum,
curAttnOut, curSoftmaxMax, curSoftmaxSum,
actualSeqQlenOptional, inputLayoutOptional, inputSoftmaxLayoutOptional,
attnOut, softmaxMax, softmaxSum, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnRingAttentionUpdateV2GetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory 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 aclnnRingAttentionUpdateV2.
ret = aclnnRingAttentionUpdateV2(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnRingAttentionUpdateV2 failed. ERROR: %d\n", ret); return ret);
// 4. (boilerplate) Synchronize and wait for 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 device memory to the host side. Modify 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 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 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;
}