|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * All rights reserved. |
| 4 | + * |
| 5 | + * This source code is licensed under the BSD-style license found in the |
| 6 | + * LICENSE file in the root directory of this source tree. |
| 7 | + */ |
| 8 | + |
| 9 | +// Unit tests for plan_prefill() (warm-resume decision). No model/session/ET |
| 10 | +// runtime dependency -- the header is pure, so this compiles and runs |
| 11 | +// standalone. Self-contained assertions (no gtest) so it has no build deps. |
| 12 | + |
| 13 | +#include <executorch/extension/llm/server/cpp/worker_prefill_plan.h> |
| 14 | + |
| 15 | +#include <cstdio> |
| 16 | +#include <cstring> |
| 17 | +#include <string> |
| 18 | +#include <vector> |
| 19 | + |
| 20 | +using executorch::extension::llm::plan_prefill; |
| 21 | +using executorch::extension::llm::PrefillPlan; |
| 22 | + |
| 23 | +namespace { |
| 24 | +int g_failures = 0; |
| 25 | + |
| 26 | +void expect( |
| 27 | + const char* name, |
| 28 | + const PrefillPlan& p, |
| 29 | + PrefillPlan::Action action, |
| 30 | + size_t suffix_start, |
| 31 | + const char* reason) { |
| 32 | + bool ok = p.action == action && p.suffix_start == suffix_start && |
| 33 | + std::strcmp(p.reason, reason) == 0; |
| 34 | + if (!ok) { |
| 35 | + ++g_failures; |
| 36 | + printf( |
| 37 | + " [FAIL] %s: got action=%d suffix_start=%zu reason=%s\n", |
| 38 | + name, |
| 39 | + (int)p.action, |
| 40 | + p.suffix_start, |
| 41 | + p.reason); |
| 42 | + } else { |
| 43 | + printf(" [PASS] %s\n", name); |
| 44 | + } |
| 45 | +} |
| 46 | +} // namespace |
| 47 | + |
| 48 | +int main() { |
| 49 | + using V = std::vector<uint64_t>; |
| 50 | + |
| 51 | + // First request: nothing resident -> full prefill, "new". |
| 52 | + expect( |
| 53 | + "new (resident empty)", |
| 54 | + plan_prefill(V{}, V{1, 2, 3}, false), |
| 55 | + PrefillPlan::kFull, |
| 56 | + 0, |
| 57 | + "new"); |
| 58 | + |
| 59 | + // Exact token extension -> prefill only the suffix. |
| 60 | + expect( |
| 61 | + "exact_prefix (suffix reuse)", |
| 62 | + plan_prefill(V{1, 2, 3}, V{1, 2, 3, 4, 5}, false), |
| 63 | + PrefillPlan::kSuffix, |
| 64 | + 3, |
| 65 | + "exact_prefix"); |
| 66 | + |
| 67 | + // Single-token extension still reuses. |
| 68 | + expect( |
| 69 | + "exact_prefix (one-token suffix)", |
| 70 | + plan_prefill(V{1, 2, 3}, V{1, 2, 3, 4}, false), |
| 71 | + PrefillPlan::kSuffix, |
| 72 | + 3, |
| 73 | + "exact_prefix"); |
| 74 | + |
| 75 | + // Divergent token -> mismatch, full reset. |
| 76 | + expect( |
| 77 | + "mismatch (divergent token)", |
| 78 | + plan_prefill(V{1, 2, 3}, V{1, 2, 9, 4}, false), |
| 79 | + PrefillPlan::kFull, |
| 80 | + 0, |
| 81 | + "mismatch"); |
| 82 | + |
| 83 | + // Prompt shorter than resident (rewind) -> mismatch, full reset. |
| 84 | + expect( |
| 85 | + "mismatch (prompt shorter)", |
| 86 | + plan_prefill(V{1, 2, 3}, V{1, 2}, false), |
| 87 | + PrefillPlan::kFull, |
| 88 | + 0, |
| 89 | + "mismatch"); |
| 90 | + |
| 91 | + // Dirty wins even over an otherwise-exact extension. |
| 92 | + expect( |
| 93 | + "dirty (overrides exact prefix)", |
| 94 | + plan_prefill(V{1, 2, 3}, V{1, 2, 3, 4}, true), |
| 95 | + PrefillPlan::kFull, |
| 96 | + 0, |
| 97 | + "dirty"); |
| 98 | + |
| 99 | + // Prompt identical to resident -> reset + full (no empty-suffix prefill). |
| 100 | + expect( |
| 101 | + "equal (prompt == resident)", |
| 102 | + plan_prefill(V{1, 2, 3}, V{1, 2, 3}, false), |
| 103 | + PrefillPlan::kFull, |
| 104 | + 0, |
| 105 | + "equal"); |
| 106 | + |
| 107 | + // Dirty + empty resident still resets as dirty (dirty checked first). |
| 108 | + expect( |
| 109 | + "dirty (empty resident)", |
| 110 | + plan_prefill(V{}, V{1, 2}, true), |
| 111 | + PrefillPlan::kFull, |
| 112 | + 0, |
| 113 | + "dirty"); |
| 114 | + |
| 115 | + printf( |
| 116 | + "\n%s (%d failure(s))\n", |
| 117 | + g_failures == 0 ? "ALL PASS" : "FAILED", |
| 118 | + g_failures); |
| 119 | + return g_failures == 0 ? 0 : 1; |
| 120 | +} |
0 commit comments