39#include <unordered_map>
53template <
typename Tok>
73 auto it = assigned_.find(key);
90 auto it = assigned_.find(key);
91 if (
it != assigned_.end()) {
95 int slot = free_slot();
97 const std::string
victim = lru_key();
102 assigned_[key] =
slot;
115 static const std::vector<Tok>
kEmpty;
116 return valid(
slot) ? resident_[
static_cast<std::size_t
>(
slot)]
129 resident_[
static_cast<std::size_t
>(
slot)] = std::move(
tokens);
141 resident_[
static_cast<std::size_t
>(
slot)].clear();
154 for (
auto&
v : resident_) {
v.clear(); }
181 auto it = assigned_.find(key);
183 const int slot =
it->second;
195 int slots()
const {
return slots_; }
213 bool valid(
int slot)
const {
223 void touch(
const std::string& key) { use_order_[key] = ++tick_; }
231 void forget(
const std::string& key) {
232 assigned_.erase(key);
233 use_order_.erase(key);
242 int free_slot()
const {
243 std::vector<bool>
taken(
static_cast<std::size_t
>(slots_),
false);
244 for (
const auto& [
k,
s] : assigned_) {
245 if (
s >= 0 &&
s < slots_) {
246 taken[
static_cast<std::size_t
>(
s)] =
true;
262 std::string lru_key()
const {
265 for (
const auto& [
k,
t] : use_order_) {
272 std::vector<std::vector<Tok>> resident_;
273 std::unordered_map<std::string, int> assigned_;
274 std::unordered_map<std::string, uint64_t> use_order_;
Per-session llama sequence slots and their resident tokens.
int slot_for(const std::string &key) const
Slot currently holding this session, if any.
const std::vector< Tok > & resident(int slot) const
Tokens recorded as resident in a slot.
int slots() const
Number of sequence slots in the pool.
std::size_t assigned_count() const
How many sessions currently hold a slot.
void invalidate_all()
Forget every slot's residency.
SessionResidency(int slots=1)
Size the pool.
void invalidate(int slot)
Forget a slot's residency (its KV was cleared or invalidated).
int forget_session(const std::string &key)
Drop one SESSION's residency and release its slot (gh#165).
int acquire(const std::string &key, std::string *evicted)
Assign a slot to this session, evicting the least-recently used one when the pool is full.
void set_resident(int slot, std::vector< Tok > tokens)
Record what a slot now holds after a decode.
Activate model on GPU (WARM → ACTIVE).
@ tokens
Gate on generated tokens since the last tool call.
constexpr int kNoSessionSlot
Sentinel for "no slot assigned".
@ optional
optional: every escape asks the host's path approver