Repository navigation
Description
Phase 6 of #324.
Base: epic/324-pyodide-browser.
Depends on: phase 4 for the float32 artifact and the export pipeline.
Parallel with: phase 5.
Why
The ICLabel network is about 2.9 M parameters, roughly 11.6 MB at float32, consistent with the 10.8 MB netICL.mat currently in the wheel.
That is a large download for a browser session and a large wheel for everyone else.
Decision already recorded on #324
int8 ships as the default artifact everywhere, native and browser, not browser-only.
One artifact, one set of numbers to validate.
This raises the stakes on the parity report: it is no longer a browser-only concession, it changes what every native eegprep user gets from pop_iclabel.
Treat the parity report as the deliverable that earns the default, not as a formality.
If parity fails the thresholds below, the right outcome is to report that and keep float32 as the default, not to adjust the thresholds.
Parity definition, from the epic, not negotiable downward
On a fixed, held-out set of real independent components from public NEMAR datasets:
- Top-1 label agreement with the float32 reference at or above 95 percent.
- Keep-or-reject decision agreement under eegprep's thresholds at or above 99 percent.
- Per-class agreement reported separately, so rare classes are not averaged away.
The held-out set has to exist and be fixed before any quantization is evaluated against it, otherwise the numbers mean nothing.
Build and freeze it first.
Scope, in order
- Build and freeze the evaluation set of real independent components from public NEMAR datasets.
Record provenance: which datasets, which subjects, how components were selected, and the class distribution.
Selection must not be biased toward components the network already classifies confidently. - Weight-only int8, targeting about 3 MB. This is the shipped default.
- Calibrated int8, evaluated against the same frozen set.
- Stretch, only if the first two clear parity comfortably: low-rank factorization of the third image convolution plus distillation against the float32 teacher, targeting about 1 MB.
Drop this without ceremony if it does not clear parity; it is explicitly a stretch goal. - Parity report committed next to the artifact.
Feature extraction, input normalization, and softmax stay float, per the epic.
Non-goals
No retraining of the float32 network.
No change to feature extraction or to the class set.
No threshold changes in eeg_icflag or the keep-or-reject logic to make parity look better.
Acceptance criteria
- Evaluation set built, frozen, and committed or referenced with reproducible provenance, before quantization is evaluated.
- Component selection methodology documented, including class distribution and how bias toward confident components was avoided.
- Weight-only int8 artifact produced, size reported against the roughly 3 MB target.
- Calibrated int8 evaluated against the same frozen set.
- Parity report committed next to the artifact, giving top-1 agreement, keep-or-reject agreement, and per-class agreement.
- All three thresholds met before int8 becomes the default. If not met, float32 stays the default and the report says why.
- Wheel size before and after reported.
- Existing MATLAB parity tests in
tests/test_iclabel.pystill pass, with tolerances unchanged. If int8 cannot meet the existing tolerance, that is a finding to report, not a tolerance to widen. - Float32 export path still reproducible from
tools/iclabel/. -
docs/source/andsrc/eegprep/resources/help/pop_iclabel.mdstate which artifact ships and what the parity guarantee is, since this changes native user results. - Pre-commit, ruff, ruff format, ty clean.
/goal prompt
/goal Quantize the ICLabel network to int8 and earn the right to ship it as the default everywhere, or demonstrate that it has not earned it.
Source of truth: phase 4's float32 ONNX artifact and export pipeline, public NEMAR datasets for the evaluation components, the parity thresholds on this issue, and the existing MATLAB fixtures in tests/test_iclabel.py. Read AGENTS.md first.
Constraints: build and freeze the held-out evaluation set before evaluating any quantized model against it; keep feature extraction, normalization, and softmax float; do not retrain the float32 network; do not touch keep-or-reject thresholds; keep the float32 export reproducible.
Finish line: the frozen evaluation set and its provenance are committed; weight-only int8 and calibrated int8 are both evaluated against it; the parity report next to the artifact gives top-1, keep-or-reject, and per-class agreement; wheel size before and after is reported; and int8 is the shipped default only if all three thresholds are met.
The thresholds are 95 percent top-1, 99 percent keep-or-reject, per-class reported. Never move a threshold or relax an existing test tolerance to pass. If int8 misses, keep float32 as the default and report the gap. The low-rank plus distillation step is a stretch goal; drop it if it does not clear parity. Report any review finding you chose not to fix, with the reason.
Activity
neuromechanist commentedon Sep 17, 2026
🤖 # Research
Phase 4 exports ICLabelNet.forward to pinned opset-17 iclabel.onnx; runtime classification keeps feature extraction, four-way augmentation, float32 inputs, post-network averaging, and softmax behavior in iclabel.py. The current iclabel_net_onnx.py loads the packaged artifact, while eeg_icflag.py applies the existing 7x2 threshold matrix without a model-dependent path. The public NEMAR catalog exposes on002680 (OpenNeuro source ds002680, NEMAR v1.0.0, CC0) with immutable Git-annex recording keys, so it is a viable real-component source pending deterministic ICA/materialization.
Relevant code
tools/iclabel/export_iclabel_onnx.py#L56-L76- reproducible float32 exportsrc/eegprep/plugins/ICLabel/iclabel.py#L58-L85- unchanged feature/postprocess pathsrc/eegprep/plugins/ICLabel/iclabel_net_onnx.py#L19-L57- packaged artifact loadingsrc/eegprep/plugins/ICLabel/eeg_icflag.py#L45-L61- keep/reject decision logicProposed Fix
Freeze a manifest before any quantized evaluation: use subject-disjoint ses-01, run-1 recordings for evaluation and calibration, retain all deterministic ICA components, and record source URLs/DOIs, mirror/source commits, Git-annex keys and sizes, raw checksums, feature-archive checksums, ICA/feature preprocessing, and component selection. Add developer-only tooling that creates a true weight-only int8 QDQ graph and a calibrated int8 graph from the frozen float32 ONNX model, evaluates both on the same frozen feature set against the float32 teacher, reports top-1, keep/reject, and every class separately, and records artifact sizes. Promote int8 only when top-1 is at least 95% and keep/reject is at least 99%; otherwise leave float32 selected and document the measured gap. Update only ICLabel artifact-selection/docs/tests needed for this policy; do not change Phase 5 async entry-point design.
neuromechanist commentedon Sep 17, 2026
🤖 Phase 6 is implemented in PR #383. The frozen subject-disjoint public-data evaluation set, reproducible float32 reference, weight-only int8 and calibrated int8 candidates, parity report, docs, and tests are committed. The shipped weight-only artifact measures 100% top-1 and 100% keep/reject agreement; calibrated int8 measures 98.1567% and 100%. A combined Phase 5 browser-runner check with the Phase 6 artifact also measured 100% top-1 and 100% keep/reject agreement on 32 components. The remaining CI benchmark is still running.
neuromechanist commentedon Sep 20, 2026
4-bit was measured and shelved. int8 stays.
Recording this here rather than in a pull request, so the numbers outlive the branch and nobody repeats the investigation.
It works. ICLabel is 10
Convnodes and zeroMatMul, soMatMulNBits, where nearly all 4-bit ONNX tooling lives, has nothing to attach to. The portable route is auint4initializer behind a blockedDequantizeLinearfeeding the existing floatConv. That executes in ONNX Runtime Web 1.30 under wasm, and in headless Chrome 153 the output is byte-identical by SHA-256 to the Node run, for int4, int8 and float32 alike. Verified with graph optimization disabled, so the 4-bit kernel genuinely runs rather than being constant-folded to float32 at session load, which would have meant 4-bit storage and float32 runtime.And it is not worth adopting.
Every one of the 2,903,817 parameters is a Conv weight, with only about 1,806 bias values and nothing unquantizable, so nothing dilutes the ratio: int8 gives a clean 3.96x and int4 6.02x. The 3 to 4 MB figure this was aimed at is already met by int8. 4 bits does not reach that target, it overshoots it. The target appears to have been set alongside an assumption of roughly 3x, which would have put int8 near 4 to 5 MB and made 4 bits necessary; for this network the reduction is 4x and the method was never needed.
Against adopting it: an opset-21 floor, an
onnxruntimeminimum of 1.19 wherepyproject.tomlcurrently declares>=1.18(1.18.1 refuses the graph outright,MLDataType for: tensor(uint4) is not currently registered), and no speed benefit at all, since weight-only 4-bit dequantizes to float before the same floatConv(3,214 ms against 3,043 ms for float32 over 868 rows). Browser-only int4 is worse still: it reintroduces the two-artifact, two-validation-set problem #324 closed deliberately, to save 0.74 MB.Parity, for the record. On the frozen 217-component gate, int4 scores 100 percent top-1 and 100 percent keep-or-reject, the same gate int8 passes. Pooled over all 466 real components available, int4 and int8 are identical at the label level in every class, with one Brain component flipping under both. int4 perturbs probabilities about 3.4x more (max 4.5e-2 against 1.3e-2), which puts 16 of 466 components inside its worst case against 6 of 466 for int8, but that exposure produced no extra flip.
Threshold to revisit, checkable by anyone later: if the ICLabel artifact ever has to fit under 2.5 MB. int8 is 2,932,897 B and cannot get there by this method; int4 is 1,931,262 B and clears it with room. Second trigger: a real Heart or Channel Noise component entering the evaluation set on which int8 passes and int4 fails.
Two findings from this work that outlive it
1. DIPFIT is now the larger lever, not ICLabel. In the current int8 wheel,
eegprep/resources/dipfitis about 3,840,620 B compressed against about 2,579,065 B for all of ICLabel. int8 already moved ICLabel below the head-model data. Further ICLabel quantization optimizes the smaller half, and no amount of it gets a 7 MB wheel to 3 to 4 MB.2. Heart and Channel Noise have never been validated, on int8 either. Zero components of either class exist in any recording reachable from this repository, so the shipped artifact has two classes with no parity coverage at all. This predates the 4-bit work and is not caused by it. An attempt to close it with public CC0 data (ds008768) failed because
eeg_runicawould not converge on 63 channels in reasonable time, which is the ICA cost problem Phase 2 measured rather than a quantization result. What exists is a bound rather than a measurement: across the 466 real components, int4 moves Heart mass by at most 2.6e-2 and Channel Noise by at most 8.6e-3. That should not be quoted as parity.Worth knowing about the gate itself: #383 re-pointed the tight
rtol=1e-4, atol=1e-5probability test at the preserved float32 reference rather than the quantized artifact, because int8's 1.3e-2 deviation could never satisfy it. The quantized guarantee is already semantic rather than probability-level. That is a decision the project has already made and int4 does not change it.neuromechanist commentedon Sep 20, 2026
One defect worth recording before the branch goes, since the tool is what anyone revisiting this would rerun.
CI found a real bug in
tools/iclabel/quantize_iclabel_int4.py. The rewrite keyed its node map onid(node)across two separate passes overmodel.graph.node. Iterating a protobuf repeated field can hand back fresh Python wrappers, so the identity is not stable between passes, and theDequantizeLinearcould be emitted after theConvthat consumes it:It is environment-dependent, which is what makes it nasty: macOS and Windows on Python 3.12 passed, Linux on 3.10 and 3.11 with onnx 1.22 failed. The existing int8 path in
quantize_iclabel_onnx.pyis not affected; it capturesgraph_nodes = list(...)once and reuses that list, which is exactly the pattern the int4 tool should have followed.Fixed on
feature/iclabel-int4-feasibilityat5c3beed3: keyed by position instead of object identity, validated withonnx.checker.check_model(..., full_check=True), and the test now asserts every producer precedes its consumer. Both guards were confirmed to reject a deliberately misordered graph, so they are not blind.The measured artifact is unaffected.
iclabel_int4_block32.onnxregenerates byte-identical after the fix (e380767ce796...) and passesfull_check, so every size and parity number above stands as reported. The fault was tool fragility on other platforms, not a bad artifact.neuromechanist commentedon Sep 20, 2026
One caveat to add to the note above, because it is the part that bites whoever revisits this.
The fix commit has no CI run. Closing #398 stopped the
pull_requesttrigger, so5c3beed3was never built by CI. The branchfeature/iclabel-int4-feasibilityis at5c3beed3; the closed pull request's head wasec39ce6f, which does not contain the fix. So the state is: the commit CI tested is the one with the bug, and the commit that fixes the bug is untested by CI.It was verified locally, on one platform, which is precisely the condition under which the original defect survived: the failure appeared only on Linux with onnx 1.22 under Python 3.10 and 3.11, while macOS and Windows on 3.12 passed. Anyone picking this up should get CI green on
5c3beed3before trusting the tool, not because the artifact is in doubt, but because the same blind spot is still open.None of this touches the shipped artifact or the conclusion.
iclabel.onnxwas never modified,iclabel_int4_block32.onnxregenerates byte-identical after the fix, and the sizes, the ORT Web evidence and the per-class parity table are unaffected. The defect was in the tool that produces the artifact, not in the artifact.