abetlen / abetlen/llama-cpp-python

enabling cache slows generation

未关闭
#1,293 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看
主要语言
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 还没有评估数据。

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。