Skip to content

Commit 98cc02c

Browse files
committed
Define a bunch of zero_tangents that should just NoTangent
1 parent 0385ea8 commit 98cc02c

2 files changed

Lines changed: 20 additions & 3 deletions

File tree

src/tangent_types/abstract_zero.jl

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,8 +112,6 @@ function zero_tangent end
112112

113113
zero_tangent(x::Number) = zero(x)
114114

115-
zero_tangent(::Type) = NoTangent()
116-
117115
function zero_tangent(x::MutableTangent{P}) where {P}
118116
zb = backing(zero_tangent(backing(x)))
119117
return MutableTangent{P}(zb)
@@ -171,10 +169,24 @@ function zero_tangent(x::Array{P,N}) where {P,N}
171169
return y
172170
end
173171

172+
zero_tangent(::T) where {K,V,T<:AbstractDict{K,V}} = Tangent{T}(Dict{K, guess_zero_tangent_type(V)}())
173+
174174
# Sad heauristic methods we need because of unassigned values
175175
guess_zero_tangent_type(::Type{T}) where {T<:Number} = T
176176
guess_zero_tangent_type(::Type{T}) where {T<:Integer} = typeof(float(zero(T)))
177177
function guess_zero_tangent_type(::Type{<:Array{T,N}}) where {T,N}
178178
return Array{guess_zero_tangent_type(T),N}
179179
end
180-
guess_zero_tangent_type(T::Type) = Any
180+
guess_zero_tangent_type(T::Type) = Any
181+
182+
183+
# Stuff that conceptually has its own identity regardless of structual implementation and doesn't have a tangent
184+
zero_tangent(::Base.AbstractLogger) = NoTangent()
185+
186+
# Prevent zero_tangent going wild on the internals
187+
zero_tangent(::Type) = NoTangent()
188+
zero_tangent(::Expr) = NoTangent()
189+
zero_tangent(::Core.Compiler.AbstractInterpreter) = NoTangent()
190+
zero_tangent(::Core.Compiler.InstructionStream) = NoTangent()
191+
zero_tangent(::Core.CodeInfo) = NoTangent()
192+
zero_tangent(::Core.MethodInstance) = NoTangent()

test/tangent_types/abstract_zero.jl

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ end
183183

184184
@test zero_tangent((1.0, 2.0)) == Tangent{Tuple{Float64,Float64}}(0.0, 0.0)
185185

186+
@test zero_tangent(Dict{Int, Float64}(1=>2.4)) == Tangent{Dict{Int,Float64}}(Dict{Int, Float64}())
187+
188+
186189
# Higher order
187190
# StructuralTangents are valid tangents for themselves (just like Numbers)
188191
# and indeed we prefer that, otherwise higher order structural tangents are kinda
@@ -200,6 +203,8 @@ end
200203
@test iszero(zero_tangent(:abc))
201204
@test iszero(zero_tangent("abc"))
202205
@test iszero(zero_tangent(sin))
206+
207+
@test iszero(zero_tangent(:(1+1)))
203208
end
204209

205210
@testset "undef elements Vector" begin

0 commit comments

Comments
 (0)