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.2.1.tar.gz (475.7 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.2.1-py3-none-any.whl (545.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: ram_wnn-0.2.1.tar.gz
  • Upload date:
  • Size: 475.7 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.2.1.tar.gz
Algorithm Hash digest
SHA256 3ae21d5a25eefb1ec0d98a77947b42e7cc8f765f41400a7ce750b58b0a6080c8
MD5 26c3a821077617693f3c99e26618369c
BLAKE2b-256 eb3caf16c5f676d71341fd72258240d260c358bd143bf552f5b9202424de7b75

See more details on using hashes here.

Provenance

The following attestation bundles were made for ram_wnn-0.2.1.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.2.1-py3-none-any.whl.

File metadata

  • Download URL: ram_wnn-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 545.0 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.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 40f9fadc1e45409bbb207f835ad01d920d9bf61f7beb9c8fa3979a6a6dc61aa7
MD5 8806b4033d2ffc7a5f89de6ac5d56101
BLAKE2b-256 2b887b3caf72c717dce1e0f4fb2691872c64646cef77dbbf6558e4411cc8c3b7

See more details on using hashes here.

Provenance

The following attestation bundles were made for ram_wnn-0.2.1-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