Skip to Content
OchreML
Transformer & LoRA

SwiGLU & FeedForward

Transformer MLP blocks implementing SwiGLU and standard GELU FeedForward networks in Rust.

ochreml.nn provides both the gated SwiGLU feed-forward block used in LLaMA-style LLMs (down(silu(gate(x)) * up(x))) and the standard Transformer FeedForward block (fc2(gelu(fc1(x)))).

Import & Signatures

import ochreml.nn as nn

swiglu = nn.SwiGLU(
    d_model,
    hidden_dim,
    bias=False,
    dtype="float32",
    device=None,
    seed=42,
)

ffn = nn.FeedForward(
    d_model,
    d_ff,
    bias=True,
    dtype="float32",
    device=None,
    seed=42,
)

Parameters

ClassParameterTypeDefaultDescription
SwiGLUd_model, hidden_dimint, intrequiredInput/output width and gated intermediate width.
SwiGLUbiasboolFalseInclude bias terms in gate, up, and down projections.
FeedForwardd_model, d_ffint, intrequiredInput/output width and inner GELU projection width.
FeedForwardbiasboolTrueInclude bias terms in fc1 and fc2.

Usage Example

import ochreml.nn as nn

x = nn.Tensor.randn([2, 16, 64])
y_swiglu = nn.SwiGLU(d_model=64, hidden_dim=192)(x)
y_ffn = nn.FeedForward(d_model=64, d_ff=256)(x)

On this page