Skip to content
Merged
Show file tree
Hide file tree
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
1 change: 1 addition & 0 deletions nemo_reinforcer/algorithms/dpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,7 @@ def dpo_train(
losses = train_results["loss"]
metrics = {
"loss": train_results["loss"].numpy(),
"grad_norm": train_results["grad_norm"],
}
metrics.update(train_results["all_mb_metrics"])
for k, v in metrics.items():
Expand Down
1 change: 1 addition & 0 deletions nemo_reinforcer/algorithms/grpo.py
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,7 @@ def grpo_train(
metrics = {
"loss": train_results["loss"].numpy(),
"reward": rewards.numpy(),
"grad_norm": train_results["grad_norm"],
}
metrics.update(train_results["all_mb_metrics"])
for k, v in metrics.items():
Expand Down
1 change: 1 addition & 0 deletions nemo_reinforcer/algorithms/sft.py
Original file line number Diff line number Diff line change
Expand Up @@ -486,6 +486,7 @@ def sft_train(
losses = train_results["loss"]
metrics = {
"loss": train_results["loss"].numpy(),
"grad_norm": train_results["grad_norm"],
}
metrics.update(train_results["all_mb_metrics"])
for k, v in metrics.items():
Expand Down
13 changes: 10 additions & 3 deletions nemo_reinforcer/models/policy/fsdp1_policy_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -310,10 +310,16 @@ def train(
all_mb_metrics.append(loss_metrics)

# Clip gradients
grad_norm = None
if not eval_mode:
torch.nn.utils.clip_grad_norm_(
self.model.parameters(), max_norm=self.cfg["max_grad_norm"]
)
if isinstance(self.model, FullyShardedDataParallel):
grad_norm = self.model.clip_grad_norm_(
Comment thread
ashors1 marked this conversation as resolved.
max_norm=self.cfg["max_grad_norm"]
)
else:
grad_norm = torch.nn.utils.clip_grad_norm_(
self.model.parameters(), max_norm=self.cfg["max_grad_norm"]
)

# Update parameters
self.optimizer.step()
Expand All @@ -336,6 +342,7 @@ def train(
metrics = {
"global_loss": global_loss.cpu(),
"local_loss": local_loss.cpu(),
"grad_norm": grad_norm,
"rank": torch.distributed.get_rank(),
"all_mb_metrics": dict(mb_metrics),
}
Expand Down
6 changes: 4 additions & 2 deletions nemo_reinforcer/models/policy/hf_policy.py
Original file line number Diff line number Diff line change
Expand Up @@ -177,8 +177,10 @@ def train(
results = self.worker_group.get_all_worker_results(futures)

# Aggregate the results
aggregated_results = {}
aggregated_results["loss"] = results[0]["global_loss"]
aggregated_results = {
"loss": results[0]["global_loss"],
"grad_norm": results[0]["grad_norm"],
}

# Aggregate metrics across all workers
all_mb_metrics = defaultdict(list)
Expand Down