session_residency.h¶
src/inference/session_residency.h
@file session_residency.h @brief gh#144: which llama sequence holds which session's KV, purely.
Owners: @tvanfossen
acquire
int entropic::SessionResidency< Tok >::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.
src/inference/session_residency.h:88
Source · 18 lines
int acquire(const std::string& key, std::string* evicted) {
if (evicted != nullptr) { evicted->clear(); }
auto it = assigned_.find(key);
if (it != assigned_.end()) {
touch(key);
return it->second;
}
int slot = free_slot();
if (slot == kNoSessionSlot) {
const std::string victim = lru_key();
slot = assigned_[victim];
forget(victim);
if (evicted != nullptr) { *evicted = victim; }
}
assigned_[key] = slot;
touch(key);
return slot;
}Activity · 16 steps
Requirements
forget
void entropic::SessionResidency< Tok >::forget(const std::string &key)
Drop a session's slot assignment and LRU entry.
src/inference/session_residency.h:231
Source · 4 lines
void forget(const std::string& key) {
assigned_.erase(key);
use_order_.erase(key);
}Activity · 2 steps
Requirements
forget_session
int entropic::SessionResidency< Tok >::forget_session(const std::string &key)
Drop one SESSION's residency and release its slot (gh#165).
src/inference/session_residency.h:180
Source · 8 lines
int forget_session(const std::string& key) {
auto it = assigned_.find(key);
if (it == assigned_.end()) { return kNoSessionSlot; }
const int slot = it->second;
invalidate(slot);
forget(key);
return slot;
}Activity · 7 steps
Requirements
free_slot
int entropic::SessionResidency< Tok >::free_slot() const
First slot no session currently holds.
src/inference/session_residency.h:242
Source · 13 lines
int free_slot() const {
std::vector<bool> taken(static_cast<std::size_t>(slots_), false);
for (const auto& [k, s] : assigned_) {
if (s >= 0 && s < slots_) {
taken[static_cast<std::size_t>(s)] = true;
}
}
int found = kNoSessionSlot;
for (int i = 0; i < slots_ && found == kNoSessionSlot; ++i) {
if (!taken[static_cast<std::size_t>(i)]) { found = i; }
}
return found;
}Activity · 5 steps
Requirements
invalidate
void entropic::SessionResidency< Tok >::invalidate(int slot)
Forget a slot's residency (its KV was cleared or invalidated).
src/inference/session_residency.h:139
Called by SCENARIO, bind_session_slot, forget_session, invalidate_resident_kv
Calls none
Source · 5 lines
void invalidate(int slot) {
if (valid(slot)) {
resident_[static_cast<std::size_t>(slot)].clear();
}
}Activity · 2 steps
Requirements
invalidate_all
void entropic::SessionResidency< Tok >::invalidate_all()
Forget every slot's residency.
src/inference/session_residency.h:153
Source · 3 lines
void invalidate_all() {
for (auto& v : resident_) { v.clear(); }
}Activity · 1 steps
Requirements
lru_key
std::string entropic::SessionResidency< Tok >::lru_key() const
Session key with the oldest use tick.
src/inference/session_residency.h:262
Source · 8 lines
std::string lru_key() const {
std::string oldest;
uint64_t best = UINT64_MAX;
for (const auto& [k, t] : use_order_) {
if (t < best) { best = t; oldest = k; }
}
return oldest;
}Activity · 3 steps
Requirements
resident
const std::vector< Tok > & entropic::SessionResidency< Tok >::resident(int slot) const
Tokens recorded as resident in a slot.
src/inference/session_residency.h:114
Source · 5 lines
const std::vector<Tok>& resident(int slot) const {
static const std::vector<Tok> kEmpty;
return valid(slot) ? resident_[static_cast<std::size_t>(slot)]
: kEmpty;
}Activity · 2 steps
Requirements
set_resident
void entropic::SessionResidency< Tok >::set_resident(int slot, std::vector< Tok > tokens)
Record what a slot now holds after a decode.
src/inference/session_residency.h:127
Source · 5 lines
void set_resident(int slot, std::vector<Tok> tokens) {
if (valid(slot)) {
resident_[static_cast<std::size_t>(slot)] = std::move(tokens);
}
}Activity · 3 steps
Requirements
slot_for
int entropic::SessionResidency< Tok >::slot_for(const std::string &key) const
Slot currently holding this session, if any.
src/inference/session_residency.h:72
Source · 4 lines
int slot_for(const std::string& key) const {
auto it = assigned_.find(key);
return it == assigned_.end() ? kNoSessionSlot : it->second;
}Activity · 2 steps
Requirements
touch
void entropic::SessionResidency< Tok >::touch(const std::string &key)
Mark a session as most recently used.
src/inference/session_residency.h:223
Source · 1 line
void touch(const std::string& key) { use_order_[key] = ++tick_; }