NIR Bridge API¶
sc_neurocore.nir_bridge
¶
NIR integration for SC-NeuroCore.
Provides bidirectional conversion between NIR graphs and SC-NeuroCore networks.
>>> import nir
>>> from sc_neurocore.nir_bridge import from_nir
>>> graph = nir.read("model.nir")
>>> network = from_nir(graph, dt=1.0)
>>> network.run(inputs, steps=100)
from_nir(source, dt=1.0, reset_mode='reset')
¶
Convert a NIR graph to an executable SC-NeuroCore network.
Parameters¶
source : nir.NIRGraph or str or Path NIR graph object, or path to a .nir file. dt : float Timestep for leaky integrator dynamics. reset_mode : str Spike reset mechanism: "reset" (v = v_reset, NIR spec default) or "subtract" (v = v - v_threshold, used by snnTorch).
Returns¶
SCNetwork Executable network with topologically sorted forward pass.
Source code in src/sc_neurocore/nir_bridge/parser.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | |
to_nir(network, path=None)
¶
Export an SC-NeuroCore SCNetwork to NIR format.
Parameters¶
network : SCNetwork The network to export. path : str or Path, optional If provided, write the NIR graph to this file.
Returns¶
nir.NIRGraph
Source code in src/sc_neurocore/nir_bridge/export.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |
Parser¶
sc_neurocore.nir_bridge.parser
¶
SCNetwork
dataclass
¶
Executable network parsed from a NIR graph.
Nodes are stored by name. Edges define the forward pass order.
Calling run() feeds input through the graph for the given
number of timesteps and returns the output node's accumulated result.
Recurrent edges (cycles) are automatically handled by inserting unit-delay nodes that feed from the previous timestep.
Source code in src/sc_neurocore/nir_bridge/parser.py
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 | |
step(inputs)
¶
Execute one timestep through the graph.
Parameters¶
inputs : dict mapping input node name → input array
Returns¶
dict mapping output node name → output array
Source code in src/sc_neurocore/nir_bridge/parser.py
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 | |
run(inputs, steps=100)
¶
Run the network for multiple timesteps.
Parameters¶
inputs : dict mapping input node name → input array (constant across steps) steps : number of timesteps
Returns¶
dict mapping output node name → list of output arrays per timestep
Source code in src/sc_neurocore/nir_bridge/parser.py
233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 | |
reset()
¶
Reset all stateful nodes.
Source code in src/sc_neurocore/nir_bridge/parser.py
252 253 254 255 256 | |
summary()
¶
Human-readable network summary.
Source code in src/sc_neurocore/nir_bridge/parser.py
258 259 260 261 262 263 264 265 266 267 268 | |
SCSubgraphNode
dataclass
¶
Executable wrapper for a nested NIR subgraph (single I/O port).
Source code in src/sc_neurocore/nir_bridge/parser.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 | |
SCMultiPortSubgraphNode
dataclass
¶
Executable wrapper for a nested NIR subgraph with multiple I/O ports.
Supports modular architectures where subgraphs expose multiple named inputs and outputs (e.g., encoder-decoder, skip connections).
Source code in src/sc_neurocore/nir_bridge/parser.py
67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 | |
forward(x)
¶
Single-input convenience: feeds x to first input, returns first output.
Source code in src/sc_neurocore/nir_bridge/parser.py
90 91 92 93 94 | |
forward_multi(inputs)
¶
Multi-port forward: provide named inputs, get named outputs.
Source code in src/sc_neurocore/nir_bridge/parser.py
96 97 98 | |
from_nir(source, dt=1.0, reset_mode='reset')
¶
Convert a NIR graph to an executable SC-NeuroCore network.
Parameters¶
source : nir.NIRGraph or str or Path NIR graph object, or path to a .nir file. dt : float Timestep for leaky integrator dynamics. reset_mode : str Spike reset mechanism: "reset" (v = v_reset, NIR spec default) or "subtract" (v = v - v_threshold, used by snnTorch).
Returns¶
SCNetwork Executable network with topologically sorted forward pass.
Source code in src/sc_neurocore/nir_bridge/parser.py
271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 | |
Recurrent Edge Handling¶
Graphs with cycles (feedback connections) are automatically handled by
inserting unit-delay nodes on back edges. The delay node buffers the
previous timestep's value, breaking algebraic loops while preserving
temporal dynamics. See _UnitDelayNode.
Multi-Port Subgraphs¶
Nested NIR graphs with multiple inputs/outputs use SCMultiPortSubgraphNode,
which exposes forward_multi(inputs_dict) → outputs_dict for named I/O ports.
Node Map¶
sc_neurocore.nir_bridge.node_map
¶
NODE_MAP = {nir.Input: lambda name, node, **kw: SCInputNode(name=name, shape=(tuple((int(x)) for x in (next(iter(node.input_type.values())).flatten())) if node.input_type else ())), nir.Output: lambda name, node, **kw: SCOutputNode(name=name, shape=(tuple((int(x)) for x in (next(iter(node.output_type.values())).flatten())) if node.output_type else ())), nir.LIF: lambda name, node, **kw: SCLIFNode.from_nir(name, node, dt=(kw.get('dt', 1.0)), reset_mode=(kw.get('reset_mode', 'reset'))), nir.IF: lambda name, node, **kw: SCIFNode.from_nir(name, node, dt=(kw.get('dt', 1.0)), reset_mode=(kw.get('reset_mode', 'reset'))), nir.LI: lambda name, node, **kw: SCLINode.from_nir(name, node, dt=(kw.get('dt', 1.0))), nir.I: lambda name, node, **kw: SCIntegratorNode.from_nir(name, node, dt=(kw.get('dt', 1.0))), nir.Affine: lambda name, node, **kw: SCAffineNode.from_nir(name, node), nir.Linear: lambda name, node, **kw: SCLinearNode.from_nir(name, node), nir.Scale: lambda name, node, **kw: SCScaleNode.from_nir(name, node), nir.Threshold: lambda name, node, **kw: SCThresholdNode.from_nir(name, node), nir.Flatten: lambda name, node, **kw: SCFlattenNode.from_nir(name, node), nir.Delay: lambda name, node, **kw: SCDelayNode.from_nir(name, node, dt=(kw.get('dt', 1.0))), nir.CubaLIF: lambda name, node, **kw: SCCubaLIFNode.from_nir(name, node, dt=(kw.get('dt', 1.0)), reset_mode=(kw.get('reset_mode', 'reset'))), nir.CubaLI: lambda name, node, **kw: SCCubaLINode.from_nir(name, node, dt=(kw.get('dt', 1.0))), nir.SumPool2d: lambda name, node, **kw: SCSumPool2dNode.from_nir(name, node), nir.AvgPool2d: lambda name, node, **kw: SCAvgPool2dNode.from_nir(name, node), nir.Conv1d: lambda name, node, **kw: SCConv1dNode.from_nir(name, node), nir.Conv2d: lambda name, node, **kw: SCConv2dNode.from_nir(name, node)}
module-attribute
¶
SCLIFNode
dataclass
¶
LIF neuron mapped from NIR LIF primitive.
NIR LIF: taudv/dt = (v_leak - v) + RI, spike when v > v_threshold Euler: v += ((v_leak - v) + R*I) * dt/tau
Source code in src/sc_neurocore/nir_bridge/node_map.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 | |
SCIFNode
dataclass
¶
IF neuron — integrator with threshold, no leak.
NIR IF: dv/dt = RI, spike when v > v_threshold Euler: v += RI*dt
Source code in src/sc_neurocore/nir_bridge/node_map.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 | |
SCLINode
dataclass
¶
Leaky integrator — LIF without threshold.
NIR LI: taudv/dt = (v_leak - v) + RI
Source code in src/sc_neurocore/nir_bridge/node_map.py
192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 | |
SCIntegratorNode
dataclass
¶
Pure integrator: dv/dt = RI (no leak, no threshold). Euler: v += RI*dt
Source code in src/sc_neurocore/nir_bridge/node_map.py
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
SCAffineNode
dataclass
¶
Dense linear transform with bias: y = Wx + b
Source code in src/sc_neurocore/nir_bridge/node_map.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |
SCLinearNode
dataclass
¶
Matrix multiply without bias: y = Wx
Source code in src/sc_neurocore/nir_bridge/node_map.py
256 257 258 259 260 261 262 263 264 265 266 267 268 269 | |
SCScaleNode
dataclass
¶
Element-wise scaling: y = s * x
Source code in src/sc_neurocore/nir_bridge/node_map.py
272 273 274 275 276 277 278 279 280 281 282 283 284 | |
SCThresholdNode
dataclass
¶
Spike threshold: y = 1 if x >= threshold else 0
Source code in src/sc_neurocore/nir_bridge/node_map.py
287 288 289 290 291 292 293 294 295 296 297 298 299 | |
SCFlattenNode
dataclass
¶
Reshape tensor — flatten dimensions.
Source code in src/sc_neurocore/nir_bridge/node_map.py
302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 | |
SCInputNode
dataclass
¶
Graph entry point — passes input through unchanged.
Source code in src/sc_neurocore/nir_bridge/node_map.py
21 22 23 24 25 26 27 28 29 | |
SCOutputNode
dataclass
¶
Graph exit point — collects output.
Source code in src/sc_neurocore/nir_bridge/node_map.py
32 33 34 35 36 37 38 39 40 41 42 | |
SCDelayNode
dataclass
¶
Temporal delay: output = input(t - delay).
NIR Delay: I(t - tau). Implemented as a circular buffer per element. Delay values are rounded to integer timesteps.
Source code in src/sc_neurocore/nir_bridge/node_map.py
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 | |
SCCubaLIFNode
dataclass
¶
Current-based LIF with synaptic filter.
tau_syn * dI_syn/dt = -I_syn + w_in * I
tau_mem * dv/dt = (v_leak - v) + R * I_syn spike when v > v_threshold, reset to v_reset
Source code in src/sc_neurocore/nir_bridge/node_map.py
404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 | |
SCCubaLINode
dataclass
¶
Current-based leaky integrator (CubaLIF without threshold).
tau_syn * dI_syn/dt = -I_syn + w_in * I
tau_mem * dv/dt = (v_leak - v) + R * I_syn
Source code in src/sc_neurocore/nir_bridge/node_map.py
497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 | |
SCConv1dNode
dataclass
¶
1D convolution: y = conv1d(x, weight) + bias.
Source code in src/sc_neurocore/nir_bridge/node_map.py
643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 | |
SCConv2dNode
dataclass
¶
2D convolution: y = conv2d(x, weight) + bias.
Source code in src/sc_neurocore/nir_bridge/node_map.py
699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 | |
SCSumPool2dNode
dataclass
¶
2D sum pooling: sum over spatial kernel windows.
Source code in src/sc_neurocore/nir_bridge/node_map.py
565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 | |
SCAvgPool2dNode
dataclass
¶
2D average pooling: SumPool / kernel_area.
Source code in src/sc_neurocore/nir_bridge/node_map.py
609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 | |
map_node(name, node, **kwargs)
¶
Convert a single NIR node to its SC-NeuroCore equivalent.
Source code in src/sc_neurocore/nir_bridge/node_map.py
805 806 807 808 809 810 811 812 | |
Export¶
sc_neurocore.nir_bridge.export
¶
to_nir(network, path=None)
¶
Export an SC-NeuroCore SCNetwork to NIR format.
Parameters¶
network : SCNetwork The network to export. path : str or Path, optional If provided, write the NIR graph to this file.
Returns¶
nir.NIRGraph
Source code in src/sc_neurocore/nir_bridge/export.py
137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 | |