Skip to main content

Astrocyte-Hebbian Spiking Transformer Plugin

A standalone PyTorch plugin (not a framework) providing Astrocyte-Hebbian spiking linear-attention components with support for both Surrogate Gradients and Exact SNN (Implicit Function Theorem / IFT) Gradients.

What this is

This package is a drop-in set of reusable nn.Module components. You import them and plug them into your own model — you stay in control of the architecture. It does not define a training loop or force a whole model on you.

The plugin API (the building blocks you plug in)

These are the core, self-contained modules:

  • AstrocyteHebbianAttention: multi-head linear attention using binary Q/K/V activations with configurable gradient mode ("surrogate" or "exact"). forward(x) -> x, same shape in/out.
  • AstrocyteHebbianBlock: pre-norm Transformer-style block (attention + spiking SpikingFFN). The single unit you'd drop into any transformer. forward(x) -> x.
  • SpikingFFN: feed-forward with a binary hidden activation.
  • ExactLinearSpike / ExactSpike: fused linear + binary-spike layers with exact IFT gradients — drop-in replacements for nn.Linear + a spike function.
  • spike_fn: binary Heaviside threshold supporting fast-sigmoid surrogate gradients and exact closed-form IFT gradients.
  • CausalAstrocyteHebbianAttention: causal/streaming variant with a recurrent decayed Hebbian state.

Example wrappers (built on top of the plugin)

These are full models assembled from the plugin components — useful starting points, not the intended way to use the package:

  • AstrocyteHebbianClassifier: ready-to-train sequence classifier (embedding + blocks + head).
  • CausalAstrocyteLanguageModel: byte-level causal language model with streaming recurrence.

The implementation avoids an N x N attention matrix by computing the K^T V trace. It uses ordinary dense PyTorch tensors for projections, normalization, residual paths, and training.

Gradient Modes: Exact Mode (Default) vs. Surrogate Mode

  • Exact Mode (gradient_mode="exact", Default): Fuses each linear projection with a binary threshold spike (ExactLinearSpike / ExactSpike) and backpropagates through the spike-time map of an exponential integrate-and-fire membrane using the Implicit Function Theorem (IFT). Only firing neurons (pre > θ) receive a gradient, with magnitude τ·θ / (pre·(pre−θ)).
  • Surrogate Mode (gradient_mode="surrogate"): Uses a fast-sigmoid surrogate derivative approximation $\sigma'(x) \cdot \alpha$ through firing thresholds during backpropagation.

Install

pip install -e .

For development and tests:

pip install -e .[test]
pytest -q

For the optional psMNIST benchmark:

pip install -e .[benchmark]
python astrohebbian/benchmark.py

For comparing Surrogate SNN, Exact SNN, and Dense Transformer on a causal language model task:

python benchmarks/lm_prototype.py \
    --data data/pretraining_code.jsonl \
    --max-bytes 5000000 --steps 30 --output results/lm_comparison.json

For sequence classification comparison:

python benchmarks/surrogate_vs_exact.py --epochs 5 --output results/surrogate_vs_exact.json

Example

Plug the block into any transformer (plugin usage)

import torch
from astrohebbian import AstrocyteHebbianAttention, AstrocyteHebbianBlock

# Drop-in attention: same shape in / out
attn = AstrocyteHebbianAttention(d_model=128, num_heads=4, gradient_mode="exact")
out = attn(torch.randn(2, 64, 128))          # torch.Size([2, 64, 128])

# Or a full pre-norm block (attention + spiking FFN) — stack these with your own
# residuals, norms, embeddings, and head however you like.
block = AstrocyteHebbianBlock(d_model=128, num_heads=4, expansion=4, gradient_mode="exact")
out = block(torch.randn(2, 64, 128))         # torch.Size([2, 64, 128])

Ready-made wrappers (built from the plugin)

from astrohebbian import AstrocyteHebbianClassifier, CausalAstrocyteLanguageModel

# Sequence Classifier with Exact Spike Gradients
classifier = AstrocyteHebbianClassifier(
    input_dim=1,
    d_model=128,
    seq_len=784,
    num_heads=4,
    v_levels=1,
    gradient_mode="exact",  # Options: "surrogate" | "exact"
)

pixels = torch.randn(8, 784, 1)
logits = classifier(pixels)
print(logits.shape)  # torch.Size([8, 10])

# Causal Language Model with Exact Spike Gradients
lm = CausalAstrocyteLanguageModel(
    vocab_size=256,
    d_model=64,
    seq_len=128,
    num_heads=4,
    gradient_mode="exact",
)

tokens = torch.randint(0, 256, (4, 128))
lm_logits = lm(tokens)
print(lm_logits.shape)  # torch.Size([4, 128, 256])

Benchmark Results

Causal Language Model (pretraining_code.jsonl, 5MB text sample, 30 steps, CUDA)

Model Variant Initial Loss Final Train Loss Validation Loss Validation Perplexity Step Time Peak VRAM
AstroHebbian SNN (Surrogate) 5.584 3.826 3.439 31.15 2.38s 29.18 MB
AstroHebbian SNN (Exact Mode) 5.752 3.624 3.588 36.15 1.99s 29.73 MB
Dense Causal Transformer 5.719 3.350 3.290 26.85 1.38s 36.36 MB

Key Observations:

  • Lower Training Loss at Step 30: AstroHebbian SNN in exact/reciprocal mode reached a lower training loss (3.624) than surrogate mode (3.826) after 30 steps.
  • VRAM Savings: Both SNN variants saved ~18% Peak VRAM (~29.2–29.7 MB) relative to the dense Transformer (~36.4 MB).
  • Speed: Exact IFT spike gradients completed 30 steps in 1.99s (vs 2.38s for surrogate gradients).

Project status

This standalone plugin is an experimental research package for prototyping and benchmarking spiking linear transformers. Results are tracked in docs/benchmark_baseline.md and results/lm_comparison.json. Further multi-epoch training and validation are recommended before production deployment.

Metadata

Release files for astrohebbian 0.2.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for astrohebbian 0.2.3
File Size Uploaded Provenance
astrohebbian-0.2.3.tar.gz 21.9 kB Without provenance Details

Built distribution (wheel)

Table of built distributions (wheels) for astrohebbian 0.2.3
File Interpreter ABI Platform
astrohebbian-0.2.3-py3-none-any.whl Python 3 none any Details

Total release size: 38.0 kB

Release files / astrohebbian-0.2.3.tar.gz

Download URL astrohebbian-0.2.3.tar.gz
Size 21.9 kB
Tags Source
SHA-256 checksum
How to use checksums
96f0e7a2e44c85ffbf41918484af64d44324bbc34142e5026c539fb3602224d2
BLAKE2b-256 checksum
How to use checksums
821b95169bedd66a144aebc99332aba45e78448231d336e7567bc131fc9bd186
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.11

Release files / astrohebbian-0.2.3-py3-none-any.whl

Download URL astrohebbian-0.2.3-py3-none-any.whl
Size 16.1 kB
Tags Python 3
SHA-256 checksum
How to use checksums
abdcc057d378acfb1272e617e5c65731c031da0553e86d892b6942105b74ac8e
BLAKE2b-256 checksum
How to use checksums
139269a57e380418e2e495201f1d4235fff7426035da960cdf04bc564985575d
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via twine/7.0.0 CPython/3.10.11

Security for astrohebbian 0.2.3

Provenance

Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.

No provenance found

None of the files in this release were uploaded with provenance metadata.

A note on package security

PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.

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