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

Check black list when contracting operators #26

Merged
merged 5 commits into from
Jul 20, 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
62 changes: 38 additions & 24 deletions src/acset.jl
Original file line number Diff line number Diff line change
Expand Up @@ -285,20 +285,26 @@ function expand_operators(d::SummationDecapode)
return e
end

""" function contract_operators(d::SummationDecapode; allowable_ops::Set{Symbol} = Set{Symbol}())
""" function contract_operators(d::SummationDecapode; white_list::Set{Symbol} = Set{Symbol}(), black_list::Set{Symbol} = Set{Symbol}())

Find chains of Op1s in the given Decapode, and replace them with
a single Op1 with a vector of function names. After this process,
all Vars that are not a part of any computation are removed.
all Vars that are not a part of any computation are removed. If a
white list is provided, only chain those operators. If a black list
is provided, do not chain those operators.
"""
function contract_operators(d::SummationDecapode; allowable_ops::Set{Symbol} = Set{Symbol}())
function contract_operators(d::SummationDecapode;
white_list::Set{Symbol} = Set{Symbol}(),
black_list::Set{Symbol} = Set{Symbol}())
e = expand_operators(d)
contract_operators!(e, allowable_ops = allowable_ops)
contract_operators!(e, white_list=white_list, black_list=black_list)
#return e
end

function contract_operators!(d::SummationDecapode; allowable_ops::Set{Symbol} = Set{Symbol}())
chains = find_chains(d, allowable_ops = allowable_ops)
function contract_operators!(d::SummationDecapode;
white_list::Set{Symbol} = Set{Symbol}(),
black_list::Set{Symbol} = Set{Symbol}())
chains = find_chains(d, white_list=white_list, black_list=black_list)
filter!(x -> length(x) != 1, chains)
for chain in chains
add_part!(d, :Op1, src=d[:src][first(chain)], tgt=d[:tgt][last(chain)], op1=Vector{Symbol}(d[:op1][chain]))
Expand Down Expand Up @@ -327,28 +333,32 @@ function remove_neighborless_vars!(d::SummationDecapode)
d
end

"""
function find_chains(d::SummationDecapode; allowable_ops::Set{Symbol} = Set{Symbol}())
""" function find_chains(d::SummationDecapode; white_list::Set{Symbol} = Set{Symbol}(), black_list::Set{Symbol} = Set{Symbol}())

Find chains of Op1s in the given Decapode. A chain ends when the
target of the last Op1 is part of an Op2 or sum, or is a target
of multiple Op1s. Only operators with names included in the
allowable_ops set are allowed to be contracted. If the set is
empty then all operators are allowed.
of multiple Op1s. If a white list is provided, only chain those
operators. If a black list is provided, do not chain those operators.
"""
function find_chains(d::SummationDecapode; allowable_ops::Set{Symbol} = Set{Symbol}())
function find_chains(d::SummationDecapode;
white_list::Set{Symbol} = Set{Symbol}(),
black_list::Set{Symbol} = Set{Symbol}())
chains = []
visited = falses(nparts(d, :Op1))
# TODO: Re-write this without two reduce-vcats.
chain_starts = unique(reduce(vcat, reduce(vcat,
[incident(d, Vector{Int64}(filter(i -> !isnothing(i), infer_states(d))), :src),
incident(d, d[:res], :src),
incident(d, d[:sum], :src)])))

if(!isempty(allowable_ops))
filter!(x -> d[x, :op1] ∈ allowable_ops, chain_starts)
end

rvrv(x) = reduce(vcat, reduce(vcat, x))
chain_starts = unique(rvrv(
[incident(d, Vector{Int64}(filter(i -> !isnothing(i), infer_states(d))), :src),
incident(d, d[:res], :src),
incident(d, d[:sum], :src),
incident(d, d[collect(Iterators.flatten(incident(d, collect(black_list), :op1))), :tgt], :src)]))

passes_white_list(x) = isempty(white_list) ? true : x ∈ white_list
passes_black_list(x) = x ∉ black_list

filter!(x -> passes_white_list(d[x, :op1]), chain_starts)
filter!(x -> passes_black_list(d[x, :op1]), chain_starts)

s = Stack{Int64}()
foreach(x -> push!(s, x), chain_starts)
while !isempty(s)
Expand All @@ -368,11 +378,14 @@ function find_chains(d::SummationDecapode; allowable_ops::Set{Symbol} = Set{Symb
is_tgt_of_many_ops(d, tgt) ||
!isempty(incident(d, tgt, :sum)) ||
!isempty(incident(d, tgt, :summand)) ||
(!isempty(allowable_ops) && d[only(next_op1s), :op1] ∉ allowable_ops))
!passes_white_list(d[only(next_op1s), :op1]) ||
!passes_black_list(d[only(next_op1s), :op1]))
# Terminate chain.
append!(chains, [curr_chain])
for next_op1 in next_op1s
visited[next_op1] || (!isempty(allowable_ops) && d[only(next_op1s), :op1] ∉ allowable_ops) || push!(s, next_op1)
for op1 in next_op1s
if !visited[op1] && passes_white_list(d[op1, :op1]) && passes_black_list(d[op1, :op1])
push!(s, op1)
end
end
break
end
Expand All @@ -382,6 +395,7 @@ function find_chains(d::SummationDecapode; allowable_ops::Set{Symbol} = Set{Symb
end
return chains
end

function add_constant!(d::AbstractNamedDecapode, k::Symbol)
return add_part!(d, :Var, type=:Constant, name=k)
end
Expand Down
15 changes: 15 additions & 0 deletions test/language.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1283,6 +1283,21 @@ end

op1 = [:⋆₂, :∂ₜ, :d₁]
end

t14_orig = @decapode begin
C == a(b(c(d(e(f(g(D)))))))
end
t14_contracted = contract_operators(t14_orig,
black_list=Set([:d]))
@test issetequal(t14_contracted[:op1], [:d, [:g, :f, :e], [:c, :b, :a]])

t15_orig = @decapode begin
C == a(b(c(d(e(f(g(D)))))))
end
t15_contracted = contract_operators(t15_orig,
white_list=Set([:a, :b, :c]),
black_list=Set([:d]))
@test issetequal(t15_contracted[:op1], [:g, :f, :e, :d, [:c, :b, :a]])
end

@testset "ASCII & Vector Calculus Operators" begin
Expand Down
Loading