Skip to content

Only promote TrackedReal with Real types, don't nest TrackedReals - #175

Open
devmotion wants to merge 2 commits into
FluxML:masterfrom
devmotion:dmw/fix-promote-rule
Open

devmotion wants to merge 2 commits into
FluxML:masterfrom
devmotion:dmw/fix-promote-rule

Conversation

@devmotion

Copy link
Copy Markdown
Member

Base.promote_rule(::Type{TrackedReal{S}}, ::Type{T}) where {S,T} accepts any T. Promoting with a non-Real type therefore builds TrackedReal{promote_type(S,T)}, which throws a TypeError because TrackedReal requires T<:Real:

julia> [Tracker.param(1.0), missing]   # also `nothing`, `Expr`, ...
ERROR: TypeError: in TrackedReal, in T, expected T<:Real, got Type{Union{Missing, Float64}}

Julia 1.13's Test hits this inside @test (vect(::Expr, args...)), which breaks the rand testset and DiffRules' downstream Tracker CI.

Changes

  • Restrict the rule to T<:Real. Other types now fall back to the default promotion (Union{Missing, TrackedReal{Float64}}, Any, ...) instead of throwing.
  • Add an explicit rule for two TrackedReals. It returns TrackedReal{promote_type(S,T)} rather than nesting TrackedReal{TrackedReal{...}}. Tracker has no tags or levels, so all orders of differentiation are recorded on a single graph and nesting would be wrong.
  • Add a promotion testset.

Tests

On Julia 1.13, 422 pass and 4 are broken (the existing conv ones). Before the change, 409 passed and 6 errored.

🤖 Generated with Claude Code

Restrict promote_rule(TrackedReal{S}, T) to T <: Real. For other types
(Missing, Nothing, Expr, ...) promote_type(S, T) is not a Real, so the rule
constructed an invalid TrackedReal{...} and threw a TypeError, e.g. for
[param(1.0), missing] or in Test's @test on Julia 1.13.

Add a rule for two TrackedReals: previously TrackedReal{Float32} and
TrackedReal{Float64} promoted to TrackedReal{TrackedReal{Float64}}, although
Tracker records all levels of differentiation on one graph.

Co-Authored-By: Claude Opus 5.5 <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant