Skip to content

Fix inv_quad_logdet with a vector rhs (Chol/Dense) and CatLinearOperator.logdet() - #160

Open
flyingdoubleG wants to merge 1 commit into
cornellius-gp:mainfrom
flyingdoubleG:fix-inv-quad-logdet-edge-cases
Open

flyingdoubleG wants to merge 1 commit into
cornellius-gp:mainfrom
flyingdoubleG:fix-inv-quad-logdet-edge-cases

Conversation

@flyingdoubleG

Copy link
Copy Markdown

Summary

This PR fixes two crashes in inv_quad_logdet.

1. Vector right-hand side through CholLinearOperator

CholLinearOperator.inv_quad_logdet explicitly accepts a 1-D inv_quad_rhs for a non-batch operator, as the default implementation does. It then calls inv_quad, which solves with the vector and sums over dim=-2. For a 1-D solve that dimension does not exist:

IndexError: Dimension out of range (expected to be in range of [-1, 0], but got -2)

Operators below max_cholesky_size compute inv_quad_logdet through a CholLinearOperator. So this also breaks the common call

DenseLinearOperator(A).inv_quad_logdet(vec, logdet=True)   # IndexError on main

while DenseLinearOperator(A).inv_quad(vec) (default path) works and returns a scalar.

Fix. In inv_quad, treat a 1-D right-hand side of a non-batch operator as one column. The output shapes then match the default implementation: a scalar with reduce_inv_quad=True, and (1,) otherwise.

2. CatLinearOperator.logdet()

CatLinearOperator.inv_quad_logdet moves every returned term to the output device, including the None returned for a term that was not requested. So CatLinearOperator.logdet() and inv_quad_logdet(logdet=True) without a right-hand side raise:

AttributeError: 'NoneType' object has no attribute 'to'

Fix. Leave None terms unchanged.

Tests

  • TestCholLinearOperator.test_inv_quad_vector: covers inv_quad (reduced and not reduced) and inv_quad_logdet with a vector, plus the DenseLinearOperator path.
  • TestCatLinearOperator.test_logdet_without_inv_quad.

Both fail on main and pass with this change. The full suite gives 5049 passed and 1 failed. The failure is test_psd_safe_cholesky_psd, which also fails on main without these changes. I ran ufmt and flake8 on the changed files.

Related, but not covered by this PR: #139 and #147 (vector right-hand sides for DiagLinearOperator batches and low-rank operators), and #144/#145 (CholLinearOperator with upper factors, and slicing).

CholLinearOperator.inv_quad_logdet accepts a vector right-hand side for a
non-batch operator, but inv_quad then summed over dim -2 of the 1-D solve
and raised an IndexError. Since small operators compute inv_quad_logdet
through a CholLinearOperator, this also broke e.g.
DenseLinearOperator(A).inv_quad_logdet(vec, logdet=True).

CatLinearOperator.inv_quad_logdet called .to(device) on every term,
including the None that is returned when no right-hand side is given, so
CatLinearOperator.logdet() raised an AttributeError.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant