programdemos/diffusion/diffusion.xtl
A learned denoiser: pictures from noise, step by step. The pictures are 10 x 10, one of three shapes (a disk, a hollow square, a cross) at some place, a pixel -1 off or 1 on. A small convolutional network (train/) was trained to see the clean picture in a noisy one at ten levels of noise. Sampling runs it backward: from a picture of pure noise, each step predicts the clean picture, infers the noise that must have been added, and steps down one level (DDIM, no new noise), so the picture that comes out depends only on the noise that went in.
The schedule
ab : Float
The kept share of the picture at each level 0 to 10 (1.0 clean, 0.001 all noise); the noisy picture is sqrt ab x + sqrt (1 - ab) e.
ab ← 0.001 m̲ax (c̲os ((p̲i @) ÷ 2.0) × (f̲loat o̲ffsets 11) ÷ 10.0) ^ 2.0
ra : Float
The roots the steps mix with, taken once: of the kept share and of the noise's.
ra ← ab ^ 0.5
The denoiser, read from data/ (just diffusion-data)
kb : Float
The 8 filters, 3 x 3 each as a row of 9 with its bias last (as Conv takes them).
kb ← 8 10 r̲eshape n̲umbers ⎕N̲GET "data/filters.txt"
wb1 : Float
The dense layers, each with its bias as one more row, as NN's d_ense takes them: the first over the 512 features, and the level's own weights wh (a row per level) added to it.
wb1 ← 513 256 r̲eshape (n̲umbers ⎕N̲GET "data/w1.txt") c̲at n̲umbers ⎕N̲GET "data/b1.txt"
wb2 : Float
wb2 ← 257 100 r̲eshape (n̲umbers ⎕N̲GET "data/w2.txt") c̲at n̲umbers ⎕N̲GET "data/b2.txt"
ᵘn̲et : Float -> Float
The network over the joined input J (a row per picture: its 100 pixels, then the level as a one-hot): the picture's 3 x 3 windows through the 8 filters (valid, so 8 maps of 8 x 8) with ReLU, the 512 features through a dense layer with the level's weights added and ReLU, and the output layer; the clean pictures it sees in -1 .. 1. One function of arrays, the part a GPU takes (docs/gpu-asks.md).
ᵘn̲et ← { J → X ← ((t̲ally J) c̲at 10 10) r̲eshape 100 t̲ake₂ J V ← ((t̲ally J) c̲at 512) r̲eshape ᵃᶜᵗr̲elu (9 t̲ake₂ kb, r̲avel -1 t̲ake₂ kb) ᶜᵛc̲onv X A ← ᵃᶜᵗr̲elu (V ⁿⁿd̲ense wb1) + (-10 t̲ake₂ J) '+ '× i̲nner wh (2.0 × ᵃᶜᵗs̲igmoid A ⁿⁿd̲ense wb2) − 1.0 }
ᵘc̲lean : Int -> Float -> Float
The clean pictures the denoiser sees in the noisy pictures X (a row each) at level t: the level joins the pixels as a one-hot.
ᵘc̲lean ← { t X → ᵘn̲et X c̲at₂ 10 ⁿⁿo̲neHot (t̲ally X) r̲eshape t }
ᵘs̲tep : (Float, Int) -> (Float, Int)
One step down from level t: the clean pictures C it sees, the noise that would make X of them, and the pictures one level cleaner.
ᵘs̲tep ← { (X, t) → C ← t ᵘc̲lean X (((t s̲elect ra) × C) + (t s̲elect rb) × (X − ((1 + t) s̲elect ra) × C) ÷ (1 + t) s̲elect rb, t − 1) }
ᵘs̲ample : Float -> Float
Ten steps from the pictures of noise E: at level 0 what is left is the last clean picture seen.
ᵘs̲ample ← { E → (X, _) ← 10 'ᵘs̲tep p̲ower (E, 10)◆ X }
Pictures from noise
ᵘg̲auss : Int -> Float
n Gaussian numbers by Box-Muller from r_oll!
ᵘg̲auss ← { n → u1 ← (1.0 + f̲loat r̲oll! n r̲eshape 999999) ÷ 1000000.0 u2 ← (f̲loat r̲oll! n r̲eshape 1000000) ÷ 1000000.0 ((n̲eg 2.0 × l̲og u1) ^ 0.5) × c̲os (2.0 × p̲i @) × u2 }
e : Float
e ← 1 100 r̲eshape n̲umbers ⎕N̲GET "data/noise.txt"