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 differential_type, to test for non-differentiability #528

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
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/ChainRulesCore.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ export RuleConfig, HasReverseMode, NoReverseMode, HasForwardsMode, NoForwardsMod
export frule_via_ad, rrule_via_ad
# definition helper macros
export @non_differentiable, @opt_out, @scalar_rule, @thunk, @not_implemented
export ProjectTo, canonicalize, unthunk # tangent operations
export ProjectTo, differential_type, canonicalize, unthunk # tangent operations
export add!! # gradient accumulation operations
export ignore_derivatives, @ignore_derivatives
# tangents
Expand Down
41 changes: 40 additions & 1 deletion src/projection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ Base.propertynames(p::ProjectTo) = propertynames(backing(p))
backing(project::ProjectTo) = getfield(project, :info)

project_type(p::ProjectTo{T}) where {T} = T
project_type(::Type{<:ProjectTo{T}}) where {T} = T
project_type(_) = Any

function Base.show(io::IO, project::ProjectTo{T}) where {T}
print(io, "ProjectTo{")
Expand Down Expand Up @@ -150,7 +152,7 @@ end
ProjectTo(::Bool) = ProjectTo{NoTangent}() # same projector as ProjectTo(::AbstractZero) above

# Other never-differentiable types
for T in (:Symbol, :Char, :AbstractString, :RoundingMode, :IndexStyle)
for T in (:Symbol, :Char, :AbstractString, :RoundingMode, :IndexStyle, :Nothing)
@eval ProjectTo(::$T) = ProjectTo{NoTangent}()
end

Expand Down Expand Up @@ -601,3 +603,40 @@ function (project::ProjectTo{SparseMatrixCSC})(dx::SparseMatrixCSC)
invoke(project, Tuple{AbstractArray}, dx)
end
end

#####
##### A related utility which wants to live nearby
#####

"""
differential_type(x)
differential_type(typeof(x))

Testing `differential_type(x) <: AbstractZero` will tell you whether `x` is
known to be non-differentiable.

This relies on `ProjectTo(x)`, and the method accepting a type relies on type inference.
Thus it will not look inside abstractly typed containers such as `x = Any[true, false]`.

```jldoctest
julia> differential_type(true)
NoTangent

julia> differential_type(Int)
Float64

julia> x = Any[true, false];

julia> differential_type(x)
NoTangent

julia> differential_type(typeof(x))
Any
```
"""
differential_type(x) = project_type(ProjectTo(x))

function differential_type(::Type{T}) where {T}
PT = Base._return_type(ProjectTo, Tuple{T}) # might be Union{} if unstable
return isconcretetype(PT) ? project_type(PT) : Any
end
10 changes: 10 additions & 0 deletions test/projection.jl
Original file line number Diff line number Diff line change
Expand Up @@ -478,3 +478,13 @@ struct NoSuperType end
@test_broken 0 == @ballocated $psymm(dx) setup = (dx = Symmetric(rand(10^3, 10^3))) # 64
end
end

@testset "differential_type" begin
@test differential_type(true) == differential_type(Bool) == NoTangent
@test differential_type(1) == differential_type(Int) == Float64
tup = (false, :x, nothing)
@test differential_type(tup) == differential_type(typeof(tup)) == NoTangent

@test differential_type(NoSuperType()) == differential_type(NoSuperType) == Any
@test differential_type(Dual(1,2)) == differential_type(Dual) == Real
end