Domino/Dflash: avoid blocking syncs - #715
Conversation
Converting CUDA Tensors to scalars is generally bad for training so we avoid it. If there are no Tensor-to-scalar conversions on the majority of steps (forward+backward+optimizer), we are able to hide some CPU dataloading latency. There were three main sources of Tensor to scalar conversion or scalar host-to-device syncs that we removed: 1. We used to acquire scalar metrics after each step, even if our log interval was greater than one step. This PR changes it so that we acquire Tensor metrics (that are detached), and only convert them to scalar when needed for the log interval. 2. We used to create an `anchor_positions` tensor with shape `valid_counts.max().item()`. This tensor was padded and masked (with `block_keep_mask`). This PR changes it so the shape is padded to `self.num_anchors`. This may waste more performance but does help us get rid of the `.item()`. 3. We used to create CUDA scalar tensors from Python scalars in the Domino hot path. These tiny host-to-device copies can still introduce stream syncs, so sentinel values now use scalar literals and the `lambda_base` metric is filled on device. We tested this on GB300 with 3 trainers and 1 rollout. At that setup, the net throughput was roughly equal to before. We will actually see perf gains once all of the blocking syncs are removed (I think the last one after this PR is figuring out the ack situation -- the acks happen per-step and are blocking).
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
|
Caution The consumer version of Gemini Code Assist on GitHub has been sunset. All code review activity has officially ceased. |
|
cc @maocheng23 |
| bs = self.block_size | ||
| bsz = loss_mask.shape[0] | ||
| max_anchor = max(seq_len - bs, 0) | ||
| max_n = max(1, self.num_anchors) |
There was a problem hiding this comment.
this probably needs careful reading
|
|
||
| valid = loss_mask[:, : max_anchor + 1] > 0.5 | ||
| valid_counts = valid.sum(dim=1) | ||
| max_n = max(1, min(self.num_anchors, int(valid_counts.max().item()) - 1)) |
There was a problem hiding this comment.
Btw, is the int(valid_counts.max().item()) - 1) intentional? (the -1) thing. It looks like when there are fewer than num_anchor anchors, we just drop one of the anchors.
I tried to replicate this behavior in the diff.
There was a problem hiding this comment.
There was a problem hiding this comment.
We have checked the code that DFlash and Domino impl have this question. The -1 is not right when the available training tokens is less. We will fix it. Good findings!
|
Independent B300 validation on head
I found three issues that I think should be addressed before merge:
Recommendation: fix the off-by-one and its test, bound the padded width without a host sync (or demonstrate the full downstream tradeoff with an end-to-end benchmark), and either update the DFlash path or narrow the stated scope. |
|
Help fix the conflicts. Thanks a lot. @zou3519 |
Converting CUDA Tensors to scalars is generally bad for training so we avoid it. If there are no Tensor-to-scalar conversions on the majority of steps (forward+backward+optimizer), we are able to hide some CPU dataloading latency.
There were three main sources of Tensor to scalar conversion or scalar host-to-device syncs that we removed:
anchor_positionstensor with shapevalid_counts.max().item(). This tensor was padded and masked (withblock_keep_mask). This PR changes it so the shape is padded toself.num_anchors. This may waste more performance but does help us get rid of the.item().lambda_basemetric is filled on device.We tested this on GB300 with 3 trainers and 1 rollout. At that setup, the net throughput was roughly equal to before. We will actually see perf gains once all of the blocking syncs are removed (I think the last one after this PR is figuring out the ack situation -- the acks happen per-step and are blocking).