Repository navigation
Conversation
stack-info: PR: #2171, branch: drisspg/stack/4
e753776 to
e23d90b
Compare
stack-info: PR: #2171, branch: drisspg/stack/4
e23d90b to
fbe6fd4
Compare
| ) | ||
|
|
||
| def __init__(self, kernel_options: dict | None = None): | ||
| super().__init__() |
There was a problem hiding this comment.
In theory this can/should be runtime args but for now Im just using for global defaults
| "debugmodel_flex_flash": TransformerModelArgs( | ||
| dim=256, | ||
| n_layers=6, | ||
| n_heads=2, # head_dim=128, required for Flash backend |
There was a problem hiding this comment.
64 is also supported, but deepseek mla is currently not
| rope_theta=500000, | ||
| attn_type="flex", | ||
| attn_mask_type="block_causal", | ||
| flex_attention_kernel_options={"BACKEND": "FLASH"}, |
There was a problem hiding this comment.
I have two questions:
- what gpus can it run on? can it run on H100 / A100?
- if so, is it the most efficient kernel on all hardware?
Basically, I'd like to understand: as a user, when should I use what.
There was a problem hiding this comment.
yeah here is my current world model:
On B200 it is the fastest when its supported. The primary things that aren't supported and being worked on
-> Dynamic shapes (its on my list)
-> Learnable bias ( I dont think this applies for any titan models)
-> Head dim constraints (deepseek mla is not but can/should be)
For hopper it is generally faster but I have been mostly focusing on blackwell so I cant tell you definitively. And the backward needs to be implemented - I will do this. There is an A100 kernel but I would expect triton to be fine and maybe better since there is much more robust benchmarking.
There was a problem hiding this comment.
The other main requirement is that users need to have fa4 installed for this to work. it is very easy to install but still.
So in the future I might roll this into auto which is the current behavior, we make not promises if we lower to a triton impl or fa4. But I am mostly using this PR for testing right now
|
Hi @drisspg, it looks like the slowdown in 2D tensor indexing within mask_mod is primarily due to the fact that we are re-computing the 2D index in a column-major order to make CuTe happy, e.g., (b, s) -> (b + s * B), which can lead to additional significant integer calculation instructions and increase register pressure. However, CuTe DSL by design accepts hierarchical coordinates (e.g., In this fix, I changed Thank you! |
|
@Aneureka Thanks again for the tip, I put up: pytorch/pytorch#171222 and good tip on crd2idx My intuition was that under the hood things would need to be flattened, but I guess I was already thinking strided so this makes sense. |
Running the old reproer, here: pytorch/torchtitan#2171 Before this PR ```Py ❯ python issue_repro_2d_indexing.py Config: B=8, S=8192, H=2, D=128 Documents per sequence: [7, 11, 8, 7, 6, 7, 6, 11] Block mask sparsity: 86.19% kv_num_blocks equal: True kv_indices equal: True Attention output max diff: 0.00e+00 Benchmark results: 2D [b, idx] FLASH : 1468.33 μs 2D [b, idx] TRITON: 386.17 μs 1D [b*S+idx] FLASH : 467.82 μs 1D [b*S+idx] TRITON: 388.35 μs ``` After this PR ``` Py Config: B=8, S=8192, H=2, D=128 Documents per sequence: [7, 11, 8, 7, 6, 7, 6, 11] Block mask sparsity: 86.19% kv_num_blocks equal: True kv_indices equal: True Attention output max diff: 0.00e+00 Benchmark results: 2D [b, idx] FLASH : 440.89 μs 2D [b, idx] TRITON: 386.76 μs 1D [b*S+idx] FLASH : 441.16 μs 1D [b*S+idx] TRITON: 390.21 μs ``` Thanks for the advice @Aneureka Pull Request resolved: #171222 Approved by: https://github.com/henrylhtsang
|
Other perf issue with update: pytorch/pytorch#171267 |
Running the old reproer, here: pytorch/torchtitan#2171 Before this PR ```Py ❯ python issue_repro_2d_indexing.py Config: B=8, S=8192, H=2, D=128 Documents per sequence: [7, 11, 8, 7, 6, 7, 6, 11] Block mask sparsity: 86.19% kv_num_blocks equal: True kv_indices equal: True Attention output max diff: 0.00e+00 Benchmark results: 2D [b, idx] FLASH : 1468.33 μs 2D [b, idx] TRITON: 386.17 μs 1D [b*S+idx] FLASH : 467.82 μs 1D [b*S+idx] TRITON: 388.35 μs ``` After this PR ``` Py Config: B=8, S=8192, H=2, D=128 Documents per sequence: [7, 11, 8, 7, 6, 7, 6, 11] Block mask sparsity: 86.19% kv_num_blocks equal: True kv_indices equal: True Attention output max diff: 0.00e+00 Benchmark results: 2D [b, idx] FLASH : 440.89 μs 2D [b, idx] TRITON: 386.76 μs 1D [b*S+idx] FLASH : 441.16 μs 1D [b*S+idx] TRITON: 390.21 μs ``` Thanks for the advice @Aneureka Pull Request resolved: pytorch#171222 Approved by: https://github.com/henrylhtsang
stack-info: PR: #2171, branch: drisspg/stack/4
fbe6fd4 to
86c632d
Compare
stack-info: PR: #2171, branch: drisspg/stack/4
86c632d to
60e9ce9
Compare
stack-info: PR: #2171, branch: drisspg/stack/4
60e9ce9 to
5ea8282
Compare
stack-info: PR: #2171, branch: drisspg/stack/4 Made-with: Cursor
5ea8282 to
5a3b180
Compare
5a3b180 to
9a409bc
Compare
| attn_backend="flex", | ||
| attn_mask_type="block_causal", | ||
| rope_backend="complex", | ||
| flex_attention_kernel_options={"BACKEND": "FLASH"}, |
There was a problem hiding this comment.
when should we use Flash backend?
There was a problem hiding this comment.
should user config anything other than BACKEND?
There was a problem hiding this comment.
the currently supported backends, Literal["AUTO", "TRITON", "FLASH", "TRITON_DECODE"]
For training workloads auto is default and should be used. IN the future we might Allow for Auto to dispatch to FLASH but not yet since it does require changes to block mask sizes on blackwell
There was a problem hiding this comment.
@drisspg When should we use which? Depending on the answer we can provide the most reasonable UX in titan.
| attn_mask_type="block_causal", | ||
| rope_backend="complex", | ||
| flex_attention_kernel_options={"BACKEND": "FLASH"}, | ||
| flex_attention_block_size=(256, 128), |
There was a problem hiding this comment.
should this be configured by user at all? what's the guidance?
There was a problem hiding this comment.
Yeah it should be, FWIW @liangel-02 will want something like this for the rl stuff.
In blackwell to use FLASH you will need at least* (256, 128) due to how the kernel works. But this in general is not a bitwise matching change. So for now making it very explicit
| flex_attention_kernel_options: dict | None = None | ||
| flex_attention_block_size: int | tuple[int, int] | None = None |
There was a problem hiding this comment.
This is violating encapsulation. Instead of hardcoding flex related fields in general GQAttention module, we should make inner_attention configurable.
There was a problem hiding this comment.
Sounds good, in general being able to impl specific options feels desirable
|
@claude review this PR |
|
Claude finished @tianyu-l's task in 2m 33s —— View job Review: Add Flex flash backend to flex attention module
OverviewThis PR adds support for the FLASH backend in the flex attention module by threading Issue 1: Encapsulation violation in
|
stack-info: PR: #2171, branch: drisspg/stack/4 Made-with: Cursor
9a409bc to
a7b58ed
Compare
|
@drisspg Add an instruction for users , document when to use which |
|
good question: flash attention wheel
when to use FLASH backend
block sizes
|
|
Thanks @drisspg ! If you don't mind, I'll take over and prepare a PR for this. |
for several things: - Save a pair of transpose for varlen attention, by moving the transpose for sdpa / flex inside their own module. I had to change `_ContextParallel(seq_dim=2, ...)` to `seq_dim=1` -> I also had to change flux model impl to adapt. - The above change follows #2709 which is only for the generator. With this PR I'm able to remove `VLLMGQAttention` module added in that PR. - Make `inner_attention` a python config, instead of string `attn_backend` which is now used in qwen3 `model_registry` as a convenient way to switch config. - Thanks to that, we can support FlexAttention with configurable options, including `block_size` and `kernel_options` to enable Flash attention backend #2171. Added a config `qwen3_debugmodel_flex_flash` to the registry. - Move `annotate_flex_attention_for_regional_inductor` from general attention file to `graph_trainer` where it's used. - remove outdated artificial restrictions on CP / CP + TP - some naming improvement NOTE: - GraphTrainer tests fail on main as well - Model test on Llama4+PP+compile fails on main as well
for several things: - Save a pair of transpose for varlen attention, by moving the transpose for sdpa / flex inside their own module. I had to change `_ContextParallel(seq_dim=2, ...)` to `seq_dim=1` -> I also had to change flux model impl to adapt. - The above change follows pytorch#2709 which is only for the generator. With this PR I'm able to remove `VLLMGQAttention` module added in that PR. - Make `inner_attention` a python config, instead of string `attn_backend` which is now used in qwen3 `model_registry` as a convenient way to switch config. - Thanks to that, we can support FlexAttention with configurable options, including `block_size` and `kernel_options` to enable Flash attention backend pytorch#2171. Added a config `qwen3_debugmodel_flex_flash` to the registry. - Move `annotate_flex_attention_for_regional_inductor` from general attention file to `graph_trainer` where it's used. - remove outdated artificial restrictions on CP / CP + TP - some naming improvement NOTE: - GraphTrainer tests fail on main as well - Model test on Llama4+PP+compile fails on main as well
for several things: - Save a pair of transpose for varlen attention, by moving the transpose for sdpa / flex inside their own module. I had to change `_ContextParallel(seq_dim=2, ...)` to `seq_dim=1` -> I also had to change flux model impl to adapt. - The above change follows pytorch#2709 which is only for the generator. With this PR I'm able to remove `VLLMGQAttention` module added in that PR. - Make `inner_attention` a python config, instead of string `attn_backend` which is now used in qwen3 `model_registry` as a convenient way to switch config. - Thanks to that, we can support FlexAttention with configurable options, including `block_size` and `kernel_options` to enable Flash attention backend pytorch#2171. Added a config `qwen3_debugmodel_flex_flash` to the registry. - Move `annotate_flex_attention_for_regional_inductor` from general attention file to `graph_trainer` where it's used. - remove outdated artificial restrictions on CP / CP + TP - some naming improvement NOTE: - GraphTrainer tests fail on main as well - Model test on Llama4+PP+compile fails on main as well




Stacked PRs:
Summary
command
Misc things
for forward our 2d indexing code is surprisingly much slower than our 1d case, need to investigate, otherwise we are seein gmuch faster backward at 8192 ~2x for doc masking and equal for fwd. Having to bump the q_len doesnt help here, we are pretty much memory bound for these distributions since the block_mask is basically 80% sparse
A quick look in ncu shows we are register splilling like crazy