aclnnThreeInterpolateBackward
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 Prototype
Each operator uses a two-phase API. Call the "aclnnThreeInterpolateBackwardGetWorkspaceSize" API first to obtain the workspace size required for computation and the executor that contains the computation process of the operator, and then call the "aclnnThreeInterpolateBackward" API to perform computation.
aclnnStatus aclnnThreeInterpolateBackwardGetWorkspaceSize(
const aclTensor *grad_x,
const aclTensor *idx,
const aclTensor *weight,
int m,
aclTensor *grad_y,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnThreeInterpolateBackward(
void *workspace,
uint64_t workspace_size,
aclOpExecutor *executor,
aclrtStream stream)Function
Description: Compute the gradient based on grad_x, idx, and weight through three-point interpolation to obtain grad_y.
Formula:
aclnnThreeInterpolateBackwardGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor grad_x (aclTensor*) Input Gradient value from the previous step of network backward propagation. Supported shape: (b, c, n) FLOAT, FLOAT16 NCHW is supported (the underlying operator processes data in NC1HWC0/5HD format). - √ idx (aclTensor*) Input Indices of the three nearest-neighbor features of the target feature. Supports shape (b, n, 3). Empty tensors are not supported. INT32, INT64 ND - √ weight (aclTensor*) Input Three nearest-neighbor feature weights of the target feature. Supports shape (b, n, 3). Empty tensors are not supported. FLOAT, FLOAT16 ND - √ m (int) Input Size of the lowest dimension of the input features, used to guide infershape to derive the shape of grad_y. - INT32 - - - grad_y (aclTensor*) Output Gradient calculation result. Supported shape: (b, c, m). FLOAT, FLOAT16 NCHW supported (the underlying operator processes data in NC1HWC0/5HD format). - √ workspaceSize (uint64_t*) Output Returns the size of the workspace to be requested on the device. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the operator computation process. - - - - - Return Value
aclnnStatus: Return status code. For details, see aclnnReturnCode.
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 grad_x, idx, weight, and grad_y are null pointers. ACLNN_ERR_PARAM_INVALID 161002 The data types and data formats of grad_x, idx, weight, m, and grad_y are out of the supported range. When grad_x, idx, weight, and grad_y are not null pointers, their data types, data formats, or shapes are out of the supported range.
aclnnThreeInterpolateBackward
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 aclnnThreeInterpolateBackwardGetWorkspaceSize. executor Input Op executor, which contains the operator computation process. stream Input Stream for task execution. Return Value
aclnnStatus: return status code. For details, see aclnn Return Codes.
Constraints
- The value in idx should be less than m.
- Deterministic computation:
- aclnnThreeInterpolateBackward defaults to a non-deterministic implementation and cannot be configured to enable deterministic computation.
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 "aclnn/aclnn_base.h"
#ifdef __cplusplus
extern "C" {
#endif
/**
* @brief The second-phase API of aclnnThreeInterpolateBackward, used to execute the computation.
*/
aclnnStatus aclnnThreeInterpolateBackward(void *workspace, uint64_t workspaceSize,
aclOpExecutor *executor, aclrtStream stream);
/**
* @brief The first-phase API of aclnnThreeInterpolateBackward, which calculates the workspace size based on the specific computation process.
* @domain aclnn_ops_train
*/
aclnnStatus aclnnThreeInterpolateBackwardGetWorkspaceSize(const aclTensor *grad_x, const aclTensor *idx,
const aclTensor *weight, int m, aclTensor *grad_y,
uint64_t *workspaceSize, aclOpExecutor **executor);
#ifdef __cplusplus
}
#endif
#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<float> 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: %f\n", i, resultData[i]);
}
}
int Init(int32_t deviceId, aclrtStream* stream) {
// Boilerplate for 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, aclFormat format) {
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 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 aclCreateTensor to create an aclTensor.
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, format,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
int main() {
// 1. (boilerplate) Initialize device/stream. For details, see the AscendCL 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);
uint32_t bs = 1;
uint32_t cs = 2;
uint32_t ms = 6;
uint32_t ns = 5;
uint32_t c0 = 16;
auto c1 = (cs + c0 - 1) / c0;
// 2. Construct the input and output based on the API interface definition.
std::vector<int64_t> gradXShape = {bs, cs, ns, 1}; // nchw
std::vector<int64_t> idxShape = {bs, ns, 3}; // nd
std::vector<int64_t> weightShape = {bs, ns, 3}; // nd
std::vector<int64_t> gradYShape = {bs, cs, ms, 1}; // nchw
void* gradXDeviceAddr = nullptr;
void* idxDeviceAddr = nullptr;
void* weightDeviceAddr = nullptr;
void* gradYDeviceAddr = nullptr;
aclTensor* gradX = nullptr;
aclTensor* idx = nullptr;
aclTensor* weight = nullptr;
aclTensor* gradY = nullptr;
std::vector<float> gradXHostData = {1, 1, 1, 1, 1, 1, 1, 1, 1, 1};
std::vector<uint32_t> idxHostData = {0, 3, 2, 3, 5, 1, 0, 3, 0, 0, 3, 5, 0, 4, 1};
std::vector<float> weightHostData = {3.2081969,-0.86573875,-1.0929844,5.6045847,5.328887,-2.0821328,0.98572457,9.612394,2.216394,
1.8722068,4.2976365,-7.019285,-8.4070425,1.0159919,-0.78814566};
std::vector<float> gradYHostData = {0};
ret = CreateAclTensor(gradXHostData, gradXShape, &gradXDeviceAddr, aclDataType::ACL_FLOAT, &gradX, aclFormat::ACL_FORMAT_NCHW);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(idxHostData, idxShape, &idxDeviceAddr, aclDataType::ACL_INT32, &idx, aclFormat::ACL_FORMAT_ND);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(weightHostData, weightShape, &weightDeviceAddr, aclDataType::ACL_FLOAT, &weight, aclFormat::ACL_FORMAT_ND);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(gradYHostData, gradYShape, &gradYDeviceAddr, aclDataType::ACL_FLOAT, &gradY, aclFormat::ACL_FORMAT_NCHW);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Replace it with the actual API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnThreeInterpolateBackward.
ret = aclnnThreeInterpolateBackwardGetWorkspaceSize(gradX, idx, weight, ms, gradY, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnThreeInterpolateBackwardGetWorkspaceSize 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 aclnnThreeInterpolateBackward.
ret = aclnnThreeInterpolateBackward(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnThreeInterpolateBackward 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 value and copy the result from device memory to host memory. Modify them based on the actual API definition.
PrintOutResult(gradYShape, &gradYDeviceAddr);
// 6. Destroy aclTensor and aclScalar. Modify them based on the actual API definition.
aclDestroyTensor(gradX);
aclDestroyTensor(idx);
aclDestroyTensor(weight);
aclDestroyTensor(gradY);
// 7. Release device resources. Modify them based on the actual API definition.
aclrtFree(gradXDeviceAddr);
aclrtFree(idxDeviceAddr);
aclrtFree(weightDeviceAddr);
aclrtFree(gradYDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}