|
| 1 | +//***************************************************************************** |
| 2 | +// Copyright (c) 2024, Intel Corporation |
| 3 | +// All rights reserved. |
| 4 | +// |
| 5 | +// Redistribution and use in source and binary forms, with or without |
| 6 | +// maxification, are permitted provided that the following conditions are met: |
| 7 | +// - Redistributions of source code must retain the above copyright notice, |
| 8 | +// this list of conditions and the following disclaimer. |
| 9 | +// - Redistributions in binary form must reproduce the above copyright notice, |
| 10 | +// this list of conditions and the following disclaimer in the documentation |
| 11 | +// and/or other materials provided with the distribution. |
| 12 | +// |
| 13 | +// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| 14 | +// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| 15 | +// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| 16 | +// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| 17 | +// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| 18 | +// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| 19 | +// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| 20 | +// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| 21 | +// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| 22 | +// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF |
| 23 | +// THE POSSIBILITY OF SUCH DAMAGE. |
| 24 | +//***************************************************************************** |
| 25 | + |
| 26 | +#include <sycl/sycl.hpp> |
| 27 | + |
| 28 | +#include "dpctl4pybind11.hpp" |
| 29 | + |
| 30 | +#include "kernels/elementwise_functions/logaddexp2.hpp" |
| 31 | +#include "logaddexp2.hpp" |
| 32 | +#include "populate.hpp" |
| 33 | + |
| 34 | +// include a local copy of elementwise common header from dpctl tensor: |
| 35 | +// dpctl/tensor/libtensor/source/elementwise_functions/elementwise_functions.hpp |
| 36 | +// TODO: replace by including dpctl header once available |
| 37 | +#include "../../elementwise_functions/elementwise_functions.hpp" |
| 38 | + |
| 39 | +// dpctl tensor headers |
| 40 | +#include "kernels/elementwise_functions/common.hpp" |
| 41 | +#include "kernels/elementwise_functions/logaddexp.hpp" |
| 42 | +#include "utils/type_dispatch.hpp" |
| 43 | + |
| 44 | +namespace dpnp::extensions::ufunc |
| 45 | +{ |
| 46 | +namespace py = pybind11; |
| 47 | +namespace py_int = dpnp::extensions::py_internal; |
| 48 | +namespace td_ns = dpctl::tensor::type_dispatch; |
| 49 | + |
| 50 | +namespace impl |
| 51 | +{ |
| 52 | +namespace ew_cmn_ns = dpctl::tensor::kernels::elementwise_common; |
| 53 | +namespace logaddexp_ns = dpctl::tensor::kernels::logaddexp; |
| 54 | + |
| 55 | +// Supports the same types table as for logaddexp function in dpctl |
| 56 | +template <typename T1, typename T2> |
| 57 | +using OutputType = logaddexp_ns::LogAddExpOutputType<T1, T2>; |
| 58 | + |
| 59 | +using dpnp::kernels::logaddexp2::Logaddexp2Functor; |
| 60 | + |
| 61 | +template <typename argT1, |
| 62 | + typename argT2, |
| 63 | + typename resT, |
| 64 | + unsigned int vec_sz = 4, |
| 65 | + unsigned int n_vecs = 2, |
| 66 | + bool enable_sg_loadstore = true> |
| 67 | +using ContigFunctor = |
| 68 | + ew_cmn_ns::BinaryContigFunctor<argT1, |
| 69 | + argT2, |
| 70 | + resT, |
| 71 | + Logaddexp2Functor<argT1, argT2, resT>, |
| 72 | + vec_sz, |
| 73 | + n_vecs, |
| 74 | + enable_sg_loadstore>; |
| 75 | + |
| 76 | +template <typename argT1, typename argT2, typename resT, typename IndexerT> |
| 77 | +using StridedFunctor = |
| 78 | + ew_cmn_ns::BinaryStridedFunctor<argT1, |
| 79 | + argT2, |
| 80 | + resT, |
| 81 | + IndexerT, |
| 82 | + Logaddexp2Functor<argT1, argT2, resT>>; |
| 83 | + |
| 84 | +using ew_cmn_ns::binary_contig_impl_fn_ptr_t; |
| 85 | +using ew_cmn_ns::binary_contig_matrix_contig_row_broadcast_impl_fn_ptr_t; |
| 86 | +using ew_cmn_ns::binary_contig_row_contig_matrix_broadcast_impl_fn_ptr_t; |
| 87 | +using ew_cmn_ns::binary_strided_impl_fn_ptr_t; |
| 88 | + |
| 89 | +static binary_contig_impl_fn_ptr_t |
| 90 | + logaddexp2_contig_dispatch_table[td_ns::num_types][td_ns::num_types]; |
| 91 | +static int logaddexp2_output_typeid_table[td_ns::num_types][td_ns::num_types]; |
| 92 | +static binary_strided_impl_fn_ptr_t |
| 93 | + logaddexp2_strided_dispatch_table[td_ns::num_types][td_ns::num_types]; |
| 94 | + |
| 95 | +MACRO_POPULATE_DISPATCH_TABLES(logaddexp2); |
| 96 | +} // namespace impl |
| 97 | + |
| 98 | +void init_logaddexp2(py::module_ m) |
| 99 | +{ |
| 100 | + using arrayT = dpctl::tensor::usm_ndarray; |
| 101 | + using event_vecT = std::vector<sycl::event>; |
| 102 | + { |
| 103 | + impl::populate_logaddexp2_dispatch_tables(); |
| 104 | + using impl::logaddexp2_contig_dispatch_table; |
| 105 | + using impl::logaddexp2_output_typeid_table; |
| 106 | + using impl::logaddexp2_strided_dispatch_table; |
| 107 | + |
| 108 | + auto logaddexp2_pyapi = [&](const arrayT &src1, const arrayT &src2, |
| 109 | + const arrayT &dst, sycl::queue &exec_q, |
| 110 | + const event_vecT &depends = {}) { |
| 111 | + return py_int::py_binary_ufunc( |
| 112 | + src1, src2, dst, exec_q, depends, |
| 113 | + logaddexp2_output_typeid_table, |
| 114 | + logaddexp2_contig_dispatch_table, |
| 115 | + logaddexp2_strided_dispatch_table, |
| 116 | + // no support of C-contig row with broadcasting in OneMKL |
| 117 | + td_ns::NullPtrTable< |
| 118 | + impl:: |
| 119 | + binary_contig_matrix_contig_row_broadcast_impl_fn_ptr_t>{}, |
| 120 | + td_ns::NullPtrTable< |
| 121 | + impl:: |
| 122 | + binary_contig_row_contig_matrix_broadcast_impl_fn_ptr_t>{}); |
| 123 | + }; |
| 124 | + m.def("_logaddexp2", logaddexp2_pyapi, "", py::arg("src1"), |
| 125 | + py::arg("src2"), py::arg("dst"), py::arg("sycl_queue"), |
| 126 | + py::arg("depends") = py::list()); |
| 127 | + |
| 128 | + auto logaddexp2_result_type_pyapi = [&](const py::dtype &dtype1, |
| 129 | + const py::dtype &dtype2) { |
| 130 | + return py_int::py_binary_ufunc_result_type( |
| 131 | + dtype1, dtype2, logaddexp2_output_typeid_table); |
| 132 | + }; |
| 133 | + m.def("_logaddexp2_result_type", logaddexp2_result_type_pyapi); |
| 134 | + } |
| 135 | +} |
| 136 | +} // namespace dpnp::extensions::ufunc |
0 commit comments