Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,8 @@ This release drops support for Python 3.10, making Python 3.11 the minimum requi
* Changed the CI to build and test the conda packages of `conda-recipe` and the wheels with Intel CI (`ci/`, `.github/workflows/intel-ci.yml`); the conda packages build against conda-forge and the wheels against PyPI, both with `dpctl` dev builds from `dppy/label/dev`, and the wheels declare the oneAPI runtime wheels in their metadata [#3151](https://github.com/IntelPython/dpnp/pull/3151)
* Changed the install instructions in `README.md` and the quick start guide to conda-forge and PyPI [#3151](https://github.com/IntelPython/dpnp/pull/3151)
* Changed the conda recipe's tests to `pip check`, the import and `python -m dpctl -f`, as in the conda-forge feedstock, so `conda build` and `conda build --test` no longer run the test suites and no longer read `SKIP_TENSOR_TESTS`; run the suites with `ci/hooks.py`, or with `python -m pytest --pyargs dpnp.tests` in an environment with dpnp installed [#3151](https://github.com/IntelPython/dpnp/pull/3151)
* Changed `dpnp::utils::keep_args_alive` in `dpnp4pybind11.hpp` to defer the release of its arguments with `dpctl::utils::defer_release` instead of submitting a `host_task`, so the returned event is now for an empty kernel and its completion means that the arguments are no longer in use rather than that they have been released [#3224](https://github.com/IntelPython/dpnp/pull/3224)
* Changed libtensor and the `lapack` and `sparse` extensions to free temporaries and keep host data alive with releases deferred through `dpctl::utils::defer_release` instead of with `host_task`, and added `async_release` to the libtensor header `utils/sycl_alloc_utils.hpp` for that. `async_smart_free` and `device_allocate_and_pack` now return the event of an empty kernel, and `utils/sycl_alloc_utils.hpp` and `utils/offset_utils.hpp` now include `dpctl4pybind11.hpp` [#3224](https://github.com/IntelPython/dpnp/pull/3224)

### Deprecated

Expand Down
97 changes: 43 additions & 54 deletions dpnp/include/dpnp4pybind11.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -648,10 +648,6 @@ namespace utils
{
namespace detail
{
// TODO: future version of dpctl will include a more general way of passing
// shared_ptrs to keep_args_alive, so that future overload can be used here
// instead of reimplementing keep_args_alive

struct ManagedMemory
{
// TODO: do we need to check for memory here? Or can we assume only
Expand Down Expand Up @@ -688,74 +684,67 @@ struct ManagedMemory
"Attempted extraction of shared_ptr on an unrecognized type");
}
};

/*! @brief What `keep_args_alive` holds for the work using its objects. */
template <std::size_t num>
struct held_args
{
std::size_t n_usm_owners = 0;
std::array<std::shared_ptr<void>, num> usm_owners{};
std::size_t n_objects = 0;
std::array<py::handle, num> objects{};

/*! @brief Drop what `self` holds, and delete it. Called with the GIL. */
static void release(void *self)
{
std::unique_ptr<held_args> held(static_cast<held_args *>(self));

for (std::size_t i = 0; i < held->n_objects; ++i) {
held->objects[i].dec_ref();
}
}
};
} // end of namespace detail

/*!
* @brief Keeps `py_objs` alive until the work covered by `depends` completes.
*
* A usm_ndarray or USM memory object is held through the shared pointer owning
* its allocation, and any other object through a reference. The release is
* deferred with `dpctl::utils::defer_release`, which runs it with the GIL held
* once the work covered by `depends` completes.
*
* Returns the event of the empty kernel the release waits for, so its
* completion means that `py_objs` are no longer in use, not that they have
* been released.
*
* Expects the caller to hold the GIL.
*/
template <std::size_t num>
sycl::event keep_args_alive(sycl::queue &q,
const py::object (&py_objs)[num],
const std::vector<sycl::event> &depends = {})
{
std::size_t n_objects_held = 0;
std::array<std::shared_ptr<py::handle>, num> shp_arr{};
using held_t = detail::held_args<num>;

std::size_t n_usm_owners_held = 0;
std::array<std::shared_ptr<void>, num> shp_usm{};
// leaked if anything below throws, as the work in `depends` may still be
// using what it holds
held_t *held = new held_t{};

for (std::size_t i = 0; i < num; ++i) {
const auto &py_obj_i = py_objs[i];
if (detail::ManagedMemory::is_usm_managed_by_shared_ptr(py_obj_i)) {
const auto &shp =
held->usm_owners[held->n_usm_owners] =
detail::ManagedMemory::extract_shared_ptr(py_obj_i);
shp_usm[n_usm_owners_held] = shp;
++n_usm_owners_held;
++held->n_usm_owners;
}
else {
shp_arr[n_objects_held] = std::make_shared<py::handle>(py_obj_i);
shp_arr[n_objects_held]->inc_ref();
++n_objects_held;
held->objects[held->n_objects] = py_obj_i.inc_ref();
++held->n_objects;
}
}

bool use_depends = true;
sycl::event host_task_ev;

if (n_usm_owners_held > 0) {
host_task_ev = q.submit([&](sycl::handler &cgh) {
if (use_depends) {
cgh.depends_on(depends);
use_depends = false;
}
else {
cgh.depends_on(host_task_ev);
}
cgh.host_task([shp_usm = std::move(shp_usm)]() {
// no body, but shared pointers are captured in
// the lambda, ensuring that USM allocation is
// kept alive
});
});
}

if (n_objects_held > 0) {
host_task_ev = q.submit([&](sycl::handler &cgh) {
if (use_depends) {
cgh.depends_on(depends);
use_depends = false;
}
else {
cgh.depends_on(host_task_ev);
}
cgh.host_task([n_objects_held, shp_arr = std::move(shp_arr)]() {
py::gil_scoped_acquire acquire;

for (std::size_t i = 0; i < n_objects_held; ++i) {
shp_arr[i]->dec_ref();
}
});
});
}

return host_task_ev;
return ::dpctl::utils::defer_release(q, depends, &held_t::release, held);
}

// add to namespace for convenience
Expand Down
2 changes: 1 addition & 1 deletion dpnp/libtensor/AGENTS.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ A new extension's `CMakeLists.txt` lists its sources and calls `build_dpnp_pybin
## Rules for Changes
- C++20. Include what you use (`<cstdint>`, `<vector>`, ...), use `std::`-qualified fixed-width types (`std::int64_t`, `std::size_t`), make by-value parameters and locals `const` (`constexpr` where possible), and pass non-output parameters as `const T &`.
- Headers use `#pragma once`. New files in `include/` and `source/` add a `/// \file` block after the license header.
- Python-facing functions take `sycl::queue &exec_q` and `const std::vector<sycl::event> &depends`, and return `std::pair<sycl::event, sycl::event>` (keep-alive host-task event, compute event). Keep arguments alive with `dpnp::utils::keep_args_alive`.
- Python-facing functions take `sycl::queue &exec_q` and `const std::vector<sycl::event> &depends`, and return `std::pair<sycl::event, sycl::event>` (keep-alive event, compute event). Keep arguments alive with `dpnp::utils::keep_args_alive`.
- Validate inputs before launching: queue compatibility, contiguity, writability, memory overlap (including the same logical tensor), dtype and ndim. Reuse the helpers neighbouring files use: `check_queue`, `check_c_contig`, `check_no_overlap`, `check_writable` from `include/ext/validation_utils.hpp` (`putmask`, `statistics`, `isclose`, `interpolate`), or `dpnp::utils::queues_are_compatible`, `dpnp::tensor::overlap::MemoryOverlap` and `dpnp::tensor::validation::CheckWritable` (extensions and the Array API functions). Don't silently accept strided input.
- Throw `py::value_error` for bad arguments and `std::runtime_error` for internal failures; catch oneMKL and SYCL exceptions and rethrow them.
- Guard double, complex<double> and half with `dpnp::tensor::type_utils::validate_type_for_device<T>(q)`; don't use double literals or casts in kernels that must run without fp64.
Expand Down
6 changes: 4 additions & 2 deletions dpnp/libtensor/extensions/fft/in_place.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,12 @@ std::pair<sycl::event, sycl::event>
throw std::runtime_error(error_msg.str());
}

// the Python object already wrapping `descr`, which the FFT uses until done
const py::object py_descr = py::cast(&descr);
sycl::event args_ev =
dpnp::utils::keep_args_alive(exec_q, {in_out}, {fft_event});
dpnp::utils::keep_args_alive(exec_q, {py_descr, in_out}, {fft_event});

return std::make_pair(fft_event, args_ev);
return std::make_pair(args_ev, fft_event);
}

} // namespace dpnp::extensions::fft
6 changes: 4 additions & 2 deletions dpnp/libtensor/extensions/fft/out_of_place.tpp
Original file line number Diff line number Diff line change
Expand Up @@ -183,10 +183,12 @@ std::pair<sycl::event, sycl::event>
throw std::runtime_error(error_msg.str());
}

