[Bugfix] Solve Ulysses-SP sequence length not divisible by SP degree (using padding and attention mask) (#672)
Signed-off-by: Didan Deng <33117903+wtomin@users.noreply.github.com>
This commit is contained in:
1 parent
89f47c910a
commit
4df794bdaa
4 files changed
+244
-12
No files matched your search
@@ -163,6 +163,36 @@ class UlyssesParallelAttention:
|
||||
joint_len=joint_len,
|
||||
joint_strategy=joint_strategy,
|
||||
)
|
||||
|
||||
if attn_metadata is not None:
|
||||
if is_joint:
|
||||
if attn_metadata.joint_attn_mask is None and attn_metadata.attn_mask is None:
|
||||
attn_metadata.attn_mask = None
|
||||
else:
|
||||
if attn_metadata.attn_mask is None:
|
||||
attn_metadata.attn_mask = torch.ones(
|
||||
[query.shape[0], query.shape[1] - attn_metadata.joint_attn_mask.shape[1]],
|
||||
dtype=torch.bool,
|
||||
device=query.device,
|
||||
)
|
||||
elif attn_metadata.joint_attn_mask is None:
|
||||
attn_metadata.joint_attn_mask = torch.ones(
|
||||
[query.shape[0], query.shape[1] - attn_metadata.attn_mask.shape[1]],
|
||||
dtype=torch.bool,
|
||||
device=query.device,
|
||||
)
|
||||
attn_metadata.attn_mask = (
|
||||
torch.cat([attn_metadata.joint_attn_mask, attn_metadata.attn_mask], dim=1)
|
||||
if joint_strategy == "front"
|
||||
else torch.cat([attn_metadata.attn_mask, attn_metadata.joint_attn_mask], dim=1)
|
||||
)
|
||||
|
||||
if attn_metadata.attn_mask is not None:
|
||||
# the final attn_mask is ready, the length should be aligedn with query length
|
||||
assert attn_metadata.attn_mask.shape[1] == query.shape[1], (
|
||||
f"attn_mask length: {attn_metadata.attn_mask.shape[1]} != query length: {query.shape[1]}"
|
||||
)
|
||||
attn_metadata.attn_mask = attn_metadata.attn_mask.bool().contiguous()
|
||||
return query, key, value, attn_metadata, ctx
|
||||
|
||||
def post_attention(self, attn_output: torch.Tensor, ctx: ParallelAttentionContext | None) -> torch.Tensor:
|
||||
|
||||
Reference in new issue
Block a user