Skip to content

Commit

Permalink
Ensure low_rank_cholesky works well with LinearOperators on non-TF …
Browse files Browse the repository at this point in the history
…backends.

PiperOrigin-RevId: 566817003
  • Loading branch information
srvasude authored and tensorflower-gardener committed Sep 20, 2023
1 parent 6efcda9 commit dff8111
Showing 1 changed file with 4 additions and 3 deletions.
7 changes: 4 additions & 3 deletions tensorflow_probability/python/math/linalg.py
Original file line number Diff line number Diff line change
Expand Up @@ -454,8 +454,9 @@ def low_rank_cholesky(matrix, max_rank, trace_atol=0, trace_rtol=0, name=None):
dtype_hint=tf.float32)
if not isinstance(matrix, tf.linalg.LinearOperator):
matrix = tf.convert_to_tensor(matrix, name='matrix', dtype=dtype)
matrix = tf.linalg.LinearOperatorFullMatrix(matrix)

mtrace = tf.linalg.trace(matrix)
mtrace = matrix.trace()
mrank = tensorshape_util.rank(matrix.shape)
batch_dims = mrank - 2

Expand Down Expand Up @@ -485,7 +486,7 @@ def lr_cholesky_body(i, lr, residual_diag):
matrix_row = tf.squeeze(matrix.row(max_j), axis=-2)
else:
matrix_row = tf.gather(
matrix, max_j, axis=-1, batch_dims=batch_dims)[..., 0]
matrix.to_dense(), max_j, axis=-1, batch_dims=batch_dims)[..., 0]
# residual_matrix[max_j, :] = matrix_row[max_j, :] - (lr * lr^t)[max_j, :]
# And (lr * lr^t)[max_j, :] = lr[max_j, :] * lr^t
lr_row_maxj = tf.gather(lr, max_j, axis=-2, batch_dims=batch_dims)
Expand Down Expand Up @@ -530,7 +531,7 @@ def lr_cholesky_body(i, lr, residual_diag):

lr = tf.zeros(matrix.shape, dtype=matrix.dtype)[..., :max_rank]

mdiag = tf.linalg.diag_part(matrix)
mdiag = matrix.diag_part()
i, lr, residual_diag = tf.while_loop(
cond=lr_cholesky_cond,
body=lr_cholesky_body,
Expand Down

0 comments on commit dff8111

Please sign in to comment.