Files
Robert KarlandClaude Fable 5.1 698b02e1a2 frontend: a picture encoded once is a picture encoded once (USTRAT-15)
A chat client resends the whole conversation every turn, pictures included, and the vision encoder is the slow part
of a request: 6 s a screenshot on 16 CPU threads, 18 s on 6, every turn, while the engine reuses the prompt's KV
prefix in a fraction of a second. Upstream's Python server kept the encoder's output per picture in an LRU of 64
files keyed by the SHA-256 of the picture's bytes (serve/server.py, class Vision); this is that cache for the C++
front end, in memory.

frontend/picture_cache.hpp (header-only, no llama.cpp): PictureCache keeps PictureRows (the projector's output,
n_tok rows of the embedding width) under mtmd's chunk id, which is the SHA-256 of the bitmap's bytes. Shared
pointers, so a request keeps the rows it is writing when the cache lets go of them; two limits, entries and bytes,
the newest entry always kept. engine_key() turns the id into the 64-bit key the engine's conversation cache takes
(img_keys= on the GENI line, #2228), so a picture encoded again with other bits still matches its KV prefix.

encode_pictures is now six short functions: open the file, fill it, find the chunk, its id, its rows (cache or
encoder), its grid, write the record. --picture-cache N (default 64, 1 GiB) sets the size; 0 turns it off.

frontend/tests/picture_cache_test.cpp: hit/miss and counters, LRU order and the entry limit, the byte limit and the
oversize newest entry, a held entry outliving its eviction and a key replaced, off and an empty key, the engine
keys, eight threads at once. In make test.

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
2026-10-20 10:33:30 -07:00

49 lines
2.2 KiB
C++

// UnbiasedStrata: the server context with a Strata engine behind it.
//
// This is the fork's replacement for llama.cpp's tools/server/server-context.cpp. Everything around it is llama.cpp's
// own, compiled unmodified from the submodule: the HTTP layer (server-http), the task queue and response reader
// (server-queue), the task and result types with their OpenAI/Anthropic/Responses JSON (server-task), request parsing,
// tokenization helpers and SSE formatting (server-common), the request schema (server-schema). What this file adds is
// the part that llama.cpp does with llama_context, llama_batch and common_sampler and that the engine does inside
// its own process: run one request at a time through `strata`/`strata-poly`, turn its `T <id>` lines into partial
// results, and its `DONE` line into the final one.
#pragma once
#include "server-http.h"
#include <memory>
#include <string>
#include <vector>
struct common_params;
struct strata_context_impl;
struct strata_context {
std::unique_ptr<strata_context_impl> impl;
strata_context();
~strata_context();
// Registers the HTTP routes on `http`. Handlers run only once http.is_ready is set, after load().
void init_routes(server_http_context & http);
// fork+exec the engine. Call before any thread exists (before http.start()).
bool start_engine(const common_params & params, const std::string & engine_exe, const std::vector<std::string> & engine_args, std::string & err);
// Loads the vocabulary and chat template from the GGUF (vocab only, no tensors) and waits for the engine's READY.
bool load(common_params & params, std::string & err);
// --picture-cache: how many pictures' encoded rows are kept for the turns that resend them (0: none); before load()
void set_picture_cache(size_t entries, size_t bytes);
// Blocks the calling thread running the task loop until terminate().
void start_loop();
void terminate();
// Non-blocking: true once the engine process has exited; `why` names the signal or exit code.
bool engine_exited(std::string & why);
bool engine_alive() const;
// the context the engine allocated (READY line)
long long n_ctx() const;
};