liblloyal 1.0.0
Branched Inference for llama.cpp
Loading...
Searching...
No Matches
lloyal::branch::Branch Class Reference

#include <lloyal/branch.hpp>

Public Member Functions

 Branch ()
 
 Branch (BranchStore *store, BranchHandle handle)
 
 ~Branch ()
 Destructor — CASCADE prunes entire subtree.
 
 Branch (Branch &&other) noexcept
 
Branchoperator= (Branch &&other) noexcept
 
 Branch (const Branch &)=delete
 
Branchoperator= (const Branch &)=delete
 
Branch fork ()
 Fork: allocates slot + lease, records topology edge.
 
void prune ()
 RESTRICT prune (throws if children exist)
 
void pruneSubtree ()
 CASCADE prune — removes entire subtree.
 
void force_snapshot_logits ()
 Force-copy shared logits buffer into this branch's snapshot.
 
void prefill (const llama_token *tokens, size_t n)
 Decode multiple tokens and capture logits atomically (prompt injection)
 
void step (llama_token token)
 Decode one token and capture logits (generation step)
 
const float * logits () const
 Get the branch's captured logits snapshot.
 
llama_token sample ()
 Sample a token from captured logits.
 
void accept (llama_token token)
 Accept a token — advance grammar, penalty window, and metrics.
 
bool is_eog (llama_token token) const
 Check if a token is end-of-generation for this branch's model.
 
template<SamplingParamsLike P>
void setSamplerParams (const P &params)
 Replace sampler chain with new parameters (memoized)
 
void setGrammar (const char *grammar_str)
 Replace grammar constraint (nullptr/empty to remove)
 
llama_pos position () const
 Current decode position (token count)
 
llama_pos forkHead () const
 Parent's position at fork time (0 for root branches)
 
float perplexity () const
 Model-level perplexity (from raw logits, pre-filter)
 
int n_vocab () const
 Vocabulary size.
 
bool valid () const
 True if this Branch holds a valid handle.
 
BranchHandle handle () const
 Underlying opaque handle (for interop with free functions)
 
BranchHandle parentHandle () const
 Parent branch handle, or INVALID_HANDLE if root.
 
const std::vector< BranchHandle > & childHandles () const
 Child branch handles (empty if leaf)
 
bool isLeaf () const
 True if this branch has no children.
 
bool isActive () const
 True if this branch holds a KV lease.
 

Static Public Member Functions

template<SamplingParamsLike P>
static Branch create (llama_context *ctx, const llama_model *model, BranchStore &store, llama_pos start_pos, const P &params, int n_batch=DEFAULT_N_BATCH, const char *grammar_str=nullptr, boundaries::BoundaryTracker *boundary_tracker=nullptr)
 Factory: allocates slot + lease from store.
 

Detailed Description

Definition at line 2409 of file branch.hpp.

Constructor & Destructor Documentation

◆ Branch() [1/4]

lloyal::branch::Branch::Branch ( )
inline

◆ Branch() [2/4]

lloyal::branch::Branch::Branch ( BranchStore store,
BranchHandle  handle 
)
inline

Definition at line 2413 of file branch.hpp.

◆ ~Branch()

lloyal::branch::Branch::~Branch ( )
inline

Destructor — CASCADE prunes entire subtree.

Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2417 of file branch.hpp.

◆ Branch() [3/4]

lloyal::branch::Branch::Branch ( Branch &&  other)
inlinenoexcept

Definition at line 2423 of file branch.hpp.

◆ Branch() [4/4]

lloyal::branch::Branch::Branch ( const Branch )
delete

Member Function Documentation

◆ accept()

void lloyal::branch::Branch::accept ( llama_token  token)
inline

Accept a token — advance grammar, penalty window, and metrics.

Parameters
tokenToken to accept (from sample())
Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2510 of file branch.hpp.

◆ childHandles()

const std::vector< BranchHandle > & lloyal::branch::Branch::childHandles ( ) const
inline

Child branch handles (empty if leaf)

Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2554 of file branch.hpp.

◆ create()

template<SamplingParamsLike P>
static Branch lloyal::branch::Branch::create ( llama_context *  ctx,
const llama_model *  model,
BranchStore store,
llama_pos  start_pos,
const P &  params,
int  n_batch = DEFAULT_N_BATCH,
const char *  grammar_str = nullptr,
boundaries::BoundaryTracker boundary_tracker = nullptr 
)
inlinestatic

Factory: allocates slot + lease from store.

Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2445 of file branch.hpp.

◆ force_snapshot_logits()

void lloyal::branch::Branch::force_snapshot_logits ( )
inline

Force-copy shared logits buffer into this branch's snapshot.

