Skip to main content

Log Depth Recurrent Modeling (wip)

Explorations into the Log Depth Recurrent Modeling proposed by Yiqin Wang of Imperial College London

AutoregressiveGatedRecursiveCell scans a sequence in log depth by padding it to the nearest power of two and running a Blelloch scan over the resulting balanced binary tree, giving it length extrapolation for free. Setting max_seq_len (a power of two) pins the window size instead, padding and folding longer sequences into independent windows processed in parallel.

Usage

Train on the parity task at sequence length 16, then extrapolate to unseen sequence lengths

import torch
from log_depth_recurrent_modeling import ARGRC

torch.manual_seed(42)

model = ARGRC(num_tokens = 2, dim_embed = 32, dim = 32)
opt = torch.optim.AdamW(model.parameters(), lr = 3e-3)

def parity_batch(seq_len, batch_size = 64):
    bits = torch.randint(0, 2, (batch_size, seq_len))
    return bits, bits.cumsum(dim = -1) % 2

# train on sequences of length 16

for step in range(1, 201):
    bits, labels = parity_batch(16)

    loss = model(bits, labels = labels)
    loss.backward()
    opt.step()
    opt.zero_grad()

    if step % 50 == 0:
        print(f"step {step:3d} | loss: {loss.item():.4f}")

# extrapolate to unseen sequence lengths

for seq_len in (16, 64, 256, 1024):
    bits, labels = parity_batch(seq_len, 100)
    acc = (model(bits).argmax(dim = -1) == labels).float().mean().item()
    print(f"seq len {seq_len:4d} | accuracy: {acc * 100:.1f}%")
step  50 | loss: 0.6559
step 100 | loss: 0.0003
step 150 | loss: 0.0000
step 200 | loss: 0.0000
seq len   16 | accuracy: 100.0%
seq len   64 | accuracy: 100.0%
seq len  256 | accuracy: 100.0%
seq len 1024 | accuracy: 100.0%

Citations

@misc{wang2026logdepthrecurrentlanguagemodeling,
    title    = {Log-Depth Recurrent Language Modeling},
    author   = {Yiqin Wang and Nuri Cingillioglu and Charles Pert},
    year     = {2026},
    eprint   = {2609.28212},
    archivePrefix = {arXiv},
    primaryClass = {cs.LG},
    url      = {https://arxiv.org/abs/2609.28212},
}
@misc{shen2019orderedmemory,
    title   = {Ordered Memory},
    author  = {Yikang Shen and Shawn Tan and Arian Hosseini and Zhouhan Lin and Alessandro Sordoni and Aaron Courville},
    year    = {2019},
    eprint  = {1910.13466},
    archivePrefix = {arXiv},
    primaryClass = {cs.LG},
    url     = {https://arxiv.org/abs/1910.13466},
}
@misc{pert2026lengthgeneralizationlogdepthrecurrent,
    title     = {Length Generalization with Log-Depth Recurrent Units},
    author    = {Charles Pert and Dalal Alrajeh and Alessandra Russo},
    year      = {2026},
    eprint    = {2605.26035},
    archivePrefix = {arXiv},
    primaryClass = {cs.LG},
    url       = {https://arxiv.org/abs/2605.26035},
}

Release files for log-depth-recurrent-modeling 0.0.4

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for log-depth-recurrent-modeling 0.0.4
File Size Uploaded
log_depth_recurrent_modeling-0.0.4.tar.gz 6.5 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for log-depth-recurrent-modeling 0.0.4
File Interpreter ABI Platform
log_depth_recurrent_modeling-0.0.4-py3-none-any.whl Python 3 none any Details

Total release size: 12.7 kB

Release files / log_depth_recurrent_modeling-0.0.4.tar.gz

Download URL log_depth_recurrent_modeling-0.0.4.tar.gz
Size 6.5 kB
Tags Source
SHA-256 checksum
How to use checksums
59dbb48afef4d7a5d7e85a4b775358e20a62d39df78ff9f4ada32a16d0ed42ac
BLAKE2b-256 checksum
How to use checksums
1ea9f8db7b8c7a76d1f8b65e885a9d6ab1462bea4d7871b2a681f8b6db53a3db
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.17

Release files / log_depth_recurrent_modeling-0.0.4-py3-none-any.whl

Download URL log_depth_recurrent_modeling-0.0.4-py3-none-any.whl
Size 6.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
f8042c857849a189ff53a155f150332f14d3185c191a8903a48d300241351f73
BLAKE2b-256 checksum
How to use checksums
0fd7011e8e321e5a45d8efcbe2298cff29bf53498bb4f2c1adc3aa3a34d691be
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.17

Release history Release notifications | RSS feed

0.0.5

2 release files

This release

0.0.4 This release

2 release files

0.0.2

2 release files

0.0.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page