aclnnSparseLightningIndexerKLLossGrad
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: SparseLightningIndexerKLLossGrad is the backward computation operator of LightningIndexer KL Loss. This API takes q, k, w, and sparseIndices from the Lightning Indexer branch, and attnSoftmaxL1Norm precomputed by the main attention branch. It computes and outputs dq, dk, dw, and softmaxOut. Unlike SparseLightningIndexerGradKLLoss, this API does not recompute
Q @ K -> softmax -> reduce/l1 normof the main attention in the kernel or output the loss. The target distribution of the main attention is provided by the attnSoftmaxL1Norm input, and softmaxOut can be used for subsequent loss computation.Formula: The Indexer logits used to obtain the Top-k values can be expressed as:
$$ S_{t,:}=q_{t,:}@K_{\operatorname{topk}(t),:}^{T} $$$$ I_{t,:}=W_{t,:}@\mathrm{ReLU}(S_{t,:}) $$Here, $q$ and $K$ correspond to q and k of this API respectively, $W$ corresponds to w, and $\operatorname{topk}(t)$ is given by sparseIndices. The softmax output of the Indexer branch is:
$$ y_{t,:}=\operatorname{Softmax}(I_{t,:}) $$This API writes $y$ to softmaxOut. The target distribution $p$ is provided by the attnSoftmaxL1Norm input, which is obtained by averaging the main attention score over heads, without L1 normalization along the context. Let $Z_{t}$ denote the sum of $p$ over the context, and $t$ the target distribution after L1 normalization:
$$ Z_{t}=\sum_{j}p_{t,j},\quad t_{t,:}=\frac{p_{t,:}}{Z_{t}} $$If the KL Loss is further computed, the corresponding loss function is:
$$ L_{t}=Z_{t}\,D_{KL}(t_{t,:}||y_{t,:})=\sum_{j}p_{t,j}(\log t_{t,j}-\log y_{t,j}) $$$$ D_{KL}(a||b){=}\sum_ia_i\mathrm{log}{\left(\frac{a_i}{b_i}\right)} $$The gradient expression of the loss can be obtained by deduction:
$$ dI_{t,:}=y_{t,:}\cdot Z_{t}-p_{t,:} $$The gradients of the w, q, and k matrices can be computed using the chain rule:
$$ dW_{t,:}=dI_{t,:}\text{@}\left(\mathrm{ReLU}(S_{t,:})\right)^{T} $$$$ dq_{t,:}=dS_{t,:}@K_{\operatorname{topk}(t),:} $$$$ dK_{\operatorname{topk}(t),:}=\left(dS_{t,:}\right)^{T}@q_{t,:} $$dK is written back via scatter-add at the key positions specified by sparseIndices. Invalid top-k positions are excluded from the computation.
Function Prototype
Each operator uses a two-phase API. Call the "aclnnSparseLightningIndexerKLLossGradGetWorkspaceSize" API first to obtain the input parameters and calculate the required workspace size based on the computation process, and then call the "aclnnSparseLightningIndexerKLLossGrad" API to perform the computation.
aclnnStatus aclnnSparseLightningIndexerKLLossGradGetWorkspaceSize(
const aclTensor *q,
const aclTensor *k,
const aclTensor *w,
const aclTensor *sparseIndices,
const aclTensor *attnSoftmaxL1Norm,
const aclTensor *cuSeqLensQOptional,
const aclTensor *cuSeqLensKOptional,
const aclTensor *seqUsedQOptional,
const aclTensor *seqUsedKOptional,
const aclTensor *cmpResidualKOptional,
const aclTensor *metadataOptional,
char *layoutQ,
char *layoutK,
int64_t maskMode,
int64_t cmpRatio,
const aclTensor *dq,
const aclTensor *dk,
const aclTensor *dw,
const aclTensor *softmaxOut,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnSparseLightningIndexerKLLossGrad(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnSparseLightningIndexerKLLossGradGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor q Input Query input of the Lightning Indexer branch. - Data type is consistent with k, dq, and dk.
- Empty tensor not supported.
FLOAT16, BFLOAT16 ND (B,S1,N1,D), (T1,N1,D) x k Input Key input of the Lightning Indexer branch. - The data type must be consistent with q, dq, and dk.
- Currently, only N2=1 is supported.
- Empty Tensor Not Supported.
FLOAT16, BFLOAT16 ND (B,S2,N2,D), (T2,N2,D) x w Input Head weight of Indexer logits. Empty Tensor Not Supported. FLOAT32 ND (B,S1,N1), (T1,N1) x sparseIndices Input Top-k key indices corresponding to each query. - The valid part is filled with key indices.
- The invalid part is filled with -1.
- Empty Tensor Not Supported.
INT32 ND (B,S1,N2,K), (T1,N2,K) x attnSoftmaxL1Norm Input Target distribution precomputed by the main Attention branch. - Replaces the recomputed main Attention result inside the legacy kernel.
- Dimensions must be consistent with those of sparseIndices.
- Empty tensors not supported.
FLOAT32 ND (B,S1,N2,K), (T1,N2,K) x cuSeqLensQOptional Optional Input Cumulative sequence length of q in TND layout. Required when layoutQ is TND. The length is B+1 and the first element is 0. INT32 ND (B+1,) x cuSeqLensKOptional Optional Input Cumulative sequence length of k in TND layout. Required when layoutK is TND. Length is B+1, and the first element is 0. INT32 ND (B+1,) x seqUsedQOptional Optional Input Indicates the actual q length used in each batch. Length is B. Each value must be greater than or equal to 0 and must not exceed the q allocation length of the corresponding batch. Multiple batches can be 0, but all batches being 0 is not supported. INT32 ND (B,) x seqUsedKOptional Optional Input Indicates the actual k length used in each batch. The length is B. Each value must be greater than or equal to 0 and cannot exceed the k allocation length of the corresponding batch. INT32 ND (B,) x cmpResidualKOptional Optional Input Required when maskMode=3 and cmpRatio!=1. The length is B. INT32 ND (B,) x metadataOptional Input Core splitting information generated by aclnnSparseLightningIndexerKLLossGradMetadata. Optional Input. If provided, the kernel uses the core splitting in metadata. INT32 ND (x,) x layoutQ Input Layout of q, w, sparseIndices, attnSoftmaxL1Norm, dq, dw, and softmaxOut. Only BSND and TND are supported. STRING - - - layoutK Input Layout of k and dk. Only BSND and TND are supported. STRING - - - maskMode Input Mask mode. For supported mask modes, see Constraints. INT64 - - - cmpRatio Input Compression ratio. Value range: 1-128. INT64 - - - dq Output Gradient of q. Data type and shape are consistent with q. FLOAT16, BFLOAT16 ND (B,S1,N1,D), (T1,N1,D) x dk Output Gradient of k. Data type and shape are consistent with those of k. FLOAT16, BFLOAT16 ND (B,S2,N2,D), (T2,N2,D) x dw Output Gradient of w. Data type and shape are consistent with those of w. FLOAT32 ND (B,S1,N1), (T1,N1) x softmaxOut Output Indexer branch softmax output. Shape consistent with attnSoftmaxL1Norm. FLOAT32 ND (B,S1,N2,K), (T1,N2,K) x workspaceSize Output Returns the size of the workspace that the user needs to allocate on the Device. - - - - - executor Output op executor, which contains the operator 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 parameter or output is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type, data format, or value of inputs, outputs, or attributes is not within the supported range. ACLNN_ERR_RUNTIME_ERROR 361001 An exception occurs when the API calls the NPU runtime API for memory.
aclnnSparseLightningIndexerKLLossGrad
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 aclnnSparseLightningIndexerKLLossGradGetWorkspaceSize. executor Input Op executor, which contains the operator computation flow. stream Input Stream for task execution. Return value
Returns an aclnnStatus status code. For details, see aclnn Return Code.
Constraints
Deterministic computation:
- aclnnSparseLightningIndexerKLLossGrad defaults to a non-deterministic implementation. Deterministic computation can be enabled through aclrtCtxSetSysParamOpt.
Common constraints:
This API does not support non-contiguous output.
The data types of q, k, dq, and dk must be consistent. FLOAT16 and BFLOAT16 are supported.
The data types of w, dw, attnSoftmaxL1Norm, and softmaxOut must be FLOAT32.
The data types of sparseIndices, cuSeqLensQOptional, cuSeqLensKOptional, seqUsedQOptional, seqUsedKOptional, cmpResidualKOptional, and metadataOptional must be INT32.
Currently, layoutQ and layoutK support BSND and TND.
If layoutQ is TND, cuSeqLensQOptional is required. If layoutK is TND, cuSeqLensKOptional is required.
If seqUsedQOptional and seqUsedKOptional exist, each value must be within the allocated length range of the corresponding batch. For BSND, the allocated lengths are S1 and S2 respectively. For TND, the allocated length is the difference between adjacent elements in the corresponding cuSeqLens.
Valid positions in sparseIndices must be within the valid key sequence range of the current batch. If seqUsedKOptional is passed in, use the corresponding seqUsedKOptional value as the upper bound. Pad invalid positions with -1.
Invalid top-k positions in attnSoftmaxL1Norm should be set to zero and kept consistent with the valid positions in sparseIndices.
maskMode Description Remarks 0 defaultMask mode; no causal mask is applied. Supported 1 allMask; the complete attenmask matrix must be passed in. Not Supported 2 Mask for leftUpCausal mode. Not Supported 3 Mask in rightDownCausal mode, corresponding to the lower-triangular scenario with the right vertex as the dividing point. Supported 4 Mask in band mode. Not Supported 5 prefix mode. Not Supported 6 global mode. Not Supported 7 dilated mode. Not Supported 8 block_local mode. Not Supported
Specification constraints:
Item Specification Specification N2 1 Only N2=1 is supported currently. D 128 The last dimensions of q and k must be consistent. cmpRatio 1-128 - Support for parameter B:
Atlas A2 products ,Atlas A3 products : B: range 1-256.
Ascend 950PR&950DT products : B>0.
Support for parameters S1 and S2:
Atlas A2 products ,Atlas A3 products : S1: range 1-8k. S2: range 1-512k.
Ascend 950PR&950DT products : S1>0, S2>0.
Support for parameter N1:
Atlas A2 products ,Atlas A3 products : N1: 8, 16, 32, or 64.
Ascend 950PR&950DT products : N1: range 1-128.
Support for parameter K:
Atlas A2 products ,Atlas A3 products : K: range 1-8192.
Ascend 950PR&950DT products : K: range 1-2048.
Example
The following sample code is for reference only. For details about the compilation and execution process, see Compile and Run Samples.
#include <algorithm>
#include <cstdint>
#include <cstdio>
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_sparse_lightning_indexer_kl_loss_grad.h"
#include "aclnnop/aclnn_sparse_lightning_indexer_kl_loss_grad_metadata.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;
}
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);
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);
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);
std::vector<int64_t> strides(shape.size(), 1);
for (int64_t i = static_cast<int64_t>(shape.size()) - 2; i >= 0; i--) {
strides[i] = shape[i + 1] * strides[i + 1];
}
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
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 < std::min<int64_t>(size, 5); i++) {
LOG_PRINT("softmaxOut[%ld] is: %f\n", i, resultData[i]);
}
}
int Init(int32_t deviceId, aclrtContext* context, aclrtStream* stream) {
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;
}
int main() {
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);
const int64_t batchSize = 1;
const int64_t t1 = 16;
const int64_t t2 = 4;
const int64_t numHeadsQ = 8;
const int64_t numHeadsK = 1;
const int64_t headDim = 128;
const int64_t topk = 512;
const int64_t maskMode = 3;
const int64_t cmpRatio = 1;
std::vector<int64_t> qShape = {t1, numHeadsQ, headDim};
std::vector<int64_t> kShape = {t2, numHeadsK, headDim};
std::vector<int64_t> wShape = {t1, numHeadsQ};
std::vector<int64_t> sparseShape = {t1, numHeadsK, topk};
std::vector<int64_t> softmaxShape = {t1, numHeadsK, topk};
std::vector<int64_t> cuSeqLensShape = {batchSize + 1};
std::vector<int64_t> metadataShape = {64};
std::vector<uint16_t> qHostData(GetShapeSize(qShape), 0x3C00);
std::vector<uint16_t> kHostData(GetShapeSize(kShape), 0x3C00);
std::vector<float> wHostData(GetShapeSize(wShape), 1.0f);
std::vector<int32_t> sparseHostData(GetShapeSize(sparseShape), -1);
std::vector<float> attnSoftmaxL1NormHostData(GetShapeSize(softmaxShape), 0.0f);
for (int64_t t = 0; t < t1; ++t) {
auto validK = std::min<int64_t>(std::max<int64_t>(1, (t + 1) / cmpRatio), t2);
for (int64_t kIdx = 0; kIdx < validK; ++kIdx) {
sparseHostData[t * numHeadsK * topk + kIdx] = static_cast<int32_t>(kIdx);
attnSoftmaxL1NormHostData[t * numHeadsK * topk + kIdx] = 1.0f / static_cast<float>(validK);
}
}
std::vector<int32_t> cuSeqLensQHostData = {0, static_cast<int32_t>(t1)};
std::vector<int32_t> cuSeqLensKHostData = {0, static_cast<int32_t>(t2)};
std::vector<int32_t> metadataHostData(GetShapeSize(metadataShape), 0);
std::vector<uint16_t> dqHostData(GetShapeSize(qShape), 0);
std::vector<uint16_t> dkHostData(GetShapeSize(kShape), 0);
std::vector<float> dwHostData(GetShapeSize(wShape), 0.0f);
std::vector<float> softmaxOutHostData(GetShapeSize(softmaxShape), 0.0f);
void* qDeviceAddr = nullptr;
void* kDeviceAddr = nullptr;
void* wDeviceAddr = nullptr;
void* sparseDeviceAddr = nullptr;
void* attnSoftmaxL1NormDeviceAddr = nullptr;
void* cuSeqLensQDeviceAddr = nullptr;
void* cuSeqLensKDeviceAddr = nullptr;
void* metadataDeviceAddr = nullptr;
void* dqDeviceAddr = nullptr;
void* dkDeviceAddr = nullptr;
void* dwDeviceAddr = nullptr;
void* softmaxOutDeviceAddr = nullptr;
aclTensor* q = nullptr;
aclTensor* k = nullptr;
aclTensor* w = nullptr;
aclTensor* sparseIndices = nullptr;
aclTensor* attnSoftmaxL1Norm = nullptr;
aclTensor* cuSeqLensQ = nullptr;
aclTensor* cuSeqLensK = nullptr;
aclTensor* metadata = nullptr;
aclTensor* dq = nullptr;
aclTensor* dk = nullptr;
aclTensor* dw = nullptr;
aclTensor* softmaxOut = nullptr;
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(wHostData, wShape, &wDeviceAddr, aclDataType::ACL_FLOAT, &w);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(sparseHostData, sparseShape, &sparseDeviceAddr, aclDataType::ACL_INT32, &sparseIndices);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(attnSoftmaxL1NormHostData, softmaxShape, &attnSoftmaxL1NormDeviceAddr,
aclDataType::ACL_FLOAT, &attnSoftmaxL1Norm);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(cuSeqLensQHostData, cuSeqLensShape, &cuSeqLensQDeviceAddr, aclDataType::ACL_INT32, &cuSeqLensQ);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(cuSeqLensKHostData, cuSeqLensShape, &cuSeqLensKDeviceAddr, aclDataType::ACL_INT32, &cuSeqLensK);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(metadataHostData, metadataShape, &metadataDeviceAddr, aclDataType::ACL_INT32, &metadata);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(dqHostData, qShape, &dqDeviceAddr, aclDataType::ACL_FLOAT16, &dq);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(dkHostData, kShape, &dkDeviceAddr, aclDataType::ACL_FLOAT16, &dk);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(dwHostData, wShape, &dwDeviceAddr, aclDataType::ACL_FLOAT, &dw);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(softmaxOutHostData, softmaxShape, &softmaxOutDeviceAddr, aclDataType::ACL_FLOAT, &softmaxOut);
CHECK_RET(ret == ACL_SUCCESS, return ret);
char layoutQ[4] = {'T', 'N', 'D', 0};
char layoutK[4] = {'T', 'N', 'D', 0};
uint64_t metadataWorkspaceSize = 0;
aclOpExecutor* metadataExecutor = nullptr;
ret = aclnnSparseLightningIndexerKLLossGradMetadataGetWorkspaceSize(
cuSeqLensQ, cuSeqLensK, nullptr, nullptr, nullptr, batchSize, t1, t2, numHeadsQ, numHeadsK, headDim, topk,
layoutQ, layoutK, maskMode, cmpRatio, metadata, &metadataWorkspaceSize, &metadataExecutor);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("aclnnSparseLightningIndexerKLLossGradMetadataGetWorkspaceSize failed. ERROR: %d\n", ret);
return ret);
void* metadataWorkspaceAddr = nullptr;
if (metadataWorkspaceSize > 0) {
ret = aclrtMalloc(&metadataWorkspaceAddr, metadataWorkspaceSize, ACL_MEM_MALLOC_HUGE_FIRST);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("allocate metadata workspace failed. ERROR: %d\n", ret); return ret);
}
ret = aclnnSparseLightningIndexerKLLossGradMetadata(metadataWorkspaceAddr, metadataWorkspaceSize,
metadataExecutor, stream);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("aclnnSparseLightningIndexerKLLossGradMetadata failed. ERROR: %d\n", ret); return ret);
uint64_t workspaceSize = 0;
aclOpExecutor* executor = nullptr;
ret = aclnnSparseLightningIndexerKLLossGradGetWorkspaceSize(
q, k, w, sparseIndices, attnSoftmaxL1Norm, cuSeqLensQ, cuSeqLensK, nullptr, nullptr, nullptr, metadata,
layoutQ, layoutK, maskMode, cmpRatio, dq, dk, dw, softmaxOut, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("aclnnSparseLightningIndexerKLLossGradGetWorkspaceSize failed. ERROR: %d\n", ret);
return ret);
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);
}
ret = aclnnSparseLightningIndexerKLLossGrad(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS,
LOG_PRINT("aclnnSparseLightningIndexerKLLossGrad failed. ERROR: %d\n", ret); return ret);
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
PrintOutResult(softmaxShape, &softmaxOutDeviceAddr);
aclDestroyTensor(q);
aclDestroyTensor(k);
aclDestroyTensor(w);
aclDestroyTensor(sparseIndices);
aclDestroyTensor(attnSoftmaxL1Norm);
aclDestroyTensor(cuSeqLensQ);
aclDestroyTensor(cuSeqLensK);
aclDestroyTensor(metadata);
aclDestroyTensor(dq);
aclDestroyTensor(dk);
aclDestroyTensor(dw);
aclDestroyTensor(softmaxOut);
aclrtFree(qDeviceAddr);
aclrtFree(kDeviceAddr);
aclrtFree(wDeviceAddr);
aclrtFree(sparseDeviceAddr);
aclrtFree(attnSoftmaxL1NormDeviceAddr);
aclrtFree(cuSeqLensQDeviceAddr);
aclrtFree(cuSeqLensKDeviceAddr);
aclrtFree(metadataDeviceAddr);
aclrtFree(dqDeviceAddr);
aclrtFree(dkDeviceAddr);
aclrtFree(dwDeviceAddr);
aclrtFree(softmaxOutDeviceAddr);
if (metadataWorkspaceSize > 0) {
aclrtFree(metadataWorkspaceAddr);
}
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtDestroyContext(context);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}