Copies the logits from the llama_context into the branch's internal buffer without performing a decode.

Warning
The shared buffer contains logits from the LAST llama_decode() call. If another branch (or batched decode) ran since this branch's last decode, the snapshot will contain wrong logits. Only call this immediately after a single-branch decode for this handle. Prefer prefill()/step() which capture atomically.
Parameters
handleBranch to capture logits for
sBranch store
Exceptions
std::runtime_errorif handle is invalid, vocab size is zero, or no logits are available (no prior decode with logits enabled)
Note
Sets has_logits = true, enabling sample() and get_logits().
Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2478 of file branch.hpp.

◆ fork()

Branch lloyal::branch::Branch::fork ( )
inline

Fork: allocates slot + lease, records topology edge.

Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2459 of file branch.hpp.

◆ forkHead()

llama_pos lloyal::branch::Branch::forkHead ( ) const
inline

Parent's position at fork time (0 for root branches)

Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2539 of file branch.hpp.

◆ handle()

BranchHandle lloyal::branch::Branch::handle ( ) const
inline

Underlying opaque handle (for interop with free functions)

Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2547 of file branch.hpp.

◆ is_eog()

bool lloyal::branch::Branch::is_eog ( llama_token  token) const
inline

Check if a token is end-of-generation for this branch's model.

Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2515 of file branch.hpp.

◆ isActive()

bool lloyal::branch::Branch::isActive ( ) const
inline

True if this branch holds a KV lease.

Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2561 of file branch.hpp.

◆ isLeaf()

bool lloyal::branch::Branch::isLeaf ( ) const
inline

True if this branch has no children.

Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2559 of file branch.hpp.

◆ logits()

const float * lloyal::branch::Branch::logits ( ) const
inline

Get the branch's captured logits snapshot.

Returns
Pointer to n_vocab floats, or nullptr if no logits captured
Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2497 of file branch.hpp.

◆ n_vocab()

int lloyal::branch::Branch::n_vocab ( ) const
inline

Vocabulary size.

Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2543 of file branch.hpp.

◆ operator=() [1/2]

Branch & lloyal::branch::Branch::operator= ( Branch &&  other)
inlinenoexcept

◆ operator=() [2/2]

Branch & lloyal::branch::Branch::operator= ( const Branch )
delete

◆ parentHandle()

BranchHandle lloyal::branch::Branch::parentHandle ( ) const
inline

Parent branch handle, or INVALID_HANDLE if root.

Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2552 of file branch.hpp.

◆ perplexity()

float lloyal::branch::Branch::perplexity ( ) const
inline

Model-level perplexity (from raw logits, pre-filter)

Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2541 of file branch.hpp.

◆ position()

llama_pos lloyal::branch::Branch::position ( ) const
inline

Current decode position (token count)

Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2537 of file branch.hpp.

◆ prefill()

void lloyal::branch::Branch::prefill ( const llama_token *  tokens,
size_t  n 
)
inline

Decode multiple tokens and capture logits atomically (prompt injection)

Parameters
tokensArray of token IDs
nNumber of tokens
Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2485 of file branch.hpp.

◆ prune()

void lloyal::branch::Branch::prune ( )
inline

RESTRICT prune (throws if children exist)

Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2465 of file branch.hpp.

◆ pruneSubtree()

void lloyal::branch::Branch::pruneSubtree ( )
inline

CASCADE prune — removes entire subtree.

Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2471 of file branch.hpp.

◆ sample()

llama_token lloyal::branch::Branch::sample ( )
inline

Sample a token from captured logits.

Returns
Sampled token ID, or -1 if no logits captured
See also
accept() to advance state after sampling
Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2504 of file branch.hpp.

◆ setGrammar()

void lloyal::branch::Branch::setGrammar ( const char *  grammar_str)
inline

Replace grammar constraint (nullptr/empty to remove)

See also
branch::set_grammar()
Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2529 of file branch.hpp.

◆ setSamplerParams()

template<SamplingParamsLike P>
void lloyal::branch::Branch::setSamplerParams ( const P &  params)
inline

Replace sampler chain with new parameters (memoized)

See also
branch::set_sampler_params()
Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2523 of file branch.hpp.

◆ step()

void lloyal::branch::Branch::step ( llama_token  token)
inline

Decode one token and capture logits (generation step)

Parameters
tokenToken ID to decode
Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2491 of file branch.hpp.

◆ valid()

bool lloyal::branch::Branch::valid ( ) const
inline

True if this Branch holds a valid handle.

Examples
/home/runner/work/liblloyal/liblloyal/include/lloyal/branch.hpp.

Definition at line 2545 of file branch.hpp.


The documentation for this class was generated from the following file: