30#include <oneapi/mkl.hpp>
31#include <pybind11/pybind11.h>
34#include "utils/memory_overlap.hpp"
35#include "utils/output_validation.hpp"
36#include "utils/type_dispatch.hpp"
38#include "common_helpers.hpp"
40namespace dpnp::extensions::lapack::gesvd_utils
42namespace dpctl_td_ns = dpctl::tensor::type_dispatch;
43namespace py = pybind11;
47inline oneapi::mkl::jobsvd process_job(
const std::int8_t job_val)
51 return oneapi::mkl::jobsvd::vectors;
53 return oneapi::mkl::jobsvd::somevec;
55 return oneapi::mkl::jobsvd::vectorsina;
57 return oneapi::mkl::jobsvd::novec;
59 throw std::invalid_argument(
"Unknown value for job");
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)
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();
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.");
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.");
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.");
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.");
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.");
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.");
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.");
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");
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);
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");
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();
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");
155 const bool is_out_s_array_c_contig = out_s.is_c_contiguous();
157 if (!is_out_s_array_c_contig) {
158 throw py::value_error(
"The output array of singular values "
159 "must be C-contiguous");
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());
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");
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)
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();
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();
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);
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);
210 return is_zeros_shape;
213inline void handle_lapack_exc(
const std::int64_t scratchpad_size,
214 const oneapi::mkl::lapack::exception &e,
215 std::stringstream &error_msg)
217 const std::int64_t info = e.info();
219 error_msg <<
"Parameter number " << -info <<
" had an illegal value.";
221 else if (info == scratchpad_size && e.detail() != 0) {
222 error_msg <<
"Insufficient scratchpad size. Required size is at least "
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";
232 <<
"Unexpected MKL exception caught during gesv() call:\nreason: "
233 << e.what() <<
"\ninfo: " << e.info();