-
Notifications
You must be signed in to change notification settings - Fork 29
Fix dpnp.einsum memory-layout
#3058
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
Open
abagusetty
wants to merge
3
commits into
IntelPython:master
Choose a base branch
from
abagusetty:fix-einsum-order-k-contiguity
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.
+150
−4
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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 |
|---|---|---|
|
|
@@ -1039,8 +1039,22 @@ def dpnp_einsum( | |
| ) | ||
| arrays.append(operands[id]) | ||
| result_dtype = dpnp.result_type(*arrays) if dtype is None else dtype | ||
| if order is not None and order in "aA": | ||
| order = "F" if all(arr.flags.fnc for arr in arrays) else "C" | ||
| # validated here because the view path below skips `dpnp.asarray` | ||
| if order is None: | ||
| order = "K" | ||
| elif not isinstance(order, str): | ||
| raise TypeError(f"order must be str, not {type(order).__name__}") | ||
| elif len(order) == 1 and order in "afkcAFKC": | ||
| order = order.upper() | ||
| else: | ||
| raise ValueError( | ||
| f"order must be one of 'C', 'F', 'A', or 'K' (got '{order}')" | ||
| ) | ||
| all_f_contiguous = all(arr.flags.f_contiguous for arr in arrays) | ||
| if order == "A": | ||
|
antonwolfy marked this conversation as resolved.
|
||
| # NumPy uses f_contiguous here, not fnc; they differ for an array that | ||
| # is both C- and F-contiguous, such as a 1-D or size-1 one | ||
| order = "F" if all_f_contiguous else "C" | ||
|
|
||
| input_subscripts = [ | ||
| _parse_ellipsis_subscript(sub, idx, ndim=arr.ndim) | ||
|
|
@@ -1110,12 +1124,16 @@ def dpnp_einsum( | |
| # no more raises | ||
| if len(operands) >= 2: | ||
| if any(arr.size == 0 for arr in operands): | ||
| return dpnp.zeros( | ||
| # every term of the sum is empty, so the result is all zeros; | ||
| # "K" has no layout to keep here, and NumPy falls back to "C" | ||
| arr_out = dpnp.zeros( | ||
| tuple(dimension_dict[label] for label in output_subscript), | ||
| dtype=result_dtype, | ||
| order="C" if order == "K" else order, | ||
| usm_type=res_usm_type, | ||
| sycl_queue=exec_q, | ||
| ) | ||
| return dpnp.get_result_array(arr_out, out, casting=casting) | ||
|
|
||
| # Don't squeeze if unary, because this affects later (in trivial sum) | ||
| # whether the return is a writeable view. | ||
|
|
@@ -1226,6 +1244,14 @@ def dpnp_einsum( | |
| [dimension_dict[label] for label in output_subscript] | ||
| ) | ||
|
|
||
| arr_out = dpnp.asarray(arr_out, order=order) | ||
| # a unary einsum without summation returns a view, as NumPy does for every | ||
| # `order` | ||
| if not returns_view: | ||
| if order == "K" and not all_f_contiguous: | ||
|
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.
|
||
| # NumPy copies the result into a new c-contiguous array, while | ||
| # the matmul above leaves a permuted one; for all-f-contiguous | ||
| # operands it keeps a layout chosen per contraction, so "K" stays | ||
| order = "C" | ||
| arr_out = dpnp.asarray(arr_out, order=order) | ||
| assert returns_view or arr_out.dtype == result_dtype | ||
| return dpnp.get_result_array(arr_out, out, casting=casting) | ||
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
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.