aclnnUpsampleBicubic2dAAGrad
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: Backward propagation of aclnnUpsampleBicubic2dAA. If the input tensor has a shape of (N, C, H, W), the output tensor has a shape of (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 gradOutput at .
- is the weight of bicubic anti-aliasing interpolation, defined as:
Where:
Function Prototype
Each operator uses a two-phase API. Call "aclnnUpsampleBicubic2dAAGradGetWorkspaceSize" first to obtain the workspace size required for computation and the executor containing the operator computation flow, and then call "aclnnUpsampleBicubic2dAAGrad" to execute the computation.
aclnnStatus aclnnUpsampleBicubic2dAAGradGetWorkspaceSize(
const aclTensor *gradOutput,
const aclIntArray *outputSize,
const aclIntArray *inputSize,
bool alignCorners,
double scalesH,
double scalesW,
aclTensor *out,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnUpsampleBicubic2dAAGrad(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnUpsampleBicubic2dAAGradGetWorkspaceSize
Parameters
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor gradOutput (aclTensor*) Input Indicates the gradient tensor for backward computation, corresponding to `gradOutput` in the formula description. - Empty tensors are not supported.
- When the data format is ND, it is processed in the NCHW format by default.
FLOAT32, FLOAT16, BFLOAT16 NCHW, ND 4 √ outputSize (aclIntArray*) Input Indicates the spatial sizes of the input `gradOutput` in the H and W dimensions. Corresponds to `outputSize` in the formula. size is 2, and each element is greater than zero. INT64 - - - inputSize (aclIntArray*) Input Indicates the spatial sizes of output `out` in the N, C, H, and W dimensions. Corresponds to `inputSize` in the formula. size is 4, and each element is greater than zero. INT64 - - - alignCorners (bool) Input Determines whether to align corner pixels, corresponding to `alignCorners` in the formula. If alignCorners is True, the corner pixels of the input and output tensors are aligned. Otherwise, they are not aligned. - - - - scalesH (double) Input Indicates the multiplier of the height dimension of output `out`, corresponding to `scalesH` in the corresponding formula. Negative values are not allowed. - - - - scalesW (double) Input Indicates the multiplier of the width dimension of the output `out`, corresponding to `scalesW` in the formula. Negative values are not supported. - - - - out (aclTensor*) Output Indicates the output tensor of backward computation, corresponding to `gradInput` in the formula. - Empty tensors are not supported.
- The data type and data format must be the same as those of `gradOutput`.
FLOAT32, FLOAT16, BFLOAT16 NCHW, ND 4 √ workspaceSize (uint64_t*) Output Returns the size of the workspace to be requested on the Device. - - - - - executor (aclOpExecutor**) Output Op executor containing the operator computation flow. - - - - - 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 The passed-in gradOutput, inputSize, or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of gradOutput or out is not supported. The data types of gradOutput and out are inconsistent. The shape of gradOutput is not 4-dimensional. The value of the H or W dimension of inputSize is less than 1. The size of inputSize is not 4. The size of outputSize is not 2. The value of scalesH or scalesW is negative.
aclnnUpsampleBicubic2dAAGrad
Parameters
Parameter Input/Output Description workspace Input Address of the workspace memory allocated on the Device side. workspaceSize Input Size of the workspace memory allocated on the Device side, obtained by the first-phase API aclnnUpsampleBicubic2dAAGradGetWorkspaceSize. executor Input Op executor that contains the operator computation flow. stream Input Stream used to execute the task. Return Value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
- Shape constraints for parameters
gradOutputandout:The value of each dimension is less than or equal to 2^20.
The N and C axes of parameter
outare the same as those ofgradOutput.The memory usage must satisfy the following conditions:
Where:
- N represents the N axis of the input and output.
- C represents the C axis of the input and output.
- dtype represents the data type of the input tensor.
N * C * gradOutput_H < 2^31
Atlas A3 products andAtlas A2 products :The downscaling factor of the input data for the backward API must be less than or equal to 50, that is:
Parameters inputSize, outputSize, scalesH, and scalesW must satisfy the following constraints:
Deterministic computation:
Atlas A3 products ,Atlas A2 products : aclnnUpsampleBicubic2dAAGrad defaults to a deterministic implementation.
Ascend 950PR&950DT products : aclnnUpsampleBicubic2dAAGrad defaults to a non-deterministic implementation. You can enable deterministic computation through 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_upsample_bicubic2d_aa_grad.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 CreateAclNchTensor(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 request 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-side data to 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_NCHW,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
int main()
{
// 1. (Boilerplate) device/stream initialization. Refer to 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("aclInit failed. ERROR: %d\n", ret); return ret);
// 2. Construct the input and output. Customize the construction based on the API.
std::vector<int64_t> inputShape = {1, 1, 4, 2};
std::vector<int64_t> outShape = {1, 1, 8, 4};
void* inputDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* input = nullptr;
aclTensor* out = nullptr;
std::vector<float> inputHostData = {0, 1, 2, 3, 4, 5, 6, 7};
const size_t kSize32 = 32U;
std::vector<float> outHostData(kSize32, 0);
const size_t kSize4 = 4U;
std::vector<int64_t> outputSize = {kSize4, 2};
std::vector<int64_t> inputSize = {1, 1, 8, 4};
bool alignCorners = true;
double scalesH = 0.5;
double scalesW = 0.5;
// Create the input aclTensor.
ret = CreateAclNchTensor(inputHostData, inputShape, &inputDeviceAddr, aclDataType::ACL_FLOAT, &input);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the input aclIntArray.
auto outputSizeArray = aclCreateIntArray(outputSize.data(), 2);
auto inputSizeArray = aclCreateIntArray(inputSize.data(), 4);
// Create the out aclTensor.
ret = CreateAclNchTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API. Replace it with the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnUpsampleBicubic2dAAGrad.
ret = aclnnUpsampleBicubic2dAAGradGetWorkspaceSize(input, outputSizeArray, inputSizeArray, alignCorners, scalesH,
scalesW, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnUpsampleBicubic2dAAGradGetWorkspaceSize 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 aclnnUpsampleBicubic2dAAGrad
ret = aclnnUpsampleBicubic2dAAGrad(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnUpsampleBicubic2dAAGrad 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 values and copy the results from device memory to host memory. Modify 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 < size; i++) {
LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]);
}
// 6. Release aclTensor and aclIntArray. Modify based on the specific API definition.
aclDestroyTensor(input);
aclDestroyTensor(out);
// 7. Release device resources. Modify based on the specific API definition.
aclrtFree(inputDeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}