Repository navigation
Make scalar operands of binary elementwise functions asynchronous #3182
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
base: master
Are you sure you want to change the base?
Changes from all commits
3350902
c219e68
16477bd
077aeb7
027f5f4
a07cd71
32f7811
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | |||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -101,12 +101,59 @@ def _copy_from_numpy(np_ary, usm_type="device", sycl_queue=None): | ||||||||||||||||||||||||||
| return Xusm | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| def _fill_from_numpy_scalar(dst, np_ary, depends): | |||||||||||||||||||||||||||
| """ | |||||||||||||||||||||||||||
| Fills `dst` with the value of 0-d `np_ary`, cast as | |||||||||||||||||||||||||||
| `_copy_numpy_ndarray_into_usm_ndarray` would cast it. | |||||||||||||||||||||||||||
|
Comment on lines
+106
to
+107
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. Not quite for float16 destinations. Master's device cast of a float64 value to float16 rounds twice ( x = dpt.zeros((), dtype="f2")
x[...] = 1 + 2**-11 + 2**-40 # this PR: 0x3c01 (as NumPy), master: 0x3c00
y = dpt.zeros(1, dtype="f2")
y[...] = np.array([1 + 2**-11 + 2**-40]) # 0x3c00 on bothIt is the same in all 0-d entry points ( The PR's result is the correctly rounded one, so I'd only reword this docstring and the "results don't change" line in the description. Casting All other casts match master bit for bit in a matrix of about 71,000 cases per device: 14 source and 14 destination dtypes, edge values, 10 entry points. The binary-operator control ( |
|||||||||||||||||||||||||||
| """ | |||||||||||||||||||||||||||
| copy_q = dst.sycl_queue | |||||||||||||||||||||||||||
| if copy_q.sycl_device.has_aspect_fp64 is False: | |||||||||||||||||||||||||||
| if np_ary.dtype.char == "d": | |||||||||||||||||||||||||||
| np_ary = np_ary.astype(np.float32) | |||||||||||||||||||||||||||
| elif np_ary.dtype.char == "D": | |||||||||||||||||||||||||||
| np_ary = np_ary.astype(np.complex64) | |||||||||||||||||||||||||||
|
Comment on lines
+110
to
+114
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. On a device without fp64 (Iris Xe), With both downcasts under
Suggested change
and the same in if copy_q.sycl_device.has_aspect_fp64 is False:
src_ary_dt_c = src_ary.dtype.char
with np.errstate(over="ignore"):
if src_ary_dt_c == "d":
src_ary = src_ary.astype(np.float32)
elif src_ary_dt_c == "D":
src_ary = src_ary.astype(np.complex64)Otherwise the fp64-less path works. |
|||||||||||||||||||||||||||
| dst_dt = dst.dtype | |||||||||||||||||||||||||||
| if np_ary.dtype.kind == "c" and dst_dt.kind not in "bc": | |||||||||||||||||||||||||||
| # the copy kernel keeps the real part, NumPy also warns | |||||||||||||||||||||||||||
| np_ary = np_ary.real | |||||||||||||||||||||||||||
| _manager = SequentialOrderManager[copy_q] | |||||||||||||||||||||||||||
| if np_ary.dtype.kind == "f" and dst_dt.kind in "iu": | |||||||||||||||||||||||||||
| # NaN, inf and out-of-range values have no defined integer value, | |||||||||||||||||||||||||||
| # so cast with the copy kernel of the device | |||||||||||||||||||||||||||
| src_dt = np_ary.dtype | |||||||||||||||||||||||||||
| val = dpt.empty((), dtype=src_dt, sycl_queue=copy_q) | |||||||||||||||||||||||||||
|
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. With a non-native byte order this raises: np_ary = np_ary.astype(np_ary.dtype.newbyteorder("="), copy=False)at the top of the function. All other casts of non-native 0-d sources now give NumPy's value, where master copied the raw bytes: |
|||||||||||||||||||||||||||
| hev, fill_ev = ti._full_usm_ndarray( | |||||||||||||||||||||||||||
| np_ary[()].item(), val, sycl_queue=copy_q | |||||||||||||||||||||||||||
| ) | |||||||||||||||||||||||||||
| _manager.add_event_pair(hev, fill_ev) | |||||||||||||||||||||||||||
| src = dpt.usm_ndarray( | |||||||||||||||||||||||||||
| dst.shape, dtype=src_dt, buffer=val, strides=(0,) * dst.ndim | |||||||||||||||||||||||||||
| ) | |||||||||||||||||||||||||||
| hev, cpy_ev = ti._copy_usm_ndarray_into_usm_ndarray( | |||||||||||||||||||||||||||
| src=src, dst=dst, sycl_queue=copy_q, depends=[*depends, fill_ev] | |||||||||||||||||||||||||||
| ) | |||||||||||||||||||||||||||
| _manager.add_event_pair(hev, cpy_ev) | |||||||||||||||||||||||||||
| return | |||||||||||||||||||||||||||
| # the copy kernel casts out-of-range values without warning | |||||||||||||||||||||||||||
| with np.errstate(all="ignore"): | |||||||||||||||||||||||||||
| fill_val = np_ary.astype(dst_dt)[()].item() | |||||||||||||||||||||||||||
| hev, fill_ev = ti._full_usm_ndarray( | |||||||||||||||||||||||||||
|
ndgrigorian marked this conversation as resolved.
Resolved
|
|||||||||||||||||||||||||||
| fill_val, dst, sycl_queue=copy_q, depends=depends | |||||||||||||||||||||||||||
| ) | |||||||||||||||||||||||||||
| _manager.add_event_pair(hev, fill_ev) | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| def _copy_from_numpy_into(dst, np_ary): | |||||||||||||||||||||||||||
| """Copies `np_ary` into `dst` of type :class:`dpnp.tensor.usm_ndarray""" | |||||||||||||||||||||||||||
| if not isinstance(np_ary, np.ndarray): | |||||||||||||||||||||||||||
| raise TypeError(f"Expected numpy.ndarray, got {type(np_ary)}") | |||||||||||||||||||||||||||
| if not isinstance(dst, dpt.usm_ndarray): | |||||||||||||||||||||||||||
| raise TypeError(f"Expected usm_ndarray, got {type(dst)}") | |||||||||||||||||||||||||||
| if np_ary.ndim == 0: | |||||||||||||||||||||||||||
| # fill instead of the host copy, which waits for the whole queue | |||||||||||||||||||||||||||
| _manager = SequentialOrderManager[dst.sycl_queue] | |||||||||||||||||||||||||||
| _fill_from_numpy_scalar(dst, np_ary, _manager.submitted_events) | |||||||||||||||||||||||||||
|
Comment on lines
+152
to
+155
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 is what fails the "Build with nightly build of DPC++ toolchain" job, in both runs on this PR: an abort through The FFT extension keeps only With the CI toolchain (nightly-2026-11-25, its OpenCL CPU runtime,
With the released 2026.2 runtime and a Two ways out:
# dpnp/fft/dpnp_utils_fft.py
_pending = [] # (descriptor, fft_event): keep each descriptor alive until its FFT ran
def _keep_descriptor(dsc, ev):
_pending[:] = [(d, e) for d, e in _pending if e.execution_status != dpctl.event_status_type.complete]
_pending.append((dsc, ev))
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. On the OpenCL CPU device this makes
A |
|||||||||||||||||||||||||||
| return | |||||||||||||||||||||||||||
|
ndgrigorian marked this conversation as resolved.
Resolved
|
|||||||||||||||||||||||||||
| if np_ary.flags["OWNDATA"]: | |||||||||||||||||||||||||||
| Xnp = np_ary | |||||||||||||||||||||||||||
| else: | |||||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||||
| Original file line number | Diff line number | Diff line change | ||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -39,6 +39,7 @@ | |||||||||||||
|
|
||||||||||||||
| from ._copy_utils import ( | ||||||||||||||
| _empty_like_orderK, | ||||||||||||||
| _fill_from_numpy_scalar, | ||||||||||||||
| _from_numpy_empty_like_orderK, | ||||||||||||||
| ) | ||||||||||||||
| from ._data_types import _get_dtype | ||||||||||||||
|
|
@@ -128,6 +129,17 @@ def _asarray_from_numpy_ndarray( | |||||||||||||
| # deduce device-representable output data type | ||||||||||||||
| dtype = _map_to_device_dtype(ary.dtype, copy_q) | ||||||||||||||
| _ensure_native_dtype_device_support(dtype, copy_q.sycl_device) | ||||||||||||||
| if ary.ndim == 0: | ||||||||||||||
| # fill instead of a host copy, which waits for the whole queue | ||||||||||||||
|
Comment on lines
+132
to
+133
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.
x = dpt.asarray(5, dtype="i4", sycl_queue=q) # now an async fill
y = dpt.from_dlpack(x) # y gets the device's cached queue, not q
y[...] = 7 # ordered only after y's queue's events
assert int(x) == 7 # 5 == 7: the fill of 5 ran last
The other test jobs passed it here and in the previous run, so it will flake.
|
||||||||||||||
| res = dpt.usm_ndarray( | ||||||||||||||
| (), | ||||||||||||||
| dtype=dtype, | ||||||||||||||
| buffer=usm_type, | ||||||||||||||
| buffer_ctor_kwargs={"queue": copy_q}, | ||||||||||||||
| ) | ||||||||||||||
| # populating new allocation, no dependent events | ||||||||||||||
| _fill_from_numpy_scalar(res, ary, []) | ||||||||||||||
| return res | ||||||||||||||
| f_contig = ary.flags["F"] | ||||||||||||||
| c_contig = ary.flags["C"] | ||||||||||||||
| fc_contig = f_contig or c_contig | ||||||||||||||
|
|
||||||||||||||
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.
Nit:
_SIZES_1Dstarts at 2**16, so this misses the small arrays where the scalar cost shows most. The issue measured 19 vs 60 us per call at shape(16,). A small size (or a separateparamslist with one) would catch a regression in the scalar path itself, not only the queue drain.