Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Make a separate attention module to cover sdpa logic
  • Loading branch information
fegin committed Apr 2, 2025
commit 8188bb22557a6f030ece66dd69355d3a52bfa6f3
11 changes: 6 additions & 5 deletions torchtitan/models/attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,18 +25,19 @@ class SDPA(torch.nn.Module):
use_flex_attn: ClassVar[bool] = False
flex_attn: ClassVar[Optional[Callable]] = None

def __init__(self) -> None:
self.use_flex_attn = model_args.use_flex_attn
def __init__(self, use_flex_attn) -> None:
super().__init__()
self.use_flex_attn = use_flex_attn

def forward(
self, q: torch.Tensor, k: torch.Tensor, v: torch.Tensor
) -> torch.Tensor:
if self.use_flex_attn:
# assert False, (type(xq), type(xk), type(xv))
_, _, seqlen, _ = q.shape
self._init_flex_attn(seqlen=seqlen)
return self.flex_attn(xq, xk, xv, block_mask=self.block_mask)
return self.flex_attn(q, k, v, block_mask=self.block_mask)
else:
return F.scaled_dot_product_attention(xq, xk, xv, is_causal=True)
return F.scaled_dot_product_attention(q, k, v, is_causal=True)

@torch.no_grad()
def _init_flex_attn(self, seqlen: int) -> None:
Expand Down
1 change: 1 addition & 0 deletions torchtitan/models/llama/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from typing import Optional

import torch
import torch.nn.functional as F
from torch import nn

from torchtitan.components.tokenizer import Tokenizer
Expand Down