Skip to content
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
11 changes: 10 additions & 1 deletion miles/backends/training_utils/log_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,8 @@ def log_rollout_data(rollout_id: int, args: Namespace, rollout_data: RolloutBatc
"rollout_routed_experts",
"max_seq_lens",
"dynamic_global_batch_size",
"weight_versions",
"metadata",
]:
continue
# Upload per sample mean for each rollout value
Expand Down Expand Up @@ -151,7 +153,14 @@ def log_rollout_data(rollout_id: int, args: Namespace, rollout_data: RolloutBatc
else:
val = val.mean() * cp_size
else:
val = sum(val) / len(val)
# Flatten nested lists (e.g. list of lists from async rollout)
flat = val
if isinstance(val[0], (list, tuple)):
flat = [x for sublist in val for x in sublist]
# Skip non-numeric values (e.g. strings from async rollout metadata)
if flat and not isinstance(flat[0], (int, float)):
continue
val = sum(flat) / len(flat)
Comment on lines +157 to +163

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

high

The current logic for processing list values has a few potential issues that could lead to runtime errors:

  • IndexError: isinstance(val[0], ...) on line 158 will fail if val is an empty list.
  • TypeError: sum(flat) on line 163 will fail if flat contains a mix of numeric and non-numeric types (e.g., [1, 2, 'a']), as the type check on line 161 only inspects the first element.
  • ZeroDivisionError: len(flat) on line 163 can be zero if val is an empty list or a list of empty lists, leading to a division by zero.

To make this more robust, I suggest checking if the list is empty before accessing elements, filtering all items for numeric types, and then calculating the average only if there are numeric items.

flat = val
if val and isinstance(val[0], (list, tuple)):
    flat = [x for sublist in val for x in sublist]
numeric_items = [item for item in flat if isinstance(item, (int, float))]
if not numeric_items:
    continue
val = sum(numeric_items) / len(numeric_items)

elif isinstance(val, torch.Tensor):
val = val.float().mean()
else:
Expand Down
Loading