aclnnChamferDistanceBackward

📄 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: The backward operator of Chamfer distance. It computes the gradient corresponding to the inputs based on the contribution of the forward inputs to the outputs and the initial gradient.

  • Formula:

    Assume there are two point sets: xyz1=[B,N,2], xyz2=[B,M,2]

    • The Chamfer distance forward operator formula is:

      dist1i=Min((x1i−x2)2+(y1i−y2)2),x2,y2∈xyz2dist1_i=Min((x_{1_i}−x_2)^2+(y_{1_i}−y_2)^2), x_2, y_2∈xyz2

      dist2i=Min((x2i−x1)2+(y2i−y1)2),x1,y1∈xyz1dist2_i=Min((x_{2_i}-x_1)^2+(y_{2_i}-y_1)^2), x_1, y_1∈xyz1

    • The backward operator computes the derivative of this formula, as follows:

      • The derivative of dist1idist1_i with respect to x1ix_{1_i} =2∗gradDist1∗(x1i−x2)=2*gradDist1*(x_{1_i}-x_2)

        Where: x1i∈xyz1x_{1_i}∈xyz1, and x2x_2 is the x-coordinate of the point with the smallest distance, which is retrieved from xyz2 based on the index value of idx1 in the forward output. The single-point derivative formula is as above. Because the positions for single-point gradient updates are consecutive, multi-point parallel calculation can be considered.

      • The derivative of dist1idist1_i with respect to y1iy_{1_i} =2∗gradDist1∗(y1i−y2)=2*gradDist1*(y_{1_i}-y_2)

        Where y1i∈xyz1y_{1_i}∈xyz1, and y2y_2 is the y-coordinate of the point with the smallest distance, which is retrieved from xyz2 based on the index value of idx1 in the forward output. The single-point derivative formula is as above. Because the positions for single-point gradient updates are consecutive, multi-point parallel calculation can also be considered.

      • The derivative of dist1idist1_i with respect to x2x_2 =−2∗gradDist1∗(x1−x2i)=-2*gradDist1*(x_1-x_{2_i})

        Where x2i∈xyz2x_{2_i}∈xyz2 and x1x_1 is the x-coordinate of the point with the smallest distance, which is retrieved from xyz2 based on the index value of idx1 in the forward output. The single-point derivative formula is as above. Because the single-point gradient needs to be updated based on the index value corresponding to the minimum distance, this part cannot be computed in parallel and can only be computed at a single point.

      • The derivative of dist1idist1_i with respect to y2y_2 =−2∗gradDist1∗(y1−y2i)=-2*gradDist1*(y_1-y_{2_i})

        Where y2i∈xyz2y_{2_i}∈xyz2 and y1y_1 is the y-coordinate of the point with the smallest distance, which is retrieved from xyz2 based on the index value of idx1 in the forward output. The single-point derivative formula is as above. Because the single-point gradient needs to be updated based on the index value corresponding to the minimum value, this part cannot be computed in parallel either and can only be computed at a single point.

    The derivatives of dist2idist2_i with respect to x2ix_{2_i}, x1x_1, y2iy_{2_i}, and y1y_1 are similar to the above process and are not repeated here.

    The final formula is as follows, where i∈[0,n):

    gradXyz1[2∗i]=2∗gradDist1∗(x1i−x2)−2∗gradDist1∗(x1−x2i)gradXyz1[2*i] = 2*gradDist1*(x_{1_i}-x_2) - 2*gradDist1*(x_1-x_{2_i})

    gradXyz1[2∗i+1]=2∗gradDist1∗(y1i−y2)−2∗gradDist1∗(y1−y2i)gradXyz1[2*i+1] = 2*gradDist1*(y_{1_i}-y_2) - 2*gradDist1*(y_1-y_{2_i})

    gradXyz2[2∗i]=2∗gradDist2∗(x1i−x2)−2∗gradDist2∗(x1−x2i)gradXyz2[2*i] = 2*gradDist2*(x_{1_i}-x_2) - 2*gradDist2*(x_1-x_{2_i})

    gradXyz2[2∗i+1]=2∗gradDist2∗(y1i−y2)−2∗gradDist2∗(y1−y2i)gradXyz2[2*i+1] = 2*gradDist2*(y_{1_i}-y_2) - 2*gradDist2*(y_1-y_{2_i})

Function Prototype

Each operator uses a two-phase API. First call the "aclnnChamferDistanceBackwardGetWorkspaceSize" API to obtain the input parameters and calculate the required workspace size based on the process. Then call the "aclnnChamferDistanceBackward" API to perform the computation.

