-
Notifications
You must be signed in to change notification settings - Fork 22
Update dpnp.linalg.qr() function #1673
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
43 commits
Select commit
Hold shift + click to select a range
c2601fd
Impl dpnp.linalg.qr for 2d array
vlad-perevezentsev 8d644aa
Update condition and random files in cupy/testing
vlad-perevezentsev 3a6cb9f
Rename cupy/testing/condition.py to .../_condition.py
vlad-perevezentsev 872a713
Add cupy tests for dpnp.linalg.qr
vlad-perevezentsev 890bffe
Add batch implementation of dpnp.linalg.qr
vlad-perevezentsev 47e309e
Uncomment cupy tests for 3d and 4d cases
vlad-perevezentsev 7e9750a
Remove an old impl of dpnp_qr
vlad-perevezentsev 71277b0
Update test_qr in test_sycl_queue
vlad-perevezentsev 0fe3346
Add test_qr in test_usm_type
vlad-perevezentsev 93534d7
Merge master into impl_qr_new
vlad-perevezentsev 3b57f0c
Rename condition to _condition in test_solve.py
vlad-perevezentsev dc6dbcb
Use _real_type for _orgqr
vlad-perevezentsev a2349fe
Use _real_type for _orgqr_batch
vlad-perevezentsev 4e993a9
Update dpnp tests for dpnp.linalg.qr
vlad-perevezentsev 95b8420
Pass scratchpad_size to the error message test
vlad-perevezentsev 3a50768
Add additional checks
vlad-perevezentsev 09b6673
Extend error handler for mkl batch funcs
vlad-perevezentsev 8b4528f
Use mkl_lapack namespace
vlad-perevezentsev ea7cb47
Add ungqr mkl extension to support complex dtype
vlad-perevezentsev 245ee80
Update tau array size check for orgqr
vlad-perevezentsev c4c29bd
Add ungqr_batch mkl extension to support complex dtype
vlad-perevezentsev f097cd1
Add arrays type check
vlad-perevezentsev a9dfe15
Fix test_det_singular_matrix
vlad-perevezentsev d3395af
Expand tests for dpnp.linalg.qr with complex types
vlad-perevezentsev ce46590
Update examples
vlad-perevezentsev 5c173ca
Remove astype for output arrays
vlad-perevezentsev f494bfd
Use empty_like instead of empty
vlad-perevezentsev fc44044
Minor update dpnp_qr/dpnp_qr_batch logic
vlad-perevezentsev 1f7800d
Describe the returned arrays
vlad-perevezentsev a9783e6
Use ht_list_ev with dpctl.SyclEvent.wait_for
vlad-perevezentsev 2e9950a
Add a comment for transpose
vlad-perevezentsev ea8cd4d
Add _triu_inplace func
vlad-perevezentsev 45ec802
Merge master into impl_qr_new
vlad-perevezentsev 1cd6c77
Use _triu_inplace for complete and reduced mode
vlad-perevezentsev d389ddb
Fix validation check
vlad-perevezentsev 71fec50
Use copy_usm for a_t array overwritten by geqrf/geqrf_batch
vlad-perevezentsev 771310d
Merge master into impl_qr_new
vlad-perevezentsev 120cf43
Address remarks
vlad-perevezentsev 30188e4
Skip cupy tests on cpu
vlad-perevezentsev d519b1a
Skip dpnp tests on cpu
vlad-perevezentsev c8786db
Remove TODOs for svd tests
vlad-perevezentsev e0ee2f2
Address remarks
vlad-perevezentsev 2705b4e
Merge branch 'master' into impl_qr_new
antonwolfy File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,262 @@ | ||
//***************************************************************************** | ||
// Copyright (c) 2024, Intel Corporation | ||
// All rights reserved. | ||
// | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions are met: | ||
// - Redistributions of source code must retain the above copyright notice, | ||
// this list of conditions and the following disclaimer. | ||
// - Redistributions in binary form must reproduce the above copyright notice, | ||
// this list of conditions and the following disclaimer in the documentation | ||
// and/or other materials provided with the distribution. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | ||
// THE POSSIBILITY OF SUCH DAMAGE. | ||
//***************************************************************************** | ||
|
||
#include <pybind11/pybind11.h> | ||
|
||
// dpctl tensor headers | ||
#include "utils/memory_overlap.hpp" | ||
#include "utils/type_utils.hpp" | ||
|
||
#include "geqrf.hpp" | ||
#include "types_matrix.hpp" | ||
|
||
#include "dpnp_utils.hpp" | ||
|
||
namespace dpnp | ||
{ | ||
namespace backend | ||
{ | ||
namespace ext | ||
{ | ||
namespace lapack | ||
{ | ||
namespace mkl_lapack = oneapi::mkl::lapack; | ||
namespace py = pybind11; | ||
namespace type_utils = dpctl::tensor::type_utils; | ||
|
||
typedef sycl::event (*geqrf_impl_fn_ptr_t)(sycl::queue, | ||
const std::int64_t, | ||
const std::int64_t, | ||
char *, | ||
std::int64_t, | ||
char *, | ||
std::vector<sycl::event> &, | ||
const std::vector<sycl::event> &); | ||
|
||
static geqrf_impl_fn_ptr_t geqrf_dispatch_vector[dpctl_td_ns::num_types]; | ||
|
||
template <typename T> | ||
static sycl::event geqrf_impl(sycl::queue exec_q, | ||
const std::int64_t m, | ||
const std::int64_t n, | ||
char *in_a, | ||
std::int64_t lda, | ||
char *in_tau, | ||
std::vector<sycl::event> &host_task_events, | ||
const std::vector<sycl::event> &depends) | ||
{ | ||
type_utils::validate_type_for_device<T>(exec_q); | ||
|
||
T *a = reinterpret_cast<T *>(in_a); | ||
T *tau = reinterpret_cast<T *>(in_tau); | ||
|
||
const std::int64_t scratchpad_size = | ||
mkl_lapack::geqrf_scratchpad_size<T>(exec_q, m, n, lda); | ||
T *scratchpad = nullptr; | ||
|
||
std::stringstream error_msg; | ||
std::int64_t info = 0; | ||
bool is_exception_caught = false; | ||
|
||
sycl::event geqrf_event; | ||
try { | ||
scratchpad = sycl::malloc_device<T>(scratchpad_size, exec_q); | ||
|
||
geqrf_event = mkl_lapack::geqrf( | ||
exec_q, | ||
m, // The number of rows in the matrix; (0 ≤ m). | ||
n, // The number of columns in the matrix; (0 ≤ n). | ||
a, // Pointer to the m-by-n matrix. | ||
lda, // The leading dimension of `a`; (1 ≤ m). | ||
tau, // Pointer to the array of scalar factors of the | ||
// elementary reflectors. | ||
scratchpad, // Pointer to scratchpad memory to be used by MKL | ||
// routine for storing intermediate results. | ||
scratchpad_size, depends); | ||
} catch (mkl_lapack::exception const &e) { | ||
is_exception_caught = true; | ||
info = e.info(); | ||
|
||
if (info < 0) { | ||
error_msg << "Parameter number " << -info | ||
<< " had an illegal value."; | ||
} | ||
else if (info == scratchpad_size && e.detail() != 0) { | ||
error_msg | ||
<< "Insufficient scratchpad size. Required size is at least " | ||
<< e.detail() << ", but current size is " << scratchpad_size | ||
<< "."; | ||
} | ||
else { | ||
error_msg << "Unexpected MKL exception caught during geqrf() " | ||
"call:\nreason: " | ||
<< e.what() << "\ninfo: " << info; | ||
} | ||
} catch (sycl::exception const &e) { | ||
is_exception_caught = true; | ||
error_msg << "Unexpected SYCL exception caught during geqrf() call:\n" | ||
<< e.what(); | ||
} | ||
|
||
if (is_exception_caught) // an unexpected error occurs | ||
{ | ||
if (scratchpad != nullptr) { | ||
sycl::free(scratchpad, exec_q); | ||
} | ||
throw std::runtime_error(error_msg.str()); | ||
} | ||
|
||
sycl::event clean_up_event = exec_q.submit([&](sycl::handler &cgh) { | ||
cgh.depends_on(geqrf_event); | ||
auto ctx = exec_q.get_context(); | ||
cgh.host_task([ctx, scratchpad]() { sycl::free(scratchpad, ctx); }); | ||
}); | ||
host_task_events.push_back(clean_up_event); | ||
|
||
return geqrf_event; | ||
} | ||
|
||
std::pair<sycl::event, sycl::event> | ||
geqrf(sycl::queue q, | ||
dpctl::tensor::usm_ndarray a_array, | ||
dpctl::tensor::usm_ndarray tau_array, | ||
const std::vector<sycl::event> &depends) | ||
{ | ||
const int a_array_nd = a_array.get_ndim(); | ||
const int tau_array_nd = tau_array.get_ndim(); | ||
|
||
if (a_array_nd != 2) { | ||
throw py::value_error( | ||
"The input array has ndim=" + std::to_string(a_array_nd) + | ||
", but a 2-dimensional array is expected."); | ||
} | ||
|
||
if (tau_array_nd != 1) { | ||
throw py::value_error("The array of Householder scalars has ndim=" + | ||
std::to_string(tau_array_nd) + | ||
", but a 1-dimensional array is expected."); | ||
} | ||
|
||
// check compatibility of execution queue and allocation queue | ||
if (!dpctl::utils::queues_are_compatible(q, {a_array, tau_array})) { | ||
throw py::value_error( | ||
"Execution queue is not compatible with allocation queues"); | ||
} | ||
|
||
auto const &overlap = dpctl::tensor::overlap::MemoryOverlap(); | ||
if (overlap(a_array, tau_array)) { | ||
throw py::value_error( | ||
"The input array and the array of Householder scalars " | ||
"are overlapping segments of memory"); | ||
} | ||
|
||
bool is_a_array_c_contig = a_array.is_c_contiguous(); | ||
if (!is_a_array_c_contig) { | ||
throw py::value_error("The input array " | ||
"must be C-contiguous"); | ||
} | ||
|
||
bool is_tau_array_c_contig = tau_array.is_c_contiguous(); | ||
bool is_tau_array_f_contig = tau_array.is_f_contiguous(); | ||
|
||
if (!is_tau_array_c_contig || !is_tau_array_f_contig) { | ||
throw py::value_error("The array of Householder scalars " | ||
"must be contiguous"); | ||
} | ||
|
||
vlad-perevezentsev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
auto array_types = dpctl_td_ns::usm_ndarray_types(); | ||
int a_array_type_id = | ||
array_types.typenum_to_lookup_id(a_array.get_typenum()); | ||
int tau_array_type_id = | ||
array_types.typenum_to_lookup_id(tau_array.get_typenum()); | ||
|
||
if (a_array_type_id != tau_array_type_id) { | ||
throw py::value_error( | ||
"The types of the input array and " | ||
"the array of Householder scalars are mismatched"); | ||
} | ||
|
||
vlad-perevezentsev marked this conversation as resolved.
Show resolved
Hide resolved
|
||
geqrf_impl_fn_ptr_t geqrf_fn = geqrf_dispatch_vector[a_array_type_id]; | ||
if (geqrf_fn == nullptr) { | ||
throw py::value_error( | ||
"No geqrf implementation defined for the provided type " | ||
"of the input matrix."); | ||
} | ||
|
||
char *a_array_data = a_array.get_data(); | ||
char *tau_array_data = tau_array.get_data(); | ||
|
||
const py::ssize_t *a_array_shape = a_array.get_shape_raw(); | ||
|
||
// The input array is transponded | ||
// Change the order of getting m, n | ||
const std::int64_t m = a_array_shape[1]; | ||
const std::int64_t n = a_array_shape[0]; | ||
const std::int64_t lda = std::max<size_t>(1UL, m); | ||
|
||
const size_t tau_array_size = tau_array.get_size(); | ||
const size_t min_m_n = std::max<size_t>(1UL, std::min<size_t>(m, n)); | ||
|
||
if (tau_array_size != min_m_n) { | ||
throw py::value_error("The array of Householder scalars has size=" + | ||
std::to_string(tau_array_size) + ", but a size=" + | ||
std::to_string(min_m_n) + " array is expected."); | ||
} | ||
|
||
std::vector<sycl::event> host_task_events; | ||
sycl::event geqrf_ev = geqrf_fn(q, m, n, a_array_data, lda, tau_array_data, | ||
host_task_events, depends); | ||
|
||
sycl::event args_ev = dpctl::utils::keep_args_alive(q, {a_array, tau_array}, | ||
host_task_events); | ||
|
||
return std::make_pair(args_ev, geqrf_ev); | ||
} | ||
|
||
template <typename fnT, typename T> | ||
struct GeqrfContigFactory | ||
{ | ||
fnT get() | ||
{ | ||
if constexpr (types::GeqrfTypePairSupportFactory<T>::is_defined) { | ||
return geqrf_impl<T>; | ||
} | ||
else { | ||
return nullptr; | ||
} | ||
} | ||
}; | ||
|
||
void init_geqrf_dispatch_vector(void) | ||
{ | ||
dpctl_td_ns::DispatchVectorBuilder<geqrf_impl_fn_ptr_t, GeqrfContigFactory, | ||
dpctl_td_ns::num_types> | ||
contig; | ||
contig.populate_dispatch_vector(geqrf_dispatch_vector); | ||
} | ||
} // namespace lapack | ||
} // namespace ext | ||
} // namespace backend | ||
} // namespace dpnp |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
//***************************************************************************** | ||
// Copyright (c) 2024, Intel Corporation | ||
// All rights reserved. | ||
// | ||
// Redistribution and use in source and binary forms, with or without | ||
// modification, are permitted provided that the following conditions are met: | ||
// - Redistributions of source code must retain the above copyright notice, | ||
// this list of conditions and the following disclaimer. | ||
// - Redistributions in binary form must reproduce the above copyright notice, | ||
// this list of conditions and the following disclaimer in the documentation | ||
// and/or other materials provided with the distribution. | ||
// | ||
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" | ||
// AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE | ||
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE | ||
// ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE | ||
// LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR | ||
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF | ||
// SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS | ||
// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN | ||
// CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) | ||
// ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF | ||
// THE POSSIBILITY OF SUCH DAMAGE. | ||
//***************************************************************************** | ||
|
||
#pragma once | ||
|
||
#include <CL/sycl.hpp> | ||
#include <oneapi/mkl.hpp> | ||
|
||
#include <dpctl4pybind11.hpp> | ||
|
||
namespace dpnp | ||
{ | ||
namespace backend | ||
{ | ||
namespace ext | ||
{ | ||
namespace lapack | ||
{ | ||
extern std::pair<sycl::event, sycl::event> | ||
geqrf(sycl::queue exec_q, | ||
dpctl::tensor::usm_ndarray a_array, | ||
dpctl::tensor::usm_ndarray tau_array, | ||
const std::vector<sycl::event> &depends = {}); | ||
|
||
extern std::pair<sycl::event, sycl::event> | ||
geqrf_batch(sycl::queue exec_q, | ||
dpctl::tensor::usm_ndarray a_array, | ||
dpctl::tensor::usm_ndarray tau_array, | ||
std::int64_t m, | ||
std::int64_t n, | ||
std::int64_t stride_a, | ||
std::int64_t stride_tau, | ||
std::int64_t batch_size, | ||
const std::vector<sycl::event> &depends = {}); | ||
|
||
extern void init_geqrf_batch_dispatch_vector(void); | ||
extern void init_geqrf_dispatch_vector(void); | ||
} // namespace lapack | ||
} // namespace ext | ||
} // namespace backend | ||
} // namespace dpnp |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.