`tanh` rule in v1.74.11 fails on symbolic inputs ("non-boolean (Num) used in boolean context")
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 2/5
- 見積もり時間
- 1〜3時間
- 初心者へのやさしさ
- 72/100
- issue の種類
- バグ
- 明瞭さ
- 明確に書かれている
- 活発さ
- 活発
- 領域
- tooling
調査の方向性
失敗している tanh ルールは src/rulesets/Base/fastmath_able.jl で定義されており、その derivatives_given_output(42行目付近)は abs(real(x)) < 1 に対して三項演算子を使っています。まず Symbolics の @variables x と frule((NoTangent(), true), tanh, x) を使って再現コードを実行し、TypeError を確認してください。シンボリックな呼び出しが例外を投げなくなり、数値結果が変わらず、#864 で変更された他のルールについても同じ非 Bool 条件がないことを確認できれば完了です。
索引モデルが issue の本文から書いたものです。
説明
Since v1.74.11 (#864), the tanh rule picks between two formulas with a ternary:
@scalar_rule tanh(x) abs(real(x)) < 1 ? 1 - Ω ^ 2 : sech(x) ^ 2
The ternary needs a Bool. For a symbolic x (e.g. Symbolics.Num), abs(real(x)) < 1 is a symbolic expression, so frule / derivatives_given_output throws.
Reproducer
using ChainRules, ChainRulesCore, Symbolics
@variables x
frule((NoTangent(), true), tanh, x)
ERROR: TypeError: non-boolean (Symbolics.Num) used in boolean context
Stacktrace:
[1] derivatives_given_output(Ω::Num, ::typeof(tanh), x::Num)
@ ChainRules ~/.julia/packages/ChainRules/.../src/rulesets/Base/fastmath_able.jl:42
[2] frule(::Tuple{NoTangent, Bool}, ::typeof(tanh), x::Num)
@ ChainRules ~/.julia/packages/ChainRules/.../src/rulesets/Base/fastmath_able.jl:42
The stack trace is from a CI failure in which TaylorDiff.jl hit this while precompiling (log). The snippet above reconstructs that call and hasn't been run on its own.
Impact
TaylorDiff.jl (v0.3.5) evaluates ChainRules rules on Symbolics variables while it precompiles, so it now fails to precompile. That in turn breaks everything that depends on it, e.g. NonlinearSolveHomotopyContinuation and the ModelingToolkit HomotopyContinuation extension tests. We see this in downstream CI: v1.74.10 passes, v1.74.11 and later fail, and v1.74.13 still has the ternary.
Suggested fix
Use ifelse instead of the ternary. Symbolics and similar packages overload it for symbolic conditions:
@scalar_rule tanh(x) ifelse(abs(real(x)) < 1, 1 - Ω ^ 2, sech(x) ^ 2)
ifelse always computes both branches, which costs one extra sech for ordinary numbers, but gives the same result. The other rules changed in #864 may be worth checking for the same problem with non-Bool comparisons.
(Diagnosed with the help of Claude Code.)
- 主要言語
- Julia
- スター
- 479
- フォーク
- 100
- 平均マージ
- 1日 4時間
- マージ済み PR(30日)
- 14
環境構築
このプロジェクトには開発コンテナ、Dockerfile、コントリビューションガイドがありません。まず README を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
JuliaDiff/ChainRules.jl のほかの issue
-
`sum` rules do not match the signatures of the Base methods再び着手できるかも このイシューのプルリクエストはマージされずにクローズされました。 オープン
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
JuliaDiff/ChainRules.jl#868 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 4/5 3〜5日 初心者へのやさしさ 25/100
JuliaDiff/ChainRules.jl#863 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 55/100
JuliaDiff/ChainRules.jl#818 ·
メンテナーはふだん 1 日以内に返信
-
refactoring
難易度 4/5 3〜5日 初心者へのやさしさ 35/100
JuliaDiff/ChainRules.jl#815 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 3/5 1〜2日 初心者へのやさしさ 45/100
JuliaDiff/ChainRules.jl#810 ·
メンテナーはふだん 1 日以内に返信
JuliaDiff/ChainRules.jl の issue をすべて見る
似ている issue
-
難易度 2/5 1〜3時間 初心者へのやさしさ 76/100
TuringLang/Turing.jl#2917 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
メンテナーはふだん 2 日以内に返信
-
難易度 1/5 1〜3時間 初心者へのやさしさ 88/100
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 67/100
メンテナーはふだん 1 日以内に返信
-
documentation
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
メンテナーはふだん 1 日以内に返信