aclnnCdistBackward

Applicable Products

  • Ascend 950PR&950DT products: 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: Completes the backward propagation of aclnnCdist.
  • Formula:out=grad⋅y′=grad⋅(∑(x1−x2)pp)′=grad⋅1p×(∑(x1−x2)p)1p−1×p×(x1−x2)p−1=grad⋅(∑(x1−x2)p)−(p−1)p×(x1−x2)p−1=grad⋅(∑(x1−x2)p)1p×(−(p−1))×(x1−x2)p−1=grad⋅diffp−1cdistp−1=grad⋅diff×∣diff∣p−2cdistp−1\begin{aligned} out&=grad \cdot y' \\ &= grad \cdot \left( \sqrt[p]{\sum (x_1 - x_2)^p} \right)' \\ &= grad \cdot \frac{1}{p} \times \left( \sum (x_1 - x_2)^p \right)^{\frac{1}{p}-1} \times p \times (x_1 - x_2)^{p-1} \\ &= grad \cdot \left( \sum (x_1 - x_2)^p \right)^{\frac{-(p-1)}{p}} \times (x_1 - x_2)^{p-1} \\ &= grad \cdot \left( \sum (x_1 - x_2)^p \right)^{\frac{1}{p} \times (-(p-1))} \times (x_1 - x_2)^{p-1} \\ &= grad \cdot \frac{diff^{p-1}}{cdist^{p-1}} \\ &= grad \cdot \frac{diff \times |diff|^{p-2}}{cdist^{p-1}} \end{aligned}
    • diff=x1−x2\mathrm{diff} = x_1 - x_2 : variable difference
    • cdist=∑(x1−x2)pp\mathrm{cdist} = \sqrt[p]{\sum (x_1 - x_2)^p} : pp-norm distance

Function Prototype

Each operator uses a two-phase API. You must first call the "aclnnCdistBackwardGetWorkspaceSize" API to obtain the input parameters and calculate the required workspace size based on the process, and then call the "aclnnCdistBackward" API to execute the computation.

aclnnStatus aclnnCdistBackwardGetWorkspaceSize(
    const aclTensor *grad,
    const aclTensor *x1,
    const aclTensor *x2,
    const aclTensor *cdist,
    float            p,
    aclTensor       *out,
    uint64_t        *workspaceSize,
    aclOpExecutor  **executor)
aclnnStatus aclnnCdistBackward(
    void*          workspace,
    uint64_t       workspaceSize,
    aclOpExecutor *executor,
    aclrtStream    stream)

aclnnCdistBackwardGetWorkspaceSize

  • Parameters:

    Parameter Input/Output Description Instruction Data Type Data Format Shape Non-contiguous Tensor
    grad (aclTensor*) Input grad in the formula. - FLOAT, FLOAT16, BFLOAT16 ND 2-7 dimensions. √
    x1 (aclTensor*) Input x1 in the formula. Data type is the same as that of grad. FLOAT, FLOAT16, BFLOAT16 ND The number of dimensions is the same as that of grad. Except for the last dimension, the shape is the same as that of grad. √
    x2 (aclTensor*) Input x2 in the formula. Data type is the same as that of grad. FLOAT, FLOAT16, BFLOAT16 ND The dimensions are the same as those of grad, the second-to-last dimension is the same as the last dimension of grad, the last dimension is the same as the last dimension of x1, and the remaining dimensions must comply with the broadcast relationship with grad. √
    cdist (aclTensor*) Input cdist in the formula. The data type is the same as that of grad. FLOAT, FLOAT16, BFLOAT16 ND The shape is the same as that of grad. √
    p (float) Attribute p in the formula. - float - - -
    out (aclTensor*) Output The out in the formula. The data type is the same as that of grad. FLOAT, FLOAT16, BFLOAT16 ND The shape is the same as that of x1. √
    workspaceSize (uint64_t*) Output Returns the workspace size to be requested on the Device. - - - - -
    executor(aclOpExecutor**) Output Returns the op executor that contains the operator computation flow. - - - - -
  • Return value

    aclnnStatus: Return 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 The input grad, x1, x2, or cdist is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data type of grad, x1, x2, or cdist is out of the supported range.
    The shape of grad, x1, x2, or cdist is out of the supported range.

