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: MlpClassifierMLP head: embed_dim -> hidden_dim -> output_dim.
Implementations§
Source§impl GraphAttentionClassifier
impl GraphAttentionClassifier
Sourcepub 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
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.
Sourcepub fn set_geometric_attention_only(&mut self, enabled: bool)
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.
Sourcepub fn forward(&self, token_ids: &[u32], neighbors: &[&[usize]]) -> Vec<f32>
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.
Sourcepub fn forward_hybrid(
&self,
token_ids: &[u32],
neighbors: &[&[usize]],
) -> Vec<f32>
pub fn forward_hybrid( &self, token_ids: &[u32], neighbors: &[&[usize]], ) -> Vec<f32>
Forward pass using the full hybrid attention graph.
Sourcepub fn forward_geometric_only(
&self,
token_ids: &[u32],
neighbors: &[&[usize]],
) -> Vec<f32>
pub fn forward_geometric_only( &self, token_ids: &[u32], neighbors: &[&[usize]], ) -> Vec<f32>
Forward pass using only geometric (neighbor) attention.
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.
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.
Sourcepub fn loss(
&self,
token_ids: &[u32],
neighbors: &[&[usize]],
target: usize,
) -> f32
pub fn loss( &self, token_ids: &[u32], neighbors: &[&[usize]], target: usize, ) -> f32
Cross-entropy loss for a single example.
Sourcepub fn predict(&self, token_ids: &[u32], neighbors: &[&[usize]]) -> usize
pub fn predict(&self, token_ids: &[u32], neighbors: &[&[usize]]) -> usize
Predicted class for a single example.
Sourcepub fn attend_mixed(
&self,
token_ids: &[u32],
neighbors: &[&[usize]],
per_position_geometric: &[bool],
) -> Vec<Vec<f32>>
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.
Sourcepub fn forward_mixed(
&self,
token_ids: &[u32],
neighbors: &[&[usize]],
per_position_geometric: &[bool],
) -> Vec<f32>
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.
Sourcepub fn forward_tiebreak(
&self,
token_ids: &[u32],
neighbors: &[&[usize]],
config: &TiebreakConfig,
) -> Vec<f32>
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.
Sourcepub fn tiebreak_mask(
&self,
token_ids: &[u32],
neighbors: &[&[usize]],
config: &TiebreakConfig,
) -> Vec<bool>
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.
Sourcepub fn forward_batch(
&self,
token_ids: &[u32],
neighbors: &[Vec<Vec<usize>>],
batch_size: usize,
) -> Vec<f32>
pub fn forward_batch( &self, token_ids: &[u32], neighbors: &[Vec<Vec<usize>>], batch_size: usize, ) -> Vec<f32>
Forward pass for a batch.
Sourcepub fn backward_batch(
&self,
token_ids: &[u32],
neighbors: &[Vec<Vec<usize>>],
targets: &[usize],
batch_size: usize,
) -> (GraphAttentionGradients, f32)
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.
Sourcepub fn apply_sgd(&mut self, grad: &GraphAttentionGradients, lr: f32)
pub fn apply_sgd(&mut self, grad: &GraphAttentionGradients, lr: f32)
Apply a single SGD step using the supplied gradients.
Sourcepub fn flatten_params(&self) -> Vec<f32>
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].
Sourcepub fn load_flat_params(&mut self, params: &[f32])
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
impl Clone for GraphAttentionClassifier
Source§fn clone(&self) -> GraphAttentionClassifier
fn clone(&self) -> GraphAttentionClassifier
1.0.0 (const: unstable) · Source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source. Read moreAuto Trait Implementations§
impl Freeze for GraphAttentionClassifier
impl RefUnwindSafe for GraphAttentionClassifier
impl Send for GraphAttentionClassifier
impl Sync for GraphAttentionClassifier
impl Unpin for GraphAttentionClassifier
impl UnsafeUnpin for GraphAttentionClassifier
impl UnwindSafe for GraphAttentionClassifier
Blanket Implementations§
impl<T> Allocation for T
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