Why my sky130 bandgap gave a different answer every run
One test in our xschem fork failed two of our last four full regression runs. On its own it always passed, so for weeks it was filed as "the known flaky one". This week we found the cause, and it is not a test problem at all. Every ordinary run of a tt_mm sky130 bench is a fresh Monte Carlo trial.
What we saw
The test simulates the bandgap testbench that ships with the open_pdks sky130 examples. Sometimes ngspice ran for about 13 seconds, exited with code 1, and wrote no results file. The failing check moved from run to run.
It looked like a load problem, because it only failed inside a full test run. It wasn't.
We copied the exact deck the test used and ran it with ngspice directly, 24 times. One run failed:
doAnalyses: TRAN: Timestep too small; time = 1.63211e-07, timestep = 5e-20: trouble with node "v4#branch" tran simulation(s) aborted
The surprise was earlier in the output. ngspice prints its starting solution before the transient begins, and no two runs printed the same one. Most started near 0 V, with noise from 1e-12 down to 1e-53. The failing run started at a non-physical point, with one node at −1.96×108 V.
Same deck, same binary, a different answer every time. Forcing ngspice onto one thread changed nothing, so it wasn't multithreading.
The cause
The bench's corner symbol says corner=tt_mm. In the sky130 model library, that section sets MC_MM_SWITCH=1, so every transistor draws a random mismatch offset when the netlist is loaded. Plain tt sets it to 0.
ngspice has no separate Monte Carlo analysis. Monte Carlo in ngspice means "run again with a different seed". These runs set no seed, so ngspice used a different one every time. Every run was a new random sample of the circuit.
Seven of the bundled sky130 example benches use tt_mm, including the two bandgaps, the comparator, lvnand and srlatch.
Adding one line before the .control block:
.option seed=1
Six concurrent runs then found the same starting solution, and their results files matched byte for byte once ngspice's date line was removed.
Is ngspice broken?
No. ngspice and the sky130 models are doing what they were designed to do. The bundled sky130_mismatch example even calls setseed 12 when it wants a repeatable run.
The bug is the gap between that design and what a Cadence user expects. In Spectre, mismatch only applies inside a Monte Carlo analysis, so an ordinary run gives the same numbers every time. Our ADE-style front end, ASE-L, handed ngspice the deck without pinning a seed. So pressing Run on a tt_mm bench quietly ran one Monte Carlo trial instead of a repeatable simulation. On this bandgap, about one trial in 24 also failed to converge.
What we're changing
ASE-L will pin the random seed for every run that isn't a Monte Carlo run, so a tt_mm bench gives the same answer each time you press Run.
Keep one subtlety in mind. A fixed seed gives you one particular mismatch sample, repeatably. It is not the mismatch-free typical device you get from tt. If you want typical numbers, use tt. If you want to see mismatch, run Monte Carlo.
If you use ngspice with sky130 today
If your corner ends in _mm and you aren't running Monte Carlo, either switch to the plain corner or put .option seed=1 in your deck. Otherwise two runs of the same schematic can disagree, and you'll chase a change you never made.
A flaky test hides its cause until someone keeps the evidence. These are the requests that got us from "known flaky" to a root cause:
- "Don't re-run it again. Find out why the simulator exits with error 1."
- "Copy the exact deck the test simulates, and run ngspice on it directly, many times, keeping the output of any run that fails."
- "Compare a failing run's output with a passing one, and tell me where they first differ."
- "Before fixing anything, give me the background: where did this come from, and is the tool itself broken?"
The third one cracked it. The difference was before the transient even started, and that pointed straight at the models instead of at the test.
