Avoid per-step CUDA syncs in the training loop#1608
Open
dxqb wants to merge 1 commit into
Open
Conversation
Two independent fixes, each removing a per-step cudaStreamSynchronize that stalled the training loop (found via multi-step profiling of FLUX.2 W8A8). Keep VAE batch-norm stats on the train device during offloaded training (Flux2 / Ideogram / Ernie): scale_latents reads vae.bn.running_mean/var every step. With latent caching the VAE is offloaded to CPU, so the per-step .to(cuda) was a blocking H2D that synchronized the whole CUDA stream and drained the command buffer once per step. Pinning the tiny bn buffers to the train device in setup_train_device (re-applied each setup, so it survives offload/backup cycles) makes the access device-local. Safe mixed-device state: the VAE forward never runs while offloaded - only the bn buffers are read. Read training loss asynchronously: accumulated_loss.item() every update-step drained the GPU at the step boundary. The loss is now copied into a pinned host buffer (AsyncTensorExchange, in modules/util/async_tensor_exchange.py) and read one update-step later - a host read that never touches the training stream, so the CPU can queue the next step's kernels. Loss/tqdm logging and the NaN check lag one update-step; the final step's loss is not logged. 🤖 Generated with Claude Code
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Two independent fixes, each removing a per-step cudaStreamSynchronize that stalled the training loop
Keep VAE batch-norm stats on the train device during training (Flux2 / Ideogram / Ernie): scale_latents reads vae.bn.running_mean/var every step. With latent caching the VAE is offloaded to CPU, so the per-step .to(cuda) was a blocking H2D that synchronized the whole CUDA stream and drained the command buffer once per step.
Read training loss asynchronously: accumulated_loss.item() every update-step drained the GPU at the step boundary. The loss is now copied into a pinned host buffer (AsyncTensorExchange, in modules/util/async_tensor_exchange.py) and read one update-step later - a host read that never touches the training stream, so the CPU can queue the next step's kernels. Loss/tqdm logging and the NaN check lag one update-step; the final step's loss is not logged.
Test plan
pre-commit run --all-filespassesAI assistance