Skip to content

Commit af312f7

Browse files
committed
implement dpnp.hanning
1 parent 2700d92 commit af312f7

File tree

8 files changed

+199
-5
lines changed

8 files changed

+199
-5
lines changed

doc/known_words.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,7 @@ Mises
5757
multinomial
5858
multivalued
5959
namespace
60+
namespaces
6061
namedtuple
6162
NaN
6263
NaT
Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
//*****************************************************************************
2+
// Copyright (c) 2025, Intel Corporation
3+
// All rights reserved.
4+
//
5+
// Redistribution and use in source and binary forms, with or without
6+
// modification, 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+
#pragma once
27+
28+
#include "common.hpp"
29+
#include <sycl/sycl.hpp>
30+
31+
namespace dpnp::extensions::window::kernels
32+
{
33+
34+
template <typename T>
35+
class HanningFunctor
36+
{
37+
private:
38+
T *data = nullptr;
39+
const std::size_t N;
40+
41+
public:
42+
HanningFunctor(T *data, const std::size_t N) : data(data), N(N) {}
43+
44+
void operator()(sycl::id<1> id) const
45+
{
46+
const auto i = id.get(0);
47+
48+
data[i] = T(0.5) - T(0.5) * sycl::cospi(T(2) * i / (N - 1));
49+
}
50+
};
51+
52+
template <typename fnT, typename T>
53+
struct HanningFactory
54+
{
55+
fnT get()
56+
{
57+
if constexpr (std::is_floating_point_v<T>) {
58+
return window_impl<T, HanningFunctor>;
59+
}
60+
else {
61+
return nullptr;
62+
}
63+
}
64+
};
65+
66+
} // namespace dpnp::extensions::window::kernels

dpnp/backend/extensions/window/window_py.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
#include "common.hpp"
3434
#include "hamming.hpp"
35+
#include "hanning.hpp"
3536

3637
namespace window_ns = dpnp::extensions::window;
3738
namespace py = pybind11;
@@ -40,6 +41,7 @@ using window_ns::window_fn_ptr_t;
4041
namespace dpctl_td_ns = dpctl::tensor::type_dispatch;
4142

4243
static window_fn_ptr_t hamming_dispatch_vector[dpctl_td_ns::num_types];
44+
static window_fn_ptr_t hanning_dispatch_vector[dpctl_td_ns::num_types];
4345

4446
PYBIND11_MODULE(_window_impl, m)
4547
{
@@ -60,4 +62,19 @@ PYBIND11_MODULE(_window_impl, m)
6062
py::arg("sycl_queue"), py::arg("result"),
6163
py::arg("depends") = py::list());
6264
}
65+
66+
{
67+
window_ns::init_window_dispatch_vectors<
68+
window_ns::kernels::HanningFactory>(hanning_dispatch_vector);
69+
70+
auto hanning_pyapi = [&](sycl::queue &exec_q, const arrayT &result,
71+
const event_vecT &depends = {}) {
72+
return window_ns::py_window(exec_q, result, depends,
73+
hanning_dispatch_vector);
74+
};
75+
76+
m.def("_hanning", hanning_pyapi, "Call hanning kernel",
77+
py::arg("sycl_queue"), py::arg("result"),
78+
py::arg("depends") = py::list());
79+
}
6380
}

dpnp/dpnp_iface_window.py

Lines changed: 109 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@
4545
import dpnp
4646
import dpnp.backend.extensions.window._window_impl as wi
4747

48-
__all__ = ["hamming"]
48+
__all__ = ["hamming", "hanning"]
4949

5050

5151
def hamming(M, device=None, usm_type=None, sycl_queue=None):
@@ -154,3 +154,111 @@ def hamming(M, device=None, usm_type=None, sycl_queue=None):
154154
_manager.add_event_pair(ht_ev, win_ev)
155155

