Repository navigation
Description
Context
The develop reference suite fails on tests/test_ds003061_stage_parity.py::test_average_reference_matches_matlab_on_64_channel_float32. This is an existing develop failure, reproduced separately from the stream PRs.
Evidence was read from [email protected] (jamming), under /home/suraj3/eegprep-bench/logs/:
| Run | Evidence | Result |
|---|---|---|
Develop, reported baseline 1bdd1a28 |
s5_ref/reref_repro_develop.log |
1 failed in 17.02 s |
S5, 585a4463d7afa45e4d6434336d2f714a33024a04 |
s5_ref/parity.pytest.log, s5_ref/reref_repro_s5.log |
Same maximum error |
S9, 74519da7804f9b6dd5e2923918e805fcc4d782d4 |
s9_ref/parity.pytest.log, s9_ref/junit_parity.xml |
Same maximum error |
S11, 8a02dd79a23cd1cb62dc4d9e84b2df94a94620fb |
s11_ref/parity.pytest.log, s11_ref/junit_parity.xml |
Same maximum error |
The develop log identifies the eegprep-develop checkout but does not embed its SHA. That checkout was still at 1bdd1a28353a4dfd51ffacd7b327443836de7f29 when inspected. Source investigation used 64e8d2fe6eb903945407c00f11f610a7552fe42c; the failing test and reref.py are unchanged from 1bdd1a28.
Reference pins: EEGLAB d0963be9d81acc5b66e84f8d3d7c89e3ce127cc4, eeglab_tests fec88f9ffe7c225eb1de8924db8b6a3e0d53a4d9. No MATLAB session was started during this investigation.
Repro steps
- Use develop with the pinned EEGLAB and eeglab_tests checkouts. Materialize the Git LFS data for
ds003061/sub-001/eeg/sub-001_task-P300_run-1_eeg.setand its BIDS sidecars. - Configure the existing MATLAB reference environment. On jamming, any future reproduction must hold
flock ~/eegprep-bench/matlab.lockfor the entire MATLAB session and test execution, with one MATLAB session at a time. - From the EEGPrep checkout, run the focused test with the configured MATLAB engine or batch transport:
uv run --no-sync pytest -vv \
--eeglab-backend=matlab \
--eeglab-root="$HOME/eegprep-bench/eeglab" \
--eeglab-suite-root="$HOME/eegprep-bench/eeglab_tests" \
--timeout=1200 --timeout-method=signal -p no:cacheprovider \
tests/test_ds003061_stage_parity.py::test_average_reference_matches_matlab_on_64_channel_float32The fixture imports sub-001 run 1, selects 64 EEG channels, saves and reloads the data, and asserts float32. Both branches receive the same source. _Side.reref() calls Python pop_reref(eeg, []) or MatlabWrapper.pop_reref(eeg, np.empty((0, 0))).
Observed
The develop log reports:
tests/test_ds003061_stage_parity.py:519:
_assert_data_close(python_eeg, matlab_eeg, REREF_ATOL, f"{SUBJECT} pop_reref")
tests/test_ds003061_stage_parity.py:357: AssertionError
E AssertionError: sub-001 pop_reref max_abs=1.093750e-01 uV exceeds 1.000000e-05
E assert 0.109375 <= 1e-05
UserWarning: sub-001 pop_reref: max_abs=1.093750e-01 uV rms=2.351263e-03 uV
Channel-label and data-shape checks pass before the numeric assertion. The maximum error exceeds the existing ceiling by 10,937.5 times.
A local Python-only diagnostic also shows shape-sensitive rounding in the production reref path. With NumPy 2.2.6, OpenBLAS 0.3.29, and OPENBLAS_NUM_THREADS=1, the following code used the same imported/reloaded fixture:
import tempfile
from pathlib import Path
import numpy as np
from eegprep.functions.sigprocfunc.reref import reref
from tests.test_ds003061_stage_parity import _import_subject, _reload
suite_root = Path("/workspace/eegprep/eeglab_tests")
with tempfile.TemporaryDirectory() as directory:
eeg = _reload(_import_subject(suite_root, "sub-001"), Path(directory) / "input.set")
data = eeg["data"]
whole = reref(data, [])[0]
first = reref(data[:, :1], [])[0]
print(data.shape, data.dtype)
print(float(np.max(np.abs(whole[:, :1] - first))))Output: (64, 194048) float32, then 0.046875 uV. These are identical input samples processed at different matrix widths. This diagnostic supports kernel-dependent rounding; it does not measure a new MATLAB comparison. The recording's peak absolute input is 259369.28125 uV, where float32 spacing is 0.015625 uV.
Expected
The existing float32 reference test passes with matching channel labels and sample shape, finite output, and max_abs <= REREF_ATOL == 1e-5 uV. Preserve the existing numerical tolerances and the test's float32 input contract.
Likely root cause
The average-reference implementation assumes that transposing the matrix product fixes MATLAB's accumulation order across BLAS implementations and matrix sizes. The local diagnostic contradicts that general assumption. Large channel offsets make float32 accumulation differences visible in absolute uV.
Relevant code paths, with line numbers at 64e8d2fe:
tests/test_ds003061_stage_parity.py:394,:479,:509: shared fixture and the Python/MATLAB comparison.src/eegprep/functions/popfunc/pop_reref.py:123: dispatches the channel data to low-levelreref.src/eegprep/functions/sigprocfunc/reref.py:81-99: constructsrefmatrixin the original dtype and calculates(block @ refmatrix).T; its comment claims bit-exact MATLAB parity on x86 BLAS.- Pinned EEGLAB
functions/sigprocfunc/reref.m:230-233: constructseye(nchansin)-ones(nchansin)*1/nchansin, then evaluatesrefmatrix*data(chansin,:). At 64 channels the coefficients are exactly representable in float32, making accumulation order a stronger suspect than coefficient rounding. src/eegprep/functions/adminfunc/eeglabcompat.py:267-302: transfers input through a dataset file and preserves MATLAB output data before the result is serialized. The test disablesoption_singleattests/test_ds003061_stage_parity.py:470; pinnedeeg_checkset.m:777only downcasts doubles when that option is enabled, so disabling it alone does not promote an existing single array.
The specific MATLAB/NumPy kernel difference remains a hypothesis. Capture input dtype and equality immediately before both low-level products, plus MATLAB and NumPy BLAS versions, CPU, thread count, memory order, and intermediate results, before choosing the repair. A local comparison of transposed, direct, and Fortran-contiguous float32 products produced identical full-recording results on this machine, so changing layout alone has no demonstrated fix here.
Definition of done
- Confirm the first divergent operation on identical float32 inputs using the pinned MATLAB reference and record its precision and BLAS configuration.
- Fix the responsible implementation or demonstrated transport defect while preserving the float32 test and all existing tolerances, including
tests/fuzz/tolerances.toml. - Pass the exact failing node and relevant
pop_rerefcoverage; retain float64 behavior, channel exclusion, and reference-channel semantics. - Run the required final MATLAB reference suite for a core-function change and record the command, pins, counts, and runtime. Obtain human approval before merging a core-function fix.
Activity
claude commentedon Nov 8, 2026
🤖 Plan:
reref.py, the parity test, and open PRspop_reref.pybut not low-levelreref.py; no overlapping fixOpen PR: not opened. The definition of done requires diagnosing against the pinned MATLAB first, and this runner has no MATLAB or Octave. The fix also needs a human decision on tolerance or contract (see below).Research
reref.pycomputes the average reference as a float32 BLAS GEMM,(data.T @ refmatrix).T, and relies on NumPy/OpenBLAS summing in the same order as MATLAB's MKLsgemm. Neither library guarantees an order: it depends on blocking, matrix width, CPU dispatch (AVX2 vs AVX-512), and thread count. Your width-dependent diagnostic (0.046875 uV betweendataanddata[:, :1]) shows this. The peak input is about 2.6e5 uV, where one float32 ULP is 0.0156 uV, soREREF_ATOL = 1e-5effectively demands bit-identical output from two different GEMM kernels. The observed 0.109 uV is 7 ULP. That is normal float32 GEMM disagreement after summing 64 terms of that size, not a logic bug.Relevant code
src/eegprep/functions/sigprocfunc/reref.py#L81-L99- float32 GEMM with the "bit-exact on x86" claimtests/test_ds003061_stage_parity.py#L80-REREF_ATOL = 1e-5tests/test_ds003061_stage_parity.py#L509-L519- failing nodefunctions/sigprocfunc/reref.m(refmatrix*data(chansin,:)) - MKL single-precision GEMMProposed Fix (for human review)
No change to Python alone can reliably match MKL's unspecified summation order bit for bit across machines. Options:
atol = k * np.spacing(np.float32(max|input|)) * sqrt(nchan), which is about 0.1 to 0.2 uV here. Keep the float32 contract and test intent. Remove the "bit-exact" comment inreref.py. This changes a test tolerance, so a human must approve it.tests/fuzz/tolerances.tomlis not affected.reref.py:refmatrixandblockin float64, result.astype(float32). Python then gets the correctly rounded result regardless of BLAS, which makes it deterministic and more accurate. MATLAB still computes in single precision, so the comparison will still differ by a few ULP. It only passes together with option 1. This changes a core function, so it needs the MATLAB reference suite run and approval.data - data.mean(axis=0)in float64, then cast. EEGLAB itself uses this subtraction for explicit reference channels. It has the same tradeoff as option 2.To meet the issue's first DoD item before choosing: on jamming (under
flock), dump MATLAB'srefmatrix*single(data)for column 1 alone and for the full matrix, plusversion -blasandgetenv('MKL_CBWR'). If MATLAB's own result also changes with width, bit parity is impossible and option 1 is required. Running MATLAB withMKL_CBWR=COMPATIBLEwould show whether the kernel dispatch path causes the gap.Open question: should parity tests for float32 kernels backed by BLAS use ULP-scaled tolerances everywhere, or only for reref?
arnodelorme commentedon Nov 28, 2026
The fix of this issue in #606 (float64 sum, cast to float32) moved the average reference 0.156 µV away from EEGLAB and broke the parity of sccn/eegprep_parity_test: after ASR, no subject kept the same samples as EEGLAB. Pull request #833 reverts it, so this issue, the dependence of the float32 product on the BLAS kernel, is open again. A fix here must keep EEGLAB's result rather than a more exact one.
— orchestrator