|
8 | 8 |
|
9 | 9 | #pragma once |
10 | 10 |
|
11 | | -// Pure warm-resume prefill decision for the model worker (no model/session/ET |
12 | | -// dependency, so it is unit-testable in isolation). Given a named session's |
13 | | -// resident token ids (exactly the tokens currently in its KV/recurrent state), |
14 | | -// the new request's prompt token ids, and whether the session is dirty, decide |
15 | | -// whether to reset + full-prefill or to keep the state and prefill only the |
16 | | -// suffix. The decision is exact-token (never string / retokenized text), so a |
17 | | -// kSuffix plan is always a correct continuation; anything uncertain falls back |
18 | | -// to kFull. See worker_loop.h for how the plan is executed. |
19 | | - |
20 | | -#include <cstddef> |
21 | | -#include <cstdint> |
22 | | -#include <vector> |
23 | | - |
24 | | -namespace executorch { |
25 | | -namespace extension { |
26 | | -namespace llm { |
27 | | - |
28 | | -struct PrefillPlan { |
29 | | - enum Action { |
30 | | - kFull, // reset + prefill the whole prompt |
31 | | - kSuffix // keep state, prefill prompt_ids[suffix_start:] at pos>0 |
32 | | - } action; |
33 | | - size_t suffix_start; // index in prompt_ids where prefill begins (0 for kFull) |
34 | | - // Reported as session_reset_reason: "new" (no resident), "exact_prefix" |
35 | | - // (suffix reuse), "dirty", "mismatch", "equal" (prompt == resident). |
36 | | - const char* reason; |
37 | | -}; |
38 | | - |
39 | | -inline PrefillPlan plan_prefill( |
40 | | - const std::vector<uint64_t>& resident, |
41 | | - const std::vector<uint64_t>& prompt, |
42 | | - bool dirty) { |
43 | | - if (dirty) { |
44 | | - return {PrefillPlan::kFull, 0, "dirty"}; |
45 | | - } |
46 | | - if (resident.empty()) { |
47 | | - return {PrefillPlan::kFull, 0, "new"}; |
48 | | - } |
49 | | - if (prompt.size() < resident.size()) { |
50 | | - return {PrefillPlan::kFull, 0, "mismatch"}; |
51 | | - } |
52 | | - for (size_t i = 0; i < resident.size(); ++i) { |
53 | | - if (prompt[i] != resident[i]) { |
54 | | - return {PrefillPlan::kFull, 0, "mismatch"}; |
55 | | - } |
56 | | - } |
57 | | - if (prompt.size() == resident.size()) { |
58 | | - // Prompt is exactly the resident state (no new tokens). The ideal would be |
59 | | - // to skip prefill and decode straight from the session's pending token, but |
60 | | - // the LLMSession API exposes no "is there a valid pending token?" query, so |
61 | | - // we conservatively reset + full prefill rather than risk |
62 | | - // prefill_tokens([]) or decoding from a stale/absent pending. Rare in |
63 | | - // practice (a new turn adds tokens); a session pending-state query is a |
64 | | - // possible later optimization. |
65 | | - return {PrefillPlan::kFull, 0, "equal"}; |
66 | | - } |
67 | | - return {PrefillPlan::kSuffix, resident.size(), "exact_prefix"}; |
68 | | -} |
69 | | - |
70 | | -} // namespace llm |
71 | | -} // namespace extension |
72 | | -} // namespace executorch |
| 11 | +#include <executorch/extension/llm/serving/prompt_history.h> |
0 commit comments