perf: optimize data / prediction hot paths - #21
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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
Accumulator.is_fullO(1) via a filled counter (was O(P²) per case).state_dicts in RAM instead of re-reading per case.Dataset.get_infosacross setup passes.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.