8000 Updated linalg.lstsq with NumPy compatible kwarg rcond by IvanYashchuk · Pull Request #54723 · pytorch/pytorch · GitHub
[go: up one dir, main page]

Skip to content

Updated linalg.lstsq with NumPy compatible kwarg rcond #54723

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

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
977e767
Updated linalg.lstsq with NumPy compatible kwarg rcond
IvanYashchuk Mar 25, 2021
b81b6c0
Update on "Updated linalg.lstsq with NumPy compatible kwarg rcond"
IvanYashchuk Mar 25, 2021
8d92c4a
Update on "Updated linalg.lstsq with NumPy compatible kwarg rcond"
IvanYashchuk Mar 29, 2021
9b94388
Update on "Updated linalg.lstsq with NumPy compatible kwarg rcond"
IvanYashchuk Mar 29, 2021
499d51d
Update on "Updated linalg.lstsq with NumPy compatible kwarg rcond"
IvanYashchuk Mar 29, 2021
8314e44
Update on "Updated linalg.lstsq with NumPy compatible kwarg rcond"
IvanYashchuk Mar 30, 2021
1da034e
Update on "Updated linalg.lstsq with NumPy compatible kwarg rcond"
IvanYashchuk Mar 30, 2021
4ba7c58
Update on "Updated linalg.lstsq with NumPy compatible kwarg rcond"
IvanYashchuk Apr 8, 2021
2ab1e1b
Update on "Updated linalg.lstsq with NumPy compatible kwarg rcond"
IvanYashchuk Apr 8, 2021
1f8eddd
Update on "Updated linalg.lstsq with NumPy compatible kwarg rcond"
IvanYashchuk Apr 8, 2021
14ffa59
Update on "Updated linalg.lstsq with NumPy compatible kwarg rcond"
IvanYashchuk Apr 8, 2021
42ce965
Update on "Updated linalg.lstsq with NumPy compatible kwarg rcond"
IvanYashchuk Apr 9, 2021
94bd964
Update on "Updated linalg.lstsq with NumPy compatible kwarg rcond"
IvanYashchuk Apr 9, 2021
e2bc32f
Update on "Updated linalg.lstsq with NumPy compatible kwarg rcond"
IvanYashchuk Apr 13, 2021
19a99a2
Update on "Updated linalg.lstsq with NumPy compatible kwarg rcond"
IvanYashchuk Apr 14, 2021
349299e
Update on "Updated linalg.lstsq with NumPy compatible kwarg rcond"
IvanYashchuk Apr 20, 2021
5100023
Update on "Updated linalg.lstsq with NumPy compatible kwarg rcond"
IvanYashchuk Apr 20, 2021
648ba48
Update on "Updated linalg.lstsq with NumPy compatible kwarg rcond"
IvanYashchuk Apr 20, 2021
b14ad54
Update on "Updated linalg.lstsq with NumPy compatible kwarg rcond"
IvanYashchuk Apr 26, 2021
78f9f97
Update on "Updated linalg.lstsq with NumPy compatible kwarg rcond"
IvanYashchuk Apr 27, 2021
84c10bd
Update on "Updated linalg.lstsq with NumPy compatible kwarg rcond"
IvanYashchuk Apr 28, 2021
843c2b1
Update on "Updated linalg.lstsq with NumPy compatible kwarg rcond"
IvanYashchuk Apr 29, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions aten/src/ATen/native/BatchLinearAlgebra.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2881,7 +2881,10 @@ struct LapackLstsqHelper {
}
return *this;
}
self_type& set_rcond(double cond) { this->rcond = static_cast<value_t>(cond); return *this; }
self_type& set_rcond(double rcond) {
this->rcond = static_cast<value_t>(rcond);
return *this;
}
self_type& set_rank(Tensor& rank) {
// only `?gels` is not rank-revealing
if (LapackLstsqDriverType::Gels != driver_type) {
Expand Down Expand Up @@ -3000,7 +3003,7 @@ struct LapackLstsqDriverTypeHash {
#endif

Tensor& _lstsq_helper_cpu(
Tensor& b, Tensor& rank, Tensor& singular_values, Tensor& infos, const Tensor& a, double cond, std::string driver_name) {
Tensor& b, Tensor& rank, Tensor& singular_values, Tensor& infos, const Tensor& a, double rcond, std::string driver_name) {
#ifndef USE_LAPACK
TORCH_CHECK(false, "torch.linalg.lstsq: LAPACK library not found in compilation");
#else
Expand Down Expand Up @@ -3039,7 +3042,7 @@ Tensor& _lstsq_helper_cpu(
.set_b(b)
.set_ldb(std::max<int64_t>(1, std::max(m, n)))
.set_jpvt()
.set_rcond(cond)
.set_rcond(rcond)
.set_rank(rank)
.set_s(singular_values)
.set_infos(infos)
Expand Down Expand Up @@ -3331,10 +3334,9 @@ std::tuple<Tensor&, Tensor&, Tensor&, Tensor&> linalg_lstsq_out(
std::string driver_name = get_default_lstsq_driver(driver, input);

// set default rcond value
// TODO: Change this to match non-legacy NumPy behaviour
double rcond_value = rcond.has_value() && (rcond.value() > 0)
double rcond_value = rcond.has_value()
? rcond.value()
: _get_epsilon(c10::toValueType(input.scalar_type()));
: _get_epsilon(c10::toValueType(input.scalar_type())) * std::max<int64_t>(input.size(-2), input.size(-1));

auto infos = at::zeros({std::max<int64_t>(1, batchCount(input))}, input.options().dtype(kInt));

Expand Down
2 changes: 1 addition & 1 deletion aten/src/ATen/native/cuda/BatchLinearAlgebra.cu
Original file line number Diff line number Diff line change
Expand Up @@ -2735,7 +2735,7 @@ Tensor _lu_solve_helper_cuda(const Tensor& self, const Tensor& LU_data, const Te
// ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ lstsq ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Tensor& _lstsq_helper_cuda(
Tensor& b, Tensor& rank, Tensor& singular_values, Tensor& infos, const Tensor& a, double cond, std::string driver_name) {
Tensor& b, Tensor& rank, Tensor& singular_values, Tensor& infos, const Tensor& a, double rcond, std::string driver_name) {
#ifndef USE_MAGMA
TORCH_CHECK(false, "torch.linalg.lstsq: MAGMA library not found in "
"compilation. Please rebuild with MAGMA.");
Expand Down
6 changes: 3 additions & 3 deletions aten/src/ATen/native/native_functions.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -8658,19 +8658,19 @@
- func: det(Tensor self) -> Tensor
variants: function, method

- func: linalg_lstsq(Tensor self, Tensor b, float? cond=None, *, str? driver=None) -> (Tensor solution, Tensor residuals, Tensor rank, Tensor singular_values)
- func: linalg_lstsq(Tensor self, Tensor b, float? rcond=None, *, str? driver=None) -> (Tensor solution, Tensor residuals, Tensor rank, Tensor singular_values)
python_module: linalg
variants: function
dispatch:
CompositeExplicitAutograd: linalg_lstsq

- func: linalg_lstsq.out(Tensor self, Tensor b, float? cond=None, *, str? driver=None, Tensor(a!) solution, Tensor(b!) residuals, Tensor(c!) rank, Tensor(d!) singular_values) -> (Tensor(a!) solution, Tensor(b!) residuals, Tensor(c!) rank, Tensor(d!) singular_values)
- func: linalg_lstsq.out(Tensor self, Tensor b, float? rcond=None, *, str? driver=None, Tensor(a!) solution, Tensor(b!) residuals, Tensor(c!) rank, Tensor(d!) singular_values) -> (Tensor(a!) solution, Tensor(b!) residuals, Tensor(c!) rank, Tensor(d!) singular_values)
python_module: linalg
variants: function
dispatch:
CPU, CUDA: linalg_lstsq_out

- func: _lstsq_helper_(Tensor(a!) self, Tensor(b!) rank, Tensor(c!) singular_values, Tensor(d!) infos, Tensor a, float cond, str driver_name) -> Tensor(a!)
- func: _lstsq_helper_(Tensor(a!) self, Tensor(b!) rank, Tensor(c!) singular_values, Tensor(d!) infos, Tensor a, float rcond, str driver_name) -> Tensor(a!)
variants: function
dispatch:
CPU: _lstsq_helper_cpu
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@
("aten::irfft", datetime.date(2021, 1, 31)),
("aten::rfft", datetime.date(2021, 1, 31)),
("aten::_lstsq_helper", datetime.date(9999, 1, 1)),
("aten::linalg_lstsq", datetime.date(2021, 5, 1)),
("aten::_svd_helper", datetime.date(2021, 1, 31)),
("aten::_syevd_helper", datetime.date(9999, 1, 1)),
("aten::_cudnn_rnn_flatten_weight", datetime.date(2020, 12, 31)),
Expand Down
71 changes: 38 additions & 33 deletions test/test_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,7 @@ def test_linalg_lstsq(self, device, dtype):
else:
drivers = ('gels', None)

def check_correctness(a, b, sol):
def check_solution_correctness(a, b, sol):
sol2 = a.pinverse() @ b
self.assertEqual(sol, sol2, atol=1e-5, rtol=1e-5)

Expand Down Expand Up @@ -196,29 +196,22 @@ def select_if_not_empty(t, i):
self.assertEqual(res.singular_values.shape, (0, ))

def check_correctness_scipy(a, b, res, driver, cond):
if TEST_SCIPY and driver not in (None, 'gels'):
# SciPy provides 3 driver options: gelsd, gelss, gelsy
if TEST_SCIPY and driver in ('gelsd', 'gelss', 'gelsy'):
import scipy.linalg

def scipy_ref(a, b):
return scipy.linalg.lstsq(a, b, lapack_driver=driver, cond=cond)
check_correctness_ref(a, b, res, scipy_ref, driver=driver)

def check_correctness_numpy(a, b, res, driver, cond):
if driver in ('gelsd', 'gelss'):
import numpy.linalg
def check_correctness_numpy(a, b, res, driver, rcond):
# NumPy uses only gelsd routine
if driver == 'gelsd':

def numpy_ref(a, b):
return numpy.linalg.lstsq(a, b, rcond=-1 if cond is None else cond)
return np.linalg.lstsq(a, b, rcond=rcond)
check_correctness_ref(a, b, res, numpy_ref)

def check_ranks(a, ranks, cond=1e-7):
ranks2 = torch.matrix_rank(a, tol=cond)
self.assertEqual(ranks, ranks2)

def check_singular_values(a, sv):
sv2 = a.svd()[1]
self.assertEqual(sv, sv2)
Comment on lines -214 to -220
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Rank and singular values are already checked in check_correctness_scipy and check_correctness_numpy.


ms = [2 ** i for i in range(5)]
m_ge_n_sizes = [(m, m // 2) for m in ms] + [(m, m) for m in ms]
# cases m < n are only supported on CPU
Expand All @@ -229,32 +222,44 @@ def check_singular_values(a, sv):
# that is why we use `cond=1.0`, the mean to cut roughly half of all
# the singular values and compare whether torch.linalg.lstsq agrees with
# SciPy and NumPy.
cond = (None, 1.0)
# if rcond is True then set value for it based on the used algorithm
# rcond == -1 or any other negative value forces LAPACK to use machine precision tolerance
rconds = (None, True, -1)

for batch, matrix_size, driver, rcond in itertools.product(batches, matrix_sizes, drivers, rconds):
# keep the rcond value if it is None or -1, set the driver specific value if it is True
if rcond and rcond != -1:
if driver in ('gelss', 'gelsd'):
# SVD based algorithm; set to zero roughly half of all the singular values
rcond = 1.0
else:
# driver == 'gelsy'
# QR based algorithm; setting the value too high might lead to non-unique solutions and flaky tests
rcond = 1e-4

# specifying rcond value has no effect for gels driver so no need to run the tests again
if driver == 'gels' and rcond is not None:
continue

for batch, matrix_size, driver, cond in itertools.product(batches, matrix_sizes, drivers, cond):
shape = batch + matrix_size
a = random_well_conditioned_matrix(*shape, dtype=dtype, device=device)
b = torch.rand(*shape, dtype=dtype, device=device)

cond = 1e-7
m = a.size(-2)
n = a.size(-1)
res = torch.linalg.lstsq(a, b, cond=cond, driver=driver)
sol = res.solution.narrow(-2, 0, n)

check_correctness_scipy(a, b, res, driver, cond)
check_correctness_numpy(a, b, res, driver, cond)

check_correctness(a, b, sol)
if self.device_type == 'cpu' and driver != 'gels':
# rank-revealing drivers are only available for the CPU.
# `gels` is not rank-revealing and is only for full
# rank inputs.
check_ranks(a, res.rank, cond)
if self.device_type == 'cpu' and driver in ('gelsd', 'gelss'):
# SVD-based drivers are only available for the CPU.
# These are only `gelsd` and `gelss`.
check_singular_values(a, res.singular_values)
res = torch.linalg.lstsq(a, b, rcond=rcond, driver=driver)
sol = res.solution

# Only checks gelsd, gelss, gelsy drivers
check_correctness_scipy(a, b, res, driver, rcond)

# Only checks gelsd driver
check_correctness_numpy(a, b, res, driver, rcond)

# gels driver is not checked by comparing to NumPy or SciPy implementation
# because NumPy and SciPy do not implement this driver
if driver == 'gels' and rcond is None:
check_solution_correctness(a, b, sol)

@skipCUDAIfNoMagma
@skipCPUIfNoLapack
Expand Down
2 changes: 1 addition & 1 deletion tools/autograd/derivatives.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -704,7 +704,7 @@
self: not_implemented("lstsq")
A: not_implemented("lstsq")

- name: linalg_lstsq(Tensor self, Tensor b, float? cond=None, *, str? driver=None) -> (Tensor solution, Tensor residuals, Tensor rank, Tensor singular_values)
- name: linalg_lstsq(Tensor self, Tensor b, float? rcond=None, *, str? driver=None) -> (Tensor solution, Tensor residuals, Tensor rank, Tensor singular_values)
self: not_implemented("linalg_lstsq")
b: not_implemented("linalg_lstsq")
output_differentiability: [True, True]
Expand Down
10 changes: 5 additions & 5 deletions torch/linalg/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -648,7 +648,7 @@
""")

lstsq = _add_docstr(_linalg.linalg_lstsq, r"""
torch.linalg.lstsq(A, B, cond=None, *, driver=None) -> (Tensor, Tensor, Tensor, Tensor)
torch.linalg.lstsq(A, B, rcond=None, *, driver=None) -> (Tensor, Tensor, Tensor, Tensor)

Computes a solution to the least squares problem of a system of linear equations.

Expand Down Expand Up @@ -714,16 +714,16 @@
computations separately.

.. warning::
The default value of :attr:`cond` may change in the future.
The default value of :attr:`rcond` may change in a future PyTorch release.
It is therefore recommended to use a fixed value to avoid potential
breaking changes.

Args:
A (Tensor): lhs tensor of shape `(*, m, n)` where `*` is zero or more batch dimensions.
B (Tensor): rhs tensor of shape `(*, m, k)` where `*` is zero or more batch dimensions.
cond (float, optional): used to determine the effective rank of :attr:`A`.
If :attr:`cond`\ `= None`, :attr:`cond` is set to the machine
precision of the dtype of :attr:`A`. Default: `None`.
rcond (float, optional): used to determine the effective rank of :attr:`A`.
If :attr:`rcond`\ `= None`, :attr:`rcond` is set to t 4AD4 he machine
precision of the dtype of :attr:`A` times `max(m, n)`. Default: `None`.

Keyword args:
driver (str, optional): name of the LAPACK/MAGMA method to be used.
Expand Down
0