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 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

import torch
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.2

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.2
File Size Uploaded Provenance
astrohebbian-0.2.2.tar.gz 21.1 kB Without provenance Details

Built distribution (wheel)

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

Total release size: 36.5 kB

Release files / astrohebbian-0.2.2.tar.gz

Download URL astrohebbian-0.2.2.tar.gz
Size 21.1 kB
Tags Source
SHA-256 checksum
How to use checksums
513956ce14115138b377c0a3f18688648829c11a4e52fc804317a5febbc09d15
BLAKE2b-256 checksum
How to use checksums
9b104c47422e8d700ebd580617b6eed539a4f81c5f7f1ec0330c9b7dd91513ba
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.2-py3-none-any.whl

Download URL astrohebbian-0.2.2-py3-none-any.whl
Size 15.4 kB
Tags Python 3
SHA-256 checksum
How to use checksums
3fedf81c9b53e8640b5bfa3e37c09c529b71cd77094fbe172271d6c436c74292
BLAKE2b-256 checksum
How to use checksums
0a6d62b8f4043306c76728c966882250bc19c06deb92e33703e18704c20cac69
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.2

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