aclnnCdistBackward

  • Parameters:

    Parameter Input/Output Description
    workspace Input Address of the workspace memory requested on the Device.
    workspaceSize Input Size of the workspace memory requested on the Device, obtained by the first-phase API aclnnCdistBackwardGetWorkspaceSize.
    executor Input Op executor that contains the operator calculation process.
    stream Input Stream for executing the task.
  • Return value

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

Constraints

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

int Init(int32_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 memory on the device.
    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 data from the host to the 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) Initialize the device/stream. For details, see the acl API manual.
    // 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);
    int B=5, P=7, Q=9, M=11;
    // 2. Construct the input and output. The construction must be customized based on the API.
    std::vector<int64_t> gradShape = {B, P, Q};
    std::vector<int64_t> x1Shape = {B, P, M};
    std::vector<int64_t> x2Shape = {B, Q, M};
    std::vector<int64_t> cdistShape = {B, P, Q};
    std::vector<int64_t> outShape = {B, P, M};
    void *gradDeviceAddr = nullptr;
    void *x1DeviceAddr = nullptr;
    void *x2DeviceAddr = nullptr;
    void *cdistDeviceAddr = nullptr;
    void *outDeviceAddr = nullptr;
    aclTensor *grad = nullptr;
    aclTensor *x1 = nullptr;
    aclTensor *x2 = nullptr;
    aclTensor *cdist = nullptr;
    aclTensor *out = nullptr;
    float p = 0.5;
    std::vector<float> gradHostData(B * P * Q, 1);
    std::vector<float> x1HostData(B * P * M, 2);
    std::vector<float> x2HostData(B * Q * M, 4);
    std::vector<float> cdistHostData(B * P * Q, 0.5);
    std::vector<float> outHostData(B * P * M, 1);
    // Create the grad aclTensor.
    ret = CreateAclTensor(gradHostData, gradShape, &gradDeviceAddr, aclDataType::ACL_FLOAT, &grad);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    // Create the x1 aclTensor.
    ret = CreateAclTensor(x1HostData, x1Shape, &x1DeviceAddr, aclDataType::ACL_FLOAT, &x1);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    // Create the x2 aclTensor.
    ret = CreateAclTensor(x2HostData, x2Shape, &x2DeviceAddr, aclDataType::ACL_FLOAT, &x2);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    // Create the cdist aclTensor.
    ret = CreateAclTensor(cdistHostData, cdistShape, &cdistDeviceAddr, aclDataType::ACL_FLOAT, &cdist);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    // Create the out aclTensor.
    ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    // 3. Call the CANN operator library API. Change it to the specific API name.
    uint64_t workspaceSize = 0;
    aclOpExecutor *executor;
    // Call the first-phase API of aclnnCdistBackward.
    ret = aclnnCdistBackwardGetWorkspaceSize(grad, x1, x2, cdist, p, out, &workspaceSize, &executor);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCdistBackwardGetWorkspaceSize 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 of aclnnCdistBackward.
    ret = aclnnCdistBackward(workspaceAddr, workspaceSize, executor, stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnCdistBackward failed. ERROR: %d\n", ret); return ret);

    // 4. (boilerplate) Synchronize and wait for the task 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 memory to host memory. Change the code based on the specific API definition.
    auto size = GetShapeSize(outShape);
    std::vector<float> 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 < 10; i++) {
        LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]);
    }

    // 6. Release the aclTensor and aclScalar. Change the code based on the specific API definition.
    aclDestroyTensor(x1);
    aclDestroyTensor(x2);
    aclDestroyTensor(cdist);
    aclDestroyTensor(grad);
    aclDestroyTensor(out);

    // 7. Release device resources.
    aclrtFree(x1DeviceAddr);
    aclrtFree(x2DeviceAddr);
    aclrtFree(cdistDeviceAddr);
    aclrtFree(gradDeviceAddr);
    aclrtFree(outDeviceAddr);
    if (workspaceSize > 0) {
        aclrtFree(workspaceAddr);
    }
    aclrtDestroyStream(stream);
    aclrtResetDevice(deviceId);
    aclFinalize();

    return 0;
}