pylea 0.1.0__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (95) hide show
  1. pylea/__init__.py +376 -0
  2. pylea/compile/__init__.py +1 -0
  3. pylea/compile/_order_worker.py +31 -0
  4. pylea/compile/cost.py +209 -0
  5. pylea/compile/lower.py +191 -0
  6. pylea/compile/network.py +139 -0
  7. pylea/compile/order.py +405 -0
  8. pylea/compile/plan.py +158 -0
  9. pylea/compile/planner.py +169 -0
  10. pylea/draw/__init__.py +172 -0
  11. pylea/draw/coordinates.py +192 -0
  12. pylea/draw/dependency.py +169 -0
  13. pylea/draw/glyph.py +345 -0
  14. pylea/draw/layout.py +526 -0
  15. pylea/draw/sketch.py +303 -0
  16. pylea/draw/svg.py +136 -0
  17. pylea/draw/tikz.py +124 -0
  18. pylea/eval/__init__.py +1 -0
  19. pylea/eval/backend.py +404 -0
  20. pylea/eval/interp.py +235 -0
  21. pylea/eval/kernels.py +194 -0
  22. pylea/examples/__init__.py +13 -0
  23. pylea/examples/bioassay.py +129 -0
  24. pylea/examples/birthday.py +35 -0
  25. pylea/examples/disease.py +46 -0
  26. pylea/examples/electoral_college.py +92 -0
  27. pylea/examples/german_tank.py +133 -0
  28. pylea/examples/holdem.py +265 -0
  29. pylea/examples/monty_hall.py +46 -0
  30. pylea/examples/percolation.py +101 -0
  31. pylea/examples/secret_santa.py +51 -0
  32. pylea/examples/simple.py +288 -0
  33. pylea/examples/sprinkler.py +92 -0
  34. pylea/ir/__init__.py +33 -0
  35. pylea/ir/bits.py +23 -0
  36. pylea/ir/body.py +207 -0
  37. pylea/ir/dependency.py +225 -0
  38. pylea/ir/inline.py +140 -0
  39. pylea/ir/prim.py +296 -0
  40. pylea/ir/program.py +329 -0
  41. pylea/ir/reference.py +84 -0
  42. pylea/sample/__init__.py +58 -0
  43. pylea/sample/_exact_worker.py +79 -0
  44. pylea/sample/certify.py +479 -0
  45. pylea/sample/collapse.py +145 -0
  46. pylea/sample/cost_model.py +201 -0
  47. pylea/sample/estimate.py +103 -0
  48. pylea/sample/packed.py +192 -0
  49. pylea/sample/pilot.py +242 -0
  50. pylea/sample/portfolio.py +421 -0
  51. pylea/sample/query.py +256 -0
  52. pylea/sample/run.py +479 -0
  53. pylea/sample/schedule.py +261 -0
  54. pylea/sample/sweep.py +622 -0
  55. pylea/semantics/__init__.py +1 -0
  56. pylea/semantics/likelihood_table.py +278 -0
  57. pylea/semantics/matrices.py +133 -0
  58. pylea/simplify/__init__.py +44 -0
  59. pylea/simplify/absorb.py +93 -0
  60. pylea/simplify/boolean.py +220 -0
  61. pylea/simplify/common.py +580 -0
  62. pylea/simplify/cse.py +96 -0
  63. pylea/simplify/dce.py +97 -0
  64. pylea/simplify/driver.py +152 -0
  65. pylea/simplify/fold.py +622 -0
  66. pylea/simplify/implication.py +238 -0
  67. pylea/simplify/inline.py +48 -0
  68. pylea/simplify/lift.py +315 -0
  69. pylea/simplify/mask.py +176 -0
  70. pylea/simplify/observe.py +173 -0
  71. pylea/syntax/__init__.py +1 -0
  72. pylea/syntax/choice.py +99 -0
  73. pylea/syntax/comparison.py +72 -0
  74. pylea/syntax/compiled.py +251 -0
  75. pylea/syntax/dpp.py +1366 -0
  76. pylea/syntax/encoding.py +108 -0
  77. pylea/syntax/enum.py +576 -0
  78. pylea/syntax/estimate.py +86 -0
  79. pylea/syntax/expectation.py +159 -0
  80. pylea/syntax/flip.py +57 -0
  81. pylea/syntax/integer.py +224 -0
  82. pylea/syntax/method.py +161 -0
  83. pylea/syntax/observe.py +73 -0
  84. pylea/syntax/pmf.py +85 -0
  85. pylea/syntax/prob.py +82 -0
  86. pylea/syntax/tuple.py +177 -0
  87. pylea/syntax/variable.py +244 -0
  88. pylea/utility/__init__.py +1 -0
  89. pylea/utility/bif_to_dpp.py +646 -0
  90. pylea/utility/dft_to_dpp.py +363 -0
  91. pylea-0.1.0.dist-info/METADATA +99 -0
  92. pylea-0.1.0.dist-info/RECORD +95 -0
  93. pylea-0.1.0.dist-info/WHEEL +5 -0
  94. pylea-0.1.0.dist-info/licenses/LICENSE +21 -0
  95. pylea-0.1.0.dist-info/top_level.txt +1 -0
