pub struct Transformer { /* private fields */ }Expand description
Ready-to-train causal Transformer language model.
This is the idiomatic Rust spelling of donor oa::NnTransformer: token and
position embeddings, registered Transformer blocks, final normalization,
and a vocabulary projection.
Implementations§
Source§impl Transformer
impl Transformer
Sourcepub fn new_with_seed(
vocab_size: usize,
context_length: usize,
model_width: usize,
hidden_width: usize,
num_layers: usize,
num_heads: usize,
epsilon: f32,
seed: u64,
) -> Result<Self>
pub fn new_with_seed( vocab_size: usize, context_length: usize, model_width: usize, hidden_width: usize, num_layers: usize, num_heads: usize, epsilon: f32, seed: u64, ) -> Result<Self>
Construct a deterministic causal language model.
§Errors
Returns an error unless all dimensions are positive, the model width is divisible by the head count, positions fit U32, and every child can be constructed and registered.
pub fn new_with_seed_on( runtime: &Runtime, vocab_size: usize, context_length: usize, model_width: usize, hidden_width: usize, num_layers: usize, num_heads: usize, epsilon: f32, seed: u64, ) -> Result<Self>
Sourcepub fn new_with_seed_moe(
vocab_size: usize,
context_length: usize,
model_width: usize,
expert_hidden_width: usize,
num_layers: usize,
num_heads: usize,
num_experts: usize,
experts_per_token: usize,
epsilon: f32,
seed: u64,
) -> Result<Self>
pub fn new_with_seed_moe( vocab_size: usize, context_length: usize, model_width: usize, expert_hidden_width: usize, num_layers: usize, num_heads: usize, num_experts: usize, experts_per_token: usize, epsilon: f32, seed: u64, ) -> Result<Self>
Construct a deterministic causal language model with MoE Transformer blocks.
Every block preserves the donor topology: pre-norm attention and residual, followed by one MoE module that owns its own RMSNorm and residual.
§Errors
Returns an error unless all dimensions are valid or when child allocation and registration fail.
pub fn new_with_seed_moe_on( runtime: &Runtime, vocab_size: usize, context_length: usize, model_width: usize, expert_hidden_width: usize, num_layers: usize, num_heads: usize, num_experts: usize, experts_per_token: usize, epsilon: f32, seed: u64, ) -> Result<Self>
Sourcepub fn new_with_seed_gqa(
vocab_size: usize,
context_length: usize,
model_width: usize,
hidden_width: usize,
num_layers: usize,
num_heads: usize,
num_kv_heads: usize,
epsilon: f32,
seed: u64,
) -> Result<Self>
pub fn new_with_seed_gqa( vocab_size: usize, context_length: usize, model_width: usize, hidden_width: usize, num_layers: usize, num_heads: usize, num_kv_heads: usize, epsilon: f32, seed: u64, ) -> Result<Self>
Construct a deterministic causal language model with grouped-query attention in every block.
num_kv_heads must divide num_heads and selects the shared
key/value head count; 1 is multi-query attention.
§Errors
Returns an error under the Self::new_with_seed dimension contract,
or when num_kv_heads is zero or does not divide num_heads.
pub fn new_with_seed_gqa_on( runtime: &Runtime, vocab_size: usize, context_length: usize, model_width: usize, hidden_width: usize, num_layers: usize, num_heads: usize, num_kv_heads: usize, epsilon: f32, seed: u64, ) -> Result<Self>
Sourcepub fn forward(&self, tokens: &Matrix) -> Result<Matrix>
pub fn forward(&self, tokens: &Matrix) -> Result<Matrix>
Evaluate all-position vocabulary logits for U8 or U32 tokens [B, S].
§Errors
Returns an error unless S equals the configured context length and the
input is nonempty U8/U32, or a child operation fails.
pub const fn vocab_size(&self) -> usize
pub const fn context_length(&self) -> usize
pub const fn model_width(&self) -> usize
pub fn num_layers(&self) -> usize
pub const fn num_heads(&self) -> usize
Sourcepub const fn num_kv_heads(&self) -> usize
pub const fn num_kv_heads(&self) -> usize
Return the key/value head count each query-head group shares.
Sourcepub fn block(&self, index: usize) -> Option<&TransformerBlock>
pub fn block(&self, index: usize) -> Option<&TransformerBlock>
Return one Transformer block by layer index.
Sourcepub fn new_cache(&self, batch_size: usize) -> Result<TransformerCache>
pub fn new_cache(&self, batch_size: usize) -> Result<TransformerCache>
Allocate per-layer attention caches for one autoregressive decode session.
§Errors
Returns an error when batch_size is zero.
Sourcepub fn step(
&self,
token: &Matrix,
cache: &mut TransformerCache,
) -> Result<Matrix>
pub fn step( &self, token: &Matrix, cache: &mut TransformerCache, ) -> Result<Matrix>
Advance one autoregressive position against cache.
token must be a [batch] U8 or U32 matrix for the cache’s batch
size. The step embeds the token and cache.position, runs every
block’s incremental TransformerBlock::step, and returns [batch, vocab_size] logits — the incremental equivalent of taking the last
position of forward. Decode is bounded by context_length and is
inference-only per MultiHeadAttention::step.
§Errors
Returns an error for an exhausted context window, mismatched token shape or dtype, a foreign cache count, or a failed child operation.
Trait Implementations§
Source§impl Module for Transformer
impl Module for Transformer
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