Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions docs/source/examples/spikeforge/nir-conversion.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
# spikeforge

[spikeforge](https://github.com/Capsize-Games/spikeforge) is a toolkit for
building, training, and deploying spiking neural networks. Its NIR bridge can
export a topology, write the graph to a NIR file, and compare the topology's
output with an independent NIR interpreter.

Install spikeforge with its NIR dependencies before running the example:

```shell
pip install "spikeforge[nir]"
```

## Export a topology to NIR

This example uses SpikeForge's deterministic `conv_net` fixture. It does not
train a model or download a dataset.

```python
import nir

from spikeforge.cli import fixture
from spikeforge.nir_bridge import graph_summary, to_nir

topology = "conv_net"
spec, module, spikes = fixture.synthetic_input(
topology, steps=4, batch=1, seed=0
)

graph = to_nir(spec, module)
nir.write("conv_net.nir", graph)

summary = graph_summary(graph)
print("topology:", topology, "| input:", tuple(spikes.shape))
print("nodes:", len(summary["nodes"]))
print("kinds:", [node["kind"] for node in summary["nodes"]])
```

`graph_summary` returns the node and edge information without tensor objects,
so it can also be used when a graph needs to be inspected or sent over a
protocol.

## Validate the exported graph

SpikeForge can run the same input through the source module and the exported
graph. The validation report includes per-layer drift and an overall
`within_tolerance` result.

```python
from spikeforge.nir_bridge import validate

report = validate(spec, module, spikes, graph=graph)
print("within_tolerance:", report["within_tolerance"])
print("worst:", report["worst"])
```

The `graph` argument makes the comparison use the graph created in the first
section. The interpreter is separate from the source module, so this checks
the exported representation rather than only checking that export succeeded.
1 change: 1 addition & 0 deletions docs/source/myst.yml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ project:
- file: examples/rockpool/nir-conversion.ipynb
- file: examples/sinabs/nir-conversion.ipynb
- file: examples/snntorch/nir-conversion.ipynb
- file: examples/spikeforge/nir-conversion.md
- file: examples/spinnaker2/import.ipynb
- file: examples/spyx/conversion.ipynb
- file: examples/snntorch_to_norse.ipynb
Expand Down
Loading