Skip to content

Commit d03dab2

Browse files
committed
Remove dflash graph breaks
Signed-off-by: Fynn Schmitt-Ulms <fschmitt@redhat.com>
1 parent a504eea commit d03dab2

4 files changed

Lines changed: 10 additions & 47 deletions

File tree

src/speculators/models/dflash/attention.py

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -54,11 +54,6 @@ def create_anchor_block_mask_mod(
5454
q_len = n_anchors * block_size
5555
kv_len = total_seq_len + q_len
5656

57-
if (oob := (anchor_positions < 0) | (anchor_positions >= total_seq_len)).any():
58-
raise ValueError(
59-
f"anchor_positions out of range: {anchor_positions[oob].tolist()}"
60-
)
61-
6257
# For each query position, which anchor does it belong to?
6358
# query q in [j*block_size, (j+1)*block_size) belongs to anchor_positions[j]
6459
query_anchor_positions = torch.repeat_interleave(anchor_positions, block_size)

src/speculators/models/dflash/core.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,7 +304,7 @@ def forward(
304304
# shape: [1, total_seq_len, hidden_size]
305305

306306
mask_position_ids = get_base_indices_for_anchored_blocks(
307-
position_ids[0, anchor_positions], self.block_size, input_ids.numel()
307+
position_ids[0, anchor_positions], self.block_size
308308
)
309309
position_ids = torch.cat([position_ids, mask_position_ids.unsqueeze(0)], dim=1)
310310
# shape: [1, total_seq_len + num_anchors*block_size]
@@ -314,7 +314,7 @@ def forward(
314314
position_embeddings = self.rotary_emb(hidden_states, position_ids)
315315

316316
anchored_block_indices = get_base_indices_for_anchored_blocks(
317-
anchor_positions, self.block_size, input_ids.numel()
317+
anchor_positions, self.block_size
318318
) # shape: [num_anchors*block_size]
319319

320320
with torch.no_grad():

src/speculators/models/dflash/utils.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
def get_base_indices_for_anchored_blocks(
77
anchor_positions: torch.Tensor, # shape: [1, num_anchors]
88
block_size: int,
9-
total_seq_len: int | None = None,
109
) -> torch.Tensor: # shape: [num_anchors*block_size]
1110
anchor_positions = anchor_positions.to(dtype=torch.long).view(-1)
1211
# dtype: long, shape: [num_anchors]
@@ -16,12 +15,6 @@ def get_base_indices_for_anchored_blocks(
1615
anchor_positions[:, None] + offsets[None, :]
1716
) # shape: [num_anchors, block_size]
1817

19-
if (idx < 0).any() or (total_seq_len and (idx >= total_seq_len).any()):
20-
raise ValueError(
21-
"Some anchor_positions + offsets are out of range for total_seq_len"
22-
f"={total_seq_len}. Max={idx.max().item()}, min={idx.min().item()}"
23-
)
24-
2518
return idx.reshape(-1)
2619

2720

@@ -60,10 +53,14 @@ def select_anchors(
6053
anchor_valid = torch.zeros(num_anchors, dtype=torch.bool, device=device)
6154

6255
k = min(num_anchors, valid_indices.numel())
63-
if k > 0:
64-
perm = torch.randperm(valid_indices.numel(), device=loss_mask.device)
65-
anchors[:k] = valid_indices[perm[:k]]
66-
anchor_valid[:k] = True
56+
57+
# Constrain value of k for torch dynamo
58+
torch._check(k <= valid_indices.numel())
59+
torch._check(k >= 0)
60+
61+
perm = torch.randperm(valid_indices.numel(), device=loss_mask.device)
62+
anchors[:k] = torch.gather(valid_indices, 0, perm[:k])
63+
anchor_valid[:k] = True
6764

6865
return anchors, anchor_valid
6966
# shape: [num_anchors], [num_anchors]

tests/unit/models/test_dflash_utils.py

Lines changed: 0 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
"""Unit tests for get_base_indices_for_anchored_blocks."""
22

3-
import pytest
43
import torch
54

65
from speculators.models.dflash.utils import get_base_indices_for_anchored_blocks
@@ -44,31 +43,3 @@ def test_output_dtype_is_long(self):
4443
anchor_positions = torch.tensor([[2.0, 5.0]])
4544
result = get_base_indices_for_anchored_blocks(anchor_positions, block_size=2)
4645
assert result.dtype == torch.long
47-
48-
def test_total_seq_len_valid(self):
49-
anchor_positions = torch.tensor([[0, 3]])
50-
result = get_base_indices_for_anchored_blocks(
51-
anchor_positions, block_size=4, total_seq_len=7
52-
)
53-
expected = torch.tensor([0, 1, 2, 3, 3, 4, 5, 6])
54-
assert torch.equal(result, expected)
55-
56-
def test_total_seq_len_out_of_range(self):
57-
anchor_positions = torch.tensor([[0, 3]])
58-
with pytest.raises(ValueError, match="out of range"):
59-
get_base_indices_for_anchored_blocks(
60-
anchor_positions, block_size=4, total_seq_len=6
61-
)
62-
63-
def test_negative_anchor_raises(self):
64-
anchor_positions = torch.tensor([[-1, 3]])
65-
with pytest.raises(ValueError, match="out of range"):
66-
get_base_indices_for_anchored_blocks(anchor_positions, block_size=2)
67-
68-
def test_no_total_seq_len_skips_upper_bound_check(self):
69-
anchor_positions = torch.tensor([[100]])
70-
result = get_base_indices_for_anchored_blocks(
71-
anchor_positions, block_size=3, total_seq_len=None
72-
)
73-
expected = torch.tensor([100, 101, 102])
74-
assert torch.equal(result, expected)

0 commit comments

Comments
 (0)