aclnnAdvanceStepV2

📄 View Source Code

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:

    vLLM is a high-performance LLM inference and serving framework focused on optimizing the inference efficiency of large-scale language models. Its core features include PageAttention and efficient memory management. The primary function of the advance_step operator is to advance the inference step, i.e., updating the model state at each generation step and generating new inputTokens, inputPositions, seqLens, and slotMapping, thereby improving efficiency for vLLM inference.

  • Formula:

    blockIdxistheindexofthekernelwherethecurrentcodeisexecuted.blockIdx is the index of the kernel where the current code is executed. blockTablesStride=blockTables.stride(0)blockTablesStride = blockTables.stride(0) inputTokens[blockIdx]=sampledTokenIds[blockIdx]inputTokens[blockIdx] = sampledTokenIds[blockIdx] inputPositions[blockIdx]=seqLens[blockIdx]inputPositions[blockIdx] = seqLens[blockIdx] seqLens[blockIdx]=seqLens[blockIdx]+1seqLens[blockIdx] = seqLens[blockIdx] + 1 slotMapping[blockIdx]=(blockTables[blockIdx]+blockTablesStride∗blockIdx)∗blockSize+(seqLens[blockIdx]%blockSize)slotMapping[blockIdx] = (blockTables[blockIdx] + blockTablesStride * blockIdx) * blockSize + (seqLens[blockIdx] \% blockSize)

Function Prototype

Each operator uses a two-phase API. First call the "aclnnAdvanceStepV2GetWorkspaceSize" API to obtain the input parameters and calculate the required workspace size based on the calculation flow. Then call the "aclnnAdvanceStepV2" API to perform the calculation.

aclnnStatus aclnnAdvanceStepV2GetWorkspaceSize(
  const aclTensor *inputTokens,
  const aclTensor *sampledTokenIds,
  const aclTensor *inputPositions,
  const aclTensor *seqLens,
  const aclTensor *slotMapping,
  const aclTensor *blockTables,
  const aclTensor *specToken,
  const aclTensor *acceptedNum,
  int64_t          numSeqs,
  int64_t          numQueries,
  int64_t          blockSize,
  uint64_t        *workspaceSize,
  aclOpExecutor  **executor)
aclnnStatus aclnnAdvanceStepV2(
  void            *workspace,
  uint64_t         workspaceSize,
  aclOpExecutor   *executor,
  aclrtStream      stream)

aclnnAdvanceStepV2GetWorkspaceSize

  • Parameters:

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    inputTokens (aclTensor*) Input/Output Input/output parameter for the AdvanceStepV2 calculation to be performed. It is output inputTokens in the formula, used to update token values in the vLLM model.
    • Empty tensors are not supported.
    • The shape is [numSeqs * (1+specNum)].
    • The value range is positive integers greater than 0.
    INT64 ND 1 ×
    sampledTokenIds (aclTensor*) Input Input parameter for the AdvanceStepV2 calculation to be performed. It is used to store token IDs and is input sampledTokenIds in the formula.
    • Empty tensors are not supported.
    • The length of the first dimension of the shape is consistent with numSeqs, and the length of the second dimension is 1+specNum.
    • The value range is positive integers greater than 0.
    INT64 ND 2 ×
    inputPositions (aclTensor*) Input/Output Input/output parameter for the AdvanceStepV2 calculation to be performed, output inputPositions in the formula, used to record the token index.
    • Empty tensors are not supported.
    • The shape is [numSeqs * (1+specNum)].
    • The value range is positive integers greater than 0.
    INT64 ND 1 ×
    seqLens (aclTensor*) Input/Output Input/output parameter for the AdvanceStepV2 calculation to be performed, used to record the length of seq for different blockIdx, which is the input/output seqLens in the formula.
    • Empty tensor is not supported.
    • The shape is [numSeqs * (1+specNum)].
    • Value range is a positive integer greater than 0.
    INT64 ND 1 ×
    slotMapping (aclTensor*) Input/Output Input/output parameter for the AdvanceStepV2 calculation to be performed. Output slotMapping in the formula, used to map the position of the token value in a sequence to the physical position.
    • Empty tensors are not supported.
    • The shape is [numSeqs * (1+specNum)].
    • The value range is positive integers greater than 0.
    INT64 ND 1 ×
    blockTables (aclTensor*) Input Input for the AdvanceStepV2 calculation to be performed, used to record the physical block ID of blocks under different blockIdx, that is, the input blockTables in the formula.
    • Empty tensors are not supported.
    • The length of the first dimension of shape is consistent with numSeqs, and the second dimension is greater than (maximum value in seqLens)/blockSize.
    • The value range is positive integers greater than 0.
    INT64 ND 2 ×
    specToken(aclTensor*) Input Input for the AdvanceStepV2 calculation to be performed, used to record the index of the token of the current speculative model.
    • Empty tensors are not supported.
    • The length of the first dimension of shape is consistent with numSeqs, and the length of the second dimension is specNum.
    • The value range is positive integers greater than 0.
    INT64 ND 2 ×
    acceptedNum (aclTensor*) Input Input for the AdvanceStepV2 calculation to be performed, used to record the number of speculations accepted by each request.
    • Empty tensors are not supported.
    • The length of shape is consistent with numSeqs.
    • The value range is positive integers greater than 0.
    INT64 ND 1 ×
    numSeqs (int64_t) Input Records the number of input seqs. The size is consistent with the length of seqLens.
    • The value range is positive integers greater than 0.
    • The value of numSeqs is greater than that of input numQueries.
    - - - -
    numQueries (int64_t) Input Records the number of queries in the input. The size is consistent with the length of the first dimension of sampledTokenIds. The value range is positive integers greater than 0. - - - -
    blockSize (int64_t) Input Size of each block, corresponding to blockSize in the formula. The value range is positive integers greater than 0. - - - -
    workspaceSize (uint64_t*) Output Returns the workspace size that needs to be requested on the Device side. - - - - -
    executor(aclOpExecutor**) Output Returns the op executor containing 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:

