Skip to content

Commit 9682416

Browse files
author
Sergio Sánchez Ramírez
committed
Move KaHyPar optimizer to package extension
1 parent 7e953f4 commit 9682416

File tree

5 files changed

+53
-42
lines changed

5 files changed

+53
-42
lines changed

Project.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ Combinatorics = "861a8166-3701-5b0c-9a16-15d98fcdc6aa"
99
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
1010
DataStructures = "864edb3b-99cc-5e75-8d2d-829cb0a9cfe8"
1111
Graphs = "86223c79-3864-5bf0-83f7-82e725a168b6"
12-
KaHyPar = "2a6221f6-aa48-11e9-3542-2d9e0ef01880"
1312
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
1413
PackageExtensionCompat = "65ce6f38-6b18-4e1d-a461-8949797d7930"
1514
SparseArrays = "2f01184e-e22b-5df5-ae63-d93ebab69eaf"
@@ -19,18 +18,21 @@ Suppressor = "fd094767-a336-5f1f-9728-57cf17d0bbfb"
1918
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
2019
FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000"
2120
GraphMakie = "1ecd5474-83a3-4783-bb4f-06765db800d2"
21+
KaHyPar = "2a6221f6-aa48-11e9-3542-2d9e0ef01880"
2222
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
2323

2424
[extensions]
2525
EinExprsChainRulesCoreExt = "ChainRulesCore"
2626
EinExprsFiniteDifferencesExt = "FiniteDifferences"
2727
EinExprsGraphMakieExt = ["Makie", "GraphMakie"]
28+
EinExprsKaHyParExt = "KaHyPar"
2829
EinExprsMakieExt = "Makie"
2930

3031
[extras]
3132
ChainRulesCore = "d360d2e6-b24c-11e9-a2a3-2a2ae2dbcce4"
3233
FiniteDifferences = "26cc04aa-876d-5657-8c51-4c34ba976000"
3334
GraphMakie = "1ecd5474-83a3-4783-bb4f-06765db800d2"
35+
KaHyPar = "2a6221f6-aa48-11e9-3542-2d9e0ef01880"
3436
Makie = "ee78f7c6-11fb-53f2-987a-cfe4a2b5a57a"
3537

3638
[compat]

ext/EinExprsKaHyParExt.jl

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
module EinExprsKaHyParExt
2+
3+
using EinExprs
4+
using AbstractTrees
5+
using SparseArrays
6+
using KaHyPar
7+
using Suppressor
8+
using Compat
9+
10+
function EinExprs.einexpr(config::EinExprs.HyPar, path)
11+
config.stop(path) && return path
12+
13+
inds = mapreduce(head, , path.args)
14+
indexmap = Dict(Iterators.map(@compat(splat(Pair)) reverse, enumerate(inds)))
15+
16+
I = EinExprs.flatmap(((i, tensor),) -> fill(i, ndims(tensor)), enumerate(path.args)) |> collect
17+
J = EinExprs.flatmap(tensor -> Iterators.map(Base.Fix1(getindex, indexmap), head(tensor)), path.args) |> collect
18+
V = fill(1, length(I))
19+
incidence_matrix = sparse(I, J, V)
20+
21+
# NOTE indices in `inds` should be in the same order as unique indices appear by iterating on `path.args` because `∪` retains order
22+
edge_weights = map(config.edge_scaler Base.Fix1(size, path), inds)
23+
vertex_weights = map(config.vertex_scaler length, args(path))
24+
25+
hypergraph = KaHyPar.HyperGraph(incidence_matrix, vertex_weights, edge_weights)
26+
KaHyPar.kahypar_set_seed(hypergraph.context, config.seed)
27+
28+
partitions = @suppress KaHyPar.partition(
29+
hypergraph,
30+
config.parts;
31+
imbalance = config.imbalance,
32+
configuration = config.configuration,
33+
)
34+
35+
_args = map(unique(partitions)) do partition
36+
selection = partitions .== partition
37+
count(selection) == 1 && return only(args(path)[selection])
38+
39+
expr = sum(args(path)[selection], skip = path.head)
40+
einexpr(config, expr)
41+
end
42+
43+
return sum(_args, skip = path.head)
44+
end
45+
46+
end

src/Optimizers/KaHyPar.jl

Lines changed: 1 addition & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,4 @@
1-
using AbstractTrees
2-
using SparseArrays
3-
using KaHyPar
4-
using Suppressor
5-
using Compat
1+
using Base: @kwdef
62

73
@kwdef struct HyPar <: Optimizer
84
parts::Int = 2
@@ -13,39 +9,3 @@ using Compat
139
vertex_scaler::Function = Base.Fix1(*, 1000) Int round log2
1410
seed::Int = 0
1511
end
16-
17-
function EinExprs.einexpr(config::HyPar, path)
18-
config.stop(path) && return path
19-
20-
inds = mapreduce(head, , path.args)
21-
indexmap = Dict(Iterators.map(@compat(splat(Pair)) reverse, enumerate(inds)))
22-
23-
I = flatmap(((i, tensor),) -> fill(i, ndims(tensor)), enumerate(path.args)) |> collect
24-
J = flatmap(tensor -> Iterators.map(Base.Fix1(getindex, indexmap), head(tensor)), path.args) |> collect
25-
V = fill(1, length(I))
26-
incidence_matrix = sparse(I, J, V)
27-
28-
# NOTE indices in `inds` should be in the same order as unique indices appear by iterating on `path.args` because `∪` retains order
29-
edge_weights = map(config.edge_scaler Base.Fix1(size, path), inds)
30-
vertex_weights = map(config.vertex_scaler length, args(path))
31-
32-
hypergraph = KaHyPar.HyperGraph(incidence_matrix, vertex_weights, edge_weights)
33-
KaHyPar.kahypar_set_seed(hypergraph.context, config.seed)
34-
35-
partitions = @suppress KaHyPar.partition(
36-
hypergraph,
37-
config.parts;
38-
imbalance = config.imbalance,
39-
configuration = config.configuration,
40-
)
41-
42-
_args = map(unique(partitions)) do partition
43-
selection = partitions .== partition
44-
count(selection) == 1 && return only(args(path)[selection])
45-
46-
expr = sum(args(path)[selection], skip = path.head)
47-
einexpr(config, expr)
48-
end
49-
50-
return sum(_args, skip = path.head)
51-
end

test/KaHyPar_test.jl

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
@testset "KaHyPar" begin
2+
using KaHyPar
3+
24
@testset begin
35
tensors = [
46
EinExpr([:j, :b, :i, :h], Dict(i => 2 for i in [:j, :b, :i, :h])),

test/Project.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
CairoMakie = "13f3f980-e62b-5c42-98c6-ff1f3baf88f0"
33
Compat = "34da2185-b29b-5c13-b0c7-acf172513d20"
44
GraphMakie = "1ecd5474-83a3-4783-bb4f-06765db800d2"
5+
KaHyPar = "2a6221f6-aa48-11e9-3542-2d9e0ef01880"
56
LinearAlgebra = "37e2e46d-f89d-539d-b4ee-838fcccc9c8e"
67
NetworkLayout = "46757867-2c16-5918-afeb-47bfcb05e46a"
78
Test = "8dfed614-e22c-5e08-85e1-65c5234f0b40"

0 commit comments

Comments
 (0)