Skip to content

perf: optimize data / prediction hot paths - #21

Merged
vboussot merged 6 commits into
mainfrom
perf/hot-paths
Jul 4, 2026
Merged

perf: optimize data / prediction hot paths#21
vboussot merged 6 commits into
mainfrom
perf/hot-paths

Conversation

@vboussot

@vboussot vboussot commented Jul 3, 2026

Copy link
Copy Markdown
Member

What

Performance optimizations on the data-loading and prediction hot paths, each with a regression/behaviour test in tests/unit/test_perf_hot_paths.py.

Commits

  • perf(patching): make Accumulator.is_full O(1) via a filled counter (was O(P²) per case).
  • perf(predict): cache ensemble state_dicts in RAM instead of re-reading per case.
  • perf(dataset): memoize Dataset.get_infos across setup passes.
  • perf(dicom): thread series info through slice reads to kill an O(Z²) rescan.
  • perf(data): device-side accumulator allocation + a float32 clamp fast path.

Scope

Touches konfai/data/patching.py, konfai/data/transform.py, konfai/predictor.py, konfai/utils/dataset.py, konfai/utils/dicom.py (+ tests). No public API or config change; the patch-ordering and reassembly invariants are preserved.

vboussot added 6 commits July 3, 2026 21:33
The completion check re-scanned every patch slot after each added patch,
i.e. O(P^2) per case — seconds of CPU on cases split into many (2D) patches.
Track a running count instead.
A local-path ensemble reloaded every checkpoint from disk on each batch: the
sub-model index cycles 0..N-1 per forward, so the next batch found the index
mismatched and re-read + re-unpickled all N checkpoints (N*P*C disk reads).
Cache the CPU state_dict per index (as the URL path already effectively does);
invalidate on load(). Adds a regression test covering P1 and P6.
get_infos re-opened the file and re-parsed the header on every call, and it
runs once per name per group per build-pass at setup (name resolution, each
DatasetManager, the validation-count pass). Cache it per (group, name) like
get_names, invalidated on write; hand back copies to avoid aliasing.
A single DICOM patch read re-scanned the series directory ~6x (discover +
sort, three times over) before reading pixels, and file_to_data_statistics
paid that per z-slice (O(Z^2) header parses). Expose the already-sorted file
list from get_dicom_info and thread it into read_dicom_series_slice, and read
statistics through a single discovery. Output stays byte-identical (adversarial
red-team: 4/4 lenses clean); a mismatched series_uid now raises instead of
silently reading the wrong series.
Accumulator.assemble now allocates the result buffer directly on the reference
device instead of zero-filling on CPU then copying H2D. Clip takes a fused
in-place clamp_ on float32 tensors (its per-patch hot path) instead of two
float()-copy + where-scatter passes; integer (CT int16) and float16/float64
tensors keep the exact legacy scatter, so output stays byte-identical.
Adversarial byte-identity verification rejected the naive unguarded variants.
The test compared str(Path(...)) reads against hardcoded forward-slash paths,
which fails on Windows where Path renders backslashes. Build the expected
values through str(Path(...)) too so the separators match the platform.
@vboussot
vboussot merged commit 87bfc28 into main Jul 4, 2026
44 checks passed
@vboussot
vboussot deleted the perf/hot-paths branch July 4, 2026 08:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant