Skip to content

Commit

Permalink
optimize calculation of cu_seqlens from position_ids (#1084) [skip ci]
Browse files Browse the repository at this point in the history
  • Loading branch information
winglian committed Jan 10, 2024
1 parent 9032e61 commit 90036eb
Showing 1 changed file with 2 additions and 1 deletion.
3 changes: 2 additions & 1 deletion src/axolotl/monkeypatch/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def get_cu_seqlens(attn_mask):
return torch.stack(results).to(dtype=torch.int32), torch.stack(max_seq_lens)


@torch.jit.script
def get_cu_seqlens_from_pos_ids(position_ids):
"""generate a cumulative sequence length mask for flash attention using pos ids"""
if len(position_ids.shape) == 1:
Expand All @@ -81,7 +82,7 @@ def get_cu_seqlens_from_pos_ids(position_ids):
# Get the indices where the sequence starts
start_indices = torch.cat(
[
(seq_starts).nonzero(as_tuple=True)[0],
torch.nonzero(seq_starts).unbind(dim=1)[0],
torch.tensor([len(adjusted_row)], dtype=torch.int32, device=device),
]
)
Expand Down

0 comments on commit 90036eb

Please sign in to comment.