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

Show on the map →

Called by SCENARIO, bind_session_slot

Calls forget, free_slot, lru_key, touch

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
lines 89–89◇ if (evicted != nullptr)lines 89–89evicted->clear();lines 90–90auto it = assigned_.find(key);lines 91–91◇ if (it != assigned_.end())lines 92–92touch(key);lines 93–93return it->second;lines 95–95int slot = free_slot();lines 96–96◇ if (slot == kNoSessionSlot)lines 97–97const std::string victim = lru_key();lines 98–98slot = assigned_[victim];lines 99–99forget(victim);lines 100–100◇ if (evicted != nullptr)lines 100–100*evicted = victim;lines 102–102assigned_[key] = slot;lines 103–103touch(key);lines 104–104return slot;

Requirements

REQ-INFER-019

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
lines 232–232assigned_.erase(key);lines 233–233use_order_.erase(key);

Requirements

REQ-INFER-019

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
lines 181–181auto it = assigned_.find(key);lines 182–182◇ if (it == assigned_.end())lines 182–182return kNoSessionSlot;lines 183–183const int slot = it->second;lines 184–184invalidate(slot);lines 185–185forget(key);lines 186–186return slot;

Requirements

REQ-INFER-019 REQ-LOOP-010

free_slot

int entropic::SessionResidency< Tok >::free_slot() const

First slot no session currently holds.

src/inference/session_residency.h:242

Show on the map →

Called by acquire

Calls none

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
lines 243–243std::vector<bool> taken(static_cast<std::size_t>(slots_), false);lines 244–244◇ for (const auto& [k, s] : assigned_)lines 251–251◇ if (!taken[static_cast<std::size_t>(i)])lines 251–251found = i;lines 253–253return found;

Requirements

REQ-INFER-019

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

Show on the map →

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
lines 140–140◇ if (valid(slot))lines 141–141resident_[static_cast<std::size_t>(slot)].clear();

Requirements

REQ-INFER-019

invalidate_all

void entropic::SessionResidency< Tok >::invalidate_all()

Forget every slot's residency.

src/inference/session_residency.h:153

Show on the map →

Called by SCENARIO, invalidate_all_resident_kv

Calls none

Source · 3 lines
    void invalidate_all() {
        for (auto& v : resident_) { v.clear(); }
    }
Activity · 1 steps
lines 154–154for (auto& v : resident_) { v.clear(); }

Requirements

REQ-INFER-019

lru_key

std::string entropic::SessionResidency< Tok >::lru_key() const

Session key with the oldest use tick.

src/inference/session_residency.h:262

Show on the map →

Called by acquire

Calls none

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
lines 263–263std::string oldest;lines 264–264uint64_t best = UINT64_MAX;lines 265–268for (const auto& [k, t] : use_order_) { if (t < best) { best = t; oldest = k; } } return oldest;

Requirements

REQ-INFER-019

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

Show on the map →

Called by SCENARIO, mtp_reuse_cut, try_warm_reuse

Calls none

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
lines 115–115static const std::vector<Tok> kEmpty;lines 116–117return valid(slot) ? resident_[static_cast<std::size_t>(slot)] : kEmpty;

Requirements

REQ-INFER-019

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

Show on the map →

Called by SCENARIO, generate_mtp, run_prefill_cached, try_warm_reuse

Calls none

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
lines 128–128◇ if (valid(slot))lines 129–129resident_[static_cast<std::size_t>(slot)] = std::lines 129–129move(tokens);

Requirements

REQ-INFER-019

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

Show on the map →

Called by SCENARIO, kv_pos_max_for

Calls none

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
lines 73–73auto it = assigned_.find(key);lines 74–74return it == assigned_.end() ? kNoSessionSlot : it->second;

Requirements

REQ-INFER-019

touch

void entropic::SessionResidency< Tok >::touch(const std::string &key)

Mark a session as most recently used.

src/inference/session_residency.h:223

Show on the map →

Called by acquire

Calls none

Source · 1 line
    void touch(const std::string& key) { use_order_[key] = ++tick_; }
Activity · 1 steps
lines 223–223use_order_[key] = ++tick_;

Requirements

REQ-INFER-019