Repository navigation
feat: support fullgraph Qwen3.5 training #3575
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
Closed
Closed
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
9e87b40
feat: support fullgraph Qwen3.5 training
samsja 4214f8d
fix: preserve FLA kernel input layouts
samsja 10efc55
fix: partition custom ops for CUDA graph capture
samsja d6981c7
fix: enable Qwen3.5 CUDA graph replay
samsja d32f52d
fix: make FA4 attention traceable under fullgraph compile
samsja File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| max_steps = 5 | ||
| dashboard = false | ||
|
|
||
| [model] | ||
| name = "Qwen/Qwen3.5-9B" | ||
| impl = "custom" | ||
| seq_len = 8192 | ||
| optim_cpu_offload = false | ||
| fsdp_cpu_offload = false | ||
| reshard_after_forward = false | ||
| ac_offloading = "None" | ||
|
|
||
| [model.debug] | ||
| random_init = true | ||
|
|
||
| [model.compile] | ||
| fullgraph = true | ||
| mode = "reduce-overhead" | ||
| cudagraph_copy_static_inputs = true | ||
|
|
||
| [data] | ||
| type = "fake" | ||
| batch_size = 8 | ||
| micro_batch_size = 1 | ||
| seq_len = 8192 | ||
| length = "fixed" | ||
| input_ids = "random" | ||
|
|
||
| [deployment] | ||
| type = "single_node" | ||
| gpus_per_node = 8 | ||
| num_train_gpus = 8 | ||
|
|
||
| [env_vars] | ||
| TORCH_LOGS = "perf_hints" | ||
| FLA_DISABLE_TENSOR_CACHE = "1" | ||
|
|
||
| [slurm] | ||
| job_name = "qwen3-5-9b-fullgraph" | ||
| partition = "all" | ||
| time = "00:45:00" |
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
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
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
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,143 @@ | ||
| import torch | ||
| from flash_attn.cute.interface import _flash_attn_bwd, _flash_attn_fwd | ||
|
|
||
| # FA4's public wrapper calls torch._guards.active_fake_mode(), which Dynamo refuses to trace, so | ||
| # fullgraph compile breaks inside it. Wrapping the kernels in custom ops keeps them opaque to Dynamo. | ||
|
|
||
|
|
||
| @torch.library.custom_op("prime_rl::flash_attn_4_varlen", mutates_args=()) | ||
| def _flash_attn_4_varlen( | ||
| q: torch.Tensor, | ||
| k: torch.Tensor, | ||
| v: torch.Tensor, | ||
| cu_seqlens_q: torch.Tensor, | ||
| cu_seqlens_k: torch.Tensor, | ||
| causal: bool, | ||
| window_size_left: int | None, | ||
| window_size_right: int | None, | ||
| ) -> tuple[torch.Tensor, torch.Tensor]: | ||
| out, lse, _, _ = _flash_attn_fwd( | ||
| q, | ||
| k, | ||
| v, | ||
| cu_seqlens_q=cu_seqlens_q, | ||
| cu_seqlens_k=cu_seqlens_k, | ||
| causal=causal, | ||
| window_size_left=window_size_left, | ||
| window_size_right=window_size_right, | ||
| return_lse=True, | ||
| ) | ||
| return out, lse | ||
|
|
||
|
|
||
| @_flash_attn_4_varlen.register_fake | ||
| def _flash_attn_4_varlen_fake( | ||
| q: torch.Tensor, | ||
| k: torch.Tensor, | ||
| v: torch.Tensor, | ||
| cu_seqlens_q: torch.Tensor, | ||
| cu_seqlens_k: torch.Tensor, | ||
| causal: bool, | ||
| window_size_left: int | None, | ||
| window_size_right: int | None, | ||
| ) -> tuple[torch.Tensor, torch.Tensor]: | ||
| total_q, num_heads, _ = q.shape | ||
| out = q.new_empty((total_q, num_heads, v.shape[-1])) | ||
| lse = q.new_empty((num_heads, total_q), dtype=torch.float32) | ||
| return out, lse | ||
|
|
||
|
|
||
| @torch.library.custom_op("prime_rl::flash_attn_4_varlen_backward", mutates_args=()) | ||
| def _flash_attn_4_varlen_backward( | ||
| q: torch.Tensor, | ||
| k: torch.Tensor, | ||
| v: torch.Tensor, | ||
| out: torch.Tensor, | ||
| grad_out: torch.Tensor, | ||
| lse: torch.Tensor, | ||
| cu_seqlens_q: torch.Tensor, | ||
| cu_seqlens_k: torch.Tensor, | ||
| causal: bool, | ||
| window_size_left: int | None, | ||
| window_size_right: int | None, | ||
| ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: | ||
| grad_q, grad_k, grad_v = _flash_attn_bwd( | ||
| q, | ||
| k, | ||
| v, | ||
| out, | ||
| grad_out, | ||
| lse, | ||
| causal=causal, | ||
| window_size_left=window_size_left, | ||
| window_size_right=window_size_right, | ||
| cu_seqlens_q=cu_seqlens_q, | ||
| cu_seqlens_k=cu_seqlens_k, | ||
| ) | ||
| return grad_q, grad_k, grad_v | ||
|
|
||
|
|
||
| @_flash_attn_4_varlen_backward.register_fake | ||
| def _flash_attn_4_varlen_backward_fake( | ||
| q: torch.Tensor, | ||
| k: torch.Tensor, | ||
| v: torch.Tensor, | ||
| out: torch.Tensor, | ||
| grad_out: torch.Tensor, | ||
| lse: torch.Tensor, | ||
| cu_seqlens_q: torch.Tensor, | ||
| cu_seqlens_k: torch.Tensor, | ||
| causal: bool, | ||
| window_size_left: int | None, | ||
| window_size_right: int | None, | ||
| ) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor]: | ||
| return torch.empty_like(q), torch.empty_like(k), torch.empty_like(v) | ||
|
|
||
|
|
||
| def _flash_attn_4_varlen_setup_context(ctx, inputs, output) -> None: | ||
| q, k, v, cu_seqlens_q, cu_seqlens_k, causal, window_size_left, window_size_right = inputs | ||
| out, lse = output | ||
| ctx.save_for_backward(q, k, v, out, lse, cu_seqlens_q, cu_seqlens_k) | ||
| ctx.causal = causal | ||
| ctx.window_size_left = window_size_left | ||
| ctx.window_size_right = window_size_right | ||
|
|
||
|
|
||
| def _flash_attn_4_varlen_autograd_backward(ctx, grad_out: torch.Tensor, grad_lse: torch.Tensor): | ||
| q, k, v, out, lse, cu_seqlens_q, cu_seqlens_k = ctx.saved_tensors | ||
| grad_q, grad_k, grad_v = _flash_attn_4_varlen_backward( | ||
| q, | ||
| k, | ||
| v, | ||
| out, | ||
| grad_out, | ||
| lse, | ||
| cu_seqlens_q, | ||
| cu_seqlens_k, | ||
| ctx.causal, | ||
| ctx.window_size_left, | ||
| ctx.window_size_right, | ||
| ) | ||
| return grad_q, grad_k, grad_v, None, None, None, None, None | ||
|
|
||
|
|
||
| _flash_attn_4_varlen.register_autograd( | ||
| _flash_attn_4_varlen_autograd_backward, | ||
| setup_context=_flash_attn_4_varlen_setup_context, | ||
| ) | ||
|
|
||
|
|
||
| def flash_attn_4_varlen( | ||
| q: torch.Tensor, | ||
| k: torch.Tensor, | ||
| v: torch.Tensor, | ||
| cu_seqlens_q: torch.Tensor, | ||
| cu_seqlens_k: torch.Tensor, | ||
| causal: bool = True, | ||
| window_size: tuple[int | None, int | None] = (None, None), | ||
| ) -> torch.Tensor: | ||
| out, _ = _flash_attn_4_varlen(q, k, v, cu_seqlens_q, cu_seqlens_k, causal, window_size[0], window_size[1]) | ||
| return out | ||
|
|
||
|
|
||
| __all__ = ["flash_attn_4_varlen"] |
Oops, something went wrong.
Oops, something went wrong.
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.
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.
why needed?