You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
tf.linalg.matrix_rank and lu_* cast float64/complex NumPy inputs to float32 (wrong rank, imaginary part dropped) #129739
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.
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.
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.
Activity
Kayyuri commentedon Oct 30, 2026
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.
google-ml-butler commentedon Nov 2, 2026
Are you satisfied with the resolution of your issue?
Yes
No
Keep float64 and complex NumPy inputs in matrix_rank and lu_*