Export¶
Model export to standard interchange formats.
Two ONNX-oriented export paths are maintained:
SCOnnxExporter— file-oriented exporter for SC networks that writes ONNX protobuf files when the optionalonnxdependency is installed and JSON sidecars for lightweight deployment.sc_neurocore.export.onnx_export.ONNXExporter— dependency-free graph exporter for SC-IR-style nodes. It emits a JSON-serializableONNXGraphenvelope using the customsc.neurocoredomain.
from sc_neurocore.export import SCOnnxExporter
exporter = SCOnnxExporter()
exporter.export(model, "model.onnx")
For dependency-free graph export, use the SC-IR graph exporter directly:
from sc_neurocore.export.onnx_export import ONNXExporter
graph = ONNXExporter().export(ir_graph, {"input_a": (128, 1024)})
payload = graph.to_dict()
Final graph output metadata follows the actual final emitted node. For example,
a final SC_POPCOUNT node produces an ONNX int32 tensor (elem_type=6),
while stochastic bitstream outputs remain bool tensors (elem_type=9).
Mapped SC-IR node types that do not have a shape inference rule fail closed
instead of silently emitting a guessed (1,) output.
For MLIR/SSA lowering, use the compiler exporter on the same graph-style surface:
from sc_neurocore.export.compiler_export import CompilerExporter
mlir_text = CompilerExporter().export_to_mlir(ir_graph, {"input_a": (128, 1024)})
CompilerExporter supports the mlir target and validates the graph before
emission. Empty graphs, duplicate node IDs, duplicate output edges, unsupported
node types, wrong node arity, missing external input shapes, non-positive tensor
dimensions, and output names that collide with graph inputs raise ValueError
before SSA text is emitted. MLIR-facing input names are validated with the
shared HDL identifier guard; invalid identifiers fail closed instead of being
rewritten.
sc_neurocore.export.onnx_exporter
¶
ONNX export for SC networks.
Supports two formats:
- .onnx (protobuf) — standard ONNX ModelProto via onnx library
- .json — legacy JSON schema (no external dependencies)
SC layers use stochastic bitstream ops not in the ONNX standard.
We map them to a custom domain sc_neurocore with op types
SC_Dense and SC_Custom.
SCOnnxExporter
¶
Export SC networks to ONNX protobuf or legacy JSON.
Source code in src/sc_neurocore/export/onnx_exporter.py
| Python | |
|---|---|
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 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 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 | |
export(layers, filename)
staticmethod
¶
Export layers to filename.
File extension selects format: .onnx → protobuf,
anything else → legacy JSON.
Source code in src/sc_neurocore/export/onnx_exporter.py
| Python | |
|---|---|
44 45 46 47 48 49 50 51 52 53 54 | |
sc_neurocore.export.onnx_export
¶
Zero-dependency ONNX exporter for SC-NeuroCore IR graphs.
Maps SC-IR nodes to ONNX-compatible graph representation with custom
operator set sc.neurocore. No ONNX runtime or protobuf dependency
required — emits a self-contained dict-based graph that can be
serialized to JSON or consumed by downstream tools.
ONNXTensorType
dataclass
¶
Tensor element type and static shape for the JSON ONNX model.
Parameters¶
elem_type: ONNX tensor element type id. shape: Static tensor dimensions.
Source code in src/sc_neurocore/export/onnx_export.py
| Python | |
|---|---|
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 | |
to_dict()
¶
Return the ONNX tensor-type dictionary representation.
Source code in src/sc_neurocore/export/onnx_export.py
| Python | |
|---|---|
43 44 45 46 47 48 | |
ONNXNode
dataclass
¶
Custom-domain ONNX node for a lowered stochastic-computing operation.
Parameters¶
op_type: ONNX operator type. domain: Operator domain. inputs: Input tensor names. outputs: Output tensor names. name: Stable node name. attributes: Optional scalar operator attributes.
Source code in src/sc_neurocore/export/onnx_export.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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
to_dict()
¶
Return the ONNX node dictionary representation.
Source code in src/sc_neurocore/export/onnx_export.py
| Python | |
|---|---|
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
ONNXGraph
dataclass
¶
JSON-serializable ONNX model envelope.
Parameters¶
name: ONNX graph name. nodes: Lowered ONNX nodes. inputs: Named graph inputs and tensor types. outputs: Named graph outputs and tensor types. metadata: String metadata entries attached to the model.
Source code in src/sc_neurocore/export/onnx_export.py
| Python | |
|---|---|
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 | |
to_dict()
¶
Return the complete ONNX model dictionary representation.
Source code in src/sc_neurocore/export/onnx_export.py
| Python | |
|---|---|
119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 | |
to_json(indent=2)
¶
Return the complete ONNX model as formatted JSON.
Source code in src/sc_neurocore/export/onnx_export.py
| Python | |
|---|---|
142 143 144 | |
ONNXExporter
¶
Export SC-NeuroCore IR graphs to ONNX-compatible dictionaries.
Parameters¶
graph_name: Name assigned to the emitted ONNX graph.
Source code in src/sc_neurocore/export/onnx_export.py
| Python | |
|---|---|
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 | |
export(ir_graph, input_shapes, metadata=None)
¶
Convert an SC-IR graph to an ONNX graph representation.
Parameters¶
ir_graph:
SC-IR graph-like object with a nodes sequence.
input_shapes:
Mapping from input tensor names to static dimensions.
metadata:
Optional string metadata to attach to the emitted graph.
Returns¶
ONNXGraph JSON-serializable ONNX graph envelope.
Raises¶
ValueError If a mapped SC-IR operator has no shape inference rule.
Source code in src/sc_neurocore/export/onnx_export.py
| Python | |
|---|---|
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 | |
sc_neurocore.export.compiler_export
¶
SSA-based TVM/MLIR compiler frontend for SC-NeuroCore IR graphs.
Exports SNN dataflow graphs to MLIR text via topological traversal with strict SSA register allocation and shape inference.
SSAEnvironment
¶
Manages Static Single Assignment (SSA) registers for MLIR/Relay.
Source code in src/sc_neurocore/export/compiler_export.py
| Python | |
|---|---|
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
allocate(edge_name)
¶
Allocate and bind the next SSA register for an SC-IR edge.
Source code in src/sc_neurocore/export/compiler_export.py
| Python | |
|---|---|
59 60 61 62 63 64 | |
get(edge_name)
¶
Return an allocated register or validate an external input register.
Source code in src/sc_neurocore/export/compiler_export.py
| Python | |
|---|---|
66 67 68 69 70 | |
ShapeInference
¶
Infers tensor shapes dynamically across the SNN graph.
Source code in src/sc_neurocore/export/compiler_export.py
| Python | |
|---|---|
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 | |
infer(node)
¶
Infer and store the output shape for one supported SC-IR node.
Source code in src/sc_neurocore/export/compiler_export.py
| Python | |
|---|---|
79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 | |
CompilerExporter
¶
Export SC-IR graph-like objects to strict SSA MLIR text.
Source code in src/sc_neurocore/export/compiler_export.py
| Python | |
|---|---|
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 | |
__init__(target='mlir')
¶
Create an exporter for a supported compiler backend target.
Source code in src/sc_neurocore/export/compiler_export.py
| Python | |
|---|---|
110 111 112 113 114 115 116 117 | |
export_to_mlir(ir_graph, input_shapes)
¶
Emit strict SSA MLIR text via topological traversal.
Source code in src/sc_neurocore/export/compiler_export.py
| Python | |
|---|---|
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 | |