// the Python object already wrapping `descr`, which the FFT uses until done
const py::object py_descr = py::cast(&descr);
sycl::event args_ev =
dpnp::utils::keep_args_alive(exec_q, {in, out}, {fft_event});
dpnp::utils::keep_args_alive(exec_q, {py_descr, in, out}, {fft_event});

return std::make_pair(fft_event, args_ev);
return std::make_pair(args_ev, fft_event);
}

} // namespace dpnp::extensions::fft
4 changes: 2 additions & 2 deletions dpnp/libtensor/extensions/lapack/evd_batch_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,9 +119,9 @@ std::pair<sycl::event, sycl::event>
evd_batch_fn(exec_q, jobz_val, uplo_val, batch_size, n, eig_vecs_data,
eig_vals_data, depends);

sycl::event ht_ev = dpnp::utils::keep_args_alive(
sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive(
exec_q, {eig_vecs, eig_vals}, {evd_batch_ev});

return std::make_pair(ht_ev, evd_batch_ev);
return std::make_pair(keep_args_alive_ev, evd_batch_ev);
}
} // namespace dpnp::extensions::lapack::evd
4 changes: 2 additions & 2 deletions dpnp/libtensor/extensions/lapack/evd_common.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -111,9 +111,9 @@ std::pair<sycl::event, sycl::event>
sycl::event evd_ev = evd_fn(exec_q, jobz_val, uplo_val, n, eig_vecs_data,
eig_vals_data, depends);

