Skip to content

flash attention exposing window_size_left and window_size_right - #171578

Closed
francesco-bertolotti wants to merge 5 commits into
pytorch:mainfrom
francesco-bertolotti:f14-varlen-sliding-window
Closed

francesco-bertolotti wants to merge 5 commits into
pytorch:mainfrom
francesco-bertolotti:f14-varlen-sliding-window

Conversation

@francesco-bertolotti

Copy link
Copy Markdown

Fixes #171577

This PR extends the varlen function signature with:

window_size_left: int = -1,
window_size_right: int = -1,

This makes it possible to easily use and implement sliding window attention.


I have updated test/test_varlen_attention.py so that the tests also run with window_size_left and window_size_right enabled.


Note:

Increasing the window size can cause test failures. These failures appear to be due to slightly larger numerical deviations than the current test tolerances allow. I suggest increasing the tolerance in these tests to account for this.

@pytorch-bot

pytorch-bot Bot commented Dec 31, 2025 •

Copy link
Copy Markdown

🔗 Helpful Links

🧪 See artifacts and rendered test results at hud.pytorch.org/pr/171578

Note: Links to docs will display an error until the docs builds have been completed.

❌ 1 New Failure

As of commit 6f682ac with merge base 77470cd (image):

NEW FAILURE - The following job has failed:

This comment was automatically generated by Dr. CI and updates every 15 minutes.

@linux-foundation-easycla

linux-foundation-easycla Bot commented Dec 31, 2025 •

Copy link
Copy Markdown

CLA Signed

The committers listed above are authorized under a signed CLA.

@f14-bertolotti

Copy link
Copy Markdown

@pytorchbot label "release notes: nn"

@pytorch-bot pytorch-bot Bot added the release notes: nn release notes category label Dec 31, 2025
@mikaylagawarecki mikaylagawarecki added the triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module label Jan 5, 2026
@mikaylagawarecki
mikaylagawarecki removed their request for review January 5, 2026 19:37
@francesco-bertolotti francesco-bertolotti changed the title exposed sliding window params flash attention exposing window_size_left and window_size_right Jan 6, 2026

@liangel-02 liangel-02 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

thanks for adding this!

  1. it'd be best if we could add a kwargs and include window_size as one so it's easier to augment w/ more params in the future
  2. in FA3 and 4, window_size is a tuple (see here and here), lets do the same
  3. lets add an error for if window_size is passed in with is_causal=True

@francesco-bertolotti

Copy link
Copy Markdown
Author

Hi @liangel-02,

No problem, I should be able to push the commit fairly soon.

Comment thread test/test_varlen_attention.py Outdated
scale: float | None = None,
window_size_left: int = -1,
window_size_right: int = -1,
window_size: tuple[int, int] = -1,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
window_size: tuple[int, int] = -1,
window_size: tuple[int, int] = (-1, -1),

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good catch. Fixed in the next commit.

Comment thread torch/nn/attention/varlen.py Outdated
Comment on lines +154 to +155
window_size: tuple[int, int] = (-1, -1),
kwargs: Any = {},

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lets only have **kwargs in the function definition and get window_size from the **kwargs:

window_size = kwargs.pop("window_size", (-1, -1))

Comment thread torch/nn/attention/varlen.py Outdated
window_size = kwargs.pop("window_size", (-1, -1))
out, lse, _ = torch.ops.torch_attn._varlen_attn(
query, key, value, cu_seq_q, cu_seq_k, max_q, max_k, is_causal, scale
query, key, value, cu_seq_q, cu_seq_k, max_q, max_k, is_causal, scale, window_size[0], window_size[1], **kwargs

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
query, key, value, cu_seq_q, cu_seq_k, max_q, max_k, is_causal, scale, window_size[0], window_size[1], **kwargs
query, key, value, cu_seq_q, cu_seq_k, max_q, max_k, is_causal, scale, window_size[0], window_size[1]

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do this, but does it not make more sense to pass kwargs forward to the kernels?

@liangel-02 liangel-02 Jan 8, 2026 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

iiuc, we can't pass kwargs into the private function _varlen_attn because we are registering it as a custom op. we'll see an error thrown here

raise ValueError(f"infer_schema(func): {what} Got func with signature {sig})")
ValueError: infer_schema(func): We do not support positional-only args, varargs, or varkwargs. Got func with signature (query: torch.Tensor, ...,*kwargs: Any) -> tuple[torch.Tensor, torch.Tensor, torch.Tensor])

