Skip to content

Commit fc72ffa

Browse files
kingderWeichao Luo
andauthored
optimize radix cache. (#968)
Co-authored-by: Weichao Luo <[email protected]>
1 parent fc5bf85 commit fc72ffa

File tree

1 file changed

+14
-7
lines changed

1 file changed

+14
-7
lines changed

lightllm/server/router/dynamic_prompt/radix_cache.py

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,20 @@ def is_leaf(self):
8282
return len(self.children) == 0
8383

8484

85-
def match(key, seq):
86-
i = 0
87-
for k, w in zip(key, seq):
88-
if k != w:
89-
break
90-
i += 1
91-
return i
85+
def match(t1: torch.Tensor, t2: torch.Tensor) -> int:
86+
# Ensure same shape for comparison: flatten and get min length
87+
t1_flat = t1.flatten()
88+
t2_flat = t2.flatten()
89+
min_len = min(t1_flat.size(0), t2_flat.size(0))
90+
91+
# Compare elements and find first mismatch
92+
diff = t1_flat[:min_len] != t2_flat[:min_len]
93+
mismatch_indices = torch.nonzero(diff)
94+
95+
if mismatch_indices.numel() == 0:
96+
return min_len # All matched up to min_len
97+
else:
98+
return mismatch_indices[0].item()
9299

93100

94101
class RadixCache:

0 commit comments

Comments
 (0)