Skip to content

MRG: Use cupy instead of pycuda+skcuda - #5452

Merged
agramfort merged 10 commits into
mne-tools:masterfrom
larsoner:cupy
Aug 29, 2018
Merged

agramfort merged 10 commits into
mne-tools:masterfrom
larsoner:cupy

Conversation

@larsoner

@larsoner larsoner commented Aug 17, 2018 •

Copy link
Copy Markdown
Member

It turns out using cupy:

  1. Simplifies things: one module to install (cupy) instead of two (pycuda, skcuda)
  2. NumPy-like operations requires less branching (see +/- of equivalent-output code of this PR: +69, -142)
  3. Is slightly faster (10%) in filtering/resampling than old code
  4. Is more future compatible (e.g., using for ReceptiveField should be easier)

So I think in this release we should probably support just cupy. I don't think the burden is so high on users, as it was very easy to install on my system (easier than pycuda for example). I don't think it's a great idea to support both for a release because it will be a lot more work and complications in our code base, but we can do it if absolutely necessary.

Todo:

Closes #5439.

@jona-sassenhagen

Copy link
Copy Markdown
Contributor

Do you think cupy will be maintained reliably? That should probably also be a concern.

Otherwise - looks great!

@jona-sassenhagen

Copy link
Copy Markdown
Contributor

Diff looks great!

@larsoner

Copy link
Copy Markdown
Member Author

The cupy linalg parts already work better than their skcuda counterparts, which bodes well for it bring better maintained. And looking at the pulse it looks pretty actively maintained.

@codecov

codecov Bot commented Aug 17, 2018 •

Copy link
Copy Markdown

Codecov Report

Merging #5452 into master will increase coverage by 0.65%.
The diff coverage is 68.33%.

@@            Coverage Diff             @@
##           master    #5452      +/-   ##
==========================================
+ Coverage   87.57%   88.23%   +0.65%     
==========================================
  Files         358      358              
  Lines       66366    66365       -1     
  Branches    11264    11270       +6     
==========================================
+ Hits        58119    58554     +435     
+ Misses       5460     5002     -458     
- Partials     2787     2809      +22

@larsoner larsoner added this to the 0.17 milestone Aug 18, 2018
@larsoner larsoner changed the title WIP: Use cupy instead of pycuda+skcuda MRG: Use cupy instead of pycuda+skcuda Aug 18, 2018
@larsoner

Copy link
Copy Markdown
Member Author

Okay I worked out bugs and updated the CUDA instructions, which got much simpler because you can just do conda install cupy (!) to get CUDA working now. This is ready for review/merge from my end.

@larsoner

Copy link
Copy Markdown
Member Author

@larsoner

Copy link
Copy Markdown
Member Author

