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

Add rrule for NamedTuple merge #795

Merged
merged 4 commits into from
Jun 3, 2024
Merged

Add rrule for NamedTuple merge #795

merged 4 commits into from
Jun 3, 2024

Conversation

oxinabox
Copy link
Member

Based on LuxDL/Lux.jl#659
but with some fixes up to use ChainRulesCore types.

Initial PR is not inferable.
I suspect i will need to rewrite this as a generated funcition to make it infer.

@avik-pal do you want to test this branch against your use case, with it deleted from Lux?

src/rulesets/Base/base.jl Outdated Show resolved Hide resolved
src/rulesets/Base/base.jl Outdated Show resolved Hide resolved
src/rulesets/Base/base.jl Outdated Show resolved Hide resolved
src/rulesets/Base/base.jl Outdated Show resolved Hide resolved
test/rulesets/Base/base.jl Outdated Show resolved Hide resolved
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@oxinabox
Copy link
Member Author

What i want is to do

_getproperty_kwexpr(key) = :($key = getproperty(dy, $key))
@generated function rrule(::typeof(merge), nt1::NamedTuple{F1}, nt2::NamedTuple{F2}) where {F1, F2}
    quote
        y = merge(nt1, nt2)
        function merge_pullback(dy)
            dnt1 = Tangent{typeof(nt1)}(; $(map(_getproperty_kwexpr, setdiff(F1, F2))...))
            dnt2 = Tangent{typeof(nt2)}(; $(map(_getproperty_kwexpr, F2)...))
            return (NoTangent(), dnt1, dnt2)
        end
        merge_pullback(dy::AbstractThunk) = merge_pullback(unthunk(dy))
        merge_pullback(x::AbstractZero) = (NoTangent(), x, x)
        
        return y, merge_pullback
    end
end

But generated functions are not allowed to have bodys that create closures

@oxinabox
Copy link
Member Author

Final solution was tricky to get to but clean

src/rulesets/Base/base.jl Outdated Show resolved Hide resolved
src/rulesets/Base/base.jl Outdated Show resolved Hide resolved
src/rulesets/Base/base.jl Outdated Show resolved Hide resolved
test/rulesets/Base/base.jl Outdated Show resolved Hide resolved
Co-authored-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com>
@oxinabox
Copy link
Member Author

@avik-pal can you review?

@avik-pal
Copy link

avik-pal commented Jun 1, 2024

Let me trigger the tests for SciMLSensitivity with this PR

@avik-pal
Copy link

avik-pal commented Jun 1, 2024

@oxinabox this should be good to go, downstream tests work

@oxinabox oxinabox merged commit fa7f884 into main Jun 3, 2024
7 of 11 checks passed
@oxinabox oxinabox deleted the ox/merge branch June 3, 2024 07:17
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.

2 participants