昇腾社区首页
中文
注册

aclnnRingAttentionUpdate

支持的产品型号

  • Atlas A2训练系列产品/Atlas 800I A2推理产品。

接口原型

每个算子分为undefined,必须先调用“aclnnRingAttentionUpdateGetWorkspaceSize”接口获取计算所需workspace大小以及包含了算子计算流程的执行器,再调用“aclnnRingAttentionUpdate”接口执行计算。

  • aclnnStatus aclnnRingAttentionUpdateGetWorkspaceSize(const aclTensor* prevAttnOut, const aclTensor* prevSoftmaxMax, const aclTensor* prevSoftmaxSum, const aclTensor* curAttnOut, const aclTensor* curSoftmaxMax, const aclTensor* curSoftmaxSum, const aclTensor* actualSeqQlenOptional, char* inputLayout, aclTensor* attnOut, aclTensor* softmaxMax, aclTensor* softmaxSum, uint64_t* workspaceSize, aclOpExecutor** executor)
  • aclnnStatus aclnnRingAttentionUpdate(void* workspace, uint64_t workspaceSize, aclOpExecutor* executor, aclrtStream stream)

功能描述

  • 接口功能:RingAttentionUpdate算子功能是将两次FlashAttention的输出根据其不同的softmax的max和sum更新。

  • 计算公式:

softmax_max=max(prev_softmax_max,cur_softmax_max)softmax\_max = max(prev\_softmax\_max, cur\_softmax\_max) softmax_sum=prev_softmax_sumexp(prev_softmax_maxsoftmax_max)+exp(cur_softmax_maxsoftmax_max)softmax\_sum = prev\_softmax\_sum * exp(prev\_softmax\_max - softmax\_max) + exp(cur\_softmax\_max - softmax\_max) attn_out=prev_attn_outexp(prev_softmax_maxsoftmax_max)/softmax_sum+cur_attn_outexp(cur_softmax_maxsoftmax_max)/softmax_sumattn\_out = prev\_attn\_out * exp(prev\_softmax\_max - softmax\_max) / softmax\_sum + cur\_attn\_out * exp(cur\_softmax\_max - softmax\_max) / softmax\_sum

aclnnRingAttentionUpdateGetWorkspaceSize

  • 参数说明:

    • prevAttnOut(aclTensor*,计算输入):Device侧的aclTensor,公式中的prev_attn_out,数据类型支持FLOAT16、FLOAT、BFLOAT16,输入shape和inputLayout属性保持一致,支持undefinedundefined支持ND。
    • prevSoftmaxMax(aclTensor*,计算输入):Device侧的aclTensor,公式中的prev_softmax_max,数据类型支持FLOAT,输入shape为(B,N,S,8),最后一维8个数字相同,且需要为正数,支持undefinedundefined支持ND。
    • prevSoftmaxSum(aclTensor*,计算输入):Device侧的aclTensor,公式中的prev_softmax_sum,数据类型支持FLOAT,输入shape和prevSoftmaxMax保持一致,最后一维8个数字相同,且需要为正数,支持undefinedundefined支持ND。
    • curAttnOut(aclTensor*,计算输入):Device侧的aclTensor,公式中的cur_attn_out,数据类型支持FLOAT16、FLOAT、BFLOAT16,数据类型和输入shape和prevAttnOut保持一致,支持undefinedundefined支持ND。
    • curSoftmaxMax(aclTensor*,计算输入):Device侧的aclTensor,公式中的cur_softmax_max,数据类型支持FLOAT,输入shape和prevSoftmaxMax保持一致,最后一维8个数字相同,且需要为正数,支持undefinedundefined支持ND。
    • curSoftmaxSum(aclTensor*,计算输入):Device侧的aclTensor,公式中的cur_softmax_sum,数据类型支持FLOAT,输入shape和prevSoftmaxMax保持一致,最后一维8个数字相同,且需要为正数,支持undefinedundefined支持ND。
    • actualSeqQlenOptional(aclTensor*,计算输入):Device侧的aclTensor,预留接口,暂时无效,当有输入时,数据类型支持INT64。
    • inputLayout(char*,计算输入):Host侧的char*常量,attn_out相关输入的数据排布。预留接口,暂时无效。
    • attnOut(aclTensor*,计算输出):Device侧的aclTensor,公式中的attn_out,数据类型支持FLOAT16、FLOAT、BFLOAT16,数据类型和输出shape和prevAttnOut保持一致,支持undefinedundefined支持ND。
    • softmaxMax(aclTensor*,计算输出):Device侧的aclTensor,公式中的softmax_max,数据类型支持FLOAT,输出shape和prevSoftmaxMax保持一致,支持undefinedundefined支持ND。
    • softmaxSum(aclTensor*,计算输出):Device侧的aclTensor,公式中的softmax_sum,数据类型支持FLOAT,输出shape和prevSoftmaxMax保持一致,支持undefinedundefined支持ND。
    • workspaceSize(uint64_t*, 出参):返回需要在Device侧申请的workspace大小。
    • executor(aclOpExecutor**, 出参):返回op执行器,包含算子计算流程。
  • 返回值:

    aclnnStatus: 返回状态码,具体参见undefined

    [object Object]

aclnnRingAttentionUpdate

  • 参数说明

    • workspace(void *, 入参):在Device侧申请的workspace内存地址。
    • workspaceSize(uint64_t, 入参):在Device侧申请的workspace大小,由第一段接口aclnnRingAttentionUpdateGetWorkspaceSize获取。
    • executor(aclOpExecutor*, 入参):op执行器,包含了算子计算流程。
    • stream(aclrtStream, 入参):指定执行任务的AscendCL Stream流。
  • 返回值: aclnnStatus:返回状态码,具体参见undefined

约束与限制

调用示例

示例代码如下,仅供参考,具体编译和执行过程请参考undefined

[object Object]