Error Code error code description
ACLNN_ERR_PARAM_NULLPTR 161001 inputTokens, sampledTokenIds, inputPositions, seqLens, slotMapping, blockTables, specToken, and acceptedNum are null pointers.
ACLNN_ERR_PARAM_INVALID 161002 The data types of inputTokens, sampledTokenIds, inputPositions, seqLens, slotMapping, blockTables, specToken, and acceptedNum are not within the supported range.
aclnnAdvanceStepV2GetWorkspaceSize failed 561002 The shape of inputTokens, inputPositions, seqLens, or slotMapping is not [numSeqs * (1+specNum)], or the length of the first dimension of the shape of blockTables, specToken, or acceptedNum is inconsistent with numSeqs.
The length of the first dimension of the shape of sampledTokenIds is inconsistent with numSeqs, or the length of the second dimension of the shape is not 1+specNum.
The length of the shape of acceptedNum is inconsistent with numSeqs.
The length of the second dimension of the shape of specToken is not specNum.
The value of numSeqs is not equal to the value of numQueries.

aclnnAdvanceStepV2

  • Parameters:

    Parameter Input/Output Description
    workspace Input Workspace memory address requested by the Device.
    workspaceSize Input Workspace size requested by the Device, obtained by calling the first-phase API aclnnAdvanceStepV2GetWorkspaceSize.
    executor Input Op executor, containing the operator computation flow.
    stream Input Stream for task execution.
  • Return value

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

Constraints

  • Deterministic computation:
    • aclnnAdvanceStepV2 defaults to a deterministic implementation.

Example

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

#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_advance_step_v2.h"
#define CHECK_RET(cond, return_expr) \
    do {                               \
    if (!(cond)) {                   \
        return_expr;                   \
    }                                \
    } while (0)

#define LOG_PRINT(message, ...)     \
    do {                              \
    printf(message, ##__VA_ARGS__); \
    } while (0)

int64_t GetShapeSize(const std::vector<int64_t>& shape) {
    int64_t shapeSize = 1;
    for (auto i : shape) {
    shapeSize *= i;
    }
    return shapeSize;
}

void PrintOutResult(std::vector<int64_t> &shape, void** deviceAddr) {
    auto size = GetShapeSize(shape);
    std::vector<int64_t> resultData(size, 0);
    auto ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]),
                        *deviceAddr, size * sizeof(resultData[0]), ACL_MEMCPY_DEVICE_TO_HOST);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("copy result from device to host failed. ERROR: %d\n", ret); return);
    for (int64_t i = 0; i < size; i++) {
    LOG_PRINT("advanceStepV2 result[%ld] is: %ld\n", i, resultData[i]);
    }
}

