Skip to main content

Astrocyte-Hebbian Spiking Transformer Plugin

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

Scope

This is a focused spiking model plugin providing:

  • AstrocyteHebbianAttention: multi-head linear attention using binary Q/K/V activations with configurable gradient mode ("surrogate" or "exact").
  • AstrocyteHebbianBlock: pre-norm Transformer-style block with a spiking FFN.
  • AstrocyteHebbianClassifier: ready-to-train sequence classifier.
  • CausalAstrocyteLanguageModel: byte-level causal language model with streaming recurrence.
  • spike_fn: binary Heaviside threshold supporting fast-sigmoid surrogate gradients and exact closed-form IFT gradients.

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 / Reciprocal Mode (gradient_mode="exact", Default): Uses a bounded inverse / reciprocal surrogate derivative $g'(x) = \frac{1}{|x| + \epsilon}$ for $|x| \le 1.0$, evaluating threshold boundary sensitivity directly out of the box. (Note: Full Time-to-First-Spike trajectory IFT solvers are available via exact_snn integration).
  • 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

import torch
from astrohebbian import AstrocyteHebbianClassifier, CausalAstrocyteLanguageModel

# Sequence Classifier with Reciprocal 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 Reciprocal 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: Reciprocal 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.1

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.1
File Size Uploaded Provenance
astrohebbian-0.2.1.tar.gz 18.2 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 31.6 kB

Release files / astrohebbian-0.2.1.tar.gz

Download URL astrohebbian-0.2.1.tar.gz
Size 18.2 kB
Tags Source
SHA-256 checksum
How to use checksums
98b5b651e37a16ab80052ffa88244340330c54b0c028ae7cdab3b28598dd0fb0
BLAKE2b-256 checksum
How to use checksums
8787204deebb720671b43fd196939270fdcf666d2d580d33cd06f1c1455970a1
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.1-py3-none-any.whl

Download URL astrohebbian-0.2.1-py3-none-any.whl
Size 13.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
e8ee9c2b5516353d98fcbcb570917a15e038f573e32046e514d1660f2e57394e
BLAKE2b-256 checksum
How to use checksums
5189aa052565b58b524bb936a0914ccb4ce03e983871b585c71b7484a7466e52
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.1

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