135 const std::vector<Message>& messages,
159 const std::vector<Message>& messages,
161 std::atomic<bool>&
cancel);
173 const std::vector<Message>& messages,
175 std::function<
void(std::string_view token)> on_token,
176 std::atomic<bool>&
cancel);
195 const std::vector<Message>& messages,
197 std::function<
void(std::string_view token)> on_token,
198 std::atomic<bool>&
cancel);
208 const std::string& prompt,
288 const std::string& text)
const {
return {}; }
446 const std::vector<Message>& messages,
461 const std::vector<Message>& messages,
463 std::function<
void(std::string_view token)> on_token,
464 std::atomic<bool>&
cancel);
484 const std::vector<std::vector<Message>>&
requests,
485 const std::vector<GenerationParams>&
params,
486 std::atomic<bool>&
cancel);
568 const std::vector<Message>& messages,
589 const std::vector<Message>& messages,
591 std::atomic<bool>&
cancel) {
612 const std::vector<std::vector<Message>>&
requests,
613 const std::vector<GenerationParams>&
params,
614 std::atomic<bool>&
cancel) {
615 std::vector<GenerationResult> out;
617 for (std::size_t
i = 0;
i <
requests.size(); ++
i) {
633 const std::vector<Message>& messages,
635 std::function<
void(std::string_view token)> on_token,
636 std::atomic<bool>&
cancel) = 0;
662 const std::vector<Message>& messages,
664 std::function<
void(std::string_view token)> on_token,
665 std::atomic<bool>&
cancel);
675 const std::string& prompt,
746 std::vector<uint8_t>&
buffer)
const;
756 const std::vector<uint8_t>&
buffer);
779 const std::vector<Message>& messages,
797 const std::vector<Message>& messages,
799 std::function<
void(std::string_view token)> on_token,
800 std::atomic<bool>&
cancel);
847 std::mutex transition_mutex_;
848 std::mutex eval_mutex_;
Backend capability flags and metadata for architecture-agnostic queries.
Concrete base class for inference backends (80% logic).
virtual bool do_load_active(const ModelConfig &config)
Load COLD → ACTIVE in one step.
virtual GenerationResult do_complete(const std::string &prompt, const GenerationParams ¶ms)=0
Subclass raw completion.
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.
GenerationResult generate_seq(int seq_id, const std::vector< Message > &messages, const GenerationParams ¶ms)
Generate with explicit sequence ID.
virtual LogprobResult do_evaluate_logprobs(const int32_t *tokens, int n_tokens)=0
Backend-specific logprob evaluation.
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).
float compute_perplexity(const int32_t *tokens, int n_tokens)
Compute perplexity for a token sequence.
virtual std::string active_tool_grammar() const
The tool-call GBNF the last render derived, if any (gh#154).
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).
std::string last_error_
Last error message for diagnostics.
virtual BackendInfo do_info() const
Populate backend metadata.
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.
bool save_state(int seq_id, std::vector< uint8_t > &buffer) const
Save model state to buffer.
void set_hooks(const HookInterface &hooks)
Set the hook dispatch interface.
bool supports(BackendCapability cap) const
Query whether this backend supports a capability.
bool restore_state(int seq_id, const std::vector< uint8_t > &buffer)
Restore model state from buffer.
bool activate()
Promote to GPU (WARM → ACTIVE).
virtual bool do_restore_state(int seq_id, const std::vector< uint8_t > &buffer)
Restore model state.
virtual int do_count_tokens(const std::string &text) const =0
Subclass token counting.
virtual bool do_supports(BackendCapability cap) const
Declare supported capabilities.
void deactivate()
Release GPU layers (ACTIVE → WARM).
bool promote_to_active()
WARM → ACTIVE promotion.
virtual void do_unload()=0
Full unload.
virtual bool do_activate()=0
Promote loaded model to GPU.
BackendInfo info() const
Get backend metadata.
bool is_active() const
True when state is ACTIVE.
ModelState state() const
Current lifecycle state (lock-free read).
virtual bool do_load(const ModelConfig &config)=0
Load model into CPU RAM.
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 void clear_prompt_cache()
Invalidate any backend-owned prompt/KV caches.
virtual void do_deactivate()=0
Release GPU, keep CPU.
virtual std::vector< int32_t > tokenize_text(const std::string &text) const
Tokenize text to token IDs.
virtual GenerationResult do_generate(const std::vector< Message > &messages, const GenerationParams ¶ms)=0
Subclass generation.
std::vector< BackendCapability > capabilities() const
Get all supported capabilities as a vector.
void unload()
Full unload (→ COLD).
const ModelConfig & config() const
Stored model config.
virtual void forget_session_kv(const std::string &session_key)
Drop one session's resident KV and release its slot (gh#165).
virtual std::string tool_call_close_marker() const
The tool-call CLOSE marker for the active chat format (gh#103).
bool clear_state(int seq_id=-1)
Clear/reset model state for a sequence.
virtual GenerationResult do_generate_seq(int seq_id, const std::vector< Message > &messages, const GenerationParams ¶ms)
Generate with sequence ID.
virtual std::string do_backend_name() const =0
Return backend name identifier.
bool is_loaded() const
True when state is WARM or ACTIVE.
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.
GenerationResult generate(const std::vector< Message > &messages, const GenerationParams ¶ms)
Generate a complete response.
bool load(const ModelConfig &config)
Load model into CPU RAM (COLD → WARM).
virtual bool do_clear_state(int seq_id)
Clear/reset model state.
int count_tokens(const std::string &text) const
Count tokens using model's tokenizer.
virtual bool do_save_state(int seq_id, std::vector< uint8_t > &buffer) const
Save model state (KV cache or hidden state).
bool fire_model_load_hook(const ModelConfig &config)
Fire ON_MODEL_LOAD pre-hook.
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.
LogprobResult evaluate_logprobs(const int32_t *tokens, int n_tokens)
Evaluate per-token log-probabilities for a token sequence.
virtual GenerationResult do_generate(const std::vector< Message > &messages, const GenerationParams ¶ms, std::atomic< bool > &cancel)
Subclass batch generation with cancel-flag support.
int context_length() const
Model's context window size.
bool load_and_activate(const ModelConfig &config)
Bring a model to ACTIVE in as few file reads as possible.
GenerationResult complete(const std::string &prompt, const GenerationParams ¶ms)
Raw text completion without chat template.
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::atomic< ModelState > state_
State transition slot accessible to subclasses for test-only injection.
Configuration structs with defaults.
int * cancel
Consumer's cancel flag, nullable.
Generation output with metrics.
Hook dispatch interface injected into engine subsystems.
Per-token log-probability evaluation result.
Message struct for conversation history.
Activate model on GPU (WARM → ACTIVE).
BackendCapability
Capabilities that an inference backend may or may not support.
@ tokens
Gate on generated tokens since the last tool call.
@ optional
optional: every escape asks the host's path approver
ModelState
C++ enum class for model VRAM lifecycle states.
@ ACTIVE
GPU layers loaded, full speed.
@ COLD
On disk only, no RAM consumed.
Backend metadata for introspection.
Generation parameters for a single inference call.
Result of a single generation call.
Per-token log-probability evaluation result.
Model configuration for a single tier.
int context_length
Context window size (512–131072)