pub struct EncoderDecoderBlock { /* private fields */ }Expand description
One pre-normalized decoder block of an encoder-decoder EncoderDecoder.
Applies causal self-attention, bidirectional cross-attention over the encoder output, and a dense GELU feed-forward stage, each behind a pre-norm residual. The cross-attention stage projects its keys and values from the encoder output on every call, so the same block also serves training and uncached evaluation.
Implementations§
Source§impl EncoderDecoderBlock
impl EncoderDecoderBlock
Sourcepub fn new_with_seed(
model_width: usize,
hidden_width: usize,
sequence_length: usize,
num_heads: usize,
epsilon: f32,
seed: u64,
) -> Result<Self>
pub fn new_with_seed( model_width: usize, hidden_width: usize, sequence_length: usize, num_heads: usize, epsilon: f32, seed: u64, ) -> Result<Self>
Construct the canonical decoder block with causal self-attention and bidirectional cross-attention.
§Errors
Returns an error for invalid dimensions or failed child construction.
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>
Sourcepub fn forward(&self, input: &Matrix, encoder_output: &Matrix) -> Result<Matrix>
pub fn forward(&self, input: &Matrix, encoder_output: &Matrix) -> Result<Matrix>
Apply pre-norm self-attention, cross-attention over encoder_output,
and GELU feed-forward stages behind residuals.
input is F32 [B*S_tgt, D]; encoder_output is F32 [B*S_src, D]
on the same runtime for the same batch.
§Errors
Returns an error unless both matrices satisfy their geometry and dtype contracts, or a child operation fails.
Sourcepub fn forward_masked(
&self,
input: &Matrix,
encoder_output: &Matrix,
cross_mask: &Matrix,
) -> Result<Matrix>
pub fn forward_masked( &self, input: &Matrix, encoder_output: &Matrix, cross_mask: &Matrix, ) -> Result<Matrix>
Evaluate the block with an additive cross-attention key-padding mask.
cross_mask is the additive [B*H*S_target, S_source] mask broadcast
into the cross-attention scores, built with 0 at visible source
positions and a large negative value at padding. Decoder self-attention
stays causal-only: under the right-padding contract padding positions
follow valid ones, so causal visibility already excludes them.
§Errors
Returns an error under the Self::forward contract or when the mask
does not match the cross-attention score geometry.
pub const fn model_width(&self) -> usize
pub fn sequence_length(&self) -> usize
pub const fn num_heads(&self) -> usize
Trait Implementations§
Source§impl Module for EncoderDecoderBlock
impl Module for EncoderDecoderBlock
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