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

Small tweaks for type stability / inferrability issues #122

Merged
merged 4 commits into from
Apr 15, 2024
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
15 changes: 12 additions & 3 deletions src/prelude.jl
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,9 @@ function structural_iterate(args)
exclude)
return iter
end
structural_iterate(args::NTuple{N, Union{Real, AbstractFIs}}) where {N} = args
structural_iterate(args::AbstractArray{T}) where {T <: Union{Real, AbstractFIs}} = args
structural_iterate(args::T) where {T <: Real} = (args,)

"""
structural_map(f, args)
Expand All @@ -46,9 +49,15 @@ often to be used on a function's input/output arguments.
Currently uses [fmap](https://fluxml.ai/Functors.jl/stable/api/#Functors.fmap)
from Functors.jl as a backend.
"""
function structural_map(f, args...; only_vals = Val{false}())
function structural_map(f, args...; only_vals = nothing)
walk = if only_vals isa Val{true}
Functors.StructuralWalk()
elseif (only_vals isa Val{false}) || isnothing(only_vals)
Functors.DefaultWalk()
else
error("Unsupported argument only_vals = $only_vals")
end
fmap((args...) -> args[1] isa AbstractArray ? f.(args...) : f(args...), args...;
cache = nothing,
walk = (only_vals isa Val{true} ? Functors.StructuralWalk() :
Functors.DefaultWalk()))
walk)
end
10 changes: 8 additions & 2 deletions src/propagate.jl
Original file line number Diff line number Diff line change
Expand Up @@ -23,13 +23,18 @@ function get_Δs(arg, FIs)
end
end

function strip_Δs(arg)
function strip_Δs(arg; use_dual = Val(true))
if arg isa StochasticTriple
# TODO: replace check below with a more robust notion of discreteness.
if valtype(arg) <: Integer
return value(arg)
else
return ForwardDiff.Dual{tag(arg)}(value(arg), delta(arg))
if use_dual isa Val{true}
return ForwardDiff.Dual{tag(arg)}(value(arg), delta(arg))
else
return StochasticAD.StochasticTriple{tag(arg)}(
value(arg), delta(arg), empty(arg.Δs))
end
end
else
return arg
Expand Down Expand Up @@ -122,6 +127,7 @@ function propagate(f,
=#
out = f(input_args...)
val = structural_map(value, out)
# TODO: what does the only_vals do in the below and why?
Δs_all = structural_map(Base.Fix2(get_Δs, backendtype(st_rep)), args;
only_vals = Val{true}())
# TODO: Coupling approach below needs to handle non-perturbable objects.
Expand Down
8 changes: 6 additions & 2 deletions src/stochastic_triple.jl
Original file line number Diff line number Diff line change
Expand Up @@ -96,17 +96,21 @@ tag(::ForwardDiff.Dual{T}) where {T} = T

"""
valtype(st::StochasticTriple)
valtype(st::Type{<:StochasticTriple})

Get the underlying type of the value tracked by a stochastic triple.
"""
valtype(::StochasticTriple{T, V}) where {T, V} = V
valtype(st::StochasticTriple) = valtype(typeof(st))
valtype(::Type{<:StochasticTriple{T, V}}) where {T, V} = V

"""
backendtype(st::StochasticTriple)
backendtype(st::Type{<:StochasticTriple})

Get the backend type of a stochastic triple.
"""
backendtype(::StochasticTriple{T, V, FIs}) where {T, V, FIs} = FIs
backendtype(st::StochasticTriple) = backendtype(typeof(st))
backendtype(::Type{<:StochasticTriple{T, V, FIs}}) where {T, V, FIs} = FIs

"""
smooth_triple(st::StochasticTriple)
Expand Down
Loading