Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Multicore GPU/MultiGPU sampling support #3341

Open
adelkhafizova opened this issue Jan 16, 2019 · 9 comments

Comments

Projects
None yet
5 participants
@adelkhafizova
Copy link

commented Jan 16, 2019

Hi! I’m trying to speed up MCMC sampling for Bayesian Multinomial Regression using GPU, the code is below. When using CPU, pymc3 utilizes as many cores as it can while sampling 4 chains at once. Multicore support fails when sampling on GPU (pm.sample with arguments cores=4), it gives “RuntimeError: Chain 2 fails”. Is there any way to parallelize computations using GPU (via using more cores or more GPUs)? At the moment using CPU with 32 cores is faster than doing the same on 1 core and 1 GPU.

def make_model(X, y):
    with pm.Model() as model:
        sd_alpha = pm.HalfCauchy('sd_alpha', beta=10)
        sd_beta = pm.HalfCauchy('sd_beta', beta=10)
        alpha = pm.Normal('alpha', mu=0, sd=sd_alpha, shape=n_classes)
        beta = pm.Normal('beta', mu=0, sd=sd_beta, shape=(n_features, n_classes))
        mu = tt.dot(X, beta) + alpha
        p = pm.Deterministic('p', tt.nnet.softmax(mu))
        label = pm.Categorical('label', p=p, observed=y)
    return model

X_shared = theano.shared(np.asarray(X_tr, theano.config.floatX))
y_shared = theano.shared(np.asarray(y_tr, theano.config.floatX))
model = make_model(X_shared, y_shared)

with model:
    niter = 500
    tune = 500
    step = pm.NUTS()
    trace = pm.sample(niter, tune=tune, chains=4, cores=4, init='jitter+adapt_diag', step=step)

The full log is attached as a file.
log.txt

Versions and components:

  • PyMC3 Version: 3.5
  • Theano Version: 1.0.2, CUDA 9.0, cuDNN 7005
  • Python Version: 3.7
  • Operating system: Ubuntu 16.04.3
  • How did you install PyMC3: conda

@adelkhafizova adelkhafizova changed the title Multicore GPU/MultiGPU sampling support Multicore GPU/MultiGPU sampling support [beginner_friendly] [gpu] Jan 16, 2019

@adelkhafizova adelkhafizova changed the title Multicore GPU/MultiGPU sampling support [beginner_friendly] [gpu] Multicore GPU/MultiGPU sampling support Jan 16, 2019

@twiecki

This comment has been minimized.

Copy link
Member

commented Jan 16, 2019

GPU sampling and multi-GPU are not supported, so I'm afraid you are largely on your own in figuring this out (of course great if someone else could chime in here). E.g. #2040 or @Spaak.

@adelkhafizova

This comment has been minimized.

Copy link
Author

commented Jan 25, 2019

Thanks for the answer.
For now the workaround is to launch sampling on different GPU's independently and merge chains afterwards.

@twiecki

This comment has been minimized.

Copy link
Member

commented Jan 25, 2019

@adelkhafizova Do you see a speed-up with GPU sampling?

@dsvolk

This comment has been minimized.

Copy link

commented Jan 30, 2019

Me and @adelkhafizova performed a few tests in different configurations. For our model, we observed about 9x speed-up of NUTS sampling when ran on 4 GPUs in parallel as compared to 32 CPU cores.

The model is a hierarchical multinomial regression with some additional terms.

@ericmjl

This comment has been minimized.

Copy link
Contributor

commented Jan 30, 2019

@adelkhafizova and @dsvolk, thanks for reporting back on this! I am wondering if you have sample code for how you launched sampling on independent GPUs, and code for how you merged chains? Did you have to launch different Python processes for this? (I'm asking mostly out of curiosity, with also the hope that I might be able to implement this myself.)

@junpenglao

This comment has been minimized.

Copy link
Member

commented Jan 30, 2019

I am very interested as well - would be great if you can share your code.

@twiecki

This comment has been minimized.

Copy link
Member

commented Jan 30, 2019

@adelkhafizova

This comment has been minimized.

Copy link
Author

commented Jan 30, 2019

If being very short the main idea is to launch processes for each CUDA device in parallel (one process per one device), dump traces independently and use merge_chains function from pymc3.backends.base to get MultiTrace object with several chains. There is a little bug in merge_chains (setting a property without defined setter method). It is also important to set different chain_idx in different processes while sampling traces. Hope to prepare a more thorough example in a few days.

@dsvolk

This comment has been minimized.

Copy link

commented Feb 13, 2019

A fix for the bug in merge_traces():
#3374

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.