-
Notifications
You must be signed in to change notification settings - Fork 56
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
Adding OMEinsumContractionOrders.jl as a backend of TensorOperations.jl for finding the optimal contraction order #185
Open
ArrogantGao
wants to merge
16
commits into
Jutho:master
Choose a base branch
from
ArrogantGao:xz/OMEinsum
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
e4b102d
add OMEinsumContractionOrders as backend of optimaltree
ArrogantGao d6bbe6f
fixed tests
ArrogantGao 6d61103
fixed size dict must be Dict{T, Number}
ArrogantGao 329952d
fix optimize
ArrogantGao c14fa7c
update optimize, reture cost of the contraction
ArrogantGao 76698c1
add ExactTreeWidth solver
ArrogantGao 81d64af
convert type of the indices as Int for ExactTreewidth solver
ArrogantGao 3846242
using kahypar_auto to avoid manully selecting sc_target
ArrogantGao 5f442e2
Merge branch 'master' into xz/OMEinsum
ArrogantGao 70301a7
update project.toml
ArrogantGao 61d1cce
add optimizer to ncon
ArrogantGao f55c1b2
formatted by JuliaFormatter
ArrogantGao 92fe983
export TreeOptimizer
ArrogantGao 4adc9cf
format src
ArrogantGao a120620
update project.toml
ArrogantGao d39c6ea
format code
ArrogantGao File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,6 @@ | ||
*.jl.cov | ||
*.jl.*.cov | ||
*.jl.mem | ||
Manifest.toml | ||
Manifest.toml | ||
.vscode | ||
.DS_Store |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
module TensorOperationsOMEinsumContractionOrdersExt | ||
|
||
using TensorOperations | ||
using TensorOperations: TensorOperations as TO | ||
using TensorOperations: TreeOptimizer | ||
using OMEinsumContractionOrders | ||
using OMEinsumContractionOrders: EinCode, NestedEinsum, SlicedEinsum, isleaf, | ||
optimize_kahypar_auto | ||
|
||
function TO.optimaltree(network, optdata::Dict{TDK,TDV}, ::TreeOptimizer{:GreedyMethod}, | ||
verbose::Bool) where {TDK,TDV} | ||
@debug "Using optimizer GreedyMethod from OMEinsumContractionOrders" | ||
ome_optimizer = GreedyMethod() | ||
return optimize(network, optdata, ome_optimizer, verbose) | ||
end | ||
|
||
function TO.optimaltree(network, optdata::Dict{TDK,TDV}, ::TreeOptimizer{:KaHyParBipartite}, | ||
verbose::Bool) where {TDK,TDV} | ||
@debug "Using optimizer KaHyParBipartite from OMEinsumContractionOrders" | ||
return optimize_kahypar(network, optdata, verbose) | ||
end | ||
|
||
function TO.optimaltree(network, optdata::Dict{TDK,TDV}, ::TreeOptimizer{:TreeSA}, | ||
verbose::Bool) where {TDK,TDV} | ||
@debug "Using optimizer TreeSA from OMEinsumContractionOrders" | ||
ome_optimizer = TreeSA() | ||
return optimize(network, optdata, ome_optimizer, verbose) | ||
end | ||
|
||
function TO.optimaltree(network, optdata::Dict{TDK,TDV}, ::TreeOptimizer{:SABipartite}, | ||
verbose::Bool) where {TDK,TDV} | ||
@debug "Using optimizer SABipartite from OMEinsumContractionOrders" | ||
ome_optimizer = SABipartite() | ||
return optimize(network, optdata, ome_optimizer, verbose) | ||
end | ||
|
||
function TO.optimaltree(network, optdata::Dict{TDK,TDV}, ::TreeOptimizer{:ExactTreewidth}, | ||
verbose::Bool) where {TDK,TDV} | ||
@debug "Using optimizer ExactTreewidth from OMEinsumContractionOrders" | ||
ome_optimizer = ExactTreewidth() | ||
return optimize(network, optdata, ome_optimizer, verbose) | ||
end | ||
|
||
function optimize(network, optdata::Dict{TDK,TDV}, ome_optimizer::CodeOptimizer, | ||
verbose::Bool) where {TDK,TDV} | ||
@assert TDV <: Number "The values of `optdata` dictionary must be of `<:Number`" | ||
|
||
# transform the network as EinCode | ||
code, size_dict = network2eincode(network, optdata) | ||
# optimize the contraction order using OMEinsumContractionOrders, which gives a NestedEinsum | ||
optcode = optimize_code(code, size_dict, ome_optimizer) | ||
|
||
# transform the optimized contraction order back to the network | ||
optimaltree = eincode2contractiontree(optcode) | ||
|
||
# calculate the complexity of the contraction | ||
cc = OMEinsumContractionOrders.contraction_complexity(optcode, size_dict) | ||
if verbose | ||
println("Optimal contraction tree: ", optimaltree) | ||
println(cc) | ||
end | ||
return optimaltree, 2.0^(cc.tc) | ||
end | ||
|
||
function optimize_kahypar(network, optdata::Dict{TDK,TDV}, verbose::Bool) where {TDK,TDV} | ||
@assert TDV <: Number "The values of `optdata` dictionary must be of `<:Number`" | ||
|
||
# transform the network as EinCode | ||
code, size_dict = network2eincode(network, optdata) | ||
# optimize the contraction order using OMEinsumContractionOrders, which gives a NestedEinsum | ||
optcode = optimize_kahypar_auto(code, size_dict) | ||
|
||
# transform the optimized contraction order back to the network | ||
optimaltree = eincode2contractiontree(optcode) | ||
|
||
# calculate the complexity of the contraction | ||
cc = OMEinsumContractionOrders.contraction_complexity(optcode, size_dict) | ||
if verbose | ||
println("Optimal contraction tree: ", optimaltree) | ||
println(cc) | ||
end | ||
return optimaltree, 2.0^(cc.tc) | ||
end | ||
|
||
function network2eincode(network, optdata) | ||
indices = unique(vcat(network...)) | ||
new_indices = Dict([i => j for (j, i) in enumerate(indices)]) | ||
new_network = [Int[new_indices[i] for i in t] for t in network] | ||
open_edges = Int[] | ||
# if a indices appear only once, it is an open index | ||
for i in indices | ||
if sum([i in t for t in network]) == 1 | ||
push!(open_edges, new_indices[i]) | ||
end | ||
end | ||
size_dict = Dict([new_indices[i] => optdata[i] for i in keys(optdata)]) | ||
return EinCode(new_network, open_edges), size_dict | ||
end | ||
|
||
function eincode2contractiontree(eincode::NestedEinsum) | ||
if isleaf(eincode) | ||
return eincode.tensorindex | ||
else | ||
return [eincode2contractiontree(arg) for arg in eincode.args] | ||
end | ||
end | ||
|
||
# TreeSA returns a SlicedEinsum, with nslice = 0, so directly using the eins | ||
function eincode2contractiontree(eincode::SlicedEinsum) | ||
return eincode2contractiontree(eincode.eins) | ||
end | ||
|
||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think here you will have to be a little careful, in principle there is no order to the keyword arguments.
If I am not mistaken, now if the user first supplies
opt=(a = 2, b = 2, ...)
, and only afterwardsopt_algorithm=...
, the algorithm will be ignored.My best guess is that you probably want to attempt to extract an optimizer and optdict, and only after all kwargs have been parsed, you can construct the
contractiontreebuilder
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you very much for pointing that out, I did not notice that perviously.
In the revised version, the
contractiontreebuilder
will be constructed after all other kwargs have been parsed.