Repository navigation
flash attention exposing window_size_left and window_size_right - #171578
francesco-bertolotti wants to merge 5 commits into
Conversation
🔗 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 FailureAs of commit 6f682ac with merge base 77470cd ( NEW FAILURE - The following job has failed:
This comment was automatically generated by Dr. CI and updates every 15 minutes. |
|
@pytorchbot label "release notes: nn" |
|
Hi @liangel-02, No problem, I should be able to push the commit fairly soon. |
| scale: float | None = None, | ||
| window_size_left: int = -1, | ||
| window_size_right: int = -1, | ||
| window_size: tuple[int, int] = -1, |
There was a problem hiding this comment.
| window_size: tuple[int, int] = -1, | |
| window_size: tuple[int, int] = (-1, -1), |
There was a problem hiding this comment.
Good catch. Fixed in the next commit.
| window_size: tuple[int, int] = (-1, -1), | ||
| kwargs: Any = {}, |
There was a problem hiding this comment.
lets only have **kwargs in the function definition and get window_size from the **kwargs:
window_size = kwargs.pop("window_size", (-1, -1))| 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 |
There was a problem hiding this comment.
| 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] |
There was a problem hiding this comment.
I can do this, but does it not make more sense to pass kwargs forward to the kernels?
There was a problem hiding this comment.
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
| window_size_left: int = -1, | ||
| window_size_right: int = -1, |
There was a problem hiding this comment.
do we need to add these to the fake impl too ?
|
|
||
| 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" |
There was a problem hiding this comment.
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, |
There was a problem hiding this comment.
might need to add this to fake impl too
|
Hi @liangel-02, I see that lintrunner complains about asserts. Should I raise an exception instead? |
| max_k, | ||
| is_causal, | ||
| scale, | ||
| window_size[0], |
There was a problem hiding this comment.
nit: lets keep this using the tuple so we only unpack once before we invoke lower level ops
SummaryWe 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:
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
) |
|
Sounds nice, I can submit a first commit tomorrow. |
|
Hi @drisspg, I am trying your signature. It appears that Optional[Tuple[int,int]] is not supported by pytorch Regarding the signature of 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]: |
|
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!! |
original pr: #171578 adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing [ghstack-poisoned]
original pr: #171578 adding sliding window attention to `varlen_attn`. modifying optional args to be positional instead for future proofing [ghstack-poisoned]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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]
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
Fixes #171577
This PR extends the
varlenfunction signature with:This makes it possible to easily use and implement sliding window attention.
I have updated
test/test_varlen_attention.pyso that the tests also run withwindow_size_leftandwindow_size_rightenabled.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.