Repository navigation
Add support for np.broadcast_arrays #7660
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
sklam
merged 14 commits into
numba:main
from
guilhermeleobas:guilhermeleobas/np-broadcast_arrays
Apr 1, 2022
Merged
Changes from 10 commits
Commits
Show all changes
14 commits
Select commit
Hold shift + click to select a range
af818d0
add support for np.broadcast_arrays
guilhermeleobas b7d86b8
update test
guilhermeleobas a476255
update test
guilhermeleobas e2bae6e
fix for 32-bit system
guilhermeleobas c4a5a4d
remove build_int_tuple
guilhermeleobas 00c24de
reorder tests
guilhermeleobas e240368
restrict input types and add tests
guilhermeleobas 243d8e8
update test
guilhermeleobas 6b7fa64
remove StringLiteral and Boolean
guilhermeleobas 6089e91
raise typeerror
guilhermeleobas f36ce65
add support for tuple
guilhermeleobas a81c4fb
adjust test
guilhermeleobas faea5d0
Fix test
guilhermeleobas a7292d1
Update numba/tests/test_array_manipulation.py
guilhermeleobas 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 |
|---|---|---|
|
|
@@ -1382,6 +1382,77 @@ def impl(array, shape): | |
| return impl | ||
|
|
||
|
|
||
| @register_jitable | ||
| def numpy_broadcast_shapes_list(r, m, shape): | ||
| for i in range(len(shape)): | ||
| k = m - len(shape) + i | ||
| tmp = shape[i] | ||
| if tmp == 1: | ||
| continue | ||
| if r[k] == 1: | ||
| r[k] = tmp | ||
| elif r[k] != tmp: | ||
| raise ValueError("shape mismatch: objects" | ||
| " cannot be broadcast" | ||
| " to a single shape") | ||
|
|
||
|
|
||
| @overload(np.broadcast_arrays) | ||
| def numpy_broadcast_arrays(*args): | ||
|
|
||
| for idx, arg in enumerate(args): | ||
| if not type_can_asarray(arg): | ||
| raise errors.TypingError(f'Argument "{idx}" must ' | ||
| 'be array-like') | ||
|
|
||
| unified_dtype = None | ||
| dt = None | ||
| for arg in args: | ||
| if isinstance(arg, (types.Array, types.BaseTuple)): | ||
| dt = arg.dtype | ||
| else: | ||
| dt = arg | ||
|
|
||
| if unified_dtype is None: | ||
| unified_dtype = dt | ||
| elif unified_dtype != dt: | ||
| raise errors.TypingError('Mismatch of argument types. Numba cannot ' | ||
| 'broadcast arrays with different types. ' | ||
| f'Got {args}') | ||
|
|
||
| # number of dimensions | ||
| m = 0 | ||
| for idx, arg in enumerate(args): | ||
| if isinstance(arg, types.ArrayCompatible): | ||
| m = max(m, arg.ndim) | ||
| elif isinstance(arg, (types.Number, types.Boolean)): | ||
| m = max(m, 1) | ||
|
Comment on lines
+1425
to
+1429
Contributor
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. This unfortunately doesn't work: from numba import njit
import numpy as np
@njit
def foo(tupl):
return np.broadcast_arrays(tupl, tupl)
print(foo((1,2)))I think the issue is that this loop is not handling tuple types? Probably needs something like: elif isinstance(arg, types.BaseTuple):
m = max(m, arg.count)?
Contributor
Author
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. Fixed |
||
|
|
||
| tup_init = (0,) * m | ||
|
|
||
| def impl(*args): | ||
| # find out the output shape | ||
| # we can't call np.broadcast_shapes here since args may have arrays | ||
| # with different shapes and it is not possible to create a list | ||
| # with those shapes dynamically | ||
| shape = [1] * m | ||
| for array in literal_unroll(args): | ||
| numpy_broadcast_shapes_list(shape, m, np.asarray(array).shape) | ||
|
|
||
| tup = tup_init | ||
|
|
||
| for i in range(m): | ||
| tup = tuple_setitem(tup, i, shape[i]) | ||
|
|
||
| # numpy checks if the input arrays have the same shape as `shape` | ||
| outs = [] | ||
| for array in literal_unroll(args): | ||
| outs.append(np.broadcast_to(np.asarray(array), tup)) | ||
| return outs | ||
|
|
||
| return impl | ||
|
|
||
|
|
||
| def fancy_setslice(context, builder, sig, args, index_types, indices): | ||
| """ | ||
| Implement slice assignment for arrays. This implementation works for | ||
|
|
||
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.
Uh oh!
There was an error while loading. Please reload this page.