OpenViSTAI · Agents · Blockchain · Crypto
Menu
AI Models

Memorizing Transformers Pytorch

Memorizing Transformers Pytorch is a Python and PyTorch library implementing transformer attention augmented with approximate-nearest-neighbor indexing and retrieval of key-value memories. It combines local attention with retrieved distant memories and includes examples of KNN memory management and integration with Transformer-XL memories.

Independent OpenViST project profile, based on linked sources.

Memorizing Transformers Pytorch — OpenViST project profile in AI Models.
OpenViST project illustration. See the linked sources for product details.
Understand the project

Practical overview

When it fits

Use it when a PyTorch prototype needs transformer attention augmented with retrieved memories, or when exploring KNN memory alongside Transformer-XL memory.

Who it is for

  • PyTorch developers prototyping memory-augmented transformer models.
  • Researchers and engineers experimenting with approximate-nearest-neighbor retrieval in attention.
  • Developers exploring KNN and Transformer-XL memory together across successive model inputs.

Documented uses

  • Configure a transformer with selected layers that use ANN memories.
  • Create and pass batch-specific KNN memories into model calls.
  • Retrieve stored key-value memories and manage their updates or clearing.
  • Experiment with local attention combined with retrieved distant context.
  • Run the repository's Enwik8 training example.
Before you choose

Requirements & limits

The project is a public GitHub repository with an MIT license; its README documents installation through pip.

  • The implementation's local/distant attention combination and cosine-similarity KNN attention differ slightly from the cited paper's setup.
  • The README documents both KNN-only usage and an example combining KNN memories with Transformer-XL memories.
  • The transformer example resets its ANN memory collection when its configured capacity is reached, citing a Faiss removal limitation.
  • A forward-call option can make passed KNN memories read-only by disabling memory additions.

Check current requirements in the official documentation. Source-based guidance does not mean OpenViST has run or independently tested this project.

Source-based practical guide

Install the library and initialize KNN memories

Set up a MemorizingTransformer and a batch-matched KNN memory collection as shown in the README.

Before you start

  • Python environment with PyTorch available.
  • Install the package with pip using the documented package name.

Steps

  1. Run the command below.

    Documented command
    pip install memorizing-transformers-pytorch
  2. Import MemorizingTransformer and configure a model, including token count, dimensions, depth, memory layers, capacity, and number of retrieved memories.

  3. Create a KNN memory collection with model.create_knn_memories(batch_size=...) using the intended batch size.

  4. Pass the collection to the model with model(data, knn_memories=knn_memories). Set add_knn_memory=False on the forward call when the example's read-only behavior is wanted.

Expected outcome

A configured model call returns logits while using the supplied KNN memory collection; the example also shows how to keep that collection from being updated during a call.

Guide limitations

  • The README notes that the transformer example resets the ANN memories after its configured capacity because of a Faiss limitation on removing entries.
  • The sample inputs and dimensions are illustrative values in the README, not a guarantee of suitability for a particular workload.
Source-based practical guide

Run the documented Enwik8 training script

Start the repository's Enwik8 training example using its documented command.

Before you start

  • A local checkout of the repository.
  • Python dependencies and environment configured for the project.

Steps

  1. Change to the repository directory.

  2. Run the command below as shown under the README's Enwik8 training section.

    Documented command
    python train.py

Expected outcome

Invokes the repository's documented Enwik8 training script.

Guide limitations

  • The README gives the command but does not specify training parameters or expected results in the cited section.
Evidence and transparency

Sources & review

Source record details
Source availability
Active
Sources checked
15 October 2026
Original website title
lucidrains/memorizing-transformers-pytorch · Official homepage title · retained in its source language