Skip to content
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

Qr complex jvp fix #2872

Merged
merged 3 commits into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
12 changes: 7 additions & 5 deletions jax/lax_linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -779,16 +779,18 @@ def qr_jvp_rule(primals, tangents, full_matrices):
x, = primals
dx, = tangents
q, r = qr_p.bind(x, full_matrices=False)
if (full_matrices or np.shape(x)[-2] < np.shape(x)[-1] or
np.issubdtype(x.dtype, np.complexfloating)):
*_, m, n = x.shape
if full_matrices or m < n:
raise NotImplementedError(
"Unimplemented case of QR decomposition derivative")
dx_rinv = triangular_solve(r, dx) # Right side solve by default
qt_dx_rinv = np.matmul(_H(q), dx_rinv)
qt_dx_rinv_lower = np.tril(qt_dx_rinv, -1)
domega = qt_dx_rinv_lower - _H(qt_dx_rinv_lower) # This is skew-symmetric
dq = np.matmul(q, domega - qt_dx_rinv) + dx_rinv
dr = np.matmul(qt_dx_rinv - domega, r)
do = qt_dx_rinv_lower - _H(qt_dx_rinv_lower) # This is skew-symmetric
# The following correction is necessary for complex inputs
do = do + np.eye(n, dtype=do.dtype) * (qt_dx_rinv - np.real(qt_dx_rinv))
dq = np.matmul(q, do - qt_dx_rinv) + dx_rinv
dr = np.matmul(qt_dx_rinv - do, r)
return (q, r), (dq, dr)

def qr_batching_rule(batched_args, batch_dims, full_matrices):
Expand Down
3 changes: 1 addition & 2 deletions tests/linalg_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -603,8 +603,7 @@ def compare_orthogonal(q1, q2):
self.assertTrue(onp.all(
norm(onp.eye(k) -onp.matmul(onp.conj(T(lq)), lq)) < 5))

if (not full_matrices and m >= n
and not np.issubdtype(dtype, np.complexfloating)):
if not full_matrices and m >= n:
jtu.check_jvp(np.linalg.qr, partial(jvp, np.linalg.qr), (a,), atol=3e-3)

@parameterized.named_parameters(jtu.cases_from_list(
Expand Down