Skip to content

Drop host_task in favor of dpctl's deferred releases - #3224

Draft
ndgrigorian wants to merge 3 commits into
masterfrom
drop-host-tasks-and-reuse-dpctl-deferral
Draft

ndgrigorian wants to merge 3 commits into
masterfrom
drop-host-tasks-and-reuse-dpctl-deferral

Conversation

@ndgrigorian

@ndgrigorian ndgrigorian commented Dec 3, 2026 •

Copy link
Copy Markdown
Collaborator

This PR removes every host_task from dpnp in favor of the deferred releases that dpctl now runs on its own thread (IntelPython/dpctl#2359).

  • dpnp::utils::keep_args_alive in dpnp4pybind11.hpp hands its arguments to dpctl::utils::defer_release instead of submitting up to two host tasks, one of which took the GIL. It still holds a usm_ndarray through the shared_ptr owning its allocation, which dpctl's own keep_args_alive can't do for dpnp.tensor.usm_ndarray.
  • libtensor and the lapack and sparse extensions free temporaries and scratchpads through async_release, a new helper in utils/sycl_alloc_utils.hpp built on defer_release, instead of calling sycl::free in a host task, which the SYCL spec doesn't allow and AdaptiveCpp doesn't implement.
  • The FFT extension now keeps the descriptor alive along with the arrays. It used to drop it while the transform could still run, which Make scalar operands of binary elementwise functions asynchronous #3182 exposes in oneMath builds and works around by holding descriptors in Python; that workaround can go once both are in. The FFT functions also return the keep-alive event first, as the other extensions do.

The keep-alive events are now those of an empty kernel submitted after the work, so their completion means that the arguments are no longer in use, not that they have been released. The Python side still passes them to dpctl's deprecated add_event_pair; moving to add_event/add_cleanup_event is left for a follow-up.

Checked on a Level Zero iGPU (0x7d41, WSL):

  • The full suite passes on the GPU, rebased on master and with Replace the deprecated add_event_pair with add_cleanup_event and add_event #3225 on top. Before the rebase, the FFT, VM GIL and DLPack tests also passed on the OpenCL CPU device.
  • With only the keep_args_alive change, the GPU suite hung intermittently, and a loop of strided x[::2] + y[::2] calls lost the device after about 2M calls on the default Level Zero adapter. With all host tasks gone, the loop completes 3M calls. The v2 adapter runs it clean either way, and a standalone SYCL loop of a copy followed by a dependent kernel also stalls on v1, so the cause looks like the v1 adapter rather than host tasks as such.

Not checked locally: the oneMath build, where the FFT descriptor change matters most, devices without fp64, and performance.

  • Have you provided a meaningful PR description?
  • Have you added a test, reproducer or referred to an issue with a reproducer?
  • Have you tested your changes locally for CPU and GPU devices?
  • Have you made sure that new changes do not introduce compiler warnings?
  • Have you checked performance impact of proposed changes?
  • Have you added documentation for your changes, if necessary?
  • Have you added your changes to the changelog?

dpctl 0.24.0dev0 (IntelPython/dpctl#2359) no longer uses `host_task` to
keep Python objects alive during offload: `dpctl::utils::keep_args_alive`
now hands a release callback to `SyclQueue_DeferRelease`, which runs it
with the GIL held once an empty kernel submitted after the dependencies
completes.

`dpnp::utils::keep_args_alive` in `dpnp4pybind11.hpp` reimplements the
dpctl helper so that it also recognizes dpnp's `usm_ndarray`, and still
submitted up to two host tasks: one holding the shared pointers owning
USM allocations, and one taking the GIL to decref every other object.
It now fills a `detail::held_args<num>` the same way and defers its
release with the public `dpctl::utils::defer_release`. The returned
event is that of the empty kernel, so its completion means that the
arguments are no longer in use rather than that they have been released.

The build requirement in pyproject.toml keeps dpctl 0.23.0, the latest
on PyPI, with a TODO until dpctl 0.24.0 is released. test_vm_gil.py no longer describes host tasks: it
now checks that the work keeping arguments alive needs no GIL.
libtensor and the `lapack` and `sparse` extensions used `host_task` to
keep host data alive until copies complete and to free USM temporaries
and scratchpads once the work using them is done. A `host_task` may not
free USM per the SYCL spec, and AdaptiveCpp does not implement it.

`dpnp::tensor::alloc_utils::async_release(q, depends, objs...)` in
`utils/sycl_alloc_utils.hpp` moves the objects into a release deferred
with `dpctl::utils::defer_release`, which destroys them once the work
covered by `depends` completes. If the release cannot be deferred, it
waits for that work before rethrowing, so nothing in use is freed.
`async_smart_free` and `device_allocate_and_pack` are built on it, and
the remaining sites in `choose`, `integer_advanced_indexing`,
`triul_ctor`, the LAPACK routines (including `potrs`) and sparse `gemv`
call it directly. The events these return are now those of an empty
kernel, so the vectors and variables holding them are renamed from
`host_task_events`, `host_tasks`, `ht_ev` and the like to
`cleanup_events`, `cleanup_ev`, `cleanup_event` and
`keep_args_alive_ev`.

With the previous commit alone, the GPU test suite hung intermittently
on an Intel iGPU with the default Level Zero adapter, and a loop of
strided binary operations lost the device after about 2M calls. With
this commit, the same loop completes 3M calls and the suite passes. The
loop also completes on the v2 adapter without this commit, so the hang
appears to be in the v1 adapter; a standalone SYCL loop of a copy
followed by a kernel depending on it stalls there too.
The FFT extension kept only the input and output arrays alive, and
_compute_result drops the descriptor on return, so the transform could
run on a freed descriptor. A blocking scalar copy when scaling the
result has hidden it so far; builds with oneMath hit it once that copy
is asynchronous (#3182). With keep_args_alive deferring releases
through dpctl, the extension now keeps the Python object wrapping the
descriptor alive too, which a host task could not release with oneMath
without deadlocking.

The FFT functions also return the keep-alive event first and the
transform's event second, as the other extensions do and as
_compute_result already unpacks them.
@ndgrigorian
ndgrigorian force-pushed the drop-host-tasks-and-reuse-dpctl-deferral branch from 9858451 to b9fd596 Compare December 3, 2026 22:58
@ndgrigorian
ndgrigorian changed the base branch from make-scalar-operands-in-binary-ops-asynchronous to master December 3, 2026 22:58
@github-actions

github-actions Bot commented Dec 3, 2026

Copy link
Copy Markdown
Contributor

View rendered docs @ https://intelpython.github.io/dpnp/pull/3224/index.html

@github-actions

github-actions Bot commented Dec 3, 2026 •

Copy link
Copy Markdown
Contributor

Array API standard conformance tests for dpnp=0.22.0dev3+79.gcbc76836e ran successfully.
Passed: 1491
Failed: 0
Skipped: 6

@ndgrigorian ndgrigorian closed this Dec 3, 2026
@ndgrigorian ndgrigorian reopened this Dec 3, 2026
@coveralls

Copy link
Copy Markdown
Collaborator

Coverage Status

coverage: 84.587% (-0.02%) from 84.609% — drop-host-tasks-and-reuse-dpctl-deferral into master

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants