Algorithmcore
Cut enumeration
Also called k-feasible cut enumeration.
Computes all k-feasible cuts of every node in topological order by merging the cuts of its fanins and dropping those with more than k leaves.
- Solves
- LUT mapping
- Time
- O(N k C^(2d)) worst
- Space
- O(N k C) extra
- Where
- C = the largest number of cuts of one node; N = the number of nodes; d = the largest number of fanins of a node; k = the largest number of leaves of a cut
- Model
- Word RAM
- Properties
- deterministicexactofflineiterative
- Evidence
- Proof sketchCode in 1 languages
- Origin
- Peichen Pan, Chih Chang Lin, Jason Cong, Chang Wu, Yuzheng Ding, 1998
Cut enumeration lists, for every node of a Boolean network or an and-inverter graph, all its -feasible cuts: the sets of at most nodes that every path from the primary inputs to the node passes through. It visits the nodes in topological order. The cuts of a gate are the trivial cut and the unions of one cut of each fanin, kept when they have at most leaves. Unions that contain another union are dropped, and then the result is exactly the set of minimal -feasible cuts. The cuts are the input of LUT mapping, where each cut is a possible lookup table, and of local rewriting such as AIG rewriting. Pan and Lin (1998) and Cong, Wu and Ding (1999) used this bottom-up computation in FPGA mappers. For large the number of cuts grows fast, and priority cuts keep only a few of them.
In plain words
Each gate in a circuit gets its value from the gates below it. Pick a small group of wires below a gate such that every route from the circuit inputs to the gate passes through the group. Then the group alone decides the gate's value. Such a group is a cut.
Find the cuts from the inputs upward. An input has one cut, itself. For a gate, take one cut from each of the gates that feed it and join them. If the joined group is small enough, it is a cut of the gate. The gate itself is also a cut. Try every way to pick and join, and throw away a group that holds a smaller group from the same list, because it has a wire it does not need.
The problem
The input is a directed acyclic graph with nodes in topological order, whose sources are the primary inputs (PIs), and an integer . The output is, for each node , the set of all minimal -feasible cuts of , the trivial cut included. A cut is minimal when no proper subset of it is a cut of . Non-minimal cuts are of no use to a mapper, because a lookup table over a minimal subset computes the same function with fewer inputs.
The algorithm
CUTS(G, k)- 1
for each node v of G in topological order do - 2
if v is a primary input then - 3
Φ(v) ← { {v} } - 4
else - 5
A ← { ∅ } - 6
for each fanin u of v do - 7
A ← { a ∪ c : a ∈ A, c ∈ Φ(u), |a ∪ c| ≤ k } - 8
A ← the sets of A that contain no other set of A - 9
Φ(v) ← { {v} } ∪ A - 10
return Φ
Line L7 merges the partial unions with each cut of the next fanin. Each holds the trivial cut , so can take a fanin itself as a leaf. Line L8 removes the dominated sets. The code of this page sorts by size and keeps a set only when no kept set is a subset of it.
Example
Take and the network with inputs and the gates , , , and . Node feeds both and , so the network is not a tree.
| node | fanins | unions at line L7 with at most 3 leaves | |
|---|---|---|---|
| and the four unions | |||
| and the four unions |
At , the 5 cuts of and the 3 cuts of give 15 unions. Eleven have 4 or 5 leaves and fail the test at line L7, for example . None of the four that remain contains another, so line L8 keeps all of them. With the same pass gives 12 cuts for and 28 cuts in all.
Line L8 matters in the network with inputs and the gates , and . The cuts of are and , since . At , line L7 makes and . The set holds , and holds , so line L8 drops both, and .
Correctness
The full proof shows by induction on the topological order that is exactly the set of minimal -feasible cuts of . Every set made at line L7 is a union of cuts of the fanins, so it is a cut of , and it has at most leaves. If a set that line L8 keeps had a smaller cut of inside it, then the minimal cuts of the fanins inside would have a union in that is a proper subset of , and line L8 would have dropped . In the other direction, a minimal cut of cuts each fanin . A minimal cut of inside is in by induction, and the union of these cuts is a cut of inside , so it is itself. Every partial union is a subset of , so line L7 keeps it.
Proof: The merge of fanin cuts gives exactly the minimal k-feasible cutsfull
Let be a directed acyclic graph whose sources are the primary inputs (PIs). A set is a cut of if every path from a PI to meets , and it is minimal if no proper subset of it is a cut of . Write for the set of minimal cuts of with at most nodes. The cone of is the set of nodes from which can be reached.
Lemma 1 (minimal cuts lie in the cone). Every minimal cut of is a subset of the cone of . If , then .
Proof. A node outside the cone is on no path to , so removing it from leaves a cut. If , then is a cut inside , so by minimality.
Lemma 2 (a cut of v cuts each fanin). Let be a fanin of and a cut of with . Then is a cut of .
Proof. A path from a PI to , followed by the edge , is a path to , so it meets . It does not meet at , so meets .
Lemma 3 (unions of fanin cuts). Let have fanins , , and let be a cut of for each . Then is a cut of .
Proof. A path from a PI to has at least one edge, since is not a PI, and its last edge is for some . The part before that edge is a path from a PI to , which meets .
Theorem. For every node , CUTS sets .
Proof. Induction on the topological order. For a PI , the only path from a PI to is the path with the one node , so a cut must contain , and is the only minimal cut. Line L3 sets .
Let be a gate with fanins , and assume for each , which holds since each fanin comes before . Let be the set after the last run of line L7, and the set after line L8. After the -th run of line L7, holds exactly the sets with such that each partial union , , has at most nodes. The partial unions grow with , so this is the same as asking that the full union has at most nodes. So
Every set of is a -feasible cut of that does not contain . It is a cut by Lemma 3 and has at most nodes. Each lies in the cone of by Lemma 1, and is not in the cone of because is acyclic.
. Let and suppose that a proper subset is a cut of . Then , so is a cut of each by Lemma 2. Let be a minimal cut of ; one exists because is finite. Then , so , and has at most nodes, so . Now , so contains another set of , and line L8 drops it, a contradiction. So is a minimal cut of .
. Let be a minimal -feasible cut of other than . By Lemma 1, , so is a cut of each by Lemma 2. Let be a minimal cut of . It has at most nodes, so . The union is a subset of , and it is a cut of by Lemma 3, so by the minimality of . Its size is at most , so . If contained another set of , then would be a cut of and a proper subset of , which contradicts minimality. So line L8 keeps .
Line L9 adds , which is a minimal cut of with one node, and no set of equals . Together, .
Corollary (without pruning). If line L8 is left out, every set of is still a -feasible cut of , and .
Proof. Induction on the topological order, with the claim that every set of is a -feasible cut of inside the cone of , and that . For a PI both hold. For a gate , every set made at line L7 is a union of cuts of the fanins, so it is a cut of by Lemma 3, it has at most nodes by the test, and it lies in the cone of . The argument of the last part of the theorem uses only , so it shows that every minimal -feasible cut of other than is made at line L7.
Termination and order. Each step handles finite sets, so CUTS ends. The set above does not depend on the order of the fanins, so neither does .
Complexity
Let be the largest size of a set and the largest fanin count. At a node, has at most sets after the -th fanin, so line L7 makes at most unions over the last fanin and over all fanins, each in time on sorted lists of leaves. Line L8 compares pairs of sets of , time. Over nodes this is , and the cuts take space. For an AIG, .
itself depends on the network. It is at most , which is polynomial in for a fixed , but large. The root of a complete binary tree of depth 4 has 9 minimal 4-feasible cuts and 49 minimal 6-feasible cuts, and the counts grow quickly with the depth and the amount of reconvergence. Mishchenko, Cho, Chatterjee and Brayton (2007) give the cost of full enumeration of 6-input cuts on large circuits as the reason for priority cuts. Implementations such as ABC keep a bit signature of the leaves of each cut, so that most subset tests at line L8 take one machine operation.
Properties
What is proved. For each node, the output is exactly the set of minimal -feasible cuts, in finite time, and the result does not depend on the order of the fanins. Without line L8, every output set is still a -feasible cut, but some are not minimal, as the example shows. A node is a function of each of its cuts, so each cut has a truth table, which the code of this page computes by simulation of the cone.
What is measured. The test file compares the output with a brute force over all subsets of at most nodes of the cone of each node, on 150 random networks with up to 4 inputs and 7 gates and with from 1 to 4 (seed 7). These are checks of the code, not benchmark results.
Variants and improvements
- Cut ranking and pruning. Cong, Wu and Ding (1999) rank the cuts of a node by a cost, such as depth or area, and keep only part of them, so that a mapper can handle large .
- Priority cuts. Keep at most a fixed number of cuts per node, chosen by the cost of the current mapping pass, and merge only those. The set is then no longer complete.
- Truth tables with the cuts. Compute the truth table of each cut at line L7 from the tables of the two merged cuts, after moving their variables to the positions of the union. Rewriting and Boolean matching need these tables.
- Sequential cuts. For circuits with registers, a cut may cross register boundaries, so that mapping and retiming are done together. Pan and Lin's mapper is of this kind.
- Cuts for structural choices. When the network holds several equivalent structures for a node, the cuts of the node are the union of the cuts of all its choices.
When to use it
Use full enumeration when is small, about 4 or 5, or when the network is small, and a complete set of cuts is needed, for example for exact rewriting or for a mapper that must see every option. For 6-input LUTs on large circuits, use priority cuts instead. For depth alone, FlowMap finds a best cut per node by max-flow without listing cuts.
Pitfalls
- Forgetting the trivial cut. If does not hold , a node can never use its fanin as a leaf, and most cuts are lost, including the cut of the two fanins.
- Keeping dominated cuts. Without line L8, the list holds cuts with leaves that the function does not need, and the lists grow faster at each level.
- Pruning too early. Dropping sets at line L7 before all fanins are merged is safe only for sets that contain a finished cut of . A partial union is not yet a cut of .
- Shared leaves. The union of two cuts can be smaller than the sum of their sizes when the fanins share logic. Test the size of the union, not the sum.
- Blow-up. The number of cuts can grow very fast with . Put a limit on the cuts per node, or use priority cuts, before running on large designs.
Implementations
Each implementation follows the pseudocode above. A comment @line links a line of code to a line of the pseudocode. Point at a line of the pseudocode to see the matching code. The build runs the native tests of each language.
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/cut-enumeration/. The authors are listed in prov/B4M940.jsonl.
"""Bottom-up enumeration of all k-feasible cuts of every node of a DAG.
The network is a list `fanins`: node v has the fanin list fanins[v], and
every fanin of v is a smaller number than v, so the list order is a
topological order. A node with no fanins is a primary input. A cut is a
frozenset of node numbers.
For a primary input v the only cut is the trivial cut {v}. For an inner
node v with fanins u_1, ..., u_d, a cut is the union of one cut of each
fanin, kept when it has at most k leaves, plus the trivial cut {v}.
With pruning, a cut that holds another cut of the same node is dropped,
and the result is exactly the set of minimal k-feasible cuts.
"""
from typing import Sequence
Cut = frozenset
def merge(a: frozenset, b: frozenset, k: int):
"""The union of two cuts, or None when it has more than k leaves."""
u = a | b
return u if len(u) <= k else None
def drop_dominated(cuts: set) -> list:
"""Keep the cuts that contain no other cut of the set."""
by_size = sorted(cuts, key=lambda c: (len(c), sorted(c)))
kept: list = []
for c in by_size:
if not any(d <= c for d in kept):
kept.append(c)
return kept
def enumerate_cuts(fanins: Sequence[Sequence[int]], k: int, prune: bool = True) -> list:
"""All k-feasible cuts of every node, as a list of lists of frozensets.
With prune=True the cuts of each node are its minimal k-feasible cuts.
With prune=False they are all unions that the merge produces, which
are cuts but not always minimal ones.
"""
if k < 1:
raise ValueError("k must be at least 1")
cuts: list = []
for v, fi in enumerate(fanins): # @line L1
if any(u >= v for u in fi):
raise ValueError("fanins must come before the node")
if not fi: # @line L2
cuts.append([frozenset([v])]) # @line L3
continue
# @line L4: v is a gate
acc = {frozenset()} # @line L5
for u in fi: # @line L6
nxt = set()
for a in acc:
for c in cuts[u]:
m = merge(a, c, k) # @line L7
if m is not None:
nxt.add(m)
acc = nxt
if prune:
mine = drop_dominated(acc) # @line L8
else:
mine = sorted(acc, key=lambda c: (len(c), sorted(c)))
cuts.append([frozenset([v])] + mine) # @line L9
return cuts # @line L10
def cut_function(fanins: Sequence[Sequence[int]], ops: Sequence, v: int, cut: Sequence[int]) -> int:
"""Truth table of node v over the ordered leaves of a cut.
ops[w] is a function from the list of fanin values to 0 or 1. The
table is an integer of 2^|cut| bits; bit m is the value of v when
leaf i has the value of bit (|cut| - 1 - i) of m, so the first leaf
is the high bit.
"""
n = len(cut)
full = (1 << (1 << n)) - 1
val: dict = {}
for i, leaf in enumerate(cut):
t = 0
for m in range(1 << n):
if (m >> (n - 1 - i)) & 1:
t |= 1 << m
val[leaf] = t
def table(w: int) -> int:
if w in val:
return val[w]
if not fanins[w]:
raise ValueError("the set is not a cut of the node")
ins = [table(u) for u in fanins[w]]
t = 0
for m in range(1 << n):
if ops[w]([(x >> m) & 1 for x in ins]):
t |= 1 << m
val[w] = t & full
return val[w]
return table(v)
test_cut_enumeration.py 119 lines
# SPDX-License-Identifier: MIT
# Part of https://algorithm.tamnd.com/cut-enumeration/. The authors are listed in prov/B4M940.jsonl.
import random
from itertools import combinations
from cut_enumeration import cut_function, enumerate_cuts
AND = lambda xs: int(all(xs)) # noqa: E731
# The example of the page: inputs a, b, c, d are nodes 0 to 3.
EXAMPLE = [[], [], [], [], [0, 1], [1, 2], [4, 5], [5, 3], [6, 7]]
def is_cut(fanins, v, cut) -> bool:
"""Every path from a primary input to v meets the set."""
if v in cut:
return True
seen, stack = set(), [v]
while stack:
w = stack.pop()
if w in seen or w in cut:
continue
seen.add(w)
if not fanins[w]:
return False # a primary input reaches v around the set
stack.extend(fanins[w])
return True
def cone(fanins, v) -> set:
out, stack = set(), [v]
while stack:
w = stack.pop()
if w not in out:
out.add(w)
stack.extend(fanins[w])
return out
def brute_minimal_cuts(fanins, v, k) -> set:
nodes = sorted(cone(fanins, v))
found = set()
for size in range(1, k + 1):
for c in combinations(nodes, size):
s = frozenset(c)
if is_cut(fanins, v, s) and not any(is_cut(fanins, v, s - {x}) for x in s):
found.add(s)
return found
def random_dag(rng: random.Random, n_in: int, n_gates: int, max_fanin: int):
fanins = [[] for _ in range(n_in)]
for v in range(n_in, n_in + n_gates):
d = rng.randint(1, min(max_fanin, v))
fanins.append(sorted(rng.sample(range(v), d)))
return fanins
def test_example_k3() -> None:
cuts = enumerate_cuts(EXAMPLE, 3)
got = [sorted(sorted(c) for c in cs) for cs in cuts]
assert got[6] == [[0, 1, 2], [0, 1, 5], [1, 2, 4], [4, 5], [6]]
assert got[7] == [[1, 2, 3], [3, 5], [7]]
assert got[8] == [[3, 4, 5], [3, 5, 6], [4, 5, 7], [6, 7], [8]]
def test_example_k4() -> None:
cuts = enumerate_cuts(EXAMPLE, 4)
assert len(cuts[8]) == 12
assert frozenset([0, 1, 2, 3]) in cuts[8]
assert frozenset([1, 2, 4, 7]) in cuts[8]
assert sum(len(c) for c in cuts) == 4 + 2 + 2 + 5 + 3 + 12
def test_unpruned_contains_redundant() -> None:
# x = 0, y = 1, a = AND(x, y), b = AND(x, a), v = AND(a, b).
# The union of {a} and the cut {x, y} of b is {a, x, y}, which holds {a, x}.
f = [[], [], [0, 1], [0, 2], [2, 3]]
pruned = enumerate_cuts(f, 3)[4]
raw = enumerate_cuts(f, 3, prune=False)[4]
assert frozenset([0, 1, 2]) in raw
assert frozenset([0, 1, 2]) not in pruned
assert set(pruned) == {frozenset([4]), frozenset([2, 3]), frozenset([0, 2]), frozenset([0, 1])}
def test_against_brute_force() -> None:
rng = random.Random(7)
for trial in range(150):
f = random_dag(rng, rng.randint(1, 4), rng.randint(1, 7), 3)
for k in (1, 2, 3, 4):
cuts = enumerate_cuts(f, k)
raw = enumerate_cuts(f, k, prune=False)
for v in range(len(f)):
assert set(cuts[v]) == brute_minimal_cuts(f, v, k), (trial, k, v)
for c in raw[v]:
assert len(c) <= k and is_cut(f, v, c)
def test_cut_function_matches_simulation() -> None:
rng = random.Random(11)
for _ in range(60):
n_in = rng.randint(2, 4)
f = random_dag(rng, n_in, rng.randint(1, 6), 2)
ops = [None] * n_in + [rng.choice([AND, lambda xs: int(any(xs)), lambda xs: sum(xs) & 1])
for _ in range(len(f) - n_in)]
for v in range(n_in, len(f)):
for c in enumerate_cuts(f, 3)[v]:
leaves = sorted(c)
t = cut_function(f, ops, v, leaves)
# evaluate the whole network on each primary input point
for m in range(1 << n_in):
val = [(m >> (n_in - 1 - i)) & 1 for i in range(n_in)]
for w in range(n_in, len(f)):
val.append(ops[w]([val[u] for u in f[w]]))
idx = 0
for leaf in leaves:
idx = 2 * idx + val[leaf]
assert (t >> idx) & 1 == val[v]
History
Cong and Ding (1994) defined -feasible cuts for FlowMap, but found one cut per node by max-flow and did not list them. In the late 1990s, mappers began to list all cuts. Pan and Lin (1998) computed the -feasible cuts of each node from the cuts of its fanins in a mapper that also retimes the circuit. Cong, Wu and Ding (1999) studied the enumeration of all cuts, and their ranking and pruning, as a general base for FPGA mapping with several objectives. Mishchenko, Cho, Chatterjee and Brayton (2007) found that full enumeration costs too much for 6-input cuts on large circuits, and kept only a few priority cuts per node. Cut enumeration is also the first step of AIG rewriting in ABC.
References
- Peichen Pan and Chih-Chang Lin, A new retiming-based technology mapping algorithm for LUT-based FPGAs, Proceedings of the 1998 ACM/SIGDA Sixth International Symposium on Field Programmable Gate Arrays (FPGA 98), pages 35 to 42, the computation of the K-feasible cuts of each node from the cuts of its fanins (general locator, not checked against the paper)
- Cut ranking and pruning, enabling a general and efficient FPGA mapping solution, the enumeration of all K-feasible cuts by merging fanin cuts, and the ranking and pruning of cuts (general locator, not checked against the paper)
- Combinational and sequential mapping with priority cuts, the discussion of exhaustive cut enumeration and its cost, which priority cuts avoid (general locator)
Where this leads
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H1 sketch |
|
| Lean | L0 no Lean part |
|
| Code (python) | I0 not checked yet |
|
| Visual | V0 none |
|
| Writing | W2 basic |
|
Tag B4M940 Edit this page History Report an error Markdown