int Init(int64_t deviceId, aclrtStream* stream) {
    // Boilerplate: resource 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) * sizeof(T);
    // Call aclrtMalloc to allocate device 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 data to device 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) Device/stream initialization. For details, see the acl API documentation.
    // Fill in 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 the input and output. Customize the construction as required by the API.
    std::vector<int64_t> input1Shape = {72};
    std::vector<int64_t> input2Shape = {8,9};
    std::vector<int64_t> input3Shape = {72};
    std::vector<int64_t> input4Shape = {72};
    std::vector<int64_t> input5Shape = {72};
    std::vector<int64_t> input6Shape = {8,1000};
    std::vector<int64_t> input7Shape = {8,8};
    std::vector<int64_t> input8Shape = {8};
    std::vector<int64_t> input1HostData(8 * 9, 1);
    std::vector<int64_t> input2HostData(8 * 9, 1);
    std::vector<int64_t> input3HostData(8 * 9, 1);
    std::vector<int64_t> input4HostData(8 * 9, 8);
    std::vector<int64_t> input5HostData(8 * 9, 1);
    std::vector<int64_t> input6HostData(8 * 1000, 1);
    std::vector<int64_t> input7HostData(8 * 8, 1);
    std::vector<int64_t> input8HostData(8, 1);

    void* input1DeviceAddr = nullptr;
    aclTensor* input1 = nullptr;
    void* input2DeviceAddr = nullptr;
    aclTensor* input2 = nullptr;
    void* input3DeviceAddr = nullptr;
    aclTensor* input3 = nullptr;
    void* input4DeviceAddr = nullptr;
    aclTensor* input4 = nullptr;
    void* input5DeviceAddr = nullptr;
    aclTensor* input5 = nullptr;
    void* input6DeviceAddr = nullptr;
    aclTensor* input6 = nullptr;
    void* input7DeviceAddr = nullptr;
    aclTensor* input7 = nullptr;
    void* input8DeviceAddr = nullptr;
    aclTensor* input8 = nullptr;
    // Create the input aclTensor.
    ret = CreateAclTensor(input1HostData, input1Shape, &input1DeviceAddr, aclDataType::ACL_INT64, &input1);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(input2HostData, input2Shape, &input2DeviceAddr, aclDataType::ACL_INT64, &input2);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(input3HostData, input3Shape, &input3DeviceAddr, aclDataType::ACL_INT64, &input3);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(input4HostData, input4Shape, &input4DeviceAddr, aclDataType::ACL_INT64, &input4);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(input5HostData, input5Shape, &input5DeviceAddr, aclDataType::ACL_INT64, &input5);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(input6HostData, input6Shape, &input6DeviceAddr, aclDataType::ACL_INT64, &input6);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(input7HostData, input7Shape, &input7DeviceAddr, aclDataType::ACL_INT64, &input7);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(input8HostData, input8Shape, &input8DeviceAddr, aclDataType::ACL_INT64, &input8);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    int64_t numseq = 8;
    int64_t specnum = 8;
    int64_t blocksize = 8;

    // 3. Call the CANN operator library API. Change it to the specific API name.
    uint64_t workspaceSize = 16 * 1024 * 1024;
    aclOpExecutor* executor;

    // Call the aclnnAdvanceStepV2 first-phase API.
    ret = aclnnAdvanceStepV2GetWorkspaceSize(
    input1,input2,input3,input4,input5,input6,input7,input8,
    numseq,numseq,blocksize,
    &workspaceSize,
    &executor);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAdvanceStepV2GetWorkspaceSize failed. ERROR: %d\n", ret); return ret);

    // Allocate device memory based on the workspaceSize calculated 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 aclnnAdvanceStepV2.
    ret = aclnnAdvanceStepV2(
    workspaceAddr,
    workspaceSize,
    executor,
    stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnAdvanceStepV2 failed. ERROR: %d\n", ret); return ret);

    // 4. (Boilerplate) Wait for the task execution to complete synchronously.
    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 memory to host memory. Modify the code based on the specific API definition.
    PrintOutResult(input1Shape, &input1DeviceAddr);
    PrintOutResult(input2Shape, &input2DeviceAddr);
    PrintOutResult(input3Shape, &input3DeviceAddr);
    PrintOutResult(input4Shape, &input4DeviceAddr);
    PrintOutResult(input5Shape, &input5DeviceAddr);

    // 6. Release the aclTensor. Modify the code based on the specific API definition.
    aclDestroyTensor(input1);
    aclDestroyTensor(input2);
    aclDestroyTensor(input3);
    aclDestroyTensor(input4);
    aclDestroyTensor(input5);
    aclDestroyTensor(input6);
    aclDestroyTensor(input7);
    aclDestroyTensor(input8);

    // 7. Release the device resources. Modify the code based on the specific API definition.
    aclrtFree(input1DeviceAddr);
    aclrtFree(input2DeviceAddr);
    aclrtFree(input3DeviceAddr);
    aclrtFree(input4DeviceAddr);
    aclrtFree(input5DeviceAddr);
    aclrtFree(input6DeviceAddr);
    aclrtFree(input7DeviceAddr);
    aclrtFree(input8DeviceAddr);

    if (workspaceSize > 0) {
    aclrtFree(workspaceAddr);
    }
    aclrtDestroyStream(stream);
    aclrtResetDevice(deviceId);
    aclFinalize();

    return 0;
}