Skip to content

Fix key_padding_mask when using attn_impl='flash' #163

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

Merged
merged 5 commits into from
Feb 14, 2023
Merged
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
13 changes: 10 additions & 3 deletions examples/llm/src/mosaic_gpt.py
Original file line number Diff line number Diff line change
Expand Up @@ -421,10 +421,17 @@ def forward(self,
seq_len=S,
key_padding_mask=key_padding_mask,
dtype=x.dtype)
if self.cfg.attn_impl == 'flash' and key_padding_mask is None:
# HazyResearch FlashMHA appears to use more memory when `key_padding_mask=None`
# in certain settings like MosaicGPT-7B. So we always provide a tensor.
# See https://github.com/mosaicml/examples/pull/163 for more details.
mod_key_padding_mask = torch.ones_like(input_ids, dtype=torch.bool)
elif self.cfg.attn_impl == 'triton':
mod_key_padding_mask = None
else:
mod_key_padding_mask = key_padding_mask
for block in self.transformer.blocks: # type: ignore
x = block(
x, None if self.cfg.attn_impl == 'triton' else key_padding_mask,
attn_mask)
x = block(x, mod_key_padding_mask, attn_mask)
x = self.transformer.ln_f(x) # type: ignore
# output embedding weight tied to input embedding
assert isinstance(self.transformer.wte, nn.Module) # pyright
Expand Down