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 weak dependency on ChainRulesCore #246

Merged
merged 8 commits into from
Oct 5, 2023
Merged

Add weak dependency on ChainRulesCore #246

merged 8 commits into from
Oct 5, 2023

Conversation

devmotion
Copy link
Member

@devmotion devmotion commented Jan 31, 2023

This PR proposes to add a weak dependency on ChainRulesCore on Julia versions with support for extensions (i.e., Julia >= 1.9).

This would fix #172 (and FluxML/Zygote.jl#1365) without affecting load and compilation times for users that do not load ChainRulesCore.

The initial set of reverse-mode rules in this PR covers the existing rules in Zygote and hence could replace these in Julia >= 1.9: Zygote could, e.g., wrap the Requires-block for Distances in a if !isdefined(Base, :get_extension) conditional.
The implementation in this PR is different from the code in FluxML/Zygote.jl#923 since tests fail with the rules in the Zygote PR (e.g., due to type inference issues).
The rules in this PR are also all explicit and do not call back into the AD system, in contrast to the current implementation in Zygote.

IMO there are two main motivations for moving the rules to Distances.jl: It allows other ChainRules-compatible AD systems to use them as well without having to depend on Zygote and it fixes precompilation issues reported in FluxML/Zygote.jl#1365. The second point could be fixed also by making Distances a weak dependency of Zygote but that would not solve the first problem.

I know that maintainers of Distances have tried to keep the package lightweight and to not add any dependencies that would increase load times. However, I think the support for weak dependencies in Pkg addresses these concerns. In a few other packages ChainRules-definitions were already moved to extensions recently, e.g., JuliaMath/ChangesOfVariables.jl#12 and JuliaStats/LogExpFunctions.jl#62.

@codecov-commenter
Copy link

codecov-commenter commented Jan 31, 2023

Codecov Report

All modified lines are covered by tests ✅

Files Coverage Δ
ext/DistancesChainRulesCoreExt.jl 100.00% <100.00%> (ø)

... and 1 file with indirect coverage changes

📢 Thoughts on this report? Let us know!.

@KristofferC
Copy link
Member

With extensions I think it is fine to add code like this even to light weight packages. I do think that JuliaStats/LogExpFunctions.jl#64 should be taken into account.

@simsurace
Copy link

Is there anything blocking this?

@simsurace
Copy link

@KristofferC, could this be merged?

@devmotion
Copy link
Member Author

Bump 🙂

@devmotion devmotion changed the title RFC: Add weak dependency on ChainRulesCore Add weak dependency on ChainRulesCore Jun 30, 2023
Copy link
Member

@nalimilan nalimilan left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks fine regarding the extension machinery AFAICT, but can somebody qualified review the rules themselves?


import ChainRulesCore

const CRC = ChainRulesCore
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not do import ChainRulesCore: rrule and using ChainRulesCore: RuleConfig, ... instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure, it was a while ago. I generally prefer explicit namespace declarations over import statements, but I assume I didn't want to write ChainRulesCore all the time and therefore added the alias (which I have seen in other packages and issues as well).

ext/DistancesChainRulesCoreExt.jl Outdated Show resolved Hide resolved
@devmotion
Copy link
Member Author

can somebody qualified review the rules themselves?

I wonder if maybe @sethaxen or @willtebbutt (who contributed to the rules in Zygote multiple times) would have time to check the rules?

Copy link

@willtebbutt willtebbutt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this broadly looks good.

Would you consider incorporating tests similar to those found here: https://github.com/FluxML/Zygote.jl/blob/29fa32a688fb4454d2ee81b9ba2a6484e4468bda/test/gradcheck.jl#L1212 ?

IIRC they came up in the context of some GP stuff, so it would be great to ensure that we don't regress.

@simsurace
Copy link

Bump!

@devmotion
Copy link
Member Author

I'll return to the PR after my vacation, the PR is high up on my todo-list 🙂

@devmotion
Copy link
Member Author

I added tests with repeated columns: 2e8563a Is this what you had in mind @willtebbutt?

@devmotion
Copy link
Member Author

Bump 🙂

Copy link

@willtebbutt willtebbutt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is great -- sorry for the delay, this PR got lost in my inbox. Please merge when you're happy :)

@devmotion
Copy link
Member Author

Good to go, @nalimilan?

test/runtests.jl Outdated Show resolved Hide resolved
@devmotion devmotion merged commit 6d0110d into master Oct 5, 2023
8 of 9 checks passed
@devmotion devmotion deleted the dw/chainrules branch October 5, 2023 22:27
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.

Distances + ChainRules
6 participants