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
6 changes: 3 additions & 3 deletions relax/backends/megatron/cp_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ def get_cp_local_num_tokens(
"""
cp_size = dynamic_cp_size if dynamic_cp_size is not None else mpu.get_context_parallel_world_size()
if cp_size == 1:
return sum([torch.clamp_min(loss_mask.sum(), 1) for loss_mask in loss_masks])
return sum([torch.clamp_min(loss_mask.sum(), 1) for loss_mask in loss_masks]).to(torch.int)

# cp_size > 1: mirror the chunk slicing done in get_sum_of_sample_mean so the
# counted tokens exactly match the ones sum_of_token contributes on this rank.
Expand All @@ -221,8 +221,8 @@ def get_cp_local_num_tokens(

if total is None:
# No samples on this rank: mirror the empty-sum behaviour of cp_size == 1.
return sum([loss_mask.sum() for loss_mask in loss_masks])
return total
return sum([loss_mask.sum() for loss_mask in loss_masks]).to(torch.int)
return total.to(torch.int)


def all_gather_with_cp(
Expand Down
3 changes: 2 additions & 1 deletion tests/backends/megatron/test_rloo_policy_loss_dispatch.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,8 @@ def test_rloo_unequal_lengths_use_global_token_scalar_and_gradient_oracle(monkey
final_loss = token_sum_loss / normalizer

assert torch.allclose(token_sum_loss, expected_token_sum)
assert torch.allclose(normalizer, expected_num_tokens)
assert normalizer.dtype == torch.int
assert torch.allclose(normalizer.to(expected_num_tokens.dtype), expected_num_tokens)
assert torch.allclose(final_loss, expected_final_loss)

first_length = response_lengths[0]
Expand Down
Loading