昇腾社区首页
中文
注册
开发者
下载

aclnnRingAttentionUpdateV2

产品支持情况

[object Object]undefined

功能说明

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

    接口的区别是:在输入排布为TND的场景下,原RingAttentionUpdate接口中的softmax相关输入的数据排布为BNS8,RingAttentionUpdateV2 接口支持传入字符串参数 inputSoftmaxLayout,用于控制 softmax 相关输入的数据排布是否与attention保持一致(即采用 TND 排布)。

  • 计算公式:

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)+cur_softmax_sumexp(cur_softmax_maxsoftmax_max)softmax\_sum = prev\_softmax\_sum * exp(prev\_softmax\_max - softmax\_max) + cur\_softmax\_sum * exp(cur\_softmax\_max - softmax\_max) attn_out=prev_attn_outexp(prev_softmax_maxsoftmax_max)prev_softmax_sum/softmax_sum+cur_attn_outexp(cur_softmax_maxsoftmax_max)cur_softmax_sum/softmax_sumattn\_out = prev\_attn\_out * exp(prev\_softmax\_max - softmax\_max) * prev\_softmax\_sum / softmax\_sum + cur\_attn\_out * exp(cur\_softmax\_max - softmax\_max) * cur\_softmax\_sum / softmax\_sum

函数原型

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

[object Object]
[object Object]

aclnnRingAttentionUpdateV2GetWorkspaceSize

  • 参数说明

    • prevAttnOut(aclTensor*,计算输入):Device侧的aclTensor,公式中的prev_attn_out,第一次FlashAttention的输出,数据类型支持FLOAT16、FLOAT、BFLOAT16,输入shape和inputLayoutOptional属性保持一致,支持支持ND。
      • [object Object]Atlas A2 训练系列产品/Atlas A2 推理系列产品[object Object]、[object Object]Atlas A3 训练系列产品/Atlas A3 推理系列产品[object Object]:当输入数据排布inputLayoutOptional为TND时,D限制为64的倍数
    • prevSoftmaxMax(aclTensor*,计算输入):Device侧的aclTensor,公式中的prev_softmax_max,第一次FlashAttention的softmax的max结果,数据类型支持FLOAT,输入shape为(B,N,S,8)或(T,N,8),最后一维8个数字相同,且需要为正数,支持支持ND。此处B为batch size,N为head number,S为sequence length,T为time。
    • prevSoftmaxSum(aclTensor*,计算输入):Device侧的aclTensor,公式中的prev_softmax_sum,第一次FlashAttention的softmax的sum结果,数据类型支持FLOAT,输入shape和prevSoftmaxMax保持一致,最后一维8个数字相同,且需要为正数,支持支持ND。
    • curAttnOut(aclTensor*,计算输入):Device侧的aclTensor,公式中的cur_attn_out,第二次FlashAttention的输出,数据类型支持FLOAT16、FLOAT、BFLOAT16,数据类型和输入shape和prevAttnOut保持一致,支持支持ND。
      • [object Object]Atlas A2 训练系列产品/Atlas A2 推理系列产品[object Object]、[object Object]Atlas A3 训练系列产品/Atlas A3 推理系列产品[object Object]:当输入数据排布inputLayoutOptional为TND时,D限制为64的倍数
    • curSoftmaxMax(aclTensor*,计算输入):Device侧的aclTensor,公式中的cur_softmax_max,第二次FlashAttention的softmax的max结果,数据类型支持FLOAT,输入shape和prevSoftmaxMax保持一致,最后一维8个数字相同,且需要为正数,支持支持ND。
    • curSoftmaxSum(aclTensor*,计算输入):Device侧的aclTensor,公式中的cur_softmax_sum,第二次FlashAttention的softmax的sum结果,数据类型支持FLOAT,输入shape和prevSoftmaxMax保持一致,最后一维8个数字相同,且需要为正数,支持支持ND。
    • actualSeqQlenOptional(aclTensor*,计算输入):Device侧的aclTensor,从0开始的sequence length的累加,数据类型支持INT64。当数据排布inputLayoutOptional为TND时,需要传入该参数,这是一个从0开始递增至T的整数aclTensor。
    • inputLayoutOptional(char*,计算输入):Host侧的char*常量,prevAttnOut和curAttnOut的数据排布。当前支持“TND”和“SBH”。
    • inputSoftmaxLayoutOptional(char*,计算输入):Host侧的char*常量,prevSoftmaxMax、prevSoftmaxSum、curSoftmaxMax和curSoftmaxSum数据排布。当输入数据排布inputLayoutOptional为TND时生效,当前支持空字符串、“SBH”和“TND”。通过此开关控制是否对softmaxMax相关输入做转置操作。
    • attnOutOut(aclTensor*,计算输出):Device侧的aclTensor,公式中的attn_out,通过两次结果更新后的输出,数据类型支持FLOAT16、FLOAT、BFLOAT16,数据类型和输出shape和prevAttnOut保持一致,支持支持ND。
    • softmaxMaxOut(aclTensor*,计算输出):Device侧的aclTensor,公式中的softmax_max,通过两次结果更新后的softmax的max,数据类型支持FLOAT,输出shape和prevSoftmaxMax保持一致,支持支持ND。
    • softmaxSumOut(aclTensor*,计算输出):Device侧的aclTensor,公式中的softmax_sum,通过两次结果更新后的softmax的sum,数据类型支持FLOAT,输出shape和prevSoftmaxMax保持一致,支持支持ND。
    • workspaceSize(uint64_t*, 出参):返回需要在Device侧申请的workspace大小。
    • executor(aclOpExecutor**, 出参):返回op执行器,包含了算子计算流程。
  • 返回值

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

    第一段接口完成入参校验,出现以下场景时报错:

    [object Object]

aclnnRingAttentionUpdateV2

  • 参数说明

    [object Object]
  • 返回值

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

约束说明

  • 确定性计算:
    • aclnnRingAttentionUpdateV2默认确定性实现。
  • 当inputLayoutOptional为“TND”时,actualSeqQlenOptional为必填。
  • 当inputLayoutOptional为“TND”时,其中:
    • N:N<=256。
    • D:D<=768且D为64的倍数。
  • 当inputLayoutOptional为“TND”时,inputSoftmaxLayoutOptional才生效。inputSoftmaxLayoutOptional只支持三种输入:空字符串、“SBH”、“TND”

调用示例

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

[object Object]