How lucidrains/fast-weight-product-key-memory works
1. Repository purpose
- This is a small Python library plus example training code for Fast Weight Product Key Memory (fwPKM), a learned memory module from the cited paper.
- The architecture is centered on a reusable memory component, with separate scripts for toy validation and an Enwik8 language-model experiment.
2. Core package boundary
- The installable library lives under fast_weight_product_key_memory/.
- fast_weight_product_key_memory/__init__.py exposes the public API, so downstream users import the memory module from the package root.
- fast_weight_product_key_memory/fwPKM.py is the main implementation file and is the architectural center of the repo.
3. Primary runtime dependency
- The implementation is PyTorch-based, as shown by the usage example with torch.randn(...) and tensor-shaped inputs/outputs.
- The module operates on batched token embeddings, so the key abstraction is a tensor pipeline rather than a service or data API.
4. fwPKM module behavior
- The fwPKM object accepts token embeddings and returns:
- retrieved: memory-augmented representations
- next_memories: updated fast-weight / episodic memory state
- The design supports stateful chaining across chunks by passing past_memories back into later calls, which is important for diagramming data flow.
5. Chunked sequence processing
- The usage example shows the model processing sequences in fixed-size chunks (chunk_size=256).
- This means memory updates and retrieval happen incrementally over subsequences, not only on full-length sequences at once.
- For an architecture diagram, show a loop or pipeline over token chunks with state carried forward between chunks.
6. Memory structure and parameters
- The module is configured by hyperparameters such as dim, num_memories, dim_queries_keys, dim_values, topk, learning_rate, and chunk_size.
- These parameters indicate the module combines learned key/query matching with sparse top-k retrieval and a fast-weight update rule.
- num_memories suggests a large associative memory table; topk implies sparse selection of candidate slots.
7. Training / experiment entry points
- train_toy_memory_task.py is likely a minimal correctness or behavior demo for the memory module.
- train_enwik8.py is the main benchmark script for character-level language modeling on Enwik8.
- These scripts sit outside the library package and serve as consumers of the core fwPKM component.
8. Dataset and local asset
- data/enwik8.gz is bundled in the repository, indicating the Enwik8 experiment can run from local data without fetching a remote dataset at runtime.
- The data directory is not a general-purpose data layer; it is an experiment asset for the training script.
9. Testing boundary
- tests/test_memory.py is the main automated test file and likely focuses on memory behavior, shape correctness, or state chaining.
- This is the main verification boundary for the library portion of the repo.
10. Packaging and developer workflow
- pyproject.toml defines the project as a Python package and supports installation via pip.
- The README uses uv run train_enwik8.py, so uv is part of the intended local development/training workflow.
- There are no external services, databases, or web endpoints; the architecture is entirely local computation around a PyTorch module and experiment scripts.
Main entry points: fwPKM.py fwPKM · train_enwik8.py Enwik8 Train · README.md README · __init__.py Package API
Components 15
Library
Installable fwPKM package and core implementation.
- fwPKM · Core fast-weight product key memory implementation.
fast_weight_product_key_memory/fwPKM.py - Package API · Package root that exposes the reusable memory module.
fast_weight_product_key_memory/__init__.py - Token Embeddings · Batched embeddings passed into fwPKM.
- Retrieved Output · Memory-augmented representations returned by fwPKM.
- Top-k Match · Selects candidate memory slots using key/query similarity.
- Fast Update · Updates episodic memory state after retrieval.
- Hyperparameters · Model settings such as dim, num_memories, topk, and chunk_size.
- PyProject · Project metadata and dependency definition for installation.
pyproject.toml
Experiments
Training entry points that consume the memory module.
- Toy Train · Minimal training script for validating memory behavior.
train_toy_memory_task.py - Enwik8 Train · Main character-level language modeling experiment.
train_enwik8.py - Chunk Loop · Processes token embeddings in fixed-size chunks.
- Memory State · Carries past memories forward across chunked calls.
- README · Documents the intended training and development workflow.
README.md
Verification
Automated checks for memory behavior and interfaces.
- Tests · Automated checks for shapes, chaining, and memory behavior.
tests/test_memory.py
Assets
Bundled local data used by experiments.
- Enwik8 Data · Bundled gzip-compressed corpus for the language-model experiment.
data/enwik8.gz
Connections 16
- Package API → fwPKM: exports · Public package root exposes the fwPKM module.
- Toy Train → fwPKM: uses · Toy training script instantiates the core memory module.
- Enwik8 Train → fwPKM: uses · Enwik8 training script depends on fwPKM for sequence modeling.
- fwPKM → Token Embeddings: consumes · Accepts token embeddings as input tensors.
- fwPKM → Retrieved Output: returns · Produces retrieved representations and updated memory state.
- fwPKM → Top-k Match: retrieves via · Matches queries against memory slots using sparse top-k selection.
- fwPKM → Fast Update: updates via · Applies fast-weight updates to memory state.
- fwPKM → Hyperparameters: parameterized by · Behavior is controlled by model and chunking hyperparameters.
- Enwik8 Train → Enwik8 Data: reads · Enwik8 benchmark uses the bundled local dataset asset.
- Enwik8 Train → Chunk Loop: runs · Training proceeds over fixed-size sequence chunks.
- Chunk Loop → Memory State: threads state · Carries past memories from one chunk to the next.
- Memory State → fwPKM: feeds back · Previous memory state is passed into later fwPKM calls.
- Tests → fwPKM: verifies · Tests focus on the core memory module.
- PyProject → Package API: packages · Project metadata enables installation of the library package.
- README → Enwik8 Train: documents · README describes the Enwik8 training workflow.
- README → Toy Train: documents · README also covers toy validation usage.