这个地方,为什么会将num_computed_tokens - 1, deepseek的模型,在判断要不要走Prefill的时候,是根据num_computed_tokens < num_prompt_tokens 判断的,那条件岂不是成立?
vLLM中,num_computed_tokens 代表已完成计算(prefill)的token数。由于在prefill阶段,最后一个token通常需要重新计算以获得logits(即不能直接用缓存),所以当num_computed_tokens == num_prompt_tokens时,实际上最后一个token还未真正“prefill”完成。因此,代码中会有if num_computed_tokens == request.num_tokens: num_computed_tokens -= 1的处理,确保只缓存到倒数第二个token,最后一个token要重新计算(见源码和官方文档说明)。
所以,deepseek等模型用num_computed_tokens < num_prompt_tokens判断是否需要prefill是合理的。因为即使num_computed_tokens == num_prompt_tokens,最后一个token也需要重新prefill,条件本质上是成立的。详细解释见官方文档。
需要更详细的原理或代码流程说明吗?
Sources:
- https://github.com/vllm-project/vllm/blob/main/vllm/v1/core/sched/scheduler.py
- https://docs.vllm.ai/en/latest/api/vllm/v1/core/sched/scheduler/#__span-0-1280
- https://docs.vllm.ai/en/latest/api/vllm/v1/core/kv_cache_manager/#__code_43
Hint: Mention @RunLLM in the post for followups.
由于在prefill阶段,最后一个token通常需要重新计算以获得logits(即不能直接用缓存) – 这个没太理解,能详细讲一下吗


