You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
SparsePCA sparse-zero count is nondeterministic across runs of one unmodified binary #378
Found while verifying #233 (AgglomerativeClustering.fit, PR #372). Unrelated to that change; filing separately.
What happens
tests/test_new_modules_v2.flow prints a SparsePCA line reading "N sparse zeros". Running one binary built from unmodified origin/main twelve consecutive times gives:
6, 4, 4, 6, 6, 4, 6, 4, 6, 6, 6, 6
Same binary, same input, different answer. The implementation is in lib/scikit/decomposition.flow.
Why it is not caught today
The value is printed, not asserted, so the test passes either way. That also means the nondeterminism has presumably been there a while without anyone noticing.
Likely cause
This has the shape of an uninitialized read: a sparsity threshold comparing against memory that was never written, so whether a coefficient counts as zero depends on heap contents. Compare #359, where tree.flow:216mallocs nodes and leaves fields unwritten, producing exactly this kind of run-to-run variation. Worth checking decomposition.flow for the same malloc-instead-of-calloc pattern, and for any accumulator read before its first write.
An alternative explanation is a genuine convergence tie in the sparse coding loop landing on either side of a threshold, in which case the fix is to make the tie deterministic rather than to zero a buffer. The distinguishing test is whether the intermediate coefficients themselves vary run to run, or only the count that thresholds them.
Why it matters beyond the print
SparsePCA feeds decomposition.flow, which also holds PCA, DictionaryLearning and FastICA. If an uninitialized buffer is shared or the pattern is copied, other estimators in that file inherit it. It also makes any future model-state diagnostic for SparsePCA unreliable, which is the same trap #359 sets for #204 and #205.
Suggested first step: assert the count in the test so the flake becomes visible, then bisect the buffer.
Found the cause while working on #264/#265/#270 in this same file. It is a use-after-free rather than an uninitialized read, but the same class as #359.
sparse_pca_fit in lib/scikit/decomposition.flow frees grad, then reads it ten lines later in the convergence test:
array_free_f32(grad)
if iter > 0 && iter % 10 == 0 {
let mut change: f32 = 0.0
for j in 0 to p {
change = change + fabs((grad[j]) as f64) as f32
}
if change / (p as f32) < tol { break }
}
change is therefore computed from whatever the allocator has since put in that block. Whether change / p < tol holds decides if the component loop breaks at iteration 10, 20, 30 and so on, so the number of iterations SparsePCA runs varies run to run, which varies the final coefficients, which varies how many of them fall under the sparsity threshold. That is the 4/6 alternation.
This distinguishes the two hypotheses in the issue: it is the first one. The intermediate coefficients themselves vary, not just the count that thresholds them.
Two other things worth noting for whoever fixes it.
The convergence test is also reading the pre-normalization gradient regardless. In the original intent change should presumably be measured either from grad before array_free_f32, or from the change in components[c] across the iteration. Those are different fixes with different fitted states, so the fix needs a decision, not just a move of the free.
Whichever is chosen, it changes SparsePCA's iteration count and therefore its fitted state on every input, so it wants its own PR with the count asserted in tests/test_new_modules_v2.flow as the issue suggests.
I have not fixed it. My three PRs (#383, #384, #386) touch dictionary_learning_transform, dictionary_learning_fit and fast_ica_fit only, and all three are bit-identical in their own outputs. I did confirm the blast radius is contained: diffing the full printed output of tests/test_new_modules_v2.flow before and after the whole stack, the SparsePCA line is the only one that ever differs, and it differs on unmodified origin/main too. PCA, DictionaryLearning and FastICA do not inherit the pattern.
Root cause confirmed, and it is worse than the uninitialized read I guessed at in the issue body. It is a use-after-free, found by the agent working #264/#265/#270.
lib/scikit/decomposition.flow:755:
array_free_f32(grad)
if iter > 0 && iter % 10 == 0 {
let mut change: f32 = 0.0
for j in 0 to p {
change = change + fabs((grad[j]) as f64) as f32
}
if change / (p as f32) < tol { break }
}
grad is freed, then read in the convergence test immediately below. The iteration count therefore depends on whatever the allocator has since put in that memory, which is the 4/6 alternation in the sparse-zero count.
There are two sites, not one. The same pattern appears again at decomposition.flow:912. Both need fixing.
This reclassifies the issue: it is not a cosmetic flaky print, it is undefined behaviour that decides how many iterations SparsePCA runs, and therefore what it fits. Under a different allocator or with ASan it could crash rather than merely vary.
Fix direction: move the array_free_f32(grad) below the convergence test, or compute change before the free. Note this will change SparsePCA's fitted state relative to today, since today's iteration count is partly random. That is a correction, but it should be called out so nobody reads the resulting benchmark movement as a regression.
Compare #359, which is a genuine uninitialized read in tree.flow. Two different defects, both producing run-to-run variation.
Activity
godofecht commentedon Aug 18, 2026
Found the cause while working on #264/#265/#270 in this same file. It is a use-after-free rather than an uninitialized read, but the same class as #359.
sparse_pca_fitinlib/scikit/decomposition.flowfreesgrad, then reads it ten lines later in the convergence test:changeis therefore computed from whatever the allocator has since put in that block. Whetherchange / p < tolholds decides if the component loop breaks at iteration 10, 20, 30 and so on, so the number of iterations SparsePCA runs varies run to run, which varies the final coefficients, which varies how many of them fall under the sparsity threshold. That is the 4/6 alternation.This distinguishes the two hypotheses in the issue: it is the first one. The intermediate coefficients themselves vary, not just the count that thresholds them.
Two other things worth noting for whoever fixes it.
The convergence test is also reading the pre-normalization gradient regardless. In the original intent
changeshould presumably be measured either fromgradbeforearray_free_f32, or from the change incomponents[c]across the iteration. Those are different fixes with different fitted states, so the fix needs a decision, not just a move of thefree.Whichever is chosen, it changes SparsePCA's iteration count and therefore its fitted state on every input, so it wants its own PR with the count asserted in
tests/test_new_modules_v2.flowas the issue suggests.I have not fixed it. My three PRs (#383, #384, #386) touch
dictionary_learning_transform,dictionary_learning_fitandfast_ica_fitonly, and all three are bit-identical in their own outputs. I did confirm the blast radius is contained: diffing the full printed output oftests/test_new_modules_v2.flowbefore and after the whole stack, the SparsePCA line is the only one that ever differs, and it differs on unmodifiedorigin/maintoo. PCA, DictionaryLearning and FastICA do not inherit the pattern.godofecht commentedon Aug 18, 2026
Root cause confirmed, and it is worse than the uninitialized read I guessed at in the issue body. It is a use-after-free, found by the agent working #264/#265/#270.
lib/scikit/decomposition.flow:755:gradis freed, then read in the convergence test immediately below. The iteration count therefore depends on whatever the allocator has since put in that memory, which is the 4/6 alternation in the sparse-zero count.There are two sites, not one. The same pattern appears again at
decomposition.flow:912. Both need fixing.This reclassifies the issue: it is not a cosmetic flaky print, it is undefined behaviour that decides how many iterations SparsePCA runs, and therefore what it fits. Under a different allocator or with ASan it could crash rather than merely vary.
Fix direction: move the
array_free_f32(grad)below the convergence test, or computechangebefore the free. Note this will change SparsePCA's fitted state relative to today, since today's iteration count is partly random. That is a correction, but it should be called out so nobody reads the resulting benchmark movement as a regression.Compare #359, which is a genuine uninitialized read in
tree.flow. Two different defects, both producing run-to-run variation.