-
Notifications
You must be signed in to change notification settings - Fork 420
perf(train): make sequence packing alignment-aware #2108
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -5,7 +5,8 @@ | |||||||||||||||
| import torch | ||||||||||||||||
|
|
||||||||||||||||
| from skyrl.backends.skyrl_train.distributed.megatron.packing_utils import ( | ||||||||||||||||
| get_packed_seq_align_size, | ||||||||||||||||
| get_packing_align_size_sequence, | ||||||||||||||||
| get_packing_align_size_total, | ||||||||||||||||
| get_unpacked_seq_align_size, | ||||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
|
|
@@ -63,8 +64,11 @@ def build_token_metadata_layout( | |||||||||||||||
| ) | ||||||||||||||||
|
|
||||||||||||||||
| cp_size = mpu.get_context_parallel_world_size() | ||||||||||||||||
| align_size = get_packed_seq_align_size(tp_size, cp_size, fp8_enabled=fp8_enabled) | ||||||||||||||||
| padded_sequence_lengths_tensor = sequence_lengths_tensor + (-sequence_lengths_tensor % align_size) | ||||||||||||||||
| packing_align_size_sequence = get_packing_align_size_sequence(tp_size, cp_size) | ||||||||||||||||
| packing_align_size_total = get_packing_align_size_total(tp_size, cp_size, fp8_enabled=fp8_enabled) | ||||||||||||||||
| padded_sequence_lengths_tensor = sequence_lengths_tensor + (-sequence_lengths_tensor % packing_align_size_sequence) | ||||||||||||||||
| aggregate_pad_size = (-padded_sequence_lengths_tensor.sum()) % packing_align_size_total | ||||||||||||||||
| padded_sequence_lengths_tensor[-1] += aggregate_pad_size | ||||||||||||||||
|
Comment on lines
+69
to
+71
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Similar to another comment, there's a potential
Suggested change
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Same as above |
||||||||||||||||
| padded_sequence_lengths = padded_sequence_lengths_tensor.tolist() | ||||||||||||||||
| cu_seqlens_padded = torch.cat( | ||||||||||||||||
| ( | ||||||||||||||||
|
|
||||||||||||||||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There's a potential
IndexErrorhere ifseqlens_in_batch_paddedis an empty tensor. This can happen ifseqlens_in_batchis empty, for example with an empty input batch. Accessingseqlens_in_batch_padded[-1]would cause a crash. It's safer to add a guard to handle this edge case.Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
SFT rejects an empty dataloader explicitly at skyrl/train/sft_trainer.py:1890, and RL padding microbatches deliberately contain one valid token at skyrl/backends/skyrl_train/workers/worker_utils.py:316. We don't need an extra defensive check here.