DPNP C++ backend kernel library 0.21.0dev0
Data Parallel Extension for NumPy*
Loading...
Searching...
No Matches
gesvd_common_utils.hpp
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// 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// - Neither the name of the copyright holder nor the names of its contributors
13// may be used to endorse or promote products derived from this software
14// without specific prior written permission.
15//
16// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
17// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
20// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
21// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
22// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
23// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
24// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
25// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
26// THE POSSIBILITY OF SUCH DAMAGE.
27//*****************************************************************************
28
29#pragma once
30#include <oneapi/mkl.hpp>
31#include <pybind11/pybind11.h>
32
33#include "dpnp4pybind11.hpp"
34
35// dpnp tensor headers
36#include "utils/memory_overlap.hpp"
37#include "utils/output_validation.hpp"
38#include "utils/type_dispatch.hpp"
39
40#include "common_helpers.hpp"
41
42namespace dpnp::extensions::lapack::gesvd_utils
43{
44namespace dpnp_td_ns = dpnp::tensor::type_dispatch;
45namespace py = pybind11;
46
47// Converts a given character code (ord) to the corresponding
48// oneapi::mkl::jobsvd enumeration value
49inline oneapi::mkl::jobsvd process_job(const std::int8_t job_val)
50{
51 switch (job_val) {
52 case 'A':
53 return oneapi::mkl::jobsvd::vectors;
54 case 'S':
55 return oneapi::mkl::jobsvd::somevec;
56 case 'O':
57 return oneapi::mkl::jobsvd::vectorsina;
58 case 'N':
59 return oneapi::mkl::jobsvd::novec;
60 default:
61 throw std::invalid_argument("Unknown value for job");
62 }
63}
64
65inline void common_gesvd_checks(sycl::queue &exec_q,
66 const dpnp::tensor::usm_ndarray &a_array,
67 const dpnp::tensor::usm_ndarray &out_s,
68 const dpnp::tensor::usm_ndarray &out_u,
69 const dpnp::tensor::usm_ndarray &out_vt,
70 const std::int8_t jobu_val,
71 const std::int8_t jobvt_val,
72 const int expected_a_u_vt_ndim,
73 const int expected_s_ndim)
74{
75 const int a_array_nd = a_array.get_ndim();
76 const int out_u_array_nd = out_u.get_ndim();
77 const int out_s_array_nd = out_s.get_ndim();
78 const int out_vt_array_nd = out_vt.get_ndim();
79
80 if (a_array_nd != expected_a_u_vt_ndim) {
81 throw py::value_error(
82 "The input array has ndim=" + std::to_string(a_array_nd) +
83 ", but a " + std::to_string(expected_a_u_vt_ndim) +
84 "-dimensional array is expected.");
85 }
86
87 if (out_s_array_nd != expected_s_ndim) {
88 throw py::value_error("The output array of singular values has ndim=" +
89 std::to_string(out_s_array_nd) + ", but a " +
90 std::to_string(expected_s_ndim) +
91 "-dimensional array is expected.");
92 }
93
94 if (jobu_val == 'N' && jobvt_val == 'N') {
95 if (out_u_array_nd != 0) {
96 throw py::value_error(
97 "The output array of the left singular vectors has ndim=" +
98 std::to_string(out_u_array_nd) +
99 ", but it is not used and should have ndim=0.");
100 }
101 if (out_vt_array_nd != 0) {
102 throw py::value_error(
103 "The output array of the right singular vectors has ndim=" +
104 std::to_string(out_vt_array_nd) +
105 ", but it is not used and should have ndim=0.");
106 }
107 }
108 else {
109 if (out_u_array_nd != expected_a_u_vt_ndim) {
110 throw py::value_error(
111 "The output array of the left singular vectors has ndim=" +
112 std::to_string(out_u_array_nd) + ", but a " +
113 std::to_string(expected_a_u_vt_ndim) +
114 "-dimensional array is expected.");
115 }
116 if (out_vt_array_nd != expected_a_u_vt_ndim) {
117 throw py::value_error(
118 "The output array of the right singular vectors has ndim=" +
119 std::to_string(out_vt_array_nd) + ", but a " +
120 std::to_string(expected_a_u_vt_ndim) +
121 "-dimensional array is expected.");
122 }
123 }
124
125 // check compatibility of execution queue and allocation queue
126 if (!dpnp::utils::queues_are_compatible(exec_q,
127 {a_array, out_s, out_u, out_vt})) {
128 throw py::value_error(
129 "Execution queue is not compatible with allocation queues.");
130 }
131
132 auto const &overlap = dpnp::tensor::overlap::MemoryOverlap();
133 if (overlap(a_array, out_s) || overlap(a_array, out_u) ||
134 overlap(a_array, out_vt) || overlap(out_s, out_u) ||
135 overlap(out_s, out_vt) || overlap(out_u, out_vt)) {
136 throw py::value_error("Arrays have overlapping segments of memory");
137 }
138
139 dpnp::tensor::validation::CheckWritable::throw_if_not_writable(a_array);
140 dpnp::tensor::validation::CheckWritable::throw_if_not_writable(out_s);
141 dpnp::tensor::validation::CheckWritable::throw_if_not_writable(out_u);
142 dpnp::tensor::validation::CheckWritable::throw_if_not_writable(out_vt);
143
144 const bool is_a_array_f_contig = a_array.is_f_contiguous();
145 if (!is_a_array_f_contig) {
146 throw py::value_error("The input array must be F-contiguous");
147 }
148
149 const bool is_out_u_array_f_contig = out_u.is_f_contiguous();
150 const bool is_out_vt_array_f_contig = out_vt.is_f_contiguous();
151
152 if (!is_out_u_array_f_contig || !is_out_vt_array_f_contig) {
153 throw py::value_error("The output arrays of the left and right "
154 "singular vectors must be F-contiguous");
155 }
156
157 const bool is_out_s_array_c_contig = out_s.is_c_contiguous();
158
159 if (!is_out_s_array_c_contig) {
160 throw py::value_error("The output array of singular values "
161 "must be C-contiguous");
162 }
163
164 auto array_types = dpnp_td_ns::usm_ndarray_types();
165 const int a_array_type_id =
166 array_types.typenum_to_lookup_id(a_array.get_typenum());
167 const int out_u_type_id =
168 array_types.typenum_to_lookup_id(out_u.get_typenum());
169 const int out_vt_type_id =
170 array_types.typenum_to_lookup_id(out_vt.get_typenum());
171
172 if (a_array_type_id != out_u_type_id || a_array_type_id != out_vt_type_id) {
173 throw py::type_error(
174 "Input array, output left singular vectors array, "
175 "and outpuy right singular vectors array must have "
176 "the same data type");
177 }
178}
179
180// Check if the shape of input arrays for gesvd has any non-zero dimension.
181inline bool check_zeros_shape_gesvd(const dpnp::tensor::usm_ndarray &a_array,
182 const dpnp::tensor::usm_ndarray &out_s,
183 const dpnp::tensor::usm_ndarray &out_u,
184 const dpnp::tensor::usm_ndarray &out_vt,
185 const std::int8_t jobu_val,
186 const std::int8_t jobvt_val)
187{
188
189 const int a_array_nd = a_array.get_ndim();
190 const int out_u_array_nd = out_u.get_ndim();
191 const int out_s_array_nd = out_s.get_ndim();
192 const int out_vt_array_nd = out_vt.get_ndim();
193
194 const py::ssize_t *a_array_shape = a_array.get_shape_raw();
195 const py::ssize_t *s_out_shape = out_s.get_shape_raw();
196 const py::ssize_t *u_out_shape = out_u.get_shape_raw();
197 const py::ssize_t *vt_out_shape = out_vt.get_shape_raw();
198
199 bool is_zeros_shape = helper::check_zeros_shape(a_array_nd, a_array_shape);
200 if (jobu_val == 'N' && jobvt_val == 'N') {
201 is_zeros_shape = is_zeros_shape || helper::check_zeros_shape(
202 out_vt_array_nd, vt_out_shape);
203 }
204 else {
205 is_zeros_shape =
206 is_zeros_shape ||
207 helper::check_zeros_shape(out_u_array_nd, u_out_shape) ||
208 helper::check_zeros_shape(out_s_array_nd, s_out_shape) ||
209 helper::check_zeros_shape(out_vt_array_nd, vt_out_shape);
210 }
211
212 return is_zeros_shape;
213}
214
215inline void handle_lapack_exc(const std::int64_t scratchpad_size,
216 const oneapi::mkl::lapack::exception &e,
217 std::stringstream &error_msg)
218{
219 const std::int64_t info = e.info();
220 if (info < 0) {
221 error_msg << "Parameter number " << -info << " had an illegal value.";
222 }
223 else if (info == scratchpad_size && e.detail() != 0) {
224 error_msg << "Insufficient scratchpad size. Required size is at least "
225 << e.detail();
226 }
227 else if (info > 0) {
228 error_msg << "The algorithm computing SVD failed to converge; " << info
229 << " off-diagonal elements of an intermediate "
230 << "bidiagonal form did not converge to zero.\n";
231 }
232 else {
233 error_msg
234 << "Unexpected MKL exception caught during gesv() call:\nreason: "
235 << e.what() << "\ninfo: " << e.info();
236 }
237}
238} // namespace dpnp::extensions::lapack::gesvd_utils