Repository navigation
Description
Dear devs,
I have run into this unexpected behaviour while running the sbi.inference.simulate_for_sbi method.
It appears that, in a multi-processing scenario, the single core saturation is capped at a certain value that decreeses in function of the number of simulations to be produced. This inevitably leads to increased simulation times.
I have tried to circumvent the sbi.inference.simulate_for_sbi method both my manually implementing multi-processing (with the straightforward Python multiprocessing.Pool) and with the p_map method from the p_tqdm library. The latter has the advantage of being extremely easy to use, and of natively supporting tqdm execution bars. These two options correctly saturate all the CPUs involved in the computation.
I wonder if this behaviour is coming from the choice of joblib as multiprocessing library, and if a switch to p_tqdm could improve the performance of the SBI library.
Below, I provide some code to test this behaviour. The requirements are the source version of sbi as well as the PIP version of p_tqdm. The code contains three benchmarking methods (vanilla Pool, p_map and simulate_for_sbi) as well as two handles for the number of processes (workers) to be used and the number of simulations to be generated.
By looking at the ETA, the runtime difference between simulate_for_sbi and p_map is negligible for a low simulation number (e.g. 100) but starts increasing together with it, arriving at a factor 10 difference in case of a num_simulations equal to 1M.
I hope this report can be helpful, especially since the simulation runtime represents a bottleneck in many common usecases.
from multiprocessing import Pool
from time import time
from datetime import datetime, timedelta
import numpy as np
import torch
from p_tqdm import p_map
from sbi.utils import BoxUniform
from sbi.utils.user_input_checks import process_simulator, process_prior
from sbi.inference import simulate_for_sbi
def timeit(func):
'''Decorator method for printing the execution time'''
def wrapper(*args, **kwargs):
print(f'\n > Generation started:\t{datetime.now().strftime("%H:%M:%S")}')
beg = time()
func(*args, **kwargs)
end = time()
print(f' > Generation complete:\t{datetime.now().strftime("%H:%M:%S")}')
tds = str(timedelta(seconds=end-beg)).split(':')
print(f' > Total runtime:\t{tds[0]}h {tds[1]}min {tds[2]:.4}s')
return wrapper
def simulation(theta):
'''Some generic simulation function.
You can change this to study, e.g. the impact of numpy's multithreading
on the code's multiprocessing - np.linalg.eig
'''
# matrix = np.random.rand(200,200)/theta
# eigval, eigvec = np.linalg.eig(matrix)
for _ in range(100):
np.random.randn(10000)
return 1
@timeit
def run_simulations_pool(prior, num_simulations, num_processes):
'''Generates the joint using python's native Pool multiprocessing'''
theta_low = prior.base_dist.low.cpu().numpy()
theta_high = prior.base_dist.high.cpu().numpy()
theta_range = theta_high - theta_low
thetas = np.random.uniform(size=(num_simulations,1)) * theta_range + theta_low
with Pool(num_processes) as p:
x = p.map(simulation, thetas)
@timeit
def run_simulations_p_map(prior, num_simulations, num_processes):
'''Generates the joint using p_map from the p_tqdm library'''
theta_low = prior.base_dist.low.cpu().numpy()
theta_high = prior.base_dist.high.cpu().numpy()
theta_range = theta_high - theta_low
thetas = np.random.uniform(size=(num_simulations,1)) * theta_range + theta_low
x = p_map(simulation, thetas, num_cpus=num_processes)
@timeit
def run_simulations_sim_for_sbi(prior, num_simulations, num_procsses):
'''Generates the joint using sbi.inference.simulate_for_sbi'''
prior, num_parameters, prior_returns_numpy = process_prior(prior)
simulator_wrapper = process_simulator(simulation, prior, prior_returns_numpy)
theta, x = simulate_for_sbi(simulator_wrapper,
prior,
num_simulations,
num_procsses)
if __name__ == '__main__':
prior = BoxUniform(low=torch.tensor([0.]),
high=torch.tensor([10.]),
device='cpu')
num_simulations = 1000000
num_processes = 16
# Uncomment the benchmark that you want to run.
# The the native pool benchmark is just as a ground truth to test.
# print('Benchmarking: sbi.inference.simulate_for_sbi')
# run_simulations_sim_for_sbi(prior, num_simulations, num_processes)
# print('Benchmarking: p_map (p_tqdm)')
# run_simulations_p_map(prior, num_simulations, num_processes)
# print('Benchmarking: native Pool')
# run_simulations_pool(prior, num_simulations, num_processes)
Activity
[-]simulate_for_sbi not saturating CPU[/-][+]`simulate_for_sbi` not saturating CPU[/+]janfb commentedon Jun 17, 2024
Dear @janko-petkovic
Thanks a lot for looking into this and proving the benchmark. I can confirm your findings. This is indeed a very interesting behavior of
simulate_for_sbi. The "embarrassingly parallel" for loops we used here seem to be embarrassingly slow 😬I think the reason could be the progress bar wrappers we use to show
tqdmprogress bars. They can are blocking the underlying processes, leading to the reduced CPU usage. Combiningjoblibwithtqdmhas been an issue for some time. But there has been some improvements tojoblibrecently which now make it possible to combine more elegantly (and performant). It's now possible to return theParallelobject as a generator which can be passed totqdmdirectly, see joblib/joblib#972 (comment).Indeed, if I add the following minimal joblib function to the benchmark I get the same performance as with
p_tqdm:Results:

