Search the guide

Start with a guide

Lesson 4 of 14 · Construct programs

Compile an exact program

Follow a counting rule into transformer weights.

Start with an explicit rule#

Count the token a in a b a c. Matching each token produces [1, 0, 1, 0]; adding the matches gives 2. Change the sequence below to inspect that computation.

A computation you can inspect

Match each token, then add the matches. Change the sequence or target to see every intermediate value.

a1
b0
a1
c0
Count = 2

This interactive view shows the exact counting computation. The Tracr example below compiles a sequence program into transformer weights.

Express it in RASP#

RASP describes sequence operations. Tracr compiles supported RASP programs into transformer weights. This source selects every matching token and writes the count at each position.

python
from tracr.rasp import rasp

count_a = rasp.SelectorWidth(
    rasp.Select(rasp.tokens, rasp.tokens, lambda key, query: key == "a")
)

The interactive counter above evaluates the token matches in JavaScript. Run the full Tracr example to compile the RASP source and execute the resulting transformer.

The compiler constructs the network#

The source rule, vocabulary and maximum sequence length determine the compiled transformer. New sequences go through that network; they do not require another compilation.

Sources and resources

Course contents