昇腾社区首页
中文
注册

aclnnRingAttentionUpdate

产品支持情况

产品 是否支持
[object Object]Atlas A3 训练系列产品/Atlas A3 推理系列产品[object Object]
[object Object]Atlas A2 训练系列产品/Atlas 800I A2 推理产品/A200I A2 Box 异构组件[object Object]
[object Object]Atlas 200I/500 A2 推理产品[object Object] ×
[object Object]Atlas 推理系列产品 [object Object] ×
[object Object]Atlas 训练系列产品[object Object] ×

功能说明

  • 接口功能: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

函数原型

每个算子分为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 *inputLayoutOptional, const aclTensor *attnOutOut, const aclTensor *softmaxMaxOut, const aclTensor *softmaxSumOut, uint64_t *workspaceSize, aclOpExecutor **executor)
  • aclnnStatus aclnnRingAttentionUpdate(void *workspace, uint64_t workspaceSize, aclOpExecutor *executor, aclrtStream stream)

aclnnRingAttentionUpdateGetWorkspaceSize

  • 参数说明:

    • prevAttnOut(aclTensor*,计算输入):Device侧的aclTensor,公式中的prev_attn_out,第一次FlashAttention的输出,数据类型支持FLOAT16、FLOAT、BFLOAT16,输入shape和inputLayoutOptional属性保持一致,支持undefinedundefined支持ND。当输入数据排布inputLayoutOptional为TND时,D限制为64的倍数。
    • prevSoftmaxMax(aclTensor*,计算输入):Device侧的aclTensor,公式中的prev_softmax_max,第一次FlashAttention的softmax的max结果,数据类型支持FLOAT,输入shape为(B,N,S,8)或(T,N,8),最后一维8个数字相同,且需要为正数,支持undefinedundefined支持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个数字相同,且需要为正数,支持undefinedundefined支持ND。
    • curAttnOut(aclTensor*,计算输入):Device侧的aclTensor,公式中的cur_attn_out,第二次FlashAttention的输出,数据类型支持FLOAT16、FLOAT、BFLOAT16,数据类型和输入shape和prevAttnOut保持一致,支持undefinedundefined支持ND。当输入数据排布inputLayoutOptional为TND时,D限制为64的倍数。
    • curSoftmaxMax(aclTensor*,计算输入):Device侧的aclTensor,公式中的cur_softmax_max,第二次FlashAttention的softmax的max结果,数据类型支持FLOAT,输入shape和prevSoftmaxMax保持一致,最后一维8个数字相同,且需要为正数,支持undefinedundefined支持ND。
    • curSoftmaxSum(aclTensor*,计算输入):Device侧的aclTensor,公式中的cur_softmax_sum,第二次FlashAttention的softmax的sum结果,数据类型支持FLOAT,输入shape和prevSoftmaxMax保持一致,最后一维8个数字相同,且需要为正数,支持undefinedundefined支持ND。
    • actualSeqQlenOptional(aclTensor*,计算输入):Device侧的aclTensor,从0开始的sequence length的累加,数据类型支持INT64。当数据排布inputLayoutOptional为TND时,需要传入该参数,这是一个从0开始递增至T的整数aclTensor。
    • inputLayoutOptional(char*,计算输入):Host侧的char*常量,attn_out相关输入的数据排布。当前支持“TND”和“SBH”。
    • attnOutOut(aclTensor*,计算输出):Device侧的aclTensor,公式中的attn_out,通过两次结果更新后的输出,数据类型支持FLOAT16、FLOAT、BFLOAT16,数据类型和输出shape和prevAttnOut保持一致,支持undefinedundefined支持ND。
    • softmaxMaxOut(aclTensor*,计算输出):Device侧的aclTensor,公式中的softmax_max,通过两次结果更新后的softmax的max,数据类型支持FLOAT,输出shape和prevSoftmaxMax保持一致,支持undefinedundefined支持ND。
    • softmaxSumOut(aclTensor*,计算输出):Device侧的aclTensor,公式中的softmax_sum,通过两次结果更新后的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, 入参):指定执行任务的Stream。
  • 返回值: aclnnStatus:返回状态码,具体参见undefined

约束说明

  • 当inputLayoutOptional为“TND”时,prevAttnOut的最后一个维度需要为64的倍数。
  • 当inputLayoutOptional为“TND”时,actualSeqQlenOptional为必填。
  • 当inputLayoutOptional为“TND”时,请注意N和D的大小,限制为:N<=256, D<=768。

调用示例

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

[object Object]