-
Notifications
You must be signed in to change notification settings - Fork 0
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
ENH add batching benchmarks #3
Open
pierreglaser
wants to merge
1
commit into
master
Choose a base branch
from
add-batching-suite
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,149 @@ | ||
#!/usr/bin/env python | ||
# -*- coding: utf-8 -*- | ||
# | ||
# Benchmark suite for joblib | ||
# | ||
# Author: Pierre Glaser | ||
"""Benchmarking the impact of joblib's task batching strategy""" | ||
import tempfile | ||
import time | ||
|
||
from joblib import Parallel, delayed | ||
import numpy as np | ||
|
||
from .common import Benchmark, sleep_noop | ||
|
||
|
||
def bench_short_tasks( | ||
task_times, | ||
input_data_size=0, | ||
output_data_size=0, | ||
memmap_input=False, | ||
parallel_inst=None, | ||
): | ||
|
||
with tempfile.NamedTemporaryFile() as temp_file: | ||
if input_data_size: | ||
# Generate some input data with the required size | ||
if memmap_input: | ||
temp_file.close() | ||
input_data = np.memmap( | ||
temp_file.name, | ||
shape=input_data_size, | ||
dtype=np.byte, | ||
mode="w+", | ||
) | ||
input_data[:] = 1 | ||
else: | ||
input_data = np.ones(input_data_size, dtype=np.byte) | ||
else: | ||
input_data = None | ||
|
||
parallel_inst( | ||
delayed(sleep_noop)(max(t, 0), input_data, output_data_size) | ||
for t in task_times | ||
) | ||
|
||
|
||
class AutoBatchingSuite(Benchmark): | ||
repeat = 1 | ||
number = 1 | ||
warmup_time = 0 | ||
|
||
# In practice, the input size does not influence the benchmarks very much: | ||
# The input is a numpy array. Small numpy arrays are pickled very fast and | ||
# do not incur significant overhead. Large numpy arrays can take time to be | ||
# pickled, but end up being memmapped, which only incurs a one-time | ||
# serialization cost, and thus will stop influence the batching after only | ||
# a few batches. Therefore, we fix this parameter for now. | ||
param_names = ["input_size", "n_jobs"] | ||
params = ([10000, 100000, 1000000], [1, 2, 4]) | ||
parallel_parameters = dict( | ||
verbose=10, backend="loky", pre_dispatch="2*n_jobs" | ||
) | ||
bench_parameters = dict( | ||
output_data_size=int(1e5) # output data size in bytes, | ||
) | ||
|
||
def setup(self, input_size, n_jobs): | ||
# Each benchmark is determined by its parameter set and the | ||
# duration profile of the tasks it has to execute. The following lines | ||
# defines a variety of task duration profiles. | ||
random_state = np.random.RandomState(42) | ||
high_variance = np.abs( | ||
random_state.normal(loc=0.000001, scale=0.01, size=5000) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. For |
||
) | ||
|
||
low_variance = np.empty_like(high_variance) | ||
low_variance[:] = np.mean(high_variance) | ||
|
||
self.high_variance = high_variance | ||
self.low_variance = low_variance | ||
|
||
# Set up a cycling task duration pattern that the auto batching | ||
# feature should be able to roughly track. We use an even power of cos | ||
# to get only positive task durations with a majority close to zero | ||
# (only data transfer overhead). | ||
slow_time = 0.2 | ||
positive_wave = np.cos(np.linspace(0, 6 * np.pi, 2000)) ** 8 | ||
self.cyclic = positive_wave * slow_time | ||
|
||
# Simulate a situation where a few long tasks have to be executed, and | ||
# the first ones are cached. Because for the cached tasks, the apparent | ||
# compute time will seem very small, joblib will increase a lot the | ||
# batch size, which will potentially strangling workers. | ||
self.partially_cached = [1e-3] * 200 + [1] * 50 | ||
|
||
self.parallel = Parallel(n_jobs=n_jobs, **self.parallel_parameters) | ||
|
||
# warm up the executor to mask worker-process creation overhead. | ||
self.parallel(delayed(time.sleep)(0.001) for _ in range(2 * n_jobs)) | ||
|
||
def time_high_variance_no_trend(self, input_size, n_jobs): | ||
bench_short_tasks( | ||
self.high_variance, | ||
parallel_inst=self.parallel, | ||
input_data_size=input_size, | ||
**self.bench_parameters | ||
) | ||
|
||
time_high_variance_no_trend.pretty_name = ( | ||
"Running time to complete tasks with high variance, untrended " | ||
"duration" | ||
) | ||
|
||
def time_low_variance_no_trend(self, input_size, n_jobs): | ||
bench_short_tasks( | ||
self.low_variance, | ||
parallel_inst=self.parallel, | ||
input_data_size=input_size, | ||
) | ||
time_low_variance_no_trend.pretty_name = ( | ||
"Running time when computing tasks with low variance, untrended " | ||
"duration" | ||
) | ||
|
||
def time_cyclic_trend(self, input_size, n_jobs): | ||
return bench_short_tasks( | ||
self.cyclic, | ||
input_data_size=input_size, | ||
parallel_inst=self.parallel, | ||
**self.bench_parameters | ||
) | ||
|
||
time_cyclic_trend.pretty_name = ( | ||
"Running time when computing tasks with cyclically trended duration" | ||
) | ||
|
||
def time_partially_cached(self, input_size, n_jobs): | ||
return bench_short_tasks( | ||
self.partially_cached, | ||
input_data_size=input_size, | ||
parallel_inst=self.parallel, | ||
**self.bench_parameters | ||
) | ||
|
||
time_partially_cached.pretty_name = ( | ||
"Running time when computing long tasks, some of which already cached " | ||
"using joblib.Memory" | ||
) |
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe you could also run the benchmark with auto-memmap disabled
max_nbytes=None
?