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

source

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);
source

pub fn from_iter<T>(data: T, sorted: bool) -> LlamaTokenDataArray
where 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

source

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 be None 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

source§

fn clone(&self) -> LlamaTokenDataArray

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for LlamaTokenDataArray

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl PartialEq for LlamaTokenDataArray

source§

fn eq(&self, other: &LlamaTokenDataArray) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl StructuralPartialEq for LlamaTokenDataArray

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for T
where T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for T
where T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for T
where U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for T
where T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more