Skip to content

Add synchronous singularity check in batched GPU linalg.solve - #109335

Closed
ayushozha wants to merge 2 commits into
tensorflow:masterfrom
ayushozha:fix/linalg-solve-gpu-singularity
Closed

ayushozha wants to merge 2 commits into
tensorflow:masterfrom
ayushozha:fix/linalg-solve-gpu-singularity

Conversation

@ayushozha

Copy link
Copy Markdown
Contributor

Summary

  • tf.linalg.solve on GPU silently returns wrong results for singular matrices in the batched solver path
  • The existing async CheckLapackInfoAndDeleteSolverAsync callback may not complete before the output tensor is returned
  • This change adds a synchronous host-side check of GetrfBatched info values immediately after factorization, raising InvalidArgument for singular matrices before proceeding to GetrsBatched

Test plan

  • Verify testNotInvertible passes on GPU with singular 3x3 matrices
  • Existing linalg.solve tests continue to pass for invertible matrices

Fixes #94657

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
@google-ml-butler google-ml-butler Bot added the size:S CL Change Size: Small label Feb 1, 2026
@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Feb 2, 2026
@github-project-automation github-project-automation Bot moved this to Assigned Reviewer in PR Queue Feb 2, 2026
@keerthanakadiri keerthanakadiri added prtype:bugfix PR to fix a bug comp:core issues related to core part of tensorflow labels Feb 2, 2026
@dmiltr3
dmiltr3 self-requested a review June 29, 2026 02:02

@dmiltr3 dmiltr3 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the contribution! The PR looks great.

Review Details

  1. Bug Resolution: This change correctly prevents silent data corruption when using tf.linalg.solve on 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.
  2. Implementation correctness: By issuing dev_info.CopyToHost(...) and calling stream->BlockHostUntilDone(), the host thread waits for the status code from the GPU and can check the condition and throw InvalidArgumentError matching the expected CPU implementation error.
  3. 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.

@github-project-automation github-project-automation Bot moved this from Assigned Reviewer to Reviewer Requested Changes in PR Queue Jun 29, 2026
@keerthanakadiri keerthanakadiri added stat:awaiting response Status - Awaiting response from author and removed awaiting review Pull request awaiting review labels Jun 29, 2026
@ayushozha

Copy link
Copy Markdown
Contributor Author

@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:

  • \python -m py_compile tensorflow\python\kernel_tests\linalg\matrix_solve_op_test.py\
  • \git diff --check HEAD~1..HEAD\

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.

@google-ml-butler google-ml-butler Bot removed the stat:awaiting response Status - Awaiting response from author label Jul 5, 2026
@nithyak0204
nithyak0204 requested a review from dmiltr3 July 9, 2026 03:36
@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Jul 9, 2026
@google-ml-butler google-ml-butler Bot added kokoro:force-run Tests on submitted change ready to pull PR ready for merge process labels Jul 13, 2026
@github-project-automation github-project-automation Bot moved this from Reviewer Requested Changes to Approved by Reviewer in PR Queue Jul 13, 2026
@kokoro-team kokoro-team removed the kokoro:force-run Tests on submitted change label Jul 13, 2026
@nithyak0204 nithyak0204 removed the awaiting review Pull request awaiting review label Jul 14, 2026

@cantonios cantonios left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you expand on

The existing async CheckLapackInfoAndDeleteSolverAsync callback may not complete before the output tensor is returned

@github-project-automation github-project-automation Bot moved this from Approved by Reviewer to Reviewer Requested Changes in PR Queue Jul 14, 2026
@nithyak0204 nithyak0204 added stat:awaiting response Status - Awaiting response from author and removed ready to pull PR ready for merge process labels Jul 14, 2026
@nithyak0204 nithyak0204 added stat:awaiting response Status - Awaiting response from author and removed stat:awaiting response Status - Awaiting response from author labels Jul 14, 2026
@ayushozha

Copy link
Copy Markdown
Contributor Author

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.

@google-ml-butler google-ml-butler Bot removed the stat:awaiting response Status - Awaiting response from author label Jul 17, 2026
@nithyak0204
nithyak0204 requested a review from cantonios July 23, 2026 16:57
@google-ml-butler google-ml-butler Bot added the awaiting review Pull request awaiting review label Jul 23, 2026
@nithyak0204

Copy link
Copy Markdown

Hi @ayushozha, Can you please resolve the conflicts? Thank you!

@nithyak0204 nithyak0204 added stat:awaiting response Status - Awaiting response from author and removed awaiting review Pull request awaiting review labels Aug 12, 2026
@github-actions

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the stale This label marks the issue/pr stale - to be closed automatically if no activity label Aug 27, 2026

@dmiltr3 dmiltr3 left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

@github-actions github-actions Bot removed stale This label marks the issue/pr stale - to be closed automatically if no activity stat:awaiting response Status - Awaiting response from author labels Sep 10, 2026
@nithyak0204 nithyak0204 added the stat:awaiting response Status - Awaiting response from author label Sep 10, 2026
@github-actions

Copy link
Copy Markdown

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.

@github-actions github-actions Bot added the stale This label marks the issue/pr stale - to be closed automatically if no activity label Sep 25, 2026
@ayushozha

Copy link
Copy Markdown
Contributor Author

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.

@ayushozha ayushozha closed this Sep 29, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

comp:core issues related to core part of tensorflow prtype:bugfix PR to fix a bug size:S CL Change Size: Small stale This label marks the issue/pr stale - to be closed automatically if no activity stat:awaiting response Status - Awaiting response from author

Projects

Status: Closed/Rejected

Development

Successfully merging this pull request may close these issues.

tf.linalg.solve behaves inconsistently on GPU for singular matrices depending on shape

7 participants