aclnnLightningIndexerGrad
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:
lightning_indexer_gradis the backward operator oflightning_indexer. It computes the gradients ofquery,key, andweightsbased on the forward operator outputsparseIndices.Formula: The backward propagation formula of LightningIndexer is as follows:
$$ S = Relu(Matmul(Query, Gather(Key, Indices))) $$$$ Y = Dy*Weights $$$$ dW = Reduce(S * dy) $$$$ dQ = Matmul(ReluGrad(Y, S), Gather(Key, Indices)) $$$$ dK = ScatterAdd(Matmul(ReluGrad(Y, S), Q), Indices) $$
Function Prototype
Each operator uses a two-phase API. First, call the "aclnnLightningIndexerGradGetWorkspaceSize" API to obtain the workspace size required for the computation and the executor containing the operator computation process. Then, call the "aclnnLightningIndexerGrad" API to perform the computation.
aclnnStatus aclnnLightningIndexerGradGetWorkspaceSize(
const aclTensor *query,
const aclTensor *key,
const aclTensor *dy,
const aclTensor *spareIndices,
const aclTensor *weights,
const aclTensor *actualSeqLengthsQuery,
const aclTensor *actualSeqLengthsKey,
int64_t headNum,
char *layout,
int64_t sparseMode,
int64_t preTokens,
int64_t nextTokens,
bool deterministic,
const aclTensor *dQuery,
const aclTensor *dKey,
const aclTensor *dWeights,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnLightningIndexerGrad(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnLightningIndexerGradGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor query Input The query in the formula. Supported shapes: [B, S1, N1, D]/[T1, N1, D]. BFLOAT16, FLOAT16 ND 3-4 √ key Input The key in the formula. Supported shapes: [B, S2, N2, D]/[T2, N2, D]. BFLOAT16, FLOAT16 ND 3-4 √ dy Input The value in the formula. Supported shapes: [B, S1, N1, D]/[T1, N1, D]. BFLOAT16, FLOAT16 ND 3-4 √ sparseIndices Input sparseIndices in the formula. The shape supports [B, S1, K]/[T1, K]. INT32 ND 2-3 √ weights Input weights in the formula. Supported shapes are [B, S1, N1]/[T1, N1]. BFLOAT16, FLOAT16 ND 2-3 √ actualSeqLengthsQuery Input Indicates the cumulative sum length of S for each Batch of query. Required as input for the TND layout; set to nullptr in other use cases. INT32 ND 1 √ actualSeqLengthsKey Input Indicates the cumulative sum length of S for each Batch of key. Required for TND layout. For other cases, input nullptr. INT32 ND 1 √ headNum Input Represents the number of heads. - INT64 - - - layout Input Represents the data layout of query/key. TND/BSND is currently supported. String - - - sparseMode Input Indicates the sparse mode. Supported values: 0/3. INT64 - - - preTokens Input Used for sparse computation. Indicates the left boundary of the sliding window. - INT64 - - - nextTokens Input Used for sparse computation. Indicates the right boundary of the sliding window. - INT64 - - - deterministic Input Specifies whether deterministic computation is supported. - BOOL - - - dQuery Output dQuery gradient. Data type is the same as query. BFLOAT16, FLOAT16 ND 3-4 √ dKey Output dKey gradient. The data type is the same as that of key. BFLOAT16, FLOAT16 ND 3-4 √ dWeights Output dWeights gradient. The data type is the same as weights. BFLOAT16, FLOAT16 ND 2-3 √ workspaceSize Output Returns the workspace size that needs to be applied by the Device side. - - - - - executor Output Returns the op executor, which contains the operator computation process. - - - - - 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 The input parameter is a required input, output, or attribute, and is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data types and data formats of query, key, dy, sparseIndices, and weights are out of the supported range. The input type of input_layout is out of the supported range.
aclnnLightningIndexerGrad
Parameters
Parameter Input/Output Description workspace Input Workspace memory address applied by the Device side. workspaceSize Input Size of the workspace applied by the Device side, obtained by calling the first-phase API aclnnLightningIndexerGradGetWorkspaceSize. executor Input Op executor that contains the operator computation flow. stream Input Stream for task execution. Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
Deterministic computation:
- aclnnLightningIndexerGrad defaults to a non-deterministic implementation and does not support enabling determinism through aclrtCtxSetSysParamOpt.
When using this API with PyTorch, ensure that the versions of CANN-related packages and PyTorch-related packages are compatible.
inputLayout supports TND/BSND.
Constraints on data shape, using the BSND layout as an example:
- B (Batchsize): The value range is 1 to 1024.
- N (Head-Num): The value is 1~64.
- G (Group): The value is N.
- S1 (Seq-LengthQ): Value range is 1 to 128K.
- S2 (Seq-LengthK): Value Range is topK to 128K.
- D (Head-Dim): Value is 128.
- TopK: value 2048.
Example
The following sample code calls the operator using aclnn, for reference only. For details about compilation and execution, see Compile and Run Samples.
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <fstream>
#include <sys/stat.h>
#include "acl/acl.h"
#include "aclnnop/aclnn_lightning_indexer_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;
}
template <typename T>
void PrintOutResult(std::vector<int64_t> &shape, void **deviceAddr)
{
auto size = GetShapeSize(shape);
std::vector<T> resultData(size, 0);
auto ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(T), *deviceAddr, size * sizeof(T),
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 < 10; i++) {
LOG_PRINT("mean result[%ld] is: %f\n", i, static_cast<float>(resultData[i]));
}
}
int Init(int32_t deviceId, aclrtContext *context, 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); aclFinalize(); return ret);
ret = aclrtCreateContext(context, deviceId);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateContext failed. ERROR: %d\n", ret); aclrtResetDevice(deviceId);
aclFinalize(); return ret);
ret = aclrtSetCurrentContext(*context);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSetCurrentContext failed. ERROR: %d\n", ret);
aclrtDestroyContext(context); aclrtResetDevice(deviceId); aclFinalize(); return ret);
ret = aclrtCreateStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtCreateStream failed. ERROR: %d\n", ret); aclrtDestroyContext(context);
aclrtResetDevice(deviceId); aclFinalize(); return ret);
return 0;
}
template <typename T>
int CreateAclTensor(const std::vector<T> &hostData, const std::vector<int64_t> &shape, void **deviceAddr,
aclDataType dataType, aclTensor **tensor)
{
auto size = GetShapeSize(shape) * sizeof(T);
// Call aclrtMalloc to allocate memory on the device side.
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 side to the device memory.
ret = aclrtMemcpy(*deviceAddr, size, hostData.data(), size, ACL_MEMCPY_HOST_TO_DEVICE);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtMemcpy failed. ERROR: %d\n", ret); return ret);
// Calculate the strides of the contiguous tensor.
std::vector<int64_t> strides(shape.size(), 1);
for (int64_t i = static_cast<int64_t>(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;
}
void FreeResource(aclTensor *q, aclTensor *k, aclTensor *dy, aclTensor *sparseIndices, aclTensor *weights,
aclTensor *dQuery, aclTensor *dKey, aclTensor *dWeights, void *qDeviceAddr, void *kDeviceAddr,
void *dyDeviceAddr, void *sparseIndicesDeviceAddr, void *weightsDeviceAddr, void *dQueryAddr,
void *dKeyAddr, void *dWeightsAddr, uint64_t workspaceSize, void *workspaceAddr, int32_t deviceId,
aclrtContext *context, aclrtStream *stream)
{
// Release aclTensor and aclScalar. Modify this based on the definition of the specific API.
if (q != nullptr) {
aclDestroyTensor(q);
}
if (k != nullptr) {
aclDestroyTensor(k);
}
if (dy != nullptr) {
aclDestroyTensor(dy);
}
if (sparseIndices != nullptr) {
aclDestroyTensor(sparseIndices);
}
if (weights != nullptr) {
aclDestroyTensor(weights);
}
if (dQuery != nullptr) {
aclDestroyTensor(dQuery);
}
if (dKey != nullptr) {
aclDestroyTensor(dKey);
}
if (dWeights != nullptr) {
aclDestroyTensor(dWeights);
}
// Release device resources.
if (qDeviceAddr != nullptr) {
aclrtFree(qDeviceAddr);
}
if (kDeviceAddr != nullptr) {
aclrtFree(kDeviceAddr);
}
if (dyDeviceAddr != nullptr) {
aclrtFree(dyDeviceAddr);
}
if (sparseIndicesDeviceAddr != nullptr) {
aclrtFree(sparseIndicesDeviceAddr);
}
if (weightsDeviceAddr != nullptr) {
aclrtFree(weightsDeviceAddr);
}
if (dQueryAddr != nullptr) {
aclrtFree(dQueryAddr);
}
if (dKeyAddr != nullptr) {
aclrtFree(dKeyAddr);
}
if (dWeightsAddr != nullptr) {
aclrtFree(dWeightsAddr);
}
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
if (stream != nullptr) {
aclrtDestroyStream(stream);
}
if (context != nullptr) {
aclrtDestroyContext(context);
}
aclrtResetDevice(deviceId);
aclFinalize();
}
int main()
{
// 1. (Boilerplate) Initialize device/context/stream. For details, see the list of AscendCL external APIs.
// Set deviceId based on 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 inputs and outputs. Customize the construction based on the API definition.
// Shape values corresponding to query, key, dy, sparseIndices, and weights. Generate data again and then run.
int64_t batch = 2;
int64_t s1 = 3;
int64_t s2 = 2048;
int64_t d = 128;
int64_t g = 64;
int64_t n2 = 1;
int64_t topK = 2048;
std::vector<int64_t> qShape = {batch, s1, n2 * g, d};
std::vector<int64_t> kShape = {batch, s2, n2, d};
std::vector<int64_t> dyShape = {batch, s1, n2 * g, d};
std::vector<int64_t> sparseIndicesShape = {batch, s1, topK};
std::vector<int64_t> weightsShape = {batch, s1, n2 * g};
std::vector<int64_t> dQueryShape = {batch, s1, n2 * g, d};
std::vector<int64_t> dKeyShape = {batch, s2, n2, d};
std::vector<int64_t> dWeightsShape = {batch, s1, n2 * g};
int64_t headNum = 64;
int64_t sparseMode = 3;
char layoutStr[] = "BSND";
bool deteminstic = true;
int64_t preToken = 65536;
int64_t nextToken = 65536;
void *qDeviceAddr = nullptr;
void *kDeviceAddr = nullptr;
void *dyDeviceAddr = nullptr;
void *sparseIndicesDeviceAddr = nullptr;
void *weightsDeviceAddr = nullptr;
void *dQueryAddr = nullptr;
void *dKeyAddr = nullptr;
void *dWeightsAddr = nullptr;
aclTensor *q = nullptr;
aclTensor *k = nullptr;
aclTensor *dy = nullptr;
aclTensor *sparseIndices = nullptr;
aclTensor *weights = nullptr;
aclTensor *dQuery = nullptr;
aclTensor *dKey = nullptr;
aclTensor *dWeights = nullptr;
std::vector<aclFloat16> qHostData(GetShapeSize(qShape), 1.0);
std::vector<aclFloat16> kHostData(GetShapeSize(kShape), 1.0);
std::vector<aclFloat16> dyHostData(GetShapeSize(dyShape), 1.0);
std::vector<int32_t> sparseIndicesHostData(GetShapeSize(sparseIndicesShape), 1);
std::vector<aclFloat16> weightsHostData(GetShapeSize(weightsShape), 1.0);
std::vector<aclFloat16> dQueryHostData(GetShapeSize(dQueryShape), 1.0);
std::vector<aclFloat16> dKeyHostData(GetShapeSize(dKeyShape), 1.0);
std::vector<aclFloat16> dWeightsHostData(GetShapeSize(dWeightsShape), 1.0);
uint64_t workspaceSize = 0;
void *workspaceAddr = nullptr;
// Create the acl Tensor.
ret = CreateAclTensor(qHostData, qShape, &qDeviceAddr, aclDataType::ACL_FLOAT16, &q);
CHECK_RET(ret == ACL_SUCCESS,
FreeResource(q, k, dy, sparseIndices, weights, dQuery, dKey, dWeights, qDeviceAddr, kDeviceAddr,
dyDeviceAddr, sparseIndicesDeviceAddr, weightsDeviceAddr, dQueryAddr, dKeyAddr, dWeightsAddr,
workspaceSize, workspaceAddr, deviceId, &context, &stream);
return ret);
ret = CreateAclTensor(kHostData, kShape, &kDeviceAddr, aclDataType::ACL_FLOAT16, &k);
CHECK_RET(ret == ACL_SUCCESS,
FreeResource(q, k, dy, sparseIndices, weights, dQuery, dKey, dWeights, qDeviceAddr, kDeviceAddr,
dyDeviceAddr, sparseIndicesDeviceAddr, weightsDeviceAddr, dQueryAddr, dKeyAddr, dWeightsAddr,
workspaceSize, workspaceAddr, deviceId, &context, &stream);
return ret);
ret = CreateAclTensor(dyHostData, dyShape, &dyDeviceAddr, aclDataType::ACL_FLOAT16, &dy);
CHECK_RET(ret == ACL_SUCCESS,
FreeResource(q, k, dy, sparseIndices, weights, dQuery, dKey, dWeights, qDeviceAddr, kDeviceAddr,
dyDeviceAddr, sparseIndicesDeviceAddr, weightsDeviceAddr, dQueryAddr, dKeyAddr, dWeightsAddr,
workspaceSize, workspaceAddr, deviceId, &context, &stream);
return ret);
ret = CreateAclTensor(sparseIndicesHostData, sparseIndicesShape, &sparseIndicesDeviceAddr, aclDataType::ACL_INT32,
&sparseIndices);
CHECK_RET(ret == ACL_SUCCESS,
FreeResource(q, k, dy, sparseIndices, weights, dQuery, dKey, dWeights, qDeviceAddr, kDeviceAddr,
dyDeviceAddr, sparseIndicesDeviceAddr, weightsDeviceAddr, dQueryAddr, dKeyAddr, dWeightsAddr,
workspaceSize, workspaceAddr, deviceId, &context, &stream);
return ret);
ret = CreateAclTensor(weightsHostData, weightsShape, &weightsDeviceAddr, aclDataType::ACL_FLOAT16, &weights);
CHECK_RET(ret == ACL_SUCCESS,
FreeResource(q, k, dy, sparseIndices, weights, dQuery, dKey, dWeights, qDeviceAddr, kDeviceAddr,
dyDeviceAddr, sparseIndicesDeviceAddr, weightsDeviceAddr, dQueryAddr, dKeyAddr, dWeightsAddr,
workspaceSize, workspaceAddr, deviceId, &context, &stream);
return ret);
ret = CreateAclTensor(dQueryHostData, dQueryShape, &dQueryAddr, aclDataType::ACL_FLOAT16, &dQuery);
CHECK_RET(ret == ACL_SUCCESS,
FreeResource(q, k, dy, sparseIndices, weights, dQuery, dKey, dWeights, qDeviceAddr, kDeviceAddr,
dyDeviceAddr, sparseIndicesDeviceAddr, weightsDeviceAddr, dQueryAddr, dKeyAddr, dWeightsAddr,
workspaceSize, workspaceAddr, deviceId, &context, &stream);
return ret);
ret = CreateAclTensor(dKeyHostData, dKeyShape, &dKeyAddr, aclDataType::ACL_FLOAT16, &dKey);
CHECK_RET(ret == ACL_SUCCESS,
FreeResource(q, k, dy, sparseIndices, weights, dQuery, dKey, dWeights, qDeviceAddr, kDeviceAddr,
dyDeviceAddr, sparseIndicesDeviceAddr, weightsDeviceAddr, dQueryAddr, dKeyAddr, dWeightsAddr,
workspaceSize, workspaceAddr, deviceId, &context, &stream);
return ret);
ret = CreateAclTensor(dWeightsHostData, dWeightsShape, &dWeightsAddr, aclDataType::ACL_FLOAT16, &dWeights);
CHECK_RET(ret == ACL_SUCCESS,
FreeResource(q, k, dy, sparseIndices, weights, dQuery, dKey, dWeights, qDeviceAddr, kDeviceAddr,
dyDeviceAddr, sparseIndicesDeviceAddr, weightsDeviceAddr, dQueryAddr, dKeyAddr, dWeightsAddr,
workspaceSize, workspaceAddr, deviceId, &context, &stream);
return ret);
// 3. Call the CANN operator library API, and change it to the specific API name.
aclOpExecutor *executor;
// Call the first-phase API of aclnnLightningIndexerGrad.
ret = aclnnLightningIndexerGradGetWorkspaceSize(q, k, dy, sparseIndices, weights, nullptr, nullptr, headNum,
layoutStr, sparseMode, preToken, nextToken, deteminstic, dQuery,
dKey, dWeights, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnLightningIndexerGradGetWorkspaceSize failed. ERROR: %d\n", ret);
FreeResource(q, k, dy, sparseIndices, weights, dQuery, dKey, dWeights, qDeviceAddr, kDeviceAddr,
dyDeviceAddr, sparseIndicesDeviceAddr, weightsDeviceAddr, dQueryAddr, dKeyAddr, dWeightsAddr,
workspaceSize, workspaceAddr, deviceId, &context, &stream);
return ret);
// Allocate device memory based on the workspaceSize calculated by the first-phase API.
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);
FreeResource(q, k, dy, sparseIndices, weights, dQuery, dKey, dWeights, qDeviceAddr, kDeviceAddr,
dyDeviceAddr, sparseIndicesDeviceAddr, weightsDeviceAddr, dQueryAddr, dKeyAddr,
dWeightsAddr, workspaceSize, workspaceAddr, deviceId, &context, &stream);
return ret);
}
// Call the second-phase API of aclnnLightningIndexerGrad.
ret = aclnnLightningIndexerGrad(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnLightningIndexerGrad failed. ERROR: %d\n", ret);
FreeResource(q, k, dy, sparseIndices, weights, dQuery, dKey, dWeights, qDeviceAddr, kDeviceAddr,
dyDeviceAddr, sparseIndicesDeviceAddr, weightsDeviceAddr, dQueryAddr, dKeyAddr, dWeightsAddr,
workspaceSize, workspaceAddr, deviceId, &context, &stream);
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);
FreeResource(q, k, dy, sparseIndices, weights, dQuery, dKey, dWeights, qDeviceAddr, kDeviceAddr,
dyDeviceAddr, sparseIndicesDeviceAddr, weightsDeviceAddr, dQueryAddr, dKeyAddr, dWeightsAddr,
workspaceSize, workspaceAddr, deviceId, &context, &stream);
return ret);
// 5. Obtain the output value, copy the result from the device to the host, and modify it based on the definition of the specific API.
PrintOutResult<aclFloat16>(dQueryShape, &dQueryAddr);
PrintOutResult<aclFloat16>(dKeyShape, &dKeyAddr);
PrintOutResult<aclFloat16>(dWeightsShape, &dWeightsAddr);
// 6. Release aclTensor and aclScalar, and modify them based on the definition of the specific API; release device resources.
FreeResource(q, k, dy, sparseIndices, weights, dQuery, dKey, dWeights, qDeviceAddr, kDeviceAddr, dyDeviceAddr,
sparseIndicesDeviceAddr, weightsDeviceAddr, dQueryAddr, dKeyAddr, dWeightsAddr, workspaceSize,
workspaceAddr, deviceId, &context, &stream);
return 0;
}