Skip to main content

AttentionOptions

Struct AttentionOptions 

Source
#[non_exhaustive]
pub struct AttentionOptions<'a> { pub mask: AttentionMask<'a>, pub sinks: Option<&'a Array>, pub window: Option<i32>, pub max_score_bytes: Option<usize>, }
Expand description

The options of scaled_dot_product_attention and plan_attention: the mask, attention sinks, a sliding window and a cap on score memory. Build them with AttentionOptions::causal or Default and the chained setters:

use nuvec_core::fast::AttentionOptions;

let opts = AttentionOptions::causal().window(4096).max_score_bytes(512 << 20);
assert_eq!(opts.window, Some(4096));

This is the one options type of attention: new features (window and head-size-512 kernels, backward routing) extend it, never a second entry point.

Fields (Non-exhaustive)§

This struct is marked as non-exhaustive
Non-exhaustive structs could have additional fields added in future. Therefore, non-exhaustive structs cannot be constructed in external crates using the traditional Struct { .. } syntax; cannot be matched against without a wildcard ..; and struct update syntax will not work.
§mask: AttentionMask<'a>

The mask: none (the default), causal, or an array.

§sinks: Option<&'a Array>

Attention sinks ([Hq]): one logit per query head that takes part in the softmax but attends to nothing.

§window: Option<i32>

A sliding window: query i attends only to the window keys up to and including its position (as mlx-lm’s create_causal_mask(..., window_size)). Needs the causal mask.

§max_score_bytes: Option<usize>

A cap, in bytes, on every score-sized intermediate (scores, masked scores, the float32 sink-softmax intermediates, materialised masks); see scaled_dot_product_attention.

Implementations§

Source§

impl<'a> AttentionOptions<'a>

Source

pub fn causal() -> Self

Causal masking: query i attends to keys 0..=i + Lk - Lq (the queries are the last Lq positions).

Source

pub fn mask(self, mask: &'a Array) -> Self

A boolean (true attends) or additive mask, broadcastable to [B, Hq, Lq, Lk].

Source

pub fn sinks(self, sinks: impl Into<Option<&'a Array>>) -> Self

Attention sinks, [Hq] (None for none).

Source

pub fn window(self, keys: i32) -> Self

A sliding window of keys keys (needs AttentionOptions::causal): query i attends to keys i + Lk − Lq − keys < j ≤ i + Lk − Lq. The vector and tiled kernels skip the keys before the window, so a full cache needs no slicing; a cache that keeps only the last W − 1 + L positions (nuvec-lm’s sliding-window KvCache) passes just those, with the same result:

use nuvec_core::{DType, fast::{self, AttentionOptions}, random};

let (w, l) = (4, 2);
let q = random::normal([1, 2, l, 8], DType::F32, 0.0, 1.0, None);
let k = random::normal([1, 2, 10, 8], DType::F32, 0.0, 1.0, None);
let opts = AttentionOptions::causal().window(w);
let full = fast::scaled_dot_product_attention(&q, &k, &k, 0.35, &opts);
let tail = k.i((.., .., 10 - (w - 1 + l)..));
let sliced = fast::scaled_dot_product_attention(&q, &tail, &tail, 0.35, &opts);
let diff = full.subtract(&sliced).abs().max(..).item::<f32>().unwrap();
assert!(diff < 1e-6);
Source

pub fn max_score_bytes(self, bytes: usize) -> Self

Caps every score-sized intermediate at bytes (positive).

Trait Implementations§

Source§

impl<'a> Clone for AttentionOptions<'a>

Source§

fn clone(&self) -> Self

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl<'a> Copy for AttentionOptions<'a>

Source§

impl<'a> Debug for AttentionOptions<'a>

Source§

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

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

impl<'a> Default for AttentionOptions<'a>

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'a> From<AttentionMask<'a>> for AttentionOptions<'a>

Source§

fn from(mask: AttentionMask<'a>) -> Self

Options with this mask and nothing else.

Auto Trait Implementations§

§

impl<'a> !RefUnwindSafe for AttentionOptions<'a>

§

impl<'a> !UnwindSafe for AttentionOptions<'a>

§

impl<'a> Freeze for AttentionOptions<'a>

§

impl<'a> Send for AttentionOptions<'a>

§

impl<'a> Sync for AttentionOptions<'a>

§

impl<'a> Unpin for AttentionOptions<'a>

§

impl<'a> UnsafeUnpin for AttentionOptions<'a>

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> AutoreleaseSafe for T
where T: ?Sized,

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<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

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

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self> ⓘ

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self> ⓘ
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

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

Source§

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>,

Source§

type Error = !

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

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

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

Source§

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.