Skip to main content

biosnn-bus

BioSNN-Plug 的模态插件骨架库。

它只负责一件事:让新增模态不需要修改已有架构。

这是骨架,不是认知核心。 本库不包含任何学习规则(e-prop / R-STDP / 核化 IB-Hebbian)、不包含脉冲神经元模型、不依赖 GPU、不依赖 PyTorch。它提供 的是插件的接口契约、注册与发现机制、以及脉冲总线的接缝。

安装

pip install biosnn-bus

# 需要 torch 桥接时(SpikeTrain.to_torch / from_torch)
pip install "biosnn-bus[torch]"

要从 main 分支装尚未发布的改动(例如正在评审的修复),可以走 Git:

pip install "biosnn-bus @ git+https://github.com/lzmd-arch/BioSNN-Plug.git#subdirectory=packages/biosnn-bus"

只依赖 numpy。

快速开始

import numpy as np
from biosnn_bus import ModalityPlugin, PassThroughMembrane, SpikeBus, SpikeTrain


class MyPlugin(ModalityPlugin):
    @property
    def modality_name(self) -> str:
        return "my_modality"

    @property
    def spike_dim(self) -> int:
        return 64

    def encode(self, raw_input):
        return SpikeTrain(data=np.asarray(raw_input) > 0.5, channel=self.channel)

    def get_membrane(self):
        return PassThroughMembrane()

    def decode(self, spike_output):
        return spike_output.rates


bus = SpikeBus(bus_dim=128, seed=0)
bus.register(MyPlugin())
out = bus.step({"my_modality": np.random.default_rng(0).random((10, 64))})
print(out)

完整可跑的版本见 examples/quickstart_register_plugin.py, 以及 Colab notebook(无 GPU 可跑)。

它跑出来就是下面这张图——一维正弦信号走时间通道(32 个单元的水平编码),8×8 的亮度变化序列走 语义通道(128 通道 ON/OFF),最下面那条是融合、投影之后真正送进认知核心的输入:

三面板脉冲栅格图:时间通道 sine_wave / 语义通道 image_diff / SpikeBus 融合输出

图内的标签是英文:默认字体没有中文字形,中文标签会渲染成方块。图只有这一份真相源—— scripts/make_figures.py 重跑上面那个示例、由示例自己把 PNG 落盘, 绘图代码不另抄一份。这是论文用图 F1。

核心概念

概念 说明
ModalityPlugin 模态插件的抽象基类:encode / decode / get_membrane 三方法,modality_name / spike_dim 两属性
SpikeTrain 形状 (T, N) 的脉冲序列容器,布尔(脉冲)或浮点(发放率)皆可
FusionChannel temporal / semantic 两条融合通道,对应计划书 §2.2 的"双通道融合"
SpikeBus 脉冲总线:编码 → 时间对齐 → 按通道融合 → 稀疏随机投影到认知核心维度
register_plugin 进程内注册装饰器
discover_plugins 扫描第三方分发包声明的 entry points

神经元:恒等膜与 ALIF

get_membrane() 返回的东西只需满足一个结构化协议(存在 forward(x))。库里有两个实现:

实现 有没有状态 用途
PassThroughMembrane 无 恒等。示例插件的最小模板,forward 原样返回输入
AdaptiveMembrane 有 纯 numpy 的 ALIF(自适应阈值 + 适应变量);beta=0 退化为 LIF
TernaryMembrane 有 有符号多值脉冲(TI-LIF):一步可发 -D..+D;depth=1, signed=False 时与 AdaptiveMembrane 逐位一致
import numpy as np

from biosnn_bus import AdaptiveMembrane

membrane = AdaptiveMembrane(128, tau=20.0, tau_adaptation=500.0, threshold=0.62, beta=0.07)
current = np.zeros(128)                 # 输入电流,形状 (128,)
spikes = membrane.forward(current)      # → 本步脉冲,形状 (128,) 的 bool
membrane.reset()                        # 状态清零

边界:它只实现神经元动力学——输入是电流,不是突触前脉冲。权重属于网络层, 所以这里没有 w_in / w_rec。研究层的 research/eprop/neurons.py 里的 ALIFCell 把两者合在一起,更新式与本类逐项对应,测试用同一组输入逐时间步对齐(tests/test_membrane.py)。

进程内的 SpikeBus.step() 不跑这个膜:它输出的仍是输入电流。要不要跑动力学、怎么接线, 由宿主(认知核心)决定。

内置插件

插件 模态 编码 融合通道
image_diff 图像 DVS 式 ON/OFF 事件(帧差) semantic
mnist_pulse MNIST 像素值即发放概率(伯努利) semantic
text_zscore 文本 冻结 embedding + z-score top-k 群体编码 temporal
audio_cochlea 音频 对数频带分解 + 发放率 temporal

第三方插件怎么接入

在你的包里声明 entry point,不需要修改 BioSNN-Plug 任何一行代码:

# 你的包的 pyproject.toml
[project.entry-points."biosnn_bus.plugins"]
audio = "my_pkg.plugins.audio:AudioPlugin"

用户侧扫描并接入。下面这段可以直接跑——即使一个第三方插件都没装:

from biosnn_bus import SpikeBus, discover_plugins, get_plugin, list_plugins
from biosnn_bus.plugins import DiffImagePlugin

