Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Update rrules so that we can support Yota #85

Merged
merged 3 commits into from
Jul 10, 2022
Merged

Update rrules so that we can support Yota #85

merged 3 commits into from
Jul 10, 2022

Conversation

avik-pal
Copy link
Member

Note that this is still not enough. Some additional patches are still needed (mostly upstream?)

function Base.:+(t::ChainRulesCore.Tangent, nt::NamedTuple)
    return merge(ChainRulesCore.backing(t), nt)
end

function Yota.bcast_rrule(rc::Yota.YotaRuleConfig, ::typeof(Base.Broadcast.broadcasted),
                          f::typeof(sqrt), args...; kw...)
    ys, pbs = Yota.unzip(ChainRulesCore.rrule.(rc, f, args...; kw...))
    function pullback(Δ)
        if Δ isa ChainRulesCore.NoTangent || Δ isa ChainRulesCore.ZeroTangent
            return (ChainRulesCore.NoTangent(), [Δ for _ in 1:(length(pbs) + 1)]...)
        end
        Δ = ChainRulesCore.unthunk(Δ)
        dxs = broadcast((pb, Δ) -> pb(Δ), pbs, Δ) |> Yota.unzip
        dxs = [all(dx .== ChainRulesCore.NoTangent()) ? ChainRulesCore.NoTangent() : dx
               for dx in dxs]
        return (ChainRulesCore.NoTangent(), dxs...)
    end
    return ys, pullback
end

function Yota.ungetindex(x::NamedTuple, dy, I...)
    dx = map(1:length(x)) do i
        return i in I ? dy : ChainRulesCore.ZeroTangent()
    end
    return NamedTuple{keys(x)}(dx)
end

Base.zero(::Val{T}) where {T} = Val(zero(T))
Base.zero(::Nothing) = nothing
Base.zero(nt::NamedTuple) = Functors.fmap(zero, nt)

@avik-pal avik-pal force-pushed the ap/yota branch 2 times, most recently from 38da981 to 6267792 Compare July 10, 2022 00:38
@codecov
Copy link

codecov bot commented Jul 10, 2022

Codecov Report

Merging #85 (31cf81d) into main (f7ba291) will increase coverage by 5.03%.
The diff coverage is 86.79%.

@@            Coverage Diff             @@
##             main      #85      +/-   ##
==========================================
+ Coverage   71.18%   76.21%   +5.03%     
==========================================
  Files          14       14              
  Lines         989     1026      +37     
==========================================
+ Hits          704      782      +78     
+ Misses        285      244      -41     
Impacted Files Coverage Δ
src/core.jl 100.00% <ø> (ø)
src/autodiff.jl 65.82% <79.41%> (+9.30%) ⬆️
src/layers/normalize.jl 86.77% <100.00%> (+35.49%) ⬆️
src/nnlib.jl 64.86% <100.00%> (+1.80%) ⬆️
src/layers/basic.jl 62.63% <0.00%> (+2.63%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update f7ba291...31cf81d. Read the comment docs.

@avik-pal avik-pal merged commit f4f9871 into main Jul 10, 2022
@avik-pal avik-pal deleted the ap/yota branch September 18, 2022 23:20
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant