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

Perform QR composition in exact prediction strategy on the CPU #1224

Merged
merged 2 commits into from
Jul 24, 2020
Merged
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
24 changes: 20 additions & 4 deletions gpytorch/models/exact_prediction_strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,25 +204,41 @@ def get_fantasy_strategy(self, inputs, targets, full_inputs, full_targets, full_
batch_shape = fant_train_covar.shape[:-2]

L_inverse = self.covar_cache
L = delazify(self.lik_train_train_covar.root_decomposition().root)
L = self.lik_train_train_covar.root_decomposition().root
m, n = L.shape[-2:]

lower_left = fant_train_covar.matmul(L_inverse)
schur = fant_fant_covar - lower_left.matmul(lower_left.transpose(-2, -1))
schur_root = psd_safe_cholesky(schur, jitter=settings.cholesky_jitter.value())

# Form new root Z = [L 0; lower_left schur_root]

# # TODO: Special case triangular case once #1102 goes in
# if isinstance(L, TriangularLazyTensor):
# # The whole thing is triangular, we can just do two triangular solves
# ...
# else:

L = delazify(L)
num_fant = schur_root.size(-2)
m, n = L.shape[-2:]
new_root = torch.zeros(*batch_shape, m + num_fant, n + num_fant, device=L.device, dtype=L.dtype)
new_root[..., :m, :n] = L
new_root[..., m:, :n] = lower_left
new_root[..., m:, n:] = schur_root

# Use pseudo-inverse of Z as new inv root
Q, R = torch.qr(new_root)

if new_root.shape[-1] <= 2048:
# Dispatch to CPU so long as pytorch/pytorch#22573 is not fixed
device = new_root.device
Q, R = torch.qr(new_root.cpu())
Q = Q.to(device)
R = R.to(device)
else:
Q, R = torch.qr(new_root)

Rdiag = torch.diagonal(R, dim1=-2, dim2=-1)
# if R is almost singular, add jitter (Rdiag is a view, so this works)
# if R is almost singular, add jitter
zeroish = Rdiag.abs() < 1e-6
if torch.any(zeroish):
# can't use in-place operation here b/c it would mess up backward pass
Expand Down