janfb commentedon Jun 17, 2024
Looking into the code for
simulate_for_sbiandsimulate_in_batches, I also see that it really needs some refactoring. I will make a PR. If you like you can contribute thejoblibfix there as well.plcrodrigues commentedon Jun 17, 2024
@tomMoral might have a word or two to say about this?
janko-petkovic commentedon Jun 17, 2024
Very glad to see this! Also, very interestingly, the fix you proposed seems to work better than
p_tqdmand the currentsimulate_for_sbiin the case whichwhere the vectorization of
np.linalg.eigis known to sometimes interfere with the higher level multiprocessing. Good that joblib takes care of this and that the fix can be implemented somewhat easily!Sure, I'd love to. How can we organize?
janfb commentedon Jun 17, 2024
yes please!
janfb commentedon Jun 17, 2024
Please follow the instructions here: https://github.com/sbi-dev/sbi/blob/main/docs/docs/contribute.md
In short: create a fork, work on your own feature branch, install pre-commit to run hooks for code style conventions, then make a PR. Let me know if anything is unclear.
Regarding the refactoring: I would
simulate_in_batchesentirely and just make a call to joblib wrapped intqdmdirectly insimulate_for_sbisimulate_in_batches)batch_size>1(seesimulate_in_batches)One strange thing I noticed and haven't understood yet: In the benchmark, you sampled thetas using a
numpy.randomcall inside each function. You could have just sampled from thepriorwithprior.sample((num_simulations,).However, if I do so, the following call to
joblibis much slower 🤔 , e.g.,600 it / svs.150 it / s. Can you confirm this?janko-petkovic commentedon Jun 17, 2024
Great! I will start working on it then.
Concerning your observeation: yes, I can confirm that, and it seems to happen because, again, the CPU saturation is reduced. From what I see, it is connected to the fact that
prior.sampleoutputs atorch.Tensor. Indeed, witheverything goes back to normal, and che CPU returns to full saturation, with equal runtimes across the board. Why
torchconflicts withjoblibI have, unfortunately, no idea yet 😆janfb commentedon Jun 18, 2024
Here is another short benchmark to check how the simulator changes the parallelization with
joblib. It's really brute force, but I wanted a clear picture for the strange behavior from above.I test four different simulators. One that
numpyarrays and usesnumpy.random.randn,numpyarrays but internally usestorch.randntorcharguments and returns.additionally I also test whether passing a list of args to
joblibchanges the timings.Here are the results:

To summarize: It seems that
torch.randnis just much faster thannp.random.randnand that the type of the inputs or returns does not matter.ChatGPT even created a histogram from it, which shows it quite clearly: argument type does not matter, but torch vs numpy does, i.e., internally using torch is much faster.

To run it, do
pytest benchmark_file.py::test_joblib_benchmark -sjanko-petkovic commentedon Jun 18, 2024
I am able to reproduce what you find on my machine; indeed,
torch.randseems to be much faster.One important thing however: 10000 runs do not incur in any unexpected behaviour; to observe it clearly, 1M runs are necessary. In this latter case, one can see that:
thetaof typenumpyandlistproduces benchmarks with300 - 1000 it/s, with the correct difference betweentorch.randandnp.random.randthetaof typetorchprocudes benchmarks around100 it/s, together with a reduced CPU saturation (on linux one can see it directly onhtopwhile running the tests).I think that this behaviour does not depend on
torch.randbut on some "conflict" betweenjoblibandtorch. I still need to read more into this however.janfb commentedon Jun 18, 2024
OK, thanks for checking this. It would be interesting to get @tomMoral take on this.
also, this https://pytorch.org/docs/stable/notes/multiprocessing.html might be a relevant read.
janko-petkovic commentedon Jun 26, 2024
Hello guys. A quick update on the state of the issue. After discussing with @janfb, we decided to proceed as follows:
tests/cpu_saturation_test.py). This test is meant to let the tester observe the CPU usage (via the process manager orhtop) while a large number of simulations is running.simulate_for_sbi. After (if) accepted, one should run again thecpu_saturation_test.pyand compare the results, hopefully noticing considerable improvement.I will now do the first PR. Please let me know if you have any concerns.