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}))
127 throw py::value_error(
128 "Execution queue is not compatible with allocation queues.");
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))
136 throw py::value_error(
"Arrays have overlapping segments of memory");
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);
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");
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();
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");
157 const bool is_out_s_array_c_contig = out_s.is_c_contiguous();
159 if (!is_out_s_array_c_contig) {
160 throw py::value_error(
"The output array of singular values "
161 "must be C-contiguous");
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());
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");
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)
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();
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();
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);
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);
212 return is_zeros_shape;
215inline void handle_lapack_exc(
const std::int64_t scratchpad_size,
216 const oneapi::mkl::lapack::exception &e,
217 std::stringstream &error_msg)
219 const std::int64_t info = e.info();
221 error_msg <<
"Parameter number " << -info <<
" had an illegal value.";
223 else if (info == scratchpad_size && e.detail() != 0) {
224 error_msg <<
"Insufficient scratchpad size. Required size is at least "
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";
234 <<
"Unexpected MKL exception caught during gesv() call:\nreason: "
235 << e.what() <<
"\ninfo: " << e.info();