aclnnStatus aclnnChamferDistanceBackwardGetWorkspaceSize(
    const aclTensor* xyz1,
    const aclTensor* xyz2,
    const aclTensor* idx1,
    const aclTensor* idx2,
    const aclTensor* gradDist1,
    const aclTensor* gradDist2,
    aclTensor*       gradXyz1,
    aclTensor*       gradXyz2,
    uint64_t*        workspaceSize,
    aclOpExecutor**  executor)
aclnnStatus aclnnChamferDistanceBackward(
    void*            workspace,
    uint64_t         workspaceSize,
    aclOpExecutor*   executor,
    aclrtStream      stream)

aclnnChamferDistanceBackwardGetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    xyz1 (aclTensor*) Input Coordinates of point set 1 in the operator forward input. The shape is (B,N,2). FLOAT, FLOAT16 ND 3 √
    xyz2 (aclTensor*) Input Coordinates of point set 2 in the operator forward input. The shape is (B,M,2). FLOAT, FLOAT16 ND 3 √
    idx1 (aclTensor*) Input Index tensor of the points in xyz2 with the minimum distance to xyz1, output by the operator forward. The shape is (B,N). INT32 ND 2 √
    idx2 (aclTensor*) Input Index tensor of the points in xyz1 closest to xyz2, output by the operator forward. The shape is (B,N). INT32 ND 2 √
    gradDist1 (aclTensor*) Input Backward gradient of the forward output dist1, and also the initial gradient of the backward operator. The shape is (B,N). FLOAT, FLOAT16 ND 2 √
    gradDist2 (aclTensor*) Input Backward gradient of the forward output dist2, and the initial gradient of the backward operator. Shape is (B,N). FLOAT, FLOAT16 ND 2 √
    gradXyz1 (aclTensor*) Output Gradient of the operator forward input xyz1 after the gradient update. The shape is (B,N,2). FLOAT, FLOAT16 ND 3 √
    gradXyz2 (aclTensor*) Output Gradient corresponding to the operator forward input xyz2 after the gradient update. The shape is (B,N,2). FLOAT, FLOAT16 ND 3 √
    workspaceSize (uint64_t*) Output Returns the size of the workspace to be requested on the Device. - - - - -
    executor (aclOpExecutor**) Output Returns the op executor containing the operator computation flow. - - - - -
  • Return value:

    aclnnStatus: return value, which is a 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 input xyz1, xyz2, idx1, idx2, gradDist1, gradDist2, or output gradXyz1 or gradXyz2 is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002 The data type of an input or output is not within the supported range.
    The data type of the input cannot be deduced.
    The derived data type cannot be converted to the type of the specified output out.

