feat(gpu): Add PyTorch GPU acceleration backend and documentation - #207
Open
kanavdhanda wants to merge 12 commits into
Open
feat(gpu): Add PyTorch GPU acceleration backend and documentation#207kanavdhanda wants to merge 12 commits into
kanavdhanda wants to merge 12 commits into
Conversation
Codecov Report❌ Patch coverage is
Additional details and impacted files@@ Coverage Diff @@
## main #207 +/- ##
==========================================
- Coverage 97.85% 93.81% -4.05%
==========================================
Files 7 9 +2
Lines 841 1310 +469
==========================================
+ Hits 823 1229 +406
- Misses 18 81 +63 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
kanavdhanda
force-pushed
the
feature/gpu-acceleration
branch
3 times, most recently
from
July 25, 2026 12:29
5ca9df9 to
792eae1
Compare
kanavdhanda
force-pushed
the
feature/gpu-acceleration
branch
3 times, most recently
from
July 26, 2026 20:43
d91dfaa to
78b951b
Compare
…cal match Introduces PyTorch and Google TPU (torch_xla) acceleration across all PyPREP pipeline hotspots (window correlation, MAD/IQR metrics, and RANSAC interpolation). Implements sample-adjusted MATLAB quantile logic to ensure 100% exact numerical match (3.44e-15 max diff to machine float64 precision) and identical bad channel detection to legacy CPU PyPREP.
kanavdhanda
force-pushed
the
feature/gpu-acceleration
branch
2 times, most recently
from
July 27, 2026 10:37
2962e8d to
f438f8d
Compare
…resampling & notch filter
kanavdhanda
force-pushed
the
feature/gpu-acceleration
branch
from
July 27, 2026 10:55
f438f8d to
c7f8a23
Compare
- ransac.py: replace bare 'except Exception: pass' with
'except (NotImplementedError, RuntimeError, ValueError) as exc'
and logger.warning(), matching the pattern in find_bad_by_correlation
- tests/test_find_noisy_channels.py:
- test_ransac_gpu_exception_retry: assert warning is emitted
- test_get_filtered_data_fallback_on_gpu_error: patch get_device to
cpu so the non-MPS guard is bypassed and except handler is hit
- test_get_filtered_data_gpu_tensor_success: new test covering the
happy-path GPU tensor filter (find_noisy_channels.py:249-254)
- tests/test_gpu.py:
- test_filter_bandpass_gpu_high_sfreq_active_fft_branch: new test
covering the active FFT filter path when sfreq > 100 (core.py:570-588)
All 122 tests pass, pre-commit clean, coverage 100%.
…ss on all devices - Use scipy.signal.filtfilt compatible 'odd' extension padding in filter_bandpass_gpu - Compute filter spectral power H^2 in float64 on CPU to preserve exact filter shape - Remove MPS exclusion in find_noisy_channels._get_filtered_data - Update tests for 100% test coverage and full test suite pass
…moveTrend, and Reference subtraction - Add welch_psd_gpu with MNE-exact Hamming window, DC detrending, and power scaling - Add mad_gpu for tensor Median Absolute Deviation - Dispatch find_bad_by_PSD and find_bad_by_hfnoise to GPU routines when GPU backend is enabled - Dispatch removeTrend high pass filtering to filter_bandpass_gpu - GPU accelerate Reference.remove_reference tensor subtraction - Full fallback to CPU on GPU exceptions with 100% test parity
…ro division in PSD/noisiness calculation - Add fallback in Reference.perform_reference when all reference channels are flagged as unusable - Guard zero PSD and zero MAD divisions in find_noisy_channels to prevent RuntimeWarnings and pipeline crashes
…selection - Filter reference_channels against actual EEG channel names present in raw.ch_names - Add multi-stage fallback to all valid EEG channels if all candidate reference channels are unusable or missing - Ensure raw_tmp.get_data(picks=...) never receives an empty pick list ([])
…s NaN to eliminate RuntimeWarning
… handling for long recordings - Implement chunked FFT filtering in filter_bandpass_gpu and notch_filter_gpu for signals > 131k samples to prevent GPU/MPS VRAM OOM - Pass on_missing='ignore' in PrepPipeline set_montage call - Safely handle NaN position coordinates in NoisyChannels.find_bad_by_nan when valid 3D montages are attached - Use _safe_interpolate_bads in Reference to avoid MNE pinv errors on non-positioned channels
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.
PR Description: Multi-Device Hardware Acceleration & Signal Processing Engine (
CUDA,MPS,TPU,XPU,HPU)Motivation & Problem Statement
In large-scale EEG/MEG preprocessing pipelines, PyPREP's legacy windowed noise detection (
find_bad_by_correlation, windowed MAD/IQR metrics, bandpass filtering, line noise notch filtering, signal resampling, and RANSAC signal prediction) executed sequentially over hundreds of time windows in Python loops on CPU. For high-density recordings (64–275 channels) or batch processing across large clinical cohorts (e.g. OpenNeuro, BCI2000), this created a significant computational bottleneck.Additionally:
This PR addresses these limitations by introducing a unified, multi-device hardware acceleration backend with zero-copy GPU VRAM tensor caching, zero-phase GPU FIR bandpass filtering, GPU FFT resampling, and GPU FFT notch filtering, while preserving 100% exact numerical match (0.000000e+00 error down to 10^-15 float64 machine epsilon) and zero API breaking changes.
Complete Suite of GPU-Accelerated PyPREP Features
NoisyChannels.__init__()EEGDataTensor&EEGFilteredTensorin VRAMpyprep.gpu.resample_gpu()torch.fft.rfft&irfftpyprep.gpu.notch_filter_gpu()pyprep.gpu.filter_bandpass_gpu()pyprep.gpu.correlate_windows_gpu()torch.bmm) in GPU VRAMpyprep.gpu.find_bad_by_deviation_gpu()pyprep.gpu.ransac_by_window_gpu()torch.matmul) across all windowsPerformance Benchmarks & Stage Speedup Breakdown
1. Compute Hotspot Speedups vs Original
pip install pypreppip install pyprep(CPU Baseline)backend="auto")API Usage
Enabling hardware acceleration requires only optional parameters:
Scientific Parity & Precision Proof
Tested on standard EEGBCI and MATPREP datasets:
pip install pyprepLocal Quality Assurance & Environment Verification
pytest)../.venv) and environment 2 (./pyprep/.venv).pytest tests/test_matprep_compare.py).pyprep/gpu/core.py@ 94%).ruff checkandruff formatwith zero errors.make -C docs html/sphinx-build -b html docs docs/_build/html).Merge Checklist
closes #<issue-number>to automatically close an issueCITATION.cfffile