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

Đang mở Phù hợp với người mới
#871 2 bình luận 0 reaction 0 người được giao Xem trên GitHub

Maintainer thường phản hồi trong vòng 1 ngày

Chưa có ai nhận issue này.

Đánh giá

Độ khó
2/5
Thời gian dự kiến
1-3 giờ
Mức phù hợp với người mới
72/100
Loại issue
Lỗi
Độ rõ ràng
Đặc tả rõ ràng
Mức độ hoạt động
Sôi nổi
Lĩnh vực
tooling

Hướng nghiên cứu

Quy tắc tanh đang lỗi được định nghĩa trong src/rulesets/Base/fastmath_able.jl, nơi derivatives_given_output (khoảng dòng 42) dùng toán tử ba ngôi trên abs(real(x)) < 1. Hãy bắt đầu bằng cách chạy reproducer với @variables x của Symbolics và frule((NoTangent(), true), tanh, x) để xác nhận TypeError. Hoàn thành khi lời gọi symbolic không còn ném lỗi, kết quả số giữ nguyên, và các quy tắc khác đã thay đổi trong #864 đã được kiểm tra xem có cùng điều kiện không phải Bool hay không.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Mô tả

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

Ngôn ngữ chính
Julia
Star
479
Fork
100
Merge trung bình
1 ngày 4 giờ
Pull request đã merge (30 ngày)
14

Chuẩn bị môi trường

Dự án này không cung cấp dev container, Dockerfile hay hướng dẫn đóng góp, nên bạn cần tự thiết lập môi trường: hãy bắt đầu từ README và xem hướng dẫn đóng góp lần đầu của chúng tôi để biết các bước chung.

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Issue khác của JuliaDiff/ChainRules.jl

Tất cả issue của JuliaDiff/ChainRules.jl

Issue tương tự

Thêm issue về Julia

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.