-
Notifications
You must be signed in to change notification settings - Fork 6
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
initial stab at the numba.typed.List benchmarks #10
Open
esc
wants to merge
17
commits into
master
Choose a base branch
from
benchmarks/numba.typed.List
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
Show all changes
17 commits
Select commit
Hold shift + click to select a range
75af513
initial stab at the numba.typed.List benchmarks
esc d0558e5
adding seeding for PRNG
esc c997b5f
this measures compile-time for sort
esc 5eeeb6c
benchmark with fastmath and w/o
esc 7234402
make sure we really do recompile
esc 255a119
increase the min_run_count of this suite to 5
esc 5d0fce2
extract clearing the dispatcher
esc 8732ef8
implement benchmarking compilation time too for the reduction case
esc cbf3e8c
enhance the typed-list benchmarks some more
esc ca0011f
add benchmarks for getitem_unchecked
esc 921321b
adding the ArrayListSuite
esc 6957bbc
fix benchmarking parameters, elim warm-up time
esc 27fb3d8
fix signature for float suits
esc db6103e
fixup the array list tests
esc fb90310
upgrade the README with instructions on how to compare results
esc a6be44c
add missing benchmarks.json file
esc 5c870d3
respond to PR feedback and improve docs
esc 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
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,294 @@ | ||
""" | ||
Benchmarks for numba.typed.List | ||
""" | ||
|
||
import numpy as np | ||
from numba import njit | ||
from numba.typed import List | ||
from numba.typed.typedlist import _sort | ||
from numba.core.registry import dispatcher_registry | ||
from numba.core.typing import Signature | ||
from numba.core.types import ListType, int64, float64, none, boolean, Array | ||
|
||
SIZE = 10**5 | ||
SEED = 23 | ||
|
||
def check_getitem_unchecked(): | ||
|
||
@njit | ||
def use_getitem_unchecked(): | ||
tl = List((1, 2, 3, 4)) | ||
return tl.getitem_unchecked(0) | ||
|
||
try: | ||
use_getitem_unchecked() | ||
except Exception: | ||
return False | ||
else: | ||
return True | ||
|
||
|
||
have_getitem_unchecked = check_getitem_unchecked() | ||
|
||
|
||
@njit | ||
def make_random_typed_list_int(n): | ||
tl = List() | ||
np.random.seed(SEED) | ||
for i in range(n): | ||
tl.append(np.random.randint(0, 100)) | ||
return tl | ||
|
||
@njit | ||
def make_random_typed_list_float(n): | ||
tl = List() | ||
np.random.seed(SEED) | ||
for i in range(n): | ||
tl.append(np.random.randn()) | ||
return tl | ||
|
||
@njit | ||
def make_random_typed_list_array(n): | ||
tl = List() | ||
np.random.seed(SEED) | ||
for i in range(n): | ||
tl.append(np.zeros(4,)) | ||
return tl | ||
|
||
def make_random_python_list(n): | ||
pl = list() | ||
np.random.seed(SEED) | ||
for i in range(n): | ||
pl.append(np.random.randint(0, 100)) | ||
return pl | ||
|
||
|
||
def clear_dispatcher(dispatcher): | ||
dispatcher._make_finalizer()() | ||
dispatcher._reset_overloads() | ||
dispatcher._cache.flush() | ||
dispatcher._can_compile = True | ||
|
||
|
||
class BaseSuite: | ||
min_run_count = 5 | ||
warmup_time = 0.0 | ||
number = 1 | ||
repeat = 5 | ||
|
||
|
||
class SortSuite(BaseSuite): | ||
|
||
def setup(self): | ||
self.tl = make_random_typed_list_int(SIZE) | ||
self.tl.sort() | ||
self.dispatcher = dispatcher_registry['cpu'](_sort.py_func) | ||
self.signature = Signature(none, | ||
[ListType(int64), none, boolean], | ||
None) | ||
clear_dispatcher(self.dispatcher) | ||
|
||
def time_execute_sort(self): | ||
self.tl.sort() | ||
|
||
def time_compile_sort(self): | ||
self.dispatcher.compile(self.signature) | ||
|
||
|
||
class ConstructionSuite(BaseSuite): | ||
|
||
def setup(self): | ||
self.pl = make_random_python_list(SIZE) | ||
List(self.pl) | ||
|
||
def time_construct_from_python_list(self): | ||
List(self.pl) | ||
|
||
def time_construct_in_njit_function(self): | ||
make_random_typed_list_int(SIZE) | ||
|
||
|
||
class ReductionSuite(BaseSuite): | ||
|
||
def setup(self): | ||
raise NotImplementedError | ||
|
||
def post_setup(self): | ||
|
||
self.reduction_sum = self.define_function() | ||
|
||
self.reduction_sum_fastmath = njit(self.reduction_sum, fastmath=True) | ||
self.reduction_sum_no_fastmath = njit(self.reduction_sum) | ||
|
||
self.reduction_sum_fastmath(self.tl) | ||
self.reduction_sum_no_fastmath(self.tl) | ||
|
||
self.fastmath_dispatcher = dispatcher_registry['cpu']( | ||
self.reduction_sum_fastmath.py_func) | ||
self.fastmath_dispatcher.targetoptions['fastmath'] = True | ||
|
||
self.no_fastmath_dispatcher = dispatcher_registry['cpu']( | ||
self.reduction_sum_no_fastmath.py_func) | ||
|
||
|
||
clear_dispatcher(self.fastmath_dispatcher) | ||
clear_dispatcher(self.no_fastmath_dispatcher) | ||
|
||
def time_execute_reduction_sum_fastmath(self): | ||
self.reduction_sum_fastmath(self.tl) | ||
|
||
def time_compile_reduction_sum_fastmath(self): | ||
self.fastmath_dispatcher.compile(self.signature) | ||
|
||
def time_execute_reduction_sum_no_fastmath(self): | ||
self.reduction_sum_no_fastmath(self.tl) | ||
|
||
def time_compile_reduction_sum_no_fastmath(self): | ||
self.no_fastmath_dispatcher.compile(self.signature) | ||
|
||
|
||
class IteratorReductionSuiteInt(ReductionSuite): | ||
|
||
def setup(self): | ||
|
||
self.tl = make_random_typed_list_int(SIZE) | ||
self.signature = Signature(int64, [ListType(int64)], None) | ||
self.post_setup() | ||
|
||
def define_function(self): | ||
|
||
def reduction_sum(tl): | ||
agg = 0 | ||
for i in tl: | ||
agg += i | ||
return agg | ||
|
||
return reduction_sum | ||
|
||
class IteratorReductionSuiteFloat(ReductionSuite): | ||
|
||
def setup(self): | ||
|
||
self.tl = make_random_typed_list_float(SIZE) | ||
self.signature = Signature(float64, [ListType(float64)], None) | ||
self.post_setup() | ||
|
||
def define_function(self): | ||
|
||
def reduction_sum(tl): | ||
agg = 0.0 | ||
for i in tl: | ||
agg += i | ||
return agg | ||
|
||
return reduction_sum | ||
|
||
class ForLoopReductionSuiteInt(ReductionSuite): | ||
|
||
def setup(self): | ||
|
||
self.tl = make_random_typed_list_int(SIZE) | ||
self.signature = Signature(int64, [ListType(int64)], None) | ||
self.post_setup() | ||
|
||
def define_function(self): | ||
|
||
def reduction_sum(tl): | ||
agg = 0 | ||
length = len(tl) | ||
for i in range(length): | ||
agg += tl[i] | ||
return agg | ||
|
||
return reduction_sum | ||
|
||
class ForLoopReductionSuiteFloat(ReductionSuite): | ||
|
||
def setup(self): | ||
|
||
self.tl = make_random_typed_list_float(SIZE) | ||
self.signature = Signature(float64, [ListType(float64)], None) | ||
self.post_setup() | ||
|
||
def define_function(self): | ||
|
||
def reduction_sum(tl): | ||
agg = 0.0 | ||
length = len(tl) | ||
for i in range(length): | ||
agg += tl[i] | ||
return agg | ||
|
||
return reduction_sum | ||
|
||
|
||
class GetitemUncheckedReductionSuiteInt(ReductionSuite): | ||
|
||
def setup(self): | ||
|
||
self.tl = make_random_typed_list_int(SIZE) | ||
self.signature = Signature(int64, [ListType(int64)], None) | ||
self.post_setup() | ||
|
||
def define_function(self): | ||
|
||
def reduction_sum_regular(tl): | ||
agg = 0 | ||
length = len(tl) | ||
for i in range(length): | ||
agg += tl[i] | ||
return agg | ||
|
||
def reduction_sum_unchecked(tl): | ||
agg = 0 | ||
length = len(tl) | ||
for i in range(length): | ||
agg += tl.getitem_unchecked(i) | ||
return agg | ||
|
||
return reduction_sum_unchecked if have_getitem_unchecked else reduction_sum_regular | ||
|
||
class GetitemUncheckedReductionSuiteFloat(ReductionSuite): | ||
|
||
def setup(self): | ||
|
||
self.tl = make_random_typed_list_float(SIZE) | ||
self.signature = Signature(float64, [ListType(float64)], None) | ||
self.post_setup() | ||
|
||
def define_function(self): | ||
|
||
def reduction_sum_regular(tl): | ||
agg = 0.0 | ||
length = len(tl) | ||
for i in range(length): | ||
agg += tl[i] | ||
return agg | ||
|
||
def reduction_sum_unchecked(tl): | ||
agg = 0.0 | ||
length = len(tl) | ||
for i in range(length): | ||
agg += tl.getitem_unchecked(i) | ||
return agg | ||
|
||
return reduction_sum_unchecked if have_getitem_unchecked else reduction_sum_regular | ||
|
||
|
||
class ArrayListSuite(ReductionSuite): | ||
|
||
def setup(self): | ||
|
||
self.tl = make_random_typed_list_array(SIZE) | ||
self.signature = Signature(float64, [ListType(Array(float64, 1, 'C'))], None) | ||
self.post_setup() | ||
|
||
def define_function(self): | ||
|
||
def array_reduction(tl): | ||
agg = 0.0 | ||
for i in tl: | ||
agg += i.sum() | ||
return agg | ||
|
||
return array_reduction |
Oops, something went wrong.
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.
it should seed the the rng
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.
done