aclnnMseLossBackward
Applicable Products
| Product | Supported |
|---|---|
| √ | |
| √ | |
| √ | |
| × | |
| √ | |
| x |
Function
Description: Backward propagation of the mean squared error function aclnnMseLoss.
Formula:
When reduction is mean:
Where x.numel() represents the number of elements in x. If reduction is not mean, then:
Function Prototype
Each operator is divided into a two-phase API. First call the "aclnnMseLossBackwardGetWorkspaceSize" API to obtain the workspace size required for computation and the executor that contains the operator computation process, and then call the "aclnnMseLossBackward" API to perform the computation.
aclnnStatus aclnnMseLossBackwardGetWorkspaceSize(
const aclTensor* gradOutput,
const aclTensor* self,
const aclTensor* target,
int64_t reduction,
aclTensor* out,
uint64_t* workspaceSize,
aclOpExecutor** executor)
```Cpp
aclnnStatus aclnnMseLossBackward(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)
aclnnMseLossBackwardGetWorkspaceSize
Parameter Description:
Parameter Name Input/Output Description Usage Notes Data Type Data Format Dimensions (shape) Non-Contiguous Tensor gradOutput(aclTensor*) Input Gradient backpropagation input, the input `grad` in the formula. The shape must satisfy the broadcast relationship with self and target. It must be consistent with self. ND 1-8 √ self(aclTensor*) Input Input tensor, the input `x` in the formula. The shape must satisfy the broadcast relationship with gradOutput and target. FLOAT、FLOAT16、BFLOAT16 ND 1-8 √ target(aclTensor*) Input The ground-truth label, the input `y` in the formula. The shape must satisfy the broadcast relationship with gradOutput and self. It must be consistent with self. ND 1-8 √ reduction(int64_t) Input Specifies the reduction to apply to the output, the parameter `reduction` in the formula. Supports 0 ('none') | 1 ('mean') | 2 ('sum').
- 'none': no reduction is applied
- 'mean': the sum of the output is divided by the number of elements in the output
- 'sum': the output is summedINT64 - - √ out(aclTensor*) Output The output loss, the output `MselossBackward(grad, x, y)` in the formula. The shape of out is consistent with the tensor after broadcast with gradOutput, self, and target.
The data type is the deductible data type of self (see conversion relationship).FLOAT、FLOAT16、BFLOAT16 ND 1-8 √ workspaceSize(uint64_t*) Output Returns the workspace size to be allocated on the Device side. - - - - - executor(aclOpExecutor**) Output Returns the op executor, which contains the operator computation flow. - - - - - Return Value:
aclnnStatus: Returns the status code. For details, see aclnn Return Code.
The first-phase API completes input validation. The following scenarios trigger an error:
Return Value Error Code Description ACLNN_ERR_PARAM_NULLPTR 161001 When the passed gradOutput, self, target, or out is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of self is not within the supported range. The data types of gradOutput and target are different from that of self. The shape of gradOutput, self, and target cannot be broadcast. The shape after broadcast of gradOutput, self, and target is inconsistent with the shape of out. The value of reduction is not within the range 0 to 2. The shape of gradOutput, self, or target exceeds 8 dimensions.
aclnnMseLossBackward
Parameter Description:
Parameter Name Input/Output Description workspace Input The workspace memory address allocated on the Device side. workspaceSize Input The workspace size allocated on the Device side, obtained by the first-phase API aclnnMseLossBackwardGetWorkspaceSize. executor Input The op executor, which contains the operator computation flow. stream Input Specifies the Stream that executes the task. Return Value:
aclnnStatus: Returns the status code. For details, see aclnn Return Code.
Constraints
- Deterministic computation:
- aclnnMseLossBackward uses a deterministic implementation by default.
Example
The sample code is as follows, for reference only. For the specific compilation and execution process, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_mse_loss_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 apply for on-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 on-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. 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);
// 2. Construct inputs and outputs. Customize the construction of gradOutput based on the API.
std::vector<int64_t> gradOutputShape = {2, 2};
std::vector<int64_t> selfShape = {2, 2};
std::vector<int64_t> targetShape = {2, 2};
std::vector<int64_t> outShape = {2, 2};
void* gradOutputDeviceAddr = nullptr;
void* selfDeviceAddr = nullptr;
void* targetDeviceAddr = nullptr;
void* outDeviceAddr = nullptr;
aclTensor* gradOutput = nullptr;
aclTensor* self = nullptr;
aclTensor* target = nullptr;
aclTensor* out = nullptr;
std::vector<float> gradOutputHostData = {0, 1, 2, 3};
std::vector<float> selfHostData = {0, 1, 2, 3};
std::vector<float> targetHostData = {1, 1, 1, 1};
std::vector<float> outHostData(4, 0);
// Create gradOutput aclTensor.
ret = CreateAclTensor(gradOutputHostData, gradOutputShape, &gradOutputDeviceAddr,
aclDataType::ACL_FLOAT, &gradOutput);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create self aclTensor.
ret = CreateAclTensor(selfHostData, selfShape, &selfDeviceAddr, aclDataType::ACL_FLOAT, &self);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create target aclTensor.
ret = CreateAclTensor(targetHostData, targetShape, &targetDeviceAddr, aclDataType::ACL_FLOAT, &target);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create out aclTensor.
ret = CreateAclTensor(outHostData, outShape, &outDeviceAddr, aclDataType::ACL_FLOAT, &out);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create reduction.
int64_t reduction = 1;
// 3. Call the CANN operator library API. Replace with the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnMseLossBackward.
ret = aclnnMseLossBackwardGetWorkspaceSize(gradOutput, self, target, reduction, out, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnMseLossBackwardGetWorkspaceSize failed. ERROR: %d\n", ret);
return ret);
// Apply for 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 aclnnMseLossBackward.
ret = aclnnMseLossBackward(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnMseLossBackward failed. ERROR: %d\n", ret); return ret);
// 4. (Boilerplate) Synchronize and wait for task completion.
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 on-device memory to the host side. Modify based on the specific API interface 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 aclScalar. Modify based on the specific API definition.
aclDestroyTensor(gradOutput);
aclDestroyTensor(self);
aclDestroyTensor(target);
aclDestroyTensor(out);
// 7. Release device resources. Modify based on the specific API definition.
aclrtFree(gradOutputDeviceAddr);
aclrtFree(selfDeviceAddr);
aclrtFree(targetDeviceAddr);
aclrtFree(outDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}