pub enum PfxTree {
Branch(Arc<IndexMap<Pfx, PfxTree>>),
Leaf(usize),
}Expand description
A tree structure for generating unique prefix keys.
The tree width is determined by the number of logprobs the LLM generates.
For flat structures (`A`, `B`), each leaf corresponds to one logprob slot.
For large response sets exceeding the logprobs limit, nested structures
(`A` `A`, `A` `B`) allow capturing preferences in stages.
Variants§
Branch(Arc<IndexMap<Pfx, PfxTree>>)
A branch containing child nodes.
Leaf(usize)
A leaf containing the response index.
Implementations§
Source§impl PfxTree
impl PfxTree
Sourcepub fn new(rng: &mut impl Rng, source_len: usize, max_branch_len: usize) -> Self
pub fn new(rng: &mut impl Rng, source_len: usize, max_branch_len: usize) -> Self
Creates a new prefix tree for the given number of responses.
The max_branch_len should match the number of logprobs the LLM generates,
ensuring each branch fits within one logprobs batch for probability capture.
Sourcepub fn new_inner(
rng: &mut impl Rng,
source: &[usize],
max_branch_len: usize,
force_sub_branch: bool,
) -> Self
pub fn new_inner( rng: &mut impl Rng, source: &[usize], max_branch_len: usize, force_sub_branch: bool, ) -> Self
Internal recursive constructor.
Sourcepub fn pfx_indices(
&self,
rng: &mut impl Rng,
source_len: usize,
) -> Vec<(String, usize)>
pub fn pfx_indices( &self, rng: &mut impl Rng, source_len: usize, ) -> Vec<(String, usize)>
Generates prefix-to-index mappings in randomized order.
Returns pairs of (prefix key, response index).
Sourcepub fn pfx_indices_inner(
&self,
parent_pfx: Option<String>,
indices: &mut Vec<(String, usize)>,
)
pub fn pfx_indices_inner( &self, parent_pfx: Option<String>, indices: &mut Vec<(String, usize)>, )
Internal recursive method for generating prefix indices.
Sourcepub fn unwrap_leaf(&self) -> usize
pub fn unwrap_leaf(&self) -> usize
Unwraps a leaf node to get its response index.
Panics if called on a branch node.
Trait Implementations§
Auto Trait Implementations§
impl Freeze for PfxTree
impl RefUnwindSafe for PfxTree
impl Send for PfxTree
impl Sync for PfxTree
impl Unpin for PfxTree
impl UnwindSafe for PfxTree
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> CloneToUninit for Twhere
T: Clone,
impl<T> CloneToUninit for Twhere
T: Clone,
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more