Entropic 2.9.4
Local-first agentic inference engine
Loading...
Searching...
No Matches
warm_keep_util.h
Go to the documentation of this file.
1// SPDX-License-Identifier: Apache-2.0
12#ifndef ENTROPIC_INFERENCE_WARM_KEEP_UTIL_H
13#define ENTROPIC_INFERENCE_WARM_KEEP_UTIL_H
14
15#include <algorithm>
16#include <cstddef>
17#include <vector>
18
19namespace entropic {
20
29template <typename Tok>
30inline std::size_t common_prefix_len(const std::vector<Tok>& a,
31 const std::vector<Tok>& b) {
32 std::size_t n = std::min(a.size(), b.size());
33 std::size_t i = 0;
34 while (i < n && a[i] == b[i]) {
35 ++i;
36 }
37 return i;
38}
39
61template <typename Tok>
62inline std::size_t warm_keep_cut(const std::vector<Tok>& resident,
63 const std::vector<Tok>& incoming,
64 long kv_pos_max) {
65 if (resident.empty() || incoming.size() < 2 || kv_pos_max < 0) {
66 return 0;
67 }
68 std::size_t common = common_prefix_len(resident, incoming);
69 if (common == 0) {
70 return 0;
71 }
72 if (static_cast<std::size_t>(kv_pos_max) + 1 < common) {
73 return 0; // KV does not actually hold the matched prefix
74 }
75 if (common >= incoming.size()) {
76 common = incoming.size() - 1; // always re-decode the last token
77 }
78 return common;
79}
80
81} // namespace entropic
82
83#endif // ENTROPIC_INFERENCE_WARM_KEEP_UTIL_H
Activate model on GPU (WARM → ACTIVE).
std::size_t common_prefix_len(const std::vector< Tok > &a, const std::vector< Tok > &b)
Length of the longest common token prefix of two sequences.
std::size_t warm_keep_cut(const std::vector< Tok > &resident, const std::vector< Tok > &incoming, long kv_pos_max)
Decide how many resident-KV tokens warm-keep may reuse this turn.