compiled-knowledge 4.0.0a17__cp312-cp312-win_amd64.whl → 4.0.0a18__cp312-cp312-win_amd64.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.
Potentially problematic release.
This version of compiled-knowledge might be problematic. Click here for more details.
- ck/circuit/__init__.py +7 -0
- ck/circuit/_circuit_cy.cp312-win_amd64.pyd +0 -0
- ck/circuit/_circuit_cy.pxd +33 -0
- ck/circuit/_circuit_cy.pyx +84 -110
- ck/circuit/_circuit_py.py +2 -2
- ck/circuit_compiler/cython_vm_compiler/_compiler.cp312-win_amd64.pyd +0 -0
- ck/circuit_compiler/cython_vm_compiler/_compiler.pyx +6 -5
- ck/pgm_compiler/support/circuit_table/__init__.py +7 -0
- ck/pgm_compiler/support/circuit_table/_circuit_table_cy.cp312-win_amd64.pyd +0 -0
- ck/pgm_compiler/support/circuit_table/_circuit_table_cy.pyx +44 -37
- ck/pgm_compiler/support/circuit_table/_circuit_table_cy_cpp_verion.pyx +601 -0
- ck/pgm_compiler/support/circuit_table/_circuit_table_cy_minimal_version.pyx +311 -0
- ck/pgm_compiler/support/circuit_table/_circuit_table_cy_v4.0.0a17.pyx +325 -0
- ck/pgm_compiler/support/circuit_table/_circuit_table_py.py +76 -41
- ck/pgm_compiler/support/named_compiler_maker.py +12 -2
- ck/utils/iter_extras.py +8 -1
- ck_demos/utils/compare.py +5 -1
- {compiled_knowledge-4.0.0a17.dist-info → compiled_knowledge-4.0.0a18.dist-info}/METADATA +1 -1
- {compiled_knowledge-4.0.0a17.dist-info → compiled_knowledge-4.0.0a18.dist-info}/RECORD +22 -19
- {compiled_knowledge-4.0.0a17.dist-info → compiled_knowledge-4.0.0a18.dist-info}/WHEEL +1 -1
- ck/circuit_compiler/cython_vm_compiler/_compiler.c +0 -16946
- {compiled_knowledge-4.0.0a17.dist-info → compiled_knowledge-4.0.0a18.dist-info}/licenses/LICENSE.txt +0 -0
- {compiled_knowledge-4.0.0a17.dist-info → compiled_knowledge-4.0.0a18.dist-info}/top_level.txt +0 -0
|
@@ -1,8 +1,14 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
-
from typing import Sequence, Tuple, Iterable
|
|
3
|
+
from typing import Sequence, Tuple, Iterable
|
|
4
|
+
|
|
5
|
+
from ck.circuit import MUL, ADD
|
|
6
|
+
|
|
7
|
+
from ck.circuit._circuit_cy cimport Circuit, CircuitNode
|
|
8
|
+
|
|
9
|
+
cdef int c_ADD = ADD
|
|
10
|
+
cdef int c_MUL = MUL
|
|
4
11
|
|
|
5
|
-
from ck.circuit import CircuitNode, Circuit, OpNode, MUL
|
|
6
12
|
|
|
7
13
|
TableInstance = Tuple[int, ...]
|
|
8
14
|
|
|
@@ -22,14 +28,14 @@ cdef class CircuitTable:
|
|
|
22
28
|
zero node. These are assumed to be optimised out already.
|
|
23
29
|
"""
|
|
24
30
|
|
|
25
|
-
cdef public
|
|
31
|
+
cdef public Circuit circuit
|
|
26
32
|
cdef public tuple[int, ...] rv_idxs
|
|
27
|
-
cdef
|
|
33
|
+
cdef dict[tuple[int, ...], CircuitNode] rows
|
|
28
34
|
|
|
29
35
|
def __init__(
|
|
30
36
|
self,
|
|
31
37
|
circuit: Circuit,
|
|
32
|
-
rv_idxs: Sequence[int
|
|
38
|
+
rv_idxs: Sequence[int],
|
|
33
39
|
rows: Iterable[Tuple[TableInstance, CircuitNode]] = (),
|
|
34
40
|
):
|
|
35
41
|
"""
|
|
@@ -53,13 +59,19 @@ cdef class CircuitTable:
|
|
|
53
59
|
def get(self, key, default=None):
|
|
54
60
|
return self.rows.get(key, default)
|
|
55
61
|
|
|
62
|
+
def keys(self) -> Iterable[CircuitNode]:
|
|
63
|
+
return self.rows.keys()
|
|
64
|
+
|
|
65
|
+
def values(self) -> Iterable[tuple[int, ...]]:
|
|
66
|
+
return self.rows.values()
|
|
67
|
+
|
|
56
68
|
def __getitem__(self, key):
|
|
57
69
|
return self.rows[key]
|
|
58
70
|
|
|
59
71
|
def __setitem__(self, key, value):
|
|
60
72
|
self.rows[key] = value
|
|
61
73
|
|
|
62
|
-
cpdef
|
|
74
|
+
cpdef CircuitNode top(self):
|
|
63
75
|
# Get the circuit top value.
|
|
64
76
|
#
|
|
65
77
|
# Raises:
|
|
@@ -80,7 +92,7 @@ cdef class CircuitTable:
|
|
|
80
92
|
# Circuit Table Operations
|
|
81
93
|
# ==================================================================================
|
|
82
94
|
|
|
83
|
-
cpdef
|
|
95
|
+
cpdef CircuitTable sum_out(CircuitTable table, object rv_idxs: Iterable[int]):
|
|
84
96
|
# Return a circuit table that results from summing out
|
|
85
97
|
# the given random variables of this circuit table.
|
|
86
98
|
#
|
|
@@ -118,12 +130,12 @@ cpdef object sum_out(object table: CircuitTable, object rv_idxs: Iterable[int]):
|
|
|
118
130
|
for rv_index in remaining_rv_idxs:
|
|
119
131
|
index_map.append(_find(table.rv_idxs, rv_index))
|
|
120
132
|
|
|
121
|
-
cdef dict[tuple[int, ...], list[
|
|
133
|
+
cdef dict[tuple[int, ...], list[CircuitNode]] groups = {}
|
|
122
134
|
cdef object got
|
|
123
135
|
cdef list[int] group_instance
|
|
124
136
|
cdef tuple[int, ...] group_instance_tuple
|
|
125
137
|
cdef int i
|
|
126
|
-
cdef
|
|
138
|
+
cdef CircuitNode node
|
|
127
139
|
cdef tuple[int, ...] instance
|
|
128
140
|
for instance, node in table.rows.items():
|
|
129
141
|
group_instance = []
|
|
@@ -136,19 +148,19 @@ cpdef object sum_out(object table: CircuitTable, object rv_idxs: Iterable[int]):
|
|
|
136
148
|
else:
|
|
137
149
|
got.append(node)
|
|
138
150
|
|
|
139
|
-
cdef
|
|
140
|
-
cdef
|
|
141
|
-
cdef dict[tuple[int, ...],
|
|
151
|
+
cdef Circuit circuit = table.circuit
|
|
152
|
+
cdef CircuitTable new_table = CircuitTable(circuit, remaining_rv_idxs)
|
|
153
|
+
cdef dict[tuple[int, ...], CircuitNode] rows = new_table.rows
|
|
142
154
|
|
|
143
155
|
for group_instance_tuple, to_add in groups.items():
|
|
144
|
-
node = circuit.
|
|
156
|
+
node = circuit.op(c_ADD, tuple(to_add))
|
|
145
157
|
if not node.is_zero:
|
|
146
158
|
rows[group_instance_tuple] = node
|
|
147
159
|
|
|
148
160
|
return new_table
|
|
149
161
|
|
|
150
162
|
|
|
151
|
-
cpdef
|
|
163
|
+
cpdef CircuitTable sum_out_all(CircuitTable table):
|
|
152
164
|
# Return a circuit table that results from summing out
|
|
153
165
|
# all random variables of this circuit table.
|
|
154
166
|
circuit: Circuit = table.circuit
|
|
@@ -158,14 +170,14 @@ cpdef object sum_out_all(object table: CircuitTable): # -> CircuitTable:
|
|
|
158
170
|
elif num_rows == 1:
|
|
159
171
|
node = next(iter(table.rows.values()))
|
|
160
172
|
else:
|
|
161
|
-
node: CircuitNode = circuit.
|
|
173
|
+
node: CircuitNode = circuit.op(c_ADD, tuple(table.rows.values()))
|
|
162
174
|
if node.is_zero:
|
|
163
175
|
return CircuitTable(circuit, ())
|
|
164
176
|
|
|
165
177
|
return CircuitTable(circuit, (), [((), node)])
|
|
166
178
|
|
|
167
179
|
|
|
168
|
-
cpdef
|
|
180
|
+
cpdef CircuitTable project(CircuitTable table: CircuitTable, object rv_idxs: Iterable[int]):
|
|
169
181
|
# Call `sum_out(table, to_sum_out)`, where
|
|
170
182
|
# `to_sum_out = table.rv_idxs - rv_idxs`.
|
|
171
183
|
cdef set[int] to_sum_out = set(table.rv_idxs)
|
|
@@ -173,13 +185,13 @@ cpdef object project(object table: CircuitTable, object rv_idxs: Iterable[int]):
|
|
|
173
185
|
return sum_out(table, to_sum_out)
|
|
174
186
|
|
|
175
187
|
|
|
176
|
-
cpdef
|
|
188
|
+
cpdef CircuitTable product(CircuitTable x, CircuitTable y):
|
|
177
189
|
# Return a circuit table that results from the product of the two given tables.
|
|
178
190
|
#
|
|
179
191
|
# If x or y equals `one_table`, then the other table is returned. Otherwise,
|
|
180
192
|
# a new circuit table will be constructed and returned.
|
|
181
193
|
cdef int i
|
|
182
|
-
cdef
|
|
194
|
+
cdef Circuit circuit = x.circuit
|
|
183
195
|
if y.circuit is not circuit:
|
|
184
196
|
raise ValueError('circuit tables must refer to the same circuit')
|
|
185
197
|
|
|
@@ -229,12 +241,12 @@ cpdef object product(x: CircuitTable, y: CircuitTable): # -> CircuitTable:
|
|
|
229
241
|
cdef tuple[int, ...] co_tuple
|
|
230
242
|
cdef tuple[int, ...] yo_tuple
|
|
231
243
|
|
|
232
|
-
cdef
|
|
233
|
-
cdef dict[tuple[int, ...],
|
|
244
|
+
cdef CircuitTable table = CircuitTable(circuit, x.rv_idxs + yo_rv_idxs)
|
|
245
|
+
cdef dict[tuple[int, ...], CircuitNode] rows = table.rows
|
|
234
246
|
|
|
235
247
|
|
|
236
248
|
# Index the y rows by common-only key (y is the smaller of the two tables).
|
|
237
|
-
cdef dict[tuple[int, ...], list[tuple[tuple[int, ...],
|
|
249
|
+
cdef dict[tuple[int, ...], list[tuple[tuple[int, ...], CircuitNode]]] y_index = {}
|
|
238
250
|
for y_instance, y_node in y.rows.items():
|
|
239
251
|
co = []
|
|
240
252
|
yo = []
|
|
@@ -271,7 +283,10 @@ cpdef object product(x: CircuitTable, y: CircuitTable): # -> CircuitTable:
|
|
|
271
283
|
got = y_index.get(co_tuple)
|
|
272
284
|
if got is not None:
|
|
273
285
|
for yo_tuple, y_node in got:
|
|
274
|
-
|
|
286
|
+
if y_node.is_one:
|
|
287
|
+
rows[x_instance + yo_tuple] = x_node
|
|
288
|
+
else:
|
|
289
|
+
rows[x_instance + yo_tuple] = circuit.op(c_MUL, (x_node, y_node))
|
|
275
290
|
|
|
276
291
|
return table
|
|
277
292
|
|
|
@@ -285,7 +300,7 @@ cdef int _find(tuple[int, ...] xs, int x):
|
|
|
285
300
|
raise RuntimeError('not found')
|
|
286
301
|
|
|
287
302
|
|
|
288
|
-
cdef
|
|
303
|
+
cdef CircuitTable _product_no_common_rvs(CircuitTable x, CircuitTable y):
|
|
289
304
|
# Return the product of x and y, where x and y have no common random variables.
|
|
290
305
|
#
|
|
291
306
|
# This is an optimisation of more general product algorithm as no index needs
|
|
@@ -296,8 +311,8 @@ cdef object _product_no_common_rvs(x: CircuitTable, y: CircuitTable): # -> Circ
|
|
|
296
311
|
# Assumes:
|
|
297
312
|
# * There are no common random variables between x and y.
|
|
298
313
|
# * x and y are for the same circuit.
|
|
299
|
-
cdef
|
|
300
|
-
cdef
|
|
314
|
+
cdef Circuit circuit = x.circuit
|
|
315
|
+
cdef CircuitTable table = CircuitTable(circuit, x.rv_idxs + y.rv_idxs)
|
|
301
316
|
cdef tuple[int, ...] instance
|
|
302
317
|
|
|
303
318
|
for x_instance, x_node in x.rows.items():
|
|
@@ -308,18 +323,10 @@ cdef object _product_no_common_rvs(x: CircuitTable, y: CircuitTable): # -> Circ
|
|
|
308
323
|
else:
|
|
309
324
|
for y_instance, y_node in y.rows.items():
|
|
310
325
|
instance = x_instance + y_instance
|
|
311
|
-
|
|
326
|
+
if y_node.is_one:
|
|
327
|
+
table.rows[instance] = x_node
|
|
328
|
+
else:
|
|
329
|
+
table.rows[instance] = circuit.op(c_MUL, (x_node, y_node))
|
|
312
330
|
|
|
313
331
|
return table
|
|
314
332
|
|
|
315
|
-
|
|
316
|
-
cdef object _optimised_mul(object circuit: Circuit, object x: CircuitNode, object y: CircuitNode): # -> CircuitNode
|
|
317
|
-
if x.is_zero:
|
|
318
|
-
return x
|
|
319
|
-
if y.is_zero:
|
|
320
|
-
return y
|
|
321
|
-
if x.is_one:
|
|
322
|
-
return y
|
|
323
|
-
if y.is_one:
|
|
324
|
-
return x
|
|
325
|
-
return circuit.mul(x, y)
|