Skip to main content

Log Depth Recurrent Modeling - Pytorch

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

Install

$ pip install log-depth-recurrent-modeling

Usage

import torch
from log_depth_recurrent_modeling import ARGRC

model = ARGRC(
    num_tokens = 256,
    dim = 128,
    depth = 2,
    max_seq_len = 65536,
    shift_tokens = True
)

tokens = torch.randint(0, 256, (1, 65535))

# autoregressive loss

loss = model(tokens, return_loss = True)
loss.backward()

# forward for logits

logits = model(tokens) # (1, 65535, 256)

Standalone ARGRCLayer, which automatically pads the sequence to a multiple of max_seq_len (or the next power of two if unset) and strips the padding from the output:

import torch
from log_depth_recurrent_modeling import ARGRCLayer

layer = ARGRCLayer(
    dim = 128,
    max_seq_len = 65536,
    prenorm = True,
    shift_tokens = True,
    separate_grc = False # shares up and down gated recursive cell
)

x = torch.randn(1, 65535, 128)

out = layer(x) + x # (1, 65535, 128)

Feed the sequence in chunks of varying length, carrying memory across calls (equivalent to one pass over the full sequence):

memory = None

for start, length in ((0, 3), (3, 5), (8, 2)):
    chunk = x[:, start : start + length]
    out, memory = layer(chunk, memory = memory, return_memory = True)

Tasks

Run parity task with length generalization:

$ python train_parity_extrapolation.py

Run character language modeling on enwik8 with memory caching during generation:

$ python train_enwik8.py

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},
}
@software{peng_bo_2021_5196578,
    author    = {PENG Bo},
    title     = {BlinkDL/RWKV-LM: 0.01},
    month     = {aug},
    year      = {2021},
    publisher = {Zenodo},
    version   = {0.01},
    doi       = {10.5281/zenodo.5196578},
    url       = {https://doi.org/10.5281/zenodo.5196578}
}

Release files for log-depth-recurrent-modeling 0.1.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.1.4
File Size Uploaded
log_depth_recurrent_modeling-0.1.4.tar.gz 10.0 kB Details

Built distribution (wheel)

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

Total release size: 23.3 kB

Release files / log_depth_recurrent_modeling-0.1.4.tar.gz

Download URL log_depth_recurrent_modeling-0.1.4.tar.gz
Size 10.0 kB
Tags Source
SHA-256 checksum
How to use checksums
ce4d025c184bbc4fb7a029b60f8db3157eb21ecdc71192bfd356b3eea7104575
BLAKE2b-256 checksum
How to use checksums
db6a249a5d1f7c3d3fa1774dd219b8418161309e4adccd99e6b5e4bcf30f1102
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.17

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

Download URL log_depth_recurrent_modeling-0.1.4-py3-none-any.whl
Size 13.2 kB
Tags Python 3
SHA-256 checksum
How to use checksums
c635cd796ca9f3bba4a919291d5947f38464dcfa0767ff5990c860a3be4f711d
BLAKE2b-256 checksum
How to use checksums
5c90259ae6c6913df3e1b109f82aa015b4da15b13e6fccde5c2d0bf72ef33876
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.8.17

Release history Release notifications | RSS feed

This release

0.1.4 This release

2 release files

0.1.2

2 release files

0.1.1

2 release files

0.1.0

2 release files

0.0.5

2 release files

0.0.4

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