aclnnFusedInferAttentionScoreV3

NOTE: This API will be deprecated in subsequent versions. Use the latest API aclnnFusedInferAttentionScoreV5 instead.

Applicable Products

  • Ascend 950PR&950DT products: Not supported
  • Atlas A3 products: Supported
  • Atlas A2 products: Supported
  • Atlas 200I/500 A2 inference products: Not supported
  • Atlas inference products: Not supported
  • Atlas training products: Not supported

Function

  • Description: A FlashAttention operator adapted to decode & prefill scenarios, supporting both the prefill computation scenario (PromptFlashAttention) and the decode computation scenario (IncreFlashAttention). Compared with FusedInferAttentionScoreV2, this API adds the queryRopeOptional, keyRopeOptional, and keyRopeAntiquantScaleOptional parameters.

    NOTE:

    KV Cache specific to the decode scenario: KV Cache is a common technique for optimizing inference performance in large models. During sampling, the Transformer model performs inference using the given prompt/context as the initial input (which can be processed in parallel), and then generates additional tokens one by one to further refine the generated sequence (reflecting the autoregressive nature of the model). During the sampling process, the Transformer performs self-attention operations, which requires extracting key-value (KV) vectors for each item in the current sequence (whether prompt/context or generated tokens). These vectors are stored in a matrix, commonly referred to as the KV cache.

  • Formula:

    Self-attention builds an attention model using the relationships within the input samples themselves. The principle is as follows: assume there is an input sample sequence $x$ of length $n$, where each element of $x$ is a $d$-dimensional vector. Each $d$-dimensional vector can be regarded as a token embedding. After passing such a sequence through three weight matrices, three matrices of dimension $n*d$ are obtained.

    The calculation formula of self-attention is generally defined as follows, where $Q$, $K$, and $V$ are important property elements of the input sample, obtained through spatial transformation of the input sample and can be unified into a feature space. "Attention" in the formula and operator name is an abbreviation of "self-attention".

    $$ Attention(Q,K,V)=Score(Q,K)V $$

    In this operator, the Score function uses the Softmax function, and the self-attention calculation formula is:

    $$ Attention(Q,K,V)=Softmax(\frac{QK^T}{\sqrt{d}})V $$

    Where: the product of $Q$ and $K^T$ represents the attention of the input $x$. To prevent this value from becoming too large, it is usually divided by the square root of $d$ for scaling, and each row is normalized by softmax. After multiplying with $V$, an $n*d$ matrix is obtained.

Function Prototype

The operator execution API is a two-phase API. Call the "aclnnFusedInferAttentionScoreV3GetWorkspaceSize" API first to obtain the input parameters and calculate the required workspace size based on the computation process, and then call the "aclnnFusedInferAttentionScoreV3" API to execute the computation.

aclnnStatus aclnnFusedInferAttentionScoreV3GetWorkspaceSize(
    const aclTensor     *query,
    const aclTensorList *key,
    const aclTensorList *value,
    const aclTensor     *pseShiftOptional,
    const aclTensor     *attenMaskOptional,
    const aclIntArray   *actualSeqLengthsOptional,
    const aclIntArray   *actualSeqLengthsKvOptional,
    const aclTensor     *deqScale1Optional,
    const aclTensor     *quantScale1Optional,
    const aclTensor     *deqScale2Optional,
    const aclTensor     *quantScale2Optional,
    const aclTensor     *quantOffset2Optional,
    const aclTensor     *antiquantScaleOptional,
    const aclTensor     *antiquantOffsetOptional,
    const aclTensor     *blockTableOptional,
    const aclTensor     *queryPaddingSizeOptional,
    const aclTensor     *kvPaddingSizeOptional,
    const aclTensor     *keyAntiquantScaleOptional,
    const aclTensor     *keyAntiquantOffsetOptional,
    const aclTensor     *valueAntiquantScaleOptional,
    const aclTensor     *valueAntiquantOffsetOptional,
    const aclTensor     *keySharedPrefixOptional,
    const aclTensor     *valueSharedPrefixOptional,
    const aclIntArray   *actualSharedPrefixLenOptional,
    const aclTensor     *queryRopeOptional,
    const aclTensor     *keyRopeOptional,
    const aclTensor     *keyRopeAntiquantScaleOptional,
    int64_t              numHeads,
    double               scaleValue,
    int64_t              preTokens,
    int64_t              nextTokens,
    char                *inputLayout,
    int64_t              numKeyValueHeads,
    int64_t              sparseMode,
    int64_t              innerPrecise,
    int64_t              blockSize,
    int64_t              antiquantMode,
    bool                 softmaxLseFlag,
    int64_t              keyAntiquantMode,
    int64_t              valueAntiquantMode,
    const aclTensor     *attentionOut,
    const aclTensor     *softmaxLse,
    uint64_t            *workspaceSize,
    aclOpExecutor       **executor)
aclnnStatus aclnnFusedInferAttentionScoreV3(
     void                *workspace,
     uint64_t             workspaceSize,
     aclOpExecutor       *executor,
     const aclrtStream    stream)

