Skip to main content

TransformerBlock

Struct TransformerBlock 

Source
pub struct TransformerBlock {
    pub attn_wq: Tensor,
    pub attn_wk: Tensor,
    pub attn_wv: Tensor,
    pub attn_wo: Tensor,
    pub ff_w1: Linear,
    pub ff_w2: Linear,
    pub ln1: LayerNorm,
    pub ln2: LayerNorm,
    pub d_model: usize,
    pub num_heads: usize,
}
Expand description

Transformer encoder block.

Fields§

§attn_wq: Tensor

Query projection weight.

§attn_wk: Tensor

Key projection weight.

§attn_wv: Tensor

Value projection weight.

§attn_wo: Tensor

Output projection weight.

§ff_w1: Linear

First feed-forward layer.

§ff_w2: Linear

Second feed-forward layer.

§ln1: LayerNorm

First layer norm (pre-attention).

§ln2: LayerNorm

Second layer norm (pre-FFN).

§d_model: usize

Model width.

§num_heads: usize

Number of attention heads.

Implementations§

Source§

impl TransformerBlock

Source

pub fn new(d_model: usize, num_heads: usize, d_ff: usize) -> Self

Create a transformer block with random-initialized parameters.

Source

pub fn forward(&self, x: &Tensor, mask: Option<&Tensor>) -> MathResult<Tensor>

Forward pass with optional attention mask.

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<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

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<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.