Skip to content

using sdpa if available - #2359

Merged
jongwook merged 2 commits into
mainfrom
jongwook/sdpa
Sep 30, 2024
Merged

jongwook merged 2 commits into
mainfrom
jongwook/sdpa

Conversation

@jongwook

Copy link
Copy Markdown
Collaborator

No description provided.

@formater

formater commented Nov 22, 2024 •

Copy link
Copy Markdown

I encountered an issue with this patch.

File "/usr/local/src/transcriber_rt/lib/python3.11/site-packages/whisper/model.py", line 111, in forward
    wv, qk = self.qkv_attention(q, k, v, mask)
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "/usr/local/src/transcriber_rt/lib/python3.11/site-packages/whisper/model.py", line 124, in qkv_attention
    a = scaled_dot_product_attention(
        ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

In this part of the code of model.py:

def qkv_attention(
        self, q: Tensor, k: Tensor, v: Tensor, mask: Optional[Tensor] = None
    ) -> Tuple[torch.Tensor, Optional[torch.Tensor]]:
        n_batch, n_ctx, n_state = q.shape
        scale = (n_state // self.n_head) ** -0.25
        q = q.view(*q.shape[:2], self.n_head, -1).permute(0, 2, 1, 3)
        k = k.view(*k.shape[:2], self.n_head, -1).permute(0, 2, 1, 3)
        v = v.view(*v.shape[:2], self.n_head, -1).permute(0, 2, 1, 3)

        if SDPA_AVAILABLE and MultiHeadAttention.use_sdpa:
            a = scaled_dot_product_attention(
                q, k, v, is_causal=mask is not None and n_ctx > 1
            )

Somehow we got Tensor and not a boolean in is_causal. If mask is a tensor, the resulting is_causal can itself be a tensor, depending on how it is handled. So maybe a potential solution would be to cast it as bool like:
is_causal = bool(mask is not None and n_ctx > 1) ?

@JonahMMay

Copy link
Copy Markdown

I have the same issue as @formater where is_causal is receiving a Tensor instead of a bool.

hiden47 pushed a commit to hiden47/whisper that referenced this pull request Feb 10, 2025
* using sdpa if available

* Update model.py
Bretterbear pushed a commit to desiredeffect/whisper that referenced this pull request Apr 4, 2025
* using sdpa if available

* Update model.py
heejipark23 pushed a commit to heejipark23/whisper that referenced this pull request Sep 21, 2025
* using sdpa if available

* Update model.py

@affinanti333 affinanti333 left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Final

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants