aclnnFlashAttentionScoreV4
Applicable Products
Ascend 950PR&950DT products : Supported
Atlas A3 products : Not 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 training use cases, this API implements self-attention computation using the FlashAttention algorithm. To align with competitor products and adapt to the gptoss model, this API supports the sink feature. The query, key, and value parameters of this API support multiple sequences of equal or unequal lengths
- Compared with the FlashAttentionScoreV3 API, the functional differences of this API are as follows::
- Dropout function adjustment: When keepProb is less than 1.0, if no external DropoutMask is passed in, the newly added parameters seed and offset are used to generate the DropoutMask; if an external DropoutMask is passed in, the external DropoutMask is used. Added the optional input sinkOptional, which supports the FLOAT32 data type.
- Compared with the FlashAttentionVarLenScoreV5 API, the functional differences are as follows:
- Dropout function adjustment: When keepProb is less than 1.0, if no external DropoutMask is passed in, the newly added parameters seed and offset are used to generate the DropoutMask; if an external DropoutMask is passed in, the external DropoutMask is used.
- Compared with the FlashAttentionScoreV3 API, the functional differences of this API are as follows::
Formula:
The forward calculation formula of attention is as follows:
When psetype = 1, the formula is the same as that of FlashAttentionScore.
When psetype is set to other values, the formula is as follows:
$ attention\_out=Dropout(Softmax(Mask(scale*(query*key^T) + pse),atten\_mask),keep\_prob)*value $After sink is added, the calculation logic is as follows. The main modifications are in the softmax_max and softmax_sum calculation logic.
$$ S = Q * K^{T} $$$$ m = max(sink, max(S)) $$$$ Attention = \frac{e^{S - m} * V}{\sum e^{S-m} + e^{sink - m}} $$
Function Prototype
Each operator uses a two-phase API. Call the "aclnnFlashAttentionScoreV4GetWorkspaceSize" API first to obtain the workspace size required for calculation and the executor that includes the calculation process of the operator, and then call the "aclnnFlashAttentionScoreV4" API to perform the calculation.
aclnnStatus aclnnFlashAttentionScoreV4GetWorkspaceSize(
const aclTensor *query,
const aclTensor *key,
const aclTensor *value,
const aclTensor *realShiftOptional,
const aclTensor *dropMaskOptional,
const aclTensor *paddingMaskOptional,
const aclTensor *attenMaskOptional,
const aclTensor *queryRopeOptional,
const aclTensor *keyRopeOptional,
const aclTensor *dScaleQOptional,
const aclTensor *dScaleKOptional,
const aclTensor *dScaleVOptional,
const aclTensor *sinkOptional,
const aclIntArray *prefixOptional,
const aclIntArray *actualSeqQLenOptional,
const aclIntArray *actualSeqKvLenOptional,
const aclIntArray *qStartIdxOptional,
const aclIntArray *kvStartIdxOptional,
double scaleValue,
double keepProb,
int64_t preTokens,
int64_t nextTokens,
int64_t headNum,
char *inputLayout,
int64_t innerPrecise,
int64_t sparseMode,
int64_t outDtype,
int64_t pseType,
char *softmaxOutLayout,
int64_t seed,
int64_t offset,
const aclTensor *softmaxMaxOut,
const aclTensor *softmaxSumOut,
const aclTensor *softmaxOutOut,
const aclTensor *attentionOutOut,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnFlashAttentionScoreV4(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
const aclrtStream stream)aclnnFlashAttentionScoreV4GetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor query Input query in the formula. The data type must be the same as the data type of key/value. FLOAT16, BFLOAT16, FLOAT32 ND 0, 3, 4 √ key Input key in the formula. The data type must be the same as the data type of query/value. FLOAT16, BFLOAT16, FLOAT32 ND 0, 3, 4 √ value Input value in the formula. The data type must be the same as the data type of query/key. FLOAT16, BFLOAT16, FLOAT32 ND 0, 3, 4 √ realShiftOptional Optional input pse in the formula. The data type is the same as that of attentionOut. FLOAT16, BFLOAT16, FLOAT32 ND [B,N,Sq,Skv], [B,N,1,Skv], [1,N,Sq,Skv], [B,N,1024,Skv], [1,N,1024,Skv], [B,N], [N] √ paddingMaskOptional Input Currently not used. - - - - - dropMaskOptional Input Dropout in the formula. - UINT8 ND 0, 1 √ attenMaskOptional Input atten_mask in the formula. A value of 1 indicates that the bit is not involved in calculation, and 0 indicates that the bit is involved in calculation. BOOL, UINT8 ND [B,N,S,S], [B,1,S,S], [1,1,S,S], [S,S] √ queryRopeOptional Input aclTensor on the Device side. The data type is the same as that of query. FLOAT16, BFLOAT16 ND 0, 3, 4 √ keyRopeOptional Input aclTensor on the device side. The data type is the same as that of key. FLOAT16, BFLOAT16 ND 0, 3, 4 √ dScaleQOptional Input Quantization parameter of query. The input shape is [B,N1,Ceil(Sq/128),1]. FLOAT32 ND 0, 3, 4 √ dScaleKOptional Input Quantization parameter of key. The input shape is [B,N2,Ceil(Skv/256),1]. FLOAT32 ND 0, 3, 4 √ dScaleVOptional Input Quantization parameter of value. The input shape is [B,N2,Ceil(Skv/512),1]. FLOAT32 ND 0, 3, 4 √ sinkOptional Input sink in the formula, which provides the function of resolving the problem that attention calculation concentrates on the first part of TOKENs. When the input is not a null pointer, only one-dimensional Tensor input is supported. FLOAT32 ND [N1] √ prefixOptional Input Indicates the N value of each Batch in the prefix sparse calculation use case. - INT64 ND 0, 1 - actualSeqQLenOptional Input Describes the sequence length of the query for each batch. - INT64 ND 0, 1 - actualSeqKvLenOptional Input Describes the sequence length of key/value corresponding to each Batch. - INT64 ND 0, 1 - qStartIdxOptional Input Indicates the start index of the query sequence of the current chunk in the global sequence in the outer-loop scenario. - INT64 ND 0, 1 - kvStartIdxOptional Input Indicates the start index of the key and value sequences of the current chunk in the global sequence, for the external splitting use case. - INT64 ND 0, 1 - scaleValue Input scale in the formula. It indicates the scaling factor. - DOUBLE - - - keepProb Input Indicates the proportion of 1 in dropMaskOptional. Value range: (0, 1]. DOUBLE - - - preTokens Input Used for sparse calculation. Indicates the left boundary of the sliding window. - INT64 - - - nextTokens Input Used for sparse calculation. Indicates the right boundary of the sliding window. - INT64 - - - headNum Input Indicates the number of heads on each device, that is, the length of the N axis of the input query. - INT64 - - - inputLayout Input Indicates the data layout format of the input query, key, and value. Supports BSH, SBH, BSND, BNSD, and TND. String - - - innerPrecise Input int64_t on the host. When configured as 2, invalid row processing is enabled. Do not configure this parameter unless necessary. INT64 - - - sparseMode Input Indicates the sparse mode. Supported configuration values are 0, 1, 2, 3, 4, 5, and 6. INT64 - - - outDtype Input int64_t on the host. Takes effect only in the quantization scenario. 0 indicates that the attenout output is in fp16 format, and 1 indicates that the output is in bf16 format. INT64 - - - softmaxOutLayout Input Controls the softmax output in the TND scenario. When "same_as_input" is passed in, the softmax output layout is the same as that of the input, that is, the TN8 layout; when an empty string is passed in, the logic is the same as the original, and the softmax output layout is BNS8. String - - - pseType Input int64_t on the host. Controls the calculation order of mul and add. The value can be 0, 1, 2, or 3. INT64 - - - seed Input int64_t on the host. Seed for dropmask generation. This parameter needs to be passed in when internal dropmask generation is enabled. Otherwise, the default value seed=0 is used for internal dropmask generation. INT64 - - - offset Input Offset for dropmask generation. If internal dropmask generation is enabled, this parameter needs to be passed in. Otherwise, the default value offset=0 is used for internal dropmask generation. INT64 - - - softmaxMaxOut Output Max intermediate result of the Softmax calculation, used for backward calculation. The output shape is determined by softmaxOutLayout. FLOAT ND 0, 3, 4 √ softmaxSumOut Output Sum intermediate result of the Softmax calculation, used for backward propagation. The output shape is determined by softmaxOutLayout. FLOAT ND 0, 3, 4 √ attentionOutOut Output Final output of the formula. The data type and shape are the same as those of query. FLOAT16, BFLOAT16, FLOAT32 ND 0, 3, 4 √ workspaceSize Output Returns the workspace size to be applied on the Device side. - - - - - executor Output Returns the op executor, which includes calculation flow of the operator. - - - - - Return value
aclnnStatus: Returns the 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 passed-in parameter is a required input, output, or required attribute and is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data types of query, key, value, realShiftOptional, dropMaskOptional, paddingMaskOptional, attenMaskOptional, sinkOptional, softmaxMaxOut, softmaxSumOut, softmaxOutOut, and attentionOutOut are not within the supported range.
aclnnFlashAttentionScoreV4
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 by the first-phase API aclnnFlashAttentionScoreV4GetWorkspaceSize. executor Input op executor, includes calculation flow of the operator. stream Input Stream used to execute the task. Return value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
Deterministic computation:
- aclnnFlashAttentionScoreV4 defaults to a deterministic implementation.
The input query, key, and value
- B: The batchsize must be equal.
- D: The head dimensions must satisfy (qD == kD && kD >= vD).
- The inputLayout must be consistent.
The input queryRopeOptional and query have the same input shape except in the D dimension. Other shape parameters should be identical.
The input keyRopeOptional and key have the same input shape except in the D dimension. Other shape parameters should be identical.
MLA concat use case: only dSize=128 and dSizeRope=64 are supported
Constraints on data shapes, taking TND, BSND, and BNSD of inputLayout as examples (H = N * D for BSH and SBH), where:
- T (B * S): The value range is 1 to 1M. In TND format, the maximum supported length of actualSeqQLenOptional is 20000.
- B: The value range is 1 to 2M. When prefixOptional is present, B supports a maximum of 2K.
- N: ranges from 1 to 256.
- S: ranges from 1 to 1M.
- D: ranges from 1 to 768.
Description of the empty tensor scenario:
- If any of B, N1 (headNum), or S1 (sequence length of query) is 0, the operator performs no computation and returns success directly, with workspaceSize being 0.
- If any of N2 (number of heads of key/value), S2 (sequence length of key/value), or D (Head-Dim) is 0, query/key/value are empty tensors (shapeSize is 0), but the output shapes of softmaxMaxOut and softmaxSumOut may be non-empty (the shapes do not depend on the D dimension). In this case, the operator performs empty input processing and initializes the non-empty outputs.
- In the preceding empty tensor use cases, the operator returns normally and no error is reported.
The data layout format of query, key, and value supports interpretation from multiple dimensions, where B (Batch) indicates the input sample batch size, S (Seq-Length) indicates the input sample sequence length, H (Head-Size) indicates the hidden layer size, N (Head-Num) indicates the number of heads, D (Head-Dim) indicates the minimum unit dimension of the hidden layer, and D = H/N is satisfied.
innerPrecise: Currently, 0 and 1 are reserved configuration values, and 2 enables invalid row computation to avoid precision loss caused by entire-row masks during computation. However, this configuration degrades performance. If the operator can detect invalid rows, it automatically enables invalid row computation, for example, when sparseMode is 3 and Sq > Skv.
Meaning of each pseType value
pseType Meaning Note 0 pse passed in when calling the operator: mul first, then add. - 1 pse passed in when calling the operator: add first, then mul. Consistent with the implementation of FlashAttentionScore. 2 pse automatically generated by the operator: mul first, then add. - 3 pse automatically generated by the operator: mul first, then add, then sqrt. - When pseType is 2 or 3, only equal-length Sq and Skv are supported.
If sinkOptional is not None, input tensors such as query and key support only float16 and bfloat16.
The constraints on sparseMode are as follows:
- When all attenMaskOptional shapes are smaller than 2048 and identical, it is recommended to use the default mode to reduce memory usage.
- When sparseMode is set to 1, 2, 3, or 5, the user-configured preTokens and nextTokens do not take effect.
- When configured as 0 or 4, ensure that attenMaskOptional is consistent with the ranges of preTokens and nextTokens.
- If not explicitly specified, it is recommended to pass 0.
- For details about different sparse modes, see sparse mode description.
In some scenarios, if the computation workload is too large, the operator execution may time out (aicore error with errorStr: timeout or trap error). In this case, axis splitting is recommended. Note: The computation workload here is affected by parameters such as B, S, N, and D. Larger values result in a heavier workload.
In the band scenario, preTokens and nextTokens must have an intersection.
For the prefixOptional sparse computation scenario, the scenarios include sparseMode=5 and sparseMode=6 when sequences have equal lengths, and sparseMode=6 when sequences have unequal lengths. In these two scenarios, when Sq > Skv, the N value range of prefix is [0, Skv]; when Sq <= Skv, the N value range of prefix is [Skv-Sq, Skv]. When sparseModeOptional=5 and prefix N > Skv, or when prefixOptional is not passed, full computation is performed. sparseModeOptional=6 requires that prefixOptional must be passed.
realShiftOptional: When Sq is greater than 1024 and BNHS or 1NHS format is configured, Sq and Skv must be of equal length.
The actualSeqQLenOptional input supports an S length of 0 on a certain batch. In this case, the optional input realShiftOptional is not supported.
In TND use cases, the attenMaskOptional input does not support padding. That is, no row in attenMaskOptional can be all 1s.
An S length of 0 on a certain batch in actualSeqQLenOptional is supported. If S is 0, the pse input is not supported. Assume that the actual S length is [2, 2, 0, 2, 2], then the passed actualSeqQLenOptional is [2, 4, 4, 6, 8].
Ascend 950PR&950DT products :- seed and offset take effect only when keepProb is less than 1.0. Otherwise, they do not take effect.
- When keepProb is less than 1.0, if dropMaskOptional is not nullptr, the input dropMask is used. Otherwise, the dropMask generated from seed and offset is used.
- In TND format, trailing batches that do not participate in computation are supported. In this case, pass the corresponding number of zeros at the end of actual_seq_q_len and actual_seq_kv_len. Assume the actual S length is [2, 3, 4, 5, 6]. If you want the last two batches to not participate in computation, pass actual_seq_q_len as [2, 3, 4, 0, 0]. In this case, if prefixOptional needs to be passed, its trailing part must also contain the same number of zeros, for example, [1, 1, 1, 0, 0].
- softmaxOutLayout supports the following input values: an empty string or "same_as_input".
- realShiftOptional: If Sq is greater than 1024, the Sq of each batch is equal in length to Skv, and the use case is a lower-triangular mask with sparseMode set to 0, 2, or 3, alibi position encoding compression can be enabled. In this case, only the last 1024 rows of the original PSE need to be input to optimize memory usage, that is, alibi_compress = ori_pse[:, :, -1024:, :]. The details are as follows:
- When the parameter differs for each batch, the shape is BNHSkv (H = 1024).
- When the parameter is the same for each batch, the shape is 1NHSkv (H = 1024).
- If pseType is 2 or 3, the data type must be FLOAT32, and the supported shape range is [B,N] or [N].
- If this parameter is not enabled, realShiftOptional must be passed as nullptr and pseType must be passed as 1.
Example
The sample code for the API call is as follows, for reference only. For details about compilation and execution, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include <cmath>
#include "acl/acl.h"
#include "aclnn/opdev/fp16_t.h"
#include "aclnnop/aclnn_flash_attention_score.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;
}
void PrintOutResult(std::vector<int64_t> &shape, void** deviceAddr) {
auto size = GetShapeSize(shape);
std::vector<float> resultData(size, 0);
auto ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]),
*deviceAddr, size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return);
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("mean result[%ld] is: %f\n", i, resultData[i]);
}
}
void PrintOutResultFp16(std::vector<int64_t> &shape, void** deviceAddr) {
auto size = GetShapeSize(shape);
std::vector<aclFloat16> resultData(size, 0);
auto ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]),
*deviceAddr, size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return);
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("mean result[%ld] is: %f\n", i, aclFloat16ToFloat(resultData[i]));
}
}
int Init(int32_t deviceId, aclrtContext* context, aclrtStream* stream) {
// Fixed boilerplate: initialize AscendCL.
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 = aclrtCreateContext(context, deviceId);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateContext failed. ERROR: %d\n", ret); return ret);
ret = aclrtSetCurrentContext(*context);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetCurrentContext 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 device 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 data to 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. (Fixed boilerplate) Initialize device/context/stream. For details, see the AscendCL public API list.
// Set deviceId to the actual device.
int32_t deviceId = 0;
aclrtContext context;
aclrtStream stream;
auto ret = Init(deviceId, &context, &stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct the input and output. Custom construction is required based on the API.
int64_t B = 1;
int64_t N1 = 1;
int64_t N2 = 1;
int64_t S1 = 256;
int64_t S2 = 256;
int64_t D = 128;
int64_t H1 = N1 * D;
int64_t H2 = N2 * D;
int64_t q_size = S1 * B * H1;
int64_t kv_size = S2 * B * H2;
int64_t atten_mask_size = S1 * S2;
int64_t softmax_size = B * N1 * S1 * 8;
int64_t sink_size = N1;
std::vector<int64_t> qShape = {S1, B, H1};
std::vector<int64_t> kShape = {S2, B, H2};
std::vector<int64_t> vShape = {S2, B, H2};
std::vector<int64_t> attenmaskShape = {S1, S2};
std::vector<int64_t> sinkShape = {N1};
std::vector<int64_t> attentionOutShape = {S1, B, H1};
std::vector<int64_t> softmaxMaxShape = {B, N1, S1, 8};
std::vector<int64_t> softmaxSumShape = {B, N1, S1, 8};
void* qDeviceAddr = nullptr;
void* kDeviceAddr = nullptr;
void* vDeviceAddr = nullptr;
void* attenmaskDeviceAddr = nullptr;
void* sinkDeviceAddr = nullptr;
void* attentionOutDeviceAddr = nullptr;
void* softmaxMaxDeviceAddr = nullptr;
void* softmaxSumDeviceAddr = nullptr;
aclTensor* q = nullptr;
aclTensor* k = nullptr;
aclTensor* v = nullptr;
aclTensor* pse = nullptr;
aclTensor* dropMask = nullptr;
aclTensor* padding = nullptr;
aclTensor* attenmask = nullptr;
aclTensor* queryRope = nullptr;
aclTensor* keyRope = nullptr;
aclTensor* dScaleQ = nullptr;
aclTensor* dScaleK = nullptr;
aclTensor* dScaleV = nullptr;
aclTensor* sink = nullptr;
aclTensor* attentionOut = nullptr;
aclTensor* softmaxMax = nullptr;
aclTensor* softmaxSum = nullptr;
aclTensor* softmaxOut = nullptr;
std::vector<op::fp16_t> qHostData(q_size, 1.0);
std::vector<op::fp16_t> kHostData(kv_size, 1.0);
std::vector<op::fp16_t> vHostData(kv_size, 1.0);
std::vector<uint8_t> attenmaskHostData(atten_mask_size, 0);
std::vector<float> sinkHostData(sink_size, 3.0);
std::vector<op::fp16_t> attentionOutHostData(q_size, 255.0);
std::vector<float> softmaxMaxHostData(softmax_size, 3.0);
std::vector<float> softmaxSumHostData(softmax_size, 3.0);
ret = CreateAclTensor(qHostData, qShape, &qDeviceAddr, aclDataType::ACL_FLOAT16, &q);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(kHostData, kShape, &kDeviceAddr, aclDataType::ACL_FLOAT16, &k);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(vHostData, vShape, &vDeviceAddr, aclDataType::ACL_FLOAT16, &v);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(attenmaskHostData, attenmaskShape, &attenmaskDeviceAddr, aclDataType::ACL_UINT8, &attenmask);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(sinkHostData, sinkShape, &sinkDeviceAddr, aclDataType::ACL_FLOAT, &sink);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(attentionOutHostData, attentionOutShape, &attentionOutDeviceAddr, aclDataType::ACL_FLOAT16, &attentionOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(softmaxMaxHostData, softmaxMaxShape, &softmaxMaxDeviceAddr, aclDataType::ACL_FLOAT, &softmaxMax);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(softmaxSumHostData, softmaxSumShape, &softmaxSumDeviceAddr, aclDataType::ACL_FLOAT, &softmaxSum);
CHECK_RET(ret == ACL_SUCCESS, return ret);
std::vector<int64_t> prefixOp = {0};
std::vector<int64_t> qStartIdxOp = {0};
std::vector<int64_t> kvStartIdxOp = {0};
std::vector<int64_t> actualSeqQLenOp = {128};
std::vector<int64_t> actualSeqKVLenOp = {128};
aclIntArray *prefix = aclCreateIntArray(prefixOp.data(), 1);
aclIntArray *qStartIdx = aclCreateIntArray(qStartIdxOp.data(), 1);
aclIntArray *kvStartIdx = aclCreateIntArray(kvStartIdxOp.data(), 1);
aclIntArray* actualSeqQLen = aclCreateIntArray(actualSeqQLenOp.data(), 1);
aclIntArray* actualSeqKVLen = aclCreateIntArray(actualSeqKVLenOp.data(), 1);
double scaleValue = 0.088388;
double keepProb = 1;
int64_t preTokens = 65536;
int64_t nextTokens = 65536;
int64_t headNum = 1;
int64_t innerPrecise = 0;
int64_t sparseMode = 0;
int64_t outDtype = 0;
int64_t pseType = 1;
int64_t seed = 0;
int64_t offset = 0;
char layOut[5] = {'S', 'B', 'H', 0};
char *softmaxOutLayout = "same_as_input";
// 3. Call the CANN operator library API. Change it to the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnFlashAttentionScoreV4.
ret = aclnnFlashAttentionScoreV4GetWorkspaceSize(
q, k, v, pse, dropMask, padding, attenmask, queryRope, keyRope, dScaleQ, dScaleK, dScaleV, sink, prefix,
actualSeqQLen, actualSeqKVLen, qStartIdx, kvStartIdx, scaleValue, keepProb, preTokens, nextTokens,
headNum, layOut, innerPrecise, sparseMode, outDtype, pseType, softmaxOutLayout, seed, offset, softmaxMax, softmaxSum,
softmaxOut, attentionOut, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnFlashAttentionScoreV4GetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
// Allocate device memory according to 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 aclnnFlashAttentionScoreV4.
ret = aclnnFlashAttentionScoreV4(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnFlashAttentionScoreV4 failed. ERROR: %d\n", ret); return ret);
// 4. (boilerplate) Synchronously 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 on the device side to the host side. Modify it based on the specific API definition.
PrintOutResultFp16(attentionOutShape, &attentionOutDeviceAddr);
PrintOutResult(softmaxMaxShape, &softmaxMaxDeviceAddr);
PrintOutResult(softmaxSumShape, &softmaxSumDeviceAddr);
// 6. Release aclTensor and aclScalar. Modify it based on the specific API definition.
aclDestroyTensor(q);
aclDestroyTensor(k);
aclDestroyTensor(v);
aclDestroyTensor(attenmask);
aclDestroyTensor(sink);
aclDestroyTensor(attentionOut);
aclDestroyTensor(softmaxMax);
aclDestroyTensor(softmaxSum);
// 7. Release device resources.
aclrtFree(qDeviceAddr);
aclrtFree(kDeviceAddr);
aclrtFree(vDeviceAddr);
aclrtFree(attenmaskDeviceAddr);
aclrtFree(sinkDeviceAddr);
aclrtFree(attentionOutDeviceAddr);
aclrtFree(softmaxMaxDeviceAddr);
aclrtFree(softmaxSumDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtDestroyContext(context);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}