batch_kv_util.h

src/inference/batch_kv_util.h

@file batch_kv_util.h @brief gh#158: which sequences a same-prefix batch may touch, purely.

Owners: @tvanfossen

plan_batch_kv

BatchKvPlan entropic::plan_batch_kv(std::size_t n, int session_slots, const std::vector< int > &temp_ids)

Plan the sequence ids a same-prefix batch may use.

src/inference/batch_kv_util.h:69

Show on the map →

Called by SCENARIO, prepare_batch_seqs

Calls none

Source · 17 lines
inline BatchKvPlan plan_batch_kv(std::size_t n, int session_slots,
                                 const std::vector<int>& temp_ids) {
    BatchKvPlan plan;
    plan.clears_whole_cache = false;
    plan.seq_ids.reserve(n);
    const int floor_id = session_slots < 1 ? 1 : session_slots;
    for (std::size_t i = 0; i < n && i < temp_ids.size(); ++i) {
        // Defence in depth: a temp pool whose base was never raised past the
        // session slots would otherwise hand back a session's sequence. The
        // configure-time exclusion in session_pool_util.h makes that
        // unreachable today; this makes it unreachable by construction.
        plan.seq_ids.push_back(
            temp_ids[i] < floor_id ? floor_id + static_cast<int>(i)
                                   : temp_ids[i]);
    }
    return plan;
}
Activity · 7 steps
lines 71–71BatchKvPlan plan;lines 72–72plan.clears_whole_cache = false;lines 73–73plan.seq_ids.reserve(n);lines 74–74const int floor_id = session_slots < 1 ? 1 : session_slots;lines 75–75◇ for (std::size_t i = 0; i < n && i < temp_ids.size(); ++i)lines 80–82plan.seq_ids.push_back( temp_ids[i] < floor_id ? floor_id + static_cast<int>(i) : temp_ids[i]);lines 84–84return plan;

Requirements

REQ-INFER-018 REQ-LOOP-009

plan_disturbs_sessions

bool entropic::plan_disturbs_sessions(const BatchKvPlan &plan, int session_slots)

Whether a plan can disturb any session's resident KV.

src/inference/batch_kv_util.h:100

Show on the map →

Called by SCENARIO

Calls none

Source · 9 lines
inline bool plan_disturbs_sessions(const BatchKvPlan& plan,
                                   int session_slots) {
    if (plan.clears_whole_cache) { return true; }
    bool disturbs = false;
    for (int id : plan.seq_ids) {
        if (id < session_slots) { disturbs = true; }
    }
    return disturbs;
}
Activity · 7 steps
lines 102–102◇ if (plan.clears_whole_cache)lines 102–102return true;lines 103–103bool disturbs = false;lines 104–104for (int id : plan.seq_ids)lines 105–105◇ if (id < session_slots)lines 105–105disturbs = true;lines 107–107return disturbs;

Requirements

REQ-LOOP-009