discover_plugins()  # 扫描第三方分发包声明的插件;一个都没装也不会报错
print(list_plugins())  # 内置示例插件在 import 时已注册进注册表

bus = SpikeBus(bus_dim=128, seed=0)
bus.register(get_plugin("image_diff")())  # 等价于 bus.register(DiffImagePlugin())
print(bus)

骨架的边界

脉冲总线当前的默认融合策略是平凡的拼接(ConcatenateFusion),不是计划书 §2.2 描述的 TAAF 时间注意力引导融合——后者是第二阶段的研究任务。总线已经把 FusionStrategy 协议和双通道路由留好,替换策略不会改动调用方代码。

理由见 ADR-0001。

SpikeBus 规范 v0.1(宿主契约,2026-10-29 草案)

范围:插件作者与宿主(产品)之间的接缝;不标准化学习机制、神经元模型或调度策略。

宿主原语 语义 参数 返回 / 错误
inject(modality, raw, t=None) 把一个模态的原始输入推进总线 modality: str、raw: Any、t: int | None None;未知模态 → UnknownModality
read_state() 读当前融合态的只读快照 — dict(通道、维度、时间步);调用方不得原地修改
wait_trigger(timeout_ms) 阻塞到 SNN 认为「值得行动」 timeout_ms ≥ 0 TriggerEvent(t, salience, associations) 或超时 None

数据契约:时间轴为整数毫秒、同一 step 内各模态对齐;脉冲为布尔,浮点发放率必须在 [0, 1];禁止 NaN/inf;融合后维度恒为 bus_dim。

插件契约:encode / decode / get_membrane 三方法,modality_name 唯一、 spike_dim ≥ 1;不依赖 torch([torch] 只做可选桥接)。

版本:语义化版本;v0.x 允许破坏性变更并须在 release notes 写明;宿主应固定 minor。

一致性测试(conformance,进 CI):C1 注册后 step 返回 (bus_dim,);C2 未知模态报错; C3 时间对齐;C4 维度恒定;C5 快照只读。

读写边界(三面矩阵,2026-10-30 定稿):开放度 = 信任边界——谁为写入负责,谁才有写权。

面 谁可以写 约束
感知面 任何插件(开放写"感觉") TAAF 融合 + 逐调用审计 + 插件默认关
记忆面 只有动力学核(宿主与 LLM 都不能直写) 外部只能 propose,经两阶段队列 + 巩固门控消费
知识面 最大开放(文档、规范、数据) 但机制核心闭源,不在总线契约内

对 MCP 的三句话:只读是立场(LLM 永不直写)、propose 是阀门(两阶段队列)、 总线是唯一的门。命名纪律:不会真实写入的工具不得叫 write_*(见 ADR-0027)。

编码署名:维度无关的文本编码(z-score 标准化 top-k 群体编码)来自 EMBER 组 (Savage, 2026, arXiv:2604.12167);本仓库的 text_zscore 是该编码的后续实现并已引用, 归因方向 EMBER → BioSNN。本规范中涉及该编码的描述,署名归 EMBER 组。

许可证

Apache-2.0。文档采用 CC-BY 4.0。

Metadata

Release files for biosnn-bus 0.6.0

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

Source distribution (sdist)

Source distribution for biosnn-bus 0.6.0
File Size Uploaded Provenance Actions
biosnn_bus-0.6.0.tar.gz 69.6 kB With provenance Details for biosnn_bus-0.6.0.tar.gz

Built distribution (wheel)

Table of built distributions (wheels) for biosnn-bus 0.6.0
File Interpreter ABI Platform Actions
biosnn_bus-0.6.0-py3-none-any.whl Python 3 none any Details for biosnn_bus-0.6.0-py3-none-any.whl

Total release size: 124.4 kB

Release files / biosnn_bus-0.6.0.tar.gz

Download URL biosnn_bus-0.6.0.tar.gz
Size 69.6 kB
Tags Source
SHA-256 checksum
How to use checksums
187c7f5a5909878464d945e9d0eb7476daa8b6d157d215a29991b09ca27f140c
BLAKE2b-256 checksum
How to use checksums
0b970526e0ae1690d99d2c775bacd3ab349005acd72ce3b260109c44409d8b20
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

Release files / biosnn_bus-0.6.0-py3-none-any.whl

Download URL biosnn_bus-0.6.0-py3-none-any.whl
Size 54.9 kB
Tags Python 3
SHA-256 checksum
How to use checksums
95ac10237aadcccae9a38d4d210edc072f9aa9ad62e96d51f11785ef6a58ce03
BLAKE2b-256 checksum
How to use checksums
119205a27eb0f8a7ac7347119034738f955ee0fd06a9ede04483842ad1253b25
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
Yes
Uploaded via twine/7.0.0 CPython/3.13.16

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.

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

Security for biosnn-bus 0.6.0

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.

Full provenance

Both files in this release have been uploaded with provenance metadata.

Select from 2 files

Attestation for biosnn_bus-0.6.0.tar.gz

PyPI Publish Attestation

PyPI verified that this artifact, at this checksum, originated from the publisher listed below.

Signed by GitHub Actions, verified by PyPI on Nov 3, 2026.

Transparency log

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.

To learn more about protecting yourself from malicious actors, visit our package security documentation.

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