DPNP C++ backend kernel library 0.20.0dev4
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 throw py::value_error(
127 "Execution queue is not compatible with allocation queues.");
128 }
129
130 auto const &overlap = dpctl::tensor::overlap::MemoryOverlap();
131 if (overlap(a_array, out_s) || overlap(a_array, out_u) ||
132 overlap(a_array, out_vt) || overlap(out_s, out_u) ||
133 overlap(out_s, out_vt) || overlap(out_u, out_vt)) {
134 throw py::value_error("Arrays have overlapping segments of memory");
135 }
136
137 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(a_array);
138 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(out_s);
139 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(out_u);
140 dpctl::tensor::validation::CheckWritable::throw_if_not_writable(out_vt);
141
142 const bool is_a_array_f_contig = a_array.is_f_contiguous();
143 if (!is_a_array_f_contig) {
144 throw py::value_error("The input array must be F-contiguous");
145 }
146
147 const bool is_out_u_array_f_contig = out_u.is_f_contiguous();
148 const bool is_out_vt_array_f_contig = out_vt.is_f_contiguous();
149
150 if (!is_out_u_array_f_contig || !is_out_vt_array_f_contig) {
151 throw py::value_error("The output arrays of the left and right "
152 "singular vectors must be F-contiguous");
153 }
154
155 const bool is_out_s_array_c_contig = out_s.is_c_contiguous();
156
157 if (!is_out_s_array_c_contig) {
158 throw py::value_error("The output array of singular values "
159 "must be C-contiguous");
160 }
161
162 auto array_types = dpctl_td_ns::usm_ndarray_types();
163 const int a_array_type_id =
164 array_types.typenum_to_lookup_id(a_array.get_typenum());
165 const int out_u_type_id =
166 array_types.typenum_to_lookup_id(out_u.get_typenum());
167 const int out_vt_type_id =
168 array_types.typenum_to_lookup_id(out_vt.get_typenum());
169
170 if (a_array_type_id != out_u_type_id || a_array_type_id != out_vt_type_id) {
171 throw py::type_error(
172 "Input array, output left singular vectors array, "
173 "and outpuy right singular vectors array must have "
174 "the same data type");
175 }
176}
177
178// Check if the shape of input arrays for gesvd has any non-zero dimension.
179inline bool check_zeros_shape_gesvd(const dpctl::tensor::usm_ndarray &a_array,
180 const dpctl::tensor::usm_ndarray &out_s,
181 const dpctl::tensor::usm_ndarray &out_u,
182 const dpctl::tensor::usm_ndarray &out_vt,
183 const std::int8_t jobu_val,
184 const std::int8_t jobvt_val)
185{
186
187 const int a_array_nd = a_array.get_ndim();
188 const int out_u_array_nd = out_u.get_ndim();
189 const int out_s_array_nd = out_s.get_ndim();
190 const int out_vt_array_nd = out_vt.get_ndim();
191
192 const py::ssize_t *a_array_shape = a_array.get_shape_raw();
193 const py::ssize_t *s_out_shape = out_s.get_shape_raw();
194 const py::ssize_t *u_out_shape = out_u.get_shape_raw();
195 const py::ssize_t *vt_out_shape = out_vt.get_shape_raw();
196
197 bool is_zeros_shape = helper::check_zeros_shape(a_array_nd, a_array_shape);
198 if (jobu_val == 'N' && jobvt_val == 'N') {
199 is_zeros_shape = is_zeros_shape || helper::check_zeros_shape(
200 out_vt_array_nd, vt_out_shape);
201 }
202 else {
203 is_zeros_shape =
204 is_zeros_shape ||
205 helper::check_zeros_shape(out_u_array_nd, u_out_shape) ||
206 helper::check_zeros_shape(out_s_array_nd, s_out_shape) ||
207 helper::check_zeros_shape(out_vt_array_nd, vt_out_shape);
208 }
209
210 return is_zeros_shape;
211}
212
213inline void handle_lapack_exc(const std::int64_t scratchpad_size,
214 const oneapi::mkl::lapack::exception &e,
215 std::stringstream &error_msg)
216{
217 const std::int64_t info = e.info();
218 if (info < 0) {
219 error_msg << "Parameter number " << -info << " had an illegal value.";
220 }
221 else if (info == scratchpad_size && e.detail() != 0) {
222 error_msg << "Insufficient scratchpad size. Required size is at least "
223 << e.detail();
224 }
225 else if (info > 0) {
226 error_msg << "The algorithm computing SVD failed to converge; " << info
227 << " off-diagonal elements of an intermediate "
228 << "bidiagonal form did not converge to zero.\n";
229 }
230 else {
231 error_msg
232 << "Unexpected MKL exception caught during gesv() call:\nreason: "
233 << e.what() << "\ninfo: " << e.info();
234 }
235}
236} // namespace dpnp::extensions::lapack::gesvd_utils