sycl::event ht_ev =
sycl::event keep_args_alive_ev =
dpnp::utils::keep_args_alive(exec_q, {eig_vecs, eig_vals}, {evd_ev});

return std::make_pair(ht_ev, evd_ev);
return std::make_pair(keep_args_alive_ev, evd_ev);
}
} // namespace dpnp::extensions::lapack::evd
22 changes: 10 additions & 12 deletions dpnp/libtensor/extensions/lapack/geqrf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
//*****************************************************************************

#include <algorithm>
#include <memory>
#include <stdexcept>

#include <pybind11/pybind11.h>
Expand Down Expand Up @@ -68,7 +69,7 @@ static sycl::event geqrf_impl(sycl::queue &exec_q,
char *in_a,
std::int64_t lda,
char *in_tau,
std::vector<sycl::event> &host_task_events,
std::vector<sycl::event> &cleanup_events,
const std::vector<sycl::event> &depends)
{
type_utils::validate_type_for_device<T>(exec_q);
Expand Down Expand Up @@ -136,14 +137,11 @@ static sycl::event geqrf_impl(sycl::queue &exec_q,
throw std::runtime_error(error_msg.str());
}

sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) {
cgh.depends_on(geqrf_event);
auto ctx = exec_q.get_context();
cgh.host_task([ctx, scratchpad]() {
dpnp::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx);
});
});
host_task_events.push_back(clean_up_event);
using dpnp::tensor::alloc_utils::USMDeleter;
sycl::event clean_up_event = dpnp::tensor::alloc_utils::async_smart_free(
exec_q, {geqrf_event},
std::unique_ptr<T, USMDeleter>(scratchpad, USMDeleter(exec_q)));
cleanup_events.push_back(clean_up_event);

return geqrf_event;
}
Expand Down Expand Up @@ -235,12 +233,12 @@ std::pair<sycl::event, sycl::event>
std::to_string(min_m_n) + " array is expected.");
}

std::vector<sycl::event> host_task_events;
std::vector<sycl::event> cleanup_events;
sycl::event geqrf_ev = geqrf_fn(exec_q, m, n, a_array_data, lda,
tau_array_data, host_task_events, depends);
tau_array_data, cleanup_events, depends);

sycl::event args_ev = dpnp::utils::keep_args_alive(
exec_q, {a_array, tau_array}, host_task_events);
exec_q, {a_array, tau_array}, cleanup_events);

return std::make_pair(args_ev, geqrf_ev);
}
Expand Down
22 changes: 10 additions & 12 deletions dpnp/libtensor/extensions/lapack/geqrf_batch.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
// THE POSSIBILITY OF SUCH DAMAGE.
//*****************************************************************************

