Fix dpnp.einsum memory-layout - #3058
Open
abagusetty wants to merge 3 commits into
Open
Conversation
abagusetty
requested review from
antonwolfy,
ndgrigorian and
vlad-perevezentsev
as code owners
September 2, 2026 17:17
|
Can one of the admins verify this patch? |
Collaborator
antonwolfy
reviewed
Sep 3, 2026
| result = dpnp.einsum(subscripts, ia, order=order) | ||
| expected = numpy.einsum(subscripts, a, order=order) | ||
| assert result.get_array()._pointer == ia.get_array()._pointer | ||
| assert result.strides == expected.strides |
Contributor
There was a problem hiding this comment.
Might fail on non-fp64 device
Contributor
Author
There was a problem hiding this comment.
good catch, the strides are byte strides. Pinned the operand to dtype.dpnp.default_float_type()
| raise ValueError( | ||
| f"order must be one of 'C', 'F', 'A', or 'K' (got '{order}')" | ||
| ) | ||
| if order == "A": |
Contributor
There was a problem hiding this comment.
Suggested change
| if order == "A": | |
| if order in ("A", "K"): |
needs to fix:
import numpy as np
import dpnp
# F-contiguous @ C-contiguous, default order="K"
a = np.asarray(np.random.random((4, 5)), order="F")
b = np.asarray(np.random.random((5, 6)), order="C")
n = np.einsum("ij,jk->ik", a, b) # order="K" default
d = dpnp.einsum("ij,jk->ik", dpnp.asarray(a), dpnp.asarray(b))
print(n.flags.c_contiguous, n.flags.f_contiguous) # True False (NumPy: C)
print(d.flags.c_contiguous, d.flags.f_contiguous) # False True (dpnp: F) -- mismatch
Contributor
Author
There was a problem hiding this comment.
True False
True False
Contributor
There was a problem hiding this comment.
size-0 operand path ignores out= and order= and causes:
import numpy as np
import dpnp
def show(tag, mod, asnp):
# size-0 contracted dim `j`, non-empty (2, 4) output
a = mod.ones((2, 0))
b = mod.ones((0, 4))
# 1) out= should be filled and returned
out = mod.full((2, 4), 9.0)
r = mod.einsum("ij,jk->ik", a, b, out=out)
out_np = asnp(out)
print(f"[{tag}] out= -> r is out: {r is out!s:5} "
f"out all-zeros: {bool((out_np == 0).all())!s:5} (expect: True / True)")
# 2) explicit order='F' should give an F-contiguous result
rf = mod.einsum("ij,jk->ik", a, b, order="F")
print(f"[{tag}] order='F' -> f_contiguous: {rf.flags.f_contiguous!s:5} "
f"c_contiguous: {rf.flags.c_contiguous!s:5} (expect: True / False)")
show("numpy", np, lambda x: x)
# Out: [numpy] out= -> r is out: True out all-zeros: True (expect: True / True)
# Out: [numpy] order='F' -> f_contiguous: True c_contiguous: False (expect: True / False)
show("dpnp ", dpnp, dpnp.asnumpy)
# Out: [dpnp ] out= -> r is out: False out all-zeros: False (expect: True / True)
# Out: [dpnp ] order='F' -> f_contiguous: False c_contiguous: True (expect: True / False)
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
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
One more minor layout issue detected during an app-testing in comparison to numpy behavior:
Fixes: #3056