Repository navigation
Add frule and rrule for oftype on numbers - #877
Closed
CarloLucibello wants to merge 1 commit into
Closed
CarloLucibello wants to merge 1 commit into
CarloLucibello wants to merge 1 commit into
Conversation
Base's `oftype` branches on `y isa typeof(x)`, so Zygote's pullback of any function calling it is not inferred (FluxML/Zygote.jl#1751). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
| test_rrule(oftype, x, y; rtol=1.0e-3, atol=1.0e-3) | ||
| end | ||
| @test rrule(oftype, 1.0, 2)[2](3.0) === (NoTangent(), ZeroTangent(), 3.0) | ||
| @test rrule(oftype, 1.0, 2.0 + 0im)[2](3.0) === (NoTangent(), ZeroTangent(), 3.0 + 0im) |
Contributor
There was a problem hiding this comment.
[JuliaFormatter] reported by reviewdog 🐶
Suggested change
| @test rrule(oftype, 1.0, 2.0 + 0im)[2](3.0) === (NoTangent(), ZeroTangent(), 3.0 + 0im) | |
| @test rrule(oftype, 1.0, 2.0 + 0im)[2](3.0) === | |
| (NoTangent(), ZeroTangent(), 3.0 + 0im) |
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #877 +/- ##
=======================================
Coverage 96.65% 96.65%
=======================================
Files 28 28
Lines 3615 3620 +5
=======================================
+ Hits 3494 3499 +5
Misses 121 121 ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
devmotion
reviewed
Nov 22, 2026
devmotion
left a comment
Member
There was a problem hiding this comment.
Thanks! I'm not convinced this belongs in ChainRules, though:
- The motivation is entirely Zygote-specific:
oftypeis differentiated fine by other ADs, and the slowdown comes from Zygote's handling of functions with multiple basic blocks (FluxML/Zygote.jl#1736). Any other branching Base function hits the same issue (FluxML/Zygote.jl#1751 mentions a second construct in the same kernel), so fixing these one-by-one in ChainRules doesn't scale. - Zygote already has the exact analogue for
convertas a Zygote-only rule:rrule(::ZygoteRuleConfig, ::typeof(convert), T::Type{<:Real}, x::Real)insrc/lib/number.jl. Anoftyperule restricted toZygoteRuleConfigright next to it seems like the natural home. - The
Numbersignature asserts thatoftypehas derivative one for every number type, which isn't true for e.g. Unitful quantities:oftype(1.0u"m", 2.0u"cm") == 0.02u"m". Currently Zygote handles this correctly (gradient(y -> ustrip(oftype(1.0u"m", y * u"cm")), 2.0) == (0.01,)), but with the rule from this PR the same call throwsZygote'sMethodError: no method matching (::ProjectTo{Number, @NamedTuple{}})(::Tangent{Any, @NamedTuple{val::Float64}})convertrule is restricted toReal; the same restriction (orUnion{Real,Complex}) would be needed here. - The
fruleisn't motivated by the issue at all.
So I'd suggest moving this to Zygote as rrule(::ZygoteRuleConfig, ::typeof(oftype), x::Real, y::Real) (plus Complex if needed) and closing this PR.
Member
Author
|
ok, seems reasonable, thanks for the thorought explanation |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Base.oftype(x, y) = y isa typeof(x) ? y : convert(typeof(x), y)branches, so in Zygote the pullback of any function that calls it is not inferred and gets much slower. FluxML/Zygote.jl#1751 has the benchmarks.This adds rules for
oftype(::Number, ::Number). The cotangent is projected ontoy, and the frule's tangent ontox. With the rule, Zygote infers the pullback off(x, y) = sin(x) * cos(y) + x * oftype(x, y)and its gradient allocates nothing (1760 bytes before), the same as withconvert(typeof(x), y).Fixes FluxML/Zygote.jl#1751
🧔♂️🤖 Assisted by AI