Repository navigation
Add frule and rrule for oftype on numbers - #877
Open
CarloLucibello wants to merge 1 commit into
Open
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:
|
This branch has not been deployed
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