pub struct TransformerBlock { /* private fields */ }Expand description
One pre-normalized causal Transformer block.
Implementations§
Source§impl TransformerBlock
impl TransformerBlock
pub fn new_with_seed( model_width: usize, hidden_width: usize, sequence_length: usize, num_heads: usize, epsilon: f32, seed: u64, ) -> Result<Self>
Sourcepub fn new_with_seed_on(
runtime: &Runtime,
model_width: usize,
hidden_width: usize,
sequence_length: usize,
num_heads: usize,
epsilon: f32,
seed: u64,
) -> Result<Self>
pub fn new_with_seed_on( runtime: &Runtime, model_width: usize, hidden_width: usize, sequence_length: usize, num_heads: usize, epsilon: f32, seed: u64, ) -> Result<Self>
Construct the canonical pre-norm attention and GELU feed-forward block.
§Errors
Returns an error for invalid dimensions or failed child construction.
Sourcepub fn new_with_seed_conditioned(
model_width: usize,
hidden_width: usize,
sequence_length: usize,
num_heads: usize,
condition_dim: usize,
epsilon: f32,
seed: u64,
) -> Result<Self>
pub fn new_with_seed_conditioned( model_width: usize, hidden_width: usize, sequence_length: usize, num_heads: usize, condition_dim: usize, epsilon: f32, seed: u64, ) -> Result<Self>
Construct a dense block with zero-initialized adaptive conditioning.
The complete child tree is registered before the block is returned. This
is the Rust replacement for donor enableAdaptiveConditioning, whose
post-construction registry mutation would violate ModuleRegistry’s
fixed structural-ownership contract.
§Errors
Returns an error for invalid block dimensions, a zero condition width, shape overflow, allocation failure, or child registration failure.
pub fn new_with_seed_conditioned_on( runtime: &Runtime, model_width: usize, hidden_width: usize, sequence_length: usize, num_heads: usize, condition_dim: usize, epsilon: f32, seed: u64, ) -> Result<Self>
Sourcepub fn new_with_seed_moe(
model_width: usize,
expert_hidden_width: usize,
sequence_length: usize,
num_heads: usize,
num_experts: usize,
experts_per_token: usize,
epsilon: f32,
seed: u64,
) -> Result<Self>
pub fn new_with_seed_moe( model_width: usize, expert_hidden_width: usize, sequence_length: usize, num_heads: usize, num_experts: usize, experts_per_token: usize, epsilon: f32, seed: u64, ) -> Result<Self>
Construct the donor pre-norm attention block with a sparse MoE residual.
The MoE owns its RMSNorm and residual connection, so this block does not add a second feed-forward normalization or residual around it.
§Errors
Returns an error for invalid attention or MoE dimensions, failed child construction, or registration failure.
pub fn new_with_seed_moe_on( runtime: &Runtime, model_width: usize, expert_hidden_width: usize, sequence_length: usize, num_heads: usize, num_experts: usize, experts_per_token: usize, epsilon: f32, seed: u64, ) -> Result<Self>
Sourcepub fn new_with_seed_moe_conditioned(
model_width: usize,
expert_hidden_width: usize,
sequence_length: usize,
num_heads: usize,
num_experts: usize,
experts_per_token: usize,
condition_dim: usize,
epsilon: f32,
seed: u64,
) -> Result<Self>
pub fn new_with_seed_moe_conditioned( model_width: usize, expert_hidden_width: usize, sequence_length: usize, num_heads: usize, num_experts: usize, experts_per_token: usize, condition_dim: usize, epsilon: f32, seed: u64, ) -> Result<Self>
Construct a sparse-MoE block with zero-initialized adaptive conditioning.
§Errors
Returns an error for invalid attention, expert, or conditioning geometry, allocation failure, or child registration failure.
pub fn new_with_seed_moe_conditioned_on( runtime: &Runtime, model_width: usize, expert_hidden_width: usize, sequence_length: usize, num_heads: usize, num_experts: usize, experts_per_token: usize, condition_dim: usize, epsilon: f32, seed: u64, ) -> Result<Self>
Sourcepub fn new_with_seed_gqa_on(
runtime: &Runtime,
model_width: usize,
hidden_width: usize,
sequence_length: usize,
num_heads: usize,
num_kv_heads: usize,
epsilon: f32,
seed: u64,
) -> Result<Self>
pub fn new_with_seed_gqa_on( runtime: &Runtime, model_width: usize, hidden_width: usize, sequence_length: usize, num_heads: usize, num_kv_heads: usize, epsilon: f32, seed: u64, ) -> Result<Self>
Construct a dense block with grouped-query attention.
num_kv_heads must divide num_heads and selects the shared
key/value head count; 1 is multi-query attention.
§Errors
Returns an error for invalid block or grouping dimensions, or failed child construction.
Sourcepub fn forward(&self, input: &Matrix) -> Result<Matrix>
pub fn forward(&self, input: &Matrix) -> Result<Matrix>
Apply pre-norm attention/residual followed by pre-norm GELU FFN/residual.
§Errors
Returns an error unless input is F32 [B*S, D] or child recording fails.
Sourcepub fn forward_masked(
&self,
input: &Matrix,
additive_mask: &Matrix,
) -> Result<Matrix>
pub fn forward_masked( &self, input: &Matrix, additive_mask: &Matrix, ) -> Result<Matrix>
Apply this block with an explicit bidirectional additive attention mask.
§Errors
Returns an error unless the mask is F32 [B*H*S, S] for the current
sequence geometry, or a child operation fails.
Sourcepub fn forward_conditioned(
&self,
input: &Matrix,
condition: &Matrix,
) -> Result<Matrix>
pub fn forward_conditioned( &self, input: &Matrix, condition: &Matrix, ) -> Result<Matrix>
Apply donor AdaLN-Zero conditioning without an explicit attention mask.
§Errors
Returns an error unless this block was constructed with adaptive
conditioning, input is F32 [B*S, D], condition is same-runtime F32
[B, C], or a child operation fails.
Sourcepub fn forward_conditioned_masked(
&self,
input: &Matrix,
condition: &Matrix,
additive_mask: &Matrix,
) -> Result<Matrix>
pub fn forward_conditioned_masked( &self, input: &Matrix, condition: &Matrix, additive_mask: &Matrix, ) -> Result<Matrix>
Apply donor AdaLN-Zero conditioning with an additive attention mask.
§Errors
Returns an error unless the conditioned input contract and the attention mask contract are both satisfied, or a child operation fails.
Sourcepub fn step(&self, input: &Matrix, cache: &mut AttentionCache) -> Result<Matrix>
pub fn step(&self, input: &Matrix, cache: &mut AttentionCache) -> Result<Matrix>
Advance one autoregressive position through the block against cache.
input must be F32 [batch, model_width] for the cache’s batch size.
Runs the same pre-norm → attention → residual → FFN → residual
sequence as forward_impl for one position, with
MultiHeadAttention::step supplying the cached key/value state.
Inference-only per MultiHeadAttention::step: an active
GradientTape is rejected there, and conditioned blocks are not
admitted for incremental decode.
§Errors
Returns an error for a conditioned block, an active tape, a foreign or mismatched cache, or a failed child operation.
pub const fn model_width(&self) -> usize
pub fn sequence_length(&self) -> usize
pub const fn num_heads(&self) -> usize
Sourcepub fn is_moe(&self) -> bool
pub fn is_moe(&self) -> bool
Return whether this block uses routed experts instead of a dense FFN.
Sourcepub fn is_adaptively_conditioned(&self) -> bool
pub fn is_adaptively_conditioned(&self) -> bool
Return whether this block owns a registered AdaLN-Zero projection.
Sourcepub const fn condition_dim(&self) -> Option<usize>
pub const fn condition_dim(&self) -> Option<usize>
Return the accepted condition width for an adaptive block.
Sourcepub fn set_attention_mode(&self, mode: AttentionMode)
pub fn set_attention_mode(&self, mode: AttentionMode)
Select causal or bidirectional attention for this block.
Sourcepub fn attention_mode(&self) -> AttentionMode
pub fn attention_mode(&self) -> AttentionMode
Return this block’s attention visibility contract.
Trait Implementations§
Source§impl Module for TransformerBlock
impl Module for TransformerBlock
Source§fn forward(&self, input: &Matrix) -> Result<Matrix>
fn forward(&self, input: &Matrix) -> Result<Matrix>
Source§fn registry(&self) -> &ModuleRegistry
fn registry(&self) -> &ModuleRegistry
Source§fn parameters(&self) -> Vec<Parameter>
fn parameters(&self) -> Vec<Parameter>
Source§fn named_parameters(&self) -> Vec<NamedParameter>
fn named_parameters(&self) -> Vec<NamedParameter>
Source§fn named_buffers(&self) -> Vec<NamedBuffer>
fn named_buffers(&self) -> Vec<NamedBuffer>
Source§fn all_parameters(&self) -> Result<Vec<Parameter>>
fn all_parameters(&self) -> Result<Vec<Parameter>>
Source§fn all_named_parameters(&self) -> Result<Vec<NamedParameter>>
fn all_named_parameters(&self) -> Result<Vec<NamedParameter>>
Source§fn all_named_buffers(&self) -> Result<Vec<NamedBuffer>>
fn all_named_buffers(&self) -> Result<Vec<NamedBuffer>>
Source§fn all_named_state_u32(&self) -> Result<Vec<NamedStateU32>>
fn all_named_state_u32(&self) -> Result<Vec<NamedStateU32>>
u32 scalar state in
deterministic depth-first order. Read more