Hacktoberfest 2026:メンテナが10月に向けて印を付けた、オープンで初心者向けの issue。 Hacktoberfest の issue を見る

Enable NaN-safe mode by default

オープン
#857 コメント 0 件 リアクション 1 件 担当者 0 名 GitHub で見る

@devmotion がすでに取り組んでいます。

2026年10月12日 から。

  • #858 @devmotion による — オープン
  • #859 @KristofferC による — オープン

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
25/100
issue の種類
機能追加
明瞭さ
おおむね明確
活発さ
活発
領域
backend

調査の方向性

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.

索引モデルが issue の本文から書いたものです。

説明

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 Real or a Dual with zero partials (see #856),
  • the chunk size: with the existing all-or-nothing check in the Dual^Real branch of ^, v[1]^0.5 + v[2] returns [Inf, 1.0] with Chunk{1} and [Inf, NaN] with Chunk{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).

主要言語
Julia
スター
1k
フォーク
162
PR マージ指標
30日以内にマージされた PR はありません

環境構築

このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

JuliaDiff/ForwardDiff.jl のほかの issue

JuliaDiff/ForwardDiff.jl の issue をすべて見る

似ている issue

Julia の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。