Skip to main content

Crate weirdnet_semcache

Crate weirdnet_semcache 

Source
Expand description

Answer cache for Weirdnet inference nodes.

An inference node asks an AnswerCache before it routes a prompt and offers it the finished answer afterwards. The cache decides what it keeps and when a stored answer may stand in.

  • AnswerCache: the seam (get, put) over weirdnet-agent infer/2 Requests.

  • AnswerStore: where cached answers live, as opaque bytes under 32-byte keys, so a store can be in memory, on disk or shared. MemoryStore is bounded and least-recently-used.

  • ExactCache: answers only requests identical to a stored one that are deterministic (temperature 0, no tools, inline messages) for the same model, and keeps only answers that finished (stop or length).

  • NoCache: caches nothing.

  • may_read / may_store: the request’s own CacheMode (weirdnet-agent 0.5.1): bypass neither reads nor stores, refresh skips the read and stores the fresh answer. ExactCache honours both.

  • Revisions: AnswerCache::get_rev / AnswerCache::put_rev and exact_key_rev key an answer on the model’s weights as well as its id, so a fine-tuned model under the same id doesn’t replay answers from the old weights.

  • SemanticCache: similarity lookup by embedding. The caller embeds the prompt; the nearest stored embedding in the same scope (see scope_key) stands in when its cosine similarity reaches the threshold. Bounded and least-recently-used, with an optional TTL and a CBOR snapshot / restore.

Pool mode, several inference nodes sharing one cache (since 0.2.3):

  • ShardRing / ShardPlan: rendezvous hashing of a scope (or an exact Key) to a home node and one replica; joins and leaves move only the shards they must.
  • SharedCacheReq / SharedCacheResp: the CBOR lookup / insert / remove protocol (PROTOCOL), and SharedCacheHost, which answers it from a node’s own cache.
  • With the pool feature, pool::PooledSemanticCache (L1, the home, the replica on failure, bounded timeouts, background replica writes, a miss whenever peers fail) over a pool::CacheTransport, and pool::serve / pool::DialerTransport over weirdnet-agent channels.

Modules§

poolpool
Pool mode: inference nodes sharing one cache (PooledSemanticCache), over any CacheTransport (DialerTransport for weirdnet-agent channels, served by serve).

Structs§

CachedAnswer
A stored answer, replayed instead of running the model.
ExactCache
Exact-match cache for deterministic requests over an AnswerStore.
HostLimits
What a SharedCacheHost accepts from other nodes.
MemoryStore
In-memory store holding at most max_entries, evicting the least recently used.
NoCache
Caches nothing.
SemanticCache
Embedding-similarity cache: the nearest stored embedding in the same scope, by cosine similarity, stands in when it reaches the threshold.
SemanticConfig
How a SemanticCache matches and evicts.
SemanticStats
Counters since the cache was made or last cleared.
ShardPlan
The home node and replica for one shard, from ShardRing::plan.
ShardRing
The inference nodes that share a cache, and which of them holds each shard.
SharedCacheHost
Answers shared-cache requests from this node’s own cache: the shards it holds.

Enums§

Lookup
The result of SemanticCache::lookup.
SemanticError
Why a SemanticCache call was refused.
SharedCacheReq
A shared-cache request, sent to the node that holds the shard.
SharedCacheResp
The answer to a SharedCacheReq.

Constants§

DEFAULT_THRESHOLD
Similarity a lookup needs to hit when none is configured.
PROTOCOL
The protocol name nodes serve the shared cache on.
WIRE_VERSION
The message version this crate writes and reads.

Traits§

AnswerCache
What an inference node asks before and after routing a prompt.
AnswerStore
Where cached answers live. Values are opaque bytes; a store may drop entries at any time.

Functions§

cosine_similarity
Cosine similarity of two embeddings, or None when either is empty, not finite or zero, or their dimensions differ.
deterministic
Requests an exact cache may answer: temperature 0, no tools, inline messages.
exact_key
The key for request on model, or None when it is not deterministic. The same as exact_key_rev with no revision.
exact_key_rev
The key for request on model at weights revision, or None when it is not deterministic. With no revision the key is the 0.2.0 one (v: 1), so stored answers still hit; with one it is a v: 2 key that includes it. The request’s CacheMode is not part of the key, so refresh overwrites the entry use reads.
may_read
The request lets a cache answer it (CacheMode::Use).
may_store
The request lets a cache keep its answer (CacheMode::Use or CacheMode::Refresh).
normalize
embedding scaled to unit length, after checking it is non-empty, finite and not zero.
scope_key
A stable scope for SemanticCache: hex SHA-256 over the length-prefixed parts, such as the model id, its revision and a hash of the system prompt. Different parts (or the same text split differently) give different scopes.

Type Aliases§

Key
A cache key: SHA-256 over the model and the parts of the request that shape the answer.