Skip to content

support constructors with non_differentiable #243

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
merged 4 commits into from
Nov 20, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/rule_definition_tools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ macro non_differentiable(sig_expr)
primal_name, orig_args = Iterators.peel(sig_expr.args)

constrained_args = _constrain_and_name.(orig_args, :Any)
primal_sig_parts = [:(::typeof($primal_name)), constrained_args...]
primal_sig_parts = [:(::Core.Typeof($primal_name)), constrained_args...]

unconstrained_args = _unconstrain.(constrained_args)

Expand Down
7 changes: 4 additions & 3 deletions test/demos/forwarddiffzero.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ using Test
# Define the AD

# Note that we never directly define Dual Number Arithmetic on Dual numbers
# instead it is automatically defined from the `frules`
# instead it is automatically defined from the `frules`
struct Dual <: Real
primal::Float64
partial::Float64
Expand All @@ -30,7 +30,8 @@ Base.to_power_type(x::Dual) = x
function define_dual_overload(sig)
sig = Base.unwrap_unionall(sig) # Not really handling most UnionAlls
opT, argTs = Iterators.peel(sig.parameters)
fieldcount(opT) == 0 || return # not handling functors
opT isa Type{<:Type} && return # not handling constructors
fieldcount(opT) == 0 || return # not handling functors
all(Float64 <: argT for argT in argTs) || return # only handling purely Float64 ops.

N = length(sig.parameters) - 1 # skip the op
Expand Down Expand Up @@ -65,7 +66,7 @@ function ChainRulesCore.frule((_, Δx, Δy), ::typeof(*), x::Number, y::Number)
end

# Manual refresh needed as new rule added in same file as AD after the `on_new_rule` call
refresh_rules();
refresh_rules();

@testset "ForwardDiffZero" begin
foo(x) = x + x
Expand Down
5 changes: 3 additions & 2 deletions test/demos/reversediffzero.jl
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@ Base.to_power_type(x::Tracked) = x
function define_tracked_overload(sig)
sig = Base.unwrap_unionall(sig) # not really handling most UnionAll
opT, argTs = Iterators.peel(sig.parameters)
fieldcount(opT) == 0 || return # not handling functors
opT isa Type{<:Type} && return # not handling constructors
fieldcount(opT) == 0 || return # not handling functors
all(Float64 <: argT for argT in argTs) || return # only handling purely Float64 ops.

N = length(sig.parameters) - 1 # skip the op
Expand Down Expand Up @@ -116,7 +117,7 @@ function ChainRulesCore.rrule(::typeof(*), x::Number, y::Number)
end

# Manual refresh needed as new rule added in same file as AD after the `on_new_rule` call
refresh_rules();
refresh_rules();

@testset "ReversedDiffZero" begin
foo(x) = x + x
Expand Down
29 changes: 28 additions & 1 deletion test/rule_definition_tools.jl
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,14 @@ macro test_macro_throws(err_expr, expr)
end
end

# struct need to be defined outside of tests for julia 1.0 compat
struct NonDiffExample
x
end

struct NonDiffCounterExample
x
end

@testset "rule_definition_tools.jl" begin
@testset "@non_differentiable" begin
Expand Down Expand Up @@ -98,6 +106,25 @@ end
end
end

@testset "Constructors" begin
@non_differentiable NonDiffExample(::Any)

@test isequal(
frule((Zero(), 1.2), NonDiffExample, 2.0),
(NonDiffExample(2.0), DoesNotExist())
)

res, pullback = rrule(NonDiffExample, 2.0)
@test res == NonDiffExample(2.0)
@test pullback(1.2) == (NO_FIELDS, DoesNotExist())

# https://github.com/JuliaDiff/ChainRulesCore.jl/issues/213
# problem was that `@nondiff Foo(x)` was also defining rules for other types.
Copy link
Member

Choose a reason for hiding this comment

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

Wow, that's funky. I guess solves the issue in this comment?
JuliaDiff/ChainRules.jl#310 (comment)

Copy link
Member Author

Choose a reason for hiding this comment

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

Yep.
Its because typeof(Foo) == DataType and typeof(Bar) == DataType
but Core.Typeof(Foo) == Type{Foo} and Core.Typeof(Bar) == Type{Bar}

# make sure that isn't happenning
@test frule((Zero(), 1.2), NonDiffCounterExample, 2.0) === nothing
@test rrule(NonDiffCounterExample, 2.0) === nothing
end

@testset "Not supported (Yet)" begin
# Varargs are not supported
@test_macro_throws ErrorException @non_differentiable vararg1(xs...)
Expand All @@ -115,7 +142,7 @@ end
@testset "@scalar_rule with multiple output" begin
simo(x) = (x, 2x)
@scalar_rule(simo(x), 1f0, 2f0)

y, simo_pb = rrule(simo, π)

@test simo_pb((10f0, 20f0)) == (NO_FIELDS, 50f0)
Expand Down