-
Notifications
You must be signed in to change notification settings - Fork 44
Tests overhaul #143
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
Merged
+106
−122
Merged
Tests overhaul #143
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
0035444
Tweak test tolerances
crusaderky 820905c
Tweak strategies ranges
crusaderky 3d0f9ad
Add central control for number of examples tested
crusaderky 0fe793e
New test for dotv invalid use case
crusaderky 5c90b69
Overaul hypothesis strategies
crusaderky 555d35f
Add threading tests for shared input
crusaderky 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 hidden or 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 hidden or 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 |
|---|---|---|
| @@ -1,32 +1,35 @@ | ||
| # Copyright ExplosionAI GmbH, released under BSD. | ||
| import numpy as np | ||
|
|
||
| np.random.seed(0) | ||
|
|
||
| from hypothesis.strategies import tuples, integers, floats | ||
| from concurrent.futures import ThreadPoolExecutor | ||
| from hypothesis import settings | ||
| from hypothesis import strategies as st | ||
| from hypothesis.extra.numpy import arrays | ||
|
|
||
|
|
||
| def lengths(lo=1, hi=10): | ||
| return integers(min_value=lo, max_value=hi) | ||
|
|
||
|
|
||
| def ndarrays_of_shape(shape, lo=-1000.0, hi=1000.0, dtype="float64"): | ||
| width = 64 if dtype == "float64" else 32 | ||
| return arrays( | ||
| dtype, | ||
| shape=shape, | ||
| elements=floats( | ||
| min_value=lo, | ||
| max_value=hi, | ||
| width=width, | ||
| ), | ||
| ) | ||
|
|
||
|
|
||
| def ndarrays( | ||
| min_len=0, max_len=10, min_val=-10000000.0, max_val=1000000.0, dtype="float64" | ||
| ): | ||
| return lengths(lo=min_len, hi=max_len).flatmap( | ||
| lambda n: ndarrays_of_shape(n, lo=min_val, hi=max_val, dtype=dtype) | ||
| ) | ||
| # Increase this to run more thorough tests | ||
| hypothesis_default_profile = settings.register_profile("default", max_examples=200) | ||
|
|
||
| # (dtype, atol, rtol) | ||
| dtypes_tols = st.sampled_from( | ||
| [ | ||
| # FIXME huge tolerance needed for float32: | ||
| # https://github.com/explosion/cython-blis/issues/142 | ||
| ("float32", 1e-2, 1e-4), | ||
| ("float64", 1e-9, 1e-9), | ||
| ] | ||
| ) | ||
|
|
||
|
|
||
| def ndarrays(shape, lo, hi, dtype): | ||
| """Draw ND NumPy arrays of floats""" | ||
| assert isinstance(dtype, str) and dtype.startswith("float") | ||
| width = int(dtype[5:]) | ||
| elements = st.floats(min_value=lo, max_value=hi, width=width) | ||
| return arrays(dtype, shape=shape, elements=elements) | ||
|
|
||
|
|
||
| def run_threaded(func, max_workers=8, outer_iterations=2): | ||
| """Runs a function many times in parallel""" | ||
| with ThreadPoolExecutor(max_workers=max_workers) as tpe: | ||
| for _ in range(outer_iterations): | ||
| futures = [tpe.submit(func) for _ in range(max_workers)] | ||
| for f in futures: | ||
| f.result() | ||
This file contains hidden or 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 |
|---|---|---|
| @@ -1,38 +1,45 @@ | ||
| # Copyright ExplosionAI GmbH, released under BSD. | ||
| from hypothesis import given, assume | ||
|
|
||
| import numpy as np | ||
| import pytest | ||
| from hypothesis import given | ||
| from hypothesis import strategies as st | ||
| from numpy.testing import assert_allclose | ||
| from blis_tests_common import ndarrays | ||
|
|
||
| from blis_tests_common import dtypes_tols, ndarrays, run_threaded | ||
| from blis.py import dotv | ||
|
|
||
|
|
||
| @given( | ||
| ndarrays(min_len=10, max_len=100, min_val=-100.0, max_val=100.0, dtype="float64"), | ||
| ndarrays(min_len=10, max_len=100, min_val=-100.0, max_val=100.0, dtype="float64"), | ||
| ) | ||
| def test_memoryview_double_noconj(A, B): | ||
| if len(A) < len(B): | ||
| B = B[: len(A)] | ||
| else: | ||
| A = A[: len(B)] | ||
| assume(A is not None) | ||
| assume(B is not None) | ||
| @st.composite | ||
| def dotv_arrays(draw): | ||
| """Draw two 1D NumPy arrays of the same size and dtype""" | ||
| size = draw(st.integers(min_value=1, max_value=100)) | ||
| dtype, atol, rtol = draw(dtypes_tols) | ||
| arr_st = ndarrays((size,), lo=-100.0, hi=100.0, dtype=dtype) | ||
| return draw(arr_st), draw(arr_st), atol, rtol | ||
|
|
||
|
|
||
| def test_incompatible_shape(): | ||
| with pytest.raises(ValueError): | ||
| dotv(np.zeros(2), np.zeros(3)) | ||
|
|
||
|
|
||
| @given(dotv_arrays()) | ||
| def test_memoryview_noconj(arrays): | ||
| A, B, atol, rtol = arrays | ||
| numpy_result = A.dot(B) | ||
| result = dotv(A, B) | ||
| assert_allclose([numpy_result], result, atol=1e-3, rtol=1e-3) | ||
|
|
||
|
|
||
| @given( | ||
| ndarrays(min_len=10, max_len=100, min_val=-100.0, max_val=100.0, dtype="float32"), | ||
| ndarrays(min_len=10, max_len=100, min_val=-100.0, max_val=100.0, dtype="float32"), | ||
| ) | ||
| def test_memoryview_float_noconj(A, B): | ||
| if len(A) < len(B): | ||
| B = B[: len(A)] | ||
| else: | ||
| A = A[: len(B)] | ||
| assume(A is not None) | ||
| assume(B is not None) | ||
| assert_allclose(result, numpy_result, atol=atol, rtol=rtol) | ||
|
|
||
|
|
||
| @given(dotv_arrays()) | ||
| @pytest.mark.thread_unsafe(reason="Uses run_threaded") | ||
| def test_threads_share_input(arrays): | ||
| """Test when multiple threads share the same input arrays.""" | ||
| A, B, atol, rtol = arrays | ||
| numpy_result = A.dot(B) | ||
| result = dotv(A, B) | ||
| assert_allclose([numpy_result], result, atol=1e-4, rtol=1e-3) | ||
|
|
||
| def f(): | ||
| result = dotv(A, B) | ||
| assert_allclose(result, numpy_result, atol=atol, rtol=rtol) | ||
|
|
||
| run_threaded(f) |
This file contains hidden or 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.
This did nothing: https://hypothesis.readthedocs.io/en/latest/reference/strategies.html#hypothesis.strategies.random_module