Core Math & Gradients

tensorflow/tensorflow · C++, Eigen, Python

Safeguarded tf.linalg.det backward pass against gradient explosion and NaN contamination on singular matrices, fixed tf.math.floordiv for -inf divisors, added Jacobian gradient tests for igamma, and prevented +inf NaN cascades in cumulative_logsumexp.

Verify with Bazel

Run the upstream regression suite in tensorflow/tensorflow for this subsystem.

$bazel test //tensorflow/python/kernel_tests/linalg:linalg_grad_test

Changed Files (1)

tensorflow/python/kernel_tests/linalg/linalg_grad_test.py9 lines
# PR #122823 — Safeguard det & slogdet gradients on singular matrices
class DetGradSingularMatrixTest(test_lib.TestCase):
  @test_util.run_in_graph_and_eager_modes(use_gpu=False)
  def testDetGradSingular2x2(self):
    # det([[1, 2], [2, 4]]) == 0; its adjugate is [[4, -2], [-2, 1]].
    m = np.array([[1.0, 2.0], [2.0, 4.0]], dtype=np.float64)
    theoretical, _ = gradient_checker_v2.compute_gradient(
        linalg_ops.matrix_determinant, [m])
    self.assertAllClose([[4.0, -2.0, -2.0, 1.0]], theoretical[0])

1. Introduction & Motivation

Differentiable linear algebra layers frequently compute determinants of learned or intermediate matrices that can become rank-deficient during training. Because TensorFlow's _MatrixDeterminantGrad evaluated det⁡(A) A−T\det(A)\, A^{-T} via direct matrix inversion, singular matrices (det⁡(A)=0\det(A) = 0) triggered division by zero, gradient explosion, and NaN contamination across the entire backward pass [1].

2. Matrix Calculus & Singular Extension

By Jacobi's formula, for any invertible matrix A∈Rn×nA \in \mathbb{R}^{n \times n}, the differential of the determinant satisfies [2]:

d(det⁡A)=tr⁡(adj⁡(A) dA)  ⟹  ∇Adet⁡(A)=det⁡(A) A−T=adj⁡(A)T(1)d(\det A) = \operatorname{tr}\big(\operatorname{adj}(A)\, dA\big) \implies \nabla_A \det(A) = \det(A)\, A^{-T} = \operatorname{adj}(A)^T \tag{1}

Because each entry of the classical adjugate adj⁡(A)T\operatorname{adj}(A)^T is an (n−1)×(n−1)(n - 1) \times (n - 1) cofactor minor (a polynomial in the entries of AA), it requires zero division by det⁡(A)\det(A) and extends continuously across singular matrices:

A=(1224),det⁡(A)=0  ⟹  ∇Adet⁡(A)=adj⁡(A)T=(4−2−21)(2)A = \begin{pmatrix} 1 & 2 \\ 2 & 4 \end{pmatrix}, \quad \det(A) = 0 \implies \nabla_A \det(A) = \operatorname{adj}(A)^T = \begin{pmatrix} 4 & -2 \\ -2 & 1 \end{pmatrix} \tag{2}
Matrix Conditiondet⁡(A) A−T\det(A)\, A^{-T} (Before)Adjugate adj⁡(A)T\operatorname{adj}(A)^T (After)
Invertible (det⁡(A)≠0\det(A) \neq 0)Finite gradientIdentical finite gradient
Singular (det⁡(A)=0\det(A) = 0)NaN / inf crashExact continuous cofactor gradient

3. Implementation & Verification

# In tensorflow/python/kernel_tests/linalg/linalg_grad_test.py:
m = np.array([[1.0, 2.0], [2.0, 4.0]], dtype=np.float64)
theoretical, _ = gradient_checker_v2.compute_gradient(linalg_ops.matrix_determinant, [m])
self.assertAllClose([[4.0, -2.0, -2.0, 1.0]], theoretical[0])

4. References

  1. Giles, An Extended Collection of Matrix Derivative Results for Forward and Reverse Mode AD, 2008.
  2. Petersen & Pedersen, The Matrix Cookbook, Section 2.1 (Derivatives of Determinants).
  3. Anandhu P Shaji, Hunting Compiler Bugs and Numerical Traps in TensorFlow, anandhupshaji.me/posts/tensorflow-stories.html#singular-matrix-gradients.
Read technical story

The soul becomes dyed with the color of its thoughts.

Marcus Aurelius

Made with <3 and lots of coffee.