Add warning when CG tolerance is not met
#!/usr/bin/env python3
import torch
+import warnings
from .. import settings
@@ -192,6 +193,10 @@ def linear_cg(
update_tridiag = True
last_tridiag_iter = 0
+
+ # It's conceivable we reach the tolerance on the last iteration, so can't just check iteration number.
+ tolerance_reached = False
+
# Start the iteration
for k in range(n_iter):
# Get next alpha
@@ -251,6 +256,7 @@ def linear_cg(
torch.lt(residual_norm, stop_updating_after, out=has_converged)
if k >= 10 and bool(residual_norm.mean() < tolerance) and not (n_tridiag and k < n_tridiag_iter):
+ tolerance_reached = True
break
# Update tridiagonal matrices, if applicable
@@ -280,6 +286,13 @@ def linear_cg(
# Un-normalize
result.mul_(rhs_norm)
+ if not tolerance_reached:
+ warnings.warn(
+ "CG terminated before reaching the tolerance specified by gpytorch.settings.cg_tolerance. If performance"
+ " is affected, consider raising the maximum number of CG iterations by running code in"
+ " a gpytorch.settings.max_cg_iterations(value) context."
+ )
+
if is_vector:
result = result.squeeze(-1)
