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

Aperta Adatta ai principianti
#871 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

I maintainer di solito rispondono entro 1 giorno

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
2/5
Tempo stimato
1-3 ore
Idoneità per principianti
72/100
Tipo di issue
Bug
Chiarezza
Specificata chiaramente
Stato di attività
Attiva
Ambito
tooling

Direzione di ricerca

La regola tanh che fallisce è definita in src/rulesets/Base/fastmath_able.jl, dove derivatives_given_output (intorno alla riga 42) usa un operatore ternario su abs(real(x)) < 1. Inizia eseguendo il reproducer con @variables x di Symbolics e frule((NoTangent(), true), tanh, x) per confermare il TypeError. È fatto quando la chiamata simbolica non genera più errori, i risultati numerici restano invariati e le altre regole modificate in #864 sono state verificate per la stessa condizione non Bool.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

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.)

Lingua principale
Julia
Stelle
479
Fork
100
Merge medio
1g 4h
PR unite (30g)
14

Preparare l'ambiente

Questo progetto non fornisce container di sviluppo, Dockerfile né guida per i contributori, quindi l'ambiente è a tuo carico: parti dal suo README e consulta la nostra guida al primo contributo per i passaggi generali.

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di JuliaDiff/ChainRules.jl

Tutte le issue di JuliaDiff/ChainRules.jl

Issue simili

Altre issue su Julia

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.