Skip to content
INDIE / MACHINE
MATHLESSON 4 OF 67 MIN

The gate and the short conv

How the hidden state decides how much memory to let in, why the demo takes a signed square root, and what the dilated causal conv adds.

STEP 1 / 6QUIZ 0 / 2
01 /

The lookup can't see context

The rows come from the last few tokens only. A hash collision brings back another n-gram's memory, and even a correct read can be wrong for this document - “bank” after “river” versus after “central”. The hidden state at this position knows much more, so Engram lets it decide.

ARCHITECTURE DIAGRAM
Rendering diagram...
02 /

The gate

The memory e (all rows, concatenated) is projected to a key and compared with the hidden state h, which plays the query:

z = ⟨RMSNorm(W_K e), RMSNorm(h)⟩ / √d
Both sides are RMS-normalized, each with its own learned scale. d is the hidden width.
gate = σ( sign(z) · √max(|z|, 10⁻⁶) )
The paper's equation applies σ to z directly; the released demo code takes the signed square root first.

Then value = gate · W_V e. With hyper-connections (the paper uses 4 streams) each stream has its own key projection and norms, so its own gate; the value projection is shared.

03 /

Why the square root

RMSNorm makes both vectors length √d, so z = cos · √d. At d = 1,024 that is 32 · cos: a cosine of 0.2 already gives σ(6.4) = 0.998. The signed square root brings the same input to σ(2.53) = 0.926, so the gate stays graded across a wider range of agreement. Drag the width up and compare the curves:

INTERACTIVE / THE GATE
-10100.51
<k, q> / √d = 6.400
signed √ = 2.530
gate = 0.9262
without √: 0.9983
The gate for one position as a function of the cosine between the normalized key and the normalized hidden state. Solid: Engram's sigmoid(signed √(·)). Dashed: the same without the square root. At the demo's width (1,024 per stream) the dashed gate is close to a hard on/off switch; the square root keeps it graded.
04 / CHECK YOURSELF

Quick check

What shape is the gate for hidden states [b, s, d] with one stream?

05 /

A short conv over the gated values

The last step runs a depthwise causal convolution over RMSNorm(value), then adds it back:

out = value + SiLU( conv( RMSNorm(value) ) )
Kernel 4, dilation = the largest n-gram order (3). Depthwise: each channel has its own 4 weights.
INTERACTIVE / SHORT CONV: WHICH POSITIONS A TOKEN READS
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15

y[12] = w0·v[3] + w1·v[6] + w2·v[9] + w3·v[12] → SiLU

Receptive field: 10 tokens.

Tap j of a depthwise causal conv reads position t − (k − 1 − j)·dilation. Engram uses k = 4 and dilation = the largest n-gram order (3), so one output mixes the gated memory of four positions spread over ten tokens. Taps before the start read zeros.

With dilation 3, the conv lines up with trigram boundaries: it mixes the memories of the previous non-overlapping trigrams into this one. The paper's ablation found that removing it “only marginally degrades performance” - it is the least important of the parts.

06 / CHECK YOURSELF

Quick check

Kernel 4, dilation 3, output position t = 10. Which positions does it read?

LESSON 4 COMPLETE

Up next: Coding it: PyTorch to Hyperfield