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

Use weak dependencies if supported #68

Merged
merged 16 commits into from
Mar 2, 2023
Merged

Use weak dependencies if supported #68

merged 16 commits into from
Mar 2, 2023

Conversation

devmotion
Copy link
Member

@devmotion devmotion commented Dec 27, 2022

This PR replaces the use of Requires with weak dependencies on Julia >= 1.9 (https://pkgdocs.julialang.org/dev/creating-packages/#Weak-dependencies).

Currently the PR is breaking since it removes the AD alias and export. I couldn't get the Requires/weak dependencies submodules working with the AD alias on a first try (I tried different combinations of using AbstractDifferentiation: AD, using AbstractDifferentiation: AbstractDifferentiation, using AD: AD etc.). I'm sure there's some way to fix it but I did not spend more time on it since

  • my impression was it is not really needed anyways due to the "renaming with as" feature in Julia >= 1.6,
  • the problems seemed to indicate its not an intended/common use case (actually, the only where I've seen this pattern is AbstractDifferentiation - e.g., FiniteDifferences only mentions and uses the alias FDM in the docs but does not define it in the package itself)

Edit: Shouldn't be merged until the Registrator deployment of JuliaRegistries/Registrator.jl#397 is done.

Edit: A few additional changes (see below):

@codecov-commenter
Copy link

codecov-commenter commented Dec 27, 2022

Codecov Report

Patch coverage: 91.96% and project coverage change: +0.54 🎉

Comparison is base (b929457) 83.82% compared to head (9db3b75) 84.36%.

Additional details and impacted files
@@            Coverage Diff             @@
##           master      #68      +/-   ##
==========================================
+ Coverage   83.82%   84.36%   +0.54%     
==========================================
  Files           6        8       +2     
  Lines         476      467       -9     
==========================================
- Hits          399      394       -5     
+ Misses         77       73       -4     
Impacted Files Coverage Δ
src/AbstractDifferentiation.jl 79.66% <64.00%> (+0.58%) ⬆️
ext/AbstractDifferentiationChainRulesCoreExt.jl 100.00% <100.00%> (ø)
ext/AbstractDifferentiationFiniteDifferencesExt.jl 100.00% <100.00%> (ø)
ext/AbstractDifferentiationForwardDiffExt.jl 100.00% <100.00%> (ø)
ext/AbstractDifferentiationReverseDiffExt.jl 100.00% <100.00%> (ø)
ext/AbstractDifferentiationTrackerExt.jl 100.00% <100.00%> (ø)
ext/AbstractDifferentiationZygoteExt.jl 100.00% <100.00%> (ø)
src/backends.jl 100.00% <100.00%> (ø)

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

☔ View full report at Codecov.
📢 Do you have feedback about the report comment? Let us know in this issue.

@mohamed82008
Copy link
Member

This is a great PR, thanks! I need to study this feature before I can review the PR. But if someone else approves the PR, I am happy to merge.

@devmotion
Copy link
Member Author

Maybe in addition to the Pkg docs linked above, it could be interesting to check Kristoffer's PR to PGFPlotsX: KristofferC/PGFPlotsX.jl#306

@devmotion devmotion marked this pull request as draft December 30, 2022 14:14
@devmotion devmotion marked this pull request as ready for review January 29, 2023 01:33
@ChrisRackauckas
Copy link
Member

Can you quasi-namespace the extensions? I.e. AbstractDifferentiationForwardDiffExt? Otherwise you'll get collisions.

@require FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000" include("finitedifferences.jl")
@require Tracker = "9f7883ad-71c0-57eb-9f7f-b5c9e6d3789c" include("tracker.jl")
@require Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" begin
@static if !EXTENSIONS_SUPPORTED
Copy link
Member

Choose a reason for hiding this comment

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

can move this out of the init definition

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 tried many different versions and this was the (only?) one that seemed to work. But I'll give it another shot.

Copy link
Member

Choose a reason for hiding this comment

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

Copy link
Member Author

Choose a reason for hiding this comment

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

Ah, I think the problem was that I had put using Requires in the same check (outside of ìnit) which does not work. Having to if statements seems to work though.

Copy link
Member Author

@devmotion devmotion Feb 20, 2023

Choose a reason for hiding this comment

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

@static if VERSION >= v"1.6"
ZygoteBackend() = ReverseRuleConfigBackend(Zygote.ZygoteRuleConfig())
@require Zygote = "e88e6eb3-aa80-5325-afca-941959d7151f" include("../ext/ZygoteExt.jl")
Copy link
Member

Choose a reason for hiding this comment

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

why is this Zygote one versioned off?

Copy link
Member

Choose a reason for hiding this comment

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

For simplicity, just bump to LTS?

@devmotion
Copy link
Member Author

Can you quasi-namespace the extensions? I.e. AbstractDifferentiationForwardDiffExt? Otherwise you'll get collisions.

Sure, I've seen this discussion in other places and Kristoffer said that probably it would be reasonable even if the collision issues are fixed upstream. It's a bit hacky though IMO since just adding a prefix does not guarantee that there won't be any collisions - e.g., an extension such as ForwardDiffChainRulesStaticArraysExt could be both an extension of ForwardDiff with weak dependencies ChainRules and StaticArrays or an extension of ForwardDiffChainRules with weak dependency StaticArrays. In any case, I'll update the PR 🙂

@devmotion
Copy link
Member Author

Well, well. Updating to import AbstractDifferentiation as AD in the extensions revealed that the macros for defining pullbacks etc. have to be fixed: They implicitly assume that AbstractDifferentiation is available in the current scope (under this name) and they can't deal with arguments without names etc. So it seems this PR will get quite big, and possibly one even wants to fix a few things on the master branch.

@devmotion
Copy link
Member Author

To summarize the last commits:

The PR is ready for a thorough review and updated with the current practices for defining extensions.

module AbstractDifferentiationChainRulesCoreExt

import AbstractDifferentiation as AD
using ChainRulesCore: ChainRulesCore
Copy link
Member

Choose a reason for hiding this comment

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

This is not being imported using the ..ChainRulesCore that's needed for Requires

Copy link
Member Author

Choose a reason for hiding this comment

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

It's not loaded with Requires.

Comment on lines +628 to +629
using Requires: @require
include("../ext/AbstractDifferentiationChainRulesCoreExt.jl")
Copy link
Member

Choose a reason for hiding this comment

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

I see, because there's no backwards compat to do? It's a bit odd but fine I guess.

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 followed the same pattern as in other packages: pre-Julia 1.9 ChainRulesCore is a proper dependency (no Requires involved) whereas in >= 1.9 it's an extension.

Copy link
Member

@ChrisRackauckas ChrisRackauckas left a comment

Choose a reason for hiding this comment

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

This looks correct.

@ChrisRackauckas ChrisRackauckas merged commit 19ce815 into master Mar 2, 2023
@ChrisRackauckas ChrisRackauckas deleted the dw/weakdeps branch March 2, 2023 08:12
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.

5 participants