Skip to content

prepare_ad_third can return NaN on Julia 1.12/1.13: Enzyme forward over forward through a matrix times a view #57

Open

Description

@nsiccha

Summary

On Julia 1.12 and 1.13, registered Enzyme (0.13.216) returns NaN on some calls of forward mode over forward mode (a second directional derivative D²f(u)[v, v]) when the function multiplies a constant matrix by a view of the active vector. Identical calls in one process disagree, and the count varies between processes. The single forward pass is exact, reverse over forward is exact, the same nested call over a slice (u[2:4]) is exact, and every call is exact on Julia 1.10.

ReactiveKernels' third-order directional derivatives, prepare_ad_third (∇(vᵀ H v) = D³f[v, v, ·], reverse over forward over forward), inherit this: ReactiveKernelsPPL reads its coefficients as views of the unconstrained vector, so any design-matrix linear predictor (X * beta) meets the shape. Third-order derivatives of such programs can therefore come back NaN on Julia 1.12/1.13. Hessian-vector products are unaffected.

Reproducer (Enzyme only)

benchmark/repro_enzyme_forward_over_forward_view_matvec.jl:

using Enzyme

const A = hcat(ones(20), [0.3 * sin(i) for i in 1:20],
               [0.2 * cos(2i) for i in 1:20])

blas_view(u, A) = sum(abs2, A * view(u, 2:4))
blas_copy(u, A) = sum(abs2, A * u[2:4])

directional(u, v, f, A) =
    only(autodiff(Forward, Const(f), Duplicated(u, v), Const(A)))
second_directional(u, v, f, A) =
    only(autodiff(Forward, Const(directional), Duplicated(u, v), Const(v),
                  Const(f), Const(A)))

u = [0.15 * cos(i + 7) for i in 1:8]
v = [sin(i) for i in 1:8]
for (label, f) in (("A * view(u, 2:4)", blas_view), ("A * u[2:4]", blas_copy))
    second_order = [second_directional(u, v, f, A) for _ in 1:300]
    println(label, ": NaN in ", count(isnan, second_order), " of 300")
end

Observed on x86-64 Linux, Enzyme 0.13.216, Enzyme_jll 0.0.303, one thread (the exact value is 34.05519704580726):

300 calls Julia 1.10.12 Julia 1.12.7 Julia 1.13.1
sum(abs2, A * view(u, 2:4)) 0 NaN 14 NaN 19 NaN
sum((A * view(u, 2:4)) .^ 2) 0 60 6
m = A * view(u, 2:4); dot(m, m) 0 25 18
sum((A * u[2:4]) .^ 2) (slice) 0 0 0

Current handling in ReactiveKernels

  • No ReactiveKernels-side workaround: the lowering is unchanged.
  • docs/src/constraints.md and the automatic-differentiation page record the boundary.
  • The RKPPL third-order tests (packages/ReactiveKernelsPPL/test/test_query_derivatives.jl) record a NaN result on Julia ≥ 1.12 as broken and still require every finite result to match the analytic third derivative.
  • Consumers that need exact third-order derivatives of design-matrix programs today run them on Julia 1.10.

Close when

A registered Enzyme release returns the exact value on every call of the reproducer on Julia 1.12 and 1.13. Then drop the VERSION >= v"1.12" NaN allowance in test_query_derivatives.jl, the constraints entry and the reproducer note together.

Activity

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

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