Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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
13 changes: 8 additions & 5 deletions csrc/cpu/adam/fused_adam.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,16 @@ void multi_tensor_adam(int chunk_size,
const int bias_correction,
const float weight_decay)
{
static bool initialized = false;
if (!initialized) {
create_adam_optimizer(0);
initialized = true;
// ds_adam_step reads lr/betas/eps/weight_decay per call; only AdamW-vs-L2 is fixed at
// construction, so keep one optimizer instance per mode (mode 1 == AdamW, as in CUDA).
static bool initialized[2] = {false, false};
Comment thread
PKUWZP marked this conversation as resolved.
const int optimizer_id = mode;
if (!initialized[mode]) {
create_adam_optimizer(optimizer_id, 1e-3f, 0.9f, 0.999f, 1e-8f, 0.0f, mode == 1);

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are these '1e-3f, 0.9f, 0.999f, 1e-8f, 0.0f' value place holder? If they are better define a macro as place holder and put it here.

initialized[mode] = true;
}
for (int i = 0; i < tensor_lists[0].size(); i++) {
ds_adam_step(0,
ds_adam_step(optimizer_id,
step,
lr,
beta1,
Expand Down
8 changes: 5 additions & 3 deletions tests/unit/ops/adam/test_adamw.py
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,10 @@ def test(self,


@pytest.mark.parametrize('adam_w_mode', [True, False], ids=["adamw", "adam"])
@pytest.mark.parametrize('dtype', [torch.float, torch.bfloat16], ids=["fp32", "bf16"])
# fp32 only: torch.optim in bf16 does its math in bf16 while the fused kernels compute in fp32,
# so it is not a valid bf16 reference. Low-precision dtypes are covered against an explicit
# fp32-math reference in the FusedAdam rework (#8300).
@pytest.mark.parametrize('dtype', [torch.float], ids=["fp32"])
def test_fused_adam_matches_torch(adam_w_mode, dtype):
if dtype not in get_accelerator().supported_dtypes():
pytest.skip(f"{dtype} not supported on {get_accelerator().device_name()}")
Expand All @@ -105,7 +108,6 @@ def test_fused_adam_matches_torch(adam_w_mode, dtype):
ref_optimizer.step()
ds_optimizer.step()

# bf16 storage rounds differently depending on where intermediates are kept, so allow one ulp.
atol = 1e-5 if dtype == torch.float else 2e-2
atol = 1e-5
for ref_param, ds_param in zip(ref_params, ds_params):
torch.testing.assert_close(ds_param.float(), ref_param.float(), atol=atol, rtol=0)
Loading