std1.0.0-beta-7 SafeC docs

std :: ml::transformer

Struct std::DiTBlock

#include <std/ml/transformer.h> source

struct DiTBlock {
    &FloatTensor Wq;
    &FloatTensor Wk;
    &FloatTensor Wv;
    &FloatTensor Wo;
    &FloatTensor W1;
    &FloatTensor b1;
    &FloatTensor W2;
    &FloatTensor b2;
    &FloatTensor WadaLN;
    unsigned long numHeads;
}

Each block computes 6 modulation vectors (shift1,scale1,gate1, shift2,scale2,gate2) from a conditioning vector c via one linear layer, then runs a pre-LN self-attention sublayer and a pre-LN FFN sublayer, each modulated by its half of the 6 and gated additively into the residual stream (the "adaLN-Zero" block from the DiT paper, minus the SiLU the paper puts before WadaLN and minus the zero-init convention — both orthogonal to the block's wiring, which is what this implements and verifies):

mod = c @ WadaLN                                    [1, 6*dModel]
h = LN(x); h = h*(1+scale1)+shift1
x = x + gate1 * (MHA(h@Wq,h@Wk,h@Wv,heads) @ Wo)
h2 = LN(x); h2 = h2*(1+scale2)+shift2
x = x + gate2 * (relu(h2@W1+b1)@W2+b2)

Fields

[dModel,dModel]

[dModel,dHidden], [1,dHidden]

[dHidden,dModel], [1,dModel]

[dModelCond, 6*dModel]

§unsigned long numHeadssource

Methods

§&FloatTensor forward(const &FloatTensor x, const &FloatTensor c) constsource · definition
§void collect_params(&Vec<&FloatTensor> out) constsource · definition

Parameterized (module.h): every weight tensor.

§void collect_state(&Vec<&FloatTensor> out) constsource · definition
§void drop()source · definition
§void free() consumesource · definition

= drop()

§static struct DiTBlock new( unsigned long dModel, unsigned long dHidden, unsigned long dModelCond, unsigned long numHeads, )source · definition