FusedSgd
产品支持情况
| 产品 | 是否支持 |
|---|---|
| Ascend 950PR/Ascend 950DT | √ |
| Atlas A3 训练系列产品/Atlas A3 推理系列产品 | √ |
| Atlas A2 训练系列产品/Atlas A2 推理系列产品 | √ |
| Atlas 200I/500 A2 推理产品 | × |
| Atlas 推理系列产品 | × |
| Atlas 训练系列产品 | × |
功能说明
-
算子功能:实现fusedSgd算子。将传统 SGD 更新过程中原本分散的多个细粒度操作(如梯度缩放、权重衰减、动量更新、参数赋值等)融合为单个NPU Kernel执行。
-
计算公式:
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}
参数说明
| 参数名 | 输入/输出/属性 | 描述 | 数据类型 | 数据格式 |
|---|---|---|---|---|
| params | 输入/输出 | 待更新参数,对应公式中的θ。 | BFLOAT16、FLOAT16、FLOAT | ND |
| grads | 输入/输出 | 待更新参数对应的梯度,对应公式中的g。 | BFLOAT16、FLOAT16、FLOAT | ND |
| x | 输入 | 待更新参数对应的动量,对应公式中的v。 | BFLOAT16、FLOAT16、FLOAT | ND |
| gradScale | 输入 | 梯度缩放大小,对应公式中的s。 | FLOAT | ND |
| y | 输出 | 待更新参数对应的动量,对应公式中的v。 | BFLOAT16、FLOAT16、FLOAT | ND |
| weightDecay | 属性 | 权重衰减值,对应公式中的weightDecay,默认为0。 | FLOAT | - |
| momentum | 属性 | 动量值,对应公式中的μ,默认为0。 | FLOAT | - |
| lr | 属性 | 学习率,对应公式中的lr,默认为1e-3。 | FLOAT | - |
| dampening | 属性 | 动量阻尼系数,对应公式中的dampening,默认为0。 | FLOAT | - |
| nesterov | 属性 | 是否启用Nesterov动量,对应公式中的nesterov,默认为False。 | BOOL | - |
| maximize | 属性 | 是否为最大化目标函数,对应公式中的maximize,默认为False。 | BOOL | - |
| isFirstStep | 属性 | 是否第一步更新,对应公式中的FirstStep,默认为True。 | BOOL | - |
约束说明
- params、grads、x、gradScale的数据类型在支持的范围之内。
- params、grads与x及其中各个tensor具有相同的数据类型。
- params、grads与x中tensor的shape维度小于等于8,gradScale的shape为[1]。
- lr、momentum、weightDecay、dampening的值大于等于0。
- params、grads与x(x不为空时)中相同索引tensor的shape相同。
- params、grads与x(x不为空时)中不能有空指针。
调用说明
| 调用方式 | 样例代码 | 说明 |
|---|---|---|
| aclnn接口 | test_aclnn_fused_sgd | 通过aclnnFusedSgd接口方式调用FusedSgd算子。 |