Skip to content

Fails to precompile with ChainRules 1.74.11: non-boolean Symbolics.Num in the tanh rule #113

Closed
SciML/OrdinaryDiffEq.jl
#4807

Description

@rcalxrc08

TaylorDiff 0.3.5 fails to precompile with ChainRules 1.74.11, the latest release:

julia> import Pkg; Pkg.activate(temp = true)

julia> Pkg.add("TaylorDiff"); Pkg.add(name = "ChainRules", version = "1.74.11")

julia> using TaylorDiff
ERROR: LoadError: TypeError: non-boolean (Symbolics.Num) used in boolean context
A symbolic expression appeared in a Boolean context. [...]
  @ ChainRules ~/.julia/packages/ChainRules/.../src/rulesets/Base/fastmath_able.jl:42
  @ TaylorDiff ~/.julia/packages/TaylorDiff/.../src/utils.jl:56

With ChainRules 1.74.10 it loads fine.

Cause

get_term_raiser in src/utils.jl evaluates the frule of each scalar function on the symbolic variable COMMON_Z while the package is being precompiled. ChainRules 1.74.11 (JuliaDiff/ChainRules.jl#864) changed the derivative of tanh to

@scalar_rule tanh(x) abs(real(x)) < 1 ? 1 - Ω ^ 2 : sech(x) ^ 2

to avoid cancellation for large |x|. The ternary needs a Bool, and abs(real(z)) < 1 on a Symbolics.Num is symbolic, hence the error. Any rule that branches on its argument would do the same.

Possible fixes: skip (or special-case) functions whose frule cannot be evaluated symbolically, for instance by catching the error in get_term_raiser and falling back to the generic path, or define the tanh recurrence directly from 1 - tanh², which is exact for truncated series.

Versions

TaylorDiff 0.3.5, ChainRules 1.74.11, Symbolics 7.48.0, SymbolicUtils 4.55.0, Julia 1.10 and 1.13 (Linux and Windows).

Found in the CI of FinancialToolbox.jl, which caps ChainRules at 1.74.10 in its test environment in the meantime.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions