DPNP C++ backend kernel library 0.20.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// dpctl tensor headers
34#include "utils/memory_overlap.hpp"
35#include "utils/output_validation.hpp"
36#include "utils/type_dispatch.hpp"
37
38#include "common_helpers.hpp"
39
40namespace dpnp::extensions::lapack::gesvd_utils
41{
42namespace dpctl_td_ns = dpctl::tensor::type_dispatch;
43namespace py = pybind11;
44
45// Converts a given character code (ord) to the corresponding
46// oneapi::mkl::jobsvd enumeration value
47inline oneapi::mkl::jobsvd process_job(const std::int8_t job_val)
48{
49 switch (job_val) {
50 case 'A':
51 return oneapi::mkl::jobsvd::vectors;
52 case 'S':
53 return oneapi::mkl::jobsvd::somevec;
54 case 'O':
55 return oneapi::mkl::jobsvd::vectorsina;
56 case 'N':
57 return oneapi::mkl::jobsvd::novec;
58 default:
59 throw std::invalid_argument("Unknown value for job");
60 }
61}
62
63inline void common_gesvd_checks(sycl::queue &exec_q,
64 const dpctl::tensor::usm_ndarray &a_array,
65 const dpctl::tensor::usm_ndarray &out_s,
66 const dpctl::tensor::usm_ndarray &out_u,
67 const dpctl::tensor::usm_ndarray &out_vt,
68 const std::int8_t jobu_val,
69 const std::int8_t jobvt_val,
70 const int expected_a_u_vt_ndim,
71 const int expected_s_ndim)
72{
73 const int a_array_nd = a_array.get_ndim();
74 const int out_u_array_nd = out_u.get_ndim();
75 const int out_s_array_nd = out_s.get_ndim();
76 const int out_vt_array_nd = out_vt.get_ndim();
77
78 if (a_array_nd != expected_a_u_vt_ndim) {
79 throw py::value_error(
80 "The input array has ndim=" + std::to_string(a_array_nd) +
81 ", but a " + std::to_string(expected_a_u_vt_ndim) +
82 "-dimensional array is expected.");
83 }
84
85 if (out_s_array_nd != expected_s_ndim) {
86 throw py::value_error("The output array of singular values has ndim=" +
87 std::to_string(out_s_array_nd) + ", but a " +
88 std::to_string(expected_s_ndim) +
89 "-dimensional array is expected.");
90 }
91
92 if (jobu_val == 'N' && jobvt_val == 'N') {
93 if (out_u_array_nd != 0) {
94 throw py::value_error(
95 "The output array of the left singular vectors has ndim=" +
96 std::to_string(out_u_array_nd) +
97 ", but it is not used and should have ndim=0.");
98 }
99 if (out_vt_array_nd != 0) {
100 throw py::value_error(
101 "The output array of the right singular vectors has ndim=" +
102 std::to_string(out_vt_array_nd) +
103 ", but it is not used and should have ndim=0.");
104 }
105 }
106 else {
107 if (out_u_array_nd != expected_a_u_vt_ndim) {
108 throw py::value_error(
109 "The output array of the left singular vectors has ndim=" +
110 std::to_string(out_u_array_nd) + ", but a " +
111 std::to_string(expected_a_u_vt_ndim) +
112 "-dimensional array is expected.");
113 }
114 if (out_vt_array_nd != expected_a_u_vt_ndim) {
115 throw py::value_error(
116 "The output array of the right singular vectors has ndim=" +
117 std::to_string(out_vt_array_nd) + ", but a " +
118 std::to_string(expected_a_u_vt_ndim) +
119 "-dimensional array is expected.");
120 }
121 }
122
123 // check compatibility of execution queue and allocation queue
124 if (!dpctl::utils::queues_are_compatible(exec_q,
125 {a_array, out_s, out_u, out_vt}))
126 {
127 throw py::value_error(
128 "Execution queue is not compatible with allocation queues.");
129 }
130
131 auto const &overlap = dpctl::tensor::overlap::MemoryOverlap();
132 if (overlap(a_array, out_s) || overlap(a_array, out_u) ||
133 overlap(a_array, out_vt) || overlap(out_s, out_u) ||
134 overlap(out_s, out_vt) || overlap(out_u, out_vt))
135 {
136 throw py::value_error("Arrays have overlapping segments of memory");
137 }
138
139 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(a_array);
140 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(out_s);
141 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(out_u);
142 dpctl::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 = dpctl_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 dpctl::tensor::usm_ndarray &a_array,
182 const dpctl::tensor::usm_ndarray &out_s,
183 const dpctl::tensor::usm_ndarray &out_u,
184 const dpctl::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