MCPcopy Create free account
hub / github.com/Niko1221/Strata / read

Method read

engine/src/split/src/session.cpp:155–292  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

153bool Session::is_eos(int t) const { return std::find(eos_.begin(), eos_.end(), t) != eos_.end(); }
154
155void Session::read(const std::vector<int>& prompt, GenStats& st, const std::function<void(int, int)>& progress, const ReadHints& hints) {
156 if (prompt.empty()) throw std::runtime_error("session: an empty prompt");
157 e_.use_lane(lane_);
158 if ((int) prompt.size() + 1 > e_.max_ctx()) throw Refused("session: the prompt does not fit the context");
159 const auto t0 = std::chrono::steady_clock::now();
160 const int n = (int) prompt.size();
161 const size_t body = (size_t) n - 1; // the tokens the engine is to hold before generate()
162 const bool caching = cache_->budget > 0 && hints.cache;
163 // how much of a conversation the prompt can start from: all of it when the prompt extends it, else its longest
164 // checkpoint inside the shared prefix (ck: that checkpoint, -1 for all)
165 auto usable = [&](const std::vector<int>& toks, const std::vector<State>& ckpts, int& ck) -> size_t {
166 size_t c = 0;
167 while (c < toks.size() && c < body && toks[c] == prompt[c]) ++c;
168 ck = -1;
169 if (c == toks.size()) return c;
170 size_t best = 0;
171 for (size_t i = 0; i < ckpts.size(); ++i)
172 if (ckpts[i]->tokens.size() <= c && ckpts[i]->tokens.size() >= best) {
173 best = ckpts[i]->tokens.size();
174 ck = (int) i;
175 }
176 return best;
177 };
178 const std::vector<int> hist = e_.history();
179 int ck_live = -1;
180 const size_t from_live = usable(hist, live_.ckpts, ck_live);
181 int best_parked = -1, ck_parked = -1;
182 size_t from_parked = 0;
183 for (size_t i = 0; i < cache_->parked.size(); ++i) {
184 int ck = -1;
185 const size_t u = usable(cache_->parked[i].kv->tokens, cache_->parked[i].ckpts, ck);
186 if (u > from_parked) {
187 from_parked = u;
188 best_parked = (int) i;
189 ck_parked = ck;
190 }
191 }
192 // the live sequence goes to host memory when the prompt leaves most of it (another conversation)
193 auto park_live = [&] {
194 if (caching) park_live_();
195 };
196 if (from_parked > from_live) {
197 park_live();
198 Conv c = std::move(cache_->parked[(size_t) best_parked]);
199 cache_->parked.erase(cache_->parked.begin() + best_parked);
200 const auto t_load = std::chrono::steady_clock::now();
201 try {
202 e_.load_state(*c.kv);
203 } catch (const Refused&) { // no pages for it: the lane holds what it held, and the conversation stays parked
204 cache_->parked.push_back(std::move(c));
205 throw;
206 }
207 if (ck_parked >= 0) e_.load_state(*c.ckpts[(size_t) ck_parked]);
208 c.kv.reset();
209 live_ = std::move(c);
210 std::fprintf(stderr, "conversation cache: restored %zu tokens in %.0f ms, parked=%zu bytes=%zu\n", from_parked, ms_since(t_load),
211 cache_->parked.size(), bytes_held());
212 } else if (from_live < hist.size()) {

Callers 15

run_onceMethod · 0.45
encodesMethod · 0.45
damageFunction · 0.45
mainFunction · 0.45
from_ggufMethod · 0.45
fixture_checkFunction · 0.45
mainFunction · 0.45
check_rowMethod · 0.45
mainFunction · 0.45

Calls 15

moveFunction · 0.85
sortFunction · 0.85
uniqueFunction · 0.85
max_ctxMethod · 0.80
historyMethod · 0.80
eraseMethod · 0.80
push_backMethod · 0.80
save_stateMethod · 0.80
RefusedClass · 0.70
ms_sinceFunction · 0.70
resetMethod · 0.65
nowFunction · 0.50

Tested by 12

run_onceMethod · 0.36
encodesMethod · 0.36
damageFunction · 0.36
rowMethod · 0.36
mainFunction · 0.36
runFunction · 0.36
runFunction · 0.36
read_allFunction · 0.36
load_realFunction · 0.36