pylea/compile/lower.py ADDED
@@ -0,0 +1,191 @@
1
+ """Lowering: a DPP becomes one plan per body.
2
+
3
+ `compile_program` first splices in every callee too wide to hold as a single matrix, then
4
+ lowers each body that remains: it builds the body's network, asks the order finder for a
5
+ contraction tree, and turns that tree into plan steps. A call surviving that first step
6
+ stays a leaf of its caller's network, and the evaluator contracts it once however many
7
+ callers load it.
8
+
9
+ `assemble` walks the tree and emits the steps. It tracks how many live tensors still carry
10
+ each index, so that an index is summed away at the contraction where its last carrier is
11
+ consumed, and a free index is never summed at all. Leaves are formed first, taking a
12
+ diagonal where the op names a wire twice and selecting a position where the index is sliced.
13
+
14
+ Lowering does no arithmetic and touches no array, so a DPP compiles the same way
15
+ whatever it is later evaluated on.
16
+ """
17
+ from __future__ import annotations
18
+
19
+ from math import prod
20
+
21
+ from collections import Counter
22
+
23
+ from pylea.compile.network import Tensor, Network, build_network, distinct as _distinct
24
+ from pylea.compile.order import AdaptiveOrderFinder, Order, OrderFinder
25
+ from pylea.compile.plan import (
26
+ Contract, Diagonal, Load, Plan, Reduce, Select, Spread, Transpose,
27
+ )
28
+ from pylea.ir.inline import flatten_wide_calls
29
+ from pylea.ir.program import Program, reachable
30
+
31
+ # A callee wider than this is inlined into its caller rather than held as one matrix.
32
+ # ``2**22`` float64 entries is 32 MiB, and a body at the ceiling is contracted once rather
33
+ # than once per use.
34
+ MAX_LEAF_BITS = 22
35
+
36
+
37
+ class PlanTooWide(ValueError):
38
+ """Raised when a plan's largest intermediate exceeds the entry budget."""
39
+
40
+
41
+ def compile_program(prog: Program, order_finder: OrderFinder | None = None,
42
+ kappa: int | None = None, max_leaf_bits: int = MAX_LEAF_BITS,
43
+ max_entries: int | None = None) -> tuple[dict[int, Plan], int]:
44
+ """Plans for every body of `prog` surviving inlining, together with the root's index.
45
+
46
+ >>> from pylea import DPP, Bool, flip
47
+ >>> @DPP
48
+ ... def both() -> Bool:
49
+ ... return flip(0.5) & flip(0.25)
50
+ >>> plans, root = compile_program(both.program())
51
+ >>> len(plans[root].steps), plans[root].peak_entries()
52
+ (5, 8)
53
+ """
54
+ finder = AdaptiveOrderFinder() if order_finder is None else order_finder
55
+ prog = flatten_wide_calls(prog, max_leaf_bits)
56
+ plans = {i: lower_body(prog, i, finder, kappa, max_entries) for i in reachable(prog)}
57
+ return plans, prog.root
58
+
59
+
60
+ def lower_body(prog: Program, body: int, order_finder: OrderFinder,
61
+ kappa: int | None = None, max_entries: int | None = None) -> Plan:
62
+ """The plan contracting body `body` to its semantics.
63
+
64
+ Raises `PlanTooWide` if `max_entries` is given and the plan's peak exceeds it.
65
+ """
66
+ net = build_network(prog, body)
67
+ order = order_finder.find(net.axes, net.output, net.size, kappa=kappa)
68
+ plan = assemble(prog, body, net, order)
69
+ if max_entries is not None and plan.peak_entries() > max_entries:
70
+ raise PlanTooWide(
71
+ f"body {body} contracts through {plan.peak_entries()} entries, "
72
+ f"over the budget of {max_entries}")
73
+ return plan
74
+
75
+
76
+ def assemble(prog: Program, body: int, net: Network, order: Order) -> Plan:
77
+ """The `Plan` the contraction tree `order` over `net` becomes."""
78
+ b = prog.bodies[body]
79
+ steps: list = []
80
+ shapes: list[tuple[int, ...]] = []
81
+ varying: list[bool] = []
82
+
83
+ def emit(step, shape, var: bool) -> int:
84
+ steps.append(step)
85
+ shapes.append(tuple(shape))
86
+ varying.append(var)
87
+ return len(steps) - 1
88
+
89
+ slice_at = {ix: k for k, ix in enumerate(order.sliced)}
90
+ free = set(net.output)
91
+ if free & set(slice_at):
92
+ raise ValueError("a free index was sliced; slices could not be summed")
93
+
94
+ # How many live tensors carry each index. An index leaves a contraction's result once
95
+ # no other live tensor and no free position still needs it.
96
+ live: Counter = Counter()
97
+ for ixs in net.axes:
98
+ for ix in set(ixs):
99
+ if ix not in slice_at:
100
+ live[ix] += 1
101
+
102
+ def keep(ix: int) -> bool:
103
+ return live[ix] > 0 or ix in free
104
+
105
+ values: list[tuple[int, list[int]]] = []
106
+ for t, axes in enumerate(net.axes):
107
+ leaf = net.leaves[t]
108
+ shape = leaf.shape if isinstance(leaf, Tensor) else leaf.dims
109
+ vid = emit(Load(t), shape, False)
110
+ if isinstance(leaf, Tensor) and leaf.reduce:
111
+ vid = emit(Reduce(vid, leaf.reduce), _drop(shape, leaf.reduce), False)
112
+ cur = list(axes)
113
+ if len(set(cur)) != len(cur): # `f(x, x)`, and a contraction cannot take a diagonal
114
+ groups = tuple(tuple(ax for ax, ix in enumerate(cur) if ix == label)
115
+ for label in _distinct(cur))
116
+ cur = list(_distinct(cur))
117
+ vid = emit(Diagonal(vid, groups), [net.size[ix] for ix in cur], False)
118
+ for ix in list(cur):
119
+ if ix in slice_at:
120
+ ax = cur.index(ix)
121
+ vid = emit(Select(vid, ax, slice_at[ix]), _drop(shapes[vid], [ax]), True)
122
+ cur.pop(ax)
123
+ values.append((vid, cur))
124
+
125
+ for a, c in order.path:
126
+ va, ia = values[a]
127
+ vb, ib = values[c]
128
+ for ix in ia:
129
+ live[ix] -= 1
130
+ for ix in ib:
131
+ live[ix] -= 1
132
+
133
+ shared = set(ia) & set(ib)
134
+ if any(not keep(ix) for ix in ia if ix not in shared) or \
135
+ any(not keep(ix) for ix in ib if ix not in shared):
136
+ raise AssertionError("an index survived to a contraction that cannot sum it")
137
+ batch = [ix for ix in ia if ix in shared and keep(ix)]
138
+ summed = [ix for ix in ia if ix in shared and not keep(ix)]
139
+ left = [ix for ix in ia if ix not in shared]
140
+ right = [ix for ix in ib if ix not in shared]
141
+
142
+ perm_a = _perm(ia, batch + left + summed)
143
+ perm_b = _perm(ib, batch + summed + right)
144
+ out = batch + left + right
145
+ vid = emit(
146
+ Contract(va, vb, perm_a, perm_b,
147
+ prod(net.size[i] for i in batch), prod(net.size[i] for i in left),
148
+ prod(net.size[i] for i in summed), prod(net.size[i] for i in right)),
149
+ [net.size[ix] for ix in out],
150
+ varying[va] or varying[vb],
151
+ )
152
+ for ix in out:
153
+ live[ix] += 1
154
+ values.append((vid, out))
155
+
156
+ vid, cur = values[-1]
157
+ contracted = vid
158
+ if cur != list(net.output):
159
+ perm = _perm(cur, list(net.output))
160
+ vid = emit(Transpose(vid, perm), [shapes[vid][p] for p in perm], varying[vid])
161
+
162
+ if len(net.output_axes) != len(net.output):
163
+ # The output names a wire more than once, so it is a diagonal. The contraction ran
164
+ # over the distinct wires, and only the result is this wide.
165
+ shape = tuple(net.size[ix] for ix in net.output_axes)
166
+ positions = tuple(tuple(ax for ax, ix in enumerate(net.output_axes) if ix == label)
167
+ for label in net.output)
168
+ vid = emit(Spread(vid, positions, shape), shape, varying[vid])
169
+
170
+ return Plan(
171
+ leaves=net.leaves,
172
+ steps=tuple(steps),
173
+ shapes=tuple(shapes),
174
+ varying=tuple(varying),
175
+ sliced_dims=tuple(net.size[ix] for ix in order.sliced),
176
+ contracted=contracted,
177
+ result=vid,
178
+ out_bits=b.out_bits,
179
+ in_bits=b.in_bits,
180
+ )
181
+
182
+
183
+ def _drop(shape, axes) -> tuple[int, ...]:
184
+ drop = set(axes)
185
+ return tuple(d for ax, d in enumerate(shape) if ax not in drop)
186
+
187
+
188
+ def _perm(current: list[int], target: list[int]) -> tuple[int, ...]:
189
+ at = {ix: ax for ax, ix in enumerate(current)}
190
+ return tuple(at[ix] for ix in target)
191
+
@@ -0,0 +1,139 @@
1
+ """The tensor network of a body.
2
+
3
+ `build_network` turns body `i` of a DPP into a `Network`: one tensor per op, whose axes
4
+ are the op's output wires followed by its argument wires, and one index per wire, of size
5
+ ``2**bits[w]``. Contracting the network down to the body's outputs followed by its inputs
6
+ gives the semantics of the body as a ``(2**out_bits, 2**in_bits)`` matrix.
7
+
8
+ A wire read by several ops is one index carried by several tensors. A wire occurring twice
9
+ on one tensor, as in ``f(x, x)``, is one index carried twice by that tensor, which the plan
10
+ resolves by taking the diagonal of the leaf as it is built, a contraction step being able to
11
+ multiply two arrays but not also to take a diagonal.
12
+
13
+ A wire that only one tensor carries and that is not part of the interface can only be summed
14
+ away, so it is summed as the leaf is formed and never reaches the order finder, which also
15
+ keeps cotengra's slicer from being handed an index it cannot slice.
16
+
17
+ An interface wire that no op carries gets a tensor of ones, so that every index of the
18
+ result is present in the network. A body with no ops and no interface leaves a single
19
+ scalar tensor of ones.
20
+
21
+ The output axes may name a wire twice, which happens for a body returning one wire twice or
22
+ returning an argument. The contraction runs over the distinct wires and the plan spreads
23
+ the result onto the diagonal at the end, so only the result is ever that wide.
24
+ """
25
+ from __future__ import annotations
26
+
27
+ from collections import Counter
28
+ from dataclasses import dataclass
29
+
30
+ from pylea.ir.prim import Prim
31
+ from pylea.ir.program import Program
32
+
33
+
34
+ @dataclass(frozen=True, slots=True)
35
+ class Tensor:
36
+ """One op's matrix, reshaped to one axis per wire: its outputs, then its arguments.
37
+
38
+ `reduce` names the axes to sum out as the leaf is formed, those being the op's wires
39
+ that nothing else in the body reads and that are outside its interface. They are summed
40
+ here rather than left to the contraction, since no contraction tree can do anything but
41
+ sum them.
42
+ """
43
+
44
+ callee: Prim | int
45
+ shape: tuple[int, ...]
46
+ reduce: tuple[int, ...] = ()
47
+
48
+
49
+ @dataclass(frozen=True, slots=True)
50
+ class Ones:
51
+ """The constant-one tensor of shape `dims`."""
52
+
53
+ dims: tuple[int, ...]
54
+
55
+
56
+ Leaf = Tensor | Ones
57
+
58
+
59
+ @dataclass(frozen=True, slots=True)
60
+ class Network:
61
+ """Tensors, the output axes, the index sizes, and how to form each tensor.
62
+
63
+ `axes[t]` are the labels of tensor `t` in the order its array is stored, once its dead
64
+ axes are summed. A label may repeat there, which is a diagonal. `output_axes` are the
65
+ result's axes and may also repeat; `output` is the distinct ones in order of first
66
+ occurrence.
67
+
68
+ Every label either occurs on at least two tensors or is free, so a contraction tree can
69
+ sum each one out where its last occurrence is consumed.
70
+ """
71
+
72
+ axes: tuple[tuple[int, ...], ...]
73
+ output_axes: tuple[int, ...]
74
+ output: tuple[int, ...]
75
+ size: dict[int, int]
76
+ leaves: tuple[Leaf, ...]
77
+
78
+ @property
79
+ def n_tensors(self) -> int:
80
+ return len(self.leaves)
81
+
82
+
83
+ def distinct(labels) -> tuple[int, ...]:
84
+ """`labels` with later repeats removed, order preserved.
85
+
86
+ >>> distinct((3, 1, 3, 2, 1))
87
+ (3, 1, 2)
88
+ """
89
+ return tuple(dict.fromkeys(labels))
90
+
91
+
92
+ def build_network(prog: Program, body: int) -> Network:
93
+ """The network of body `body`, whose output axes are its outputs then its inputs.
94
+
95
+ >>> from pylea import DPP, Bool, flip
96
+ >>> @DPP
97
+ ... def both() -> Bool:
98
+ ... return flip(0.5) & flip(0.25)
99
+ >>> net = build_network(both.program(), 0)
100
+ >>> net.n_tensors, net.output
101
+ (3, (2,))
102
+ """
103
+ b = prog.bodies[body]
104
+ size: dict[int, int] = {w: 1 << b.bits[w] for w in range(len(b.bits))}
105
+ axes: list[tuple[int, ...]] = []
106
+ leaves: list[Leaf] = []
107
+
108
+ for op in b.ops:
109
+ wires = op.outs + op.args
110
+ axes.append(wires)
111
+ leaves.append(Tensor(op.callee, tuple(size[w] for w in wires)))
112
+
113
+ output_axes = b.outputs + b.inputs
114
+ free = set(output_axes)
115
+
116
+ # A wire read nowhere else and outside the interface can only be summed, so it is summed
117
+ # as the leaf is formed and never reaches the order finder. cotengra's slicer fails on
118
+ # an index that only one tensor carries.
119
+ occurrences: Counter = Counter(ix for t in axes for ix in set(t))
120
+ for t, leaf in enumerate(leaves):
121
+ dead = tuple(ax for ax, ix in enumerate(axes[t])
122
+ if occurrences[ix] == 1 and ix not in free)
123
+ if dead:
124
+ assert isinstance(leaf, Tensor)
125
+ leaves[t] = Tensor(leaf.callee, leaf.shape, dead)
126
+ axes[t] = tuple(ix for ax, ix in enumerate(axes[t]) if ax not in set(dead))
127
+
128
+ incident = {ix for t in axes for ix in t}
129
+ for ix in distinct(output_axes):
130
+ if ix not in incident:
131
+ axes.append((ix,))
132
+ leaves.append(Ones((size[ix],)))
133
+ incident.add(ix)
134
+
135
+ if not axes: # a body with no ops and no interface is just 1
136
+ axes.append(())
137
+ leaves.append(Ones(()))
138
+
139
+ return Network(tuple(axes), output_axes, distinct(output_axes), size, tuple(leaves))