weirdnet-semcache
The answer cache for Weirdnet inference nodes. Before an inference node routes a prompt it asks
the cache; afterwards it offers the finished answer. Requests and answers are
weirdnet-agent 0.5.1 infer/2 types.
AnswerCache: the seam,get(model, request)andput(model, request, answer).CachedAnswer: text, reasoning, tool calls, finish reason, usage and model;From<&Answer>for aweirdnet_agent::remote::pool::Answer.AnswerStore: where answers live, opaque bytes under 32-byte keys, so the store can be in memory, on disk or shared between nodes.MemoryStoreis bounded and least-recently-used.ExactCache: answers only byte-identical deterministic requests (temperature 0, no tools, inline messages, same model, seed, response format and raw flag), and keeps only answers that finished withstoporlength. The key is SHA-256 over the CBOR of those parts.NoCache: caches nothing.may_read(request)/may_store(request): the request's ownCacheMode.bypassneither reads nor stores;refreshskips the read and stores the fresh answer over the old one.ExactCachehonours both.get_rev/put_revandexact_key_rev: key on the model's revision (its weights) as well as its id, so a fine-tune served under the same id doesn't replay the old weights' answers. With no revision the key is unchanged from 0.2.0.SemanticCache: similarity lookup by embedding. You embed the prompt; the nearest stored embedding in the same scope stands in when its cosine similarity reaches the threshold (default 0.95). Embeddings (&[f32], any length) are checked (non-empty, finite, not zero, the scope's dimension) and normalized on the way in.lookupreturns aLookup::Hitwith the value, score and entry id, or aLookup::Misswith the best score below the threshold. Bounded and least-recently-used across scopes, with an optional TTL,remove,remove_scope,clear,stats, and a CBORsnapshot/restore. Values default toCachedAnswerbut can be anyClonetype.scope_key(&[model, revision, system_prompt_hash]): a scope so answers never cross models, weights or system prompts.cosine_similarityandnormalizeare public too.
use ;
let cache = new;
if let Some = cache.get_rev
// after a finished answer:
cache.put_rev;
Semantic lookup (check may_read / may_store on infer/2 requests first, as for ExactCache):
use Duration;
use ;
let cache: SemanticCache = with_config;
let scope = scope_key;
match cache.lookup?
Pool mode (shared cache)
Several inference nodes in one pool can share one cache (since 0.2.3). Each entry lives on a home node and one replica, picked by rendezvous hashing of its scope (or exact key), so a lookup is one call and nothing is coordinated. All nodes must use the same embedding model and revision.
ShardRing/ShardPlan: the members and, per scope,plan(scope)(home and replica) andranked(scope). When a node leaves, only its shards move, to their replica; when one joins, it takes over only the shards it now ranks first for, and their old home becomes the replica.SharedCacheReq/SharedCacheResp: the wire protocol onPROTOCOL(/weirdnet-semcache/shared/1), CBOR with a version:lookup,insert(with the entry's age, so a copy keeps its TTL),remove_scope,exact_get/exact_put/exact_remove,ping.SharedCacheHost: answers those requests from a node's ownSemanticCache(and, optionally, anAnswerStorefor exact entries), withinHostLimits(scope length, dimensions, exact value size). It doesn't authorize; the serving side does.- With the
poolfeature (no new dependencies beyond whatweirdnet-agentalready pulls in):pool::PooledSemanticCache: one node's view.lookuptries a local L1 copy, then the home (its own shard when it is the home), then the replica when the home fails.insertwrites its own shard when it holds the shard, the home (waited for, bounded) and the replica (in the background;settle()waits).remove_scopereaches every member. Every remote call has a timeout, a failing node is skipped fordown_for, and when no owner answers a lookup is aPoolLookup::Miss { degraded: true }: a cache outage never blocks inference. Optional read repair (ask_replica_on_miss) copies a replica's hit back to a new home.exact_get/exact_putshare exact entries the same way.stats()counts hits by source, misses, timeouts, errors and skips.pool::CacheTransport: how a request reaches a node.pool::DialerTransportdoes it over aweirdnet-agentDialer, andpool::serve(channel, caller, auth, host)answers on the other end afterauth(for Weirdnet, the pool'sRosterAuth) lets the caller in.
use Arc;
use ;
use ;
let shard = new;
let cache: PooledSemanticCache = new;
cache.set_nodes; // this node included
// serve cache.host() on weirdnet_semcache::PROTOCOL with weirdnet_semcache::pool::serve
let scope = scope_key;
match cache.lookup.await?
License
BSD-3-Clause.