Skip to content

tf.linalg.matrix_rank and lu_* cast float64/complex NumPy inputs to float32 (wrong rank, imaginary part dropped) #129739

Closed

Description

@bodapatisaikrishna

Issue type

Bug

Have you reproduced the bug with TensorFlow Nightly?

Yes

Source

binary

TensorFlow version

tf-nightly 2.23.0-dev20261028 (git v1.12.1-148391-g87a97a3655f)

Custom code

Yes

OS platform and distribution

macOS 15.7.2 (arm64)

Mobile device

No response

Python version

3.12.0

Bazel version

No response

GCC/compiler version

No response

CUDA/cuDNN version

No response

GPU model and memory

No response

Current behavior?

tf.linalg.matrix_rank, tf.linalg.lu_reconstruct, tf.linalg.lu_matrix_inverse and tf.linalg.lu_solve convert their matrix argument with ops.convert_to_tensor(..., dtype_hint=dtypes.float32) (linalg_impl.py#L851, #L1044-L1045, #L1146-L1147, #L1198-L1199). The hint isn't only used for Python lists: NumPy arrays get converted to float32 too whenever that's possible, so a float64 array loses precision and a complex array loses its imaginary part (NumPy only emits a ComplexWarning). Passing the same values as a tf.Tensor works, so results depend on whether the caller handed over a NumPy array or a tensor:

  • matrix_rank(np.diag([1., 1e-9])) returns 1. The same float64 matrix as a tensor gives 2, as does NumPy. Any singular value under ~2.4e-7 of the largest is dropped because the SVD and the tolerance are both float32.
  • matrix_rank of a complex NumPy matrix computes the rank of its real part. For a complex tensor it raises TypeError: Input a must have float-like dtype, so the NumPy path should probably raise too.
  • lu_reconstruct/lu_matrix_inverse/lu_solve given the NumPy output of tf.linalg.lu on a complex matrix return a float32 result built from the real parts only. For float64 they return float32.

Expected: float64 and complex NumPy inputs keep their dtype, the same as for tensor inputs. The float32 hint still makes sense for Python lists and integer arrays.

Standalone code to reproduce the issue

import numpy as np
import tensorflow as tf

x = np.diag([1.0, 1e-9])  # float64
print(tf.linalg.matrix_rank(x).numpy(), tf.linalg.matrix_rank(tf.constant(x)).numpy())

z = np.array([[1, 1j], [1j, -1]])  # rank 1
print(tf.linalg.matrix_rank(z).numpy(), np.linalg.matrix_rank(z))

m = np.array([[1 + 2j, 3j], [2, 1 - 1j]])
lu, perm = tf.linalg.lu(m)
print(tf.linalg.lu_reconstruct(lu.numpy(), perm.numpy()).numpy())

Relevant log output

1 2
2 1
[[ 1.   0. ]
 [ 0.4 -1.4]]

The last one should give back m ([[1+2j, 3j], [2, 1-1j]]). There's also a ComplexWarning: Casting complex values to real discards the imaginary part from constant_op.py.

I have a fix and will open a PR.

Activity

  1. added
    awaiting PR merge
    2.21.0
    comp:ops
    on Oct 30, 2026
  2. Kayyuri commented on Oct 30, 2026

    @Kayyuri

    Hi @bodapatisaikrishna,
    Thanks for reporting the issue and submitting a fix for TensorFlow. Once the PR #129741 is merged, the issue will be resolved. We truly appreciate your effort in contributing the fix.

  3. google-ml-butler commented on Nov 2, 2026

    @google-ml-butler

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

  4. added a commit that references this issue on Nov 2, 2026
    76811e0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

2.21.0awaiting PR mergecomp:opstype:bug

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions