`tanh` rule in v1.74.11 fails on symbolic inputs ("non-boolean (Num) used in boolean context")

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

メンテナーはふだん 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 を読み、一般的な手順ははじめてのコントリビューションガイドを参照してください。

はじめの一歩

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

JuliaDiff/ChainRules.jl のほかの issue

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

似ている issue

Julia の issue をもっと見る

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

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