Skip to main content

GraphAttentionClassifier

Struct GraphAttentionClassifier 

Source
pub struct GraphAttentionClassifier {
    pub embedding: Vec<f32>,
    pub w_q: Vec<f32>,
    pub w_k: Vec<f32>,
    pub w_v: Vec<f32>,
    pub edge_weights_raw: Vec<f32>,
    pub mlp: MlpClassifier,
    /* private fields */
}
Expand description

Single-head graph-attention classifier.

Each context token attends to itself plus its pre-computed geometric neighbors. The attended representation of the last context token is passed through a two-layer MLP head for classification.

Fields§

§embedding: Vec<f32>

Token embedding table, row-major [vocab_size, embed_dim].

§w_q: Vec<f32>

Query projection, row-major [embed_dim, embed_dim].

§w_k: Vec<f32>

Key projection, row-major [embed_dim, embed_dim].

§w_v: Vec<f32>

Value projection, row-major [embed_dim, embed_dim].

§edge_weights_raw: Vec<f32>

Learnable raw edge weights, row-major [vocab_size, num_neighbors]. The actual positive weight is exp(raw) so gradients are well behaved and weights stay non-negative. When plasticity is disabled this vector is empty.

§mlp: MlpClassifier

MLP head: embed_dim -> hidden_dim -> output_dim.

Implementations§

Source§

impl GraphAttentionClassifier

Source

pub fn new( vocab_size: usize, embed_dim: usize, hidden_dim: usize, output_dim: usize, num_neighbors: usize, seed: u32, rope_theta: Option<f32>, plasticity: bool, ) -> Self

Create a fresh classifier with Xavier-like random weights.

When plasticity is true, a learnable positive weight is attached to each geometric neighbor edge and updated during training.

Source

pub fn set_geometric_attention_only(&mut self, enabled: bool)

Enable or disable geometric-only attention.

In geometric-only mode each context token attends only to itself and its pre-computed geometric neighbors, removing the O(L^2) full self-attention term. This is intended as a fast CPU-native variant for long contexts.

Source

pub fn forward(&self, token_ids: &[u32], neighbors: &[&[usize]]) -> Vec<f32>

Forward pass for a single example.

token_ids has length n_context. neighbors[j] lists the dense neighbor indices for token_ids[j] (may be empty). The token itself is implicitly included as the first attended element.

Source

pub fn forward_hybrid( &self, token_ids: &[u32], neighbors: &[&[usize]], ) -> Vec<f32>

Forward pass using the full hybrid attention graph.

Source

pub fn forward_geometric_only( &self, token_ids: &[u32], neighbors: &[&[usize]], ) -> Vec<f32>

Forward pass using only geometric (neighbor) attention.

Source

pub fn forward_hidden( &self, token_ids: &[u32], neighbors: &[&[usize]], ) -> (Vec<f32>, Vec<f32>)

Forward pass that also returns the MLP hidden state for the last context position. This is useful for diagnosing the FFN head, e.g. low-rank approximation experiments that want real hidden-state activations rather than random probes.

Source

pub fn forward_hidden_only( &self, token_ids: &[u32], neighbors: &[&[usize]], ) -> Vec<f32>

Hidden state for the last context position without the output projection. Used by confidence-gated FFN routing to decide whether to compute the cheap low-rank logits or the full dense logits.

Source

pub fn loss( &self, token_ids: &[u32], neighbors: &[&[usize]], target: usize, ) -> f32

Cross-entropy loss for a single example.

Source

pub fn predict(&self, token_ids: &[u32], neighbors: &[&[usize]]) -> usize

Predicted class for a single example.

Source

pub fn attend_mixed( &self, token_ids: &[u32], neighbors: &[&[usize]], per_position_geometric: &[bool], ) -> Vec<Vec<f32>>

Compute attended hidden states using a per-position attention mode.

per_position_geometric[j] selects geometric-only attention for position j; otherwise the full context is attended. This is the primitive used to measure approximation divergence and to prototype residual correction tie-breaks.

Source

pub fn forward_mixed( &self, token_ids: &[u32], neighbors: &[&[usize]], per_position_geometric: &[bool], ) -> Vec<f32>

Forward pass with a per-position mixed attention mode.

Source

pub fn forward_tiebreak( &self, token_ids: &[u32], neighbors: &[&[usize]], config: &TiebreakConfig, ) -> Vec<f32>

Tiebreak-corrected forward pass.

Runs cheap geometric-only attention, identifies uncertain positions by inspecting the geometric attention distribution, then recomputes those positions with full hybrid attention via Self::forward_mixed. This is the prototype residual-correction stage for the approximate transformer math experiment.

Source

pub fn tiebreak_mask( &self, token_ids: &[u32], neighbors: &[&[usize]], config: &TiebreakConfig, ) -> Vec<bool>

Build the per-position mixed-attention mask used by Self::forward_tiebreak and Self::forward_mixed.

A returned value of true means “keep the cheap geometric-only result for this position”; false means “recompute this position with full hybrid attention”. This matches the semantics expected by Self::forward_mixed.

Source

pub fn forward_batch( &self, token_ids: &[u32], neighbors: &[Vec<Vec<usize>>], batch_size: usize, ) -> Vec<f32>

Forward pass for a batch.

Source

pub fn backward_batch( &self, token_ids: &[u32], neighbors: &[Vec<Vec<usize>>], targets: &[usize], batch_size: usize, ) -> (GraphAttentionGradients, f32)

Back-propagation for a batch.

neighbors[b][j] is the neighbor list for the j-th context token of example b. Returns gradients averaged over the batch and the average cross-entropy loss.

Source

pub fn apply_sgd(&mut self, grad: &GraphAttentionGradients, lr: f32)

Apply a single SGD step using the supplied gradients.

Source

pub fn flatten_params(&self) -> Vec<f32>

Flatten all learnable parameters into one vector, ordered [embedding, w_q, w_k, w_v, edge_weights_raw, mlp].

Source

pub fn load_flat_params(&mut self, params: &[f32])

Restore parameters from a flat vector produced by Self::flatten_params.

Trait Implementations§

Source§

impl Clone for GraphAttentionClassifier

Source§

fn clone(&self) -> GraphAttentionClassifier

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 Debug for GraphAttentionClassifier

Source§

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

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Allocation for T
where T: RefUnwindSafe + Send + Sync,

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

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
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 = 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>,

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

impl<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

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