pub struct EncoderDecoder { /* private fields */ }Expand description
Ready-to-train encoder-decoder Transformer for sequence-to-sequence tasks.
Source and target token/position embeddings feed separate block stacks:
encoder blocks run bidirectional self-attention, decoder blocks run
causal self-attention and bidirectional cross-attention over the encoder
output. forward(source, target) returns all-position target logits.
Implementations§
Source§impl EncoderDecoder
impl EncoderDecoder
Sourcepub fn new_with_seed(
source_vocab_size: usize,
target_vocab_size: usize,
source_context_length: usize,
target_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( source_vocab_size: usize, target_vocab_size: usize, source_context_length: usize, target_context_length: usize, model_width: usize, hidden_width: usize, num_layers: usize, num_heads: usize, epsilon: f32, seed: u64, ) -> Result<Self>
Construct a deterministic encoder-decoder Transformer.
Both stacks use num_layers blocks of the same width recipe.
§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, source_vocab_size: usize, target_vocab_size: usize, source_context_length: usize, target_context_length: usize, model_width: usize, hidden_width: usize, num_layers: usize, num_heads: usize, epsilon: f32, seed: u64, ) -> Result<Self>
Sourcepub fn encode(&self, tokens: &Matrix) -> Result<Matrix>
pub fn encode(&self, tokens: &Matrix) -> Result<Matrix>
Embed source tokens tokens, a U8/U32 [batch, source_context]
matrix, through the encoder stack.
§Errors
Returns an error unless tokens is nonempty U8 or U32 [batch, source_context_length], or a child operation fails.
Sourcepub fn decode(&self, tokens: &Matrix, encoder_output: &Matrix) -> Result<Matrix>
pub fn decode(&self, tokens: &Matrix, encoder_output: &Matrix) -> Result<Matrix>
Evaluate all-position target logits for tokens, a U8/U32 [batch, target_context] matrix, against a computed encoder_output.
encoder_output must be F32 [batch*source_context_length, model_width] on the same runtime — normally the result of
Self::encode.
§Errors
Returns an error unless tokens and encoder_output satisfy their
geometry, dtype, and runtime contracts, or a child operation fails.
Sourcepub fn forward(&self, source: &Matrix, target: &Matrix) -> Result<Matrix>
pub fn forward(&self, source: &Matrix, target: &Matrix) -> Result<Matrix>
Encode source and decode target, returning [batch*target_context, target_vocab] logits.
§Errors
Returns an error under the Self::encode and Self::decode
contracts.
Sourcepub fn encode_masked(
&self,
tokens: &Matrix,
source_mask: &Matrix,
) -> Result<Matrix>
pub fn encode_masked( &self, tokens: &Matrix, source_mask: &Matrix, ) -> Result<Matrix>
Encode source with an additive encoder self-attention key-padding
mask.
source_mask is the additive [B*H*S_source, S_source] mask with 0 at
visible positions and a large negative value at padding, preventing
bidirectional encoder attention from reading padding keys.
§Errors
Returns an error under the Self::encode contract or when the mask
does not match the encoder score geometry.
Sourcepub fn decode_masked(
&self,
tokens: &Matrix,
encoder_output: &Matrix,
cross_mask: &Matrix,
) -> Result<Matrix>
pub fn decode_masked( &self, tokens: &Matrix, encoder_output: &Matrix, cross_mask: &Matrix, ) -> Result<Matrix>
Decode target against a masked-encoding output with an additive
cross-attention key-padding mask.
cross_mask is the additive [B*H*S_target, S_source] mask over
source keys. Under the right-padding contract decoder self-attention
needs no padding mask because causal visibility already excludes
trailing padding.
§Errors
Returns an error under the Self::decode contract or when the mask
does not match the cross-attention score geometry.
Sourcepub fn forward_masked(
&self,
source: &Matrix,
target: &Matrix,
source_mask: &Matrix,
cross_mask: &Matrix,
) -> Result<Matrix>
pub fn forward_masked( &self, source: &Matrix, target: &Matrix, source_mask: &Matrix, cross_mask: &Matrix, ) -> Result<Matrix>
Encode and decode with key-padding masks on both source visibility
routes, returning [batch*target_context, target_vocab] logits.
§Errors
Returns an error under the Self::encode_masked and
Self::decode_masked contracts.
pub const fn source_vocab_size(&self) -> usize
pub const fn target_vocab_size(&self) -> usize
pub const fn source_context_length(&self) -> usize
pub const fn target_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 fn encoder_block(&self, index: usize) -> Option<&TransformerBlock>
pub fn encoder_block(&self, index: usize) -> Option<&TransformerBlock>
Return one encoder block by layer index.
Sourcepub fn decoder_block(&self, index: usize) -> Option<&EncoderDecoderBlock>
pub fn decoder_block(&self, index: usize) -> Option<&EncoderDecoderBlock>
Return one decoder block by layer index.
Trait Implementations§
Source§impl Module for EncoderDecoder
impl Module for EncoderDecoder
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