Skip to content

Commit

Permalink
delete all the functions relevant to merge_all_chains.
Browse files Browse the repository at this point in the history
  • Loading branch information
houpc committed Oct 26, 2023
1 parent 39d7986 commit dafa0bd
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 421 deletions.
141 changes: 2 additions & 139 deletions src/computational_graph/optimize.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ function optimize!(graphs::Union{Tuple,AbstractVector{<:AbstractGraph}}; verbose
else
graphs = collect(graphs)
leaf_mapping = remove_duplicated_leaves!(graphs, verbose=verbose, normalize=normalize)
# merge_all_chains!(graphs, verbose=verbose)
flatten_all_chains!(graphs, verbose=verbose)
merge_all_linear_combinations!(graphs, verbose=verbose)
return leaf_mapping
Expand All @@ -44,146 +43,10 @@ function optimize(graphs::Union{Tuple,AbstractVector{<:AbstractGraph}}; verbose=
return graphs_new, leaf_mapping
end

"""
function merge_all_chain_prefactors!(g::AbstractGraph; verbose=0)
In-place merge prefactors of all nodes representing trivial unary chains towards the root level for a single graph.
# Arguments:
- `g`: An AbstractGraph.
- `verbose`: Level of verbosity (default: 0).
# Returns:
- Optimized graph.
#
"""
function merge_all_chain_prefactors!(g::AbstractGraph; verbose=0)
verbose > 0 && println("merge prefactors of all nodes representing trivial unary chains toward root level.")
# Post-order DFS
for sub_g in g.subgraphs
merge_all_chain_prefactors!(sub_g)
merge_chain_prefactors!(sub_g)
end
merge_chain_prefactors!(g)
return g
end

"""
function merge_all_chain_prefactors!(graphs::AbstractVector{<:AbstractGraph}; verbose=0)
In-place merge prefactors of all nodes representing trivial unary chains towards the root level for given graphs.
# Arguments:
- `graphs`: An AbstractVector of graphs.
- `verbose`: Level of verbosity (default: 0).
# Returns:
- Optimized graphs.
#
"""
function merge_all_chain_prefactors!(graphs::AbstractVector{<:AbstractGraph}; verbose=0)
verbose > 0 && println("merge prefactors of all nodes representing trivial unary chains toward root level.")
# Post-order DFS
for g in graphs
merge_all_chain_prefactors!(g.subgraphs)
merge_chain_prefactors!(g)
end
return graphs
end

"""
function merge_all_factorless_chains!(g::AbstractGraph; verbose=0)
In-place merge all nodes representing factorless trivial unary chains within a single graph.
# Arguments:
- `g`: An AbstractGraph.
- `verbose`: Level of verbosity (default: 0).
# Returns:
- Optimized graph.
#
"""
function merge_all_factorless_chains!(g::AbstractGraph; verbose=0)
verbose > 0 && println("merge all nodes representing factorless trivial unary chains.")
# Post-order DFS
for sub_g in g.subgraphs
merge_all_factorless_chains!(sub_g)
merge_factorless_chain!(sub_g)
end
merge_factorless_chain!(g)
return g
end

"""
function merge_all_factorless_chains!(graphs::AbstractVector{<:AbstractGraph}; verbose=0)
In-place merge all nodes representing factorless trivial unary chains within given graphs.
# Arguments:
- `graphs`: An AbstractVector of graphs.
- `verbose`: Level of verbosity (default: 0).
# Returns:
- Optimized graphs.
#
"""
function merge_all_factorless_chains!(graphs::AbstractVector{<:AbstractGraph}; verbose=0)
verbose > 0 && println("merge all nodes representing factorless trivial unary chains.")
# Post-order DFS
for g in graphs
merge_all_factorless_chains!(g.subgraphs)
merge_factorless_chain!(g)
end
return graphs
end

"""
function merge_all_chains!(g::AbstractGraph; verbose=0)
In-place merge all nodes representing trivial unary chains within a single graph.
This function consolidates both chain prefactors and factorless chains.
# Arguments:
- `g`: An AbstractGraph.
- `verbose`: Level of verbosity (default: 0).
# Returns:
- Optimized graph.
#
"""
function merge_all_chains!(g::AbstractGraph; verbose=0)
verbose > 0 && println("merge all nodes representing trivial unary chains.")
merge_all_chain_prefactors!(g, verbose=verbose)
merge_all_factorless_chains!(g, verbose=verbose)
return g
end

"""
function merge_all_chains!(graphs::AbstractVector{<:AbstractGraph}; verbose=0) where {G<:AbstractGraph}
In-place merge all nodes representing trivial unary chains in given graphs.
This function consolidates both chain prefactors and factorless chains.
# Arguments:
- `graphs`: An AbstractVector of graphs.
- `verbose`: Level of verbosity (default: 0).
# Returns:
- Optimized graphs.
#
"""
function merge_all_chains!(graphs::AbstractVector{<:AbstractGraph}; verbose=0)
verbose > 0 && println("merge all nodes representing trivial unary chains.")
merge_all_chain_prefactors!(graphs, verbose=verbose)
merge_all_factorless_chains!(graphs, verbose=verbose)
return graphs
end

"""
function flatten_all_chains!(g::AbstractGraph; verbose=0)
In-place flattens all nodes representing trivial unary chains in the given graph `g`.
F
Flattens all nodes representing trivial unary chains in-place in the given graph `g`.
# Arguments:
- `graphs`: The graph to be processed.
Expand Down
128 changes: 0 additions & 128 deletions src/computational_graph/transform.jl
Original file line number Diff line number Diff line change
Expand Up @@ -157,134 +157,6 @@ function replace_subgraph(g::AbstractGraph, w::AbstractGraph, m::AbstractGraph)
return g_new
end

"""
function merge_factorless_chain!(g::AbstractGraph)
Simplifies `g` in-place if it represents a factorless trivial unary chain. For example, +(+(+g)) ↦ g.
Does nothing unless g has the following structure: 𝓞 --- 𝓞' --- ⋯ --- 𝓞'' ⋯ (!),
where the stop-case (!) represents a leaf, a non-trivial unary operator 𝓞'''(g) != g,
a node with non-unity multiplicative prefactor, or a non-unary operation.
# Arguments:
- `g::AbstractGraph`: graph to be modified
"""
function merge_factorless_chain!(g::AbstractGraph)
if unary_istrivial(g.operator) && onechild(g) && isfactorless(g)
child = eldest(g)
for field in fieldnames(typeof(g))
value = getproperty(child, field)
setproperty!(g, field, value)
end
end
while unary_istrivial(g.operator) && onechild(g) && isfactorless(g)
child = eldest(g)
for field in fieldnames(typeof(g))
value = getproperty(child, field)
setproperty!(g, field, value)
end
end
return g
end

"""
function merge_factorless_chain(g::AbstractGraph)
Returns a simplified copy of `g` if it represents a factorless trivial unary chain.
Otherwise, returns the original graph. For example, +(+(+g)) ↦ g.
Does nothing unless g has the following structure: 𝓞 --- 𝓞' --- ⋯ --- 𝓞'' ⋯ (!),
where the stop-case (!) represents a leaf, a non-trivial unary operator 𝓞'''(g) != g,
a node with non-unity multiplicative prefactor, or a non-unary operation.
# Arguments:
- `g::AbstractGraph`: graph to be modified
"""
function merge_factorless_chain(g::AbstractGraph)
while unary_istrivial(g.operator) && onechild(g) && isfactorless(g)
g = eldest(g)
end
return g
end

"""
function merge_chain_prefactors!(g::AbstractGraph)
Simplifies subgraphs of g representing trivial unary chains by merging their
subgraph factors toward root level, e.g., 2*(3*(5*g)) + 7*(9*(h)) ↦ 30*(*(*g)) + 63*(*h).
Acts only on subgraphs of g with the following structure: 𝓞 --- 𝓞' --- ⋯ --- 𝓞'' ⋯ (!),
where the stop-case (!) represents a leaf, a non-trivial unary operator 𝓞'''(g) != g, or a non-unary operation.
# Arguments:
- `g::AbstractGraph`: graph to be modified
"""
function merge_chain_prefactors!(g::AbstractGraph)
for (i, child) in enumerate(g.subgraphs)
total_chain_factor = 1
while onechild(child)
# Break case: end of trivial unary chain
unary_istrivial(child.operator) == false && break
# Move this subfactor to running total
total_chain_factor *= child.subgraph_factors[1]
child.subgraph_factors[1] = 1
# Descend one level
child = eldest(child)
end
# Update g subfactor with total factors from children
g.subgraph_factors[i] *= total_chain_factor
end
return g
end

"""
function merge_chain_prefactors(g::AbstractGraph)
Returns a copy of g with subgraphs representing trivial unary chains simplified by merging
their subgraph factors toward root level, e.g., 2*(3*(5*g)) + 7*(9*(h)) ↦ 30*(*(*g)) + 63*(*h).
Acts only on subgraphs of g with the following structure: 𝓞 --- 𝓞' --- ⋯ --- 𝓞'' ⋯ (!),
where the stop-case (!) represents a leaf, a non-trivial unary operator 𝓞'''(g) != g, or a non-unary operation.
# Arguments:
- `g::AbstractGraph`: graph to be modified
"""
merge_chain_prefactors(g::AbstractGraph) = merge_chain_prefactors!(deepcopy(g))

"""
function merge_chains!(g::AbstractGraph)
Converts subgraphs of g representing trivial unary chains
to in-place form, e.g., 2*(3*(5*g)) + 7*(9*(h)) ↦ 30*g + 63*h.
Acts only on subgraphs of g with the following structure: 𝓞 --- 𝓞' --- ⋯ --- 𝓞'' ⋯ (!),
where the stop-case (!) represents a leaf, a non-trivial unary operator 𝓞'''(g) != g, or a non-unary operation.
# Arguments:
- `g::AbstractGraph`: graph to be modified
"""
function merge_chains!(g::AbstractGraph)
merge_chain_prefactors!(g) # shift chain subgraph factors towards root level
for sub_g in g.subgraphs # prune factorless chain subgraphs
merge_factorless_chain!(sub_g)
end
return g
end

"""
function merge_chains(g::AbstractGraph)
Returns a copy of a graph g with subgraphs representing trivial unary chain
simplified to in-place form, e.g., 2*(3*(5*g)) + 7*(9*(h)) ↦ 30*g + 63*h.
Acts only on subgraphs of g with the following structure: 𝓞 --- 𝓞' --- ⋯ --- 𝓞'' ⋯ (!),
where the stop-case (!) represents a leaf, a non-trivial unary operator 𝓞'''(g) != g, or a non-unary operation.
# Arguments:
- `g::AbstractGraph`: graph to be modified
"""
merge_chains(g::AbstractGraph) = merge_chains!(deepcopy(g))

"""
function flatten_chains!(g::AbstractGraph)
Expand Down
Loading

0 comments on commit dafa0bd

Please sign in to comment.