abetlen / abetlen/llama-cpp-python
enabling cache slows generation
- 主要語言
- Python
- 星號
- 10.6k
- 分支
- 1.4k
- PR 合併指標
- PR 指標待擷取
描述
# Expected Behavior
Caching should improve performance
# Current Behaviour
When running the server with caching enabled:
```bash
python -m llama_cpp.server --model <...> --cache true --cache_type disk
```
generation is much slower, as can be seen b running benchmarks with `hyperfine` and `httpie
```bash
hyperfine --warmup 1 --runs 5 \
http --ignore-stdin localhost:8000/v1/completions prompt= temperature=0 max_tokens=16
```
note that I'm keeping the temperature fixed and using a small number of max tokens.
- Cache disabled:
```
Time (mean ± σ): 1.219 s ± 0.058 s [User: 0.259 s, System: 0.057 s]
Range (min … max): 1.160 s … 1.307 s 5 runs
```
- RAM cache:
```
Time (mean ± σ): 3.681 s ± 0.154 s [User: 0.274 s, System: 0.061 s]
Range (min … max): 3.459 s … 3.800 s 5 runs
```
- Disk cache
```
Time (mean ± σ): 3.687 s ± 0.206 s [User: 0.276 s, System: 0.055 s]
Range (min … max): 3.522 s … 4.026 s 5 runs
```
# Environment and Context
- M1 with MacOS/metal
# Failure Information (for bugs)
Please help provide information about the failure if this is a bug. If it is not a bug, please remove the rest of this template.
# Analysis
What I found out while debugging this:
1. When cache is active, it's never hit, possibly due to a `>` instead of `>=` in https://github.com/abetlen/llama-cpp-python/blob/3db03b73027036cf336fda2448894c36d3899cab/llama_cpp/llama.py#L993
2. In non-streaming use cases, the cache is updated before the generated response is returned, meaning we have to wait before getting the response. For streaming use cases, this does not happen.
3. Most of the time spent in generation when caching is active is in `save_state`
Now, 1. has a simple workaround, and if implemented, the cache is actually loaded.
For 2., the state saving could be offloaded to a new thread, meaning that the response response could be returned immediately, although I'm not 100% sure of any possible issues with this approach.
Regarding 3. getting rid of `.copy()` speeds it up a bit, although I'm not sure if this can cause issues.
https://github.com/abetlen/llama-cpp-python/blob/3db03b73027036cf336fda2448894c36d3899cab/llama_cpp/llama.py#L1792-L1820
貢獻指南
評估
這個 Issue 還沒有評估資料。