-
-
Notifications
You must be signed in to change notification settings - Fork 37
First matmul implementation #366
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
Merged
Changes from 9 commits
Commits
Show all changes
15 commits
Select commit
Hold shift + click to select a range
725059d
[FEAT] Initial version for matmul
ricardosp4 af7d678
[TEST] Initial version of matmul tests
ricardosp4 89229da
[TEST] Second version of matmul tests
ricardosp4 8845e9d
[FIX] Correction of 1D arrays multiplication
ricardosp4 ebea726
[FEAT] Small changes
ricardosp4 bc6729c
[DOC, TESTS] Add doc, tests
ricardosp4 877d010
[FEAT] Output is a blosc2.NDArray
ricardosp4 0af7df2
[FEAT] Squeeze function returns itself
ricardosp4 97d3206
Merge branch 'Blosc:main' into matmul
ricardosp4 d5965f1
[BENCH] First benchmark on matmul
ricardosp4 d1d164c
Merge branch 'matmul' of github.com:ricardosp4/python-blosc2 into matmul
ricardosp4 0fea63b
GB to MB
ricardosp4 dc48bdf
[FIX] Comments added
ricardosp4 1354fff
[TEST] New test for special cases
ricardosp4 5e27651
[TEST] New test for urlpaths.
ricardosp4 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -6,3 +6,4 @@ Operations with arrays | |
|
|
||
| lazy_functions | ||
| reduction_functions | ||
| linear_algebra | ||
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 |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| .. _linear_algebra: | ||
|
|
||
| Linear Algebra | ||
| -------------- | ||
|
|
||
| The next functions can be used for computing linear algebra operations with :ref:`NDArray <NDArray>`. | ||
|
|
||
| .. currentmodule:: blosc2 | ||
|
|
||
| .. autosummary:: | ||
| :toctree: autofiles/operations_with_arrays/ | ||
| :nosignatures: | ||
|
|
||
| matmul |
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 |
|---|---|---|
|
|
@@ -236,6 +236,7 @@ class Tuner(Enum): | |
| ones, | ||
| full, | ||
| save, | ||
| matmul, | ||
| ) | ||
|
|
||
| from .c2array import c2context, C2Array, URLPath | ||
|
|
||
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 |
|---|---|---|
| @@ -0,0 +1,84 @@ | ||
| import pytest | ||
| import numpy as np | ||
| import blosc2 | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("ashape", "achunks", "ablocks"), | ||
| [ | ||
| ((12, 10), (7, 5), (3, 3)), | ||
| ((10,), (9,), (7,)), | ||
| ], | ||
| ) | ||
| @pytest.mark.parametrize( | ||
| ("bshape", "bchunks", "bblocks"), | ||
| [ | ||
| ((10,), (4,), (2,)), | ||
| ((10, 5), (3, 4), (1, 3)), | ||
| ((10, 12), (2, 4), (1, 2)), | ||
| ], | ||
| ) | ||
| @pytest.mark.parametrize( | ||
| "dtype", [np.float32, np.float64, np.complex64, np.complex128], | ||
| ) | ||
| def test_matmul(ashape, achunks, ablocks, bshape, bchunks, bblocks, dtype): | ||
| a = blosc2.linspace(0, 10, dtype=dtype, shape=ashape, chunks=achunks, blocks=ablocks) | ||
| b = blosc2.linspace(0, 10, dtype=dtype, shape=bshape, chunks=bchunks, blocks=bblocks) | ||
| blosc2_res = blosc2.matmul(a, b) | ||
|
|
||
| na = a[:] | ||
| nb = b[:] | ||
| np_res = np.matmul(na, nb) | ||
|
|
||
| np.testing.assert_allclose(blosc2_res, np_res, rtol=1e-6) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ("ashape", "achunks", "ablocks"), | ||
| [ | ||
| ((12, 11), (7, 5), (3, 1)), | ||
| ((0, 0), (0, 0), (0, 0)), | ||
| ((10,), (4,), (2,)), | ||
| ], | ||
| ) | ||
| @pytest.mark.parametrize( | ||
| ("bshape", "bchunks", "bblocks"), | ||
| [ | ||
| ((1, 5), (1, 4), (1, 3)), | ||
| ((4, 6), (2, 4), (1, 3)), | ||
| ((5,), (4,), (2,)), | ||
| ], | ||
| ) | ||
| def test_matmul_shapes(ashape, achunks, ablocks, bshape, bchunks, bblocks): | ||
| a = blosc2.linspace(0, 10, shape=ashape, chunks=achunks, blocks=ablocks) | ||
| b = blosc2.linspace(0, 10, shape=bshape, chunks=bchunks, blocks=bblocks) | ||
|
|
||
| with pytest.raises(ValueError): | ||
| blosc2.matmul(a, b) | ||
|
|
||
| with pytest.raises(ValueError): | ||
| blosc2.matmul(b, a) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("scalar", [ | ||
| 5, # int | ||
| 5.3, # float | ||
| 1 + 2j, # complex | ||
| np.int32(5), # NumPy int32 | ||
| np.int64(5), # NumPy int64 | ||
| np.float32(5.3), # NumPy float32 | ||
| np.float64(5.3), # NumPy float64 | ||
| np.complex64(1 + 2j), # NumPy complex64 | ||
| np.complex128(1 + 2j), # NumPy complex128 | ||
| ]) | ||
| def test_matmul_scalars(scalar): | ||
| vector = blosc2.asarray(np.array([1, 2, 3])) | ||
|
|
||
| with pytest.raises(ValueError): | ||
| blosc2.matmul(scalar, vector) | ||
|
|
||
| with pytest.raises(ValueError): | ||
| blosc2.matmul(vector, scalar) | ||
|
|
||
| with pytest.raises(ValueError): | ||
| blosc2.matmul(scalar, scalar) | ||
ricardosp4 marked this conversation as resolved.
Show resolved
Hide resolved
|
||
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.
Instead of using
x1.dtypeas the outcome, it would be better to useout_dtype = np.result_type(x1, x2).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.
Also, you should pass
**kwargstoblosc2.zeros()for passing NDArray compression/storage details to the output.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.
The exception error was this one:
> E numpy._core._exceptions._UFuncOutputCastingError: Cannot cast ufunc 'add' output from dtype('complex128') to dtype('float64') with casting rule 'same_kind'What if that line is receives these arguments:
result = blosc2.zeros((n, m), dtype=np.result_type(x1, x2), **kwargs)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.
Yes, I was suggesting exactly that :-)
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 would be nice to add tests that combines operands with different dtypes. Also, add tests that set
kwargsand check that they work (by checking e.gout.cparams.codec).