aclnnFusedInferAttentionScoreV3GetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    query Input Input Q in the formula. - FLOAT16, BFLOAT16, INT8, HIFLOAT8, FLOAT8_E4M3FN ND See the inputLayout parameter. ×
    key Input Input K in the formula. - FLOAT16, BFLOAT16, INT8, HIFLOAT8, FLOAT8_E4M3FN, INT4(INT32), FLOAT4_E2M1 ND See the inputLayout parameter. ×
    value Input Input V in the formula. - FLOAT16, BFLOAT16, INT8, HIFLOAT8, FLOAT8_E4M3FN, INT4(INT32), FLOAT4_E2M1 ND See parameter inputLayout ×
    pseShiftOptional Input Position encoding.
    • Empty tensors are not supported.
    • Pass nullptr if this function is not used.
    • For comprehensive constraints, see Constraints.
    FLOAT16, BFLOAT16 ND Recommended input shapes: (B,Q_N,Q_S,KV_S) and (1,Q_N,Q_S,KV_S) ×
    attenMaskOptional Input Mask matrix.
    • Empty tensors are not supported.
    • Pass in nullptr if this parameter is not used.
    • For comprehensive constraints, see Constraints.
    BOOL, INT8, UINT8 ND
    • When sparseMode is 2, 3, or 4, the shape of attenMaskOptional must be (2048,2048), (1,2048,2048), or (1,1,2048,2048).
    • When sparseMode is set to another value and Q_S is not 1, the recommended shapes are (Q_S,KV_S); (B,Q_S,KV_S); (1,Q_S,KV_S); (B,1,Q_S,KV_S); (1,1,Q_S,KV_S).
    • When sparseMode is set to another value and Q_S is 1, the recommended shapes are (B,KV_S); (B,1,KV_S); (B,1,1,KV_S).
    ×
    actualSeqLengthsOptional Input Effective sequence length of query in each batch.
    • Pass in nullptr if the sequence length is not specified.
    • For comprehensive constraints, see Constraints.
    INT64 - - -
    actualSeqLengthsKvOptional Input Effective sequence length of key/value in each batch.
    • Pass in nullptr if the sequence length is not specified.
    • For comprehensive constraints, see Constraints.
    INT64 - - -
    deqScale1Optional Input Dequantization factor after BMM1.
    • Empty tensors are not supported.
    • Supports per-tensor.
    • Pass in nullptr if this function is not used.
    • For comprehensive constraints, see Constraints.
    UINT64, FLOAT32 ND See Comprehensive Constraints on the Number of Input Parameters Related to int8 Quantization and Input and Output Data Formats. -
    quantScale1Optional Input Quantization factor before BMM2.
    • Empty tensors are not supported.
    • per-tensor is supported.
    • Set this parameter to nullptr if this function is not used.
    • For comprehensive constraints, see Constraints.
    FLOAT32 ND See Comprehensive Constraints on the Number of Input Parameters Related to int8 Quantization and Input and Output Data Formats. -
    deqScale2Optional Input Dequantization factor after BMM2.
    • Empty tensors are not supported.
    • per-tensor is supported.
    • nullptr can be passed in when this function is not used.
    • For the comprehensive constraints, see Constraints.
    UINT64, FLOAT32 ND See comprehensive restrictions on the number of input parameters related to int8 quantization and the data formats of inputs and outputs. -
    quantScale2Optional Input Quantization factor of the output.
    • Empty tensors are not supported.
    • Supports per-tensor and per-channel.
    • Pass nullptr if this function is not used.
    • For comprehensive constraints, see Constraints.
    FLOAT32, BFLOAT16 ND per-channel format: When the output layout is BSH, the product of all dimensions of quantScale2 must equal H; for other layouts, the product must equal N*D. (It is recommended to set the shape of quantScale2 to [1,1,H] or [H] when the output layout is BSH; to [1,N,1,D] or [N,D] when the output is BNSD; to [1,1,N,D] or [N,D] when the output is BSND; and to [1,N,D] or [N,D] when the output is TND). -
    quantOffset2Optional Input Quantization offset of the output.
    • Empty tensors are not supported.
    • per-tensor and per-channel are supported.
    • Pass nullptr if this feature is not used.
    • For comprehensive constraints, see Constraints.
    FLOAT32, BFLOAT16 ND Same as quantScale2Optional -
    antiquantScaleOptional Input Pseudo-quantization factor.
    • Empty tensors are not supported.
    • per-tensor, per-channel, and per-token are supported.
    • Pass nullptr if this feature is not used.
    • The KV pseudo-quantization parameter separate mode is recommended.
    • For comprehensive constraints, see Constraints.
    FLOAT16, BFLOAT16, FLOAT32 ND See antiquantScale and antiquantOffset pseudo-quantization constraints. -
    antiquantOffsetOptional Input Pseudo-quantization offset.
    • Empty tensors are not supported.
    • Supports per-tensor, per-channel, and per-token.
    • If used, the shape must be the same as that of antiquantScaleOptional.
    • Pass nullptr if this parameter is not used.
    • It is recommended to use the KV pseudo-quantization parameter separation mode.
    • For comprehensive constraints, see Constraints.
    FLOAT16, BFLOAT16, FLOAT32 ND Same as antiquantScaleOptional -
    blockTableOptional Input Block mapping table used for KV storage in PageAttention.
    • Empty tensors are not supported.
    • Pass nullptr when this function is not used.
    INT32 ND First dimension length must equal B, second dimension length cannot be less than maxBlockNumPerSeq (maxBlockNumPerSeq refers to the number of blocks corresponding to the maximum actualSeqLengthsKv among different batches). -
    queryPaddingSizeOptional Input Indicates whether the data of each batch in Query is right-aligned, and the number of right-aligned elements.
    • Empty tensors are not supported.
    • Only Q_S greater than 1 is supported. This parameter does not take effect in other use cases.
    • Pass nullptr when this function is not used.
    INT64 ND (1) -
    kvPaddingSizeOptional Input Indicates whether the data of each batch in key/value is right-aligned, and the number of right-aligned elements.
    • Empty tensors are not supported.
    • Pass nullptr when this function is not used.
    INT64 ND (1) -
    keyAntiquantScaleOptional Input Indicates the dequantization factor of key when separating kv pseudo-quantization parameters.
    • Empty tensors are not supported.
    • Pass nullptr when this function is not used.
    • For comprehensive constraints, see Constraints.
    FLOAT16, BFLOAT16, FLOAT32, FLOAT8_E8M0 ND See Constraints -
    keyAntiquantOffsetOptional Input Indicates the dequantization offset of key when separating the KV Pseudo-Quantization Parameter.
    • Empty tensors are not supported.
    • If used, the shape must be the same as that of keyAntiquantScaleOptional.
    • If this function is not used, pass in nullptr.
    • For comprehensive constraints, see Constraints.
    FLOAT16, BFLOAT16, FLOAT32 ND See Constraints -
    valueAntiquantScaleOptional Input When separating KV pseudo-quantization parameters, this parameter indicates the dequantization factor of value.
    • Empty tensors are not supported.
    • nullptr can be passed in when this function is not used.
    • For comprehensive constraints, see Constraints.
    FLOAT16, BFLOAT16, FLOAT32, FLOAT8_E8M0 ND See Constraints -
    valueAntiquantOffsetOptional Input When kv Pseudo-Quantization Parameters are separated, this represents the Dequantization factor of value.
    • Empty tensors are not supported.
    • When used, the shape must be the same as that of valueAntiquantScaleOptional.
    • Pass nullptr when this function is not used.
    • For Comprehensive Constraints, see Constraints.
    FLOAT16, BFLOAT16, FLOAT32 ND See Constraints. -
    keySharedPrefixOptional Input Parameters of the system prefix part of Key in the attention structure.
    • Empty tensors are not supported.
    • Pass in nullptr if this function is not used.
    • For comprehensive constraints, see Constraints.
    FLOAT16, BFLOAT16, INT8 ND
    • If input_layout is BSH, the shape is (1, prefix_S, H=KV_N*KV_D)
    • If input_layout is BSND, the shape is (1, prefix_S, KV_N, KV_D)
    • If input_layout is BNSD or BNSD_BSND, the shape is (1, KV_N, prefix_S, KV_D)
    ×
    valueSharedPrefixOptional Input Input of the system prefix part of Value in the attention structure.
    • Empty tensors are not supported.
    • Pass in nullptr if this function is not used.
    • For comprehensive constraints, see Constraints.
    FLOAT16, BFLOAT16, INT8 ND
    • When input_layout is BSH, the shape is (1, prefix_S, H=KV_N*KV_D).
    • When input_layout is BSND, the shape is (1, prefix_S, KV_N, KV_D).
    • When input_layout is BNSD or BNSD_BSND, the shape is (1, KV_N, prefix_S, KV_D).
    ×
    actualSharedPrefixLenOptional Input Valid Sequence Length of keySharedPrefix/valueSharedPrefix.
    • If this function is not used, pass in nullptr, indicating that the length is the same as the s length of keySharedPrefix/valueSharedPrefix.
    • The valid Sequence Length in this input parameter should not be greater than the Sequence Length in keySharedPrefix/valueSharedPrefix.
    • For comprehensive constraints, see Constraints.
    INT64 - (1) -
    queryRopeOptional Input Rope information of the query in the MLA structure.
    • Empty tensors are not supported.
    • For comprehensive constraints, see Constraints.
    FLOAT16, BFLOAT16 ND In the shape of queryRope, d is 64 and the remaining dimensions are the same as those of query. ×
    keyRopeOptional Input Rope information of the key in the MLA structure.
    • Empty tensors are not supported.
    • For comprehensive constraints, see Constraints.
    FLOAT16, BFLOAT16 ND In the shape of keyRope, d is 64, and the remaining dimensions are the same as those of key ×
    keyRopeAntiquantScaleOptional Input Dequantization factor of the rope information of the key in the MLA structure.
    • Empty tensors are not supported.
    • Reserved parameter. It does not take effect in the current version. Pass in nullptr.
    FLOAT16, BFLOAT16 - - -
    numHeads Input Number of heads in query. In BNSD, BSND, BNSD_BSND, and TND use cases, this value must be the same as the N-axis shape value of query in shape. Otherwise, an execution exception occurs. INT64 - - -
    scaleValue Input The reciprocal of the square root of d in the formula.
    • The data type must comply with the data type deduction rules of query.
    • When the user does not specifically specify it, 1.0 is recommended.
    DOUBLE - - -
    preTokens Input Used for sparse computation, indicating the number of preceding tokens that attention needs to correlate with.
    • If not specifically specified, it is recommended to pass in 2147483647.
    • This parameter does not take effect when Q_S is 1.
    INT64 - - -
    nextTokens Input Indicates that attention needs to be associated with the next several tokens for computation.
    • Recommended to pass in 2147483647 when not specifically specified.
    • This parameter is invalid when Q_S is 1.
    INT64 - - -
    inputLayout Input Indicates the data layout format of the input query, key, and value.
    • Currently, BSH, BSND, BNSD, BNSD_BSND (when the input is BNSD, the output format is BSND, and only Q_S greater than 1 is supported), BSH_NBSD, BSND_NBSD, BNSD_NBSD (when the output format is NBSD, only Q_S greater than 1 and less than or equal to 16 is supported), TND, TND_NTD, and NTD_TND are supported (for comprehensive constraints on TND-related use cases, see Constraints). If not specifically specified, it is recommended to pass in "BSH".
    STRING - - -
    numKeyValueHeads Input Number of heads in key and value.
    • If the user does not specifically specify, it is recommended to pass in 0, indicating that the number of heads in key/value is the same as that in query.
    INT64 - - -
    sparseMode Input Sparse mode. See Constraints for comprehensive constraints. INT64 - - -
    innerPrecise Input Indicates the selection between high precision and high performance. For comprehensive constraints, see Constraints. INT64 - - -
    blockSize Input Maximum number of tokens in each block of the KV storage in PageAttention. For Comprehensive Constraints, see Constraints. INT64 - - -
    antiquantMode Input Pseudo-quantization mode.
    • Passing in 0 indicates per-channel (per-channel includes per-tensor).
    • Passing in 1 indicates per-token.
    • If not specifically specified, it is recommended to pass in 0.
    • If Q_S is 1, passing in values other than 0 and 1 causes an exception. If Q_S is greater than or equal to 2, this parameter is invalid.
    • It is recommended to use the KV pseudo-quantization parameter separate module mode.
    INT64 - - -
    softmaxLseFlag Input Whether to output softmax_lse.
    • Supports external splitting of the S axis (output is added).
    • If the user does not specifically specify, it is recommended to pass in false.
    BOOL - - -
    keyAntiquantMode Input Pseudo-quantization mode of key.
    • It is recommended to pass in 0 when not specifically specified.
    • For comprehensive constraints, see Constraints.
    INT64 - - -
    valueAntiquantMode Input Pseudo-quantization mode of value.
    • Must be consistent with keyAntiquantMode, except for the scenario where keyAntiquantMode is 0 and valueAntiquantMode is 1.
    • If the user does not specifically specify it, 0 is recommended.
    • For comprehensive constraints, see Constraints.
    INT64 - - -
    attentionOut Output Output in the formula. - FLOAT16, BFLOAT16, INT8 ND The D dimension of this input parameter must be the same as that of value, and the remaining dimensions must be the same as the shape of the query input parameter. -
    softmaxLse Output Result of query multiplied by key in the ring attention algorithm. For comprehensive constraints, see Constraints. FLOAT32 ND When softmaxLseFlag is True, the shape must be [B, N, Q_S, 1] in general. When inputLayout is TND/NTD_TND, the shape must be [T, N, 1]. -
    workspaceSize Output Returns the workspace size that the user needs to allocate on the Device side. - - - - -
    executor Output Returns the op executor that contains the operator computation flow. - - - - -
  • Return Value

    aclnnStatus: return status code. For details, see aclnn Return Codes.

    The first-phase API performs input parameter validation and returns an error in the following scenarios:

    Return Value Error Code Description
    ACLNN_ERR_PARAM_NULLPTR 161001 The query, key, value, or attentionOut passed in is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data types and data formats of query, key, value, pseShift, attenMask, and attentionOut are out of the supported range.
    ACLNN_ERR_RUNTIME_ERROR 361001 The API fails to call the npu runtime interface for memory.