#include <memory>
#include <stdexcept>

#include <pybind11/pybind11.h>
Expand Down Expand Up @@ -75,7 +76,7 @@ static sycl::event geqrf_batch_impl(sycl::queue &exec_q,
char *in_tau,
std::int64_t stride_tau,
std::int64_t batch_size,
std::vector<sycl::event> &host_task_events,
std::vector<sycl::event> &cleanup_events,
const std::vector<sycl::event> &depends)
{
type_utils::validate_type_for_device<T>(exec_q);
Expand Down Expand Up @@ -157,14 +158,11 @@ static sycl::event geqrf_batch_impl(sycl::queue &exec_q,
throw std::runtime_error(error_msg.str());
}

sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) {
cgh.depends_on(geqrf_batch_event);
auto ctx = exec_q.get_context();
cgh.host_task([ctx, scratchpad]() {
dpnp::tensor::alloc_utils::sycl_free_noexcept(scratchpad, ctx);
});
});
host_task_events.push_back(clean_up_event);
using dpnp::tensor::alloc_utils::USMDeleter;
sycl::event clean_up_event = dpnp::tensor::alloc_utils::async_smart_free(
exec_q, {geqrf_batch_event},
std::unique_ptr<T, USMDeleter>(scratchpad, USMDeleter(exec_q)));
cleanup_events.push_back(clean_up_event);
return geqrf_batch_event;
}

Expand Down Expand Up @@ -243,13 +241,13 @@ std::pair<sycl::event, sycl::event>

const std::int64_t lda = std::max<size_t>(1UL, m);

std::vector<sycl::event> host_task_events;
std::vector<sycl::event> cleanup_events;
sycl::event geqrf_batch_ev = geqrf_batch_fn(
exec_q, m, n, a_array_data, lda, stride_a, tau_array_data, stride_tau,
batch_size, host_task_events, depends);
batch_size, cleanup_events, depends);

sycl::event args_ev = dpnp::utils::keep_args_alive(
exec_q, {a_array, tau_array}, host_task_events);
exec_q, {a_array, tau_array}, cleanup_events);

return std::make_pair(args_ev, geqrf_batch_ev);
}
Expand Down
21 changes: 10 additions & 11 deletions dpnp/libtensor/extensions/lapack/gesv.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
//*****************************************************************************

#include <exception>
#include <memory>
#include <stdexcept>

#include <pybind11/pybind11.h>
Expand All @@ -35,6 +36,7 @@
#include "ext/common.hpp"

// dpnp tensor headers
#include "utils/sycl_alloc_utils.hpp"
#include "utils/type_utils.hpp"

#include "common_helpers.hpp"
Expand Down Expand Up @@ -214,16 +216,13 @@ static sycl::event gesv_impl(sycl::queue &exec_q,
throw std::runtime_error(error_msg.str());
}

sycl::event ht_ev = exec_q.submit([&](sycl::handler &cgh) {
cgh.depends_on(comp_event);
auto ctx = exec_q.get_context();
cgh.host_task([ctx, scratchpad, ipiv]() {
sycl_free_noexcept(scratchpad, ctx);
sycl_free_noexcept(ipiv, ctx);
});
});
using dpnp::tensor::alloc_utils::USMDeleter;
sycl::event cleanup_ev = dpnp::tensor::alloc_utils::async_smart_free(
exec_q, {comp_event},
std::unique_ptr<T, USMDeleter>(scratchpad, USMDeleter(exec_q)),
std::unique_ptr<std::int64_t, USMDeleter>(ipiv, USMDeleter(exec_q)));

return ht_ev;
return cleanup_ev;
}

std::pair<sycl::event, sycl::event>
Expand Down Expand Up @@ -276,10 +275,10 @@ std::pair<sycl::event, sycl::event>
sycl::event gesv_ev = gesv_fn(exec_q, n, nrhs, coeff_matrix_data,
dependent_vals_data, depends);

sycl::event ht_ev = dpnp::utils::keep_args_alive(
sycl::event keep_args_alive_ev = dpnp::utils::keep_args_alive(
exec_q, {coeff_matrix, dependent_vals}, {gesv_ev});

return std::make_pair(ht_ev, gesv_ev);
return std::make_pair(keep_args_alive_ev, gesv_ev);
}

template <typename fnT, typename T>
Expand Down
Loading
Loading