Skip to content

Commit 2bb1e8b

Browse files
authored
implement dpnp.hamming (#2341)
In this PR, `dpnp.hamming` is implemented.
1 parent d57ac14 commit 2bb1e8b

26 files changed

+668
-56
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
88

99
### Added
1010

11+
* Added implementation of `dpnp.hamming` [#2341](https://github.com/IntelPython/dpnp/pull/2341)
12+
1113
### Changed
1214

1315
### Fixed

doc/reference/routines.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,3 +27,4 @@ These functions cover a subset of
2727
set
2828
sort
2929
statistics
30+
window

doc/reference/window.rst

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
Window functions
2+
================
3+
4+
.. https://numpy.org/doc/stable/reference/routines.window.html
5+
6+
Various windows
7+
---------------
8+
9+
.. autosummary::
10+
:toctree: generated/
11+
:nosignatures:
12+
13+
dpnp.bartlett
14+
dpnp.blackman
15+
dpnp.hamming
16+
dpnp.hanning
17+
dpnp.kaiser

dpnp/CMakeLists.txt

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -58,11 +58,13 @@ endfunction()
5858
add_subdirectory(backend)
5959
add_subdirectory(backend/extensions/blas)
6060
add_subdirectory(backend/extensions/fft)
61+
add_subdirectory(backend/extensions/indexing)
6162
add_subdirectory(backend/extensions/lapack)
62-
add_subdirectory(backend/extensions/vm)
63-
add_subdirectory(backend/extensions/ufunc)
6463
add_subdirectory(backend/extensions/statistics)
65-
add_subdirectory(backend/extensions/indexing)
64+
add_subdirectory(backend/extensions/ufunc)
65+
add_subdirectory(backend/extensions/vm)
66+
add_subdirectory(backend/extensions/window)
67+
6668

6769
add_subdirectory(dpnp_algo)
6870
add_subdirectory(dpnp_utils)

dpnp/backend/extensions/indexing/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# *****************************************************************************
2-
# Copyright (c) 2016-2024, Intel Corporation
2+
# Copyright (c) 2025, Intel Corporation
33
# All rights reserved.
44
#
55
# Redistribution and use in source and binary forms, with or without

dpnp/backend/extensions/indexing/choose.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//*****************************************************************************
2-
// Copyright (c) 2024, Intel Corporation
2+
// Copyright (c) 2025, Intel Corporation
33
// All rights reserved.
44
//
55
// Redistribution and use in source and binary forms, with or without

dpnp/backend/extensions/indexing/choose.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//*****************************************************************************
2-
// Copyright (c) 2024, Intel Corporation
2+
// Copyright (c) 2025, Intel Corporation
33
// All rights reserved.
44
//
55
// Redistribution and use in source and binary forms, with or without

dpnp/backend/extensions/indexing/choose_kernel.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//*****************************************************************************
2-
// Copyright (c) 2024, Intel Corporation
2+
// Copyright (c) 2025, Intel Corporation
33
// All rights reserved.
44
//
55
// Redistribution and use in source and binary forms, with or without

dpnp/backend/extensions/indexing/indexing_py.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
//*****************************************************************************
2-
// Copyright (c) 2024, Intel Corporation
2+
// Copyright (c) 2025, Intel Corporation
33
// All rights reserved.
44
//
55
// Redistribution and use in source and binary forms, with or without
@@ -23,12 +23,11 @@
2323
// THE POSSIBILITY OF SUCH DAMAGE.
2424
//*****************************************************************************
2525
//
26-
// This file defines functions of dpnp.backend._lapack_impl extensions
26+
// This file defines functions of dpnp.backend._indexing_impl extensions
2727
//
2828
//*****************************************************************************
2929

3030
#include <pybind11/pybind11.h>
31-
#include <pybind11/stl.h>
3231

3332
#include "choose.hpp"
3433

dpnp/backend/extensions/statistics/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# *****************************************************************************
2-
# Copyright (c) 2016-2025, Intel Corporation
2+
# Copyright (c) 2024-2025, Intel Corporation
33
# All rights reserved.
44
#
55
# Redistribution and use in source and binary forms, with or without

dpnp/backend/extensions/statistics/statistics_py.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,11 @@
2323
// THE POSSIBILITY OF SUCH DAMAGE.
2424
//*****************************************************************************
2525
//
26-
// This file defines functions of dpnp.backend._lapack_impl extensions
26+
// This file defines functions of dpnp.backend._statistics_impl extensions
2727
//
2828
//*****************************************************************************
2929

3030
#include <pybind11/pybind11.h>
31-
#include <pybind11/stl.h>
3231

3332
#include "bincount.hpp"
3433
#include "histogram.hpp"

dpnp/backend/extensions/ufunc/ufunc_py.cpp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@
2727

2828
#include "elementwise_functions/common.hpp"
2929

30-
namespace py = pybind11;
3130
namespace ufunc_ns = dpnp::extensions::ufunc;
3231

3332
PYBIND11_MODULE(_ufunc_impl, m)
Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
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+
27+
set(python_module_name _window_impl)
28+
set(_module_src
29+
${CMAKE_CURRENT_SOURCE_DIR}/hamming.cpp
30+
${CMAKE_CURRENT_SOURCE_DIR}/window_py.cpp
31+
)
32+
33+
pybind11_add_module(${python_module_name} MODULE ${_module_src})
34+
add_sycl_to_target(TARGET ${python_module_name} SOURCES ${_module_src})
35+
36+
if(_dpnp_sycl_targets)
37+
# make fat binary
38+
target_compile_options(
39+
${python_module_name}
40+
PRIVATE
41+
-fsycl-targets=${_dpnp_sycl_targets}
42+
)
43+
target_link_options(
44+
${python_module_name}
45+
PRIVATE
46+
-fsycl-targets=${_dpnp_sycl_targets}
47+
)
48+
endif()
49+
50+
if (WIN32)
51+
if (${CMAKE_VERSION} VERSION_LESS "3.27")
52+
# this is a work-around for target_link_options inserting option after -link option, cause
53+
# linker to ignore it.
54+
set(CMAKE_CXX_LINK_FLAGS "${CMAKE_CXX_LINK_FLAGS} -fsycl-device-code-split=per_kernel")
55+
endif()
56+
endif()
57+
58+
set_target_properties(${python_module_name} PROPERTIES CMAKE_POSITION_INDEPENDENT_CODE ON)
59+
60+
target_include_directories(${python_module_name} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../include)
61+
target_include_directories(${python_module_name} PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/../../src)
62+
63+
target_include_directories(${python_module_name} PUBLIC ${Dpctl_INCLUDE_DIR})
64+
target_include_directories(${python_module_name} PUBLIC ${Dpctl_TENSOR_INCLUDE_DIR})
65+
66+
if (WIN32)
67+
target_compile_options(${python_module_name} PRIVATE
68+
/clang:-fno-approx-func
69+
/clang:-fno-finite-math-only
70+
)
71+
else()
72+
target_compile_options(${python_module_name} PRIVATE
73+
-fno-approx-func
74+
-fno-finite-math-only
75+
)
76+
endif()
77+
78+
target_link_options(${python_module_name} PUBLIC -fsycl-device-code-split=per_kernel)
79+
80+
if (DPNP_GENERATE_COVERAGE)
81+
target_link_options(${python_module_name} PRIVATE -fprofile-instr-generate -fcoverage-mapping)
82+
endif()
83+
84+
if (DPNP_WITH_REDIST)
85+
set_target_properties(${python_module_name} PROPERTIES INSTALL_RPATH "$ORIGIN/../../../../../../")
86+
endif()
87+
88+
install(TARGETS ${python_module_name}
89+
DESTINATION "dpnp/backend/extensions/window"
90+
)
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
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+
#include <pybind11/pybind11.h>
27+
#include <pybind11/stl.h>
28+
#include <sycl/sycl.hpp>
29+
30+
#include "dpctl4pybind11.hpp"
31+
#include "hamming_kernel.hpp"
32+
#include "utils/output_validation.hpp"
33+
#include "utils/type_dispatch.hpp"
34+
35+
namespace dpnp::extensions::window
36+
{
37+
38+
namespace dpctl_td_ns = dpctl::tensor::type_dispatch;
39+
40+
static kernels::hamming_fn_ptr_t hamming_dispatch_table[dpctl_td_ns::num_types];
41+
42+
namespace py = pybind11;
43+
44+
std::pair<sycl::event, sycl::event>
45+
py_hamming(sycl::queue &exec_q,
46+
const dpctl::tensor::usm_ndarray &result,
47+
const std::vector<sycl::event> &depends)
48+
{
49+
dpctl::tensor::validation::CheckWritable::throw_if_not_writable(result);
50+
51+
int nd = result.get_ndim();
52+
if (nd != 1) {
53+
throw py::value_error("Array should be 1d");
54+
}
55+
56+
if (!dpctl::utils::queues_are_compatible(exec_q, {result.get_queue()})) {
57+
throw py::value_error(
58+
"Execution queue is not compatible with allocation queue.");
59+
}
60+
61+
const bool is_result_c_contig = result.is_c_contiguous();
62+
if (!is_result_c_contig) {
63+
throw py::value_error("The result input array is not c-contiguous.");
64+
}
65+
66+
size_t nelems = result.get_size();
67+
if (nelems == 0) {
68+
return std::make_pair(sycl::event{}, sycl::event{});
69+
}
70+
71+
int result_typenum = result.get_typenum();
72+
auto array_types = dpctl_td_ns::usm_ndarray_types();
73+
int result_type_id = array_types.typenum_to_lookup_id(result_typenum);
74+
auto fn = hamming_dispatch_table[result_type_id];
75+
76+
if (fn == nullptr) {
77+
throw std::runtime_error("Type of given array is not supported");
78+
}
79+
80+
char *result_typeless_ptr = result.get_data();
81+
sycl::event hamming_ev = fn(exec_q, result_typeless_ptr, nelems, depends);
82+
sycl::event args_ev =
83+
dpctl::utils::keep_args_alive(exec_q, {result}, {hamming_ev});
84+
85+
return std::make_pair(args_ev, hamming_ev);
86+
}
87+
88+
template <typename fnT, typename T>
89+
struct HammingFactory
90+
{
91+
fnT get()
92+
{
93+
if constexpr (std::is_floating_point_v<T>) {
94+
return kernels::hamming_impl<T>;
95+
}
96+
else {
97+
return nullptr;
98+
}
99+
}
100+
};
101+
102+
void init_hamming_dispatch_tables(void)
103+
{
104+
using kernels::hamming_fn_ptr_t;
105+
106+
dpctl_td_ns::DispatchVectorBuilder<hamming_fn_ptr_t, HammingFactory,
107+
dpctl_td_ns::num_types>
108+
contig;
109+
contig.populate_dispatch_vector(hamming_dispatch_table);
110+
111+
return;
112+
}
113+
114+
void init_hamming(py::module_ m)
115+
{
116+
dpnp::extensions::window::init_hamming_dispatch_tables();
117+
118+
m.def("_hamming", &py_hamming, "Call hamming kernel", py::arg("sycl_queue"),
119+
py::arg("result"), py::arg("depends") = py::list());
120+
121+
return;
122+
}
123+
124+
} // namespace dpnp::extensions::window
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
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 <pybind11/pybind11.h>
29+
30+
namespace py = pybind11;
31+
32+
namespace dpnp::extensions::window
33+
{
34+
void init_hamming(py::module_ m);
35+
}

0 commit comments

Comments
 (0)