aclnnFusedSgd

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: Implements the fusedSgd operator. Fuses multiple fine-grained operations that were originally scattered across the traditional SGD update process (such as gradient scaling, weight decay, momentum update, and parameter assignment) into a single NPU Kernel for execution.
  • Formula:g~t={gt/ss≠Nonegtotherwiseg^t={−g~tmaximizeg~totherwisegˉt=g^t+weightDecay⋅θtvt+1={gˉtfirst stepμvt+(1−dampening)gˉtotherwisegtfinal={gˉt+μvt+1nesterovvt+1vt≠Nonegˉtotherwiseθt+1=θt−lr⋅gtfinalgt+1=g~t\begin{aligned} &\tilde{g}_t = \begin{cases} g_t / s & s \neq \text{None} \\ g_t & \text{otherwise} \end{cases} \\ &\hat{g}_t = \begin{cases} -\tilde{g}_t & \text{maximize} \\ \tilde{g}_t & \text{otherwise} \end{cases} \\ &\bar{g}_t = \hat{g}_t + weightDecay \cdot \theta_t \\ &v_{t+1} = \begin{cases} \bar{g}_t & \text{first step} \\ \mu v_t + (1-dampening)\bar{g}_t & \text{otherwise} \end{cases} \\ &g_t^{\text{final}} = \begin{cases} \bar{g}_t + \mu v_{t+1} & \text{nesterov} \\ v_{t+1} & v_t \neq \text{None} \\ \bar{g}_t & \text{otherwise} \end{cases} \\ &\theta_{t+1} = \theta_t - lr \cdot g_t^{\text{final}} \\ &g_{t+1} = \tilde{g}_t \end{aligned}

Function Prototype

The operator execution API is a two-phase API. Call the "aclnnFusedSgdGetWorkspaceSize" API first to obtain the input parameters and calculate the required workspace size based on the computation process, and then call the "aclnnFusedSgd" API to perform the computation.

aclnnStatus aclnnFusedSgdGetWorkspaceSize(
    const aclTensorList   *paramsRef,
    const aclTensorList   *gradsRef,
    const aclTensorList   *momentumBufferListOptionalRef,
    const aclTensor       *gradScaleOptional,
    float                  weightDecay,
    float                  momentum,
    float                  lr,
    float                  dampening,
    bool                   nesterov,
    bool                   maximize,
    bool                   isFirstStep,
    uint64_t              *workspaceSize,
    aclOpExecutor        **executor);
aclnnStatus aclnnFusedSgd(
  void              *workspace,
  uint64_t           workspaceSize,
  aclOpExecutor     *executor,
  const aclrtStream  stream)

aclnnFusedSgdGetWorkspaceSize

  • Parameters

    Parameter Input/Output Description Instruction Data Type Data Format Dimension (shape) Non-contiguous Tensor
    paramsRef input/Output Updates parameters. Does not support empty tensors. BFLOAT16, FLOAT16, FLOAT32 ND less than or equal to 8 dimensions √
    gradsRef input/Output Updates gradients. Empty tensors are not supported. BFLOAT16, FLOAT16, FLOAT32 ND Less than or equal to 8 dimensions √
    momentumBufferListOptionalRef Input/Output Updates momentum. Empty tensors are supported. BFLOAT16, FLOAT16, FLOAT32 ND Less than or equal to 8 dimensions √
    gradScaleOptional Input Gradient scaling value Supports empty tensors. BFLOAT16, FLOAT16, FLOAT32 ND shape equal to [1] √
    weightDecay attribute Weight decay value. - FLOAT - - -
    momentum attribute Momentum value. - FLOAT - - -
    lr attribute Learning rate. - FLOAT - - -
    dampening attribute Momentum damping coefficient. - FLOAT - - -
    nesterov attribute Specifies whether to enable Nesterov momentum. - BOOL - - -
    maximize attribute Whether the objective function is to be maximized. - BOOL - - -
    isFirstStep attribute Specifies whether it is the first update step. - BOOL - - -
    workspaceSize Output Returns the workspace size to be side-applied on the Device by the user. - - - 1 -
    executor Output Returns the op executor, which contains the operator computation process. - - - 1 -
  • Return Value

    aclnnStatus: return status code. For details, see aclnn Return Codes.

    The first-phase API performs input parameter validation and returns an error in the following scenarios:

    Return Value Error Code Description
    ACLNN_ERR_PARAM_NULLPTR 161001 The input paramsRef, gradsRef, momentumBufferListOptionalRef, or gradScaleOptional is a null pointer.
    ACLNN_ERR_PARAM_INVALID 161002
    • The data type of paramsRef, gradsRef, momentumBufferListOptionalRef, or gradScaleOptional is not within the supported range.
    • The data types of paramsRef, gradsRef, momentumBufferListOptionalRef, and the tensors in them are different.
    • The shape dimension of tensors in paramsRef, gradsRef, or momentumBufferListOptionalRef is greater than 8.
    • The shapes of tensors at the same index in paramsRef, gradsRef, and momentumBufferListOptionalRef (when momentumBufferListOptionalRef is not empty) are different.
    • There is a null pointer in paramsRef, gradsRef, or momentumBufferListOptionalRef (when momentumBufferListOptionalRef is not empty).
    • The value of lr, momentum, weightDecay, or dampening is less than 0.

aclnnFusedSgd

  • Parameters:
Parameter Input/Output Description
workspace Input Device-side-applied workspace memory address.
workspaceSize Input Size of the workspace side-applied on the Device, obtained by the first-phase API aclnnFusedSgd.
executor Input Op executor that contains the operator computation process.
stream Input Stream for task execution.
  • ​Return value​: Returns the aclnnStatus status code. For details, see aclnn return codes.

Constraints

  1. The data types of paramsRef, gradsRef, momentumBufferListOptionalRef, and gradScaleOptional are within the supported range.
  2. paramsRef, gradsRef, momentumBufferListOptionalRef, and each tensor in them have the same data type.
  3. The tensor shapes in paramsRef, gradsRef, and momentumBufferListOptionalRef have dimensions less than or equal to 8.
  4. Tensors at the same index in paramsRef, gradsRef, and momentumBufferListOptionalRef (when momentumBufferListOptionalRef is not empty) have the same shape.
  5. paramsRef, gradsRef, and momentumBufferListOptionalRef (when momentumBufferListOptionalRef is not empty) cannot contain null pointers.
  6. The values of lr, momentum, weightDecay, and dampening are greater than or equal to 0.
  7. gradScale is not equal to 0.

Example

The following sample code is for reference only. For the compilation and execution process, see Compile and Run Samples.

#include "acl/acl.h"
#include "aclnnop/aclnn_fused_sgd.h"
#include <iostream>
#include <vector>

#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;
}

void PrintOutResult(std::vector<int64_t> &shape, void **deviceAddr) {
  auto size = GetShapeSize(shape);
  std::vector<float> resultData(size, 0);
  auto ret = aclrtMemcpy(
      resultData.data(), resultData.size() * sizeof(resultData[0]), *deviceAddr,
      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 );
  for (int64_t i = 0; i < size; i++) {
    LOG_PRINT("result[%ld] is: %f\n", i, resultData[i]);
  }
}

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<float> paramsRefHostData1 = {1, 2, 3, 4, 5, 6, 7, 8};
  std::vector<float> gradsRefHostData1 = {0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8};
  std::vector<float> momentumHostData1 = {0, 0, 0, 0, 0, 0, 0, 0};
  std::vector<float> paramsRefHostData2 = {9, 10, 11, 12};
  std::vector<float> gradsRefHostData2 = {0.9, 1.0, 1.1, 1.2};
  std::vector<float> momentumHostData2 = {0, 0, 0, 0};
  std::vector<float> gradScaleOptionalHostData = {1.0};
  std::vector<int64_t> inputShape1 = {2, 2, 2};
  std::vector<int64_t> inputShape2 = {2, 2};
  std::vector<int64_t> scalarShape = {1};

  void *paramsRef1DeviceAddr = nullptr;
  void *gradsRef1DeviceAddr = nullptr;
  void *momentum1DeviceAddr = nullptr;
  void *paramsRef2DeviceAddr = nullptr;
  void *gradsRef2DeviceAddr = nullptr;
  void *momentum2DeviceAddr = nullptr;
  void *gradScaleOptionalDeviceAddr = nullptr;

  aclTensor *paramsRef1 = nullptr;
  aclTensor *gradsRef1 = nullptr;
  aclTensor *momentum1 = nullptr;
  aclTensor *paramsRef2 = nullptr;
  aclTensor *gradsRef2 = nullptr;
  aclTensor *momentum2 = nullptr;
  aclTensor *gradScaleOptional = nullptr;

  ret = CreateAclTensor(paramsRefHostData1, inputShape1, ¶msRef1DeviceAddr, aclDataType::ACL_FLOAT, ¶msRef1);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(gradsRefHostData1, inputShape1, &gradsRef1DeviceAddr, aclDataType::ACL_FLOAT, &gradsRef1);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(momentumHostData1, inputShape1, &momentum1DeviceAddr, aclDataType::ACL_FLOAT, &momentum1);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(paramsRefHostData2, inputShape2, ¶msRef2DeviceAddr, aclDataType::ACL_FLOAT, ¶msRef2);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(gradsRefHostData2, inputShape2, &gradsRef2DeviceAddr, aclDataType::ACL_FLOAT, &gradsRef2);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(momentumHostData2, inputShape2, &momentum2DeviceAddr, aclDataType::ACL_FLOAT, &momentum2);
  CHECK_RET(ret == ACL_SUCCESS, return ret);
  ret = CreateAclTensor(gradScaleOptionalHostData, scalarShape, &gradScaleOptionalDeviceAddr, aclDataType::ACL_FLOAT, &gradScaleOptional);
  CHECK_RET(ret == ACL_SUCCESS, return ret);

  std::vector<aclTensor*> paramsRefListData = {paramsRef1, paramsRef2};
  std::vector<aclTensor*> gradsRefListData = {gradsRef1, gradsRef2};
  std::vector<aclTensor*> momentumListData = {momentum1, momentum2};
  aclTensorList* paramsRefList = aclCreateTensorList(paramsRefListData.data(), paramsRefListData.size());
  aclTensorList* gradsRefList = aclCreateTensorList(gradsRefListData.data(), gradsRefListData.size());
  aclTensorList* momentumList = aclCreateTensorList(momentumListData.data(), momentumListData.size());

  float weightDecay = 0.01f;
  float momentumVal = 0.9f;
  float lr = 0.001f;
  float dampening = 0.0f;
  bool nesterov = false;
  bool maximize = false;
  bool isFirstStep = true;

  uint64_t workspaceSize = 0;
  aclOpExecutor *executor;

  ret = aclnnFusedSgdGetWorkspaceSize(paramsRefList, gradsRefList, momentumList, gradScaleOptional,
                                       weightDecay, momentumVal, lr, dampening,
                                       nesterov, maximize, isFirstStep,
                                       &workspaceSize, &executor);
  CHECK_RET(
      ret == ACL_SUCCESS,
      LOG_PRINT("aclnnFusedSgdGetWorkspaceSize 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 = aclnnFusedSgd(workspaceAddr, workspaceSize, executor, stream);
  CHECK_RET(ret == ACL_SUCCESS,
            LOG_PRINT("aclnnFusedSgd 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);

  LOG_PRINT("====== Tensor 1 paramsRef results ======\n");
  PrintOutResult(inputShape1, ¶msRef1DeviceAddr);
  LOG_PRINT("====== Tensor 1 gradsRef results ======\n");
  PrintOutResult(inputShape1, &gradsRef1DeviceAddr);
  LOG_PRINT("------ Momentum buffer 1 ------\n");
  PrintOutResult(inputShape1, &momentum1DeviceAddr);
  LOG_PRINT("====== Tensor 2 paramsRef results ======\n");
  PrintOutResult(inputShape2, ¶msRef2DeviceAddr);
  LOG_PRINT("====== Tensor 2 gradsRef results ======\n");
  PrintOutResult(inputShape2, &gradsRef2DeviceAddr);
  LOG_PRINT("------ Momentum buffer 2 ------\n");
  PrintOutResult(inputShape2, &momentum2DeviceAddr);

  aclDestroyTensorList(paramsRefList);
  aclDestroyTensorList(gradsRefList);
  aclDestroyTensorList(momentumList);
  aclDestroyTensor(gradScaleOptional);

  aclrtFree(paramsRef1DeviceAddr);
  aclrtFree(gradsRef1DeviceAddr);
  aclrtFree(momentum1DeviceAddr);
  aclrtFree(paramsRef2DeviceAddr);
  aclrtFree(gradsRef2DeviceAddr);
  aclrtFree(momentum2DeviceAddr);
  aclrtFree(gradScaleOptionalDeviceAddr);
  if (workspaceSize > 0) {
    aclrtFree(workspaceAddr);
  }

  ret = aclrtDestroyStream(stream);
  CHECK_RET(ret == ACL_SUCCESS,
            LOG_PRINT("destroy stream failed. ERROR: %d\n", ret);
            return ret);
  ret = aclrtResetDevice(deviceId);
  CHECK_RET(ret == ACL_SUCCESS,
            LOG_PRINT("reset device failed. ERROR: %d\n", ret);
            return ret);
  ret = aclFinalize();
  CHECK_RET(ret == ACL_SUCCESS,
            LOG_PRINT("finalize acl failed. ERROR: %d\n", ret);
            return ret);
  return 0;
}