@tduncan / Nvidia-Optimization-Stack-VLLM Mirror · read-only / commits / bf7c99dfc4
Fork 0

A high-throughput and memory-efficient inference and serving engine for LLMs

Mirrored from https://github.com/tduncan-lum/Nvidia-Optimization-Stack---VLLM.git

Synced Nov 14, 05:30 UTC

[Perf] Speed up function `_convert_tokens_to_string_with_added_encoders` by 13.7x (#20413)

Signed-off-by: Saurabh Misra <misra.saurabh1@gmail.com> Signed-off-by: Aseem Saxena <aseem.bits@gmail.com> Co-authored-by: codeflash-ai[bot] <148906541+codeflash-ai[bot]@users.noreply.github.com> Co-authored-by: Aseem Saxena <aseem.bits@gmail.com>

Saurabh Misra committed Aug 20, 2025 at 13:17 UTC bf7c99dfc40bff6844b2ae57554516922eb93b71
1 file changed +15 -10
vllm/transformers_utils/detokenizer_utils.py
+15 -10
@@ -23,27 +23,32 @@ def _convert_tokens_to_string_with_added_encoders(
23 # NOTE(woosuk): The following code is slow because it runs a for loop over
24 # the output_tokens. In Python, running a for loop over a list can be slow
25 # even when the loop body is very simple.
26 + # Performance improvements: avoid repeated attribute and function lookups;
27 + # localize frequently used objects;
28 +
29 sub_texts: list[str] = []
30 current_sub_text: list[str] = []
28 - all_special_tokens = set(tokenizer.all_special_tokens)
31 + convert_tokens_to_string = tokenizer.convert_tokens_to_string
32 + added_vocab_set = set(tokenizer.get_added_vocab())
33 + all_special_tokens = set(
34 + tokenizer.all_special_tokens) if skip_special_tokens else ()
35 +
36 for token in output_tokens:
30 - if skip_special_tokens and token in all_special_tokens:
37 + # Use precomputed set for skip-special check
38 + if token in all_special_tokens:
39 continue
32 - if token in tokenizer.get_added_vocab():
40 + if token in added_vocab_set:
41 if current_sub_text:
34 - sub_text = tokenizer.convert_tokens_to_string(current_sub_text)
35 - sub_texts.append(sub_text)
36 - current_sub_text = []
42 + sub_texts.append(convert_tokens_to_string(current_sub_text))
43 + current_sub_text.clear()
44 sub_texts.append(token)
45 else:
46 current_sub_text.append(token)
47 if current_sub_text:
41 - sub_text = tokenizer.convert_tokens_to_string(current_sub_text)
42 - sub_texts.append(sub_text)
48 + sub_texts.append(convert_tokens_to_string(current_sub_text))
49 if spaces_between_special_tokens:
50 return " ".join(sub_texts)
45 - else:
46 - return "".join(sub_texts)
51 + return "".join(sub_texts)
52
53
54 # 5 is an arbitrary value that should work for all