|
Entropic 2.14.3
Local-first agentic inference engine
|
Concrete base class for inference backends (80% logic). More...
#include <entropic/inference/backend.h>

Public Member Functions | |
| bool | load (const ModelConfig &config) |
| Load model into CPU RAM (COLD → WARM). | |
| bool | activate () |
| Promote to GPU (WARM → ACTIVE). | |
| void | deactivate () |
| Release GPU layers (ACTIVE → WARM). | |
| void | unload () |
| Full unload (→ COLD). | |
| bool | load_and_activate (const ModelConfig &config) |
| Bring a model to ACTIVE in as few file reads as possible. | |
| GenerationResult | generate (const std::vector< Message > &messages, const GenerationParams ¶ms) |
| Generate a complete response. | |
| GenerationResult | generate (const std::vector< Message > &messages, const GenerationParams ¶ms, std::atomic< bool > &cancel) |
| Generate a complete response with cancellation support. | |
| GenerationResult | generate_streaming (const std::vector< Message > &messages, const GenerationParams ¶ms, std::function< void(std::string_view token)> on_token, std::atomic< bool > &cancel) |
| Generate with per-token streaming callback. | |
| GenerationResult | generate_speculative (const std::vector< Message > &messages, const GenerationParams ¶ms, std::function< void(std::string_view token)> on_token, std::atomic< bool > &cancel) |
| Generate via the speculative-decoding kernel (v2.1.11). | |
| GenerationResult | complete (const std::string &prompt, const GenerationParams ¶ms) |
| Raw text completion without chat template. | |
| LogprobResult | evaluate_logprobs (const int32_t *tokens, int n_tokens) |
| Evaluate per-token log-probabilities for a token sequence. | |
| float | compute_perplexity (const int32_t *tokens, int n_tokens) |
| Compute perplexity for a token sequence. | |
| ModelState | state () const |
| Current lifecycle state (lock-free read). | |
| bool | is_active () const |
| True when state is ACTIVE. | |
| bool | is_loaded () const |
| True when state is WARM or ACTIVE. | |
| int | count_tokens (const std::string &text) const |
| Count tokens using model's tokenizer. | |
| virtual std::vector< int32_t > | tokenize_text (const std::string &text) const |
| Tokenize text to token IDs. | |
| int | context_length () const |
| Model's context window size. | |
| virtual void | clear_prompt_cache () |
| Invalidate any backend-owned prompt/KV caches. | |
| virtual std::string | tool_call_close_marker () const |
| The tool-call CLOSE marker for the active chat format (gh#103). | |
| const ModelConfig & | config () const |
| Stored model config. | |
| bool | supports (BackendCapability cap) const |
| Query whether this backend supports a capability. | |
| std::vector< BackendCapability > | capabilities () const |
| Get all supported capabilities as a vector. | |
| BackendInfo | info () const |
| Get backend metadata. | |
| virtual std::string | active_tool_grammar () const |
| The tool-call GBNF the last render derived, if any (gh#154). | |
| bool | save_state (int seq_id, std::vector< uint8_t > &buffer) const |
| Save model state to buffer. | |
| bool | restore_state (int seq_id, const std::vector< uint8_t > &buffer) |
| Restore model state from buffer. | |
| bool | clear_state (int seq_id=-1) |
| Clear/reset model state for a sequence. | |
| GenerationResult | generate_seq (int seq_id, const std::vector< Message > &messages, const GenerationParams ¶ms) |
| Generate with explicit sequence ID. | |
| GenerationResult | generate_streaming_seq (int seq_id, const std::vector< Message > &messages, const GenerationParams ¶ms, std::function< void(std::string_view token)> on_token, std::atomic< bool > &cancel) |
| Streaming generation with explicit sequence ID. | |
| std::vector< GenerationResult > | generate_batch (const std::vector< std::vector< Message > > &requests, const std::vector< GenerationParams > ¶ms, std::atomic< bool > &cancel) |
| Generate N independent same-prefix requests together. | |
| virtual void | forget_session_kv (const std::string &session_key) |
| Drop one session's resident KV and release its slot (gh#165). | |
Protected Member Functions | |
| virtual bool | do_load (const ModelConfig &config)=0 |
| Load model into CPU RAM. | |
| virtual bool | do_activate ()=0 |
| Promote loaded model to GPU. | |
| virtual bool | do_load_active (const ModelConfig &config) |
| Load COLD → ACTIVE in one step. | |
| virtual void | do_deactivate ()=0 |
| Release GPU, keep CPU. | |
| virtual void | do_unload ()=0 |
| Full unload. | |
| virtual GenerationResult | do_generate (const std::vector< Message > &messages, const GenerationParams ¶ms)=0 |
| Subclass generation. | |
| virtual GenerationResult | do_generate (const std::vector< Message > &messages, const GenerationParams ¶ms, std::atomic< bool > &cancel) |
| Subclass batch generation with cancel-flag support. | |
| virtual std::vector< GenerationResult > | do_generate_batch (const std::vector< std::vector< Message > > &requests, const std::vector< GenerationParams > ¶ms, std::atomic< bool > &cancel) |
| Subclass same-prefix batch generation (gh#98, v2.8.0). | |
| virtual GenerationResult | do_generate_streaming (const std::vector< Message > &messages, const GenerationParams ¶ms, std::function< void(std::string_view token)> on_token, std::atomic< bool > &cancel)=0 |
| Subclass streaming generation. | |
| virtual GenerationResult | do_generate_speculative (const std::vector< Message > &messages, const GenerationParams ¶ms, std::function< void(std::string_view token)> on_token, std::atomic< bool > &cancel) |
| Subclass speculative-decoding streaming generation. | |
| virtual GenerationResult | do_complete (const std::string &prompt, const GenerationParams ¶ms)=0 |
| Subclass raw completion. | |
| virtual int | do_count_tokens (const std::string &text) const =0 |
| Subclass token counting. | |
| virtual LogprobResult | do_evaluate_logprobs (const int32_t *tokens, int n_tokens)=0 |
| Backend-specific logprob evaluation. | |
| virtual bool | do_supports (BackendCapability cap) const |
| Declare supported capabilities. | |
| virtual std::string | do_backend_name () const =0 |
| Return backend name identifier. | |
| virtual BackendInfo | do_info () const |
| Populate backend metadata. | |
| virtual bool | do_save_state (int seq_id, std::vector< uint8_t > &buffer) const |
| Save model state (KV cache or hidden state). | |
| virtual bool | do_restore_state (int seq_id, const std::vector< uint8_t > &buffer) |
| Restore model state. | |
| virtual bool | do_clear_state (int seq_id) |
| Clear/reset model state. | |
| virtual GenerationResult | do_generate_seq (int seq_id, const std::vector< Message > &messages, const GenerationParams ¶ms) |
| Generate with sequence ID. | |
| virtual GenerationResult | do_generate_streaming_seq (int seq_id, const std::vector< Message > &messages, const GenerationParams ¶ms, std::function< void(std::string_view token)> on_token, std::atomic< bool > &cancel) |
| Streaming generation with sequence ID. | |
| bool | fire_model_load_hook (const ModelConfig &config) |
| Fire ON_MODEL_LOAD pre-hook. | |
| bool | promote_to_active () |
| WARM → ACTIVE promotion. | |
| void | set_hooks (const HookInterface &hooks) |
| Set the hook dispatch interface. | |
Protected Attributes | |
| std::string | last_error_ |
| Last error message for diagnostics. | |
| std::atomic< ModelState > | state_ {ModelState::COLD} |
| State transition slot accessible to subclasses for test-only injection. | |
Concrete base class for inference backends (80% logic).
Public methods implement the lifecycle state machine, transition locking, timing, and logging. Protected virtual methods are the 20% that subclasses override with backend-specific logic.
Invalid transitions are no-ops with INFO log (not errors).
| bool entropic::InferenceBackend::activate | ( | ) |
Promote to GPU (WARM → ACTIVE).
Loads first if COLD.
Definition at line 88 of file backend.cpp.
|
inlinevirtual |
The tool-call GBNF the last render derived, if any (gh#154).
The second grammar source (the first being GenerationParams::grammar). It never appears on the params, so a caller holding only the params cannot tell whether the decode was constrained — the orchestrator asks the backend instead, and builds GenerationResult::grammar from both.
Base returns "" — a backend that stages no tools has no such grammar, which is the honest answer rather than a stub.
Reimplemented in entropic::LlamaCppBackend.
| std::vector< BackendCapability > entropic::InferenceBackend::capabilities | ( | ) | const |
Get all supported capabilities as a vector.
Get all supported capabilities.
Convenience method. Iterates BackendCapability enum, calls supports() on each.
Iteration is bounded by the _COUNT sentinel rather than a hardcoded length, so a capability appended to the enum is enumerated here without a second edit — the sentinel is what makes the sweep exhaustive.
_COUNT for which supports() is true; the sentinel is never included. @req REQ-TYPE-004 Definition at line 591 of file backend.cpp.
Invalidate any backend-owned prompt/KV caches.
Called when identity or prompt-prefix inputs change so stale cached prefixes are never served against the new system prompt. Default is a no-op for backends with no cache. (P1-7, 2.0.6-rc16)
@utility
Reimplemented in entropic::LlamaCppBackend.
Clear/reset model state for a sequence.
Clear model state.
| seq_id | Sequence identifier (-1 for all sequences). |
For transformers: clears KV cache. For recurrent: resets hidden state to initial values.
Requires WARM or ACTIVE.
| seq_id | Sequence ID, or -1 to clear every sequence. |
Definition at line 675 of file backend.cpp.
| GenerationResult entropic::InferenceBackend::complete | ( | const std::string & | prompt, |
| const GenerationParams & | params | ||
| ) |
Raw text completion without chat template.
Raw text completion.
| prompt | Raw prompt string (no chat formatting). |
| params | Generation parameters. |
Requires ACTIVE state.
| prompt | Raw prompt string. |
| params | Generation parameters. |
Definition at line 422 of file backend.cpp.
Compute perplexity for a token sequence.
| tokens | Array of token IDs. |
| n_tokens | Number of tokens (minimum 2). |
Convenience method — calls evaluate_logprobs() and returns only the perplexity value.
Convenience wrapper — calls evaluate_logprobs() and returns only the perplexity value.
| tokens | Array of token IDs. |
| n_tokens | Number of tokens (minimum 2). |
Definition at line 517 of file backend.cpp.
|
inline |
Stored model config.
|
inline |
Count tokens using model's tokenizer.
Count tokens.
Exact if loaded, estimate if COLD.
| text | Text to tokenize. |
Definition at line 558 of file backend.cpp.
| void entropic::InferenceBackend::deactivate | ( | ) |
Release GPU layers (ACTIVE → WARM).
No-op if not ACTIVE.
No-op if not ACTIVE. @dg_internal
Definition at line 132 of file backend.cpp.
Promote loaded model to GPU.
Called under transition_mutex_.
Implemented in entropic::LlamaCppBackend.
|
protectedpure virtual |
Return backend name identifier.
Pure virtual — every backend must identify itself.
Implemented in entropic::LlamaCppBackend.
Clear/reset model state.
Default: state clear not supported.
| seq_id | Sequence ID, or -1 for all. |
| seq_id | Sequence identifier. |
Reimplemented in entropic::LlamaCppBackend.
Definition at line 815 of file backend.cpp.
|
protectedpure virtual |
Subclass raw completion.
Called only when ACTIVE.
| prompt | Raw prompt string (no chat template applied). |
| params | Generation parameters. |
Implemented in entropic::LlamaCppBackend.
|
protectedpure virtual |
Subclass token counting.
Called only when model loaded.
| text | Text whose tokens should be counted. |
Implemented in entropic::LlamaCppBackend.
Release GPU, keep CPU.
Called under transition_mutex_.
Implemented in entropic::LlamaCppBackend.
|
protectedpure virtual |
Backend-specific logprob evaluation.
| tokens | Token IDs to evaluate. |
| n_tokens | Number of tokens. |
Called by evaluate_logprobs() after state validation and eval_mutex_ acquisition. The base class handles state assertion, minimum token count validation, mutex, perplexity computation from logprobs, and logging. The implementation handles batch allocation, decode calls, logit extraction, and temporary seq_id lifecycle.
Implemented in entropic::LlamaCppBackend.
|
protectedpure virtual |
Subclass generation.
Called only when ACTIVE.
| messages | Conversation history. |
| params | Generation parameters. |
Implemented in entropic::LlamaCppBackend.
|
inlineprotectedvirtual |
Subclass batch generation with cancel-flag support.
gh#81 (v2.4.2): default implementation ignores cancel and delegates to the no-cancel do_generate override, preserving pre-v2.4.2 behavior for backends that haven't opted in. Backends that want responsive interrupts override this method and poll cancel per decode iteration (mirroring do_generate_streaming).
| messages | Conversation history. |
| params | Generation parameters. |
| cancel | Atomic cancel flag; ignored by this default. |
do_generate override — behaviourally identical to not passing a flag at all. @req REQ-INFER-005 Reimplemented in entropic::LlamaCppBackend.
|
inlineprotectedvirtual |
Subclass same-prefix batch generation (gh#98, v2.8.0).
Default implementation is a transparent serial fallback: each request runs through the single-request do_generate in turn. Backends that support multi-sequence batched decode (LlamaCppBackend on a plain-KV arch) override this to prefill the shared prefix once and fan out.
| requests | Per-request message lists. |
| params | Per-request generation params. |
| cancel | Atomic cancel flag. |
Reimplemented in entropic::LlamaCppBackend.
|
protectedvirtual |
Generate with sequence ID.
Default: ignores seq_id, delegates to do_generate().
| seq_id | Sequence identifier. |
| messages | Conversation history. |
| params | Generation parameters. |
Default: ignores seq_id, delegates to do_generate().
| seq_id | Sequence identifier (ignored). |
| messages | Conversation history. |
| params | Generation parameters. |
Definition at line 828 of file backend.cpp.
|
protectedvirtual |
Subclass speculative-decoding streaming generation.
Default implementation of speculative streaming generation.
Same contract as do_generate_streaming (callback fires once per accepted token, cancel flag honored between accept rounds) but the backend is expected to drive a draft model through common_speculative_* (or equivalent) to propose tokens, then verify them in batch against the target. Output distribution MUST be bit-identical to plain decode on rejection cases (correctness contract from the v2.1.11 proposal).
Default returns a result with ENTROPIC_ERROR_NOT_SUPPORTED — backends that don't implement speculative fall through to the standard streaming path via the orchestrator.
| messages | Conversation history. |
| params | Generation parameters. |
| on_token | Callback for each accepted token. |
| cancel | Atomic cancel flag. |
Returns ENTROPIC_ERROR_NOT_SUPPORTED so the orchestrator falls back to plain do_generate_streaming. Backends that implement the speculative kernel override this method. (v2.1.11, gh#36)
| messages | Unused in default impl. |
| params | Unused in default impl. |
| on_token | Unused in default impl. |
| cancel | Unused in default impl. |
Reimplemented in entropic::LlamaCppBackend.
Definition at line 400 of file backend.cpp.
|
protectedpure virtual |
Subclass streaming generation.
Called only when ACTIVE.
| messages | Conversation history. |
| params | Generation parameters. |
| on_token | Callback invoked per emitted token. |
| cancel | Atomic flag — when true, subclass must stop streaming. |
Implemented in entropic::LlamaCppBackend.
|
protectedvirtual |
Streaming generation with sequence ID.
Default: ignores seq_id, delegates to do_generate_streaming().
| seq_id | Sequence identifier. |
| messages | Conversation history. |
| params | Generation parameters. |
| on_token | Per-token callback. |
| cancel | Cancellation flag. |
Default: ignores seq_id, delegates to do_generate_streaming().
| seq_id | Sequence identifier (ignored). |
| messages | Conversation history. |
| params | Generation parameters. |
| on_token | Per-token callback. |
| cancel | Cancellation flag. |
Definition at line 847 of file backend.cpp.
|
protectedvirtual |
Populate backend metadata.
Default: BackendInfo with name only.
Default: returns BackendInfo with name from do_backend_name().
Reimplemented in entropic::LlamaCppBackend.
Definition at line 774 of file backend.cpp.
|
protectedpure virtual |
Load model into CPU RAM.
Called under transition_mutex_.
| config | Validated model config. |
Implemented in entropic::LlamaCppBackend.
|
inlineprotectedvirtual |
Load COLD → ACTIVE in one step.
Called under transition_mutex_.
The default is the historical behaviour — load, then activate — so a subclass that has no cheaper route (and every mock) keeps working unchanged. LlamaCppBackend overrides it to read the file ONCE with the configured gpu_layers instead of reading it once for the CPU placement and again for the real one (gh#148).
| config | Validated model config. |
Reimplemented in entropic::LlamaCppBackend.
|
protectedvirtual |
Restore model state.
Default: state restore not supported.
| seq_id | Sequence identifier. |
| buffer | State data to restore. |
| seq_id | Sequence identifier. |
| buffer | State data. |
Reimplemented in entropic::LlamaCppBackend.
Definition at line 802 of file backend.cpp.
|
protectedvirtual |
Save model state (KV cache or hidden state).
Default: state save not supported.
| seq_id | Sequence identifier. |
| buffer | Output buffer. |
| seq_id | Sequence identifier. |
| buffer | Output buffer. |
Reimplemented in entropic::LlamaCppBackend.
Definition at line 788 of file backend.cpp.
|
protectedvirtual |
Declare supported capabilities.
Default: no capabilities supported.
| cap | Capability to check. |
Default: returns false for everything.
| cap | Capability to check. |
Reimplemented in entropic::LlamaCppBackend.
Definition at line 764 of file backend.cpp.
| LogprobResult entropic::InferenceBackend::evaluate_logprobs | ( | const int32_t * | tokens, |
| int | n_tokens | ||
| ) |
Evaluate per-token log-probabilities for a token sequence.
Evaluate per-token log-probabilities.
| tokens | Array of token IDs to evaluate. |
| n_tokens | Number of tokens in the array (minimum 2). |
| std::runtime_error | if model is not ACTIVE. |
| std::runtime_error | if n_tokens < 2. |
Requires ACTIVE state.
The 80% logic: state check, input validation, eval_mutex_, perplexity computation from raw logprobs, and logging. Delegates to do_evaluate_logprobs() for backend-specific batch/decode work.
| tokens | Array of token IDs. |
| n_tokens | Number of tokens (minimum 2). |
| std::runtime_error | on state/input errors. |
Definition at line 459 of file backend.cpp.
|
protected |
Fire ON_MODEL_LOAD pre-hook.
| config | Model config being loaded. |
| config | Model config being loaded. |
Definition at line 533 of file backend.cpp.
|
inlinevirtual |
Drop one session's resident KV and release its slot (gh#165).
Called when a session's conversation is REPLACED wholesale (entropic_session_context_set), so the restored history cannot decode against a prefix the conversation it replaced left in the cache. The session's next turn costs a cold prefill, which is exactly what an LRU-evicted session already costs.
Default is a no-op: a backend with no per-session KV has nothing to forget, and every mock therefore needs no override (decision #32 — new methods carry a default implementation).
| session_key | Session whose KV to drop; "" is the default session. @req REQ-LOOP-010 |
Reimplemented in entropic::LlamaCppBackend.
| GenerationResult entropic::InferenceBackend::generate | ( | const std::vector< Message > & | messages, |
| const GenerationParams & | params | ||
| ) |
Generate a complete response.
| messages | Conversation history. |
| params | Generation parameters. |
Requires ACTIVE state.
| messages | Conversation history. |
| params | Generation parameters. |
Definition at line 234 of file backend.cpp.
| GenerationResult entropic::InferenceBackend::generate | ( | const std::vector< Message > & | messages, |
| const GenerationParams & | params, | ||
| std::atomic< bool > & | cancel | ||
| ) |
Generate a complete response with cancellation support.
Generate with cancel support.
gh#81 (v2.4.2): the no-cancel generate overload above runs to max_tokens or natural stop with no path to honor a mid-decode interrupt. The engine's loop-top interrupt check can then take a full generation cycle to fire — observed ~60s on qwen3_6_a3b at max_tokens=400.
This overload mirrors generate_streaming's contract: the backend polls cancel per token (latency: one token) and returns a result with finish_reason="cancelled" and error_code=ENTROPIC_ERROR_CANCELLED when cancellation lands.
| messages | Conversation history. |
| params | Generation parameters. |
| cancel | Atomic flag — when true, subclass must stop decoding. |
Requires ACTIVE state. (gh#81, v2.4.2)
| messages | Conversation history. |
| params | Generation parameters. |
| cancel | Atomic cancel flag forwarded to the subclass decode loop. |
Definition at line 264 of file backend.cpp.
| std::vector< GenerationResult > entropic::InferenceBackend::generate_batch | ( | const std::vector< std::vector< Message > > & | requests, |
| const std::vector< GenerationParams > & | params, | ||
| std::atomic< bool > & | cancel | ||
| ) |
Generate N independent same-prefix requests together.
Same-prefix batch generation.
For traffic where N requests share a large verbatim prompt prefix and differ only in a short suffix (e.g. NPC agents per game tick): the shared prefix is prefilled once and fanned out to N sequences, then the suffixes + outputs are decoded in batched forward passes, each sampled under its own request's grammar. Requires ACTIVE state.
| requests | Per-request message lists (all sharing a prefix). |
| params | Per-request generation params (each may carry its grammar). |
| cancel | Cooperative cancel flag. |
Requires ACTIVE state. (gh#98, v2.8.0)
| requests | Per-request message lists. |
| params | Per-request generation params. |
| cancel | Atomic cancel flag forwarded to the batch decode loop. |
Definition at line 293 of file backend.cpp.
| GenerationResult entropic::InferenceBackend::generate_seq | ( | int | seq_id, |
| const std::vector< Message > & | messages, | ||
| const GenerationParams & | params | ||
| ) |
Generate with explicit sequence ID.
| seq_id | Sequence identifier for multi-sequence backends. |
| messages | Conversation history. |
| params | Generation parameters. |
Default: ignores seq_id, delegates to generate().
Requires ACTIVE.
| seq_id | Sequence identifier. |
| messages | Conversation history. |
| params | Generation parameters. |
Definition at line 698 of file backend.cpp.
| GenerationResult entropic::InferenceBackend::generate_speculative | ( | const std::vector< Message > & | messages, |
| const GenerationParams & | params, | ||
| std::function< void(std::string_view token)> | on_token, | ||
| std::atomic< bool > & | cancel | ||
| ) |
Generate via the speculative-decoding kernel (v2.1.11).
Public entry point for speculative-decoding streaming.
Public wrapper around do_generate_speculative. Validates ACTIVE state, then delegates to the subclass override. Backends that do not implement the kernel return ENTROPIC_ERROR_NOT_SUPPORTED — the orchestrator falls back to generate_streaming in that case.
| messages | Conversation history. |
| params | Generation parameters. |
| on_token | Per-accepted-token callback. |
| cancel | Cancellation flag. |
Mirrors generate_streaming: validates ACTIVE state then delegates to the subclass override. Stamps generation_time_ms. Returns the subclass's NOT_SUPPORTED on stub backends — caller falls back to generate_streaming.
| messages | Conversation history. |
| params | Generation parameters. |
| on_token | Per-accepted-token callback. |
| cancel | Cancellation flag. |
Definition at line 362 of file backend.cpp.
| GenerationResult entropic::InferenceBackend::generate_streaming | ( | const std::vector< Message > & | messages, |
| const GenerationParams & | params, | ||
| std::function< void(std::string_view token)> | on_token, | ||
| std::atomic< bool > & | cancel | ||
| ) |
Generate with per-token streaming callback.
Streaming generation.
| messages | Conversation history. |
| params | Generation parameters. |
| on_token | Called for each token (valid only during callback). |
| cancel | Set to true to abort. Latency: one token. |
Requires ACTIVE state.
| messages | Conversation history. |
| params | Generation parameters. |
| on_token | Per-token callback. |
| cancel | Atomic cancel flag forwarded to the subclass decode loop. |
Definition at line 325 of file backend.cpp.
| GenerationResult entropic::InferenceBackend::generate_streaming_seq | ( | int | seq_id, |
| const std::vector< Message > & | messages, | ||
| const GenerationParams & | params, | ||
| std::function< void(std::string_view token)> | on_token, | ||
| std::atomic< bool > & | cancel | ||
| ) |
Streaming generation with explicit sequence ID.
Streaming generation with sequence ID.
| seq_id | Sequence identifier. |
| messages | Conversation history. |
| params | Generation parameters. |
| on_token | Per-token callback. |
| cancel | Cancellation flag. |
Requires ACTIVE.
| seq_id | Sequence identifier. |
| messages | Conversation history. |
| params | Generation parameters. |
| on_token | Per-token callback. |
| cancel | Cancellation flag. |
Definition at line 730 of file backend.cpp.
| BackendInfo entropic::InferenceBackend::info | ( | ) | const |
Get backend metadata.
Base class returns a default-constructed BackendInfo with name from do_backend_name(). Subclasses override do_info() to populate architecture, quantization, memory usage, etc.
Delegates to do_info().
Definition at line 610 of file backend.cpp.
|
inline |
|
inline |
| bool entropic::InferenceBackend::load | ( | const ModelConfig & | config | ) |
Load model into CPU RAM (COLD → WARM).
| config | Model configuration (path, context length, GPU layers, etc.). |
Acquires transition_mutex_. No-op if already WARM/ACTIVE.
| config | Validated model config. |
Definition at line 54 of file backend.cpp.
| bool entropic::InferenceBackend::load_and_activate | ( | const ModelConfig & | config | ) |
Bring a model to ACTIVE in as few file reads as possible.
Reach ACTIVE in as few whole-file reads as possible (gh#148).
From COLD this is ONE whole-file read, straight into the residency the config asks for (gh#148). It used to be load() then activate(): the first read placed every layer on the CPU with n_gpu_layers = 0, and the second immediately threw that model away and read the whole file again with the configured split. One cold activation, two whole-file reads of a file that can be 13 GB.
From WARM it promotes, which IS still a reload — llama.cpp ties offloading to the model load (design decision #19), so a WARM model cannot re-place its layers without being read again. That path is what keep_warm buys and is deliberately unchanged.
| config | Model configuration (path, context length, gpu_layers). |
From COLD: ONE read, straight into the residency config asks for. Before v2.13.0 this was load() + activate(), which read the whole file with n_gpu_layers = 0, discarded that model, and read the whole file again with the configured split — two reads of a file that can be 13 GB, for one cold activation.
From WARM: promotion, which IS still a reload (design decision #19 — llama.cpp ties offloading to the model load). That is the path keep_warm buys and it is deliberately unchanged.
Holds transition_mutex_ across the whole decision, so the state cannot change between choosing a path and taking it.
| config | Model config. |
Definition at line 200 of file backend.cpp.
|
protected |
WARM → ACTIVE promotion.
WARM → ACTIVE promotion, with the transition mutex already held.
CALLER MUST HOLD transition_mutex_.
Extracted from activate() (gh#148) so load_and_activate can pick between the one-read cold path and this promotion under a SINGLE acquisition of the non-recursive transition mutex.
gh#148 split this out of activate() so load_and_activate can choose between the one-read cold path and this promotion under a single acquisition of the non-recursive transition mutex.
Definition at line 105 of file backend.cpp.
| bool entropic::InferenceBackend::restore_state | ( | int | seq_id, |
| const std::vector< uint8_t > & | buffer | ||
| ) |
Restore model state from buffer.
Restore model state.
| seq_id | Sequence identifier to restore into. |
| buffer | Previously saved state buffer. |
Requires ACTIVE.
| seq_id | Sequence identifier. |
| buffer | Previously saved state. |
Definition at line 650 of file backend.cpp.
Save model state to buffer.
Save model state.
| seq_id | Sequence identifier (0 for single-sequence backends). |
| buffer | Output buffer. Caller owns the returned data. |
For transformers: saves KV cache state for the sequence. For recurrent: saves hidden state.
Requires ACTIVE.
| seq_id | Sequence identifier. |
| buffer | Caller-owned output buffer. |
Definition at line 625 of file backend.cpp.
|
inlineprotected |
|
inline |
| bool entropic::InferenceBackend::supports | ( | BackendCapability | cap | ) | const |
Query whether this backend supports a capability.
Query backend capability.
| cap | Capability to query. |
Base class returns false for all capabilities. Subclasses override do_supports() to declare their capabilities. Lock-free — no state transitions involved.
Delegates to do_supports().
| cap | Capability to query. |
Definition at line 575 of file backend.cpp.
|
inlinevirtual |
Tokenize text to token IDs.
| text | Input text. |
Reimplemented in entropic::LlamaCppBackend.
|
inlinevirtual |
The tool-call CLOSE marker for the active chat format (gh#103).
Returns the string that terminates a single tool call in the model's native format (e.g. "</tool_call>"), or "" when there is no reliable marker (no tools staged / content-only format). The orchestrator appends it to GenerationParams.stop for tiers in "sequential" tool_call_mode so the decode loop halts at the FIRST closed tool call (one tool/turn). Default "" → no stop injected (batch behavior). Backends that own a native tool-call format override this. (gh#103)
Reimplemented in entropic::LlamaCppBackend.
| void entropic::InferenceBackend::unload | ( | ) |
Full unload (→ COLD).
Releases all RAM + VRAM.
Idempotent.
The base half of teardown: fires ON_MODEL_UNLOAD, delegates the resource release to do_unload(), and lands the state machine in COLD. Safe to call on a backend whose load failed, and safe to call twice.
@req REQ-INFER-002
Definition at line 159 of file backend.cpp.
|
protected |
|
protected |
State transition slot accessible to subclasses for test-only injection.
Pre-v2.3.10 this lived in the private section, blocking LlamaCppBackend::inject_tokenizer_for_test from flipping the backend into WARM without a real model. Production code path (load/activate/etc) still owns the normal transitions; this opens just a side door for mocks.