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

Improve Small-Union Performance #85

Merged
merged 5 commits into from
Feb 22, 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
1 change: 1 addition & 0 deletions src/codual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ Shorthand for `CoDual{P, tangent_type(P}}` when `P` is concrete, equal to `CoDua
"""
function codual_type(::Type{P}) where {P}
P == DataType && return CoDual
P isa Union && return Union{codual_type(P.a), codual_type(P.b)}
return isconcretetype(P) ? CoDual{P, tangent_type(P)} : CoDual
end

Expand Down
25 changes: 16 additions & 9 deletions src/interpreter/reverse_mode_ad.jl
Original file line number Diff line number Diff line change
Expand Up @@ -11,14 +11,20 @@ const BwdsInst = Core.OpaqueClosure{Tuple{Int}, Int}

const RuleSlot{V} = Union{SlotRef{V}, ConstSlot{V}} where {V<:Tuple{CoDual, Ref}}

primal_type(::AbstractSlot{<:Tuple{<:CoDual{P}, <:Any}}) where {P} = @isdefined(P) ? P : Any
primal_type(::AbstractSlot{<:Tuple{<:CoDual, <:Any}}) = Any
__primal_type(::Type{<:Tuple{<:CoDual{P}, <:Any}}) where {P} = @isdefined(P) ? P : Any
function __primal_type(::Type{P}) where {P<:Tuple{<:CoDual, <:Any}}
P isa Union && return Union{__primal_type(P.a), __primal_type(P.b)}
return Any
end

function make_rule_slot(::SlotRef{P}, ::Any) where {P}
return SlotRef{Tuple{codual_type(P), tangent_ref_type_ub(P)}}()
primal_type(::AbstractSlot{P}) where {P} = __primal_type(P)

function rule_slot_type(::Type{P}) where {P}
return Tuple{codual_type(P), tangent_ref_type_ub(P)}
end
function make_rule_slot(::SlotRef{P}, ::PhiNode) where {P}
return SlotRef{Tuple{codual_type(P), tangent_ref_type_ub(P)}}()

function make_rule_slot(::SlotRef{P}, ::Any) where {P}
return SlotRef{rule_slot_type(P)}()
end
function make_rule_slot(x::ConstSlot{P}, ::Any) where {P}
cd = uninit_codual(x[])
Expand Down Expand Up @@ -112,17 +118,18 @@ function build_coinsts(
tangent_stack_stack = make_tangent_ref_stack(tangent_ref_type_ub(primal_type(val)))

make_fwds(v) = R(primal(v), tangent(v))
fwds_inst = @opaque function (p::Int)
function fwds_run()
v, tangent_stack = val[]
push!(my_tangent_stack, tangent(v))
push!(tangent_stack_stack, tangent_stack)
ret[] = (make_fwds(v), top_ref(my_tangent_stack))
return next_blk
end
bwds_inst = @opaque function (j::Int)
fwds_inst = @opaque (p::Int) -> fwds_run()
function bwds_run()
increment_ref!(pop!(tangent_stack_stack), pop!(my_tangent_stack))
return j
end
bwds_inst = @opaque (j::Int) -> (bwds_run(); return j)
return fwds_inst::FwdsInst, bwds_inst::BwdsInst
end

Expand Down
3 changes: 1 addition & 2 deletions src/tangents.jl
Original file line number Diff line number Diff line change
Expand Up @@ -212,8 +212,7 @@ end
))

# If the type is a Union, then take the union type of its arguments.
# P isa Union && return Union{tangent_type(P.a), tangent_type(P.b)}
P isa Union && return Any
P isa Union && return Union{tangent_type(P.a), tangent_type(P.b)}

# If the type is itself abstract, it's tangent could be anything.
# The same goes for if the type has any undetermined type parameters.
Expand Down
10 changes: 10 additions & 0 deletions src/test_utils.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1412,6 +1412,15 @@ end
return @inbounds x[1] + test_recursive_sum(x[2:end])
end

# Copied over from https://github.com/TuringLang/Turing.jl/issues/1140
function _sum(x)
z = 0
for i in eachindex(x)
z += x[i]
end
return z
end

function generate_test_functions()
return Any[
(false, :allocs, nothing, const_tester),
Expand Down Expand Up @@ -1587,6 +1596,7 @@ function generate_test_functions()
randn(sr(2), 700, 500),
randn(sr(3), 300, 700),
),
(false, :none, nothing, _sum, randn(1024)),
]
end

Expand Down
4 changes: 4 additions & 0 deletions test/codual.jl
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,8 @@
@test codual_type(Real) == CoDual
@test codual_type(Any) == CoDual
@test codual_type(Type{UnitRange{Int}}) == CoDual{Type{UnitRange{Int}}, NoTangent}
@test(==(
codual_type(Union{Float64, Int}),
Union{CoDual{Float64, Float64}, CoDual{Int, NoTangent}},
))
end
Loading