aclnnUpsampleBicubic2dBackward
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 : Supported
Function
Description: backward propagation of aclnnUpsampleBicubic2d. If the input tensor shape is (N, C, H, W), the output tensor shape is (N, C, inputSize[2], inputSize[3]).
Formula: For a 2D interpolation point , the interpolation can be expressed as:
Where:
- alignCorners = true means the corner pixels of the input and output tensors are aligned; alignCorners = false means the edge pixels of the input and output tensors are aligned.
- i and j are the index variables of .
- is the pixel value of gradOut at .
- is the weight of bicubic anti-aliasing interpolation, defined as:
Where:
Function Prototype
Each operator uses a two-phase API. First call the "aclnnUpsampleBicubic2dBackwardGetWorkspaceSize" API to obtain the input parameters and calculate the required workspace size based on the computation flow. Then call the "aclnnUpsampleBicubic2dBackward" API to execute the computation.
aclnnStatus aclnnUpsampleBicubic2dBackwardGetWorkspaceSize(
const aclTensor* gradOut,
const aclIntArray* outputSize,
const aclIntArray* inputSize,
const bool alignCorners,
double scalesH,
double scalesW,
aclTensor* gradInput,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnUpsampleBicubic2dBackward(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnUpsampleBicubic2dBackwardGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor gradOut (aclTensor*) Input Indicates the gradient tensor of backward propagation, corresponding to `gradOut` in the formula description. - Empty tensors are not supported.
- The data type is the same as that of `gradInput`.
- When the data format is ND, the tensor is processed as NCHW by default.
FLOAT32, FLOAT16, BFLOAT16 NCHW, ND, NHWC 4 √ outputSize (aclIntArray*) Input Indicates the spatial size of the input `gradOut` on the H and W dimensions. Corresponds to `outputSize` in the formula. size is 2, and each element is greater than 0. INT64 - - - inputSize (aclIntArray*) Input Indicates the spatial sizes of the output `gradInput` on the N, C, H, W or N, H, W, C dimensions, respectively. Corresponds to `inputSize` in the formula. size is 4, and each element is greater than 0. INT64 - - - alignCorners (bool) Input Determines whether to align corner pixels, corresponding to `alignCorners` in the formula. If True, the corner pixels of the input and output tensors are aligned; otherwise, they are not aligned. - - - - scalesH (double) Input Indicates the multiplier on the height dimension of the output `gradInput`, corresponding to `scalesH` in the formula. - - - - - scalesW (double) Input Indicates the width dimension multiplier of the output `gradInput`, corresponding to `scalesW` in the formula. - - - - - gradInput (aclTensor*) Output Indicates the output tensor of the backward computation, corresponding to `gradInput` in the formula. - Empty tensors are not supported.
- The data type and data format are the same as those of `gradOut`.
- The N and C dimensions of shape are the same as those of `gradOut`.
FLOAT32, FLOAT16, BFLOAT16 NCHW, ND, NHWC 4 √ workspaceSize (uint64_t*) Output Returns the workspace size to be applied on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the op executor, which contains the operator computation process. - - - - - Atlas training products :- The parameters
gradOutandgradInputdo not support the BFLOAT16 data type. - The data format of parameters
gradOutandgradInputdoes not support NHWC.
- The parameters
Atlas A2 products ,Atlas A3 products :The data format of parameters
gradOutandgradInputdoes not support NHWC.
Return Value
aclnnStatus: return status code. For details, see aclnn Error 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 gradOut, outputSize, inputSize, or gradInput is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type and data format of gradOut are not within the supported range. The data types of gradOut and gradInput are inconsistent. The number of dimensions of gradOut is not 4. The size of outputSize is not 2. The value of an element in outputSize is less than 1. The size of inputSize is not 4. The value of an element in inputSize is less than 1. The sizes of gradOut and inputSize on the N and C dimensions are different. The size of gradOut on the H and W dimensions is inconsistent with outputSize[0] and outputSize[1]. The sizes of gradOut and gradInput on the N/C axes are not equal. The data format of gradOut and gradInput is out of the supported range.
aclnnUpsampleBicubic2dBackward
Parameters
Parameter Input/Output Description workspace Input Workspace memory address applied on the Device side. workspaceSize Input Workspace size applied on the Device side, obtained by the first-phase API aclnnUpsampleBicubic2dBackwardGetWorkspaceSize. executor Input op executor, which contains the operator computation flow. stream Input Specifies the Stream for task execution. Return Value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
Atlas A3 products ,Atlas A2 products :- All dimension values of gradOut and gradInput are less than or equal to (2^31-1).
- <= 2^31-1
Where:
- N represents the N axis of the input and output.
- C represents the C axis of the input and output.
Parameters inputSize, outputSize, scalesH, and scalesW must satisfy the following constraints:
Deterministic computation:
aclnnUpsampleBicubic2dBackward defaults to a non-deterministic implementation. Deterministic computation can be enabled through aclrtCtxSetSysParamOpt. Deterministic computation is not supported when the following conditions are met:
- > 130000
- / >=50
- / * * > * 0.5
- / < 0.02 && / < 0.02 && * * > * 10000
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_upsample_bicubic_2d_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 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 data from the host to the 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_NCHW,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
int main()
{
// 1. (Boilerplate) Initialize device/stream. For details, see the acl API manual.
// 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 based on the API.
std::vector<int64_t> selfShape = {1, 1, 2, 2};
std::vector<int64_t> outShape = {1, 1, 3, 3};
void* selfDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* self = nullptr;
aclTensor* out = nullptr;
std::vector<float> selfHostData = {1, 2, 3, 4.1};
std::vector<float> outHostData = {0, 0, 0, 0, 0, 0, 0, 0, 0};
// Create the self aclTensor.
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
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);
std::vector<int64_t> outArraySize = {2, 2};
const aclIntArray* outputSize = aclCreateIntArray(outArraySize.data(), outArraySize.size());
CHECK_RET(outputSize != nullptr, return ACL_ERROR_INTERNAL_ERROR);
std::vector<int64_t> inputArraySize = {1, 1, 3, 3};
const aclIntArray* inputSize = aclCreateIntArray(inputArraySize.data(), inputArraySize.size());
CHECK_RET(inputSize != nullptr, return ACL_ERROR_INTERNAL_ERROR);
// 3. Call the CANN operator library API. Replace it with the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
const double scalesH = 1.1;
const double scalesW = 1.1;
// Call the first-phase API of aclnnUpsampleBicubic2dBackward.
ret = aclnnUpsampleBicubic2dBackwardGetWorkspaceSize(self, outputSize, inputSize, 1, scalesH, scalesW, out, &workspaceSize,
&executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnUpsampleBicubic2dBackwardGetWorkspaceSize failed. ERROR: %d\n", ret);
return ret);
// Allocate device memory based on 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 aclnnUpsampleBicubic2dBackward
ret = aclnnUpsampleBicubic2dBackward(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnUpsampleBicubic2dBackward failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Wait synchronously 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 the device memory to the host. Modify the code based on the 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 < size; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]);
}
// 6. Release the aclTensor. Modify the code based on the API definition.
aclDestroyTensor(self);
aclDestroyTensor(out);
aclDestroyIntArray(outputSize);
aclDestroyIntArray(inputSize);
// 7. Release device resources. Modify the code based on the API definition.
aclrtFree(selfDeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}