-
Notifications
You must be signed in to change notification settings - Fork 65
Deprecate extern #365
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
Merged
Deprecate extern #365
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1 +1,53 @@ | ||
| Base.@deprecate_binding NO_FIELDS NoTangent() | ||
|
|
||
| const EXTERN_DEPRECATION = "`extern` is deprecated, use `unthunk` or `backing` instead, " * | ||
| "depending on the use case." | ||
|
|
||
| """ | ||
| extern(x) | ||
|
|
||
| Makes a best effort attempt to convert a differential into a primal value. | ||
| This is not always a well-defined operation. | ||
| For two reasons: | ||
| - It may not be possible to determine the primal type for a given differential. | ||
| For example, `Zero` is a valid differential for any primal. | ||
| - The primal type might not be a vector space, thus might not be a valid differential type. | ||
| For example, if the primal type is `DateTime`, it's not a valid differential type as two | ||
| `DateTime` can not be added (fun fact: `Milisecond` is a differential for `DateTime`). | ||
|
|
||
| Where it is defined the operation of `extern` for a primal type `P` should be | ||
| `extern(x) = zero(P) + x`. | ||
|
|
||
| !!! note | ||
| Because of its limitations, `extern` should only really be used for testing. | ||
| It can be useful, if you know what you are getting out, as it recursively removes | ||
| thunks, and otherwise makes outputs more consistent with finite differencing. | ||
|
|
||
| The more useful action in general is to call `+`, or in the case of a [`Thunk`](@ref) | ||
| to call [`unthunk`](@ref). | ||
|
|
||
| !!! warning | ||
| `extern` may return an alias (not necessarily a copy) to data | ||
| wrapped by `x`, such that mutating `extern(x)` might mutate `x` itself. | ||
| """ | ||
| @inline function extern(x) | ||
| Base.depwarn(EXTERN_DEPRECATION, :extern) | ||
| return x | ||
| end | ||
|
|
||
| extern(x::ZeroTangent) = (Base.depwarn(EXTERN_DEPRECATION, :extern); return false) # false is a strong 0. E.g. `false * NaN = 0.0` | ||
|
|
||
| function extern(x::NoTangent) | ||
| Base.depwarn(EXTERN_DEPRECATION, :extern) | ||
| throw(ArgumentError("Derivative does not exit. Cannot be converted to an external type.")) | ||
| end | ||
|
|
||
| extern(comp::Tangent) = (Base.depwarn(EXTERN_DEPRECATION, :extern); return backing(map(extern, comp))) # gives a NamedTuple or Tuple | ||
|
|
||
| extern(x::NotImplemented) = (Base.depwarn(EXTERN_DEPRECATION, :extern); throw(NotImplementedException(x))) | ||
|
|
||
| @inline extern(x::AbstractThunk) = (Base.depwarn(EXTERN_DEPRECATION, :extern); return extern(unthunk(x))) | ||
|
|
||
|
|
||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,24 @@ | ||
| @testset "NO_FIELDS" begin | ||
| @test (@test_deprecated NO_FIELDS) isa NoTangent | ||
| end | ||
|
|
||
| @testset "extern" begin | ||
| @test extern(@thunk(3)) == 3 | ||
| @test extern(@thunk(@thunk(3))) == 3 | ||
|
|
||
| @test extern(Tangent{Foo}(x=2.0)) == (;x=2.0) | ||
| @test extern(Tangent{Tuple{Float64,}}(2.0)) == (2.0,) | ||
| @test extern(Tangent{Dict}(Dict(4 => 3))) == Dict(4 => 3) | ||
|
|
||
| # with differentials on the inside | ||
| @test extern(Tangent{Foo}(x=@thunk(0+2.0))) == (;x=2.0) | ||
| @test extern(Tangent{Tuple{Float64,}}(@thunk(0+2.0))) == (2.0,) | ||
| @test extern(Tangent{Dict}(Dict(4 => @thunk(3)))) == Dict(4 => 3) | ||
|
|
||
| z = ZeroTangent() | ||
| @test extern(z) === false | ||
| dne = NoTangent() | ||
| @test_throws Exception extern(dne) | ||
| E = ChainRulesCore.NotImplementedException | ||
| @test_throws E extern(ni) | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I thought I asked this before, but if so github has eaten the reply.
(Probably i forget to ask)
Why are we using
Base.depwarnrather than@deprecate?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I think it's because we are not providing a definite alternative. I.e. it isn't always just
extern->unthunk, it could be usingbackingas wellThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
In general we are not.
But for any specific case they can make the change to put the RHS of the deprecation in the place of the
extern.which they might have to do recursively til it stops but still.
Though perhaps that is worse.