aclnnMaskedCausalConv1dBackward

📄 View Source Code

Applicable Products

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

Function

  • Description: Performs backward gradient computation for 1D grouped convolution operations on tokens in the hidden layer.

  • Formula:

    Assume that the convolution input x, the gradient of the convolution output gradY, and the gradient of the convolution input gradX have a shape of [S, B, H], weight has a shape of [W, H], i and j denote the indices of the S/B axes respectively, and k is the index within the convolution window W. The computation is expressed as:

    $$ gradYMasked[i,j] = mask[j,i] * gradY[i,j] $$
    $$ gradX[i,j] = \sum_{k=0}^{W-1} gradYMasked[i+k,j] * weight[W-1-k] $$
    $$ gradWeight[k] = \sum_{j=0}^{B-1}\sum_{i=0}^{S-1} gradYMasked[i+W-1-k,j] * x[i,j] $$

    where invalid positions are padded with 0; currently only 3 is supported for W; the operation on the H axis is elementwise and is not reflected in the preceding formulas.

Function Prototype

Each operator uses a two-phase API. You must first call the "aclnnMaskedCausalConv1dBackwardGetWorkspaceSize" API to obtain the workspace size required for the computation and the executor containing the computation process, and then call the "aclnnMaskedCausalConv1dBackward" API to execute the computation.

aclnnStatus aclnnMaskedCausalConv1dBackwardGetWorkspaceSize(
  const aclTensor   *gradY,
  const aclTensor   *x,
  const aclTensor   *weight,
  const aclTensor   *maskOptional,
  const aclTensor   *gradXOut,
  const aclTensor   *gradWeightOut,
  uint64_t          *workspaceSize,
  aclOpExecutor    **executor)
aclnnStatus aclnnMaskedCausalConv1dBackward(
  void             *workspace,
  uint64_t          workspaceSize,
  aclOpExecutor    *executor,
  aclrtStream       stream)

aclnnMaskedCausalConv1dBackwardGetWorkspaceSize

  • Parameters:

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    gradY (aclTensor*) Input Gradient of the grouped convolution output y, corresponding to gradY in the formula.
    • Does not support null Tensor.
    • Shape is [S, B, H].
    BFLOAT16, FLOAT16 ND 3 √
    x (aclTensor*) Input Grouped convolution input, corresponding to x in the formula.
    • Does not support null tensor.
    • The shape is consistent with gradY.
    Data type consistent with gradY ND 3 √
    weight (aclTensor*) Input Represents the convolution weight, which is the weight in the corresponding formula.
    • Does not support null tensor.
    • The shape is [W, H].
    Data type consistent with gradY ND 2 √
    maskOptional (aclTensor*) Optional input Represents the output mask of the convolution operation, corresponding to mask in the formula.
    • Does not support null Tensor.
    • The shape is [B, S].
    • The default value is nullptr.
    BOOL ND 2 √
    gradXOut (aclTensor*) Output Represents the gradient of the grouped convolution input x, corresponding to gradX in the formula.
    • Does not support null Tensor.
    • The shape must be consistent with that of gradY.
    Data type consistent with that of gradY ND 3 x
    gradWeightOut (aclTensor*) Output Gradient of the grouped convolution input weight, corresponding to gradWeight in the formula.
    • Does not support null Tensor.
    • The shape is consistent with that of weight.
    Data type: consistent with gradY ND 2 x
    workspaceSize Output Returns the size of the workspace to be allocated on the device. - - - - -
    executor Output Returns the op executor, which contains the operator computation flow. - - - - -
  • Return value

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

    Error Code Error Code Description
    ACLNN_ERR_PARAM_NULLPTR 161001 The input parameter is a required input, output, or required attribute and is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data type or data format of gradY, x, weight, maskOptional, gradXOut, or gradWeightOut is out of the supported range.
    ACLNN_ERR_INNER_TILING_ERROR 561002 The shape of the input or output tensor is out of the supported range.

aclnnMaskedCausalConv1dBackward

  • Parameters:

    Parameter Input/Output Description
    workspace Input Workspace memory address allocated on the Device.
    workspaceSize Input Size of the workspace allocated on the Device, obtained by calling the first-phase API aclnnMaskedCausalConv1dBackwardGetWorkspaceSize.
    executor Input op executor that contains the operator computation flow.
    stream Input Stream for executing tasks.
  • Return value

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