(Turns out they are quick to respond to PRs, too: cupy/cupy#1538)

@larsoner

Copy link
Copy Markdown
Member Author

Failures are unrelated and are in matplotlib/matplotlib#11877, might need to add an xfail until the mpl bug is fixed

@agramfort

Copy link
Copy Markdown
Member

trying a linux machine I get before enabling cuda:

___________________________________________________________________________ test_resample_scipy ___________________________________________________________________________

    @requires_version('scipy', '0.13')  # 0.12 at least has a Nyquist bug
    def test_resample_scipy():
        """Test resampling against SciPy."""
        n_jobs_test = (1, 'cuda')
        for window in ('boxcar', 'hann'):
            for N in (100, 101, 102, 103):
                x = np.arange(N).astype(float)
                err_msg = '%s: %s' % (N, window)
                x_2_sp = sp_resample(x, 2 * N, window=window)
                for n_jobs in n_jobs_test:
                    x_2 = resample(x, 2, 1, 0, window=window, n_jobs=n_jobs)
                    assert_allclose(x_2, x_2_sp, atol=1e-12, err_msg=err_msg)
                new_len = int(round(len(x) * (1. / 2.)))
                x_p5_sp = sp_resample(x, new_len, window=window)
                for n_jobs in n_jobs_test:
                    x_p5 = resample(x, 1, 2, 0, window=window, n_jobs=n_jobs)
>                   assert_allclose(x_p5, x_p5_sp, atol=1e-12, err_msg=err_msg)
E                   AssertionError:
E                   Not equal to tolerance rtol=1e-07, atol=1e-12
E                   100: boxcar
E                   (mismatch 100.0%)
E                    x: array([ 24.5     ,  -4.320516,   7.258379,   3.944628,   9.41837 ,
E                            8.976348,  12.753955,  13.443091,  16.405701,  17.714794,
E                           20.186249,  21.897057,  24.031334,  26.031334,  27.912953,...
E                    y: array([ 25.      ,  -4.820516,   7.758379,   3.444628,   9.91837 ,
E                            8.476348,  13.253955,  12.943091,  16.905701,  17.214794,
E                           20.686249,  21.397057,  24.531334,  25.531334,  28.412953,...

N          = 100
err_msg    = '100: boxcar'
n_jobs     = 1
n_jobs_test = (1, 'cuda')
new_len    = 50
window     = 'boxcar'
x          = array([ 0.,  1.,  2.,  3.,  4.,  5.,  6.,  7.,  8.,  9., 10., 11., 12.,
       13., 14., 15., 16., 17., 18., 19., 20.,...  78., 79., 80., 81., 82., 83., 84., 85., 86., 87., 88., 89., 90.,
       91., 92., 93., 94., 95., 96., 97., 98., 99.])
x_2        = array([-2.27373675e-15, -1.31567412e+01,  1.00000000e+00,  9.04820763e+00,
        2.00000000e+00, -2.65799711e+00,  3...7997e+02,  9.70000000e+01,  8.99517924e+01,
        9.80000000e+01,  1.12156741e+02,  9.90000000e+01,  4.95000000e+01])
x_2_sp     = array([  0.        , -13.15674116,   1.        ,   9.04820763,
         2.        ,  -2.65799711,   3.        ,   7.39... 96.        , 101.65799711,  97.        ,  89.95179237,
        98.        , 112.15674116,  99.        ,  49.5       ])
x_p5       = array([ 24.5       ,  -4.32051595,   7.25837904,   3.94462752,
         9.41837035,   8.97634778,  12.75395463,  13.44...29852,  87.55690902,
        86.24604537,  92.02365222,  89.58162965,  97.05537248,
        91.74162096, 105.32051595])
x_p5_sp    = array([ 25.        ,  -4.82051595,   7.75837904,   3.44462752,
         9.91837035,   8.47634778,  13.25395463,  12.94...29852,  87.05690902,
        86.74604537,  91.52365222,  90.08162965,  96.55537248,
        92.24162096, 104.82051595])

mne/tests/test_filter.py:274: AssertionError
-------------------------------------------------------------------------- Captured stdout call ---------------------------------------------------------------------------
CUDA not enabled in config, skipping initialization
CUDA not used, CUDA could not be initialized, falling back to n_jobs=1
============================================================= 1 failed, 12 passed, 3 skipped in 17.72 seconds ====

thoughts?

@larsoner

Copy link
Copy Markdown
Member Author

Do you also have this error on master?

What is your mne.sys_info()? I wonder if your SciPy is too old / has a bug. I had a dec for 0.12 but didn't triage the exact version.

@larsoner

Copy link
Copy Markdown
Member Author

You should have this on master because it errors on n_jobs=1 already

@larsoner

Copy link
Copy Markdown
Member Author

Updated the decorator to reflect when scipy fixed their Nyquist bug. Upgrade to SciPy >= 1.0 for the test to run. Before that, SciPy had a Nyquist bug.

@agramfort

Copy link
Copy Markdown
Member

worked like a charm on my desktop machine with CUDA.

I will tick the box "Get @agramfort on board" above and merge :)

@agramfort
agramfort merged commit 0fe359e into mne-tools:master Aug 29, 2018
@larsoner
larsoner deleted the cupy branch August 29, 2018 20:27
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.

3 participants