Enable NaN-safe mode by default
@devmotion đang làm issue này rồi.
Từ ngày 12/10/2026.
Đánh giá
- Độ khó
- 5/5
- Thời gian dự kiến
- Hơn một tuần
- Mức phù hợp với người mới
- 25/100
- Loại issue
- Tính năng
- Độ rõ ràng
- Khá rõ ràng
- Mức độ hoạt động
- Sôi nổi
- Lĩnh vực
- backend
Hướng nghiên cứu
Read the discussion in #719 and the behavior changes in #777 and #856, then trace nansafe_mode and the Dual^Real branch. Decide whether NaN-safe mode should become the default, accounting for correctness and the reported performance cost; done means the default behavior and opt-out preference are settled and documented.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
I'd like to propose enabling NaN-safe mode by default, and making the current behaviour opt-in via the nansafe_mode preference.
This was discussed in #719 (starting at https://github.com/JuliaDiff/ForwardDiff.jl/issues/719#issuecomment-2471545577), but since switching the default is not breaking, the question was not resolved there and #719 was closed with the 1.0 release. So I open a separate issue for it, as suggested in https://github.com/JuliaDiff/ForwardDiff.jl/issues/719#issuecomment-2474602850.
IMO NaN-safe mode is not primarily about NaNs but about the definition of dual numbers: a dual number with a zero partial is unperturbed in that direction, so it must not contribute to the derivative in that direction, regardless of the derivative of the primal function at that point. The default mode violates this whenever the derivative is not finite (0 * Inf and 0 * NaN are NaN). Our documentation even says so ("oops, this NaN should be 0.0").
As a consequence, the default mode returns incorrect derivatives:
julia> f(v) = sqrt(v[1]) + v[2];
julia> ForwardDiff.gradient(f, [0.0, 1.0]) # ∂f/∂v[2] = 1
2-element Vector{Float64}:
Inf
NaN
Moreover, results depend on implementation details that should be irrelevant:
- whether an unperturbed argument is a
Realor aDualwith zero partials (see #856), - the chunk size: with the existing all-or-nothing check in the
Dual^Realbranch of^,v[1]^0.5 + v[2]returns[Inf, 1.0]withChunk{1}and[Inf, NaN]withChunk{2}.
In NaN-safe mode, all of these examples return [Inf, 1.0] for any chunk size (since #777 the check is applied to each partial separately).
IMO NaN-safe mode should be the default regardless of its performance impact. Correctness should be the default, and faster but incorrect results should be opt-in (as with, e.g., @fastmath) - one can always be fast by returning nonsense. The documentation states a performance impact of ~5-10%, but that's outdated: in the Rosenbrock benchmark in #777 the gradient took 853 μs with NaN-safe mode vs 597 μs without (median), i.e., about 40% more. That's worth documenting for users who want to opt out, but it shouldn't decide the default. Also note that since #777 NaN-safe mode does not turn the partials multiplication into branchy code anymore (one of the concerns in https://github.com/JuliaDiff/ForwardDiff.jl/issues/719#issuecomment-2484955066): _mul_partials compiles to SIMD instructions with an additional comparison and mask per partial (see the native code in #777).
- Ngôn ngữ chính
- Julia
- Star
- 1k
- Fork
- 162
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của JuliaDiff/ForwardDiff.jl
-
missing floatmin, floatmax, and issubnormal for DualCó thể đã có người làm @devmotion đã nhận 17 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
JuliaDiff/ForwardDiff.jl#852 · 6 bình luận ·
-
`fma` and `muladd` ignore NaN-safe modeCó thể đã có người làm @devmotion đã nhận 2 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 55/100
JuliaDiff/ForwardDiff.jl#860 ·
-
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 52/100
JuliaDiff/ForwardDiff.jl#854 · 1 bình luận ·
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 75/100
JuliaDiff/ForwardDiff.jl#849 ·
-
`gradient!` leaves part of the result unwritten when `f` does not depend on its argumentCó thể đã có người làm @devmotion đã nhận 45 ngày trước. Đang mở
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 78/100
JuliaDiff/ForwardDiff.jl#847 ·
Tất cả issue của JuliaDiff/ForwardDiff.jl
Issue tương tự
-
Add more snippets to R and JuliaĐang mởenhancement good first issue help wanted
Độ khó 2/5 Nửa ngày Mức phù hợp với người mới 82/100
-
authority-vechtstromen ready-for-agent
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 75/100
Deltares/Ribasim-NL#890 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
Maintainer thường phản hồi trong vòng 1 ngày
-
Precompilation fails on Julia 1.14 nightly: HessenbergQ lmul! methods overwrite LinearAlgebra'sĐang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
JuliaLinearAlgebra/GenericLinearAlgebra.jl#183 · 1 bình luận ·
-
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 72/100
JuliaGPU/KernelAbstractions.jl#897 ·
Maintainer thường phản hồi trong vòng 1 ngày