Skip to content

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

Closed

Description

@rookieLiu2018

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

No

Source

source

TensorFlow version

tf 2.17

Custom code

Yes

OS platform and distribution

No response

Mobile device

No response

Python version

No response

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

In a Linux + GPU environment, calling tf.linalg.solve on singular matrices yields different results on GPU depending on the matrix shape:
For a 3×3 singular matrix, the CPU backend raises InvalidArgumentError, but the GPU backend returns an identity matrix without error.
For a 2×2 singular matrix, both CPU and GPU backends correctly raise InvalidArgumentError.

Standalone code to reproduce the issue

import tensorflow as tf

for name, matrix in [
    ("3×3 float64 singular matrix", tf.constant([
        [1.0, 2.0, 3.0],
        [2.0, 5.0, 6.0],
        [3.0, 6.0, 9.0]
    ], dtype=tf.float64)),
    ("2×2 float64 singular matrix", tf.constant([
        [1.0, 2.0],
        [2.0, 4.0]
    ], dtype=tf.float64))
]:
    print(f"\n=== {name} ===")

    with tf.device('/CPU:0'):
        try:
            x_cpu = tf.linalg.solve(matrix, matrix)
            print("CPU solve ->\n", x_cpu.numpy())
        except Exception as e:
            print("CPU solve raised:", type(e).__name__, e)

    with tf.device('/GPU:0'):
        try:
            x_gpu = tf.linalg.solve(matrix, matrix)
            print("GPU solve ->\n", x_gpu.numpy())
        except Exception as e:
            print("GPU solve raised:", type(e).__name__, e)

Relevant log output

=== 3×3 float64 singular matrix ===
CPU solve raised: InvalidArgumentError {{function_node __wrapped__MatrixSolve_device_/job:localhost/replica:0/task:0/device:CPU:0}} Input matrix is not invertible. [Op:MatrixSolve] name: 
GPU solve ->
 [[1. 0. 0.]
 [0. 1. 0.]
 [0. 0. 1.]]

=== 2×2 float64 singular matrix ===
CPU solve raised: InvalidArgumentError {{function_node __wrapped__MatrixSolve_device_/job:localhost/replica:0/task:0/device:CPU:0}} Input matrix is not invertible. [Op:MatrixSolve] name: 
GPU solve raised: InvalidArgumentError {{function_node __wrapped__MatrixSolve_device_/job:localhost/replica:0/task:0/device:GPU:0}} Input matrix is not invertible. [Op:MatrixSolve] name:

Activity

  1. added
    2.17
    comp:ops
    comp:gpu
    on Jun 5, 2025
  2. Venkat6871 commented on Jun 5, 2025

    @Venkat6871
    Contributor

    I was able to reproduce the issue using TensorFlow version 2.19.0 and the nightly build. I have attached a Gist for your reference.

    Thank you!

  3. tripathi-genius commented on Jun 20, 2025

    @tripathi-genius

    Is it open for contribution? I want to contribute.

  4. Venkat6871 commented on Jun 23, 2025

    @Venkat6871
    Contributor

    Hi @tripathi-genius ,
    Yes, TensorFlow welcomes contributions! Please feel free to submit a Pull Request (PR). I am sharing the contribution guide for your reference, kindly follow the guidelines provided there.

    Thank you!

  5. added
    stat:contribution welcome
    on Jun 23, 2025
  6. added
    stat:awaiting response
    and removed
    stat:contribution welcome
    on Jul 2, 2025
  7. github-actions commented on Jul 10, 2025

    @github-actions

    This issue is stale because it has been open for 7 days with no activity. It will be closed if no further activity occurs. Thank you.

  8. added
    stale
    on Jul 10, 2025
  9. added
    stat:contribution welcome
    and removed
    stat:awaiting response
    stale
    on Jul 10, 2025
  10. girinithr commented on Jul 21, 2025

    @girinithr

    i wish to contribute to this, can i take up this task and contribute?

  11. Venkat6871 commented on Jul 22, 2025

    @Venkat6871
    Contributor

    Hi @girinithr ,
    Yes, TensorFlow welcomes contributions! Please feel free to submit a Pull Request (PR). I am sharing the contribution guide for your reference, kindly follow the guidelines provided there.

    Thank you!

  12. HamiAlyasin54 commented on Aug 27, 2025

    @HamiAlyasin54
  13. Harshit057 commented on Feb 3, 2026

    @Harshit057

    Thanks for the updates and the linked PRs (#103324, #109335).

    I can help by validating the fix on GPU (different shapes / dtypes / batched vs non-batched cases) and checking consistency with CPU error behavior. If there are specific scenarios you’d like tested, I’m happy to do so.

  14. added 3 commits that reference this issue on Mar 18, 2026
    23a0931
    598b3d9
    6d376ad
  15. removed
    stat:contribution welcome
    on May 22, 2026
  16. google-ml-butler commented on Aug 11, 2026

    @google-ml-butler

    Are you satisfied with the resolution of your issue?
    Yes
    No

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

2.17awaiting PR mergecomp:gpucomp:opstype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions