Skip to content

Nx.LinAlg.invert gradient broken for batched inputs #1744

@blasphemetheus

Description

@blasphemetheus

Nx.LinAlg.invert gradient raises on batched inputs. The custom_grad at nx/lib/nx/lin_alg.ex:866 uses Nx.dot without batch axes, producing a higher-rank intermediate that can't broadcast.

x = Nx.tensor([
      [[4.0, 1.0], [1.0, 5.0]],
      [[9.0, 1.0], [1.0, 10.0]]
    ])                                  # {2, 2, 2}

Nx.Defn.grad(x, fn a -> Nx.sum(Nx.LinAlg.invert(a)) end)
# ** (ArgumentError) cannot broadcast tensor of dimensions
#    {2, 3, 2, 2, 3} to {2, 3, 3}

Same class as #1741/#1742/#1743 (triangular_solve / LU / SVD batched-grad bugs). Fix pattern mirrors QR/Cholesky in #1731: use batch_axes/1 + Nx.dot(a, [-1], ba, g, [-2], ba) style in the custom_grad formula.

Found while extending the batched-grad fuzz probe across remaining hand-derived-grad linalg ops.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions