-
-
Notifications
You must be signed in to change notification settings - Fork 215
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
Add a benchmark/example for numexpr usage under free-threading conditions #508
Draft
andfoy
wants to merge
1
commit into
pydata:master
Choose a base branch
from
andfoy:free_threaded_example
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.
Draft
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,154 @@ | ||
################################################################################# | ||
# To mimic the scenario that computation is i/o bound and constrained by memory | ||
# | ||
# It's a much simplified version that the chunk is computed in a loop, | ||
# and expression is evaluated in a sequence, which is not true in reality. | ||
# Neverthless, numexpr outperforms numpy. | ||
################################################################################# | ||
""" | ||
Benchmarking Expression 1: | ||
NumPy time (threaded over 32 chunks with 2 threads): 4.612313 seconds | ||
numexpr time (threaded with re_evaluate over 32 chunks with 2 threads): 0.951172 seconds | ||
numexpr speedup: 4.85x | ||
---------------------------------------- | ||
Benchmarking Expression 2: | ||
NumPy time (threaded over 32 chunks with 2 threads): 23.862752 seconds | ||
numexpr time (threaded with re_evaluate over 32 chunks with 2 threads): 2.182058 seconds | ||
numexpr speedup: 10.94x | ||
---------------------------------------- | ||
Benchmarking Expression 3: | ||
NumPy time (threaded over 32 chunks with 2 threads): 20.594895 seconds | ||
numexpr time (threaded with re_evaluate over 32 chunks with 2 threads): 2.927881 seconds | ||
numexpr speedup: 7.03x | ||
---------------------------------------- | ||
Benchmarking Expression 4: | ||
NumPy time (threaded over 32 chunks with 2 threads): 12.834101 seconds | ||
numexpr time (threaded with re_evaluate over 32 chunks with 2 threads): 5.392480 seconds | ||
numexpr speedup: 2.38x | ||
---------------------------------------- | ||
""" | ||
|
||
import os | ||
|
||
os.environ["NUMEXPR_NUM_THREADS"] = "1" | ||
import threading | ||
import timeit | ||
|
||
import numpy as np | ||
|
||
import numexpr as ne | ||
|
||
array_size = 10**8 | ||
num_runs = 10 | ||
num_chunks = 32 # Number of chunks | ||
num_threads = 16 # Number of threads constrained by how many chunks memory can hold | ||
|
||
a = np.random.rand(array_size).reshape(10**4, -1) | ||
b = np.random.rand(array_size).reshape(10**4, -1) | ||
c = np.random.rand(array_size).reshape(10**4, -1) | ||
|
||
chunk_size = array_size // num_chunks | ||
|
||
expressions_numpy = [ | ||
lambda a, b, c: a + b * c, | ||
lambda a, b, c: a**2 + b**2 - 2 * a * b * np.cos(c), | ||
lambda a, b, c: np.sin(a) + np.log(b) * np.sqrt(c), | ||
lambda a, b, c: np.exp(a) + np.tan(b) - np.sinh(c), | ||
] | ||
|
||
expressions_numexpr = [ | ||
"a + b * c", | ||
"a**2 + b**2 - 2 * a * b * cos(c)", | ||
"sin(a) + log(b) * sqrt(c)", | ||
"exp(a) + tan(b) - sinh(c)", | ||
] | ||
|
||
|
||
def benchmark_numpy_chunk(func, a, b, c, results, indices): | ||
for index in indices: | ||
start = index * chunk_size | ||
end = (index + 1) * chunk_size | ||
time_taken = timeit.timeit( | ||
lambda: func(a[start:end], b[start:end], c[start:end]), number=num_runs | ||
) | ||
results.append(time_taken) | ||
|
||
|
||
def benchmark_numexpr_re_evaluate(expr, a, b, c, results, indices): | ||
for index in indices: | ||
start = index * chunk_size | ||
end = (index + 1) * chunk_size | ||
# if index == 0: | ||
# Evaluate the first chunk with evaluate | ||
time_taken = timeit.timeit( | ||
lambda: ne.evaluate( | ||
expr, | ||
local_dict={ | ||
"a": a[start:end], | ||
"b": b[start:end], | ||
"c": c[start:end], | ||
}, | ||
), | ||
number=num_runs, | ||
) | ||
# else: | ||
# Re-evaluate subsequent chunks with re_evaluate | ||
# time_taken = timeit.timeit( | ||
# lambda: ne.re_evaluate( | ||
# local_dict={"a": a[start:end], "b": b[start:end], "c": c[start:end]} | ||
# ), | ||
# number=num_runs, | ||
# ) | ||
results.append(time_taken) | ||
|
||
|
||
def run_benchmark_threaded(): | ||
chunk_indices = list(range(num_chunks)) | ||
|
||
for i in range(len(expressions_numpy)): | ||
print(f"Benchmarking Expression {i+1}:") | ||
|
||
results_numpy = [] | ||
results_numexpr = [] | ||
|
||
threads_numpy = [] | ||
for j in range(num_threads): | ||
indices = chunk_indices[j::num_threads] # Distribute chunks across threads | ||
thread = threading.Thread( | ||
target=benchmark_numpy_chunk, | ||
args=(expressions_numpy[i], a, b, c, results_numpy, indices), | ||
) | ||
threads_numpy.append(thread) | ||
thread.start() | ||
|
||
for thread in threads_numpy: | ||
thread.join() | ||
|
||
numpy_time = sum(results_numpy) | ||
print( | ||
f"NumPy time (threaded over {num_chunks} chunks with {num_threads} threads): {numpy_time:.6f} seconds" | ||
) | ||
|
||
threads_numexpr = [] | ||
for j in range(num_threads): | ||
indices = chunk_indices[j::num_threads] # Distribute chunks across threads | ||
thread = threading.Thread( | ||
target=benchmark_numexpr_re_evaluate, | ||
args=(expressions_numexpr[i], a, b, c, results_numexpr, indices), | ||
) | ||
threads_numexpr.append(thread) | ||
thread.start() | ||
|
||
for thread in threads_numexpr: | ||
thread.join() | ||
|
||
numexpr_time = sum(results_numexpr) | ||
print( | ||
f"numexpr time (threaded with re_evaluate over {num_chunks} chunks with {num_threads} threads): {numexpr_time:.6f} seconds" | ||
) | ||
print(f"numexpr speedup: {numpy_time / numexpr_time:.2f}x") | ||
print("-" * 40) | ||
|
||
|
||
if __name__ == "__main__": | ||
run_benchmark_threaded() |
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.
@FrancescAlted, it seems that this lock is a performance bottleneck when multiple threads are ran in parallel, is it necessary?
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.
With the lock:
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.
Without locking:
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.
Uh, that was introduced long ago, so I don't remember at all; but if it is there, I'd say that it is necessary, yes. Just to double check, what happens to the test suite if you remove the lock?
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.
Tests are passing locally and on CI
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.
Yep, I did my tests locally on a series of packages that depend on numexpr, and everything seems fine with removing the lock. So, feel free in proceeding with the lock removal.
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 the lock was in place to prevent concurrent access to the caches, which shouldn't be much of an issue now, given they were made thread-local