Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add bf16 Support to Adam #134

Closed
wants to merge 21 commits into from
Closed
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
modified: add adam_fp32_accum_bf16 function
  • Loading branch information
JerryYin777 committed Aug 3, 2023
commit e2fdcc5109f1e884add31284ada5e07da0b81058
32 changes: 32 additions & 0 deletions csrc/cuda/adam_cuda.cu
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,38 @@ __global__ void adam_fp32_accum(
m[col] = __float2half(local_m);
}
}

__global__ void adam_fp32_accum_bf16(
int32_t n,
const nv_bfloat16 *g, // (n)
nv_bfloat16 *m, // (n)
float *v, // (n)
float* param, // (n)
nv_bfloat16* param_h, // (n)
float beta1,
float beta2,
float eps,
float lr,
float scale,
float weight_decay,
float bias_correction1,
float bias_correction2
) {
int32_t col = blockIdx.x * blockDim.x + threadIdx.x;
if (col < n) {
nv_bfloat16 local_g = __bfloat162float(g[col]) / scale; // real_g
nv_bfloat16 local_m = beta1 * __bfloat162float(m[col]) + (1 - beta1) * local_g; // real_m
nv_bfloat16 local_v = beta2 * v[col] + (1 - beta2) * local_g * local_g; // real_v
float local_p = param[col];
local_p = local_p - lr * local_m / bias_correction1 / (sqrtf(local_v * scale / bias_correction2) + eps * scale) - lr * weight_decay * local_p;

param_h[col] = __float2bfloat16(local_p);
param[col] = local_p;
v[col] = local_v;
m[col] = __float2bfloat16(local_m);
}
}

}

void adam_launcher(
Expand Down