Constraints

  • When using this API with PyTorch, ensure that the versions of CANN-related packages and PyTorch-related packages are compatible.
  • Deterministic computation:
    • aclnnMaskedCausalConv1dBackward defaults to a deterministic implementation.
  • Input restrictions:
    • The shape data range constraints for inputs and outputs are as follows:
      • B (Batchsize): value range 1-32.
      • B S (Batchsize SeqLength): value range 1-512K.
      • H (hiddenSize): value range 384-24576, and it must be a multiple of 64.
      • W: only 3 is supported currently.
  • The operator input parameters and intermediate computation results do not exceed the value range of the corresponding runtime data types (float16/bfloat16).
  • Operator inputs containing ±inf or nan are not supported.

Example

The sample code for calling a single operator via aclnn is as follows, for reference only. For details about compilation and execution, see Compile and Run Samples.

#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnn/opdev/fp16_t.h"
#include "aclnnop/aclnn_masked_causal_conv1d_backward.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<int8_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("result[%ld] is: %d\n", i, resultData[i]);
    }
}

int Init(int32_t deviceId, aclrtStream* stream)
{
    // Boilerplate for 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-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 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);

    // Compute 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. Refer to the ACL API.
    // 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 inputs and outputs according to the API definition.
    int64_t S = 1;
    int64_t B = 2;
    int64_t H = 192 * 2;
    int64_t W = 3;

    std::vector<int64_t> gradYShape = {S, B, H};
    std::vector<int64_t> xShape = {S, B, H};
    std::vector<int64_t> weightShape = {W, H};
    std::vector<int64_t> maskShape = {B, S};
    std::vector<int64_t> gradXShape = {S, B, H};
    std::vector<int64_t> gradWeightShape = {W, H};

    std::vector<op::fp16_t> gradYHostData(S * B * H, 0);
    std::vector<op::fp16_t> xHostData(S * B * H, 0);
    std::vector<op::fp16_t> weightHostData(W * H, 0);
    std::vector<uint8_t> maskHostData(B * S, 0);
    std::vector<op::fp16_t> gradXHostData(S * B * H, 0);
    std::vector<op::fp16_t> gradWeightHostData(W * H, 0);

    void* gradYDeviceAddr = nullptr;
    void* xDeviceAddr = nullptr;
    void* weightDeviceAddr = nullptr;
    void* maskDeviceAddr = nullptr;
    void* gradXDeviceAddr = nullptr;
    void* gradWeightDeviceAddr = nullptr;

    aclTensor* gradY = nullptr;
    aclTensor* x = nullptr;
    aclTensor* weight = nullptr;
    aclTensor* mask = nullptr;
    aclTensor* gradX = nullptr;
    aclTensor* gradWeight = nullptr;

    ret = CreateAclTensor(gradYHostData, gradYShape, &gradYDeviceAddr, aclDataType::ACL_FLOAT16, &gradY);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(xHostData, xShape, &xDeviceAddr, aclDataType::ACL_FLOAT16, &x);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(weightHostData, weightShape, &weightDeviceAddr, aclDataType::ACL_FLOAT16, &weight);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(maskHostData, maskShape, &maskDeviceAddr, aclDataType::ACL_BOOL, &mask);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(gradXHostData, gradXShape, &gradXDeviceAddr, aclDataType::ACL_FLOAT16, &gradX);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    ret = CreateAclTensor(gradWeightHostData, gradWeightShape, &gradWeightDeviceAddr, aclDataType::ACL_FLOAT16, &gradWeight);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    // 3. Call the CANN operator library API. Replace it with the specific API.
    uint64_t workspaceSize = 0;
    aclOpExecutor* executor;

    // Call the first-phase API of aclnnMaskedCausalConv1dBackward.
    ret = aclnnMaskedCausalConv1dBackwardGetWorkspaceSize(
        gradY, x, weight, mask, gradX, gradWeight, &workspaceSize, &executor);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnMaskedCausalConv1dBackwardGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);

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

    // 4. Boilerplate. Synchronously 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 values and copy the results from the device memory to the host memory. Modify the code according to the specific API definition.
    PrintOutResult(gradXShape, &gradXDeviceAddr);
    PrintOutResult(gradWeightShape, &gradWeightDeviceAddr);

    // 6. Release aclTensor and aclScalar. Modify the code according to the specific API definition.
    aclDestroyTensor(gradY);
    aclDestroyTensor(x);
    aclDestroyTensor(weight);
    aclDestroyTensor(mask);
    aclDestroyTensor(gradX);
    aclDestroyTensor(gradWeight);

    // 7. Release device resources.
    aclrtFree(gradYDeviceAddr);
    aclrtFree(xDeviceAddr);
    aclrtFree(weightDeviceAddr);
    aclrtFree(maskDeviceAddr);
    aclrtFree(gradXDeviceAddr);
    aclrtFree(gradWeightDeviceAddr);

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

    return 0;
}