Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

adds softmax_scale to flash attention #209

Merged
merged 1 commit into from
Mar 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
8 changes: 5 additions & 3 deletions examples/llm/src/models/layers/attention.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,16 +122,18 @@ def __init__(self, cfg: DictConfig, device: Optional[str] = None):

self.clip_qkv = cfg.get('attn_clip_qkv')
self.attn_qk_ln = cfg.get('attn_qk_ln')
self.softmax_scale = cfg.get('softmax_scale')
self.d_model = cfg.d_model
self.n_heads = cfg.n_heads

if self.attn_qk_ln or self.clip_qkv:
if self.attn_qk_ln or self.clip_qkv or self.softmax_scale:
self.W_qkv = nn.Linear(self.d_model,
3 * self.d_model,
bias=True,
device=device)
self.inner_attn = FlashAttention(attention_dropout=cfg.attn_pdrop,
Copy link
Contributor

Choose a reason for hiding this comment

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

I see we can now scale the attention by any custom value...what is the best value and how should we keep track of it?

Copy link
Contributor

Choose a reason for hiding this comment

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

1/srqt(d/n_heads) is standard.
1/(d/n_heads) is the recommended muP mod.
Since its part of the model config, it'll get dumped into wandb if we ever need to check what we used.

device=device)
device=device,
softmax_scale=self.softmax_scale)
self.out_proj = nn.Linear(self.d_model,
self.d_model,
bias=True,
Expand All @@ -157,7 +159,7 @@ def __init__(self, cfg: DictConfig, device: Optional[str] = None):
def forward(self, x, key_padding_mask, attn_mask=None):
assert attn_mask is None

if self.attn_qk_ln or self.clip_qkv:
if self.attn_qk_ln or self.clip_qkv or self.softmax_scale:
qkv = self.W_qkv(x)
if self.clip_qkv:
qkv.clamp_(min=-self.clip_qkv, max=self.clip_qkv)
Expand Down
7 changes: 7 additions & 0 deletions examples/llm/src/models/mosaic_gpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ def __init__(self, cfg: DictConfig):
'QKV clipping only implemented with flash and triton attention.'
)

if cfg.get('softmax_scale') and cfg.attn_impl not in [
'flash', 'triton'
]:
raise NotImplementedError(
'softmax_scale only implemented with flash and triton attention.'
)

self.alibi = cfg.get('alibi', False)
self.alibi_bias_max = cfg.get('alibi_bias_max',
8 if self.alibi else None)
Expand Down