Skip to main content

Weightless Neural Networks for Language Modeling — RAM-based neurons in PyTorch

Project description

ram-wnn

Weightless Neural Networks for Language Modeling — RAM-based neurons in PyTorch.

PyPI Python License

This research explores whether Weightless Neural Networks (WNNs) — specifically RAM-based neurons — can serve as a foundation for language modeling, traditionally dominated by weighted transformer architectures.

RAM neurons use lookup tables instead of weighted connections. Partial connectivity is the generalization mechanism: each neuron observes a subset of input bits, so similar inputs map to the same address and trigger the same response.

Installation

pip install ram-wnn

Requires Python 3.11+ and PyTorch 2.0+.

Quick Start

from wnn.ram.core.models.bitwise_ramlm import BitwiseRAMLM
from wnn.eval import WIKITEXT2_TEST

# Create a per-bit output language model (16 clusters for GPT-2's 16-bit vocab)
model = BitwiseRAMLM(
    vocab_size=50257,      # GPT-2 tokenizer
    context_size=4,        # 4-gram context
    neurons_per_cluster=200,
    bits_per_neuron=20,
)

# Load WikiText-2 test data (GPT-2 BPE tokenizer)
tokens = WIKITEXT2_TEST.load_tokens()

# Train
model.train_on_tokens(tokens[:200_000])

# Evaluate
stats = model.evaluate_fast(tokens[200_000:])
print(f"CE: {stats['cross_entropy']:.2f}, PPL: {stats['perplexity']:.0f}")

Key Results

All models evaluated on WikiText-2 with GPT-2 tokenizer (50,257 vocab).

Architecture CE PPL Acc Notes
Random baseline 10.82 50,257 0.002% Uniform prediction
Tiered RAMLM (50K clusters) ~10.20 ~27,000 ~4.9% 5-tier, EMPTY=0.0
BitwiseRAMLM (16 clusters) ~9.15 ~9,400 ~6.6% Per-bit prediction
Target: GPT-2 Small (124M) 3.38 29.41 -- Zero-shot

Architecture

BitwiseRAMLM

Instead of 50K output clusters (one per token), BitwiseRAMLM uses 16 clusters (one per output bit). Each cluster predicts P(bit_i=1 | context). Token probabilities are reconstructed via log-product:

log P(token=t) = Σ_i [b_i(t)·log(P_i) + (1-b_i(t))·log(1-P_i)]

Key advantage: every neuron sees ALL training examples (not just ~20 for rare tokens).

Tiered RAMLM

Frequency-based architecture where frequent tokens get more capacity:

Tier Tokens Neurons Bits Data %
0 50 most frequent 15 20 42%
1 Next 50 13 18 5%
2 Next 400 9 10 13%
3 Next 20K 7 9 37%
4 Rest (~30K) 5 8 3%

Reproducibility

Save and load checkpoints for reproducible results:

from wnn.eval import Checkpoint, WIKITEXT2_TEST

# Save (connections only = ~381 KB)
Checkpoint.save(
    path="checkpoints/my_model",
    model=model,
    eval_task=WIKITEXT2_TEST,
    results={"ce": 9.15, "ppl": 9430, "accuracy": 0.066},
)

# Load and reconstruct
ckpt = Checkpoint.load("checkpoints/my_model")
model = ckpt.create_model()
print(ckpt.summary())

Rust+Metal Accelerator (Optional)

For Apple Silicon Macs, a Rust accelerator provides 3-8x speedup using Metal GPU compute:

# Requires Rust toolchain
cd src/wnn/ram/strategies/accelerator
pip install maturin
maturin develop --release

# Verify
python -c "import ram_accelerator; print(ram_accelerator.cpu_cores())"

The accelerator is optional — all functionality works with pure PyTorch.

Research Blog

Follow the research progress at lacg.github.io/llm-optimizer.

Citation

@software{garcia2025ramwnn,
  author = {Garcia, Luiz Alberto Crispiniano},
  title = {RAM-WNN: Weightless Neural Networks for Language Modeling},
  year = {2025},
  url = {https://github.com/lacg/wnn}
}

License

MIT

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

ram_wnn-0.1.0.tar.gz (475.6 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

ram_wnn-0.1.0-py3-none-any.whl (545.1 kB view details)

Uploaded Python 3

File details

Details for the file ram_wnn-0.1.0.tar.gz.

File metadata

  • Download URL: ram_wnn-0.1.0.tar.gz
  • Upload date:
  • Size: 475.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ram_wnn-0.1.0.tar.gz
Algorithm Hash digest
SHA256 6561f04b879e3ef64218c8257ce5f8c05e30a3ba09f41e7c14b9c60d20a51e24
MD5 5150cdc3b98a640638d76afbe038e9a0
BLAKE2b-256 1ecaf50a98342d3f8fddcdf2ed3aae47c9551b641db893c8ea2d5c644b85c407

See more details on using hashes here.

Provenance

The following attestation bundles were made for ram_wnn-0.1.0.tar.gz:

Publisher: publish.yml on lacg/wnn

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

File details

Details for the file ram_wnn-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: ram_wnn-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 545.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/6.1.0 CPython/3.13.7

File hashes

Hashes for ram_wnn-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 61390e2a37594dfb56c59eb2c43ce9b831cbc6fa4f038afe24c9734f54bd05ba
MD5 cd3221b7174caba8b1bb70b9e8181354
BLAKE2b-256 fbab14311a4dc5767683693fbe3d07d73a81337b6bbfd5f5b3139af74a5c1f77

See more details on using hashes here.

Provenance

The following attestation bundles were made for ram_wnn-0.1.0-py3-none-any.whl:

Publisher: publish.yml on lacg/wnn

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page