
aclnnDenseLightningIndexerSoftmaxLse【免费下载链接】ops-transformer本项目是CANN提供的transformer类大模型算子库实现网络在NPU上加速计算。项目地址: https://gitcode.com/cann/ops-transformer产品支持情况产品是否支持Ascend 950PR/Ascend 950DT×Atlas A3 训练系列产品/Atlas A3 推理系列产品√Atlas A2 训练系列产品/Atlas A2 推理系列产品√Atlas 200I/500 A2 推理产品×Atlas 推理系列产品×Atlas 训练系列产品×功能说明接口功能DenseLightningIndexerSoftmaxLse算子是DenseLightningIndexerGradKlLoss算子计算Softmax输入的一个分支算子。计算公式$$ \text{res}\text{AttentionMask}\left(\text{ReduceSum}\left(W\odot\text{ReLU}\left(Q_{index}K_{index}^T\right)\right)\right) $$$$ \text{maxIndex}\text{max}\left(res\right) $$$$ \text{sumIndex}\text{ReduceSum}\left(\text{exp}\left(res-maxIndex\right)\right) $$maxIndexsumIndex作为输出传递给算子DenseLightningIndexerGradKlLoss作为输入计算Softmax使用。函数原型算子执行接口为两段式接口必须先调用“aclnnDenseLightningIndexerSoftmaxLseGetWorkspaceSize”接口获取入参并根据计算流程计算所需workspace大小再调用“aclnnDenseLightningIndexerSoftmaxLse”接口执行计算。aclnnStatus aclnnDenseLightningIndexerSoftmaxLseGetWorkspaceSize( const aclTensor *queryIndex, const aclTensor *keyIndex, const aclTensor *weight, const aclIntArray *actualSeqLengthsQueryOptional, const aclIntArray *actualSeqLengthsKeyOptional, char *layoutOptional, int64_t sparseMode, int64_t preTokens, int64_t nextTokens, const aclTensor *softmaxMaxOut, const aclTensor *softmaxSumOut, uint64_t *workspaceSize, aclOpExecutor **executor);aclnnStatus aclnnDenseLightningIndexerSoftmaxLse( void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream);aclnnDenseLightningIndexerSoftmaxLse参数说明:参数名输入/输出描述使用说明数据类型数据格式维度(shape)非连续TensorqueryIndexaclTensor*输入lightningIndexer结构的输入queryIndex。B支持泛化且与query的B保持一致。Nidx164、32、16、8。D128。T1多个Batch的S1累加。FLOAT16、BFLOAT16ND(B,S1,Nidx1,D);(T1,Nidx1,D)×keyIndexaclTensor*输入lightningIndexer结构的输入keyIndex。B支持泛化且与queryIndex的B保持一致。S2支持泛化。Nidx21。D128。T2多个Batch的S2累加。FLOAT16、BFLOAT16ND(B,S2,Nidx2,D);(T2,Nidx2,D)×weightaclTensor*输入权重B支持泛化且与queryIndex的B保持一致。S1支持泛化且与queryIndex的S1保持一致。Nidx164、32、16、8。T1多个Batch的S1累加。FLOAT16、BFLOAT16、FLOAT32ND(B,S1,Nidx1);(T1,Nidx1)×actualSeqLengthsQueryOptionalaclIntArray*输入每个Batch中Query的有效token数值依赖。长度与B保持一致。TND格式下最后一个元素为累加和累加和小于等于T1。INT64ND(B,)-actualSeqLengthsKeyOptionalaclIntArray*输入每个Batch中Key的有效token数值依赖。长度与B保持一致。TND格式下最后一个元素为累加和累加和小于等于T2。INT64ND(B,)-layoutOptionalchar*输入layout格式仅支持BSND和TND格式。STRING---sparseModeint64_t输入sparse的模式表示sparse的模式。sparse不同模式的详细说明请参见约束说明。仅支持模式3。----preTokensint64_t输入用于稀疏计算表示Attention需要和前几个token计算关联和Attention中的preTokens定义相同在sparseMode 0和4的时候生效仅支持2^63-1。----nextTokensint64_t输入用于稀疏计算表示Attention需要和后几个token计算关联和Attention中的nextTokens定义相同在sparseMode 0和4的时候生效仅支持2^63-1。----softmaxMaxOutaclTensor*输出softmax计算使用的max值B支持泛化与queryIndex的B保持一致。Nidx2与keyIndex的Nidx2保持一致。S1支持泛化且与queryIndex的S1保持一致。T1多个Batch的S1累加。FLOAT32ND(B,Nidx2,S1);(Nidx2,T1)×softmaxSumOutaclTensor*输出softmax计算使用的sum值B支持泛化与query的B保持一致。Nidx2与keyIndex的Nidx2保持一致。S1支持泛化且与queryIndex的S1保持一致。T1多个Batch的S1累加。FLOAT32ND(B,Nidx2,S1);(Nidx2,T1)×workspaceSizeuint64_t*输出返回需要在Device侧申请的workspace大小。-----executoraclOpExecutor**输出返回op执行器包含了算子计算流程。-----返回值返回aclnnStatus状态码具体参见aclnn返回码。第一段接口完成入参校验出现以下场景时报错返回值错误码描述ACLNN_ERR_PARAM_NULLPTR161001必选参数或者输出是空指针。ACLNN_ERR_PARAM_INVALID161002queryIndex、keyIndex、weights等输入变量的数据类型和数据格式不在支持的范围内。ACLNN_ERR_INNER_TILING_ERROR561002多个输入tensor之间的shape信息不匹配详见参数说明。aclnnDenseLightningIndexerSoftmaxLse参数说明参数名输入/输出描述workspace输入在Device侧申请的workspace内存地址。workspaceSize输入在Device侧申请的workspace大小由第一段接口aclnnDenseLightningIndexerSoftmaxLseGetWorkspaceSize获取。executor输入op执行器包含了算子计算流程。stream输入指定执行任务的Stream流。返回值返回aclnnStatus状态码具体参见aclnn返回码。约束说明参数queryIndex、keyIndex的数据类型应保持一致。参数weights不为float32时参数queryIndex、keyIndex、weights的数据类型应保持一致。确定性计算 aclnnDenseLightningIndexerSoftmaxLse默认确定性实现。公共约束入参为空的场景处理queryIndex为空Tensor直接返回。SFAG公共约束里入参为空的场景和FAG保持一致。sparseMode含义备注0defaultMask模式如果attenmask未传入则不做mask操作忽略preTokens和nextTokens如果传入则需要传入完整的attenmask矩阵表示preTokens和nextTokens之间的部分需要计算不支持1allMask必须传入完整的attenmask矩阵不支持2leftUpCausal模式的mask需要传入优化后的attenmask矩阵不支持3rightDownCausal模式的mask对应以右顶点为划分的下三角场景需要传入优化后的attenmask矩阵支持4band模式的mask需要传入优化后的attenmask矩阵不支持5prefix不支持6global不支持7dilated不支持8block_local不支持规格约束规格项规格规格说明B1~256-S1、S20~128KS1、S2支持不等长当layout为BSND时S1S2layout为TND时actualSeqLengthsQuery小于等于actualSeqLengthsKey相同索引位置的值且相同索引位置S1S2。Nidx18、16、32、64-Nidx21-D128-layoutBSND/TND-典型值规格项典型值queryIndexN1 64/32; D 128 ; S1 64k/128kkeyIndexD 128。调用示例调用示例代码如下仅供参考具体编译和执行过程请参考编译与运行样例。#include iostream #include vector #include cstdint #include cmath #include acl/acl.h #include aclnnop/aclnn_dense_lightning_indexer_softmax_lse.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::vectorint64_t shape) { int64_t shapeSize 1; for (auto i : shape) { shapeSize * i; } return shapeSize; } void PrintOutResult(std::vectorint64_t shape, void** deviceAddr) { auto size GetShapeSize(shape); std::vectoraclFloat16 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) { // 固定写法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::vectorT hostData, const std::vectorint64_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::vectorint64_t strides(shape.size(), 1); for (int64_t i 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; } 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的接口自定义构造 int64_t s1 4096; int64_t s2 4096; int64_t n1Index 8; int64_t n2Index 1; int64_t dQueryIndex 128; int64_t t1 s1; int64_t t2 s2; int64_t G n1Index / n2Index; std::vectorint64_t qIndexShape {t1, n1Index, dQueryIndex}; std::vectorint64_t kIndexShape {t2, n2Index, dQueryIndex}; std::vectorint64_t weightShape {t1, n1Index}; std::vectorint64_t softmaxMaxIndexShape {n2Index, t1}; std::vectorint64_t softmaxSumIndexShape {n2Index, t1}; void* qIndexDeviceAddr nullptr; void* kIndexDeviceAddr nullptr; void* weightDeviceAddr nullptr; void* softmaxMaxIndexDeviceAddr nullptr; void* softmaxSumIndexDeviceAddr nullptr; aclTensor* qIndex nullptr; aclTensor* kIndex nullptr; aclTensor* weight nullptr; aclTensor* softmaxMaxIndex nullptr; aclTensor* softmaxSumIndex nullptr; std::vectoraclFloat16 qIndexHostData(t1 * n1Index * dQueryIndex, aclFloatToFloat16(0.2)); std::vectoraclFloat16 kIndexHostData(t2 * n2Index * dQueryIndex, aclFloatToFloat16(0.1)); std::vectoraclFloat16 weightHostData(t1 * n1Index, aclFloatToFloat16(0.005)); std::vectorfloat softmaxMaxIndexHostData(t1 * n2Index, 25.4483f); std::vectorfloat softmaxSumIndexHostData(t1 * n2Index, 1.0f); ret CreateAclTensor(qIndexHostData, qIndexShape, qIndexDeviceAddr, aclDataType::ACL_FLOAT16, qIndex); CHECK_RET(ret ACL_SUCCESS, return ret); ret CreateAclTensor(kIndexHostData, kIndexShape, kIndexDeviceAddr, aclDataType::ACL_FLOAT16, kIndex); CHECK_RET(ret ACL_SUCCESS, return ret); ret CreateAclTensor(weightHostData, weightShape, weightDeviceAddr, aclDataType::ACL_FLOAT16, weight); CHECK_RET(ret ACL_SUCCESS, return ret); ret CreateAclTensor(softmaxMaxIndexHostData, softmaxMaxIndexShape, softmaxMaxIndexDeviceAddr, aclDataType::ACL_FLOAT, softmaxMaxIndex); CHECK_RET(ret ACL_SUCCESS, return ret); ret CreateAclTensor(softmaxSumIndexHostData, softmaxSumIndexShape, softmaxSumIndexDeviceAddr, aclDataType::ACL_FLOAT, softmaxSumIndex); CHECK_RET(ret ACL_SUCCESS, return ret); std::vectorint64_t acSeqQLenOp {t1}; std::vectorint64_t acSeqKvLenOp {t2}; aclIntArray* acSeqQLen aclCreateIntArray(acSeqQLenOp.data(), acSeqQLenOp.size()); aclIntArray* acSeqKvLen aclCreateIntArray(acSeqKvLenOp.data(), acSeqKvLenOp.size()); int64_t preTokens 9223372036854775807; int64_t nextTokens 9223372036854775807; int64_t sparseMode 3; char layOut[5] {T, N, D, 0}; // 3. 调用CANN算子库API需要修改为具体的Api名称 uint64_t workspaceSize 0; aclOpExecutor* executor; // 调用aclnnDenseLightningIndexerSoftmaxLseGetWorkspaceSize第一段接口 ret aclnnDenseLightningIndexerSoftmaxLseGetWorkspaceSize( qIndex, kIndex, weight, acSeqQLen, acSeqKvLen, layOut, sparseMode, preTokens, nextTokens, softmaxMaxIndex, softmaxSumIndex, workspaceSize, executor); CHECK_RET(ret ACL_SUCCESS, LOG_PRINT(aclnnDenseLightningIndexerSoftmaxLseGetWorkspaceSize failed. ERROR: %d\n, ret); return ret); // 根据第一段接口计算出的workspaceSize申请device内存 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); } // 调用aclnnDenseLightningIndexerSoftmaxLse第二段接口 ret aclnnDenseLightningIndexerSoftmaxLse(workspaceAddr, workspaceSize, executor, stream); CHECK_RET(ret ACL_SUCCESS, LOG_PRINT(aclnnDenseLightningIndexerSoftmaxLse failed. ERROR: %d\n, ret); return ret); // 4. 固定写法同步等待任务执行结束 ret aclrtSynchronizeStream(stream); CHECK_RET(ret ACL_SUCCESS, LOG_PRINT(aclrtSynchronizeStream failed. ERROR: %d\n, ret); return ret); // 5. 获取输出的值将device侧内存上的结果拷贝至host侧需要根据具体API的接口定义修改 PrintOutResult(softmaxMaxIndexShape, softmaxMaxIndexDeviceAddr); PrintOutResult(softmaxSumIndexShape, softmaxSumIndexDeviceAddr); // 6. 释放aclTensor和aclScalar需要根据具体API的接口定义修改 aclDestroyTensor(qIndex); aclDestroyTensor(kIndex); aclDestroyTensor(weight); aclDestroyTensor(softmaxMaxIndex); aclDestroyTensor(softmaxSumIndex); // 7. 释放device资源 aclrtFree(qIndexDeviceAddr); aclrtFree(kIndexDeviceAddr); aclrtFree(weightDeviceAddr); aclrtFree(softmaxMaxIndexDeviceAddr); aclrtFree(softmaxSumIndexDeviceAddr); if (workspaceSize 0) { aclrtFree(workspaceAddr); } aclrtDestroyStream(stream); aclrtDestroyContext(context); aclrtResetDevice(deviceId); aclFinalize(); return 0; }【免费下载链接】ops-transformer本项目是CANN提供的transformer类大模型算子库实现网络在NPU上加速计算。项目地址: https://gitcode.com/cann/ops-transformer创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考