The Continuous Thought Machine

The Continuous Thought Machine (CTM) is a recurrent architecture from Sakana AI (Darlow et al., arXiv 2505.05522). Its depth comes from internal ticks, not stacked layers: one pool of d_model neurons runs T times before it answers. modgrad-ctm is a Rust port of the reference ContinuousThoughtMachine.forward() and SynapseUNET, with gradients through every tick.

The tick loop

At each of the T ticks the model:

  1. Attends. A query from the current activations attends over the (layer-normed) input tokens.
  2. Mixes. The attention output and the activations go through the U-Net synapse, giving each neuron its next pre-activation.
  3. Remembers. Each neuron keeps a trace of its last M pre-activations, and a small per-neuron network maps it to the next activation.
  4. Synchronizes. Pairs of neurons accumulate a decaying product of their activations: one vector for the output, one for the attention query.
  5. Predicts. The output synchronization gives a prediction and a certainty for this tick.

More thinking costs more ticks, not more parameters.

The U-Net synapse

SynapseUNet is a U-Net whose widths narrow and widen again (a linspace bottleneck), with a skip connection at each level.

Neuron-level memory

Every neuron keeps its last memory_length pre-activations. A per-neuron map turns that history into the next activation: M → H → 2 with GLU gating with deep_nlms, otherwise M → 2. A neuron’s output depends on its own recent history, not only the current input.

Synchronization read-out

The output is read from pairs of neurons, not raw activations. For each pair (from left and right index lists) the model accumulates

αᵢ ← decayᵢ · αᵢ + act[leftᵢ] · act[rightᵢ]
βᵢ ← decayᵢ · βᵢ + 1
syncᵢ = αᵢ / sqrt(βᵢ)

with a learned decay per pair. In the published code the pairs are self-pairs for power-of-two widths; see known issues.

Adaptive early exit

CtmConfig::exit_strategy chooses when to stop:

strategy behavior
None run all T ticks
Certainty stop when an entropy-derived certainty passes a threshold
AdaptiveGate a learned linear halt gate on the synchronization: each tick gives λ, and the loop stops when the cumulative exit probability passes a threshold

The gate is regularized with a KL term. Depth then depends on the input, and λ can be read at every tick.

Loss

The default CtmLoss does not use the last tick. It averages the loss at the lowest-loss tick and at the most-certain tick, as in the paper. ThinkingLoss adds a reward for improving across ticks; ImaginationLoss splits the ticks into an ungraded first part and a graded second part. LastTickCE is plain cross-entropy on the last tick.

Data structures

type role
CtmConfig ticks, d_model, memory_length, synapse depth, exit strategy
CtmWeights every trainable parameter
CtmState per-forward state: activations, traces, sync accumulators
CtmCache per-tick intermediates kept for the backward pass
CtmGradients gradients, mirroring CtmWeights

ctm_forward_with_attn_trace also returns the attention weights for every tick and head, [ticks][heads][tokens].

Backpropagation through time

Training unrolls all T ticks and computes gradients for every weight. Tests check the backward pass against finite differences. Hot operations go through modgrad-device, so the same code runs on the CPU or a GPU backend.

The region graph

modgrad_ctm::graph wires CTMs into a directed graph of regions (RegionalConfig, RegionalWeights). Regions pass activations over learned linear connections, an optional router learns which regions feed which on each tick, and a global synchronization over all regions feeds one read-out. eight_region_small has four cortical regions of 32 neurons and four of 8, named input, attention, output, motor, cerebellum, basal ganglia, insula and hippocampus. No test shows the regions take on those roles.

The brain that used to run on /play was an eight_region_small brain (cortex width 32), trained on 9×9 maze images to predict a 20-step route. It got the first step right on 199 of 200 random mazes but the whole route on none. The maze demo uses a different, single-block model: the spatial maze CTM from the results.

A graph of blocks did no better than one block at matched parameters, so new work uses one block; see results.

Next: the crates →