aclnnChamferDistanceBackward

  • Parameters:

    Parameter Input/Output Description
    workspace Input Workspace memory address allocated on the Device.
    workspaceSize Input Workspace size allocated on the Device, obtained by calling the first-phase API aclnnChamferDistanceBackwardGetWorkspaceSize.
    executor Input Op executor that contains 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:
    • aclnnChamferDistanceBackward defaults to a non-deterministic implementation. You can enable deterministic computation by calling aclrtCtxSetSysParamOpt.

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_chamfer_distance_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 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 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 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 a 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 ACL API list.
    // Fill in deviceId based on your actual device.
    int32_t deviceId = 0;
    aclrtStream stream;
    auto ret = Init(deviceId, &stream);
    // Handle the check as needed.
    CHECK_RET(ret == 0, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);

    // 2. Construct the input and output. Customize the construction based on the API.
    std::vector<int64_t> xyz1Shape = {2, 2, 2};
    std::vector<int64_t> xyz2Shape = {2, 2, 2};
    std::vector<int64_t> idx1Shape = {2, 2};
    std::vector<int64_t> idx2Shape = {2, 2};
    std::vector<int64_t> gradDist1Shape = {2, 2};
    std::vector<int64_t> gradDist2Shape = {2, 2};
    std::vector<int64_t> gradXyz1Shape = {2, 2, 2};
    std::vector<int64_t> gradXyz2Shape = {2, 2, 2};
    void* xyz1DeviceAddr = nullptr;
    void* xyz2DeviceAddr = nullptr;
    void* idx1DeviceAddr = nullptr;
    void* idx2DeviceAddr = nullptr;
    void* gradDist1DeviceAddr = nullptr;
    void* gradDist2DeviceAddr = nullptr;
    void* gradXyz1DeviceAddr = nullptr;
    void* gradXyz2DeviceAddr = nullptr;
    aclTensor* xyz1 = nullptr;
    aclTensor* xyz2 = nullptr;
    aclTensor* idx1 = nullptr;
    aclTensor* idx2 = nullptr;
    aclTensor* gradDist1 = nullptr;
    aclTensor* gradDist2 = nullptr;
    aclTensor* gradXyz1 = nullptr;
    aclTensor* gradXyz2 = nullptr;
    std::vector<float> xyz1HostData = {0, 1, 2, 3, 4, 5, 6, 7};
    std::vector<float> xyz2HostData = {1, 1, 1, 2, 2, 2, 3, 3};
    std::vector<int32_t> idx1HostData = {0, 1, 2, 3};
    std::vector<int32_t> idx2HostData = {0, 1, 2, 3};
    std::vector<float> gradDist1HostData = {0, 1, 2, 3};
    std::vector<float> gradDist2HostData = {0, 1, 2, 3};
    std::vector<float> gradXyz1HostData = {0, 0, 0, 0, 0, 0, 0, 0};
    std::vector<float> gradXyz2HostData = {0, 0, 0, 0, 0, 0, 0, 0};
    // Create the xyz1 aclTensor.
    ret = CreateAclTensor(xyz1HostData, xyz1Shape, &xyz1DeviceAddr, aclDataType::ACL_FLOAT, &xyz1);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create the xyz2 aclTensor.
    ret = CreateAclTensor(xyz2HostData, xyz2Shape, &xyz2DeviceAddr, aclDataType::ACL_FLOAT, &xyz2);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create idx1 aclTensor.
    ret = CreateAclTensor(idx1HostData, idx1Shape, &idx1DeviceAddr, aclDataType::ACL_INT32, &idx1);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create idx2 aclTensor.
    ret = CreateAclTensor(idx2HostData, idx2Shape, &idx2DeviceAddr, aclDataType::ACL_INT32, &idx2);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create gradDist1 aclTensor.
    ret = CreateAclTensor(gradDist1HostData, gradDist1Shape, &gradDist1DeviceAddr, aclDataType::ACL_FLOAT, &gradDist1);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create gradDist2 aclTensor.
    ret = CreateAclTensor(gradDist2HostData, gradDist2Shape, &gradDist2DeviceAddr, aclDataType::ACL_FLOAT, &gradDist2);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create gradXyz1 aclTensor.
    ret = CreateAclTensor(gradXyz1HostData, gradXyz1Shape, &gradXyz1DeviceAddr, aclDataType::ACL_FLOAT, &gradXyz1);
    CHECK_RET(ret == ACL_SUCCESS, return ret);
    // Create gradXyz2 aclTensor.
    ret = CreateAclTensor(gradXyz2HostData, gradXyz2Shape, &gradXyz2DeviceAddr, aclDataType::ACL_FLOAT, &gradXyz2);
    CHECK_RET(ret == ACL_SUCCESS, return ret);

    // 3. Call the CANN operator library API, and change it to the specific API.
    uint64_t workspaceSize = 0;
    aclOpExecutor* executor;
    // Call the first-phase API of aclnnChamferDistanceBackward.
    ret = aclnnChamferDistanceBackwardGetWorkspaceSize(xyz1, xyz2, idx1, idx2, gradDist1, gradDist2, gradXyz1, gradXyz2,
                                                       &workspaceSize, &executor);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnChamferDistanceBackwardGetWorkspaceSize 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 aclnnChamferDistanceBackward.
    ret = aclnnChamferDistanceBackward(workspaceAddr, workspaceSize, executor, stream);
    CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnChamferDistanceBackward 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 memory to host memory. Modify it based on the specific API definition.
    auto size = GetShapeSize(gradXyz1Shape);
    std::vector<float> resultData(size, 0);
    ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), gradXyz1DeviceAddr,
                      size * sizeof(float), 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++) {
        LOG_PRINT("result1[%ld] is: %f\n", i, resultData[i]);
    }

    ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), gradXyz2DeviceAddr,
                      size * sizeof(float), 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++) {
        LOG_PRINT("result2[%ld] is: %f\n", i, resultData[i]);
    }

    // 6. Release aclTensor and aclScalar. Modify it based on the specific API definition.
    aclDestroyTensor(xyz1);
    aclDestroyTensor(xyz2);
    aclDestroyTensor(idx1);
    aclDestroyTensor(idx2);
    aclDestroyTensor(gradDist1);
    aclDestroyTensor(gradDist2);
    aclDestroyTensor(gradXyz1);
    aclDestroyTensor(gradXyz2);

    // 7. Release device resources. Modify it based on the specific API definition.
    aclrtFree(xyz1DeviceAddr);
    aclrtFree(xyz2DeviceAddr);
    aclrtFree(idx1DeviceAddr);
    aclrtFree(idx2DeviceAddr);
    aclrtFree(gradDist1DeviceAddr);
    aclrtFree(gradDist2DeviceAddr);
    aclrtFree(gradXyz1DeviceAddr);
    aclrtFree(gradXyz2DeviceAddr);
    if (workspaceSize > 0) {
        aclrtFree(workspaceAddr);
    }
    aclrtDestroyStream(stream);
    aclrtResetDevice(deviceId);
    aclFinalize();
    return 0;
}