The Induction Heads task is associative recall: somewhere in the sequence a special trigger token is followed by an answer token, and at the very end the trigger appears again, so the next token to predict is the answer. Mamba is trained on sequences of length 256 and tested on much longer ones to measure length extrapolation. Build the generator, an evaluator that scores any prediction function across test lengths, and the extrapolation summary.
Token convention: ordinary tokens are 0, ..., vocab_size - 2 and the trigger is vocab_size - 1.
make_induction_heads(n_samples, seq_len, vocab_size, seed)
(inputs, targets): an int64 array of shape (n_samples, seq_len) and an int64 array of shape (n_samples,).p with 0 <= p <= seq_len - 3 and at the final position seq_len - 1. The trigger appears exactly twice. The target is the token at position p + 1.np.random.default_rng(seed) per call, called rng below. For each sample in order, draw the row with rng.integers(0, vocab_size - 1, size=seq_len), then draw p with rng.integers(0, seq_len - 2), then overwrite positions p and seq_len - 1 with the trigger.ValueError unless seq_len >= 3 and vocab_size >= 2.induction_accuracy(predict_fn, lengths, n_samples, vocab_size, seed)
i-th length L in lengths, generate a batch with make_induction_heads(n_samples, L, vocab_size, seed + i), call predict_fn(inputs), which returns n_samples predicted tokens as any array-like, and compute the fraction that equal the targets.int(L) to that accuracy as a Python float rounded to 4 decimals, in the order of lengths.extrapolation_factor(acc_by_len, train_len, threshold=0.99)
acc_by_len in increasing order and find the largest length L such that every length up to and including L has accuracy >= threshold. Return L / train_len rounded to 4 decimals, or 0.0 if the shortest length already fails.n_samples integers.