diff --git a/onedal/basic_statistics/basic_statistics.cpp b/onedal/basic_statistics/basic_statistics.cpp index 6d037ef08a..1d8e860ff4 100644 --- a/onedal/basic_statistics/basic_statistics.cpp +++ b/onedal/basic_statistics/basic_statistics.cpp @@ -20,7 +20,7 @@ #include "onedal/version.hpp" #define NO_IMPORT_ARRAY // import_array called in table.cpp -#include "onedal/datatypes/data_conversion.hpp" +#include "onedal/datatypes/numpy/data_conversion.hpp" #include #include @@ -210,30 +210,30 @@ void init_partial_compute_result(py::module_& m) { .def(py::pickle( [](const result_t& res) { return py::make_tuple( - py::cast(convert_to_pyobject(res.get_partial_n_rows())), - py::cast(convert_to_pyobject(res.get_partial_min())), - py::cast(convert_to_pyobject(res.get_partial_max())), - py::cast(convert_to_pyobject(res.get_partial_sum())), - py::cast(convert_to_pyobject(res.get_partial_sum_squares())), + py::cast(numpy::convert_to_pyobject(res.get_partial_n_rows())), + py::cast(numpy::convert_to_pyobject(res.get_partial_min())), + py::cast(numpy::convert_to_pyobject(res.get_partial_max())), + py::cast(numpy::convert_to_pyobject(res.get_partial_sum())), + py::cast(numpy::convert_to_pyobject(res.get_partial_sum_squares())), py::cast( - convert_to_pyobject(res.get_partial_sum_squares_centered()))); + numpy::convert_to_pyobject(res.get_partial_sum_squares_centered()))); }, [](py::tuple t) { if (t.size() != 6) throw std::runtime_error("Invalid state!"); result_t res; if (py::cast(t[0].attr("size")) != 0) - res.set_partial_n_rows(convert_to_table(t[0])); + res.set_partial_n_rows(numpy::convert_to_table(t[0])); if (py::cast(t[1].attr("size")) != 0) - res.set_partial_min(convert_to_table(t[1])); + res.set_partial_min(numpy::convert_to_table(t[1])); if (py::cast(t[2].attr("size")) != 0) - res.set_partial_max(convert_to_table(t[2])); + res.set_partial_max(numpy::convert_to_table(t[2])); if (py::cast(t[3].attr("size")) != 0) - res.set_partial_sum(convert_to_table(t[3])); + res.set_partial_sum(numpy::convert_to_table(t[3])); if (py::cast(t[4].attr("size")) != 0) - res.set_partial_sum_squares(convert_to_table(t[4])); + res.set_partial_sum_squares(numpy::convert_to_table(t[4])); if (py::cast(t[5].attr("size")) != 0) - res.set_partial_sum_squares_centered(convert_to_table(t[5])); + res.set_partial_sum_squares_centered(numpy::convert_to_table(t[5])); return res; })); diff --git a/onedal/covariance/covariance.cpp b/onedal/covariance/covariance.cpp index cc9bf83aba..231b1ba3f0 100644 --- a/onedal/covariance/covariance.cpp +++ b/onedal/covariance/covariance.cpp @@ -19,7 +19,7 @@ #include "oneapi/dal/algo/covariance.hpp" #define NO_IMPORT_ARRAY // import_array called in table.cpp -#include "onedal/datatypes/data_conversion.hpp" +#include "onedal/datatypes/numpy/data_conversion.hpp" #include "onedal/common.hpp" #include "onedal/version.hpp" @@ -141,20 +141,21 @@ inline void init_partial_compute_result(pybind11::module_& m) { .def(py::pickle( [](const result_t& res) { return py::make_tuple( - py::cast(convert_to_pyobject(res.get_partial_n_rows())), - py::cast(convert_to_pyobject(res.get_partial_crossproduct())), - py::cast(convert_to_pyobject(res.get_partial_sum()))); + py::cast(numpy::convert_to_pyobject(res.get_partial_n_rows())), + py::cast( + numpy::convert_to_pyobject(res.get_partial_crossproduct())), + py::cast(numpy::convert_to_pyobject(res.get_partial_sum()))); }, [](py::tuple t) { if (t.size() != 3) throw std::runtime_error("Invalid state!"); result_t res; if (py::cast(t[0].attr("size")) != 0) - res.set_partial_n_rows(convert_to_table(t[0])); + res.set_partial_n_rows(numpy::convert_to_table(t[0])); if (py::cast(t[1].attr("size")) != 0) - res.set_partial_crossproduct(convert_to_table(t[1])); + res.set_partial_crossproduct(numpy::convert_to_table(t[1])); if (py::cast(t[2].attr("size")) != 0) - res.set_partial_sum(convert_to_table(t[2])); + res.set_partial_sum(numpy::convert_to_table(t[2])); return res; })); } diff --git a/onedal/datatypes/utils/dtype_dispatcher.hpp b/onedal/datatypes/dtype_dispatcher.hpp similarity index 76% rename from onedal/datatypes/utils/dtype_dispatcher.hpp rename to onedal/datatypes/dtype_dispatcher.hpp index d3d2b2e0f3..decbe66a66 100644 --- a/onedal/datatypes/utils/dtype_dispatcher.hpp +++ b/onedal/datatypes/dtype_dispatcher.hpp @@ -86,6 +86,27 @@ constexpr inline void apply(Op&& op, Args&&... args) { #endif // Version check +#define SET_CTYPE_FROM_DAL_TYPE(_T, _FUNCT, _EXCEPTION) \ + switch (_T) { \ + case dal::data_type::float32: { \ + _FUNCT(float); \ + break; \ + } \ + case dal::data_type::float64: { \ + _FUNCT(double); \ + break; \ + } \ + case dal::data_type::int32: { \ + _FUNCT(std::int32_t); \ + break; \ + } \ + case dal::data_type::int64: { \ + _FUNCT(std::int64_t); \ + break; \ + } \ + default: _EXCEPTION; \ + }; + namespace oneapi::dal::python { using supported_types_t = std::tuple auto reverse_map(const std::map& input) { @@ -50,4 +50,4 @@ npy_dtype_t convert_dal_to_npy_type(dal::data_type type) { return get_dal_to_npy_map().at(type); } -} // namespace oneapi::dal::python +} // namespace oneapi::dal::python::numpy diff --git a/onedal/datatypes/utils/numpy_helpers.hpp b/onedal/datatypes/numpy/numpy_utils.hpp similarity index 98% rename from onedal/datatypes/utils/numpy_helpers.hpp rename to onedal/datatypes/numpy/numpy_utils.hpp index 461498d1b2..61f8b53aac 100644 --- a/onedal/datatypes/utils/numpy_helpers.hpp +++ b/onedal/datatypes/numpy/numpy_utils.hpp @@ -140,7 +140,7 @@ #define array_data(a) PyArray_DATA((PyArrayObject *)a) #define array_size(a, i) PyArray_DIM((PyArrayObject *)a, i) -namespace oneapi::dal::python { +namespace oneapi::dal::python::numpy { using npy_dtype_t = decltype(NPY_FLOAT); using npy_to_dal_t = std::map; @@ -152,4 +152,4 @@ const dal_to_npy_t &get_dal_to_npy_map(); dal::data_type convert_npy_to_dal_type(npy_dtype_t); npy_dtype_t convert_dal_to_npy_type(dal::data_type); -} // namespace oneapi::dal::python +} // namespace oneapi::dal::python::numpy diff --git a/onedal/datatypes/data_conversion_sua_iface.cpp b/onedal/datatypes/sycl_usm/data_conversion.cpp similarity index 96% rename from onedal/datatypes/data_conversion_sua_iface.cpp rename to onedal/datatypes/sycl_usm/data_conversion.cpp index 303b9c9629..93f5d0048f 100644 --- a/onedal/datatypes/data_conversion_sua_iface.cpp +++ b/onedal/datatypes/sycl_usm/data_conversion.cpp @@ -27,12 +27,11 @@ #include "oneapi/dal/table/detail/homogen_utils.hpp" #include "onedal/common/sycl_interfaces.hpp" -#include "onedal/datatypes/data_conversion_sua_iface.hpp" -#include "onedal/datatypes/utils/dtype_conversions.hpp" -#include "onedal/datatypes/utils/dtype_dispatcher.hpp" -#include "onedal/datatypes/utils/sua_iface_helpers.hpp" +#include "onedal/datatypes/sycl_usm/data_conversion.hpp" +#include "onedal/datatypes/sycl_usm/dtype_conversion.hpp" +#include "onedal/datatypes/sycl_usm/sycl_usm_utils.hpp" -namespace oneapi::dal::python { +namespace oneapi::dal::python::sycl_usm { using namespace pybind11::literals; // Please follow & table_obj) { table_obj.def_property_readonly("__sycl_usm_array_interface__", &construct_sua_iface); } -} // namespace oneapi::dal::python +} // namespace oneapi::dal::python::sycl_usm #endif // ONEDAL_DATA_PARALLEL diff --git a/onedal/datatypes/data_conversion_sua_iface.hpp b/onedal/datatypes/sycl_usm/data_conversion.hpp similarity index 91% rename from onedal/datatypes/data_conversion_sua_iface.hpp rename to onedal/datatypes/sycl_usm/data_conversion.hpp index 4d4b33e4da..d162b6e2ac 100644 --- a/onedal/datatypes/data_conversion_sua_iface.hpp +++ b/onedal/datatypes/sycl_usm/data_conversion.hpp @@ -23,12 +23,12 @@ #include "oneapi/dal/table/common.hpp" -namespace oneapi::dal::python { +namespace oneapi::dal::python::sycl_usm { namespace py = pybind11; // Convert oneDAL table with zero-copy by use of `__sycl_usm_array_interface__` protocol. -dal::table convert_from_sua_iface(py::object obj); +dal::table convert_to_table(py::object obj); // Create a dictionary for `__sycl_usm_array_interface__` protocol from oneDAL table properties. py::dict construct_sua_iface(const dal::table& input); @@ -37,4 +37,4 @@ py::dict construct_sua_iface(const dal::table& input); // USM allocations. void define_sycl_usm_array_property(py::class_& t); -} // namespace oneapi::dal::python +} // namespace oneapi::dal::python::sycl_usm diff --git a/onedal/datatypes/utils/dtype_conversions.cpp b/onedal/datatypes/sycl_usm/dtype_conversion.cpp similarity index 95% rename from onedal/datatypes/utils/dtype_conversions.cpp rename to onedal/datatypes/sycl_usm/dtype_conversion.cpp index c3abb966ee..ab5dba5da9 100644 --- a/onedal/datatypes/utils/dtype_conversions.cpp +++ b/onedal/datatypes/sycl_usm/dtype_conversion.cpp @@ -21,10 +21,10 @@ #include "oneapi/dal/common.hpp" #include "oneapi/dal/detail/common.hpp" -#include "onedal/datatypes/utils/dtype_conversions.hpp" -#include "onedal/datatypes/utils/dtype_dispatcher.hpp" +#include "onedal/datatypes/sycl_usm/dtype_conversion.hpp" +#include "onedal/datatypes/dtype_dispatcher.hpp" -namespace oneapi::dal::python { +namespace oneapi::dal::python::sycl_usm { using fwd_map_t = std::unordered_map; using inv_map_t = std::unordered_map; @@ -139,4 +139,4 @@ std::string convert_dal_to_sua_type(dal::data_type dtype) { return get_inv_map().at(dtype); } -} // namespace oneapi::dal::python +} // namespace oneapi::dal::python::sycl_usm diff --git a/onedal/datatypes/sycl_usm/dtype_conversion.hpp b/onedal/datatypes/sycl_usm/dtype_conversion.hpp new file mode 100644 index 0000000000..67bd1b945c --- /dev/null +++ b/onedal/datatypes/sycl_usm/dtype_conversion.hpp @@ -0,0 +1,33 @@ +/******************************************************************************* +* Copyright 2024 Intel Corporation +* +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*******************************************************************************/ + +#pragma once + +#include + +#include + +#include "oneapi/dal/common.hpp" +#include "onedal/datatypes/dtype_dispatcher.hpp" + +namespace py = pybind11; + +namespace oneapi::dal::python::sycl_usm { + +dal::data_type convert_sua_to_dal_type(std::string dtype); +std::string convert_dal_to_sua_type(dal::data_type dtype); + +} // namespace oneapi::dal::python::sycl_usm diff --git a/onedal/datatypes/utils/sua_iface_helpers.cpp b/onedal/datatypes/sycl_usm/sycl_usm_utils.cpp similarity index 96% rename from onedal/datatypes/utils/sua_iface_helpers.cpp rename to onedal/datatypes/sycl_usm/sycl_usm_utils.cpp index d345a35645..cbc7de4bec 100644 --- a/onedal/datatypes/utils/sua_iface_helpers.cpp +++ b/onedal/datatypes/sycl_usm/sycl_usm_utils.cpp @@ -27,9 +27,8 @@ #include "oneapi/dal/table/detail/homogen_utils.hpp" #include "onedal/common/sycl_interfaces.hpp" -#include "onedal/datatypes/data_conversion_sua_iface.hpp" -#include "onedal/datatypes/utils/dtype_conversions.hpp" -#include "onedal/datatypes/utils/dtype_dispatcher.hpp" +#include "onedal/datatypes/sycl_usm/data_conversion.hpp" +#include "onedal/datatypes/sycl_usm/dtype_conversion.hpp" /* __sycl_usm_array_interface__ * @@ -53,7 +52,7 @@ * api_reference/dpctl/sycl_usm_array_interface.html#sycl-usm-array-interface-attribute> */ -namespace oneapi::dal::python { +namespace oneapi::dal::python::sycl_usm { // Convert a string encoding elemental data type of the array to oneDAL homogen table data type. dal::data_type get_sua_dtype(const py::dict& sua) { @@ -197,6 +196,6 @@ py::tuple get_npy_strides(const dal::data_layout& data_layout, return strides; } -} // namespace oneapi::dal::python +} // namespace oneapi::dal::python::sycl_usm #endif // ONEDAL_DATA_PARALLEL diff --git a/onedal/datatypes/utils/sua_iface_helpers.hpp b/onedal/datatypes/sycl_usm/sycl_usm_utils.hpp similarity index 90% rename from onedal/datatypes/utils/sua_iface_helpers.hpp rename to onedal/datatypes/sycl_usm/sycl_usm_utils.hpp index 050dad036f..75834bedef 100644 --- a/onedal/datatypes/utils/sua_iface_helpers.hpp +++ b/onedal/datatypes/sycl_usm/sycl_usm_utils.hpp @@ -27,11 +27,10 @@ #include "oneapi/dal/table/detail/homogen_utils.hpp" #include "onedal/common/sycl_interfaces.hpp" -#include "onedal/datatypes/data_conversion_sua_iface.hpp" -#include "onedal/datatypes/utils/dtype_conversions.hpp" -#include "onedal/datatypes/utils/dtype_dispatcher.hpp" +#include "onedal/datatypes/sycl_usm/data_conversion.hpp" +#include "onedal/datatypes/sycl_usm/dtype_conversion.hpp" -namespace oneapi::dal::python { +namespace oneapi::dal::python::sycl_usm { dal::data_type get_sua_dtype(const py::dict& sua); @@ -62,6 +61,6 @@ py::tuple get_npy_strides(const dal::data_layout& data_layout, npy_intp row_count, npy_intp column_count); -} // namespace oneapi::dal::python +} // namespace oneapi::dal::python::sycl_usm #endif // ONEDAL_DATA_PARALLEL diff --git a/onedal/datatypes/table.cpp b/onedal/datatypes/table.cpp index ed8a931522..df0957ea5d 100644 --- a/onedal/datatypes/table.cpp +++ b/onedal/datatypes/table.cpp @@ -18,11 +18,11 @@ #include "oneapi/dal/table/homogen.hpp" #ifdef ONEDAL_DATA_PARALLEL -#include "onedal/datatypes/data_conversion_sua_iface.hpp" +#include "onedal/datatypes/sycl_usm/data_conversion.hpp" #endif // ONEDAL_DATA_PARALLEL -#include "onedal/datatypes/data_conversion.hpp" -#include "onedal/datatypes/utils/numpy_helpers.hpp" +#include "onedal/datatypes/numpy/data_conversion.hpp" +#include "onedal/datatypes/numpy/numpy_utils.hpp" #include "onedal/common/pybind11_helpers.hpp" #include "onedal/version.hpp" @@ -74,25 +74,25 @@ ONEDAL_PY_INIT_MODULE(table) { }); table_obj.def_property_readonly("dtype", [](const table& t) { // returns a numpy dtype, even if source was not from numpy - return py::dtype(convert_dal_to_npy_type(t.get_metadata().get_data_type(0))); + return py::dtype(numpy::convert_dal_to_npy_type(t.get_metadata().get_data_type(0))); }); #ifdef ONEDAL_DATA_PARALLEL - define_sycl_usm_array_property(table_obj); + sycl_usm::define_sycl_usm_array_property(table_obj); #endif // ONEDAL_DATA_PARALLEL m.def("to_table", [](py::object obj, py::object queue) { #ifdef ONEDAL_DATA_PARALLEL if (py::hasattr(obj, "__sycl_usm_array_interface__")) { - return convert_from_sua_iface(obj); + return sycl_usm::convert_to_table(obj); } #endif // ONEDAL_DATA_PARALLEL - return convert_to_table(obj, queue); + return numpy::convert_to_table(obj, queue); }); m.def("from_table", [](const dal::table& t) -> py::handle { - auto* obj_ptr = convert_to_pyobject(t); + auto* obj_ptr = numpy::convert_to_pyobject(t); return obj_ptr; }); } diff --git a/onedal/datatypes/utils/dtype_conversions.hpp b/onedal/datatypes/utils/dtype_conversions.hpp deleted file mode 100644 index 8b9dd89db6..0000000000 --- a/onedal/datatypes/utils/dtype_conversions.hpp +++ /dev/null @@ -1,53 +0,0 @@ -/******************************************************************************* -* Copyright 2024 Intel Corporation -* -* Licensed under the Apache License, Version 2.0 (the "License"); -* you may not use this file except in compliance with the License. -* You may obtain a copy of the License at -* -* http://www.apache.org/licenses/LICENSE-2.0 -* -* Unless required by applicable law or agreed to in writing, software -* distributed under the License is distributed on an "AS IS" BASIS, -* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. -* See the License for the specific language governing permissions and -* limitations under the License. -*******************************************************************************/ - -#pragma once - -#include - -#include - -#include "oneapi/dal/common.hpp" - -namespace py = pybind11; - -#define SET_CTYPE_FROM_DAL_TYPE(_T, _FUNCT, _EXCEPTION) \ - switch (_T) { \ - case dal::data_type::float32: { \ - _FUNCT(float); \ - break; \ - } \ - case dal::data_type::float64: { \ - _FUNCT(double); \ - break; \ - } \ - case dal::data_type::int32: { \ - _FUNCT(std::int32_t); \ - break; \ - } \ - case dal::data_type::int64: { \ - _FUNCT(std::int64_t); \ - break; \ - } \ - default: _EXCEPTION; \ - }; - -namespace oneapi::dal::python { - -dal::data_type convert_sua_to_dal_type(std::string dtype); -std::string convert_dal_to_sua_type(dal::data_type dtype); - -} // namespace oneapi::dal::python diff --git a/onedal/decomposition/pca.cpp b/onedal/decomposition/pca.cpp index 929824ce9b..49226f3265 100644 --- a/onedal/decomposition/pca.cpp +++ b/onedal/decomposition/pca.cpp @@ -16,7 +16,7 @@ #include "oneapi/dal/algo/pca.hpp" #include "onedal/common.hpp" #define NO_IMPORT_ARRAY // import_array called in table.cpp -#include "onedal/datatypes/data_conversion.hpp" +#include "onedal/datatypes/numpy/data_conversion.hpp" namespace py = pybind11; @@ -134,12 +134,13 @@ void init_partial_train_result(py::module_& m) { int auxiliary_size = res.get_auxiliary_table_count(); for (int i = 0; i < auxiliary_size; i++) { auto aux_table = res.get_auxiliary_table(i); - auxiliary.append(py::cast(convert_to_pyobject(aux_table))); + auxiliary.append(py::cast(numpy::convert_to_pyobject(aux_table))); } return py::make_tuple( - py::cast(convert_to_pyobject(res.get_partial_n_rows())), - py::cast(convert_to_pyobject(res.get_partial_crossproduct())), - py::cast(convert_to_pyobject(res.get_partial_sum())), + py::cast(numpy::convert_to_pyobject(res.get_partial_n_rows())), + py::cast( + numpy::convert_to_pyobject(res.get_partial_crossproduct())), + py::cast(numpy::convert_to_pyobject(res.get_partial_sum())), auxiliary); }, [](py::tuple t) { @@ -147,14 +148,14 @@ void init_partial_train_result(py::module_& m) { throw std::runtime_error("Invalid state!"); result_t res; if (py::cast(t[0].attr("size")) != 0) - res.set_partial_n_rows(convert_to_table(t[0])); + res.set_partial_n_rows(numpy::convert_to_table(t[0])); if (py::cast(t[1].attr("size")) != 0) - res.set_partial_crossproduct(convert_to_table(t[1])); + res.set_partial_crossproduct(numpy::convert_to_table(t[1])); if (py::cast(t[2].attr("size")) != 0) - res.set_partial_sum(convert_to_table(t[2])); + res.set_partial_sum(numpy::convert_to_table(t[2])); py::list aux_list = t[3].cast(); for (int i = 0; i < aux_list.size(); i++) { - res.set_auxiliary_table(convert_to_table(aux_list[i])); + res.set_auxiliary_table(numpy::convert_to_table(aux_list[i])); } return res; })); diff --git a/onedal/linear_model/linear_model.cpp b/onedal/linear_model/linear_model.cpp index 450ef905ea..ae32a752e0 100644 --- a/onedal/linear_model/linear_model.cpp +++ b/onedal/linear_model/linear_model.cpp @@ -20,7 +20,7 @@ #include "onedal/version.hpp" #define NO_IMPORT_ARRAY // import_array called in table.cpp -#include "onedal/datatypes/data_conversion.hpp" +#include "onedal/datatypes/numpy/data_conversion.hpp" #include @@ -244,17 +244,17 @@ void init_partial_train_result(py::module_& m) { .def(py::pickle( [](const result_t& res) { return py::make_tuple( - py::cast(convert_to_pyobject(res.get_partial_xtx())), - py::cast(convert_to_pyobject(res.get_partial_xty()))); + py::cast(numpy::convert_to_pyobject(res.get_partial_xtx())), + py::cast(numpy::convert_to_pyobject(res.get_partial_xty()))); }, [](py::tuple t) { if (t.size() != 2) throw std::runtime_error("Invalid state!"); result_t res; if (py::cast(t[0].attr("size")) != 0) - res.set_partial_xtx(convert_to_table(t[0])); + res.set_partial_xtx(numpy::convert_to_table(t[0])); if (py::cast(t[1].attr("size")) != 0) - res.set_partial_xty(convert_to_table(t[1])); + res.set_partial_xty(numpy::convert_to_table(t[1])); return res; })); }