since compile needs to trace through everything to register the schema but it's impossible to know at compile time what these kwargs might be

Comment on lines +47 to +48
window_size_left: int = -1,
window_size_right: int = -1,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

do we need to add these to the fake impl too ?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is added correct?

Comment thread torch/nn/attention/varlen.py Outdated

if use_cudnn:
log.info("Using cuDNN backend for varlen_attn")
assert window_size_left == -1 and window_size_right == -1, "cuDNN backend does not support windowed attention"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

should we add a similar assertion in the bwd ?

rng_state: torch.Tensor,
scale: float | None = None,
window_size_left: int = -1,
window_size_right: int = -1,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

might need to add this to fake impl too

@francesco-bertolotti

Copy link
Copy Markdown
Author

Hi @liangel-02,

I see that lintrunner complains about asserts. Should I raise an exception instead?

max_k,
is_causal,
scale,
window_size[0],

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: lets keep this using the tuple so we only unpack once before we invoke lower level ops

@drisspg

drisspg commented Jan 9, 2026

Copy link
Copy Markdown
Contributor

Summary

We had a discussion offline. The current plan is that we want to embrace the kwarg only and have this op iterate and move faster than other parts of pytorch.

So the plan we want to do is:

  1. Remove the is_causal flag. This is a special case of (-inf, 0). We should probably update to Optional tuple() where -1 means the full extent e.g. -1 = -inf in left slot and inf in right slot.
  2. We dont want **kwargs but instead just have a kwarg only slots.

That means that the signature should be

def _varlen_attn(
    query: torch.Tensor,
    key: torch.Tensor,
    value: torch.Tensor,
    cu_seq_q: torch.Tensor,
    cu_seq_k: torch.Tensor,
    max_q: int,
    max_k: int,
    *,
	# Deleted, bc break but this API is prototype   is_causal: bool = False,
    scale: float | None = None,
	window_size: Tuple[int,int] | None = None
) 

@francesco-bertolotti

Copy link
Copy Markdown
Author

Sounds nice, I can submit a first commit tomorrow.

@francesco-bertolotti

Copy link
Copy Markdown
Author

Hi @drisspg,

I am trying your signature. It appears that Optional[Tuple[int,int]] is not supported by pytorch infer_schema.
I can work around it with Optional[List[int]] but I do not think that this would be satisfying.

Regarding the signature of varlen_attn exposed to the users, you want to keep it as is:

def varlen_attn(
    query: torch.Tensor,
    key: torch.Tensor,
    value: torch.Tensor,
    cu_seq_q: torch.Tensor,
    cu_seq_k: torch.Tensor,
    max_q: int,
    max_k: int,
    is_causal: bool = False,
    return_aux: AuxRequest | None = None,
    scale: float | None = None,
    **kwargs,
) -> torch.Tensor | tuple[torch.Tensor, torch.Tensor]:

@liangel-02

Copy link
Copy Markdown
Contributor

hey @francesco-bertolotti thanks so much for the work on this PR. after some more offline discussions, we’re planning on extending the API further, so i think it’ll be easiest to close this PR for now. we'll take it from here and implement this with additional context in mind. thanks again for all the help + inspo!!

@liangel-02 liangel-02 closed this Jan 12, 2026
liangel-02 added a commit that referenced this pull request Jan 12, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 12, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 13, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 13, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 14, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 14, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 14, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 14, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 15, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 15, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 15, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 15, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 15, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 15, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 15, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 15, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 16, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 16, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 16, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 16, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 20, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 20, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 20, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 20, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 21, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
liangel-02 added a commit that referenced this pull request Jan 21, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing




cc albanD mruberry jbschlosser walterddr mikaylagawarecki

[ghstack-poisoned]
pytorchmergebot pushed a commit that referenced this pull request Jan 21, 2026
original pr: #171578
adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing

Pull Request resolved: #172238
Approved by: https://github.com/drisspg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

open source release notes: nn release notes category triaged This issue has been looked at a team member, and triaged and prioritized into an appropriate module

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants