aclnnApplyAdamW
Applicable Products
Ascend 950PR&950DT products : Supported
Atlas A3 products : Not supported
Atlas A2 products : Not supported
Atlas 200I/500 A2 inference products : Not supported
Atlas inference products : Not supported
Atlas training products : Not supported
Function
Description: Implements the AdamW optimizer function.
Formula:
Function Prototype
Each operator uses a two-phase API. Call "aclnnApplyAdamWGetWorkspaceSize" first to obtain the workspace size required for the computation and the executor that contains the operator computation flow, and then call "aclnnApplyAdamW" to execute the computation.
aclnnStatus aclnnApplyAdamWGetWorkspaceSize(
aclTensor* varRef,
aclTensor* mRef,
aclTensor* vRef,
const aclTensor* beta1Power,
const aclTensor* beta2Power,
const aclTensor* lr,
const aclTensor* weightDecay,
const aclTensor* beta1,
const aclTensor* beta2,
const aclTensor* eps,
const aclTensor* grad,
const aclTensor* maxGradNormOptional,
bool amsgrad,
bool maximize,
uint64_t* workspaceSize,
aclOpExecutor** executor)aclnnStatus aclnnApplyAdamW(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnApplyAdamWGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor varRef (aclTensor*) Input/Output Weight input to be calculated, which is also the output, corresponding to θ in the formula. FLOAT16, BFLOAT16, FLOAT32 ND 1-8 √ mRef (aclTensor*) Input/Output The m parameter in the AdamW optimizer, corresponding to m in the formula. The shape must be the same as that of the input varRef. Same as varRef ND 1-8 √ vRef (aclTensor*) Input/Output The v parameter in the AdamW optimizer, corresponding to v in the formula. The shape must be the same as that of input varRef. Same as varRef ND 1-8 √ beta1Power (aclTensor*) Input beta1^(t-1) parameter. The shape must be [1]. Consistent with varRef. ND 1 √ beta2Power (aclTensor*) Input beta2^(t-1) parameter. The shape must be [1]. Consistent with varRef. ND 1 √ lr (aclTensor*) Input Learning rate, that is, η in the formula. Typically 1e-3, 1e-6, or 1e-9. The shape must be [1]. Consistent with varRef. ND 1 √ weightDecay (aclTensor*) Input Weight decay coefficient. The shape must be [1]. Same as varRef. ND 1 √ beta1 (aclTensor*) Input beta1 parameter. The shape must be [1]. Same as varRef. ND 1 √ beta2 (aclTensor*) Input beta2 parameter. The shape must be [1]. Consistent with varRef ND 1 √ eps (aclTensor*) Input Prevents division by zero. The shape must be [1]. Same as varRef ND 1 √ grad (aclTensor*) Input Gradient data, which is g in the formula. The shape must be the same as that of input varRef. Same as varRef ND 1-8 √ maxGradNormOptional(aclTensor*) Input If amsgrad=true, saves the maximum value of maxGradNorm and v in vRef. If amsgrad=false, no additional computation is performed on the value in vRef. This parameter is mandatory when amsgrad is true and optional when amsgrad is false. The shape must be the same as that of input varRef. Consistent with varRef. ND 1-8 √ amsgrad (bool) Input Whether to use maxGradNormOptional. - bool - - - maximize (bool) Input Specifies whether to negate the gradient grad and apply gradient ascent to optimize the weights to maximize the loss function. - bool - - - 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 computation process of the operator. - - - - - 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 calculation input parameter is a null pointer. When amsgrad is true, maxGradNormOptional is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 The data type of the calculation input is out of the supported range. The data types of calculation inputs are inconsistent. The shapes of calculation inputs are inconsistent. When amsgrad is true, the data type or shape of maxGradNormOptional is inconsistent with that of varRef. The shape of beta1Power, beta2Power, lr, weightDecay, beta1, beta2, or eps is not 1.
aclnnApplyAdamW
Parameters:
Parameter Input/Output Description workspace Input Workspace memory address allocated on the Device side. workspaceSize Input Size of the workspace allocated on the Device side, obtained by calling the first-phase API aclnnApplyAdamWGetWorkspaceSize. executor Input Op executor that contains the operator computation flow. stream Input Specifies the stream that executes the task. Return value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
- Deterministic computation:
- aclnnApplyAdamW defaults to a deterministic implementation.
Example
The sample code is as follows 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_apply_adam_w.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 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) {
auto size = GetShapeSize(shape) * sizeof(T);
// Call aclrtMalloc to allocate memory on the Device side.
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-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_ND,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
int main() {
// 1. (Boilerplate) Initialize the 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);
// Handle the Error Code check as needed.
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("Init acl failed. ERROR: %d\n", ret); return ret);
// 2. Construct the inputs and outputs. Customize the construction based on the API.
std::vector<int64_t> varShape = {2, 2};
std::vector<int64_t> mShape = {2, 2};
std::vector<int64_t> vShape = {2, 2};
std::vector<int64_t> beta1PowerShape = {1};
std::vector<int64_t> beta2PowerShape = {1};
std::vector<int64_t> lrShape = {1};
std::vector<int64_t> weightDecayShape = {1};
std::vector<int64_t> beta1Shape = {1};
std::vector<int64_t> beta2Shape = {1};
std::vector<int64_t> epsShape = {1};
std::vector<int64_t> gradShape = {2, 2};
std::vector<int64_t> maxgradShape = {2, 2};
void* varDeviceAddr = nullptr;
void* mDeviceAddr = nullptr;
void* vDeviceAddr = nullptr;
void* beta1PowerDeviceAddr = nullptr;
void* beta2PowerDeviceAddr = nullptr;
void* lrDeviceAddr = nullptr;
void* weightDecayDeviceAddr = nullptr;
void* beta1DeviceAddr = nullptr;
void* beta2DeviceAddr = nullptr;
void* epsDeviceAddr = nullptr;
void* gradDeviceAddr = nullptr;
void* maxgradDeviceAddr = nullptr;
aclTensor* var = nullptr;
aclTensor* m = nullptr;
aclTensor* v = nullptr;
aclTensor* beta1Power = nullptr;
aclTensor* beta2Power = nullptr;
aclTensor* lr = nullptr;
aclTensor* weightDecay = nullptr;
aclTensor* beta1 = nullptr;
aclTensor* beta2 = nullptr;
aclTensor* eps = nullptr;
aclTensor* grad = nullptr;
aclTensor* maxgrad = nullptr;
std::vector<float> varHostData = {0, 1, 2, 3};
std::vector<float> mHostData = {0, 1, 2, 3};
std::vector<float> vHostData = {0, 1, 2, 3};
std::vector<float> beta1PowerHostData = {0.431};
std::vector<float> beta2PowerHostData = {0.992};
std::vector<float> lrHostData = {0.001};
std::vector<float> weightDecayHostData = {0.01};
std::vector<float> beta1HostData = {0.9};
std::vector<float> beta2HostData = {0.999};
std::vector<float> epsHostData = {1e-8};
std::vector<float> gradHostData = {0, 1, 2, 3};
std::vector<float> maxgradHostData = {0, 1, 2, 3};
bool amsgrad = true;
bool maximize = true;
// Create the var aclTensor.
ret = CreateAclTensor(varHostData, varShape, &varDeviceAddr, aclDataType::ACL_FLOAT, &var);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the m aclTensor.
ret = CreateAclTensor(mHostData, mShape, &mDeviceAddr, aclDataType::ACL_FLOAT, &m);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the v aclTensor.
ret = CreateAclTensor(vHostData, vShape, &vDeviceAddr, aclDataType::ACL_FLOAT, &v);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the beta1Power aclTensor.
ret = CreateAclTensor(beta1PowerHostData, beta1PowerShape, &beta1PowerDeviceAddr, aclDataType::ACL_FLOAT, &beta1Power);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the beta2Power aclTensor.
ret = CreateAclTensor(beta2PowerHostData, beta2PowerShape, &beta2PowerDeviceAddr, aclDataType::ACL_FLOAT, &beta2Power);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the lr aclTensor.
ret = CreateAclTensor(lrHostData, lrShape, &lrDeviceAddr, aclDataType::ACL_FLOAT, &lr);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the weightDecay aclTensor.
ret = CreateAclTensor(weightDecayHostData, weightDecayShape, &weightDecayDeviceAddr, aclDataType::ACL_FLOAT, &weightDecay);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the beta1 aclTensor.
ret = CreateAclTensor(beta1HostData, beta1Shape, &beta1DeviceAddr, aclDataType::ACL_FLOAT, &beta1);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the beta2 aclTensor.
ret = CreateAclTensor(beta2HostData, beta2Shape, &beta2DeviceAddr, aclDataType::ACL_FLOAT, &beta2);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the eps aclTensor.
ret = CreateAclTensor(epsHostData, epsShape, &epsDeviceAddr, aclDataType::ACL_FLOAT, &eps);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the grad aclTensor.
ret = CreateAclTensor(gradHostData, gradShape, &gradDeviceAddr, aclDataType::ACL_FLOAT, &grad);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// Create the maxgrad aclTensor.
ret = CreateAclTensor(maxgradHostData, maxgradShape, &maxgradDeviceAddr, aclDataType::ACL_FLOAT, &maxgrad);
CHECK_RET(ret == ACL_SUCCESS, return ret);
// 3. Call the CANN operator library API, and replace it with the specific API name.
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
// Call the first-phase API of aclnnApplyAdamW.
ret = aclnnApplyAdamWGetWorkspaceSize(var, m, v, beta1Power, beta2Power, lr, weightDecay, beta1, beta2, eps, grad, maxgrad, amsgrad, maximize, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnApplyAdamWGetWorkspaceSize 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 aclnnApplyAdamW.
ret = aclnnApplyAdamW(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnApplyAdamW 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 device memory to host memory. Modify it based on the specific API definition.
auto size = GetShapeSize(varShape);
std::vector<float> resultData(size, 0);
ret = aclrtMemcpy(resultData.data(), resultData.size() * sizeof(resultData[0]), varDeviceAddr, 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. Modify it based on the specific API definition.
aclDestroyTensor(var);
aclDestroyTensor(m);
aclDestroyTensor(v);
aclDestroyTensor(beta1Power);
aclDestroyTensor(beta2Power);
aclDestroyTensor(lr);
aclDestroyTensor(weightDecay);
aclDestroyTensor(beta1);
aclDestroyTensor(beta2);
aclDestroyTensor(eps);
aclDestroyTensor(grad);
aclDestroyTensor(maxgrad);
// 7. Release device resources.
aclrtFree(varDeviceAddr);
aclrtFree(mDeviceAddr);
aclrtFree(vDeviceAddr);
aclrtFree(beta1PowerDeviceAddr);
aclrtFree(beta2PowerDeviceAddr);
aclrtFree(lrDeviceAddr);
aclrtFree(weightDecayDeviceAddr);
aclrtFree(beta1DeviceAddr);
aclrtFree(beta2DeviceAddr);
aclrtFree(epsDeviceAddr);
aclrtFree(gradDeviceAddr);
aclrtFree(maxgradDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}