|
liblloyal 1.0.0
Branched Inference for llama.cpp
|
Consolidated mutable state for a single branch. More...
#include <lloyal/branch.hpp>
Public Member Functions | |
| void | poison () |
| void | capture_logits (int32_t index) |
Copy the logits at batch row index into this branch's private snapshot. | |
Public Attributes | |
| llama_context * | ctx = nullptr |
| Llama context (not owned, must outlive branch) | |
| const llama_model * | model = nullptr |
| Llama model (not owned, must outlive branch) | |
| llama_seq_id | seq_id = NO_LEASE |
| KV cache sequence identifier (NO_LEASE when inactive) | |
| llama_pos | position = 0 |
| Current decode position in the sequence. | |
| llama_pos | fork_head = 0 |
| Parent's position at fork time (0 for root branches) | |
| uint64_t | revision = 0 |
| Counts every change to the state fast_forward() moves: decodes, logits, sampling, grammar and bias. | |
| uint64_t | fork_revision = 0 |
| The parent's revision at fork time (0 for root branches). | |
| bool | poisoned = false |
| A decode failed and left the KV ahead of these books (see decode::leaves_branch_intact). | |
| uint32_t | img_slack_own = 0 |
| Embedding-row slack: cells beyond position-delta from decode_embd() (an M-RoPE image adds nx*ny cells but advances position by max(nx,ny)). | |
| uint32_t | img_slack_total = 0 |
| SamplerChainHandle | sampler_chain = 0 |
| Handle into BranchStore's sampler registry. | |
| GrammarHandle | grammar = 0 |
| Handle into BranchStore's grammar registry. | |
| bool | grammar_sample_first = false |
| Grammar order for sample(): false masks the whole vocabulary before the chain (exact under truncation); true lets the chain pick first and asks the grammar about that one token, masking only when it is refused. | |
| CachedSamplingParams | cached_params |
| Params used to create current chain (for memoization) | |
| boundaries::BoundaryTracker * | boundary_tracker = nullptr |
| Token boundary detector (owned, optional) | |
| std::vector< llama_logit_bias > | logit_bias |
| Static token biases, cloned on fork. | |
| std::function< void(llama_token_data_array &)> | steer_fn |
| Dynamic logit callback, NOT cloned on fork. | |
| MetricsHandle | metrics = 0 |
| Handle into BranchStore's metrics registry. | |
| llama_token | last_token = -1 |
| Last token returned by sample() | |
| std::vector< llama_token_data > | last_candidates |
| Filtered candidates from last sample() | |
| std::vector< float > | logits_snapshot |
| Captured logit distribution (n_vocab floats) | |
| bool | has_logits = false |
| True once logits_snapshot holds a live distribution: capture_logits(), set_logits(), or a fork that cloned it. | |
| std::vector< llama_token_data > | candidates_buffer |
| Reusable scratch buffer for sampling (avoids O(n_vocab) allocs per sample call). | |
| int | n_batch = DEFAULT_N_BATCH |
| Batch size for decode operations. | |
| int | n_vocab = 0 |
| Vocabulary size (cached for buffer pre-allocation) | |
| uint16_t | generation = 0 |
| Slot generation counter (for ABA prevention) | |
| bool | in_use = false |
| True when slot is allocated to an active branch. | |
| BranchHandle | parent = INVALID_HANDLE |
| Parent branch (INVALID_HANDLE if root) | |
| std::vector< BranchHandle > | children |
| Child branches forked from this one. | |
Consolidated mutable state for a single branch.
Each branch encapsulates all state needed for independent generation:
Forkable state (cloned by fork()):
Non-forkable state (NOT cloned by fork()):
Sampling application order in sample(): Grammar → Logit Bias → Steer → Sampler Chain or, with grammar_sample_first: Logit Bias → Steer → Sampler Chain → Grammar check of the pick, falling back to the first order only when the pick is refused.
Definition at line 293 of file branch.hpp.
|
inline |
Copy the logits at batch row index into this branch's private snapshot.
The context's buffer is shared and overwritten by the next llama_decode; the snapshot is what sample()/get_logits() read. A branch with no vocab has nowhere to put them — refused loud, never copied into an empty buffer.
Definition at line 354 of file branch.hpp.
|
inline |
Definition at line 312 of file branch.hpp.
| boundaries::BoundaryTracker* lloyal::branch::BranchState::boundary_tracker = nullptr |
Token boundary detector (owned, optional)
Definition at line 335 of file branch.hpp.
| CachedSamplingParams lloyal::branch::BranchState::cached_params |
Params used to create current chain (for memoization)
Definition at line 333 of file branch.hpp.
| std::vector<llama_token_data> lloyal::branch::BranchState::candidates_buffer |
Reusable scratch buffer for sampling (avoids O(n_vocab) allocs per sample call).
Memory footprint per branch for large vocab models (128k tokens):
last_candidates: typically ~40 entries with top_k=40 (~480 bytes)
Definition at line 372 of file branch.hpp.
| std::vector<BranchHandle> lloyal::branch::BranchState::children |
Child branches forked from this one.
Definition at line 382 of file branch.hpp.
| llama_context* lloyal::branch::BranchState::ctx = nullptr |
Llama context (not owned, must outlive branch)
Definition at line 294 of file branch.hpp.
| llama_pos lloyal::branch::BranchState::fork_head = 0 |
Parent's position at fork time (0 for root branches)
Definition at line 299 of file branch.hpp.
| uint64_t lloyal::branch::BranchState::fork_revision = 0 |
The parent's revision at fork time (0 for root branches).
Definition at line 306 of file branch.hpp.
| uint16_t lloyal::branch::BranchState::generation = 0 |
Slot generation counter (for ABA prevention)
Definition at line 377 of file branch.hpp.
| GrammarHandle lloyal::branch::BranchState::grammar = 0 |
Handle into BranchStore's grammar registry.
Definition at line 327 of file branch.hpp.
| bool lloyal::branch::BranchState::grammar_sample_first = false |
Grammar order for sample(): false masks the whole vocabulary before the chain (exact under truncation); true lets the chain pick first and asks the grammar about that one token, masking only when it is refused.
Reset to false whenever a grammar is attached; cloned by fork().
Definition at line 331 of file branch.hpp.
| bool lloyal::branch::BranchState::has_logits = false |
True once logits_snapshot holds a live distribution: capture_logits(), set_logits(), or a fork that cloned it.
Definition at line 346 of file branch.hpp.
| uint32_t lloyal::branch::BranchState::img_slack_own = 0 |
Embedding-row slack: cells beyond position-delta from decode_embd() (an M-RoPE image adds nx*ny cells but advances position by max(nx,ny)).
own = slack accrued on THIS branch since its fork (release() adds it to the position-delta when subtracting unique cells); total = inherited + own for the whole prefix (retainOnly()'s winner absorbs the prefix via fork_head=0 and needs it). fork() copies total, zeroes own.
Definition at line 323 of file branch.hpp.
| uint32_t lloyal::branch::BranchState::img_slack_total = 0 |
Definition at line 324 of file branch.hpp.
| bool lloyal::branch::BranchState::in_use = false |
True when slot is allocated to an active branch.
Definition at line 378 of file branch.hpp.
| std::vector<llama_token_data> lloyal::branch::BranchState::last_candidates |
Filtered candidates from last sample()
Definition at line 343 of file branch.hpp.
| llama_token lloyal::branch::BranchState::last_token = -1 |
Last token returned by sample()
Definition at line 342 of file branch.hpp.
| std::vector<llama_logit_bias> lloyal::branch::BranchState::logit_bias |
Static token biases, cloned on fork.
Definition at line 337 of file branch.hpp.
| std::vector<float> lloyal::branch::BranchState::logits_snapshot |
Captured logit distribution (n_vocab floats)
Definition at line 345 of file branch.hpp.
| MetricsHandle lloyal::branch::BranchState::metrics = 0 |
Handle into BranchStore's metrics registry.
Definition at line 340 of file branch.hpp.
| const llama_model* lloyal::branch::BranchState::model = nullptr |
Llama model (not owned, must outlive branch)
Definition at line 295 of file branch.hpp.
| int lloyal::branch::BranchState::n_batch = DEFAULT_N_BATCH |
Batch size for decode operations.
Definition at line 374 of file branch.hpp.
| int lloyal::branch::BranchState::n_vocab = 0 |
Vocabulary size (cached for buffer pre-allocation)
Definition at line 375 of file branch.hpp.
| BranchHandle lloyal::branch::BranchState::parent = INVALID_HANDLE |
Parent branch (INVALID_HANDLE if root)
Definition at line 381 of file branch.hpp.
| bool lloyal::branch::BranchState::poisoned = false |
A decode failed and left the KV ahead of these books (see decode::leaves_branch_intact).
Prune and replay; a fork shares the damage and inherits the mark.
Definition at line 310 of file branch.hpp.
| llama_pos lloyal::branch::BranchState::position = 0 |
Current decode position in the sequence.
Definition at line 298 of file branch.hpp.
| uint64_t lloyal::branch::BranchState::revision = 0 |
Counts every change to the state fast_forward() moves: decodes, logits, sampling, grammar and bias.
Compared for equality only, so a fork can tell whether its parent is still the branch it was forked from.
Definition at line 304 of file branch.hpp.
| SamplerChainHandle lloyal::branch::BranchState::sampler_chain = 0 |
Handle into BranchStore's sampler registry.
Definition at line 326 of file branch.hpp.
| llama_seq_id lloyal::branch::BranchState::seq_id = NO_LEASE |
KV cache sequence identifier (NO_LEASE when inactive)
Definition at line 297 of file branch.hpp.
| std::function<void(llama_token_data_array&)> lloyal::branch::BranchState::steer_fn |
Dynamic logit callback, NOT cloned on fork.
Definition at line 338 of file branch.hpp.