aclnnApplyAdam
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 Adam optimizer functionality.
Formula:
$$ lr_t = lr \times \frac{\sqrt{1 - \beta_2^t}}{1 - \beta_1^t} $$$$ m_t = \beta_1 \times m_{t-1} + (1 - \beta_1) \times g_t $$$$ v_t = \beta_2 \times v_{t-1} + (1 - \beta_2) \times g_t^2 $$If use_nesterov = true:
$ var_t = var_{t-1} - lr_t \times \frac{\beta_1 \times m_t + (1 - \beta_1) \times g_t}{\sqrt{v_t} + \epsilon} $If use_nesterov = false:
$ var_t = var_{t-1} - lr_t \times \frac{m_t}{\sqrt{v_t} + \epsilon} $
Function Prototype
Each operator uses a two-phase API. First, call the "aclnnApplyAdamGetWorkspaceSize" API to obtain the workspace size required for the computation and the executor containing the operator computation process. Then, call the "aclnnApplyAdam" API to perform the computation.
aclnnStatus aclnnApplyAdamGetWorkspaceSize(
aclTensor *varRef,
const aclTensor *m,
const aclTensor *v,
const aclTensor *beta1Power,
const aclTensor *beta2Power,
const aclTensor *lr,
const aclTensor *beta1,
const aclTensor *beta2,
const aclTensor *epsilon,
const aclTensor *grad,
bool useLocking,
bool useNesterov,
uint64_t *workspaceSize,
aclOpExecutor **executor)aclnnStatus aclnnApplyAdam(
void *workspace,
uint64_t workspaceSize,
aclOpExecutor *executor,
aclrtStream stream)aclnnApplyAdamGetWorkspaceSize
Parameters:
Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor varRef(aclTensor*) Input/Output Weight to be computed, serving as both input and output, var in the formula. FLOAT16, BFLOAT16, FLOAT32 ND 1-8 √ m (aclTensor*) Input m parameter in the Adam optimizer, m in the formula. shape requirements and input varRef remain consistent. Remains consistent with varRef ND 1-8 √ v (aclTensor*) Input v parameter in the Adam optimizer, which is v in the formula. Shape requirements and input varRef remain consistent. Remains consistent with varRef ND 1-8 √ beta1Power (aclTensor*) Input t-th power of beta1. shape is required to be [1]. FLOAT32 ND 1 √ beta2Power (aclTensor*) Input The t-th power of beta2. The shape is required to be [1]. FLOAT32 ND 1 √ lr (aclTensor*) Input Learning rate, lr in the formula. The shape is required to be [1]. FLOAT32 ND 1 √ beta1 (aclTensor*) Input beta1 parameter. The shape is required to be [1]. FLOAT32 ND 1 √ beta2 (aclTensor*) Input beta2 parameter. The shape is required to be [1]. FLOAT32 ND 1 √ epsilon (aclTensor*) Input Prevents division by zero. shape is required to be [1]. FLOAT32 ND 1 √ grad (aclTensor*) Input Gradient data, g in the formula. The shape requirements and input varRef remain consistent. Remain consistent with varRef ND 1-8 √ useLocking (bool) Input Whether to use the lock mechanism. - bool - - - useNesterov (bool) Input Whether to use Nesterov momentum. - bool - - - workspaceSize (uint64_t*) Output Returns the size of the workspace required on the Device side. - - - - - executor (aclOpExecutor**) Output Returns the op executor that contains 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 Returned when the incoming computation input parameter is a null pointer. ACLNN_ERR_PARAM_INVALID 161002 Returned when the data type of the incoming computation input is not within the supported range. The data types of the incoming computation inputs are inconsistent. The shapes of the incoming computation inputs are inconsistent. The data types of beta1Power, beta2Power, lr, beta1, beta2, and epsilon are not FLOAT32. The shape sizes of beta1Power, beta2Power, lr, beta1, beta2, and epsilon are not 1.
aclnnApplyAdam
Parameters:
Parameter Input/Output Description workspace Input Memory address of the workspace requested by the Device. workspaceSize Input Size of the workspace requested by the Device, obtained by calling the first-phase API aclnnApplyAdamGetWorkspaceSize. executor Input Op executor, which contains the operator computation flow. stream Input Specifies the stream for executing the task. Return value
aclnnStatus: return status code. For details, see aclnn Error Codes.
Constraints
- Deterministic computation:
- aclnnApplyAdam defaults to a deterministic implementation.
Example
The sample code is as follows and is for reference only. For details about how to compile and run, see Compile and Run Samples.
#include <iostream>
#include <vector>
#include "acl/acl.h"
#include "aclnnop/aclnn_apply_adam.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) {
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);
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);
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);
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];
}
*tensor = aclCreateTensor(shape.data(), shape.size(), dataType, strides.data(), 0, aclFormat::ACL_FORMAT_ND,
shape.data(), shape.size(), *deviceAddr);
return 0;
}
int main() {
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);
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> beta1Shape = {1};
std::vector<int64_t> beta2Shape = {1};
std::vector<int64_t> epsilonShape = {1};
std::vector<int64_t> gradShape = {2, 2};
void* varDeviceAddr = nullptr;
void* mDeviceAddr = nullptr;
void* vDeviceAddr = nullptr;
void* beta1PowerDeviceAddr = nullptr;
void* beta2PowerDeviceAddr = nullptr;
void* lrDeviceAddr = nullptr;
void* beta1DeviceAddr = nullptr;
void* beta2DeviceAddr = nullptr;
void* epsilonDeviceAddr = nullptr;
void* gradDeviceAddr = nullptr;
aclTensor* var = nullptr;
aclTensor* m = nullptr;
aclTensor* v = nullptr;
aclTensor* beta1Power = nullptr;
aclTensor* beta2Power = nullptr;
aclTensor* lr = nullptr;
aclTensor* beta1 = nullptr;
aclTensor* beta2 = nullptr;
aclTensor* epsilon = nullptr;
aclTensor* grad = 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> beta1HostData = {0.9};
std::vector<float> beta2HostData = {0.999};
std::vector<float> epsilonHostData = {1e-8};
std::vector<float> gradHostData = {0, 1, 2, 3};
bool useLocking = false;
bool useNesterov = false;
ret = CreateAclTensor(varHostData, varShape, &varDeviceAddr, aclDataType::ACL_FLOAT, &var);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(mHostData, mShape, &mDeviceAddr, aclDataType::ACL_FLOAT, &m);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(vHostData, vShape, &vDeviceAddr, aclDataType::ACL_FLOAT, &v);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(beta1PowerHostData, beta1PowerShape, &beta1PowerDeviceAddr, aclDataType::ACL_FLOAT, &beta1Power);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(beta2PowerHostData, beta2PowerShape, &beta2PowerDeviceAddr, aclDataType::ACL_FLOAT, &beta2Power);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(lrHostData, lrShape, &lrDeviceAddr, aclDataType::ACL_FLOAT, &lr);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(beta1HostData, beta1Shape, &beta1DeviceAddr, aclDataType::ACL_FLOAT, &beta1);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(beta2HostData, beta2Shape, &beta2DeviceAddr, aclDataType::ACL_FLOAT, &beta2);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(epsilonHostData, epsilonShape, &epsilonDeviceAddr, aclDataType::ACL_FLOAT, &epsilon);
CHECK_RET(ret == ACL_SUCCESS, return ret);
ret = CreateAclTensor(gradHostData, gradShape, &gradDeviceAddr, aclDataType::ACL_FLOAT, &grad);
CHECK_RET(ret == ACL_SUCCESS, return ret);
uint64_t workspaceSize = 0;
aclOpExecutor* executor;
ret = aclnnApplyAdamGetWorkspaceSize(var, m, v, beta1Power, beta2Power, lr, beta1, beta2, epsilon, grad, useLocking, useNesterov, &workspaceSize, &executor);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnApplyAdamGetWorkspaceSize failed. ERROR: %d\n", ret); return ret);
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);
}
ret = aclnnApplyAdam(workspaceAddr, workspaceSize, executor, stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclnnApplyAdam failed. ERROR: %d\n", ret); return ret);
ret = aclrtSynchronizeStream(stream);
CHECK_RET(ret == ACL_SUCCESS, LOG_PRINT("aclrtSynchronizeStream failed. ERROR: %d\n", ret); return ret);
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]);
}
aclDestroyTensor(var);
aclDestroyTensor(m);
aclDestroyTensor(v);
aclDestroyTensor(beta1Power);
aclDestroyTensor(beta2Power);
aclDestroyTensor(lr);
aclDestroyTensor(beta1);
aclDestroyTensor(beta2);
aclDestroyTensor(epsilon);
aclDestroyTensor(grad);
aclrtFree(varDeviceAddr);
aclrtFree(mDeviceAddr);
aclrtFree(vDeviceAddr);
aclrtFree(beta1PowerDeviceAddr);
aclrtFree(beta2PowerDeviceAddr);
aclrtFree(lrDeviceAddr);
aclrtFree(beta1DeviceAddr);
aclrtFree(beta2DeviceAddr);
aclrtFree(epsilonDeviceAddr);
aclrtFree(gradDeviceAddr);
if (workspaceSize > 0) {
aclrtFree(workspaceAddr);
}
aclrtDestroyStream(stream);
aclrtResetDevice(deviceId);
aclFinalize();
return 0;
}