156156
return result
157+
158+
159+
def hanning(M, device=None, usm_type=None, sycl_queue=None):
160+
r"""
161+
Return the Hanning window.
162+
163+
The Hanning window is a taper formed by using a weighted cosine.
164+
165+
For full documentation refer to :obj:`numpy.hanning`.
166+
167+
Parameters
168+
----------
169+
M : int
170+
Number of points in the output window. If zero or less, an empty array
171+
is returned.
172+
device : {None, string, SyclDevice, SyclQueue, Device}, optional
173+
An array API concept of device where the output array is created.
174+
`device` can be ``None``, a oneAPI filter selector string, an instance
175+
of :class:`dpctl.SyclDevice` corresponding to a non-partitioned SYCL
176+
device, an instance of :class:`dpctl.SyclQueue`, or a
177+
:class:`dpctl.tensor.Device` object returned by
178+
:attr:`dpnp.ndarray.device`.
179+
180+
Default: ``None``.
181+
usm_type : {None, "device", "shared", "host"}, optional
182+
The type of SYCL USM allocation for the output array.
183+
184+
Default: ``None``.
185+
sycl_queue : {None, SyclQueue}, optional
186+
A SYCL queue to use for output array allocation and copying. The
187+
`sycl_queue` can be passed as ``None`` (the default), which means
188+
to get the SYCL queue from `device` keyword if present or to use
189+
a default queue.
190+
191+
Default: ``None``.
192+
193+
Returns
194+
-------
195+
out : dpnp.ndarray of shape (M,)
196+
The window, with the maximum value normalized to one (the value one
197+
appears only if the number of samples is odd).
198+
199+
See Also
200+
--------
201+
:obj:`dpnp.bartlett` : Return the Bartlett window.
202+
:obj:`dpnp.blackman` : Return the Blackman window.
203+
:obj:`dpnp.hamming` : Return the Hamming window.
204+
:obj:`dpnp.kaiser` : Return the Kaiser window.
205+
206+
Notes
207+
-----
208+
The Hanning window is defined as
209+
210+
.. math:: w(n) = 0.5 - 0.5\cos\left(\frac{2\pi{n}}{M-1}\right)
211+
\qquad 0 \leq n \leq M-1
212+
213+
Examples
214+
--------
215+
>>> import dpnp as np
216+
>>> np.hanning(12)
217+
array([0. , 0.07937323, 0.29229249, 0.57115742, 0.82743037,
218+
0.97974649, 0.97974649, 0.82743037, 0.57115742, 0.29229249,
219+
0.07937323, 0. ])
220+
221+
Creating the output array on a different device or with a
222+
specified usm_type:
223+
224+
>>> x = np.hanning(4) # default case
225+
>>> x, x.device, x.usm_type
226+
(array([0. , 0.75, 0.75, 0. ]), Device(level_zero:gpu:0), 'device')
227+
228+
>>> y = np.hanning(4, device="cpu")
229+
>>> y, y.device, y.usm_type
230+
(array([0. , 0.75, 0.75, 0. ]), Device(opencl:cpu:0), 'device')
231+
232+
>>> z = np.hanning(4, usm_type="host")
233+
>>> z, z.device, z.usm_type
234+
(array([0. , 0.75, 0.75, 0. ]), Device(level_zero:gpu:0), 'host')
235+
236+
"""
237+
238+
try:
239+
M = int(M)
240+
except Exception as e:
241+
raise TypeError("M must be an integer") from e
242+
243+
cfd_kwarg = {
244+
"device": device,
245+
"usm_type": usm_type,
246+
"sycl_queue": sycl_queue,
247+
}
248+
249+
if M < 1:
250+
return dpnp.empty(0, **cfd_kwarg)
251+
if M == 1:
252+
return dpnp.ones(1, **cfd_kwarg)
253+
254+
result = dpnp.empty(int(M), **cfd_kwarg)
255+
exec_q = result.sycl_queue
256+
_manager = dpu.SequentialOrderManager[exec_q]
257+
258+
ht_ev, win_ev = wi._hanning(
259+
exec_q, dpnp.get_usm_ndarray(result), depends=_manager.submitted_events
260+
)
261+
262+
_manager.add_event_pair(ht_ev, win_ev)
263+
264+
return result

dpnp/tests/test_sycl_queue.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,7 @@ def assert_sycl_queue_equal(result, expected):
6868
pytest.param("full", [(2, 2)], {"fill_value": 5}),
6969
pytest.param("geomspace", [1, 4, 8], {}),
7070
pytest.param("hamming", [10], {}),
71+
pytest.param("hanning", [10], {}),
7172
pytest.param("identity", [4], {}),
7273
pytest.param("linspace", [0, 4, 8], {}),
7374
pytest.param("logspace", [0, 4, 8], {}),

dpnp/tests/test_usm_type.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,7 @@ def test_array_creation_from_array(func, args, usm_type_x, usm_type_y):
192192
pytest.param("full", [(2, 2)], {"fill_value": 5}),
193193
pytest.param("geomspace", [1, 4, 8], {}),
194194
pytest.param("hamming", [10], {}),
195+
pytest.param("hanning", [10], {}),
195196
pytest.param("identity", [4], {}),
196197
pytest.param("linspace", [0, 4, 8], {}),
197198
pytest.param("logspace", [0, 4, 8], {}),

dpnp/tests/test_window.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
from .helper import assert_dtype_allclose
88

99

10-
@pytest.mark.parametrize("func", ["hamming"])
10+
@pytest.mark.parametrize("func", ["hamming", "hanning"])
1111
@pytest.mark.parametrize(
1212
"M",
1313
[
@@ -32,7 +32,7 @@ def test_window(func, M):
3232
assert_dtype_allclose(result, expected)
3333

3434

35-
@pytest.mark.parametrize("func", ["hamming"])
35+
@pytest.mark.parametrize("func", ["hamming", "hanning"])
3636
@pytest.mark.parametrize(
3737
"M",
3838
[

dpnp/tests/third_party/cupy/math_tests/test_window.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,8 @@
1010
*testing.product(
1111
{
1212
"m": [0, 1, -1, 1024],
13-
# TODO: add ["bartlett", "blackman", "hanning"] when supported
14-
"name": ["hamming"],
13+
# TODO: add ["bartlett", "blackman"] when supported
14+
"name": ["hamming", "hanning"],
1515
}
1616
)
1717
)

0 commit comments

Comments
 (0)