2022-02-03 11:16:18 -08:00
|
|
|
# Copyright 2022 Google LLC
|
|
|
|
#
|
|
|
|
# Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
# you may not use this file except in compliance with the License.
|
|
|
|
# You may obtain a copy of the License at
|
|
|
|
#
|
|
|
|
# https://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
#
|
|
|
|
# Unless required by applicable law or agreed to in writing, software
|
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
# See the License for the specific language governing permissions and
|
|
|
|
# limitations under the License
|
|
|
|
|
2022-04-29 16:42:08 -07:00
|
|
|
"""A JIT-compatible library for QDWH-based singular value decomposition.
|
2022-02-03 11:16:18 -08:00
|
|
|
|
|
|
|
QDWH is short for QR-based dynamically weighted Halley iteration. The Halley
|
|
|
|
iteration implemented through QR decmopositions is numerically stable and does
|
|
|
|
not require solving a linear system involving the iteration matrix or
|
|
|
|
computing its inversion. This is desirable for multicore and heterogeneous
|
|
|
|
computing systems.
|
|
|
|
|
|
|
|
References:
|
|
|
|
Nakatsukasa, Yuji, and Nicholas J. Higham.
|
|
|
|
"Stable and efficient spectral divide and conquer algorithms for the symmetric
|
|
|
|
eigenvalue decomposition and the SVD." SIAM Journal on Scientific Computing 35,
|
|
|
|
no. 3 (2013): A1325-A1349.
|
|
|
|
https://epubs.siam.org/doi/abs/10.1137/120876605
|
|
|
|
|
|
|
|
Nakatsukasa, Yuji, Zhaojun Bai, and François Gygi.
|
|
|
|
"Optimizing Halley's iteration for computing the matrix polar decomposition."
|
|
|
|
SIAM Journal on Matrix Analysis and Applications 31, no. 5 (2010): 2700-2720.
|
|
|
|
https://epubs.siam.org/doi/abs/10.1137/090774999
|
|
|
|
"""
|
|
|
|
|
|
|
|
import functools
|
|
|
|
|
2022-04-29 16:42:08 -07:00
|
|
|
from typing import Any, Sequence, Union
|
2022-02-03 11:16:18 -08:00
|
|
|
import jax
|
|
|
|
from jax import core
|
|
|
|
from jax import lax
|
|
|
|
import jax.numpy as jnp
|
|
|
|
|
|
|
|
|
2022-04-19 11:28:04 -07:00
|
|
|
@functools.partial(jax.jit, static_argnums=(1, 2, 3))
|
2022-04-29 16:42:08 -07:00
|
|
|
def _svd(a: Any,
|
2022-04-20 12:31:30 -07:00
|
|
|
hermitian: bool,
|
2022-04-19 11:28:04 -07:00
|
|
|
compute_uv: bool,
|
2022-04-29 16:42:08 -07:00
|
|
|
max_iterations: int) -> Union[Any, Sequence[Any]]:
|
2022-02-03 11:16:18 -08:00
|
|
|
"""Singular value decomposition for m x n matrix and m >= n.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
a: A matrix of shape `m x n` with `m >= n`.
|
2022-04-20 12:31:30 -07:00
|
|
|
hermitian: True if `a` is Hermitian.
|
2022-04-19 11:28:04 -07:00
|
|
|
compute_uv: Whether to compute also `u` and `v` in addition to `s`.
|
2022-02-03 11:16:18 -08:00
|
|
|
max_iterations: The predefined maximum number of iterations of QDWH.
|
|
|
|
|
|
|
|
Returns:
|
|
|
|
A 3-tuple (`u`, `s`, `v`), where `u` is a unitary matrix of shape `m x n`,
|
|
|
|
`s` is vector of length `n` containing the singular values in the descending
|
|
|
|
order, `v` is a unitary matrix of shape `n x n`, and
|
2022-04-19 11:28:04 -07:00
|
|
|
`a = (u * s) @ v.T.conj()`. For `compute_uv=False`, only `s` is returned.
|
2022-02-03 11:16:18 -08:00
|
|
|
"""
|
|
|
|
|
2022-04-20 12:31:30 -07:00
|
|
|
u, h, _, _ = lax.linalg.qdwh(a, hermitian, max_iterations)
|
2022-02-03 11:16:18 -08:00
|
|
|
|
2022-04-19 11:28:04 -07:00
|
|
|
# TODO: Uses `eigvals_only=True` if `compute_uv=False`.
|
2022-02-03 11:16:18 -08:00
|
|
|
v, s = lax.linalg.eigh(h)
|
|
|
|
|
|
|
|
# Flips the singular values in descending order.
|
|
|
|
s_out = jnp.flip(s)
|
|
|
|
|
2022-04-19 11:28:04 -07:00
|
|
|
if not compute_uv:
|
|
|
|
return s_out
|
|
|
|
|
2022-02-03 11:16:18 -08:00
|
|
|
# Reorders eigenvectors.
|
|
|
|
v_out = jnp.fliplr(v)
|
|
|
|
|
|
|
|
u_out = u @ v_out
|
|
|
|
|
|
|
|
# Makes correction if computed `u` from qdwh is not unitary.
|
|
|
|
# Section 5.5 of Nakatsukasa, Yuji, and Nicholas J. Higham. "Stable and
|
|
|
|
# efficient spectral divide and conquer algorithms for the symmetric
|
|
|
|
# eigenvalue decomposition and the SVD." SIAM Journal on Scientific Computing
|
|
|
|
# 35, no. 3 (2013): A1325-A1349.
|
|
|
|
def correct_rank_deficiency(u_out):
|
|
|
|
u_out, r = lax.linalg.qr(u_out, full_matrices=False)
|
|
|
|
u_out = u_out @ jnp.diag(lax.sign(jnp.diag(r)))
|
|
|
|
return u_out
|
|
|
|
|
|
|
|
eps = jnp.finfo(a.dtype).eps
|
|
|
|
u_out = lax.cond(s[0] < a.shape[1] * eps * s_out[0],
|
|
|
|
correct_rank_deficiency,
|
|
|
|
lambda u_out: u_out,
|
|
|
|
operand=(u_out))
|
|
|
|
|
|
|
|
return (u_out, s_out, v_out)
|
|
|
|
|
|
|
|
|
2022-04-20 12:31:30 -07:00
|
|
|
@functools.partial(jax.jit, static_argnums=(1, 2, 3, 4))
|
2022-04-29 16:42:08 -07:00
|
|
|
def svd(a: Any,
|
2022-04-20 12:31:30 -07:00
|
|
|
full_matrices: bool,
|
2022-04-19 11:28:04 -07:00
|
|
|
compute_uv: bool = True,
|
2022-04-20 12:31:30 -07:00
|
|
|
hermitian: bool = False,
|
2022-04-29 16:42:08 -07:00
|
|
|
max_iterations: int = 10) -> Union[Any, Sequence[Any]]:
|
2022-02-03 11:16:18 -08:00
|
|
|
"""Singular value decomposition.
|
|
|
|
|
|
|
|
Args:
|
|
|
|
a: A matrix of shape `m x n`.
|
2022-04-20 12:31:30 -07:00
|
|
|
full_matrices: If True, `u` and `vh` have the shapes `m x m` and `n x n`,
|
|
|
|
respectively. If False, the shapes are `m x k` and `k x n`, respectively,
|
|
|
|
where `k = min(m, n)`.
|
2022-04-19 11:28:04 -07:00
|
|
|
compute_uv: Whether to compute also `u` and `v` in addition to `s`.
|
2022-04-20 12:31:30 -07:00
|
|
|
hermitian: True if `a` is Hermitian.
|
2022-02-03 11:16:18 -08:00
|
|
|
max_iterations: The predefined maximum number of iterations of QDWH.
|
|
|
|
|
|
|
|
Returns:
|
2022-04-20 12:31:30 -07:00
|
|
|
A 3-tuple (`u`, `s`, `vh`), where `u` and `vh` are unitary matrices,
|
|
|
|
`s` is vector of length `k` containing the singular values in the
|
|
|
|
non-increasing order, and `k = min(m, n)`. The shapes of `u` and `vh`
|
|
|
|
depend on the value of `full_matrices`. For `compute_uv=False`,
|
|
|
|
only `s` is returned.
|
2022-02-03 11:16:18 -08:00
|
|
|
"""
|
|
|
|
|
2022-04-20 12:31:30 -07:00
|
|
|
hermitian = core.concrete_or_error(
|
|
|
|
bool, hermitian, 'The `hermitian` argument must be statically '
|
2022-02-03 11:16:18 -08:00
|
|
|
'specified to use `qdwh` within JAX transformations.')
|
|
|
|
|
|
|
|
max_iterations = core.concrete_or_error(
|
|
|
|
int, max_iterations, 'The `max_iterations` argument must be statically '
|
|
|
|
'specified to use `qdwh` within JAX transformations.')
|
|
|
|
|
|
|
|
m, n = a.shape
|
|
|
|
|
|
|
|
is_flip = False
|
|
|
|
if m < n:
|
|
|
|
a = a.T.conj()
|
|
|
|
m, n = a.shape
|
|
|
|
is_flip = True
|
|
|
|
|
|
|
|
reduce_to_square = False
|
2022-04-20 12:31:30 -07:00
|
|
|
if full_matrices:
|
|
|
|
q_full, a_full = lax.linalg.qr(a, full_matrices=True)
|
|
|
|
q = q_full[:, :n]
|
|
|
|
u_out_null = q_full[:, n:]
|
|
|
|
a = a_full[:n, :]
|
2022-02-03 11:16:18 -08:00
|
|
|
reduce_to_square = True
|
2022-04-20 12:31:30 -07:00
|
|
|
else:
|
|
|
|
# The constant `1.15` comes from Yuji Nakatsukasa's implementation
|
|
|
|
# https://www.mathworks.com/matlabcentral/fileexchange/36830-symmetric-eigenvalue-decomposition-and-the-svd?s_tid=FX_rc3_behav
|
|
|
|
if m > 1.15 * n:
|
|
|
|
q, a = lax.linalg.qr(a, full_matrices=False)
|
|
|
|
reduce_to_square = True
|
2022-02-03 11:16:18 -08:00
|
|
|
|
2022-04-19 11:28:04 -07:00
|
|
|
if not compute_uv:
|
2022-04-20 12:31:30 -07:00
|
|
|
return _svd(a, hermitian, compute_uv, max_iterations)
|
2022-04-19 11:28:04 -07:00
|
|
|
|
2022-04-20 12:31:30 -07:00
|
|
|
u_out, s_out, v_out = _svd(a, hermitian, compute_uv, max_iterations)
|
2022-02-03 11:16:18 -08:00
|
|
|
|
|
|
|
if reduce_to_square:
|
|
|
|
u_out = q @ u_out
|
|
|
|
|
2022-04-20 12:31:30 -07:00
|
|
|
if full_matrices:
|
|
|
|
u_out = jnp.hstack((u_out, u_out_null))
|
|
|
|
|
2022-02-03 11:16:18 -08:00
|
|
|
if is_flip:
|
|
|
|
return(v_out, s_out, u_out.T.conj())
|
|
|
|
|
|
|
|
return (u_out, s_out, v_out.T.conj())
|