Struct llama_cpp_2::token::data_array::LlamaTokenDataArray
source · pub struct LlamaTokenDataArray {
pub data: Vec<LlamaTokenData>,
pub sorted: bool,
}
Expand description
a safe wrapper around llama_token_data_array
.
Fields§
§data: Vec<LlamaTokenData>
the underlying data
sorted: bool
is the data sorted?
Implementations§
source§impl LlamaTokenDataArray
impl LlamaTokenDataArray
sourcepub fn new(data: Vec<LlamaTokenData>, sorted: bool) -> Self
pub fn new(data: Vec<LlamaTokenData>, sorted: bool) -> Self
Create a new LlamaTokenDataArray
from a vector and weather or not the data is sorted.
let array = LlamaTokenDataArray::new(vec![
LlamaTokenData::new(LlamaToken(0), 0.0, 0.0),
LlamaTokenData::new(LlamaToken(1), 0.1, 0.1)
], false);
assert_eq!(array.data.len(), 2);
assert_eq!(array.sorted, false);
sourcepub fn from_iter<T>(data: T, sorted: bool) -> LlamaTokenDataArraywhere
T: IntoIterator<Item = LlamaTokenData>,
pub fn from_iter<T>(data: T, sorted: bool) -> LlamaTokenDataArraywhere
T: IntoIterator<Item = LlamaTokenData>,
Create a new LlamaTokenDataArray
from an iterator and weather or not the data is sorted.
let array = LlamaTokenDataArray::from_iter([
LlamaTokenData::new(LlamaToken(0), 0.0, 0.0),
LlamaTokenData::new(LlamaToken(1), 0.1, 0.1)
], false);
assert_eq!(array.data.len(), 2);
assert_eq!(array.sorted, false);
source§impl LlamaTokenDataArray
impl LlamaTokenDataArray
sourcepub fn sample_repetition_penalty(
&mut self,
ctx: Option<&mut LlamaContext<'_>>,
last_tokens: &[LlamaToken],
penalty_last_n: usize,
penalty_repeat: f32,
penalty_freq: f32,
penalty_present: f32
)
pub fn sample_repetition_penalty( &mut self, ctx: Option<&mut LlamaContext<'_>>, last_tokens: &[LlamaToken], penalty_last_n: usize, penalty_repeat: f32, penalty_freq: f32, penalty_present: f32 )
Repetition penalty described in CTRL academic paper, with negative logit fix. Frequency and presence penalties described in OpenAI API.
§Parameters
ctx
- the context to use. May beNone
if you do not care to record the sample timings.last_tokens
- the last tokens in the context.penalty_last_n
- the number of tokens to consider for the repetition penalty. (1.0 for no penalty)penalty_repeat
- the repetition penalty. (0.0 for no penalty)penalty_freq
- the frequency penalty. (0.0 for no penalty)
§Example
let history = vec![
LlamaToken::new(2),
LlamaToken::new(1),
LlamaToken::new(0),
];
let candidates = vec![
LlamaToken::new(0),
LlamaToken::new(1),
LlamaToken::new(2),
LlamaToken::new(3),
];
let mut candidates = LlamaTokenDataArray::from_iter(candidates.iter().map(|&token| LlamaTokenData::new(token, 0.0, 0.0)), false);
candidates.sample_repetition_penalty(None, &history, 2, 1.1, 0.1, 0.1);
let token_logits = candidates.data.into_iter().map(|token_data| (token_data.id(), token_data.logit())).collect::<BTreeMap<_, _>>();
assert_eq!(token_logits[&LlamaToken(0)], 0.0, "expected no penalty as it is out of `penalty_last_n`");
assert!(token_logits[&LlamaToken(1)] < 0.0, "expected penalty as it is in `penalty_last_n`");
assert!(token_logits[&LlamaToken(2)] < 0.0, "expected penalty as it is in `penalty_last_n`");
assert_eq!(token_logits[&LlamaToken(3)], 0.0, "expected no penalty as it is not in `history`");
Trait Implementations§
source§impl Clone for LlamaTokenDataArray
impl Clone for LlamaTokenDataArray
source§fn clone(&self) -> LlamaTokenDataArray
fn clone(&self) -> LlamaTokenDataArray
Returns a copy of the value. Read more
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
Performs copy-assignment from
source
. Read moresource§impl Debug for LlamaTokenDataArray
impl Debug for LlamaTokenDataArray
source§impl PartialEq for LlamaTokenDataArray
impl PartialEq for LlamaTokenDataArray
source§fn eq(&self, other: &LlamaTokenDataArray) -> bool
fn eq(&self, other: &LlamaTokenDataArray) -> bool
This method tests for
self
and other
values to be equal, and is used
by ==
.impl StructuralPartialEq for LlamaTokenDataArray
Auto Trait Implementations§
impl RefUnwindSafe for LlamaTokenDataArray
impl Send for LlamaTokenDataArray
impl Sync for LlamaTokenDataArray
impl Unpin for LlamaTokenDataArray
impl UnwindSafe for LlamaTokenDataArray
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
Mutably borrows from an owned value. Read more