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

Write docs on how to write rules to run different computations at different times #46

Open
oxinabox opened this issue Sep 3, 2019 · 3 comments
Labels
documentation Improvements or additions to documentation

Comments

@oxinabox
Copy link
Member

oxinabox commented Sep 3, 2019

With #30
this might be a great example

function rrule(::typeof(/), A::AbstractVecOrMat{<:Real}, B::AbstractVecOrMat{<:Real})
    # Compute C (return value differently) to just calling `A/B`
    # so we have some parts precomputed 
    # that we need to use for computing the pullback

    Aᵀ, dA_pb = rrule(adjoint, A)
    Bᵀ, dB_pb = rrule(adjoint, B)
    Cᵀ, dS_pb = rrule(\, Bᵀ, Aᵀ)
    C, dC_pb = rrule(adjoint, Cᵀ)
    
    function slash_pullback(Ȳ)
         # These two run no matter which partial you want
         # but they don't run if you only want the result (C)

        _, dC = dC_pb(Ȳ)
        _, dAᵀ, dBᵀ = dS_pb(dC)

        # Each of these only run if you want that particular partial
        ∂A = @thunk last(dA_pb(dAᵀ))
        ∂B = @thunk last(dA_pb(dBᵀ))

        (NO_FIELDS, ∂A, ∂B)
    end
    return C, slash_pullback
end
@simeonschaub
Copy link
Member

If we replaced adjoint with transpose, I believe this should even be correct for complex input.

@oxinabox
Copy link
Member Author

oxinabox commented Sep 4, 2019

A better example would not call rrule internally,
and also would have more obvious work happenning in the thunk

@oxinabox oxinabox mentioned this issue Sep 4, 2019
@nickrobinson251
Copy link
Contributor

related: JuliaDiff/ChainRules.jl#103
but does not add a specific example like suggested here

@nickrobinson251 nickrobinson251 added the documentation Improvements or additions to documentation label Jan 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
documentation Improvements or additions to documentation
Projects
None yet
Development

No branches or pull requests

3 participants