Skip to content
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

[enhancement] Refactor onedal/datatypes in preparation for dlpack support #2195

Merged
merged 17 commits into from
Jan 28, 2025
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
remove unneeded includes and move dtype_dispatcher into a central loc…
…ation
icfaust committed Jan 27, 2025
commit e4a0a564e5ddb0487957597b63ae3b632eae42de
File renamed without changes.
1 change: 0 additions & 1 deletion onedal/datatypes/sycl_usm/data_conversion.cpp
Original file line number Diff line number Diff line change
@@ -29,7 +29,6 @@
#include "onedal/common/sycl_interfaces.hpp"
#include "onedal/datatypes/sycl_usm/data_conversion.hpp"
#include "onedal/datatypes/sycl_usm/dtype_conversion.hpp"
#include "onedal/datatypes/sycl_usm/dtype_dispatcher.hpp"
#include "onedal/datatypes/sycl_usm/sycl_usm_helpers.hpp"

namespace oneapi::dal::python::sycl_usm {
2 changes: 1 addition & 1 deletion onedal/datatypes/sycl_usm/dtype_conversion.cpp
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@
#include "oneapi/dal/detail/common.hpp"

#include "onedal/datatypes/sycl_usm/dtype_conversion.hpp"
#include "onedal/datatypes/sycl_usm/dtype_dispatcher.hpp"
#include "onedal/datatypes/dtype_dispatcher.hpp"

namespace oneapi::dal::python::sycl_usm {


Unchanged files with check annotations Beta

return res_table;
}
dal::table convert_to_table(py::object inp_obj, py::object queue) {
dal::table res;
#ifdef ONEDAL_DATA_PARALLEL
if (!queue.is(py::none()) && !queue.attr("sycl_device").attr("has_aspect_fp64").cast<bool>()) {
// If the queue exists, doesn't have the fp64 aspect, and the data is float64
// then cast it to float32
int type = reinterpret_cast<PyArray_Descr *>(inp_obj.attr("dtype").ptr())->type_num;
if (type == NPY_DOUBLE || type == NPY_DOUBLELTR) {
PyErr_WarnEx(
PyExc_RuntimeWarning,
"Data will be converted into float32 from float64 because device does not support it",
1);
// use astype instead of PyArray_Cast in order to support scipy sparse inputs
inp_obj = inp_obj.attr("astype")(py::dtype::of<float>());
res = convert_to_table(
inp_obj); // queue will be set to none, as this check is no longer necessary
return res;
}
}
#endif // ONEDAL_DATA_PARALLEL
PyObject *obj = inp_obj.ptr();
if (obj == nullptr || obj == Py_None) {
return res;
}
if (is_array(obj)) {
PyArrayObject *ary = reinterpret_cast<PyArrayObject *>(obj);
if (!PyArray_ISCARRAY_RO(ary) && !PyArray_ISFARRAY_RO(ary)) {
// NOTE: this will make a C-contiguous deep copy of the data
// this is expected to be a special case
obj = reinterpret_cast<PyObject *>(PyArray_GETCONTIGUOUS(ary));
if (obj) {
res = convert_to_table(py::cast<py::object>(obj), queue);
Py_DECREF(obj);
return res;
}
else {
throw std::invalid_argument(
"[convert_to_table] Numpy input could not be converted into onedal table.");
}
}
#define MAKE_HOMOGEN_TABLE(CType) res = convert_to_homogen_impl<CType>(ary);
SET_NPY_FEATURE(array_type(ary),
array_type_sizeof(ary),
MAKE_HOMOGEN_TABLE,
throw std::invalid_argument("Found unsupported array type"));
#undef MAKE_HOMOGEN_TABLE
}
else if (strcmp(Py_TYPE(obj)->tp_name, "csr_matrix") == 0 ||
strcmp(Py_TYPE(obj)->tp_name, "csr_array") == 0) {
PyObject *py_data = PyObject_GetAttrString(obj, "data");
PyObject *py_column_indices = PyObject_GetAttrString(obj, "indices");
PyObject *py_row_indices = PyObject_GetAttrString(obj, "indptr");
PyObject *py_shape = PyObject_GetAttrString(obj, "shape");
if (!(is_array(py_data) && is_array(py_column_indices) && is_array(py_row_indices) &&
array_numdims(py_data) == 1 && array_numdims(py_column_indices) == 1 &&
array_numdims(py_row_indices) == 1)) {
throw std::invalid_argument("[convert_to_table] Got invalid csr_matrix object.");
}
PyObject *np_data = PyArray_FROMANY(py_data, array_type(py_data), 0, 0, NPY_ARRAY_CARRAY);
PyObject *np_column_indices =
PyArray_FROMANY(py_column_indices,
NPY_UINT64,
0,
0,
NPY_ARRAY_CARRAY | NPY_ARRAY_ENSURECOPY | NPY_ARRAY_FORCECAST);
PyObject *np_row_indices =
PyArray_FROMANY(py_row_indices,
NPY_UINT64,
0,
0,
NPY_ARRAY_CARRAY | NPY_ARRAY_ENSURECOPY | NPY_ARRAY_FORCECAST);
PyObject *np_row_count = PyTuple_GetItem(py_shape, 0);
PyObject *np_column_count = PyTuple_GetItem(py_shape, 1);
if (!(np_data && np_column_indices && np_row_indices && np_row_count && np_column_count)) {
throw std::invalid_argument(
"[convert_to_table] Failed accessing csr data when converting csr_matrix.\n");
}
const std::int64_t row_count = static_cast<std::int64_t>(PyLong_AsSsize_t(np_row_count));
const std::int64_t column_count =
static_cast<std::int64_t>(PyLong_AsSsize_t(np_column_count));
#define MAKE_CSR_TABLE(CType) \
res = convert_to_csr_impl<CType>(np_data, \
np_column_indices, \
np_row_indices, \
row_count, \
column_count);
SET_NPY_FEATURE(array_type(np_data),
array_type_sizeof(np_data),
MAKE_CSR_TABLE,
throw std::invalid_argument("Found unsupported data type in csr_matrix"));
#undef MAKE_CSR_TABLE
Py_DECREF(np_column_indices);
Py_DECREF(np_row_indices);
}
else {
throw std::invalid_argument(
"[convert_to_table] Not available input format for convert Python object to onedal table.");
}
return res;
}

Check notice on line 262 in onedal/datatypes/numpy/data_conversion.cpp

codefactor.io / CodeFactor

onedal/datatypes/numpy/data_conversion.cpp#L155-L262

Complex Method
static void free_capsule(PyObject *cap) {
// TODO: check safe cast
dal::base *stored_array = static_cast<dal::base *>(PyCapsule_GetPointer(cap, NULL));