Skip to content

A straightforward method for training your LLM, from downloading data to generating text.

fix(ddp): run the SFT and DPO final eval on the unwrapped model

FareedKhan-dev806659c04d2a
diff --git a/scripts/train_dpo.py b/scripts/train_dpo.py
index c53c0ed..bf22286 100644
--- a/scripts/train_dpo.py
+++ b/scripts/train_dpo.py
@@ -129,7 +129,9 @@ def main():
metrics={"train_loss": loss.item()})
if ctx.is_main:
- acc, marg = eval_implicit_acc(policy, ref, cfg, ctx)
+ # Unwrap for the final eval: other ranks are already at cleanup(), so a collective on
+ # the DDP-wrapped policy here would hang (NCCL timeout). The periodic eval runs on all ranks.
+ acc, marg = eval_implicit_acc(unwrap(policy), ref, cfg, ctx)
save_stage_ckpt(cfg.out_ckpt, policy, optimizer, stage=f"dpo_{cfg.loss_type}", cfg=cfg, step=total_steps,
metrics={"test_acc": acc, "test_margin": marg})
print(f"Done DPO[{cfg.loss_type}]. test_acc {acc:.3f} margin {marg:.3f} -> {cfg.out_ckpt}")
diff --git a/scripts/train_sft.py b/scripts/train_sft.py
index a878b59..74fb3ce 100644
--- a/scripts/train_sft.py
+++ b/scripts/train_sft.py
@@ -111,7 +111,10 @@ def main():
metrics={"train_loss": loss.item()})
if ctx.is_main:
- dev = eval_dev(model, cfg, ctx, DEV_PATH)
+ # Use the unwrapped model for the final eval: the other ranks have already reached
+ # cleanup(), so calling the DDP-wrapped model here would launch a collective with no
+ # peer and hang (NCCL timeout). The periodic eval above runs on all ranks, so it is fine.
+ dev = eval_dev(unwrap(model), cfg, ctx, DEV_PATH)
save_stage_ckpt(cfg.out_ckpt, model, optimizer, stage="sft", cfg=cfg, step=total_steps,
metrics={"dev_loss": dev})
print(f"Done SFT. dev_loss {dev:.4f} -> {cfg.out_ckpt}")