SC-NIR API¶
SC-NIR is the SC-NeuroCore metadata layer for stochastic-computing semantics that plain NIR does not encode. It records bitstream length, stochastic encoding, stream signal kind, fixed-point precision, deterministic transform metadata, optional online-learning contracts for weight streams, random-source metadata, stream correlation constraints, and explicit hierarchy instance boundaries before a model reaches hardware compilation or experiment handoff.
The schema is intentionally strict. Unknown fields, missing fields, duplicate stream identifiers, invalid random-source metadata, dangling correlation references, and hierarchy ports that do not match an existing stream are rejected rather than ignored.
JSON Schema¶
The reference schema is tracked at:
schemas/scnir/scnir.schema.json
Current schema version:
sc-neurocore.scnir.v0.7
Each stream entry must provide:
| Field | Purpose |
|---|---|
stream_id |
Stable identifier used by correlation constraints |
layer |
Producer layer or graph node name |
bitstream_length |
Positive integer SC stream length |
encoding |
unipolar, bipolar, low-discrepancy, replay, LFSR, or hardware-source encoding |
signal_kind |
Logical stream role: spike, analogue_state, or weight |
delay_steps |
Explicit unit-delay count as a scalar integer or exact source-width integer vector; feed-forward streams use 0 |
transforms |
Ordered deterministic transforms applied to the stream, currently threshold comparators |
precision |
Signedness, total bits, fractional bits, accumulator bits, rounding, overflow |
source |
LFSR, Sobol, Halton, replay, or hardware source metadata |
correlation_constraints |
Pairwise policy metadata between streams |
online_learning |
null or a validated Online O(1) learning annotation on weight streams only |
The top-level hierarchy array records bounded nested-hardware contracts. Each
entry must provide a stable instance_id, synthesisable module_name, and at
least one port. Each port records port_name, direction, stream_id,
signal_kind, and bit_width; the referenced stream must exist and its
signal_kind must match the port. Single-input/single-output nested NIR graphs
that are inlined for flat HDL lowering preserve their original boundary as a
hierarchy instance, with ports generated from the SC-NIR streams produced by
the inlined contents.
CLI¶
Validate a document:
sc-neurocore scnir validate model.scnir.json
Upgrade a supported document to the current canonical schema:
sc-neurocore scnir upgrade model.scnir.json --output upgraded.scnir.json
The sc-neurocore.scnir.v0.1 upgrade path adds explicit delay_steps=0 to
legacy streams, every pre-v0.3 upgrade adds signal_kind inferred from the
stream identifier, and every pre-v0.4 upgrade adds an explicit empty
transforms list. Version v0.5 keeps scalar delay metadata compatible and
adds per-source delay vectors for heterogeneous NIR Delay lowering. Version
v0.6 adds top-level hierarchy instance and port metadata; legacy documents
upgrade with an empty hierarchy list. Version v0.7 adds optional
online_learning annotations for weight streams and rejects those annotations
on spike or analogue-state streams. The typed validator and deterministic JSON
writer then produce sc-neurocore.scnir.v0.7. Unknown schema versions fail
closed so migration support must be added deliberately when the schema evolves.
Export SC-NIR metadata from a NIR graph:
sc-neurocore scnir export model.nir --output model.scnir.json --T 1024
Exit code 0 means the document passed the SC-NIR validator. Exit code 1
means validation, upgrade, export, or input loading failed.
FPGA Compilation Integration¶
compile_network_to_fpga(...) constructs SC-NIR metadata for the lowered
NeuronGraph before emitting top-level RTL. The returned
NetworkCompilationResult exposes scnir_document,
scnir_source_modules, and scnir_source_manifest. The generated top module
includes deterministic handoff localparams:
localparam integer SCNIR_BITSTREAM_LENGTH = 1024;
localparam integer SCNIR_STREAM_COUNT = 2;
localparam integer SCNIR_SOURCE_MODULE_COUNT = 2;
scnir_source_modules is keyed by emitted Verilog module name and contains the
standalone LFSR-16 or Sobol-16 source RTL generated from each SC-NIR stream.
The compile-nir CLI writes the full validated document as
scnir_document.json in the output directory so dense exported-network runs
can be reproduced from the same stream metadata that drove source generation.
scnir_source_manifest records the stream identifier, module name, source
family, seed, bitstream length, encoding, signal kind, recurrent delay steps,
precision, transform metadata, optional online-learning metadata, and
source-specific metadata used for each module. CLI manifests also record the
selected interconnect, Q-format, total
neuron count, total synapse count, SC-NIR stream count,
scnir_signal_kinds counts, scnir_signal_routes, and
scnir_external_inputs so AER/event-driven and mixed analogue/spiking output
directories carry machine-readable compile evidence. FPGA compilation
marks non-spiking LI/CubaLI/integrator population streams as
analogue_state, so mixed analogue/spiking NIR graphs expose voltage-state
handoff metadata instead of being mislabeled as spike streams; combined mixed
AER graphs record analogue-state streams as direct MAC routes and spike streams
as weighted event routes. FPGA
compilation currently materialises LFSR-16 and Sobol-16 source families
because both expose the standard threshold[15:0]/bit_out contract;
unsupported source families fail closed instead of being emitted through an
incompatible HDL interface.
NIR Primitive Compatibility¶
The executable compatibility matrix is exposed by
scnir_compatibility_matrix() and checked against the parser's declared
NODE_MAP support. It separates parser execution from SC-NIR/FPGA handoff so
documentation cannot claim hardware closure for primitives that are currently
parser-only or only closed under a bounded shape/port contract.
| NIR primitive | SC-NIR / FPGA level | Stream metadata | HDL handoff |
|---|---|---|---|
Input, Output |
boundary | none | external input/output buses |
LIF, IF, CubaLIF |
metadata and HDL | signal_kind=spike, encoding=unipolar |
canonical ODE module plus direct or AER interconnect |
LI, CubaLI, I |
metadata and HDL | signal_kind=analogue_state, encoding=bipolar |
canonical ODE or integrator state-update module with direct analogue-state MAC routing |
Affine, Linear |
metadata and HDL | signal_kind=weight, encoding=bipolar; recurrent or explicit delayed streams carry delay_steps |
weight ROM plus direct or weighted-event interconnect |
Scale |
metadata and HDL when adjacent to Affine/Linear |
folded into the downstream weight stream as connection gain | folded fixed-point gain in direct/AER weight terms |
Flatten |
metadata and HDL when exact shape metadata preserves element count adjacent to Affine/Linear |
folded into the downstream weight stream as shape_preserving_flatten |
fixed-point weight indexing with exact flattened width checks |
Threshold |
metadata and HDL when adjacent to Affine/Linear with scalar or exact-width thresholds |
weight stream carries a threshold transform with source or destination position |
fixed-point comparator before weighted-event contribution or destination current |
Delay |
metadata and HDL for scalar or exact source-width source-side delays feeding Affine/Linear population connections |
downstream weight stream carries scalar delay_steps>=0 or vector delay_steps=[...] |
direct-interconnect register chains with per-source delay taps for spike and analogue-state sources |
Conv1d |
metadata and HDL when input_shape is explicit and output is flattened into a destination population |
lowered to a signal_kind=weight stream as convolution_lowered_weight |
dense Toeplitz-style fixed-point MAC terms through the weight path |
Conv2d |
metadata and HDL when exact spatial input shape is explicit and output is flattened into a destination population | lowered to a signal_kind=weight stream as convolution_lowered_weight |
dense 2D convolution fixed-point MAC terms through the weight path |
SumPool2d, AvgPool2d |
metadata and HDL when exact CHW shape metadata is present and output is flattened into a destination population | lowered to a signal_kind=weight stream as pool2d_lowered_weight |
dense pooling fixed-point MAC terms through the weight path |
nested NIRGraph |
metadata and HDL for single-input/single-output subgraphs and exact one-edge-per-port multi-port subgraphs, including exact multi-output boundaries, inlined into the parent graph; ambiguous multi-port hierarchy fails closed | namespaced stream IDs from the inlined subgraph contents plus a top-level hierarchy instance whose ports reference those streams | namespaced inline fixed-point terms with stable external input-bus lanes plus standalone hierarchy boundary module artefacts, top-level contract instances, and packed hierarchy weight outputs consumed by the top-level MAC |
Use validate_scnir_compatibility_matrix() in tests or release checks to fail
when parser support changes without a corresponding compatibility row. Use
build_scnir_compatibility_audit() for release evidence bundles that need the
validated matrix, support-level counts, explicit parser-only and metadata-only
closure blocker lists, the locally closed handoff primitive list, the exact
evidence file set, file sizes, per-evidence SHA-256 digests, and a canonical
matrix SHA-256 digest in one versioned JSON object. A
closed_for_local_handoff audit status is limited to the SC-NIR/HDL software
handoff and is emitted with external_hardware_evidence_status=not_claimed
until separate Vivado, PYNQ, or physical hardware evidence is attached.
Python API¶
from sc_neurocore.ir import (
SCNIR_HDL_HANDOFF_MANIFEST_VERSION,
SCNIR_PREVIOUS_SCHEMA_VERSION,
SCNIR_SCHEMA_VERSION,
SCNIR_COMPATIBILITY_AUDIT_VERSION,
SCNIRCompatibilityRow,
SCNIRConversionConfig,
SCNIRDocument,
SCNIRHierarchyInstance,
SCNIRHierarchyPort,
SCNIRPrecision,
SCNIRSignalKind,
SCNIRSource,
SCNIRStream,
build_scnir_compatibility_audit,
build_scnir_source_bundle,
build_scnir_from_neuron_graph,
export_scnir_from_nir,
load_scnir,
scnir_compatibility_matrix,
scnir_compatibility_matrix_dicts,
validate_scnir_compatibility_matrix,
validate_scnir_dict,
write_scnir,
upgrade_scnir_dict,
)
sc_neurocore.ir.scnir_schema
¶
SC-aware NIR metadata schema and validator.
The SC-NIR layer records stochastic-computing semantics that plain NIR does not carry: stream length, encoding, fixed-point precision, random-source metadata, deterministic stream transforms, and correlation constraints. Validation is intentionally fail-closed so unrecognised or under-specified metadata cannot silently reach hardware generation.
SCNIR_SCHEMA_VERSION = 'sc-neurocore.scnir.v0.7'
module-attribute
¶
SCNIR_PREVIOUS_SCHEMA_VERSION = 'sc-neurocore.scnir.v0.5'
module-attribute
¶
SCNIRSignalKind = Literal['spike', 'analogue_state', 'weight']
module-attribute
¶
SCNIRValidationError
¶
Bases: ValueError
Raised when an SC-NIR payload violates the fail-closed contract.
Source code in src/sc_neurocore/ir/scnir_schema.py
| Python | |
|---|---|
103 104 | |
SCNIRPrecision
dataclass
¶
Fixed-point interpretation attached to one stochastic stream.
Source code in src/sc_neurocore/ir/scnir_schema.py
| Python | |
|---|---|
107 108 109 110 111 112 113 114 115 116 | |
SCNIRSource
dataclass
¶
Random or deterministic source metadata for a stochastic stream.
Source code in src/sc_neurocore/ir/scnir_schema.py
| Python | |
|---|---|
119 120 121 122 123 124 125 126 127 128 129 130 | |
SCNIRCorrelationConstraint
dataclass
¶
Correlation rule between two stochastic streams.
Source code in src/sc_neurocore/ir/scnir_schema.py
| Python | |
|---|---|
133 134 135 136 137 138 139 140 | |
SCNIRHierarchyPort
dataclass
¶
One typed port on a hierarchical SC-NIR hardware instance.
Source code in src/sc_neurocore/ir/scnir_schema.py
| Python | |
|---|---|
170 171 172 173 174 175 176 177 178 | |
SCNIRHierarchyInstance
dataclass
¶
One hierarchy instance boundary for future nested hardware handoff.
Source code in src/sc_neurocore/ir/scnir_schema.py
| Python | |
|---|---|
181 182 183 184 185 186 187 | |
SCNIRStream
dataclass
¶
SC metadata for one logical stochastic bitstream.
Source code in src/sc_neurocore/ir/scnir_schema.py
| Python | |
|---|---|
153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 | |
SCNIRDocument
dataclass
¶
Top-level SC-NIR metadata document.
Source code in src/sc_neurocore/ir/scnir_schema.py
| Python | |
|---|---|
190 191 192 193 194 195 196 197 | |
validate_scnir_dict(payload)
¶
Validate a decoded SC-NIR payload or raise SCNIRValidationError.
Source code in src/sc_neurocore/ir/scnir_schema.py
| Python | |
|---|---|
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 | |
scnir_from_dict(payload)
¶
Build a typed SC-NIR document from a decoded mapping.
Source code in src/sc_neurocore/ir/scnir_schema.py
| Python | |
|---|---|
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 | |
scnir_to_dict(document)
¶
Convert a typed SC-NIR document to deterministic JSON-ready data.
Source code in src/sc_neurocore/ir/scnir_schema.py
| Python | |
|---|---|
267 268 269 270 271 272 273 274 275 276 277 | |
load_scnir(path)
¶
Load and validate an SC-NIR JSON document.
Source code in src/sc_neurocore/ir/scnir_schema.py
| Python | |
|---|---|
280 281 282 283 284 | |
write_scnir(path, document)
¶
Write an SC-NIR JSON document after validating it.
Source code in src/sc_neurocore/ir/scnir_schema.py
| Python | |
|---|---|
287 288 289 290 291 292 293 294 | |
upgrade_scnir_dict(payload)
¶
Upgrade supported SC-NIR payloads to the current canonical schema.
Version v0.1 did not encode recurrent connection delay, and versions
before v0.3 did not distinguish spiking, analogue-state, and weight
streams. Version v0.4 added explicit stream transform metadata for
threshold comparators. Version v0.5 permits delay_steps to be
either a scalar integer or a per-source-column integer vector. Version
v0.6 adds top-level hierarchy instance and port metadata. Version
v0.7 adds optional validated per-weight-stream online-learning
annotations. Legacy upgrades insert the missing fields before validating
through the typed schema. Current documents are canonicalised through the
same deterministic writer.
Source code in src/sc_neurocore/ir/scnir_schema.py
| Python | |
|---|---|
297 298 299 300 301 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 335 336 337 338 339 340 341 342 343 344 | |
sc_neurocore.ir.scnir_convert
¶
Conversion utilities that attach SC-NIR metadata to NIR-derived graphs.
SCNIRConversionConfig
dataclass
¶
Configuration for deterministic SC-NIR metadata export.
Source code in src/sc_neurocore/ir/scnir_convert.py
| Python | |
|---|---|
43 44 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 | |
resolved_accumulator_bits
property
¶
Accumulator width used by exported precision metadata.
build_scnir_from_neuron_graph(neuron_graph, *, config)
¶
Build an SC-NIR document from an existing NIR-derived NeuronGraph.
Source code in src/sc_neurocore/ir/scnir_convert.py
| Python | |
|---|---|
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 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 | |
export_scnir_from_nir(model_path, *, output_path, config, dt=1.0)
¶
Read a NIR model, export SC-NIR metadata, and write it to JSON.
Source code in src/sc_neurocore/ir/scnir_convert.py
| Python | |
|---|---|
285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 | |
sc_neurocore.ir.scnir_compatibility
¶
Executable compatibility matrix for NIR primitives in the SC-NIR pipeline.
The NIR parser supports more primitives than the current SC-NIR to FPGA handoff path can lower. This module makes that distinction explicit and machine-checkable so documentation, tests, and future closure gates cannot silently over-claim support.
SCNIR_COMPATIBILITY_AUDIT_VERSION = 'sc-neurocore.scnir.compatibility-audit.v0.2'
module-attribute
¶
SCNIRCompatibilityRow
dataclass
¶
One compatibility row for a NIR primitive.
Source code in src/sc_neurocore/ir/scnir_compatibility.py
| Python | |
|---|---|
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 | |
as_dict()
¶
Return a deterministic JSON-ready row.
Source code in src/sc_neurocore/ir/scnir_compatibility.py
| Python | |
|---|---|
49 50 51 52 53 54 55 56 | |
scnir_compatibility_matrix()
¶
Return the deterministic SC-NIR compatibility matrix.
Source code in src/sc_neurocore/ir/scnir_compatibility.py
| Python | |
|---|---|
335 336 337 338 | |
scnir_compatibility_matrix_dicts()
¶
Return the matrix as deterministic JSON-ready dictionaries.
Source code in src/sc_neurocore/ir/scnir_compatibility.py
| Python | |
|---|---|
341 342 343 344 | |
build_scnir_compatibility_audit(evidence_root)
¶
Build a versioned closure-audit report for the SC-NIR compatibility matrix.
The report is intentionally derived from the executable matrix after validation, so release automation consumes the same data that enforces parser coverage and evidence-path freshness.
Source code in src/sc_neurocore/ir/scnir_compatibility.py
| Python | |
|---|---|
347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 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 402 403 404 405 406 407 408 409 410 411 412 413 414 | |
validate_scnir_compatibility_matrix(evidence_root=None)
¶
Fail if the matrix drifts from parser-declared support or stale evidence paths.
Parameters¶
evidence_root:
Optional repository root used to verify that every audit_evidence
path in the matrix resolves to an existing file.
Source code in src/sc_neurocore/ir/scnir_compatibility.py
| Python | |
|---|---|
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 | |
sc_neurocore.ir.scnir_hdl
¶
Materialise SC-NIR source metadata into concrete HDL artefacts.
SCNIRHDLSourceManifestEntry
dataclass
¶
Serialisable manifest row for one emitted stochastic source module.
Source code in src/sc_neurocore/ir/scnir_hdl.py
| Python | |
|---|---|
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 | |
as_dict()
¶
Return a deterministic JSON-ready representation.
Source code in src/sc_neurocore/ir/scnir_hdl.py
| Python | |
|---|---|
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 | |
SCNIRHDLSourceBundle
dataclass
¶
Concrete HDL source modules plus the manifest that explains them.
Source code in src/sc_neurocore/ir/scnir_hdl.py
| Python | |
|---|---|
74 75 76 77 78 79 80 81 82 83 84 | |
manifest_dicts()
¶
Return deterministic JSON-ready manifest rows.
Source code in src/sc_neurocore/ir/scnir_hdl.py
| Python | |
|---|---|
81 82 83 84 | |
build_scnir_source_bundle(document)
¶
Emit deterministic HDL source modules for every SC-NIR stream.
Only source kinds with the standard threshold-bit output contract are materialised here. Unsupported SC-NIR source kinds fail closed instead of being lowered to semantically incompatible RTL.
Source code in src/sc_neurocore/ir/scnir_hdl.py
| Python | |
|---|---|
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 | |
sc_neurocore.ir.scnir_handoff_audit
¶
Executable audit checks for compile-nir SC-NIR HDL handoff artefacts.
SCNIR_HDL_HANDOFF_MANIFEST_VERSION = 'sc-neurocore.scnir.hdl-sources.v0.2'
module-attribute
¶
SCNIRHDLHandoffAuditReport
dataclass
¶
Deterministic summary of a validated SC-NIR HDL handoff directory.
Source code in src/sc_neurocore/ir/scnir_handoff_audit.py
| Python | |
|---|---|
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 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 | |
as_dict()
¶
Return a stable JSON-ready report.
Source code in src/sc_neurocore/ir/scnir_handoff_audit.py
| Python | |
|---|---|
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 | |
audit_scnir_hdl_handoff(directory)
¶
Validate a compile-nir HDL output directory and return an audit report.
The audit is intentionally structural and fail-closed: every SC-NIR stream must have exactly one matching source-manifest row and emitted source module, aggregate counts must match the typed document, and top-level SC-NIR localparams must agree with the JSON handoff metadata.
Source code in src/sc_neurocore/ir/scnir_handoff_audit.py
| Python | |
|---|---|
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 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 | |
write_scnir_hdl_handoff_audit(directory, output_path)
¶
Validate a handoff directory and write the JSON audit report.
Source code in src/sc_neurocore/ir/scnir_handoff_audit.py
| Python | |
|---|---|
186 187 188 189 190 191 192 193 194 195 196 197 | |