Repository navigation
Conversation
The batched solver path (used when matrix size <= 128 * batch_size) calls GetrfBatched for LU factorization but proceeds to GetrsBatched without checking if the factorization detected a singular matrix. The singularity info was only checked asynchronously after the solve completed, allowing garbage results to be returned silently. Add a synchronous singularity check after GetrfBatched by copying the device info to host and verifying all info values are <= 0 before proceeding to the solve step. This matches the CPU kernel behavior which checks pivot magnitudes before solving. Fixes tensorflow#94657
dmiltr3
left a comment
There was a problem hiding this comment.
Thanks for the contribution! The PR looks great.
Review Details
- Bug Resolution: This change correctly prevents silent data corruption when using
tf.linalg.solveon the GPU with singular matrices. Proceeding to the getrs step with singular factorization outputs is unsafe and mathematical results are undefined, so a synchronous check of host info values right after LU factorization (GetrfBatched) is the correct block-device safety tradeoff here. - Implementation correctness: By issuing
dev_info.CopyToHost(...)and callingstream->BlockHostUntilDone(), the host thread waits for the status code from the GPU and can check the condition and throwInvalidArgumentErrormatching the expected CPU implementation error. - Compatibility: Checked both CUDA-specific code and tested the changes against ROCm and standard CPU runs.
Unit Tests To Be Added
Please add unit tests in tensorflow/python/kernel_tests/linalg/matrix_solve_op_test.py to ensure GPU code paths are validated, checking both single and batched input cases. Below is the test case implementation:
@test_util.run_in_graph_and_eager_modes(use_gpu=True)
def testNotInvertible(self):
# All rows of the matrix below add to zero
matrix = constant_op.constant([[1., 0., -1.], [-1., 1., 0.],
[0., -1., 1.]])
with self.assertRaisesOpError("Input matrix is not invertible."):
self.evaluate(linalg_ops.matrix_solve(matrix, matrix))
# Test batched input
matrix_batch = constant_op.constant([[[1., 0., -1.], [-1., 1., 0.],
[0., -1., 1.]],
[[1., 0., -1.], [-1., 1., 0.],
[0., -1., 1.]]])
with self.assertRaisesOpError("Input matrix is not invertible."):
self.evaluate(linalg_ops.matrix_solve(matrix_batch, matrix_batch))This test has been verified and successfully compiles/asserts the expected InvalidArgumentError on GPU execution.
|
@dmiltr3 I pushed the requested MatrixSolve GPU regression coverage in commit d8c5e02. The new test covers singular single-matrix inputs and a batched singular input under @test_util.run_in_graph_and_eager_modes(use_gpu=True)\ in \ ensorflow/python/kernel_tests/linalg/matrix_solve_op_test.py. Validation:
Local Bazel execution is blocked because \�azel\ is not installed on this machine's PATH; the focused CI target is //tensorflow/python/kernel_tests/linalg:matrix_solve_op_test\ filtered to \MatrixSolveOpTest.testNotInvertibleGpu. |
cantonios
left a comment
There was a problem hiding this comment.
Can you expand on
The existing async CheckLapackInfoAndDeleteSolverAsync callback may not complete before the output tensor is returned
|
The current maintainer feedback objects to synchronously blocking the host and asks for a different per-batch singular-result contract. I have not pushed a cosmetic patch over that design objection. The asynchronous GPU diagonal-detection approach is represented by PR #112658 and was updated there for 64-bit-safe indexing. I am leaving this PR open and unchanged pending maintainer direction; no PR was closed or force-pushed. |
|
Hi @ayushozha, Can you please resolve the conflicts? Thank you! |
|
This PR is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you. |
dmiltr3
left a comment
There was a problem hiding this comment.
Hi @kshitij12345,
Thank you for your contribution!
It appears that this branch has developed merge conflicts with the latest master branch. Could you please rebase your branch on top of master or merge master into it and resolve the conflicts?
Once that is done, we can proceed with running the internal automated test suite and review the changes.
|
This PR is stale because it has been open for 14 days with no activity. It will be closed if no further activity occurs. Thank you. |
|
Closing as superseded by #112658, merged on August 11. Current master contains its asynchronous GPU singularity check and the same three regression cases, so rebasing this PR would only restore the rejected host-blocking approach. Correction to the original description: I found no evidence that the asynchronous callback returns an unchecked result. The status copy and callback are ordered on the GPU stream, and MatrixSolve calls done() after checking status. The merged fix addresses missing singularity information through an explicit LU-diagonal check. Verified against master ac3adda. This audit did not rerun CUDA tests. |
Summary
tf.linalg.solveon GPU silently returns wrong results for singular matrices in the batched solver pathCheckLapackInfoAndDeleteSolverAsynccallback may not complete before the output tensor is returnedGetrfBatchedinfo values immediately after factorization, raisingInvalidArgumentfor singular matrices before proceeding toGetrsBatchedTest plan
testNotInvertiblepasses on GPU with singular 3x3 matriceslinalg.solvetests continue to pass for invertible matricesFixes #94657