You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
perf: optimize tokenization and detokenization logic
Refactor `tokenize`, `token_to_piece`, and `detokenize` methods in `_internals.py` to significantly reduce Python overhead and improve stability.
Key changes:
- Replace O(N) Python loops in `detokenize` with `llama.cpp` native batch C-API (`llama_detokenize`).
- Implement dynamic buffer allocation to safely handle arbitrary token lengths (removing the hardcoded 32-byte limit).
- Add automatic buffer resizing for `tokenize` to prevent overflow errors.
Performance observations (based on user benchmarks):
- Small Batch Processing (127 tokens):
Latency reduced from ~117ms to ~37ms (approx. 3.1x speedup in processing loop).
- Large Batch Processing (2420 tokens):
Throughput improved from ~6905 t/s to ~8258 t/s.
- General Latency:
Total execution time for standard chat scenarios reduced by ~1.1s (from 8.4s to 7.3s).
Signed-off-by: JamePeng <jame_peng@sina.com>
0 commit comments