aclnnNsaSelectedAttention
产品支持情况
| 产品 | 是否支持 |
|---|---|
| Ascend 950PR/Ascend 950DT | × |
| Atlas A3 训练系列产品/Atlas A3 推理系列产品 | x |
| Atlas A2 训练系列产品/Atlas A2 推理系列产品 | √ |
| Atlas 200I/500 A2 推理产品 | × |
| Atlas 推理系列产品 | × |
| Atlas 训练系列产品 | × |
功能说明
-
接口功能:训练场景下,实现NativeSparseAttention算法中selected-attention(选择注意力)的计算。
-
计算公式: 选择注意力的正向计算公式如下:
selected_key=Gather(key,topk_indices[i]),0<=i<selected_block_countselected_value=Gather(value,topk_indices[i]),0<=i<selected_block_countselected\_key = Gather(key, topk\_indices[i]),0<=i<selected\_block\_count \\ selected\_value = Gather(value, topk\_indices[i]),0<=i<selected\_block\_count
attention_out=Softmax(Mask(scale∗(query@selected_keyT),atten_mask))@selected_valueattention\_out = Softmax(Mask(scale * (query @ selected\_key^T), atten\_mask)) @ selected\_value
函数原型
每个算子分为两段式接口,必须先调用“aclnnNsaSelectedAttentionGetWorkspaceSize”接口获取计算所需workspace大小以及包含了算子计算流程的执行器,再调用“aclnnNsaSelectedAttention”接口执行计算。
aclnnStatus aclnnNsaSelectedAttentionGetWorkspaceSize(
const aclTensor *query,
const aclTensor *key,
const aclTensor *value,
const aclTensor *topkIndices,
const aclTensor *attenMaskOptional,
const aclIntArray *actualSeqQLenOptional,
const aclIntArray *actualSeqKvLenOptional,
double scaleValue,
int64_t headNum,
char *inputLayout,
int64_t sparseMode,
int64_t selectedBlockSize,
int64_t selectedBlockCount,
const aclTensor *softmaxMaxOut,
const aclTensor *softmaxSumOut,
const aclTensor *attentionOut,
uint64_t *workspaceSize,
aclOpExecutor **executor)
aclnnStatus aclnnNsaSelectedAttention(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
const aclrtStream stream)
aclnnNsaSelectedAttentionGetWorkspaceSize
-
参数说明
参数名 输入/输出 描述 使用说明 数据类型 数据格式 维度(shape) 非连续Tensor query 输入 公式中的query。 数据类型需与key/value一致。 BFLOAT16、FLOAT16 ND 3-4 √ key 输入 公式中的key。 数据类型需与query/value一致。 BFLOAT16、FLOAT16 ND 3-4 √ value 输入 公式中的value。 数据类型需与query/key一致。 BFLOAT16、FLOAT16 ND 3-4 √ topkIndices 输入 公式中的topk_indices。 shape需为[T_q, N_kv, selected_block_count], 表示所选数据的索引。 INT32 ND 3 √ attenMaskOptional 输入 公式中的atten_mask。 - 取值true/1表示不参与计算。
- 取值false/0表示参与计算。
BOOL、UINT8 ND 2 √ actualSeqQLenOptional 输入 表示query每个Batch S的累加和长度。 TND排布时需要输入,其余场景输入nullptr。 INT64 ND 1 - actualSeqKvLenOptional 输入 表示key/value每个Batch S的累加和长度。 TND排布时需要输入,其余场景输入nullptr。 INT64 ND 1 - scaleValue 输入 公式中的scale,代表缩放系数。 一般设置为D^-0.5,其中D为query的head维度。 DOUBLE - - - headNum 输入 代表head个数。 - INT64 - - - inputLayout 输入 代表query/key/value的数据排布格式。 当前仅支持TND。 String - - - selectedBlockSize 输入 表示select的每个block长度。 - INT64 - - - selectedBlockCount 输入 表示select block的数量。 - INT64 - - - sparseMode 输入 表示sparse模式。 支持取值0或2。 INT64 - - - softmaxMaxOut 输出 Softmax计算的Max中间结果。 用于反向计算。 FLOAT ND 3 √ softmaxSumOut 输出 Softmax计算的Sum中间结果。 用于反向计算。 FLOAT ND 3 √ attentionOut 输出 计算公式的最终输出。 数据类型与query一致。 BFLOAT16、FLOAT16 ND 3-4 √ workspaceSize 输出 返回需要在Device侧申请的workspace大小。 - - - - - executor 输出 返回op执行器,包含算子计算流程。 - - - - - -
返回值
返回aclnnStatus状态码,具体参见aclnn返回码。
第一段接口会完成入参校验,出现以下场景时报错:
返回值 错误码 描述 ACLNN_ERR_PARAM_NULLPTR 161001 传入参数是必选输入,输出或者必选属性,且是空指针。 ACLNN_ERR_PARAM_INVALID 161002 query、key、value、attenMaskOptional、softmaxMaxOut、softmaxSumOut、attentionOut的数据类型和数据格式不在支持的范围内。 inputLayout输入的类型不在支持的范围内。
aclnnNsaSelectedAttention
-
参数说明
参数名 输入/输出 描述 workspace 输入 在Device侧申请的workspace内存地址。 workspaceSize 输入 在Device侧申请的workspace大小,由第一段接口aclnnNsaSelectedAttentionGetWorkspaceSize获取。 executor 输入 op执行器,包含了算子计算流程。 stream 输入 指定执行任务的Stream。 -
返回值
返回aclnnStatus状态码,具体参见aclnn返回码。
约束说明
-
确定性计算:
- aclnnNsaSelectedAttention默认确定性实现。
-
该接口与PyTorch配合使用时,需要保证CANN相关包与PyTorch相关包的版本匹配。
-
输入query、key、value的batchsize必须相等,即要求传入的actualSeqQLenOptional和actualSeqKvLenOptional具有相同的长度。
-
输入query、key、value的D:Head-Dim必须满足(D_q == D_k && D_k >= D_v)。
-
输入query、key、value的数据类型必须一致。
-
输入query、key、value的inputLayout必须一致。
-
sparseMode目前支持0和2。
-
selectedBlockSize支持<=128且满足16的整数倍。
-
selectedBlockCount:支持[1~128]。 总计选择的大小
selectedBlockCount * selectedBlockSize< 128*64(8K) -
Layout为TND时,每个Batch的S2都要大于总计选择的大小
selectedBlockCount * selectedBlockSize -
inputLayout目前仅支持TND。
-
支持输入query的N和key/value的N不相等,但必须成比例关系,即N_q / N_kv必须是非0整数,称为G(group),且需满足G <= 32。
-
当attenMaskOptional输入为nullptr时,sparseMode参数不生效,固定为全计算。
-
关于数据shape的约束,以inputLayout的TND举例(注:T等于各batch S的长度累加和。当各batch的S相等时,T=B*S)。其中:
- B(Batchsize):取值范围为1~1024。
- N(Head-Num):取值范围为1~128。
- G(Group):取值范围为1~32。
- S(Seq-Length):取值范围为1~128K。同时需要满足S_kv >= selectedBlockSize * selectedBlockCount,且S_kv长度为selectedBlockSize的整数倍。
- D(Head-Dim):D_qk=192,D_v=128。
调用示例
通过aclnn单算子调用示例代码如下,仅供参考,具体编译和执行过程请参考编译与运行样例。
#include <iostream>
#include <cstdio>
#include <string>
#include <vector>
#include <fstream>
#include <sys/stat.h>
#include "acl/acl.h"
#include "aclnnop/aclnn_nsa_selected_attention.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 CopyOutResult(int64_t outIndex, 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(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);
if(outIndex == 2) {
for (int64_t i = 0; i < size; i++) {
LOG_PRINT("attention out result 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); 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);
// 调用aclrtMalloc申请device侧内存
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);
// 调用aclrtMemcpy将host侧数据拷贝到device侧内存上
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);
// 计算连续tensor的strides
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];
}
// 调用aclCreateTensor接口创建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 *v, aclTensor *attentionOut, aclTensor *softmaxMax,
aclTensor *softmaxSum, void *qDeviceAddr, void *kDeviceAddr, void *vDeviceAddr, void *attentionOutDeviceAddr,
void *softmaxMaxDeviceAddr, void *softmaxSumDeviceAddr, uint64_t workspaceSize, void *workspaceAddr,
int32_t deviceId, aclrtContext *context, aclrtStream *stream)
{
// 释放aclTensor和aclScalar,需要根据具体API的接口定义修改
if (q != nullptr) {
aclDestroyTensor(q);
}
if (k != nullptr) {
aclDestroyTensor(k);
}
if (v != nullptr) {
aclDestroyTensor(v);
}
if (attentionOut != nullptr) {
aclDestroyTensor(attentionOut);
}
if (softmaxMax != nullptr) {
aclDestroyTensor(softmaxMax);
}
if (softmaxSum != nullptr) {
aclDestroyTensor(softmaxSum);
}
// 释放device资源
if (qDeviceAddr != nullptr) {
aclrtFree(qDeviceAddr);
}
if (kDeviceAddr != nullptr) {
aclrtFree(kDeviceAddr);
}
if (vDeviceAddr != nullptr) {
aclrtFree(vDeviceAddr);
}
if (attentionOutDeviceAddr != nullptr) {
aclrtFree(attentionOutDeviceAddr);
}
if (softmaxMaxDeviceAddr != nullptr) {
aclrtFree(softmaxMaxDeviceAddr);
}
if (softmaxSumDeviceAddr != nullptr) {
aclrtFree(softmaxSumDeviceAddr);
}
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
if (stream != nullptr) {
aclrtDestroyStream(stream);
}
if (context != nullptr) {
aclrtDestroyContext(context);
}
aclrtResetDevice(deviceId);
aclFinalize();
}
int main()
{
// 1. (固定写法)device/context/stream初始化,参考AscendCL对外接口列表
// 根据自己的实际device填写deviceId
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. 构造输入与输出,需要根据API的接口自定义构造
// 如果需要修改shape值,需要同步修改../scripts/fa_generate_data.py中 test_nsa_selected_attention 分支下生成
// query、key、value对应的shape值,并重新gen data,再执行
int64_t batch = 2;
int64_t s1 = 512;
int64_t s2 = 2048;
int64_t d1 = 192;
int64_t d2 = 128;
int64_t g = 4;
int64_t n2 = 4;
std::vector<int64_t> qShape = {batch * s1, n2 * g, d1};
std::vector<int64_t> kShape = {batch * s2, n2, d1};
std::vector<int64_t> vShape = {batch * s2, n2, d2};
std::vector<int64_t> topKIndicesShape = {batch * s1, n2, 16};
std::vector<int64_t> attentionOutShape = {batch * s1, n2 * g, d2};
std::vector<int64_t> softmaxMaxShape = {batch * s1, n2 * g, 8};
std::vector<int64_t> softmaxSumShape = {batch * s1, n2 * g, 8};
double scaleValue = 1.0;
int64_t headNum = 16;
int64_t selectedBlockSize = 64;
int64_t selectedBlockCount = 16;
int64_t sparseMod = 2;
char layOut[] = "TND";
void *qDeviceAddr = nullptr;
void *kDeviceAddr = nullptr;
void *vDeviceAddr = nullptr;
void *topKIndicesDeviceAddr = nullptr;
void *attentionOutDeviceAddr = nullptr;
void *softmaxMaxDeviceAddr = nullptr;
void *softmaxSumDeviceAddr = nullptr;
aclTensor *q = nullptr;
aclTensor *k = nullptr;
aclTensor *v = nullptr;
aclTensor *topKIndices = nullptr;
aclTensor *attenMaskOptional = nullptr;
aclTensor *softmaxMax = nullptr;
aclTensor *softmaxSum = nullptr;
aclTensor *attentionOut = nullptr;
std::vector<int64_t> actualSeqQLenVec = {512, 1024};
std::vector<int64_t> actualSeqKvLenVec = {2048, 4096};
aclIntArray *actualSeqQLenOptional = aclCreateIntArray(actualSeqQLenVec.data(), actualSeqQLenVec.size());
aclIntArray *actualSeqKvLenOptional = aclCreateIntArray(actualSeqKvLenVec.data(), actualSeqKvLenVec.size());
std::vector<aclFloat16> qHostData(GetShapeSize(qShape), 1);
std::vector<aclFloat16> kHostData(GetShapeSize(kShape), 1);
std::vector<aclFloat16> vHostData(GetShapeSize(vShape), 1);
std::vector<int32_t> topkIndicesHostData(GetShapeSize(topKIndicesShape), 2);
std::vector<float> attentionOutHostData(GetShapeSize(attentionOutShape), 0.0);
std::vector<float> softmaxMaxHostData(GetShapeSize(softmaxMaxShape), 0.0);
std::vector<float> softmaxSumHostData(GetShapeSize(softmaxSumShape), 0.0);
uint64_t workspaceSize = 0;
void *workspaceAddr = nullptr;
// 创建acl Tensor
ret = CreateAclTensor(qHostData, qShape, &qDeviceAddr, aclDataType::ACL_FLOAT16, &q);
CHECK_RET(ret == ACL_SUCCESS,
FreeResource(q, k, v, attentionOut, softmaxMax, softmaxSum, qDeviceAddr, kDeviceAddr, vDeviceAddr,
attentionOutDeviceAddr, softmaxMaxDeviceAddr, softmaxSumDeviceAddr, workspaceSize, workspaceAddr,
deviceId, &context, &stream);
return ret);
ret = CreateAclTensor(kHostData, kShape, &kDeviceAddr, aclDataType::ACL_FLOAT16, &k);
CHECK_RET(ret == ACL_SUCCESS,
FreeResource(q, k, v, attentionOut, softmaxMax, softmaxSum, qDeviceAddr, kDeviceAddr, vDeviceAddr,
attentionOutDeviceAddr, softmaxMaxDeviceAddr, softmaxSumDeviceAddr, workspaceSize, workspaceAddr,
deviceId, &context, &stream);
return ret);
ret = CreateAclTensor(vHostData, vShape, &vDeviceAddr, aclDataType::ACL_FLOAT16, &v);
CHECK_RET(ret == ACL_SUCCESS,
FreeResource(q, k, v, attentionOut, softmaxMax, softmaxSum, qDeviceAddr, kDeviceAddr, vDeviceAddr,
attentionOutDeviceAddr, softmaxMaxDeviceAddr, softmaxSumDeviceAddr, workspaceSize, workspaceAddr,
deviceId, &context, &stream);
return ret);
ret = CreateAclTensor(topkIndicesHostData, topKIndicesShape, &topKIndicesDeviceAddr, aclDataType::ACL_INT32, &topKIndices);
CHECK_RET(ret == ACL_SUCCESS,
FreeResource(q, k, v, attentionOut, softmaxMax, softmaxSum, qDeviceAddr, kDeviceAddr, vDeviceAddr,
attentionOutDeviceAddr, softmaxMaxDeviceAddr, softmaxSumDeviceAddr, workspaceSize, workspaceAddr,
deviceId, &context, &stream);
return ret);
ret = CreateAclTensor(attentionOutHostData, attentionOutShape, &attentionOutDeviceAddr, aclDataType::ACL_FLOAT16,
&attentionOut);
CHECK_RET(ret == ACL_SUCCESS,
FreeResource(q, k, v, attentionOut, softmaxMax, softmaxSum, qDeviceAddr, kDeviceAddr, vDeviceAddr,
attentionOutDeviceAddr, softmaxMaxDeviceAddr, softmaxSumDeviceAddr, workspaceSize, workspaceAddr,
deviceId, &context, &stream);
return ret);
ret = CreateAclTensor(softmaxMaxHostData, softmaxMaxShape, &softmaxMaxDeviceAddr, aclDataType::ACL_FLOAT,
&softmaxMax);
CHECK_RET(ret == ACL_SUCCESS,
FreeResource(q, k, v, attentionOut, softmaxMax, softmaxSum, qDeviceAddr, kDeviceAddr, vDeviceAddr,
attentionOutDeviceAddr, softmaxMaxDeviceAddr, softmaxSumDeviceAddr, workspaceSize, workspaceAddr,
deviceId, &context, &stream);
return ret);
ret = CreateAclTensor(softmaxSumHostData, softmaxSumShape, &softmaxSumDeviceAddr, aclDataType::ACL_FLOAT,
&softmaxSum);
CHECK_RET(ret == ACL_SUCCESS,
FreeResource(q, k, v, attentionOut, softmaxMax, softmaxSum, qDeviceAddr, kDeviceAddr, vDeviceAddr,
attentionOutDeviceAddr, softmaxMaxDeviceAddr, softmaxSumDeviceAddr, workspaceSize, workspaceAddr,
deviceId, &context, &stream);
return ret);
// 3. 调用CANN算子库API,需要修改为具体的Api名称
aclOpExecutor *executor;
// 调用aclnnNsaSelectedAttention第一段接口
ret = aclnnNsaSelectedAttentionGetWorkspaceSize(
q, k, v, topKIndices, attenMaskOptional, actualSeqQLenOptional, actualSeqKvLenOptional, scaleValue, headNum,
layOut, sparseMod, selectedBlockSize, selectedBlockCount, softmaxMax, softmaxSum, attentionOut, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnNsaSelectedAttentionGetWorkspaceSize failed. ERROR: %d\n", ret);
FreeResource(q, k, v, attentionOut, softmaxMax, softmaxSum, qDeviceAddr, kDeviceAddr, vDeviceAddr,
attentionOutDeviceAddr, softmaxMaxDeviceAddr, softmaxSumDeviceAddr, workspaceSize, workspaceAddr,
deviceId, &context, &stream);
return ret);
// 根据第一段接口计算出的workspaceSize申请device内存
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, v, attentionOut, softmaxMax, softmaxSum, qDeviceAddr, kDeviceAddr, vDeviceAddr,
attentionOutDeviceAddr, softmaxMaxDeviceAddr, softmaxSumDeviceAddr, workspaceSize, workspaceAddr,
deviceId, &context, &stream);
return ret);
}
// 调用aclnnNsaSelectedAttention第二段接口
ret = aclnnNsaSelectedAttention(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnNsaSelectedAttention failed. ERROR: %d\n", ret);
FreeResource(q, k, v, attentionOut, softmaxMax, softmaxSum, qDeviceAddr, kDeviceAddr, vDeviceAddr,
attentionOutDeviceAddr, softmaxMaxDeviceAddr, softmaxSumDeviceAddr, workspaceSize, workspaceAddr,
deviceId, &context, &stream);
return ret);
// 4. (固定写法)同步等待任务执行结束
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret);
FreeResource(q, k, v, attentionOut, softmaxMax, softmaxSum, qDeviceAddr, kDeviceAddr, vDeviceAddr,
attentionOutDeviceAddr, softmaxMaxDeviceAddr, softmaxSumDeviceAddr, workspaceSize, workspaceAddr,
deviceId, &context, &stream);
return ret);
// 5. 获取输出的值,将device侧内存上的结果拷贝至host侧,需要根据具体API的接口定义修改
CopyOutResult<float>(0, softmaxMaxShape, &softmaxMaxDeviceAddr);
CopyOutResult<float>(1, softmaxSumShape, &softmaxSumDeviceAddr);
CopyOutResult<aclFloat16>(2, attentionOutShape, &attentionOutDeviceAddr);
// 6. 释放aclTensor和aclScalar,需要根据具体API的接口定义修改; 释放device资源
FreeResource(q, k, v, attentionOut, softmaxMax, softmaxSum, qDeviceAddr, kDeviceAddr, vDeviceAddr,
attentionOutDeviceAddr, softmaxMaxDeviceAddr, softmaxSumDeviceAddr, workspaceSize, workspaceAddr,
deviceId, &context, &stream);
return 0;
}