Optimiser API¶
The sc_neurocore.optimizer package exposes two optimisation surfaces:
fit_to_target(...)for deterministic resource fitting against FPGA target budgets.SurrogateSCOptimizerfor ML-guided stochastic-computing compiler choices using generated analytical design points plus optional measured observations.
It also exposes strict evidence loaders so benchmark and synthesis reports can feed the surrogate without fabricating missing LUT, power, latency, or accuracy values.
Deterministic Resource Fitting¶
Use fit_to_target when you have layer dimensions and weights and want the
legacy prune / quantise / bitstream-length loop to reduce estimated FPGA
resource use:
import numpy as np
from sc_neurocore.optimizer import fit_to_target
weights = [np.ones((32, 16), dtype=np.float32)]
result = fit_to_target(
layer_sizes=[(32, 16)],
weights=weights,
target="ice40",
initial_bitstream_length=256,
)
print(result.summary())
The result reports whether the design fits, final estimated LUT use, selected bitstream length, sparsity, and each optimisation step.
Surrogate-Guided SC Optimisation¶
Use SurrogateSCOptimizer when the compiler must choose per-layer stochastic
computing settings under LUT, power, and latency pressure:
from sc_neurocore.optimizer import SurrogateSCOptimizer, TargetHardwareProfile
from sc_neurocore.optimizer.sc_optimizer import HardwareBudget, LayerProfile
target = TargetHardwareProfile(
name="ice40-low-power",
budget=HardwareBudget(max_luts=7680, max_power_mw=250.0, max_latency_cycles=4096),
)
network = [
LayerProfile(id="encoder", mac_count=256, is_critical_path=True),
LayerProfile(id="decoder", mac_count=128),
]
report = SurrogateSCOptimizer(target).optimise(network)
The report contains selected bitstream length, decorrelator, precision, LFSR polynomial, estimated LUTs, power, latency, utility score, and any rejected layers.
Measured Evidence¶
Measured observations are optional but preferred. The loader accepts JSON
payloads with observations, benchmark_observations, layers, runs, or
results records and raises ObservationLoadError when required fields are
missing. Numeric metrics must be finite, non-negative numbers; integer fields
such as mac_count, bitstream_length, precision_bits, luts_used, and
latency_cycles reject booleans and fractional values.
from sc_neurocore.optimizer import load_observations
observations = load_observations("benchmarks/results/fpga_power_observations.json")
For raw Vivado or Quartus reports, use the package helper:
from sc_neurocore.optimizer import build_payload_from_reports, write_payload
payload = build_payload_from_reports(
design_path="build/network_design.json",
utilisation_path="build/vivado_utilisation.rpt",
power_path="build/vivado_power.rpt",
timing_path="build/vivado_timing.rpt",
accuracy_score=0.991,
clock_mhz=100.0,
inferences_per_run=1,
)
write_payload(payload, "build/synthesis_observations.json")
The same flow is available from the CLI:
sc-neurocore collect-synthesis \
--design build/network_design.json \
--utilisation build/vivado_utilisation.rpt \
--power build/vivado_power.rpt \
--timing build/vivado_timing.rpt \
--accuracy-score 0.991 \
--clock-mhz 100 \
--inferences-per-run 1 \
--out build/synthesis_observations.json
Energy fields are computed only when both clock_mhz and
inferences_per_run are provided. Vendor reports remain the evidence source;
the helper does not run synthesis or invent measurements.
sc_neurocore.optimizer
¶
Automatically compress and tune SNNs for target hardware.
ObservationLoadError
¶
Bases: ValueError
Raised when a benchmark/synthesis observation cannot be trusted.
Source code in src/sc_neurocore/optimizer/observation_loader.py
| Python | |
|---|---|
28 29 | |
SynthesisFeedbackResult
dataclass
¶
Result of one measured-evidence optimiser feedback pass.
Source code in src/sc_neurocore/optimizer/feedback_loop.py
| Python | |
|---|---|
28 29 30 31 32 33 34 | |
OptimizationResult
dataclass
¶
Result of the resource optimization process.
Source code in src/sc_neurocore/optimizer/resource_optimizer.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 | |
BenchmarkObservation
dataclass
¶
Measured or externally supplied design-point observation.
The optimiser treats these as higher-priority training points than its analytical generated points. Callers should only pass observations that come from real benchmark or synthesis outputs.
Source code in src/sc_neurocore/optimizer/surrogate_sc_optimizer.py
| Python | |
|---|---|
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 | |
SurrogateOptimizerReport
dataclass
¶
Budgeted per-layer compiler configuration.
Source code in src/sc_neurocore/optimizer/surrogate_sc_optimizer.py
| Python | |
|---|---|
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 | |
feasible
property
¶
Whether every layer received a configuration.
SurrogateSCOptimizer
¶
Compiler optimiser using a learned surrogate over SC design points.
Source code in src/sc_neurocore/optimizer/surrogate_sc_optimizer.py
| Python | |
|---|---|
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 269 270 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 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 345 346 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 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 | |
optimise(network)
¶
Select budgeted layer settings for network.
Source code in src/sc_neurocore/optimizer/surrogate_sc_optimizer.py
| Python | |
|---|---|
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 | |
TargetHardwareProfile
dataclass
¶
Target device budget and compiler preference weights.
Source code in src/sc_neurocore/optimizer/surrogate_sc_optimizer.py
| Python | |
|---|---|
42 43 44 45 46 47 48 49 50 51 | |
load_observations(path)
¶
Load benchmark observations from a JSON evidence file.
Source code in src/sc_neurocore/optimizer/observation_loader.py
| Python | |
|---|---|
32 33 34 35 36 37 38 39 | |
load_synthesis_observation(report_paths, *, design, accuracy_score, latency_cycles=None)
¶
Load one observation from Vivado/Quartus report files plus design metadata.
Raw vendor reports do not describe the compiler decision that produced the hardware, and many do not carry model accuracy. The caller must therefore provide the design fields and measured accuracy explicitly.
Source code in src/sc_neurocore/optimizer/observation_loader.py
| Python | |
|---|---|
42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 | |
observation_from_synthesis_reports(reports, *, design, accuracy_score, latency_cycles=None, source='<synthesis-reports>')
¶
Build one observation from raw Vivado/Quartus text reports.
Source code in src/sc_neurocore/optimizer/observation_loader.py
| Python | |
|---|---|
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 | |
observations_from_payload(payload, *, source='<memory>')
¶
Convert an in-memory benchmark/synthesis payload into observations.
Source code in src/sc_neurocore/optimizer/observation_loader.py
| Python | |
|---|---|
86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 | |
optimise_from_evidence_payload(*, network, target, payload)
¶
Rerun the SC optimiser from an in-memory evidence payload.
Source code in src/sc_neurocore/optimizer/feedback_loop.py
| Python | |
|---|---|
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 | |
optimise_from_synthesis_reports(*, network, target, design_path, utilisation_path, power_path, accuracy_score, timing_path=None, latency_cycles=None, clock_mhz=None, inferences_per_run=None)
¶
Parse synthesis reports and immediately rerun the SC optimiser.
This helper is the local closed loop for the first production path: report files are parsed into strict evidence, evidence becomes measured observations, and those observations bias the surrogate optimiser for the supplied layer network. It never invokes vendor tools or fabricates missing metrics; callers must provide reports and measured accuracy.
Source code in src/sc_neurocore/optimizer/feedback_loop.py
| Python | |
|---|---|
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 | |
fit_to_target(layer_sizes, weights, target='ice40', max_iterations=10, min_bitstream_length=32, initial_bitstream_length=256)
¶
Automatically compress an SNN to fit a target FPGA.
Iteratively applies: 1. Bitstream length reduction (halving L) 2. Weight pruning (increasing threshold) 3. Weight quantization (reducing bit width)
Stops when the energy estimator says the network fits on the target.
Parameters¶
layer_sizes : list of (n_inputs, n_neurons) weights : list of ndarray target : str FPGA target ('ice40', 'ecp5', 'artix7', 'zynq'). max_iterations : int Maximum optimization steps. min_bitstream_length : int Minimum allowed L. initial_bitstream_length : int Starting bitstream length.
Returns¶
OptimizationResult
Source code in src/sc_neurocore/optimizer/resource_optimizer.py
| Python | |
|---|---|
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 175 176 177 178 179 180 181 182 183 184 185 | |
build_payload_from_reports(*, design_path, utilisation_path, power_path, timing_path=None, accuracy_score, latency_cycles=None, clock_mhz=None, inferences_per_run=None)
¶
Build an evidence payload from report files and explicit metadata.
Source code in src/sc_neurocore/optimizer/synthesis_evidence.py
| Python | |
|---|---|
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 | |
write_payload(payload, output)
¶
Write evidence JSON to a file or stdout.
Source code in src/sc_neurocore/optimizer/synthesis_evidence.py
| Python | |
|---|---|
128 129 130 131 132 133 134 135 136 | |