aclnnFusedInferAttentionScoreV3

  • Parameters

    Parameter Input/Output Description
    workspace Input Workspace memory address applied on the device side.
    workspaceSize Input Workspace size applied on the device side, obtained by calling the first-phase API aclnnFusedInferAttentionScoreV3GetWorkspaceSize.
    executor Input Op executor, which contains the operator computation flow.
    stream Input Specifies the stream for executing tasks.
  • Return Value

    Returns a status code of type aclnnStatus. For details, see aclnn return codes.

Constraints

  • Deterministic computation:

    • aclnnFusedInferAttentionScoreV3 defaults to a deterministic implementation.
  • When using this API with PyTorch, ensure that the versions of CANN-related packages and PyTorch-related packages are compatible.

  • Handling of null input parameters: The operator internally checks whether the query parameter is null. If it is null, the operator returns directly. If the query parameter is a non-null tensor and the key and value parameters are null tensors (i.e., S2 is 0), attentionOut is filled with all zeros. When attentionOut is a null tensor, the AscendCLNN framework handles it. For other input parameters marked as "nullable" in the parameter descriptions above, no processing is performed when they are null pointers.

  • The shapes of the corresponding tensors in parameters key and value must be identical. In non-continuous use cases, the batch in the tensorlist of key and value can only be 1, the number equals the B of query, and N and D must be equal. Due to the tensorlist limitation, B cannot be greater than 256 in non-continuous use cases.

  • When the data type of attenMask is INT8 or UINT8, the values in its tensor must be 0 or 1.

  • Comprehensive constraints on the number of INT8 quantization-related input parameters and the input/output data formats:

    • When both input and output are INT8: The input parameters deqScale1, quantScale1, deqScale2, and quantScale2 must all be present. quantOffset2 is optional and defaults to 0 if not passed.
    • Use case where the input is INT8 and the output is FLOAT16: input parameters deqScale1, quantScale1, and deqScale2 must be input at the same time. If input parameter quantOffset2 or quantScale2 exists (that is, not nullptr), an error is reported and returned.
    • Use case where the input is FLOAT16 or BFLOAT16 and the output is INT8: the quantScale2 parameter is required, and quantOffset2 is optional, defaulting to 0 if not passed. If the parameters deqScale1, quantScale1, or deqScale2 exist (that is, are not nullptr), an error is reported and returned.
    • The parameters quantScale2 and quantOffset2 support two formats (per-tensor/per-channel) and two data types (FLOAT32/BFLOAT16). If quantOffset2 is passed, its type and shape information must be consistent with those of quantScale2. When the input is BFLOAT16, both FLOAT32 and BFLOAT16 are supported; otherwise, only FLOAT32 is supported. In per-channel format, when the output layout is BSH, the product of all dimensions of quantScale2 must equal H; for other layouts, the product must equal N*D. (It is recommended to pass a quantScale2 shape of [1,1,H] or [H] when the output layout is BSH, [1,N,1,D] or [N,D] when the output is BNSD, and [1,1,N,D] or [N,D] when the output is BSND)
    • inputLayout supports only BSH, BNSD, BSND, and BNSD_BSND.
  • Constraints on pseudo-quantization parameters antiquantScale and antiquantOffset:

    • Only the pseudo-quantization scenario where kv_dtype is int8 is supported.
    • Per-channel mode: The shape of the two parameters can be (2, N, 1, D), (2, N, D), or (2, H), where N is numKeyValueHeads. The parameter data type is the same as the query data type, and antiquantMode is set to 0.
    • per-tensor mode: the shapes of the two parameters are both (2), the data type is the same as that of query, and antiquantMode is set to 0.
    • per-token mode: the shapes of the two parameters are both (2, B, S), the data type is fixed to FLOAT32, and antiquantMode is set to 1.
    • In asymmetric quantization mode, both antiquantScale and antiquantOffset parameters must be present.
    • In symmetric quantization mode, antiquantOffset can be null (i.e., nullptr). When the antiquantOffset parameter is null, symmetric quantization is performed; otherwise, asymmetric quantization is performed.
  • Comprehensive constraints for query, key, and value inputs in TND, TND_NTD, and NTD_TND scenarios:

    • actualSeqLengths and actualSeqLengthsKv must be passed, and the number of elements in these input parameters is used as the Batch value. The value of each element in these input parameters represents the sum of the sequence lengths of the current batch and all previous batches. Therefore, the value of a subsequent element must be greater than or equal to the value of the preceding element.
    • Atlas A2 products:
      • The sparse mode only supports sparse=0 with attenMask set to nullptr, or sparse=3 with attenMask not set to nullptr, or sparse=4 with attenMask not set to nullptr.
      • When d of query equals 512:
        • Supports TND and TND_NTD.
        • Page attention must be enabled. In this case, the length of actualSeqLengthsKv equals the batch value of key/value, representing the actual length of each batch, and the value is not greater than KV_S.
        • Supports s of 1 to 16 for each batch of query.
        • Requires n of query to be 32, 64, or 128, and n of key and value to be 1.
        • Requires queryRope and keyRope to be non-null, and d of queryRope and keyRope to be 64.
        • Not supported: left padding, tensorlist, pse, prefix, pseudo-quantization, full quantization, post-quantization.
        • In the NTD_TND scenario, enabling SoftMaxLse is not supported.
      • When d of query is not equal to 512:
        • When queryRope and keyRope are null: in the TND scenario, Q_D, K_D, and V_D must be equal and less than or equal to 256, or Q_D and K_D must be equal to 192 with V_D equal to 128 or 192; in the NTD_TND scenario, Q_D and K_D must be equal to 128 or 192, and V_D must be equal to 128. When queryRope and keyRope are not null, Q_D, K_D, and V_D must be equal to 128.
        • TND and NTD_TND are supported.
        • In TND scenarios, only FLOAT16 and BFLOAT16 data types are supported. In NTD_TND scenarios, only BFLOAT16 data type is supported.
        • In TND scenarios, when the head ratio is GQA/MQA (that is, the numHeads and numKeyValueHeads parameters must be fully passed, numHeads is an integer multiple of numKeyValueHeads, and the two are not equal), the following constraints apply:
          • When the data type is FLOAT16 or BFLOAT16, sparse=0 with attenMask set to nullptr is supported, or sparse=3 with an optimized attenMask is supported:
            • When Q_D, K_D, and V_D are equal and less than or equal to 256, or Q_D and K_D are equal to 192 and V_D is equal to 128 or 192, sparse=4 with an optimized attenMask is supported, requiring preTokens>=-actualSeqLengths, nextTokens>=-actualSeqLengthsKv, and preTokens+nextTokens>=0.
          • Only innerPrecise=0 is supported, that is, the high-precision mode without row invalidation.
          • Page attention is supported. The kv cache layout format supports BnBsH (blocknum, blocksize, H), where H is not greater than 65535, and blockSize supports values ≤ 128 and must be 16-aligned.
        • In the TND scenario, when the head ratio is MHA, the following constraints apply:
          • When the data type is FLOAT16 or BFLOAT16, sparse=0 with attenMask set to nullptr is supported, or sparse=3 or 4 with an optimized attenMask passed in is supported.
          • When the data type is FLOAT16, innerPrecise=0 and innerPrecise=1 are supported.
          • When the data type is BFLOAT16, only innerPrecise=0 is supported.
          • Page attention is supported. The kv cache layout format supports BnBsH (blocknum, blocksize, H), where H is not greater than 65535, and blockSize supports values ≤ 128 and must be 16-aligned.
        • In the NTD_TND scenario, page attention is not supported.
        • When sparse = 3, actualSeqLengths < actualSeqLengthsKv is required for each batch individually.
        • Left padding, tensorlist, pse, page attention, prefix, pseudo-quantization, full quantization, and post-quantization are not supported.
        • The number of elements in actualSeqLengths and actualSeqLengthsKv must not be greater than 4096.
    • Ascend 950PR&950DT products:
      • TND is supported;
      • Left padding, tensorlist, pseType=0, and prefix are not supported.
  • When queryRope and keyRope are input, it is an MLA scenario, and the parameter constraints are as follows:

    • The data type and data format of queryRope must be consistent with query.

    • The data type and data format of keyRope are consistent with those of key.

    • queryRope and keyRope must be configured together or not configured at all. Configuring only one of them is not supported.

    • When queryRope and keyRope are input, only the following features are supported:

      • dtype: FP16, BF16;
      • The d of query only supports 512/128;
      • Atlas A2 products:
        • When Q_S is greater than 1 (i.e., MTP), the actualSeqLengths parameter is supported only when inputLayout is TND, and is not supported for other layouts.
        • When d of query equals 512:
          • When queryRope is configured, the s of query must be 1-16, the d in the queryRope shape must be 64, and the remaining dimensions must be the same as those of query;
          • When keyRope is configured, n of key must be 1, d must be 512, d in the shape of keyRope must be 64, and the remaining dimensions must be consistent with key.
          • sparse: When Q_S equals 1, only sparse=0 is supported and attenMask must be nullptr. When Q_S is greater than 1, only sparse=3 is supported and mask must be passed.
          • key&value&keyRope supports ND and NZ input. When the input is in NZ format, the input format is [blockNum, N, D/16, blockSize, 16].
          • inputLayout: BSH, BSND, BNSD, BNSD_NBSD, BSND_NBSD, BSH_NBSD, TND, TND_NTD. NZ input does not support BNSD or BNSD_NBSD.
          • Page attention must be enabled: blockSize supports 16 and 128. NZ input does not support configuring 16.
          • Not supported: left padding, tensorlist, pse, prefix, pseudo-quantization, full quantization, post-quantization.
        • When d of query equals 128:
          • inputLayout: TND, NTD_TND;
          • When queryRope is configured, d in the shape of queryRope must be 64, and the remaining dimensions must be consistent with query;
          • When keyRope is configured, d in the shape of keyRope must be 64, and the remaining dimensions must be consistent with key;
          • Other constraints remain consistent with the comprehensive restrictions in TND and NTD_TND scenarios;
          • Left padding, tensorlist, pse, page attention, prefix, pseudo-quantization, full quantization, and post-training quantization are not supported.
      • Ascend 950PR&950DT products:
        • When d of query equals 512:
          • When queryRope is configured, s of query must be 1-16 and d must be 512. In the queryRope shape, b, n, and s must be the same as those of query, and d must be 64;
          • When keyRope is configured, n of key must be 1, d must be 512, and in the shape of keyRope, b, n, and s must be consistent with key, and d must be 64.
          • sparse: Supports sparse=0, sparse=3 with mask passed, and sparse=4 with mask passed.
          • key&value supports ND input.
          • inputLayout: BSH, BSND, BNSD, TND.
          • Supports actualSeqLengths and actualSeqLengthsKv parameters. When Q_S is greater than 1 (i.e., MTP) and the normal part of key&value reuses the same data, the actualSeqLengths parameter is supported only when inputLayout is TND, and is not supported for other layouts.
          • pse is not supported.
        • When d of query equals 128:
          • When queryRope is configured, b, n, and s in the shape of queryRope must be consistent with query, and d must be 64.
          • When keyRope is configured, b, n, and s in the shape of keyRope must be consistent with key, and d must be 64.
          • inputLayout: BSH, BSND, BNSD, BNSD_BSND, TND;
          • Page attention, prefix, pseudo-quantization, full quantization, and post-training quantization are not supported.
          • When kv is a tensorlist, b in the shape of keyRope must be consistent with the tensorlist length, n and s must be equal to n and s of each tensor in the tensorlist, and d is 64.
          • pse is not supported.
  • numKeyValueHeads usage constraints: numHeads must be divisible by numKeyValueHeads. In BSND, BNSD, BNSD_BSND, and TND scenarios, it must also be the same as the N-axis shape value of key/value in the shape; otherwise, an execution exception occurs.

    • Ascend 950PR&950DT products:
      • In pseudo quantization and full quantization use cases, the ratio of numHeads to numKeyValueHeads cannot exceed 64. In non-quantized MLA decode use cases, the ratio of numHeads to numKeyValueHeads is unlimited. In non-quantized and MLA prefill use cases, a ratio of numHeads to numKeyValueHeads greater than 64 is supported only when the D axis is 64 or 128; it is not supported for other D axis values.
  • The usage restrictions of sparseMode are as follows:

    sparseMode Mode Description
    0 defaultMask mode
    • If attenmask is not passed in: no mask operation is performed, and preTokens and nextTokens are ignored (internally set to INT_MAX);
    • If attenmask is passed in: the complete attenmask matrix (S1 * S2) must be passed in, indicating that the part between preTokens and nextTokens needs to be computed; preTokens > -actualSeqLengthsKv and preTokens + nextTokens >= 0 are required. In the prefix use case, the prefix length must be added to actualSeqLengthsKv.
    1 allMask mode The complete attenmask matrix (S1 * S2) must be passed in.
    2 leftUpCausal mode The optimized attenmask matrix (2048 * 2048) must be passed in.
    3 rightDownCausal mode Corresponds to the lower triangle use case with the right vertex as the partition point. The optimized attenmask matrix (2048 * 2048) must be passed in.
    4 band mode
    • The optimized attenmask matrix (2048 * 2048) must be passed in;
    • Ensure that preTokens > -actualSeqLengths, nextTokens > -actualSeqLengthsKv, and preTokens + nextTokens >= 0. In the prefix use case, the prefix length must be added to actualSeqLengthsKv.
    5 prefix mode This mode is not supported currently. If the user does not specifically specify this parameter, it is recommended to pass in 0.
    6 global mode This mode is currently not supported. Passing 0 is recommended when the user does not specifically specify this mode.
    7 dilated mode This mode is currently not supported. Passing 0 is recommended when the user does not specifically specify this mode.
    8 block_local mode This mode is currently not supported. Passing 0 is recommended when the user does not specifically specify this mode.
    Special constraints: When Q_S is 1 and no rope input is provided, the sparseMode parameter does not take effect.
  • The usage restrictions of innerPrecise are as follows:

    • There are four modes: 0, 1, 2, and 3. They occupy two bits in total. Bit 0 indicates the selection between high precision and high performance, and bit 1 indicates whether row invalidation correction is performed.

      innerPrecise Mode invalid row correction
      0 High precision ×
      1 High performance ×
      2 High precision √
      3 High performance √
    • NOTE: BFLOAT16 and INT8 do not distinguish between high precision and high performance. The invalid row correction takes effect for FLOAT16, BFLOAT16, and INT8. Currently, 0 and 1 are reserved configuration values. If the mask part involved in computation contains an entire row of all 1s, precision loss may occur. In this case, you can set this parameter to 2 or 3 to enable the invalid row function to improve precision, but this setting degrades performance. If the operator can detect invalid rows, the invalid row computation is automatically enabled, for example, when sparse_mode is 3 and Sq > Skv.

  • keyAntiquantMode usage restrictions are as follows:

    keyAntiquantMode Description
    0 Represents per-channel mode (per-channel includes per-tensor).
    1 Represents per-token mode.
    2 Represents per-tensor combined with per-head mode.
    3 Represents per-token combined with per-head mode.
    4 Represents per-token combined with the mode where scale/offset is managed in page attention mode.
    5 Represents per-token overlaid with per-head, where scale/offset is managed using the page attention mode.
    6 Represents the per-token-group mode.
    Special constraint: It must be consistent with valueAntiquantMode, except for the scenario where keyAntiquantMode is 0 and valueAntiquantMode is 1.
    • Atlas A2 products: If Q_S is 1, passing a value other than 0, 1, 2, 3, 4, and 5 will cause an exception. If Q_S is greater than or equal to 2, only 0 and 1 are supported; other values will cause an exception.
    • Ascend 950PR&950DT products: Passing a value other than 0, 1, 2, 3, 4, 5, and 6 will cause an exception.
  • The usage restrictions of valueAntiquantMode are as follows:

    • Except for the use case where keyAntiquantMode is 0 and valueAntiquantMode is 1, this parameter must be the same as keyAntiquantMode.
    • Atlas A2 products: If Q_S is 1, passing a value other than 0, 1, 2, 3, 4, and 5 will cause an exception. If Q_S is greater than or equal to 2, only 0 and 1 are supported; other values will cause an exception.
    • Ascend 950PR&950DT products: Passing a value other than 0, 1, 2, 3, 4, 5, and 6 will cause an exception.
  • softmaxLse usage constraints are as follows:

    • In the ring attention algorithm, max is taken over the result of query multiplied by key to obtain softmax_max. Then exp is applied to the result of query multiplied by key minus softmax_max, and the sum is computed to obtain softmax_sum. Finally, log is taken of softmax_sum, and softmax_max is added to obtain the result.
    • When softmaxLseFlag is True, the shape must be [B,N,Q_S,1] in general. When inputLayout is TND/NTD_TND, the shape must be [T,N,1].
    • When softmaxLseFlag is False, if the softmaxLse tensor passed in is not empty, the tensor data is directly returned. If nullptr is passed for softmaxLse, a tensor of shape {1} with all zeros is returned.
  • When Q_S is greater than 1:

    • The function usage restrictions for query, key, and value inputs are as follows:

      • B-axis restrictions

        • Supports B-axis less than or equal to 65536.
        • In the non-contiguous use case, the batch in the tensorlists of key and value must be 1, the number of tensors must equal B of query, and N and D must be the same. Due to the tensorlist limitation, B cannot be greater than 256 in the non-contiguous use case.
        • Atlas A2 products:
          • If the input type is INT8 and the D-axis is not 32-byte-aligned, the maximum supported value of the B-axis is 128. If the input type is FLOAT16 or BFLOAT16 and the D-axis is not 16-byte-aligned, the B-axis is also only supported up to 128.
      • N-axis restrictions

        • Ascend 950PR&950DT products:
          • In GQA non-quantization scenarios, the N axis supports values greater than 256. In pseudo-quantization and full-quantization scenarios, the N axis must be less than or equal to 256.
        • Atlas A2 products: Supports N axis values less than or equal to 256.
      • The D axis supports values less than or equal to 512. When inputLayout is BSH or BSND, it is recommended that N*D be less than 65535.

      • S supports values less than or equal to 20971520 (20M). In some long sequence scenarios, if the computation workload is too large, the operator execution may time out (aicore error, with errorStr: timeout or trap error). In such scenarios, S splitting is recommended. Note: The computation workload here is affected by B, S, N, D, etc. Larger values result in greater computation workload. Typical long sequence scenarios that may cause timeouts (i.e., where the product of B, S, N, and D is large) include but are not limited to:

        B Q_N Q_S D KV_N KV_S
        1 20 2097152 256 1 2097152
        1 2 20971520 256 2 20971520
        20 1 2097152 256 1 2097152
        1 10 2097152 512 1 2097152
      • D-axis limits: Atlas A2 products:
        • When the type of query, key, value, or attentionOut includes INT8, the D-axis must be 32-aligned. When the type of query, key, value, or attentionOut includes INT4, the D-axis must be 64-aligned. When all types are FLOAT16 or BFLOAT16, the D-axis must be 16-aligned.
      • Ascend 950PR&950DT products:

        • Non-quantization scenario: The types of query, key, and value are all FLOAT16 or BFLOAT16, and D-axis 1–512 is fully supported.
        • Full-quantization scenario: The types of query, key, and value are all INT8, and D-axis 1–512 is fully supported.
        • Pseudo-quantization scenario: The type of query is FLOAT16 or BFLOAT16, and the types of key and value are INT8/HIFLOAT8/FLOAT8_E4M3FN/FLOAT4_E2M1/INT4(INT32). When the types of key and value are FLOAT4_E2M1/INT4(INT32), the D-axis of query and the D-axis of key and value only support 64-aligned (under INT32, the D-axis of key and value is 8-aligned).
    • The actualSeqLengths input parameter must be a non-negative number when passed.

      • Atlas A2 products: In this input parameter, the valid Sequence Length of each batch should not be greater than the Sequence Length of the corresponding batch in query. When the length of seqlen passed in is 1, each Batch uses the same seqlen; when the length passed in is greater than or equal to Batch, the first Batch values of seqlen are used. Other lengths are not supported. This constraint applies when the inputLayout of query is TND/NTD_TND.
      • Ascend 950PR&950DT products: The meaning of this input parameter and its check conditions vary with inputLayout: When inputLayout is not TND, this input parameter is optional. Its length is 1 or greater than or equal to the batch value of query, the values in this input parameter represent the actual length of each batch, and the values should not be greater than Q_S. When inputLayout is TND, this input parameter must be passed in. The b-th value indicates the cumulative S-axis length of the first b batches, the values must be arranged in ascending order (greater than or equal to the previous value), and the length of this input parameter represents the total number of batches.
    • The actualSeqLengthsKv input parameter should be non-negative when passed.

      • Atlas A2 products: In this input parameter, the valid Sequence Length of each batch should not be greater than the Sequence Length of the corresponding batch in key/value. When the length of seqlenKv passed in is 1, each Batch uses the same seqlenKv; when the length passed in is greater than or equal to Batch, the first Batch values of seqlenKv are used. Other lengths are not supported. This constraint applies when the inputLayout of key/value is TND/NTD_TND.
      • Ascend 950PR&950DT products: The meaning of this input parameter and its check conditions vary with inputLayout: When inputLayout is not TND, this input parameter is optional. Its length is 1 or greater than or equal to the batch value of key/value, the values in this input parameter represent the actual length of each batch, and the values should not be greater than KV_S. When inputLayout is TND, this input parameter must be passed in. In non-PA use cases, the b-th value indicates the cumulative S-axis length of the first b batches, the values must be arranged in ascending order (greater than or equal to the previous value), and the length of this input parameter represents the total number of batches. In PA use cases, its length equals the batch value of key/value, representing the actual length of each batch, with the value not greater than KV_S.
    • The sparseMode parameter currently only supports values of 0, 1, 2, 3, and 4. An error is reported when other values are used.

      • When sparseMode = 0, if attenMask is a null pointer or attenMask is passed in a left-padding scenario, the input parameters preTokens and nextTokens are ignored.
      • When sparseMode = 2, 3, or 4, the shape of attenMask must be S,S or 1,S,S or 1,1,S,S, where the value of S must be fixed to 2048, and the user must ensure that the passed attenMask is lower triangular. If attenMask is nullptr or the passed shape is incorrect, an error is reported.
      • In scenarios where sparseMode = 1, 2, or 3, the input parameters preTokens and nextTokens are ignored and assigned values according to relevant rules.
    • The composite parameter scenario for kvCache dequantization supports only the case where query is FLOAT16, dequantizing key and value of INT8 type to FLOAT16. The product range of the datarange of the input parameter key/value and the datarange of the input parameter antiQuantScale falls within (-1, 1). The high-performance mode can guarantee precision; otherwise, the high-precision mode must be enabled to guarantee precision.

    • Page attention scenario:

      • Enabling page attention requires blockTable to exist and be valid, and key and value to be laid out in a contiguous memory region based on the indexes in blockTable. In this use case, the inputLayout parameter of key and value does not take effect. blockTable is filled with blockids. The validity of blockid is not verified currently and must be ensured by users.

      • blockSize is a user-defined parameter whose value affects the performance of page attention. When page attention is enabled, blockSize ranges from 128 to 512 and must be a multiple of 128. Generally, page attention improves throughput but degrades performance.

      • In page attention use cases, if the input kv cache layout is BnBsH (blocknum, blocksize, H) and KV_N * D exceeds 65535, an error is reported due to hardware instruction constraints. To resolve this issue, enable GQA (to reduce KV_N) or change the kv cache layout to BnNBsD (blocknum, KV_N, blocksize, D). If the inputLayout of query is BNSD or TND, the kv cache layout supports two formats: BnBsH and BnNBsD. If the inputLayout of query is BSH or BSND, the kv cache layout supports only the BnBsH format. blocknum cannot be less than the sum of the number of blocks per batch calculated based on actualSeqLengthsKv and blockSize. In addition, the shapes of key and value must be consistent.

      • page attention pseudo quantization use case

        • Atlas A2 products: Supports FLOAT16/BFLOAT16 for query, and INT8 for key and value.
        • Ascend 950PR&950DT products: Supports key and value of dtype FLOAT16/BFLOAT16/INT8/HIFLOAT8/FLOAT8_E4M3FN/FLOAT4_E2M1/INT4(INT32).
      • page attention full quantization use case

        • Atlas A2 products: Does not support query of dtype INT8.
        • Ascend 950PR&950DT products: Supports query of dtype INT8.
      • Page attention does not support the tensorlist scenario or the left-padding scenario.

      • In the page attention scenario, actualSeqLengthsKv must be passed.

      • In page attention scenarios, blockTable must be two-dimensional. The first dimension length must equal B, and the second dimension length cannot be less than maxBlockNumPerSeq (maxBlockNumPerSeq is the number of blocks corresponding to the maximum actualSeqLengthsKv across different batches).

      • When page attention is enabled, KV_S of the input must be greater than or equal to maxBlockNumPerSeq * blockSize in the following use cases:

        • When attenMask is passed in, for example, the mask shape is (B, 1, Q_S, KV_S)
        • When pseShift is passed in, for example, the pseShift shape is (B, N, Q_S, KV_S)
    • Query left-padding scenario:

      • The formula for the query copy start position is: Q_S - queryPaddingSize - actualSeqLengths. The formula for the query copy end position is: Q_S - queryPaddingSize. The query copy start position must not be less than 0, and the end position must not be greater than Q_S. Otherwise, the result will not meet expectations.
      • If kvPaddingSize is less than 0, it is set to 0.
      • Must be used together with the actualSeqLengths parameter. Otherwise, the query right-padding use case is used by default.
      • PageAttention is not supported and cannot be used together with the blocktable parameter.
      • Atlas A2 products: Does not support the use case where Q is BF16/FP16 and KV is INT4.
    • kv left-padding scenario:

      • The copy start point of key and value is calculated as: KV_S - kvPaddingSize - actualSeqLengthsKv. The copy end point of key and value is calculated as: KV_S - kvPaddingSize. The copy start point of key and value must not be less than 0, and the end point must not be greater than KV_S. Otherwise, the result will not meet expectations.
      • If kvPaddingSize is less than 0, it is set to 0.
      • It must be enabled together with the actualSeqLengthsKv parameter. Otherwise, the KV right padding use case is used by default.
      • PageAttention is not supported and cannot be used together with the blocktable parameter.
      • Atlas A2 products: Does not support the use case where Q is BF16/FP16 and KV is INT4.
    • When the output is int8 and quantScale2 and quantOffset2 are per-channel, left padding, Ring Attention, or the use case where D is not 32-byte aligned is currently not supported.

    • When the output is INT8, the scenario where sparse is band and preTokens/nextTokens are negative is not supported.

    • When the output is INT8, if a non-null pointer and a non-empty tensor are passed for quantOffset2, and sparseMode, preTokens, and nextTokens meet the following conditions, some rows of the matrix do not participate in the computation, resulting in calculation errors. In this use case, the call is intercepted (Solution: if this use case is not to be intercepted, perform post quantization outside the FIA API instead of enabling it inside the FIA API):

      • sparseMode = 0: if attenMask is a non-null pointer and actualSeqLengths - actualSeqLengthsKV - actualSharedPrefixLen - preTokens > 0 or nextTokens < 0 for each batch, the interception condition is met
      • sparseMode = 1 or 2: the interception condition is never met
      • sparseMode = 3, if each batch satisfies actualSeqLengthsKV + actualSharedPrefixLen - actualSeqLengths < 0, the interception condition is met.
      • sparseMode = 4: If preTokens < 0 or nextTokens + actualSeqLengthsKV + actualSharedPrefixLen - actualSeqLengths of each batch < 0, the interception condition is met.
    • pseShift function usage restrictions as follows:

      • The pseShift function is supported when the query data type is FLOAT16, BFLOAT16, or INT8.
      • When the query data type is FLOAT16 and pseShift is present, the high-precision mode is forcibly used, and the corresponding restrictions are inherited from those of the high-precision mode.
      • Q_S must be greater than or equal to the S length of query, and KV_S must be greater than or equal to the S length of key. In the prefix scenario, KV_S must be greater than or equal to the sum of actualSharedPrefixLen and the S length of key.
      • Ascend 950PR&950DT products: Non-quantization and full quantization use cases: no alignment restriction.
    • Prefix-related parameter constraints:

      • keySharedPrefix and valueSharedPrefix must either both be null or both be non-null.
      • When both keySharedPrefix and valueSharedPrefix are non-null, keySharedPrefix, valueSharedPrefix, key, and value must have the same number of dimensions and keep their dtypes consistent.
      • When both keySharedPrefix and valueSharedPrefix are non-null, the first dimension (batch) of keySharedPrefix must be 1; in BNSD and BSND layouts, the N and D axes must be consistent with key; in BSH layout, H must be consistent with key. The same applies to valueSharedPrefix. S of keySharedPrefix and valueSharedPrefix must be equal.
      • When actualSharedPrefixLen is present, its shape must be [1], and its value cannot exceed S of keySharedPrefix and valueSharedPrefix.
      • The sum of S of the shared prefix and S of key or value must satisfy the original S constraints of key or value.
      • prefix does not support PageAttention scenarios, left padding scenarios, or tensorlist scenarios.
      • In prefix scenarios, when sparse is 0 or 1, if attenmask is passed, S2 must be greater than or equal to the sum of actualSharedPrefixLen and the S length of key.
      • In prefix scenarios, the case where all qkv inputs are int8 is not supported.
    • KV pseudo-quantization parameter separation:

      • Atlas A2 products:
        • keyAntiquantMode and valueAntiquantMode must be the same.
        • keyAntiquantScale and valueAntiquantScale are either both null or both non-null; keyAntiquantOffset and valueAntiquantOffset are either both null or both non-null
        • If keyAntiquantScale and valueAntiquantScale are both non-empty, their shapes must be the same; if keyAntiquantOffset and valueAntiquantOffset are both non-empty, their shapes must be the same.
        • Only per-token and per-channel modes are supported. In per-token mode, the shapes of both parameters must be (B, S) and the data type is fixed to FLOAT32. In per-channel mode, the shapes of both parameters must be (N, D), (N, 1, D) or (H) and the data type is fixed to BF16.
        • When both fake-quantization and KV separation quantization parameters are passed, the KV separation quantization parameters are used.
        • In scenarios where keyAntiquantScale and valueAntiquantScale are non-null, the s of query must be less than or equal to 16.
        • In scenarios where keyAntiquantScale and valueAntiquantScale are non-null, the dtype of query must be BFLOAT16, the dtype of key and value must be INT8, and the dtype of the output must be BFLOAT16.
        • In scenarios where keyAntiquantScale and valueAntiquantScale are non-null, the tensorlist, left padding, and page attention features are not supported.
      • Ascend 950PR&950DT products:
        • Except for the use case where keyAntiquantMode is 0 and valueAntiquantMode is 1, keyAntiquantMode and valueAntiquantMode must be the same
        • keyAntiquantScale and valueAntiquantScale are either both null or both non-null; keyAntiquantOffset and valueAntiquantOffset are either both null or both non-null
        • When keyAntiquantScale and valueAntiquantScale are both non-null, their shapes must be the same, except for the use case where keyAntiquantMode is 0 and valueAntiquantMode is 1; when keyAntiquantOffset and valueAntiquantOffset are both non-null, their shapes must be the same, except for the use case where keyAntiquantMode is 0 and valueAntiquantMode is 1
        • Nine modes are supported: per-channel, per-tensor, per-token, per-tensor stacked with per-head, per-token stacked with per-head, per-token using page attention mode to manage scale/offset, per-token stacked with per-head using page attention mode to manage scale/offset, key supporting per-channel stacked with value supporting per-token, and per-token-group. In the following, N denotes numKeyValueHeads.
        • per-channel mode: The shapes of the two parameters can be (1, N, 1, D), (1, N, D), (1, H), (N, 1, D), (N, D), or (H). The parameter data type is the same as the query data type. This mode is supported when the key and value data types are INT8, INT4 (INT32), HIFLOAT8, or FLOAT8_E4M3FN. When the key and value data types are HIFLOAT8 or FLOAT8_E4M3FN, antiQuantOffset is not supported.
        • per-tensor mode: The shape of both parameters is (1). The data type is the same as the query data type. This mode is supported when the key and value data types are INT8.
        • per-token mode: The shapes of the two parameters can be (1, B, S) or (B, S), and the data type is fixed to FLOAT32. This mode is supported when the key and value data types are INT8 or INT4(INT32).
        • per-tensor plus per-head mode: The shapes of the two parameters are both (N), with the data type the same as the query data type. Supported when the key and value data types are INT8.
        • key per-channel plus value per-token mode: For key per-channel, the shapes of the two parameters can be (1, N, 1, D), (1, N, D), (1, H), (N, 1, D), (N, D), or (H), and the parameter data type is the same as the query data type. For value per-token, the shapes of the two parameters are both (1, B, S) and the data type is fixed to FLOAT32. Supported when the key and value data types are INT8 or INT4 (INT32). When the key and value data types are INT8, the query and output support only FLOAT16.
        • per-token-group mode: the shape of antiquantScale is (1, B, N, S, D/32), the data type is fixed to FLOAT8_E8M0, and antiquantOffset is not supported. It is supported when the data types of key and value are FLOAT4_E2M1.
        • per-token combined with per-head mode: The shapes of both parameters are (B, N, S), and the data type is fixed to FLOAT32. Supported when the key and value data types are INT8 or INT4(INT32).
        • per-token mode using page attention to manage scale/offset mode: The shapes of both parameters are (blocknum, blocksize), and the data type is fixed to FLOAT32. Supported when the key and value data types are INT8.
        • per-token superimposed with per-head mode using page attention to manage scale/offset mode: The shape of both parameters is (blocknum, N, blocksize), the data type is fixed to FLOAT32, and it is supported when the data type of key and value is INT8.
        • When both fake-quantization and KV separation quantization parameters are passed, the KV separation quantization parameters are used.
        • The INT4 (INT32) pseudo-quantization scenario supports only KV pseudo-quantization parameter separation, specifically including:
          • per-channel mode;
          • per-token mode;
          • per-token plus per-head mode;
          • key supports per-channel plus value supports per-token mode.
        • Some pseudo-quantization scenarios do not support post-quantization
          • Atlas A2 products: Does not support post-quantization in the INT4(INT32) pseudo-quantization use case.
          • Ascend 950PR&950DT products: Post-quantization is not supported in the INT4(INT32) and FLOAT4_E2M1 pseudo-quantization use cases.
  • When Q_S equals 1:

    • The function usage restrictions for query, key, and value inputs are as follows:

      • The B-axis must be less than or equal to 65536, and the D-axis must be less than or equal to 512.

      • N-axis restrictions

        • Ascend 950PR&950DT products:
          • In GQA non-quantization scenarios, the N axis supports values greater than 256. In pseudo-quantization and full-quantization scenarios, the N axis must be less than or equal to 256.
        • Atlas A2 products: The N axis must be less than or equal to 256.
      • The scenario where query, key, and value input types are all INT8 is not supported yet.

      • In INT4 (INT32) pseudo-quantization scenarios, the aclnn single-operator call supports KV INT4 input or INT4 concatenated into INT32 input. (It is recommended to use dynamicQuant to generate data in INT4 format, because dynamicQuant treats one INT32 as containing eight INT4 values.)

      • In the INT4 (INT32) pseudo-quantization scenario, if KV INT4 is packed into INT32 input, then N, D, or H of KV is one-eighth of the actual value (same for prefix).

      • key and value have restrictions on the D axis under specific data types.

        • Atlas A2 products: When the input data types of key and value are INT4(INT32), the D axis must be 64-aligned (INT32 supports only 8-alignment of D).
        • Ascend 950PR&950DT products: When the input data types of key and value are FLOAT4_E2M1/INT4(INT32), the D axis of query and the D axes of key and value must be 64-aligned (INT32 supports only 8-alignment of the D axis of key and value).
    • The actualSeqLengths input parameter must be a non-negative number when passed.

      • Atlas A2 products: When the inputLayout of query is not TND, this parameter does not take effect when Q_S is 1. When the inputLayout of query is TND/TND_NTD.
      • Ascend 950PR&950DT products: The meaning and interception conditions vary depending on inputLayout: When inputLayout is not TND, this input parameter is optional. Its length is 1 or greater than or equal to the batch value of query. The values in this input parameter represent the actual length of each batch, and the values should not be greater than Q_S. When inputLayout is TND, this input parameter must be passed in. The b-th value indicates the cumulative S-axis length of the first b batches, the values should be arranged in ascending order (greater than or equal to the previous value), and the length of this input parameter represents the total number of batches.
    • The actualSeqLengthsKv input parameter should be non-negative when passed.

      • Atlas A2 products: The valid Sequence Length of each batch in this input parameter should not be greater than the Sequence Length of the corresponding batch in key/value. When the length of seqlenKv passed in is 1, each Batch uses the same seqlenKv; when the length passed in is greater than or equal to Batch, the first Batch values of seqlenKv are used. Other lengths are not supported. When the inputLayout of key/value is TND/TND_NTD.
      • Ascend 950PR&950DT products: The meaning and interception conditions vary depending on inputLayout: When inputLayout is not TND, this input parameter is optional. Its length is 1 or greater than or equal to the batch value of key/value. The values in this input parameter represent the actual length of each batch, and the values should not be greater than KV_S. When inputLayout is TND, this input parameter must be passed in. In non-PA use cases, the b-th value indicates the cumulative S-axis length of the first b batches, the values should be arranged in ascending order (greater than or equal to the previous value), and the length of this input parameter represents the total number of batches. In PA use cases, its length equals the batch value of key/value, represents the actual length of each batch, and the value is not greater than KV_S.
    • Page attention scenario:

      • Page attention can be enabled only when blocktable exists and is valid, and key and value are arranged in a contiguous memory block according to the indexes in blocktable. In this use case, the inputLayout parameters of key and value are invalid.

        • Atlas A2 products: key and value support the FLOAT16/BFLOAT16/INT8 dtype.
        • Ascend 950PR&950DT products: key and value support the FLOAT16/BFLOAT16/INT8/HIFLOAT8/FLOAT8_E4M3FN/FLOAT4_E2M1/INT4(INT32) data type.
      • blockSize is a user-defined parameter whose value affects the performance of page attention. When page attention is enabled, a non-zero value must be passed to blockSize, and the maximum value of blockSize is 512. Generally, page attention improves throughput but causes performance degradation.

        • Atlas A2 products: alignment to 16 is required when key and value are of the FLOAT16/BFLOAT16 input type; alignment to 32 is required when key and value are of the INT8 input type, and 128 is recommended.
        • Ascend 950PR&950DT products: alignment to 16 is required when key and value are of the FLOAT16/BFLOAT16 input type; alignment to 32 is required when key and value are of the INT8/HIFLOAT8/FLOAT8_E4M3FN input type; alignment to 64 is required when key and value are of the FLOAT4_E2M1/INT4(INT32) input type.
      • In the page attention scenario, when the inputLayout of query is BNSD or TND, the KV cache layout supports two formats: BnBsH (blocknum, blocksize, H) and BnNBsD (blocknum, KV_N, blocksize, D). When the inputLayout of query is BSH or BSND, the KV cache layout supports only the BnBsH format. blocknum cannot be less than the sum of the number of blocks for each batch calculated based on actualSeqLengthsKv and blockSize. In addition, the shapes of key and value must be consistent.

      • In the page attention scenario, the performance of the KV cache layout BnNBsD is generally better than that of BnBsH. It is recommended to prefer the BnNBsD format.

      • When page attention is enabled, if the input kv cache layout is BnBsH and numKvHeads * headDim exceeds 64k, the operation is intercepted and an error is reported due to hardware instruction constraints. To resolve this issue, enable GQA (reduce numKvHeads) or change the kv cache layout to BnNBsD.

      • Page attention does not support the tensorlist scenario or the left-padding scenario.

        • Atlas A2 products: the use case where Q is BF16/FP16 and KV is INT4(INT32) is not supported.
        • Ascend 950PR&950DT products: the use case where Q is BF16/FP16 and KV is INT4(INT32) is supported.
      • In the page attention scenario, actualSeqLengthsKv must be passed.

      • In the page attention scenario, blockTable must be two-dimensional, with the first dimension length equal to B and the second dimension length not less than maxBlockNumPerSeq (maxBlockNumPerSeq is the number of blocks corresponding to the maximum actualSeqLengthsKv in each batch).

      • When page attention is enabled, input S must be greater than or equal to the second dimension of blockTable multiplied by blockSize in the following use cases.

        • Attention mask is enabled, for example, the mask shape is (B, 1, 1, S).
        • pseShift is enabled, for example, with a shape of (B, N, 1, S).
        • The pseudo quantization per-token mode is enabled: the shapes of the input parameters antiquantScale and antiquantOffset are both (2, B, S).
        • The per-token plus per-head mode is enabled: the shapes of the two parameters are both (B, N, S), the data type is fixed to FLOAT32, and this mode is supported when the data types of key and value are INT8 and INT4(INT32).
        • The per-token-group mode is enabled: the shape of antiquantScale is (1, B, N, S, D/32),, the data type is fixed to FLOAT8_E8M0, and antiquantOffset is not supported. It is supported when the data types of key and value are FLOAT4_E2M1.
    • kv left-padding scenario:

      • Atlas A2 products: the use case where Q is BF16/FP16 and KV is INT4(INT32) is not supported.
      • Ascend 950PR&950DT products: the use case where Q is BF16/FP16 and KV is INT4(INT32) is supported, and there is no restriction on the QKV data type.
      • The formula for the kvCache copy start point is: KV_S - kvPaddingSize - actualSeqLengthsKv. The formula for the kvCache copy end point is: KV_S - kvPaddingSize. If the kvCache copy start point or end point is less than 0, the returned data result is all zeros.
      • If kvPaddingSize is less than 0, it is set to 0.
      • It must be enabled together with the actualSeqLengthsKv parameter. Otherwise, the KV right padding use case is used by default.
      • PageAttention and tensorlist are not supported; otherwise, the default is the kv right-padding scenario.
      • When it is enabled together with the attenMask parameter, ensure that attenMask is correctly defined, that is, it can correctly hide invalid data. Otherwise, precision issues are introduced.
    • pseShift function usage restrictions as follows:

      • The data type of pseShift must be consistent with that of query.
    • KV pseudo-quantization parameter separation:

      • Except for the use case where keyAntiquantMode is 0 and valueAntiquantMode is 1, keyAntiquantMode and valueAntiquantMode must be the same
      • keyAntiquantScale and valueAntiquantScale are either both null or both non-null; keyAntiquantOffset and valueAntiquantOffset are either both null or both non-null
      • When keyAntiquantScale and valueAntiquantScale are both non-null, their shapes must be the same, except for the use case where keyAntiquantMode is 0 and valueAntiquantMode is 1; when keyAntiquantOffset and valueAntiquantOffset are both non-null, their shapes must be the same, except for the use case where keyAntiquantMode is 0 and valueAntiquantMode is 1
      • Atlas A2 products: eight modes are supported: per-channel, per-tensor, per-token, per-tensor combined with per-head, per-token combined with per-head, per-token using the page attention mode to manage scale/offset, per-token combined with per-head and using the page attention mode to manage scale/offset, and per-channel for key combined with per-token for value. In the following, N refers to numKeyValueHeads.
        • Per-channel mode: The shapes of the two parameters can be (1, N, 1, D), (1, N, D), or (1, H). The parameter data type is the same as the query data type. Supported when the key and value data types are INT8 or INT4 (INT32).
        • Per-tensor mode: The shapes of both parameters are (1). The data type is the same as the query data type. Supported when the key and value data types are INT8.
        • Per-token mode: The shapes of both parameters are (1, B, S). The data type is fixed to FLOAT32. Supported when the key and value data types are INT8 or INT4 (INT32).
        • Per-tensor plus per-head mode: The shapes of both parameters are (N). The data type is the same as the query data type. Supported when the key and value data types are INT8.
        • key per-channel and value per-token mode: For key per-channel, the shapes of the two parameters can be (1, N, 1, D), (1, N, D), or (1, H), and the parameter data type is the same as the query data type. For value per-token, the shapes of the two parameters are both (1, B, S) and the data type is fixed to FLOAT32. This mode is supported when the key and value data types are INT8 or INT4(INT32). When the key and value data types are INT8, only FLOAT16 is supported for the query and attentionOut data types.
      • Ascend 950PR&950DT products: nine modes are supported: per-channel, per-tensor, per-token, per-tensor combined with per-head, per-token combined with per-head, per-token using the page attention mode to manage scale/offset, per-token combined with per-head and using the page attention mode to manage scale/offset, and per-channel for key combined with per-token and per-token-group for value. In the following, N refers to numKeyValueHeads.
        • per-channel mode: The shapes of the two parameters can be (1, N, 1, D), (1, N, D), or (1, H). The parameter data type is the same as the query data type. This mode is supported when the key and value data types are INT8, INT4(INT32), HIFLOAT8, or FLOAT8_E4M3FN. When the key and value data types are HIFLOAT8 or FLOAT8_E4M3FN, carrying antiQuantOffset is not supported.
        • per-tensor mode: The shapes of the two parameters are both (1), and the data type is the same as the query data type. This mode is supported when the key and value data types are INT8 or INT4(INT32).
        • per-token mode: The shapes of the two parameters can be (1, B, S) or (B, S), and the data type is fixed to FLOAT32. This mode is supported when the key and value data types are INT8 or INT4(INT32).
          • per-tensor combined with per-head mode: The shapes of both parameters are (N), and the data type is the same as the query data type. Supported when the key and value data types are INT8 or INT4(INT32).
        • key supports per-channel combined with value supports per-token mode: For key supporting per-channel, the shapes of the two parameters can be (1, N, 1, D), (1, N, D), or (1, H), and the parameter data type is the same as the query data type. For value supporting per-token, the shapes of both parameters are (1, B, S) and the data type is fixed to FLOAT32. Supported when the key and value data types are INT8 or INT4(INT32). When the key and value data types are INT8, the query and output support only FLOAT16.
        • per-token-group mode: the shape of antiquantScale is (1, B, N, S, D/32), the data type is fixed to FLOAT8_E8M0, and antiquantOffset is not supported. It is supported when the data types of key and value are FLOAT4_E2M1.
        • per-token combined with per-head mode: The shapes of both parameters are (B, N, S), and the data type is fixed to FLOAT32. Supported when the key and value data types are INT8 or INT4(INT32).
        • per-token mode using page attention to manage scale/offset mode: The shapes of both parameters are (blocknum, blocksize), and the data type is fixed to FLOAT32. Supported when the key and value data types are INT8.
        • per-token superimposed with per-head mode using page attention to manage scale/offset mode: The shape of both parameters is (blocknum, N, blocksize), the data type is fixed to FLOAT32, and it is supported when the data type of key and value is INT8.
        • When both fake-quantization and KV separation quantization parameters are passed, the KV separation quantization parameters are used.
      • The INT4 (INT32) pseudo-quantization scenario supports only KV pseudo-quantization parameter separation, specifically including:

        • per-tensor mode;
        • per-channel mode;
        • per-token mode;
        • per-tensor plus per-head mode;
        • per-token plus per-head mode;
        • key supports per-channel plus value supports per-token mode.
      • Some pseudo-quantization scenarios do not support post-quantization

        • Atlas A2 products: post-quantization is not supported in the INT4(INT32) pseudo-quantization use case.
        • Ascend 950PR&950DT products: Post-quantization is not supported in INT4(INT32) or FLOAT4_E2M1 pseudo-quantization use cases.
    • Prefix-related parameter constraints:

      • keySharedPrefix and valueSharedPrefix must either both be null or both be non-null.
      • When both keySharedPrefix and valueSharedPrefix are non-null, keySharedPrefix, valueSharedPrefix, key, and value must have the same number of dimensions and keep their dtypes consistent.
      • When both keySharedPrefix and valueSharedPrefix are not empty, the first dimension (batch) of keySharedPrefix's shape must be 1. In BNSD and BSND layouts, the N and D axes must be consistent with those of key. In BSH layout, H must be consistent with that of key. The same applies to valueSharedPrefix. The S of keySharedPrefix and valueSharedPrefix must be equal.
      • When actualSharedPrefixLen exists, its shape must be [1], and its value cannot be greater than the S of keySharedPrefix and valueSharedPrefix.
      • The result of adding the S of the common prefix to the S of key or value must satisfy the original S constraints of key or value.

