Skip to main content

EncoderDecoder

oa::ml::nn

Struct EncoderDecoder 

Source
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

Source

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.

Source

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>

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

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.

Source

pub const fn source_vocab_size(&self) -> usize

Source

pub const fn target_vocab_size(&self) -> usize

Source

pub const fn source_context_length(&self) -> usize

Source

pub const fn target_context_length(&self) -> usize

Source

pub const fn model_width(&self) -> usize

Source

pub const fn hidden_width(&self) -> usize

Source

pub fn num_layers(&self) -> usize

Source

pub const fn num_heads(&self) -> usize

Source

pub fn encoder_block(&self, index: usize) -> Option<&TransformerBlock>

Return one encoder block by layer index.

Source

pub fn decoder_block(&self, index: usize) -> Option<&EncoderDecoderBlock>

Return one decoder block by layer index.

Trait Implementations§

Source§

impl Module for EncoderDecoder

Source§

fn forward(&self, input: &Matrix) -> Result<Matrix>

Evaluate one Matrix input without submitting or waiting.
Source§

fn registry(&self) -> &ModuleRegistry

Return this module’s structural registry.
Source§

fn parameters(&self) -> Vec<Parameter>

Return direct parameters only.
Source§

fn named_parameters(&self) -> Vec<NamedParameter>

Return direct parameters with local registration names.
Source§

fn buffers(&self) -> Vec<Matrix>

Return direct non-trainable buffers only.
Source§

fn named_buffers(&self) -> Vec<NamedBuffer>

Return direct buffers with local registration names and persistence flags.
Source§

fn all_parameters(&self) -> Result<Vec<Parameter>>

Return every parameter exactly once in deterministic depth-first order. Read more
Source§

fn all_named_parameters(&self) -> Result<Vec<NamedParameter>>

Return every parameter with its registration-derived dotted path. Read more
Source§

fn all_named_buffers(&self) -> Result<Vec<NamedBuffer>>

Return all direct and recursive buffers in deterministic depth-first order. Read more
Source§

fn all_named_state_u32(&self) -> Result<Vec<NamedStateU32>>

Return all direct and recursive persistent u32 scalar state in deterministic depth-first order. Read more
Source§

fn all_named_rng_states(&self) -> Result<Vec<NamedRngState>>

Return all direct and recursive stochastic RNG streams in deterministic depth-first order. Read more
Source§

fn num_parameters(&self) -> Result<usize>

Return the total number of trainable scalar values. Read more
Source§

fn train(&self, training: bool)

Set train/eval mode recursively.
Source§

fn eval(&self)

Enter evaluation mode recursively.
Source§

fn is_training(&self) -> bool

Return this module’s current train/eval mode.
Source§

fn scoped_eval(&self) -> ScopedEval<'_>

Enter evaluation mode until the returned guard is dropped.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<ST, DT> CastableFrom<ST, Initialized, Initialized> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<ST, DT> CastableFrom<ST, Uninit, Uninit> for DT
where ST: ?Sized, DT: ?Sized,

Source§

impl<S, T> Duplex<S> for T
where T: FromSample<S> + ToSample<S>,

Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<S> FromSample<S> for S

Source§

fn from_sample_(s: S) -> S

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<F, T> IntoSample<T> for F
where T: FromSample<F>,

Source§

fn into_sample(self) -> T

Source§

impl<T> Read<Exclusive, BecauseExclusive> for T
where T: ?Sized,

Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
Source§

impl<T, U> ToSample<U> for T
where U: FromSample<T>,

Source§

fn to_sample_(self) -> U

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.