aclnnFlashAttentionScoreGradV4
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: Computes the backward output of attention in training use cases, that is, the backward computation of FlashAttentionScoreV4. The query, key, and value parameters of this API support multiple sequences of equal or unequal lengths
This API combines the FlashAttentionScoreGradV2 API and the FlashAttentionUnpaddingScoreGradV2 API, and adjusts the Dropout functionality:
Ascend 950PR&950DT products : When keepProb is less than 1.0, if no DropoutMask is passed in externally, a DropoutMask is generated using the newly added parameters; if a DropoutMask is passed in externally, the external DropoutMask is used
Formula:
- When pseType is 1, the formula is the same as that of FlashAttentionScoreGrad.
- For other values of pseType, the formula is as follows:
$$ Y=Dropout(Softmax(Mask(\frac{QK^T}{\sqrt{d}}+pse),atten\_mask),keep\_prob)V $$For ease of expression, variables $S$ and $P$ are used to represent the formulas:
$$ S=Mask(\frac{QK^T}{\sqrt{d}}+pse),atten\_mask $$$$ P=Dropout(Softmax(S),keep\_prob) $$$$ Y=PV $$The backward propagation formula for attention is:
$$ dV=P^TdY $$$$ dQ=\frac{((dS)*K)}{\sqrt{d}} $$$$ dK=\frac{((dS)^T*Q)}{\sqrt{d}} $$The computation logic after adding sink is as follows, mainly modifying the relevant softmax_max and softmax_sum logic computation parts.
$$ m = max(sink, max(S)) $$$$ Attention = \frac{e^{S - m} @ V}{\sum e^{S-m} + S^{sink - m}} $$$$ dSink = reduce(-Softmax(S) * dP * SimpleSoftmax(sink, x\_max, x\_sum)) $$NOTE: The data layout formats of query, keyIn, and value can be interpreted in multiple dimensions. T (Total S Length) indicates the total length of S of all batches. B (Batch) indicates the input batch size. S (Seq-Length) indicates the input sequence length. H (Head-Size) indicates the hidden layer size. N (Head-Num) indicates the number of heads. d (Head-Dim) indicates the smallest unit size of the hidden layer, where d=H/N.
Function Prototype
Each operator uses a two-phase API. You must first call the "aclnnFlashAttentionScoreGradV4GetWorkspaceSize" API to obtain the workspace size required for the computation and the executor containing the operator computation process, and then call the "aclnnFlashAttentionScoreGradV4" API to perform the computation.
aclnnStatus aclnnFlashAttentionScoreGradV4GetWorkspaceSize(
const aclTensor *query,
const aclTensor *keyIn,
const aclTensor *value,
const aclTensor *dy,
const aclTensor *pseShiftOptional,
const aclTensor *dropMaskOptional,
const aclTensor *paddingMaskOptional,
const aclTensor *attenMaskOptional,
const aclTensor *softmaxMaxOptional,
const aclTensor *softmaxSumOptional,
const aclTensor *softmaxInOptional,
const aclTensor *attentionInOptional,
const aclTensor *sinkInOptional,
const aclTensor *queryRopeOptional,
const aclTensor *keyRopeOptional,
const aclTensor *dScaleQOptional,
const aclTensor *dScaleKOptional,
const aclTensor *dScaleVOptional,
const aclTensor *dScaleDyOptional,
const aclTensor *dScaleOOptional,
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,
char *softmaxInLayout,
int64_t innerPrecise,
int64_t sparseMode,
int64_t pseType,
int64_t seed,
int64_t offset,
int64_t outDtype,
const aclTensor *dqOut,
const aclTensor *dkOut,
const aclTensor *dvOut,
const aclTensor *dqRopeOut,
const aclTensor *dkRopeOut,
const aclTensor *dpseOut,
const aclTensor *dsinkOut,
uint64_t *workspaceSize,
aclOpExecutor **executor)`aclnnStatus aclnnFlashAttentionScoreGradV4(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnFlashAttentionScoreGradV4GetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor query Input Q in the formula. The data type must be consistent with keyIn/value. FLOAT16, BFLOAT16, FLOAT32 ND [BNSD], [BSND], [BSH], [SBH], [TND] √ queryRopeOptional Input The rope part of input Q in the formula. Data type is consistent with query. FLOAT16, BFLOAT16 ND [BNSD], [BSND], [BSH], [SBH], [TND] √ keyIn Input K in the formula. Data type is consistent with query/value. FLOAT16, BFLOAT16, FLOAT32 ND [BNSD], [BSND], [BSH], [SBH], [TND] √ keyRopeOptional Input Rope part of input K in the formula. The data type must be consistent with keyIn. FLOAT16, BFLOAT16 ND [BNSD], [BSND], [BSH], [SBH], [TND] √ value Input V in the formula. Data type is consistent with query/keyIn. FLOAT16, BFLOAT16, FLOAT32 ND [BNSD], [BSND], [BSH], [SBH], [TND] √ dy Input dY in the formula. - FLOAT16, BFLOAT16, FLOAT32 ND [BNSD], [BSND], [BSH], [SBH], [TND] √ pseShiftOptional Optional input pse in the formula, indicating positional encoding. The data type is consistent with query/keyIn/value. FLOAT16, BFLOAT16, FLOAT32 ND [B,N,Sq,Skv], [B,N,1,Skv], [1,N,Sq,Skv], [B,N,1024,Skv], [1,N,1024,Skv] √ dropMaskOptional Optional input Dropout in the formula. Supports [B,N,S,S] UINT8 ND 0, 1 √ paddingMaskOptional Optional input Reserved parameter, not yet used. - - - - - qStartIdxOptional Optional input Indicates the start index of the current Q block's sequence in the global sequence in the long-sequence use case. - INT64 ND 0, 1 √ kvStartIdxOptional Optional input Indicates the start index of the current chunked KV sequence in the global scope for the KV-split use case. - INT64 ND 0, 1 √ attenMaskOptional Optional input Indicates atten_mask in the formula. - The value 1 indicates that the bit is not involved in calculation, and 0 indicates that it is involved in calculation.
BOOL, UINT8 ND [B,N,Sq,Skv], [B,1,Sq,Skv], [1,1,Sq,Skv], [Sq,Skv] √ softmaxMaxOptional Optional input Intermediate output of the attention forward computation. - FLOAT ND [B,N,Sq,8],[N,T,8],[T,N,8] √ softmaxSumOptional Optional input Intermediate output of attention forward computation. - FLOAT ND [B,N,Sq,8],[N,T,8],[T,N,8] √ softmaxInOptional Optional input Intermediate output of the attention forward computation. Reserved parameter, not yet used. - FLOAT ND [B,N,Sq,8] √ attentionInOptional Optional input Final output of the attention forward computation. Data type and shape are consistent with dy. FLOAT16, BFLOAT16, FLOAT32 ND [BNSD], [BSND], [BSH], [SBH], [TND] √ sinkInOptional Optional input sink in the formula. The length is headNum. FLOAT32 ND [headNum] √ dScaleQOptional Optional input Dequantization parameter of the query input. Not used for now. FLOAT32 ND 0, 1 √ dScaleKOptional Optional input Dequantization parameter of the key input. Not used for now. FLOAT32 ND 0, 1 √ dScaleVOptional Optional input Is the value input reverse quantization parameter. Not used for now FLOAT32 ND 0, 1 √ dScaleDyOptional Optional input Dequantization parameter of the dy input. not used for now FLOAT32 ND 0, 1 √ dScaleOOptional Optional input Dequantization parameter of the attentionInOptional input. not used for now FLOAT32 ND 0, 1 √ prefixOptional Optional input N of each Batch in the prefix sparse use case. - INT64 ND 0, 1 - actualSeqQLenOptional Input Indicates the query sequence length of each Batch. - INT64 ND 0, 1 - actualSeqKvLenOptional Input Indicates the KV sequence length of each batch. - INT64 ND 0, 1 - scaleValue Input Scale coefficient in the formula. - DOUBLE - - - keepProb Input Ratio of 1s in dropMask. - DOUBLE - - - preTokens Input Left boundary of the sparse computation window. - INT64 - - - nextTokens Input Right boundary of the sparse computation window. - INT64 - - - headNum Input Number of heads on a single device, corresponding to the N dimension of query. - INT64 - - - inputLayout Input Data layout format of query/key/value. Supports BSH, SBH, BSND, BNSD, and TND. String - - - softmaxInLayout Input Controls the actual data layout of softmaxMax and softmaxSum. Pass "same_as_input" when shape is TND but the actual data layout needs NTD. Pass "" for TND. String - - - innerPrecise Input Reserved parameter, not yet used. - - - - - sparseMode Input Sparse mode. Supported values: 0-8. INT64 - - - pseType Input Integer on the host. Supports configuration values 0-3. INT64 - - - seed Input If keepProbOptional is less than 1.0, the DropoutMask is generated based on seedOptional and offsetOptional. - INT64 - - - offset Input An integer on the host side. - INT64 - - - outDtype Input A value of 0 indicates that outputs such as dqOut are FLOAT16, and a value of 1 indicates BFLOAT16. - INT64 - - - dqOut Output dQ in the formula. Gradient of query. Data type: consistent with query FLOAT16, BFLOAT16, FLOAT32 ND [BNSD], [BSND], [BSH], [SBH], [TND] √ dqRopeOut Output Indicates the gradient of queryRope. Data type: consistent with dqOut FLOAT16, BFLOAT16 ND [BNSD], [BSND], [BSH], [SBH], [TND] √ dkOut Output dK in the formula, the gradient of keyIn. The data type is consistent with keyIn. FLOAT16, BFLOAT16, FLOAT32 ND [BNSD], [BSND], [BSH], [SBH], [TND] √ dkRopeOut Output dkRope in the formula, indicating the gradient of keyInRope. Data type consistent with dkOut. FLOAT16, BFLOAT16 ND [BNSD], [BSND], [BSH], [SBH], [TND] √ dvOut Output dV in the formula, the gradient of value. Data type consistent with value. FLOAT16, BFLOAT16, FLOAT32 ND [BNSD], [BSND], [BSH], [SBH], [TND] √ dpseOut Output Gradient of d(pse). Not yet used. The passed-in shape must be consistent with pse_shape. FLOAT16, BFLOAT16, FLOAT32 ND 0, 4 √ dsinkOut Output Gradient of dSink and d(sinkInOptional) in the formula. - FLOAT32 ND 1 √ workspaceSize Output Returns the workspace size to be applied for on the Device. - - - - - executor Output Returns the operator executor containing the computation flow. - - - - - Return value
aclnnStatus: return 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 A required input, output, or required attribute parameter is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data types of query, keyIn, value, dy, pseShiftOptional, dropMaskOptional, paddingMaskOptional, attenMaskOptional, softmaxMaxOptional, softmaxSumOptional, softmaxInOptional, attentionInOptional, dqOut, dkOut, dvOut, and softmaxInLayout are not within the supported range.
aclnnFlashAttentionScoreGradV4
Parameters:
Parameter Input/Output Description workspace Input Memory address of the workspace requested on the Device side. workspaceSize Input Size of the workspace requested on the Device side, obtained by the first-phase API aclnnFlashAttentionScoreGradV4GetWorkspaceSize. executor Input Op executor, containing the operator calculation process. stream Input Specifies the stream for task execution. Return value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Deterministic computation:
- aclnnFlashAttentionScoreGradV4 defaults to a non-deterministic implementation. Deterministic computation can be enabled through aclrtCtxSetSysParamOpt.
Constraints on the inputs query, key, value, and dy are as follows:
- B: The batchsize must be equal.
- The inputLayout must be consistent.
- D: For Head-Dim, the D of query and key must be equal, the D of value and dy must be equal, and the D of query and key must be greater than or equal to the D of value and dy.
The N of query and dy and the N of key and value can be different, but they must be in a proportional relationship, that is, Nq/Nkv must be a non-zero integer, with Nq in the value range of 1 to 256.
Constraints on data shape, taking TND of inputLayout as an example, where:
- B: value range is 1 to 2K. When prefixOptional is present, B supports a maximum of 1K.
- N: ranges from 1 to 256.
- S: ranges from 1 to 1M.
- D: ranges from 1 to 768.
- KeepProb: value range is (0, 1].
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.
The prefixOptional sparse computation supports only the compression scenario, sparseModeOptional=6. 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, prefix N > Skv, or prefixOptional is not passed, full computation is performed. sparseModeOptional=6 requires that prefixOptional must be passed.
When sparseMode=7, the optional input pseShiftOptional is not supported.
When sparseMode=8, the optional input pseShiftOptional is supported when q and kv of each sequence have equal lengths, and pse is generated globally. Outer splitting in the q direction is supported, which requires that q and kv of each sequence have equal lengths before outer splitting, and after outer splitting, actualSeqQLenOptional[0] - actualSeqKvLenOptional[0] + qStartIdxOptional - kvStartIdxOptional == 0 (this feature is experimental).
The actualSeqQLenOptional input supports a case where the S length of a batch is 0, but the optional input pseShiftOptional is not supported in this case.
Constraints on the softmaxMax and softmaxSum parameters: The input format is fixed as [B, N, S, 8], except for the TND input format, which is [N, T, 8]. Note: T = B * S.
The value of headNum must be consistent with the N value in the input query.
Ascend 950PR&950DT products :- When queryRopeOptional and keyRopeOptional are not empty, the per-head D of query and keyIn must be 128, the per-head D of value and dy must be 128, and the per-head D of queryRopeOptional and keyRopeOptional must be 64.
- seedOptional and offsetOptional take effect only when keepProbOptional is less than 1.0; otherwise, they do not take effect.
- When keepProbOptional 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].
- When sinkInOptional is not None, input tensors such as query and key only support two data types: float16 and bfloat16. The dimension of sinkInOptional is 1, and its length must be the same as the headnum of query.
- When the data layout of softmaxSum and softmaxMax is TND, softmaxInLayout must be "same_as_input".
Example
The sample code is for reference only. For details about compilation and execution, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include <cstdint>
#include <cmath>
#include <random>
#include "acl/acl.h"
#include "aclnnop/aclnn_flash_attention_score_grad.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]);
}
}
int Init(int32_t deviceId, aclrtStream* stream) {
// Boilerplate: AscendCL 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 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 data from the host 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);
// Compute 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 aclCreateTensor 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. For details, see the AscendCL API list.
// Set deviceId based on the 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 input and output. Customize the construction based on the API.
int64_t B = 1;
int64_t N1 = 1;
int64_t N2 = 1;
int64_t S1 = 128;
int64_t S2 = 128;
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;
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> dxShape = {S1, B, H1};
std::vector<int64_t> attenmaskShape = {S1, S2};
std::vector<int64_t> softmaxMaxShape = {B, N1, S1, 8};
std::vector<int64_t> softmaxSumShape = {B, N1, S1, 8};
std::vector<int64_t> attentionInShape = {S1, B, H1};
std::vector<int64_t> sinkInOptionalShape = {N1};
std::vector<int64_t> dqShape = {S1, B, H1};
std::vector<int64_t> dkShape = {S2, B, H2};
std::vector<int64_t> dvShape = {S2, B, H2};
std::vector<int64_t> dsinkShape = {N1};
void* qDeviceAddr = nullptr;
void* kDeviceAddr = nullptr;
void* vDeviceAddr = nullptr;
void* dxDeviceAddr = nullptr;
void* attenmaskDeviceAddr = nullptr;
void* softmaxMaxDeviceAddr = nullptr;
void* softmaxSumDeviceAddr = nullptr;
void* attentionInDeviceAddr = nullptr;
void* sinkInOptionalDeviceAddr = nullptr;
void* dqDeviceAddr = nullptr;
void* dkDeviceAddr = nullptr;
void* dvDeviceAddr = nullptr;
void* dsinkDeviceAddr = nullptr;
aclTensor* q = nullptr;
aclTensor* k = nullptr;
aclTensor* v = nullptr;
aclTensor* dx = 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* dScaleDy = nullptr;
aclTensor* dScaleO = nullptr;
aclTensor* softmaxMax = nullptr;
aclTensor* softmaxSum = nullptr;
aclTensor* softmaxIn = nullptr;
aclTensor* attentionIn = nullptr;
aclTensor* sinkInOptional = nullptr;
aclTensor* dq = nullptr;
aclTensor* dk = nullptr;
aclTensor* dv = nullptr;
aclTensor* dpse = nullptr;
aclTensor* dqRope = nullptr;
aclTensor* dkRope = nullptr;
aclTensor* dsink = nullptr;
std::random_device rd;
std::mt19937 gen(rd());
std::normal_distribution<float> dist(0.0f, 1.0f); // Normal distribution with a mean of 0 and a standard deviation of 1.
std::vector<float> qHostData(q_size);
for (auto& val : qHostData) {
val = dist(gen);
}
std::vector<float> kHostData(kv_size);
for (auto& val : kHostData) {
val = dist(gen);
}
std::vector<float> vHostData(kv_size);
for (auto& val : vHostData) {
val = dist(gen);
}
std::vector<float> dxHostData(q_size);
for (auto& val : dxHostData) {
val = dist(gen);
}
std::vector<uint8_t> attenmaskHostData(atten_mask_size, 0);
std::vector<float> softmaxMaxHostData(softmax_size, 3.0);
std::vector<float> softmaxSumHostData(softmax_size, 3.0);
std::vector<float> attentionInHostData(q_size, 255.0);
std::vector<float> sinkInOptionalHostData(N1, 0);
std::vector<float> dqHostData(q_size, 2.0);
std::vector<float> dkHostData(kv_size, 2.0);
std::vector<float> dvHostData(kv_size, 3.0);
std::vector<float> dsinkHostData(N1, 0);
ret = CreateAclTensor(qHostData, qShape, &qDeviceAddr, aclDataType::ACL_FLOAT, &q);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(kHostData, kShape, &kDeviceAddr, aclDataType::ACL_FLOAT, &k);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(vHostData, vShape, &vDeviceAddr, aclDataType::ACL_FLOAT, &v);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(dxHostData, dxShape, &dxDeviceAddr, aclDataType::ACL_FLOAT, &dx);
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(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);
ret = CreateAclTensor(attentionInHostData, attentionInShape, &attentionInDeviceAddr, aclDataType::ACL_FLOAT, &attentionIn);
ret = CreateAclTensor(sinkInOptionalHostData, sinkInOptionalShape, &sinkInOptionalDeviceAddr, aclDataType::ACL_FLOAT, &sinkInOptional);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(dqHostData, dqShape, &dqDeviceAddr, aclDataType::ACL_FLOAT, &dq);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(dkHostData, dkShape, &dkDeviceAddr, aclDataType::ACL_FLOAT, &dk);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(dvHostData, dvShape, &dvDeviceAddr, aclDataType::ACL_FLOAT, &dv);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(dsinkHostData, dsinkShape, &dsinkDeviceAddr, aclDataType::ACL_FLOAT, &dsink);
CHECK_RET(ret == ACL_SUCCESS, return ret);
double scaleValue = 1.0/sqrt(128);
double keepProb = 1.0;
int64_t preTokens = 65536;
int64_t nextTokens = 65536;
int64_t headNum = 1;
int64_t innerPrecise = 0;
int64_t sparseMode = 0;
int64_t pseType = 1;
int64_t outDtype = 1;
int64_t seed = 0;
int64_t offset = 0;
char inputLayOut[5] = {'S', 'B', 'H', 0};
char softmaxInLayoutArr[] = "";
// 3. Call the CANN operator library API. Replace Api with the actual API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnFlashAttentionScoreGradV4
ret = aclnnFlashAttentionScoreGradV4GetWorkspaceSize(q, k, v, dx, pse, dropMask, padding,
attenmask, softmaxMax, softmaxSum, softmaxIn, attentionIn, nullptr, queryRope, keyRope, dScaleQ, dScaleK, dScaleV,
dScaleDy, dScaleO, nullptr, nullptr, nullptr, nullptr, nullptr, scaleValue, keepProb,
preTokens, nextTokens, headNum, inputLayOut, softmaxInLayoutArr, innerPrecise, sparseMode, pseType, seed, offset, outDtype,
dq,dk,dv,dqRope,dkRope,dpse, nullptr, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnFlashAttentionScoreGradV4GetWorkspaceSize 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 aclnnFlashAttentionScoreGradV4
ret = aclnnFlashAttentionScoreGradV4(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnFlashAttentionScoreGradV4 failed. ERROR: %d\n", ret); return ret);
// 4. (boilerplate) 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 values and copy the results from device memory to host memory. Modify based on the specific API definition
PrintOutResult(dqShape, &dqDeviceAddr);
PrintOutResult(dkShape, &dkDeviceAddr);
PrintOutResult(dvShape, &dvDeviceAddr);
// 6. Release aclTensor and aclScalar. Modify based on the specific API definition
aclDestroyTensor(q);
aclDestroyTensor(k);
aclDestroyTensor(v);
aclDestroyTensor(dx);
aclDestroyTensor(attenmask);
aclDestroyTensor(softmaxMax);
aclDestroyTensor(softmaxSum);
aclDestroyTensor(attentionIn);
aclDestroyTensor(sinkInOptional);
aclDestroyTensor(dq);
aclDestroyTensor(dk);
aclDestroyTensor(dv);
aclDestroyTensor(dsink);
// 7. Release device resources
aclrtFree(qDeviceAddr);
aclrtFree(kDeviceAddr);
aclrtFree(vDeviceAddr);
aclrtFree(dxDeviceAddr);
aclrtFree(attenmaskDeviceAddr);
aclrtFree(softmaxMaxDeviceAddr);
aclrtFree(softmaxSumDeviceAddr);
aclrtFree(attentionInDeviceAddr);
aclrtFree(sinkInOptionalDeviceAddr);
aclrtFree(dqDeviceAddr);
aclrtFree(dkDeviceAddr);
aclrtFree(dvDeviceAddr);
aclrtFree(dsinkDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}