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

Fix batch all gather #1177

Merged
merged 2 commits into from
Jan 4, 2024
Merged
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
30 changes: 16 additions & 14 deletions trl/trainer/ppo_trainer.py
Original file line number Diff line number Diff line change
Expand Up @@ -1320,10 +1320,26 @@ def log_stats(
rewards (`List[torch.FloatTensor]`):
A tensor of rewards.
"""

# all gather stats
if not isinstance(rewards, torch.Tensor):
rewards = torch.tensor(rewards).to(self.current_device)
rewards = self.accelerator.gather(rewards).flatten()

if self.config.log_with == "wandb":
import wandb

if any([column_to_log not in batch.keys() for column_to_log in columns_to_log]):
raise ValueError(f"Columns to log {columns_to_log} are not present in the batch {batch.keys()}.")

batch_list = [batch[column_to_log] for column_to_log in columns_to_log]
if self.is_distributed:
gathered_batch_list = []
for b in batch_list:
flattened = gather_object(b)
gathered_batch_list.append(flattened)
batch_list = gathered_batch_list

# Log only if we are in the main process
if self.accelerator.is_main_process:
logs = {}
Expand All @@ -1336,20 +1352,6 @@ def log_stats(
"'response'. "
)
elif self.config.log_with == "wandb":
import wandb

if any([column_to_log not in batch.keys() for column_to_log in columns_to_log]):
raise ValueError(f"Columns to log {columns_to_log} are not present in the batch {batch.keys()}.")

batch_list = [batch[column_to_log] for column_to_log in columns_to_log]
if self.is_distributed:
self.accelerator.wait_for_everyone()
gathered_batch_list = []
for batch in batch_list:
flattened = gather_object(batch)
gathered_batch_list.append(flattened)
batch_list = gathered_batch_list

table_rows = [list(r) for r in zip(*batch_list, rewards.cpu().tolist())]
logs.update({"game_log": wandb.Table(columns=[*columns_to_log, "reward"], rows=table_rows)})

Expand Down
Loading