-
Notifications
You must be signed in to change notification settings - Fork 23
implement dpnp.kaiser
#2387
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
Merged
Merged
implement dpnp.kaiser
#2387
Changes from 2 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
c8a2822
implement dpnp.kaiser
15ce51d
update changelog
a3d191c
Update dpnp/dpnp_iface_window.py
vtavana 24c10d9
Merge branch 'master' into impl-kaiser
vtavana 6fe1e2b
pass beta as python object
672d0f8
reuse init_window_dispatch_vectors for kaiser func
b394d2f
reuse window_fn
202b6a0
get rid of unnecessary dependent events
c15015f
Merge branch 'master' into impl-kaiser
vtavana cdd022b
Merge branch 'master' into impl-kaiser
vtavana File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,180 @@ | ||
| //***************************************************************************** | ||
| // Copyright (c) 2025, Intel Corporation | ||
| // All rights reserved. | ||
| // | ||
| // Redistribution and use in source and binary forms, with or without | ||
| // modification, are permitted provided that the following conditions are met: | ||
| // - Redistributions of source code must retain the above copyright notice, | ||
| // this list of conditions and the following disclaimer. | ||
| // - Redistributions in binary form must reproduce the above copyright notice, | ||
| // this list of conditions and the following disclaimer in the documentation | ||
| // and/or other materials provided with the distribution. | ||
| // | ||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | ||
| // THE POSSIBILITY OF SUCH DAMAGE. | ||
| //***************************************************************************** | ||
|
|
||
| #include "kaiser.hpp" | ||
| #include "utils/output_validation.hpp" | ||
| #include "utils/type_dispatch.hpp" | ||
| #include "utils/type_utils.hpp" | ||
| #include <sycl/sycl.hpp> | ||
|
|
||
| /** | ||
| * Version of SYCL DPC++ 2025.1 compiler where an issue with | ||
| * sycl::ext::intel::math::cyl_bessel_i0(x) is fully resolved. | ||
| */ | ||
| #ifndef __SYCL_COMPILER_BESSEL_I0_SUPPORT | ||
| #define __SYCL_COMPILER_BESSEL_I0_SUPPORT 20241208L | ||
| #endif | ||
|
|
||
| #if __SYCL_COMPILER_VERSION >= __SYCL_COMPILER_BESSEL_I0_SUPPORT | ||
| #include <sycl/ext/intel/math.hpp> | ||
| #endif | ||
|
|
||
| #include "../kernels/elementwise_functions/i0.hpp" | ||
|
|
||
| namespace dpnp::extensions::window | ||
| { | ||
| namespace dpctl_td_ns = dpctl::tensor::type_dispatch; | ||
|
|
||
| typedef sycl::event (*kaiser_fn_ptr_t)(sycl::queue &, | ||
| char *, | ||
| const std::size_t, | ||
| const float, | ||
| const std::vector<sycl::event> &); | ||
|
|
||
| static kaiser_fn_ptr_t kaiser_dispatch_vector[dpctl_td_ns::num_types]; | ||
|
|
||
| template <typename T> | ||
| class KaiserFunctor | ||
| { | ||
| private: | ||
| T *data = nullptr; | ||
| const std::size_t N; | ||
| const float beta; | ||
|
|
||
| public: | ||
| KaiserFunctor(T *data, const std::size_t N, const float beta) | ||
| : data(data), N(N), beta(beta) | ||
| { | ||
| } | ||
|
|
||
| void operator()(sycl::id<1> id) const | ||
| { | ||
| #if __SYCL_COMPILER_VERSION >= __SYCL_COMPILER_BESSEL_I0_SUPPORT | ||
| using sycl::ext::intel::math::cyl_bessel_i0; | ||
| #else | ||
| using dpnp::kernels::i0::impl::cyl_bessel_i0; | ||
| #endif | ||
|
|
||
| const auto i = id.get(0); | ||
| const T alpha = (N - 1) / T(2); | ||
| const T tmp = (i - alpha) / alpha; | ||
| data[i] = cyl_bessel_i0(beta * sycl::sqrt(1 - tmp * tmp)) / | ||
| cyl_bessel_i0(beta); | ||
| } | ||
| }; | ||
|
|
||
| template <typename T, template <typename> class Functor> | ||
| sycl::event kaiser_impl(sycl::queue &q, | ||
| char *result, | ||
| const std::size_t nelems, | ||
| const float beta, | ||
| const std::vector<sycl::event> &depends) | ||
| { | ||
| dpctl::tensor::type_utils::validate_type_for_device<T>(q); | ||
|
|
||
| T *res = reinterpret_cast<T *>(result); | ||
|
|
||
| sycl::event kaiser_ev = q.submit([&](sycl::handler &cgh) { | ||
| cgh.depends_on(depends); | ||
|
|
||
| using KaiserKernel = Functor<T>; | ||
| cgh.parallel_for<KaiserKernel>(sycl::range<1>(nelems), | ||
| KaiserKernel(res, nelems, beta)); | ||
| }); | ||
|
|
||
| return kaiser_ev; | ||
| } | ||
|
|
||
| template <typename fnT, typename T> | ||
| struct KaiserFactory | ||
| { | ||
| fnT get() | ||
| { | ||
| if constexpr (std::is_floating_point_v<T>) { | ||
| return kaiser_impl<T, KaiserFunctor>; | ||
| } | ||
| else { | ||
| return nullptr; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| std::pair<sycl::event, sycl::event> | ||
| py_kaiser(sycl::queue &exec_q, | ||
| const float beta, | ||
| const dpctl::tensor::usm_ndarray &result, | ||
| const std::vector<sycl::event> &depends) | ||
| { | ||
| dpctl::tensor::validation::CheckWritable::throw_if_not_writable(result); | ||
|
|
||
| int nd = result.get_ndim(); | ||
| if (nd != 1) { | ||
| throw py::value_error("Array should be 1d"); | ||
| } | ||
|
|
||
| if (!dpctl::utils::queues_are_compatible(exec_q, {result.get_queue()})) { | ||
| throw py::value_error( | ||
| "Execution queue is not compatible with allocation queue."); | ||
| } | ||
|
|
||
| const bool is_result_c_contig = result.is_c_contiguous(); | ||
| if (!is_result_c_contig) { | ||
| throw py::value_error("The result input array is not c-contiguous."); | ||
| } | ||
|
|
||
| size_t nelems = result.get_size(); | ||
| if (nelems == 0) { | ||
| return std::make_pair(sycl::event{}, sycl::event{}); | ||
| } | ||
|
|
||
| int result_typenum = result.get_typenum(); | ||
| auto array_types = dpctl_td_ns::usm_ndarray_types(); | ||
| int result_type_id = array_types.typenum_to_lookup_id(result_typenum); | ||
| auto fn = kaiser_dispatch_vector[result_type_id]; | ||
|
|
||
| if (fn == nullptr) { | ||
| throw std::runtime_error("Type of given array is not supported"); | ||
| } | ||
|
|
||
| char *result_typeless_ptr = result.get_data(); | ||
vtavana marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| sycl::event kaiser_ev = | ||
| fn(exec_q, result_typeless_ptr, nelems, beta, depends); | ||
| sycl::event args_ev = | ||
| dpctl::utils::keep_args_alive(exec_q, {result}, {kaiser_ev}); | ||
|
|
||
| return std::make_pair(args_ev, kaiser_ev); | ||
| } | ||
|
|
||
| void init_kaiser_dispatch_vectors() | ||
| { | ||
| dpctl_td_ns::DispatchVectorBuilder<kaiser_fn_ptr_t, KaiserFactory, | ||
vtavana marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| dpctl_td_ns::num_types> | ||
| contig; | ||
| contig.populate_dispatch_vector(kaiser_dispatch_vector); | ||
|
|
||
| return; | ||
| } | ||
|
|
||
| } // namespace dpnp::extensions::window | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| //***************************************************************************** | ||
| // Copyright (c) 2025, Intel Corporation | ||
| // All rights reserved. | ||
| // | ||
| // Redistribution and use in source and binary forms, with or without | ||
| // modification, are permitted provided that the following conditions are met: | ||
| // - Redistributions of source code must retain the above copyright notice, | ||
| // this list of conditions and the following disclaimer. | ||
| // - Redistributions in binary form must reproduce the above copyright notice, | ||
| // this list of conditions and the following disclaimer in the documentation | ||
| // and/or other materials provided with the distribution. | ||
| // | ||
| // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
| // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
| // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
| // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
| // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
| // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
| // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
| // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
| // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
| // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | ||
| // THE POSSIBILITY OF SUCH DAMAGE. | ||
| //***************************************************************************** | ||
|
|
||
| #pragma once | ||
|
|
||
| #include <dpctl4pybind11.hpp> | ||
| #include <sycl/sycl.hpp> | ||
|
|
||
| namespace dpnp::extensions::window | ||
| { | ||
| extern std::pair<sycl::event, sycl::event> | ||
| py_kaiser(sycl::queue &exec_q, | ||
| const float beta, | ||
| const dpctl::tensor::usm_ndarray &result, | ||
| const std::vector<sycl::event> &depends); | ||
|
|
||
| extern void init_kaiser_dispatch_vectors(void); | ||
|
|
||
| } // namespace dpnp::extensions::window |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.