Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
18 commits
Select commit Hold shift + click to select a range
60f92e2
Fill scalars on the device instead of copying them from the host
ndgrigorian Nov 5, 2026
ddf3213
Wait for the fill before reading memory with copy_to_host in a test
ndgrigorian Nov 24, 2026
e09e80f
Share the no-host-copy patch of the scalar tests in the tensor test h…
ndgrigorian Nov 24, 2026
124b6fc
Cast a float scalar to an integer type on the device
ndgrigorian Nov 24, 2026
964f1ee
Fill a 1-D strided array without packing its shape and strides
ndgrigorian Nov 24, 2026
1aaccb4
Add an ASV benchmark of binary ufuncs with a Python scalar operand
ndgrigorian Nov 24, 2026
bb03604
Fill contiguous memory with a kernel on CPU devices
ndgrigorian Dec 2, 2026
362e05b
Downcast float64 scalars for devices without fp64 without a warning
ndgrigorian Dec 2, 2026
edb3bd6
Test scalar assignment of float values out of the destination range
ndgrigorian Dec 2, 2026
28ea4ab
Test a scalar fill of a view that stays 2-D after simplification
ndgrigorian Dec 2, 2026
32e96ea
Document the single rounding of a float64 scalar into float16
ndgrigorian Dec 2, 2026
c8e11a0
Fill from a 0-d array of non-native byte order
ndgrigorian Dec 2, 2026
b97bb9e
Time the scalar operand benchmark on a 16-element array too
ndgrigorian Dec 2, 2026
fc2533d
Keep the FFT descriptor alive until its transform completes
ndgrigorian Dec 2, 2026
c217c93
Wait for submitted work before copying in to_device and __dlpack__
ndgrigorian Dec 2, 2026
020228c
Order a DLPack import after the work on the exporter's queue
ndgrigorian Dec 2, 2026
29f2c02
Add a changelog entry for the faster fill on CPU devices
ndgrigorian Dec 2, 2026
46e6313
Merge branch 'master' into make-scalar-operands-in-binary-ops-asynchr…
ndgrigorian Dec 2, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@ This release drops support for Python 3.10, making Python 3.11 the minimum requi
* Changed `dpnp.median`, `dpnp.nanmedian` and `dpnp.partition` with a single `kth` to use a radix select instead of a full sort for all but complex data types and, on a GPU, `dpnp.partition` of rows of 65 to 128 elements of 8-byte integers [#3138](https://github.com/IntelPython/dpnp/pull/3138)
* Improved performance of `dpnp.top_k` and `dpnp.tensor.top_k` on a GPU for batches of rows of 65536 to about 10^6 elements that occupy it with a work-group per row [#3138](https://github.com/IntelPython/dpnp/pull/3138)
* Changed the C++ standard used to build dpnp and the pybind11 example from C++17 to C++20. C++ extensions that include the headers shipped with dpnp (`dpnp4pybind11.hpp`, the libtensor headers) must now be built as C++20 [#3190](https://github.com/IntelPython/dpnp/pull/3190)
* Changed `dpnp.asarray`, `dpnp.tensor.asarray` and assignment through basic indexing (e.g. `x[0] = 1`, `x[:, 0] = 0`) to fill a scalar or 0-d NumPy array on the device instead of copying it from the host, which waited for all work queued on the device, so binary operators and functions with a Python or NumPy scalar operand (e.g. `2 * x`, `x == 0`, `dpnp.clip(x, 0, 1)`) no longer block the host. Code that reads the memory directly, e.g. with `x.usm_data.copy_to_host()`, must call `x.sycl_queue.wait()` first [#3182](https://github.com/IntelPython/dpnp/pull/3182)
* Improved performance of filling an array with a nonzero value on a CPU device, e.g. in `dpnp.full`, `dpnp.tensor.full` and `x[...] = 1` [#3182](https://github.com/IntelPython/dpnp/pull/3182)

### Deprecated

Expand Down Expand Up @@ -66,6 +68,8 @@ This release drops support for Python 3.10, making Python 3.11 the minimum requi
* Fixed wrong values from `dpnp.from_dlpack` and `dpnp.tensor.from_dlpack` when copying a `float64` or `complex128` host array to a SYCL device without fp64 support, which copied its bytes unchanged into the `float32` or `complex64` result [#3209](https://github.com/IntelPython/dpnp/pull/3209)
* Fixed a random segfault of the OpenCL CPU runtime in coverage builds, and their link failure with `DPNP_TENSOR_GENERATE_COVERAGE_FOR_PYBIND11_EXTENSIONS=ON`, by instrumenting only host code [#3193](https://github.com/IntelPython/dpnp/pull/3193)
* Fixed building with the open source DPC++ compiler and CMake 4.5 or newer, which identifies it as `DPClang` instead of `Clang` [#3216](https://github.com/IntelPython/dpnp/pull/3216)
* Fixed wrong results and crashes of `dpnp.fft` functions in builds with oneMath, which released the FFT descriptor before the transform ran [#3182](https://github.com/IntelPython/dpnp/pull/3182)
* Fixed `dpnp.ndarray.to_device` and `dpnp.tensor.usm_ndarray.to_device` to a device in another SYCL context, and `__dlpack__` with `copy=True`, copying the array before work queued to compute it completed [#3182](https://github.com/IntelPython/dpnp/pull/3182)

### Security

Expand Down
26 changes: 26 additions & 0 deletions benchmarks/benchmarks/bench_elementwise.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,29 @@ def setup(self, executor, ufunc, size, dtype):

def time_binary(self, executor, ufunc, size, dtype):
self.sync(self.fn(self.a, self.b))


# ---------------------------------------------------------------------------
# A Python scalar operand
# ---------------------------------------------------------------------------


class BinaryScalar(_Ufunc):
"""Binary ufuncs with a Python scalar operand, e.g. ``2 * x``."""

# the cost of the scalar itself shows most on small arrays
params = [
_EXECUTOR_NAMES,
["add", "multiply"],
[2**4, *_SIZES_1D],
_FLOAT_DTYPES,
]

def setup(self, executor, ufunc, size, dtype):
super().setup(executor, ufunc, size, dtype)
self.a = self._input(size, dtype, _DEFAULT_RANGE)
self.sync(self.fn(self.a, 2))

def time_binary_scalar(self, executor, ufunc, size, dtype):
# the second call's scalar is filled behind the first call
self.sync(self.fn(self.fn(self.a, 2), 2))
3 changes: 3 additions & 0 deletions dpnp/dpnp_iface_arraycreation.py
Original file line number Diff line number Diff line change
Expand Up @@ -2320,6 +2320,9 @@ def from_dlpack(x, /, *, device=None, copy=None):

"""

if isinstance(x, dpnp_array):
# dpnp.tensor orders the import after the work on the queue of `x`
x = x.get_array()
result = dpt.from_dlpack(x, device=device, copy=copy)
if isinstance(result, dpt.usm_ndarray):
return dpnp_array._create_from_usm_ndarray(result)
Expand Down
31 changes: 31 additions & 0 deletions dpnp/fft/dpnp_utils_fft.py
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,11 @@
# pylint: disable=protected-access
# pylint: disable=no-name-in-module

import atexit
import threading
from collections.abc import Sequence

import dpctl
import dpctl.utils as dpu
import numpy

Expand All @@ -60,6 +63,33 @@
_standardize_strides_to_nonzero,
)

# The FFT extension keeps only the arrays alive until its kernel ends, and
# releasing a oneMath descriptor in a host task deadlocks, so each descriptor
# stays here until its FFT has completed.
# TODO: remove once dpnp uses dpctl's deferred releases, which need no host task
_pending_descriptors = []
_pending_descriptors_lock = threading.Lock()


def _keep_descriptor_alive(dsc, fft_event):
"""Holds `dsc` until `fft_event` completes, releasing completed ones."""
complete = dpctl.event_status_type.complete
with _pending_descriptors_lock:
_pending_descriptors[:] = [
(d, ev)
for d, ev in _pending_descriptors
if ev.execution_status != complete
]
_pending_descriptors.append((dsc, fft_event))


@atexit.register
def _release_pending_descriptors():
with _pending_descriptors_lock:
for _, ev in _pending_descriptors:
ev.wait()
_pending_descriptors.clear()


def _check_norm(norm):
if norm not in (None, "ortho", "forward", "backward"):
Expand Down Expand Up @@ -240,6 +270,7 @@ def _compute_result(dsc, a, out, forward, c2c, out_strides):
dsc, a_usm, res_usm, forward, depends=dep_evs
)
_manager.add_event_pair(ht_fft_event, fft_event)
_keep_descriptor_alive(dsc, fft_event)

if not isinstance(result, dpnp_array):
return dpnp_array._create_from_usm_ndarray(result)
Expand Down
73 changes: 72 additions & 1 deletion dpnp/libtensor/include/kernels/constructors.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,12 @@ class linear_sequence_step_kernel;
template <typename Ty, typename wTy>
class linear_sequence_affine_kernel;
template <typename Ty>
class full_contig_kernel;
template <typename Ty>
class full_strided_kernel;
template <typename Ty>
class full_strided_1d_kernel;
template <typename Ty>
class eye_kernel;

using namespace dpnp::tensor::offset_utils;
Expand Down Expand Up @@ -248,6 +252,19 @@ sycl::event lin_space_affine_impl(sycl::queue &exec_q,

/* ================ Full ================== */

template <typename Ty>
class ContigFullFunctor
{
private:
Ty *p = nullptr;
Ty fill_v;

public:
ContigFullFunctor(Ty *p_, const Ty &fill_v_) : p(p_), fill_v(fill_v_) {}

void operator()(sycl::id<1> id) const { p[id.get(0)] = fill_v; }
};

/*!
* @brief Function to submit kernel to fill given contiguous memory allocation
* with specified value.
Expand All @@ -271,10 +288,18 @@ sycl::event full_contig_impl(sycl::queue &q,
const std::vector<sycl::event> &depends)
{
dpnp::tensor::type_utils::validate_type_for_device<dstTy>(q);
const bool is_cpu = q.get_device().is_cpu();
sycl::event fill_ev = q.submit([&](sycl::handler &cgh) {
cgh.depends_on(depends);
dstTy *p = reinterpret_cast<dstTy *>(dst_p);
cgh.fill<dstTy>(p, fill_v, nelems);
if (is_cpu) {
// the OpenCL CPU runtime runs cgh.fill several times slower
cgh.parallel_for<full_contig_kernel<dstTy>>(
sycl::range<1>{nelems}, ContigFullFunctor<dstTy>(p, fill_v));
}
else {
cgh.fill<dstTy>(p, fill_v, nelems);
}
});

return fill_ev;
Expand Down Expand Up @@ -348,6 +373,52 @@ sycl::event full_strided_impl(sycl::queue &q,
return fill_ev;
}

/*!
* @brief Function to submit kernel to fill given one-dimensional strided
* memory allocation with specified value.
*
* @param exec_q Sycl queue to which kernel is submitted for execution.
* @param nelems Length of the sequence
* @param offset Offset of the first element from `dst_p`, in elements.
* @param step Stride between consecutive elements, in elements.
* @param fill_v Value to fill the array with
* @param dst_p Kernel accessible USM pointer to the start of array to be
* populated.
* @param depends List of events to wait for before starting computations, if
* any.
*
* @return Event to wait on to ensure that computation completes.
* @defgroup CtorKernels
*/
template <typename dstTy>
sycl::event full_strided_1d_impl(sycl::queue &q,
std::size_t nelems,
ssize_t offset,
ssize_t step,
dstTy fill_v,
char *dst_p,
const std::vector<sycl::event> &depends)
{
dpnp::tensor::type_utils::validate_type_for_device<dstTy>(q);

dstTy *dst_tp = reinterpret_cast<dstTy *>(dst_p);

using dpnp::tensor::offset_utils::Strided1DIndexer;
const Strided1DIndexer indexer(offset, nelems, step);

sycl::event fill_ev = q.submit([&](sycl::handler &cgh) {
cgh.depends_on(depends);

using KernelName = full_strided_1d_kernel<dstTy>;
using Impl = FullStridedFunctor<dstTy, Strided1DIndexer>;

cgh.parallel_for<KernelName>(sycl::range<1>{nelems},
Impl(dst_tp, fill_v, indexer));
});

return fill_ev;
}

/* ================ Eye ================== */

typedef sycl::event (*eye_fn_ptr_t)(sycl::queue &,
Expand Down
103 changes: 99 additions & 4 deletions dpnp/libtensor/source/full_ctor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
#include "utils/type_utils.hpp"

#include "full_ctor.hpp"
#include "simplify_iteration_space.hpp"

namespace py = pybind11;
namespace td_ns = dpnp::tensor::type_dispatch;
Expand Down Expand Up @@ -220,8 +221,65 @@ struct FullStridedFactory
}
};

typedef sycl::event (*full_strided_1d_fn_ptr_t)(
sycl::queue &,
std::size_t,
py::ssize_t,
py::ssize_t,
const py::object &,
char *,
const std::vector<sycl::event> &);

/*!
* @brief Function to submit kernel to fill given one-dimensional strided
* memory allocation with specified value.
*
* @param exec_q Sycl queue to which kernel is submitted for execution.
* @param nelems Length of the sequence
* @param offset Offset of the first element from `dst_p`, in elements.
* @param step Stride between consecutive elements, in elements.
* @param py_value Python object representing the value to fill the array with.
* Must be convertible to `dstTy`.
* @param dst_p Kernel accessible USM pointer to the start of array to be
* populated.
* @param depends List of events to wait for before starting computations, if
* any.
*
* @return Event to wait on to ensure that computation completes.
* @defgroup CtorKernels
*/
template <typename dstTy>
sycl::event full_strided_1d_impl(sycl::queue &exec_q,
std::size_t nelems,
py::ssize_t offset,
py::ssize_t step,
const py::object &py_value,
char *dst_p,
const std::vector<sycl::event> &depends)
{
dstTy fill_v = py::cast<dstTy>(py_value);

using dpnp::tensor::kernels::constructors::full_strided_1d_impl;
sycl::event fill_ev = full_strided_1d_impl<dstTy>(
exec_q, nelems, offset, step, fill_v, dst_p, depends);

return fill_ev;
}

template <typename fnT, typename Ty>
struct FullStrided1DFactory
{
fnT get()
{
fnT f = full_strided_1d_impl<Ty>;
return f;
}
};

static full_contig_fn_ptr_t full_contig_dispatch_vector[td_ns::num_types];
static full_strided_fn_ptr_t full_strided_dispatch_vector[td_ns::num_types];
static full_strided_1d_fn_ptr_t
full_strided_1d_dispatch_vector[td_ns::num_types];

std::pair<sycl::event, sycl::event>
usm_ndarray_full(const py::object &py_value,
Expand Down Expand Up @@ -264,16 +322,48 @@ std::pair<sycl::event, sycl::event>
}
else {
int nd = dst.get_ndim();
auto const &dst_shape = dst.get_shape_vector();
const py::ssize_t *dst_shape = dst.get_shape_raw();
auto const &dst_strides = dst.get_strides_vector();

using shT = std::vector<py::ssize_t>;
shT simplified_shape;
shT simplified_strides;
py::ssize_t dst_offset(0);

// nd, simplified_* and dst_offset are modified by reference
simplify_iteration_space_1(nd, dst_shape, dst_strides,
// output
simplified_shape, simplified_strides,
dst_offset);

if (nd == 1) {
// shape and stride go by value, so no packed temporaries to free
// in host tasks
sycl::event full_1d_event;
const py::ssize_t step = simplified_strides.front();
if (step == 1) {
auto fn = full_contig_dispatch_vector[dst_typeid];
full_1d_event =
fn(exec_q, static_cast<std::size_t>(dst_nelems), py_value,
dst_data + dst_offset * dst.get_elemsize(), depends);
}
else {
auto fn = full_strided_1d_dispatch_vector[dst_typeid];
full_1d_event =
fn(exec_q, static_cast<std::size_t>(dst_nelems), dst_offset,
step, py_value, dst_data, depends);
}
return std::make_pair(
keep_args_alive(exec_q, {dst}, {full_1d_event}), full_1d_event);
}

auto fn = full_strided_dispatch_vector[dst_typeid];

std::vector<sycl::event> host_task_events;
host_task_events.reserve(2);
using dpnp::tensor::offset_utils::device_allocate_and_pack;
auto ptr_size_event_tuple = device_allocate_and_pack<py::ssize_t>(
exec_q, host_task_events, dst_shape, dst_strides);
exec_q, host_task_events, simplified_shape, simplified_strides);
auto shape_strides_owner = std::move(std::get<0>(ptr_size_event_tuple));
const sycl::event &copy_shape_ev = std::get<2>(ptr_size_event_tuple);
py::ssize_t *shape_strides = shape_strides_owner.get();
Expand All @@ -285,8 +375,8 @@ std::pair<sycl::event, sycl::event>
all_deps.push_back(copy_shape_ev);

const sycl::event &full_strided_ev =
fn(exec_q, nd, dst_nelems, shape_strides, py_value, dst_data,
all_deps);
fn(exec_q, nd, dst_nelems, shape_strides, py_value,
dst_data + dst_offset * dst.get_elemsize(), all_deps);

// free shape_strides
const auto &temporaries_cleanup_ev =
Expand All @@ -310,6 +400,11 @@ void init_full_ctor_dispatch_vectors(void)
DispatchVectorBuilder<full_strided_fn_ptr_t, FullStridedFactory, num_types>
dvb2;
dvb2.populate_dispatch_vector(full_strided_dispatch_vector);

DispatchVectorBuilder<full_strided_1d_fn_ptr_t, FullStrided1DFactory,
num_types>
dvb3;
dvb3.populate_dispatch_vector(full_strided_1d_dispatch_vector);
}

} // namespace dpnp::tensor::py_internal
Loading