Example

The following sample code is for reference only. For details about the compilation and execution process, see Compile and Run Samples.

#include <iostream>
#include <vector>
#include <math.h>
#include <cstring>
#include "acl/acl.h"
#include "aclnn/opdev/fp16_t.h"
#include "aclnnop/aclnn_fused_infer_attention_score_v3.h"

using namespace std;

#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;
}

int Init(int32_t deviceId, aclrtStream* stream) {
  // Boilerplate: AscendCL initialization.
  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 = 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::vector<T>& hostData, const std::vector<int64_t>& shape, void** deviceAddr,
                  aclDataType dataType, aclTensor** tensor) {
  auto size = GetShapeSize(shape) * aclDataTypeSize(dataType);
  // Call aclrtMalloc to allocate device-side memory.
  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);
  // Call aclrtMemcpy to copy host-side data to the device-side memory.
  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);

  // Calculate the strides of the contiguous tensor.
  std::vector<int64_t> strides(shape.size(), 1);
  for (int64_t i = shape.size() - 2; i >= 0; i--) {
      strides[i] = shape[i + 1] * strides[i + 1];
  }

  // Call the aclCreateTensor API to create an 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. (Boilerplate) Initialize device/stream. For details, see the AscendCL external API list.
  // Set deviceId based on the actual device.
  int32_t deviceId = 0;
  aclrtStream stream;
  auto ret = Init(deviceId, &stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);

  // 2. Construct inputs and outputs. The construction is custom based on the API.
  int32_t batchSize = 1;
  int32_t numHeads = 2;
  int32_t sequenceLengthQ = 1;
  int32_t headDims = 16;
  int32_t numKeyValueHeads = 2;
  int32_t sequenceLengthKV = 16;
  std::vector<int64_t> queryShape = {batchSize, numHeads, sequenceLengthQ, headDims};           // BNSD
  std::vector<int64_t> keyShape = {batchSize, numKeyValueHeads, sequenceLengthKV, headDims};    // BNSD
  std::vector<int64_t> valueShape = {batchSize, numKeyValueHeads, sequenceLengthKV, headDims};  // BNSD
  std::vector<int64_t> attenMaskShape = {batchSize, 1, sequenceLengthQ, sequenceLengthKV};      // B 1 S1 S2
  std::vector<int64_t> outShape = {batchSize, numHeads, sequenceLengthQ, headDims};             // BNSD
  void *queryDeviceAddr = nullptr;
  void *keyDeviceAddr = nullptr;
  void *valueDeviceAddr = nullptr;
  void *attenMaskDeviceAddr = nullptr;
  void *outDeviceAddr = nullptr;
  aclTensor *queryTensor = nullptr;
  aclTensor *keyTensor = nullptr;
  aclTensor *valueTensor = nullptr;
  aclTensor *attenMaskTensor = nullptr;
  aclTensor *outTensor = nullptr;
  int64_t queryShapeSize = GetShapeSize(queryShape);          // BNSD
  int64_t keyShapeSize = GetShapeSize(keyShape);              // BNSD
  int64_t valueShapeSize = GetShapeSize(valueShape);          // BNSD
  int64_t attenMaskShapeSize = GetShapeSize(attenMaskShape);  // B 1 S1 S2
  int64_t outShapeSize = GetShapeSize(outShape);              // BNSD
  std::vector<op::fp16_t> queryHostData(queryShapeSize, 1.0f);
  std::vector<op::fp16_t> keyHostData(keyShapeSize, 1.0f);
  std::vector<op::fp16_t> valueHostData(valueShapeSize, 1.0f);
  std::vector<int8_t> attenMaskHostData(attenMaskShapeSize, 0);
  std::vector<op::fp16_t> outHostData(outShapeSize, 1.0f);

  // Create the query aclTensor.
  ret = CreateAclTensor(queryHostData, queryShape, &queryDeviceAddr, aclDataType::ACL_FLOAT16, &queryTensor);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create the key aclTensor.
  ret = CreateAclTensor(keyHostData, keyShape, &keyDeviceAddr, aclDataType::ACL_FLOAT16, &keyTensor);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  int kvTensorNum = 1;
  aclTensor *tensorsOfKey[kvTensorNum];
  tensorsOfKey[0] = keyTensor;
  auto tensorKeyList = aclCreateTensorList(tensorsOfKey, kvTensorNum);
  // Create the value aclTensor.
  ret = CreateAclTensor(valueHostData, valueShape, &valueDeviceAddr, aclDataType::ACL_FLOAT16, &valueTensor);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  aclTensor *tensorsOfValue[kvTensorNum];
  tensorsOfValue[0] = valueTensor;
  auto tensorValueList = aclCreateTensorList(tensorsOfValue, kvTensorNum);
  // Create the attenMask aclTensor.
  ret = CreateAclTensor(attenMaskHostData, attenMaskShape, &attenMaskDeviceAddr, aclDataType::ACL_BOOL, &attenMaskTensor);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  // Create the out aclTensor.
  ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT16, &outTensor);
  CHECK_RET(ret == ACL_SUCCESS, return ret);

  std::vector<int64_t> actualSeqlenVector = {sequenceLengthKV};
  auto actualSeqLengths = aclCreateIntArray(actualSeqlenVector.data(), actualSeqlenVector.size());

  double scaleValue = 1 / sqrt(headDims); // 1/sqrt(d)
  int64_t preTokens = 2147483647;
  int64_t nextTokens = 2147483647;
  string sLayerOut = "BNSD";
  char layerOut[sLayerOut.length()+1];
  strcpy(layerOut, sLayerOut.c_str());
  int64_t sparseMode = 0;
  int64_t innerPrecise = 1;
  int blockSize = 0;
  int antiquantMode = 0;
  bool softmaxLseFlag = false;
  int keyAntiquantMode = 0;
  int valueAntiquantMode = 0;
  // 3. Call the CANN operator library API.
  uint64_t workspaceSize = 0;
  aclOpExecutor* executor;
  // Call the first-phase API.
  ret = aclnnFusedInferAttentionScoreV3GetWorkspaceSize(queryTensor, tensorKeyList, tensorValueList,  nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, nullptr, numHeads, scaleValue, preTokens, nextTokens, layerOut, numKeyValueHeads, sparseMode, innerPrecise, blockSize, antiquantMode, softmaxLseFlag, keyAntiquantMode, valueAntiquantMode, outTensor, nullptr, &workspaceSize, &executor);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnFusedInferAttentionScoreV3GetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
  // Allocate device memory based on the workspaceSize computed by the first-phase API.
  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);
  }
  // Call the second-phase API.
  ret = aclnnFusedInferAttentionScoreV3(workspaceAddr, workspaceSize, executor, stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnFusedInferAttentionScoreV3 failed. ERROR: %d\n", ret); return ret);

  // 4. (boilerplate) Synchronize and wait for the task execution to complete.
  ret = aclrtSynchronizeStream(stream);
  CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);

  // 5. Obtain the output value and copy the result from device-side memory to the host side. Modify the code based on the specific API definition.
  auto size = GetShapeSize(outShape);
  std::vector<op::fp16_t> resultData(size, 0);
  ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), outDeviceAddr,
                    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 ret);
  for (int64_t i = 0; i < size; i++) {
      std::cout << "index: " << i << ": " << static_cast<float>(resultData[i]) << std::endl;
  }

  // 6. Release resources.
  aclDestroyTensor(queryTensor);
  aclDestroyTensor(keyTensor);
  aclDestroyTensor(valueTensor);
  aclDestroyTensor(attenMaskTensor);
  aclDestroyTensor(outTensor);
  aclDestroyIntArray(actualSeqLengths);
  aclrtFree(queryDeviceAddr);
  aclrtFree(keyDeviceAddr);
  aclrtFree(valueDeviceAddr);
  aclrtFree(attenMaskDeviceAddr);
  aclrtFree(outDeviceAddr);
  if (workspaceSize > 0) {
      aclrtFree(workspaceAddr);
  }
  aclrtDestroyStream(stream);
  aclrtResetDevice(deviceId);
  aclFinalize();
  return 0;
}