vllm-0001: punica_wrapper/utils.py convert_mapping() calls lora_index_to_id.index(x) per token in batch — O(B*L) where B=batch_size, L=loaded_loras. Code has "TODO index can be slow" comment. Fix: pre-build dict for O(1) lookup. 7x at B=2000/L=64. Ollama: all slices.Contains on bounded slices (1-8 items). LangChain: orchestration code, all membership bounded by k param.
28 lines
1.1 KiB
Diff
28 lines
1.1 KiB
Diff
--- a/vllm/lora/punica_wrapper/utils.py
|
|
+++ b/vllm/lora/punica_wrapper/utils.py
|
|
@@ -86,15 +86,18 @@
|
|
embeddings_indices).
|
|
"""
|
|
index_mapping_indices: list[int] = list(mapping.index_mapping).copy()
|
|
embedding_indices = index_mapping_indices.copy()
|
|
lora_indices = index_mapping_indices.copy()
|
|
|
|
+ # Pre-build reverse lookup: lora_id -> position in lora_index_to_id
|
|
+ # Replaces O(L) list.index() with O(1) dict lookup per token
|
|
+ id_to_index: dict[int, int] = {
|
|
+ v: i for i, v in enumerate(lora_index_to_id) if v is not None and v > 0
|
|
+ }
|
|
+
|
|
prompt_mapping: list[int] = [
|
|
- lora_index_to_id.index(x) if x > 0 else -1 for x in mapping.prompt_mapping
|
|
+ id_to_index[x] if x > 0 else -1 for x in mapping.prompt_mapping
|
|
]
|
|
lora_idx = None
|
|
for i in range(len(index_mapping_indices)):
|
|
- # TODO index can be slow. optimize
|
|
lora_idx = (
|
|
- lora_index_to_id.index(index_mapping_indices[i])
|
|
+ id_to_index[index_mapping_indices[i]]
|
|
if index_mapping_indices[i] > 0
|
|
else -1
|
|
)
|