compiled-knowledge 4.0.0a22__cp312-cp312-macosx_11_0_arm64.whl → 4.0.0a23__cp312-cp312-macosx_11_0_arm64.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/_circuit_cy.c +50 -60
- ck/circuit/_circuit_cy.cpython-312-darwin.so +0 -0
- ck/circuit/_circuit_cy.pyx +1 -1
- ck/circuit/_circuit_py.py +1 -1
- ck/circuit_compiler/cython_vm_compiler/_compiler.c +179 -170
- ck/circuit_compiler/cython_vm_compiler/_compiler.cpython-312-darwin.so +0 -0
- ck/circuit_compiler/cython_vm_compiler/_compiler.pyx +3 -3
- ck/circuit_compiler/cython_vm_compiler/cython_vm_compiler.py +14 -7
- ck/circuit_compiler/interpret_compiler.py +35 -4
- ck/circuit_compiler/llvm_vm_compiler.py +9 -3
- ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_cy.c +1 -1
- ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_cy.cpython-312-darwin.so +0 -0
- ck/circuit_compiler/support/llvm_ir_function.py +18 -1
- ck/pgm_compiler/support/circuit_table/_circuit_table_cy.c +1 -1
- ck/pgm_compiler/support/circuit_table/_circuit_table_cy.cpython-312-darwin.so +0 -0
- ck/program/raw_program.py +40 -7
- ck_demos/ace/simple_ace_demo.py +18 -0
- ck_demos/getting_started/__init__.py +0 -0
- ck_demos/getting_started/simple_demo.py +18 -0
- ck_demos/programs/demo_raw_program_dump.py +17 -0
- {compiled_knowledge-4.0.0a22.dist-info → compiled_knowledge-4.0.0a23.dist-info}/METADATA +1 -1
- {compiled_knowledge-4.0.0a22.dist-info → compiled_knowledge-4.0.0a23.dist-info}/RECORD +25 -21
- {compiled_knowledge-4.0.0a22.dist-info → compiled_knowledge-4.0.0a23.dist-info}/WHEEL +0 -0
- {compiled_knowledge-4.0.0a22.dist-info → compiled_knowledge-4.0.0a23.dist-info}/licenses/LICENSE.txt +0 -0
- {compiled_knowledge-4.0.0a22.dist-info → compiled_knowledge-4.0.0a23.dist-info}/top_level.txt +0 -0
|
Binary file
|
|
@@ -38,7 +38,7 @@ DTYPE_TO_CVM_TYPE: Dict[DTypeNumeric, str] = {
|
|
|
38
38
|
}
|
|
39
39
|
|
|
40
40
|
|
|
41
|
-
def make_function(analysis: CircuitAnalysis, dtype: DTypeNumeric) -> Tuple[RawProgramFunction, int]:
|
|
41
|
+
def make_function(analysis: CircuitAnalysis, dtype: DTypeNumeric) -> Tuple[RawProgramFunction, int, int]:
|
|
42
42
|
"""
|
|
43
43
|
Make a RawProgram function that interprets the circuit.
|
|
44
44
|
|
|
@@ -47,7 +47,7 @@ def make_function(analysis: CircuitAnalysis, dtype: DTypeNumeric) -> Tuple[RawPr
|
|
|
47
47
|
dtype: a numpy data type that must be a key in the dictionary, DTYPE_TO_CVM_TYPE.
|
|
48
48
|
|
|
49
49
|
Returns:
|
|
50
|
-
(function, number_of_tmps)
|
|
50
|
+
(function, number_of_tmps, number_of_instructions)
|
|
51
51
|
"""
|
|
52
52
|
|
|
53
53
|
cdef Instructions instructions
|
|
@@ -128,7 +128,7 @@ def make_function(analysis: CircuitAnalysis, dtype: DTypeNumeric) -> Tuple[RawPr
|
|
|
128
128
|
else:
|
|
129
129
|
raise ValueError(f'cvm_type_name unexpected: {cvm_type_name!r}')
|
|
130
130
|
|
|
131
|
-
return function, len(analysis.op_to_tmp)
|
|
131
|
+
return function, len(analysis.op_to_tmp), len(analysis.op_nodes)
|
|
132
132
|
|
|
133
133
|
# VM instructions
|
|
134
134
|
cdef int ADD = circuit.ADD
|
|
@@ -48,15 +48,16 @@ class CythonRawProgram(RawProgram):
|
|
|
48
48
|
result: Sequence[CircuitNode],
|
|
49
49
|
dtype: DTypeNumeric,
|
|
50
50
|
):
|
|
51
|
-
|
|
52
|
-
self.result = result
|
|
53
|
-
|
|
54
|
-
function, number_of_tmps = _make_function(
|
|
51
|
+
function, number_of_tmps, number_of_instructions = _make_function(
|
|
55
52
|
var_nodes=in_vars,
|
|
56
53
|
result_nodes=result,
|
|
57
54
|
dtype=dtype,
|
|
58
55
|
)
|
|
59
56
|
|
|
57
|
+
self.in_vars = in_vars
|
|
58
|
+
self.result = result
|
|
59
|
+
self.number_of_instructions = number_of_instructions
|
|
60
|
+
|
|
60
61
|
super().__init__(
|
|
61
62
|
function=function,
|
|
62
63
|
dtype=dtype,
|
|
@@ -66,6 +67,10 @@ class CythonRawProgram(RawProgram):
|
|
|
66
67
|
var_indices=tuple(var.idx for var in in_vars),
|
|
67
68
|
)
|
|
68
69
|
|
|
70
|
+
def dump(self, *, prefix: str = '', indent: str = ' ') -> None:
|
|
71
|
+
super().dump(prefix=prefix, indent=indent)
|
|
72
|
+
print(f'{prefix}number of instructions = {self.number_of_instructions}')
|
|
73
|
+
|
|
69
74
|
def __getstate__(self):
|
|
70
75
|
"""
|
|
71
76
|
Support for pickle.
|
|
@@ -94,7 +99,7 @@ class CythonRawProgram(RawProgram):
|
|
|
94
99
|
self.in_vars = state['in_vars']
|
|
95
100
|
self.result = state['result']
|
|
96
101
|
|
|
97
|
-
self.function, _ = _make_function(
|
|
102
|
+
self.function, _, self.number_of_instructions = _make_function(
|
|
98
103
|
var_nodes=self.in_vars,
|
|
99
104
|
result_nodes=self.result,
|
|
100
105
|
dtype=self.dtype,
|
|
@@ -105,7 +110,7 @@ def _make_function(
|
|
|
105
110
|
var_nodes: Sequence[VarNode],
|
|
106
111
|
result_nodes: Sequence[CircuitNode],
|
|
107
112
|
dtype: DTypeNumeric,
|
|
108
|
-
) -> Tuple[RawProgramFunction, int]:
|
|
113
|
+
) -> Tuple[RawProgramFunction, int, int]:
|
|
109
114
|
"""
|
|
110
115
|
Make a RawProgram function that interprets the circuit.
|
|
111
116
|
|
|
@@ -115,7 +120,9 @@ def _make_function(
|
|
|
115
120
|
dtype: a numpy data type that must be a key in the dictionary, DTYPE_TO_CVM_TYPE.
|
|
116
121
|
|
|
117
122
|
Returns:
|
|
118
|
-
(function, number_of_tmps)
|
|
123
|
+
(function, number_of_tmps, number_of_instructions)
|
|
119
124
|
"""
|
|
120
125
|
analysis: CircuitAnalysis = analyze_circuit(var_nodes, result_nodes)
|
|
126
|
+
DEBUG = _compiler.make_function(analysis, dtype)
|
|
127
|
+
|
|
121
128
|
return _compiler.make_function(analysis, dtype)
|
|
@@ -1,17 +1,17 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import ctypes as ct
|
|
3
4
|
from dataclasses import dataclass
|
|
4
5
|
from typing import Sequence, Optional, Dict, List, Tuple, Callable
|
|
5
6
|
|
|
6
7
|
import numpy as np
|
|
7
|
-
import ctypes as ct
|
|
8
8
|
|
|
9
|
+
from .support.circuit_analyser import CircuitAnalysis, analyze_circuit
|
|
10
|
+
from .support.input_vars import InputVars, InferVars, infer_input_vars
|
|
9
11
|
from ..circuit import Circuit, CircuitNode, VarNode, OpNode, ADD, MUL
|
|
10
12
|
from ..program.raw_program import RawProgram, RawProgramFunction
|
|
11
13
|
from ..utils.iter_extras import multiply, first
|
|
12
14
|
from ..utils.np_extras import NDArrayNumeric, DTypeNumeric
|
|
13
|
-
from .support.circuit_analyser import CircuitAnalysis, analyze_circuit
|
|
14
|
-
from .support.input_vars import InputVars, InferVars, infer_input_vars
|
|
15
15
|
|
|
16
16
|
# index to a value array
|
|
17
17
|
_VARS = 0
|
|
@@ -85,6 +85,15 @@ class InterpreterRawProgram(RawProgram):
|
|
|
85
85
|
var_indices=tuple(var.idx for var in in_vars),
|
|
86
86
|
)
|
|
87
87
|
|
|
88
|
+
def dump(self, *, prefix: str = '', indent: str = ' ', show_instructions: bool = True) -> None:
|
|
89
|
+
super().dump(prefix=prefix, indent=indent)
|
|
90
|
+
print(f'{prefix}number of instructions = {len(self.instructions)}')
|
|
91
|
+
if show_instructions:
|
|
92
|
+
print(f'{prefix}instructions:')
|
|
93
|
+
next_prefix: str = prefix + indent
|
|
94
|
+
for instruction in self.instructions:
|
|
95
|
+
print(f'{next_prefix}{instruction.to_str(self.var_indices, self.np_consts)}')
|
|
96
|
+
|
|
88
97
|
def __getstate__(self):
|
|
89
98
|
"""
|
|
90
99
|
Support for pickle.
|
|
@@ -123,7 +132,6 @@ def _make_instructions(
|
|
|
123
132
|
analysis: CircuitAnalysis,
|
|
124
133
|
dtype: DTypeNumeric,
|
|
125
134
|
) -> Tuple[Sequence[_Instruction], NDArrayNumeric]:
|
|
126
|
-
|
|
127
135
|
# Store const values in a numpy array
|
|
128
136
|
node_to_const_idx: Dict[int, int] = {
|
|
129
137
|
id(node): i
|
|
@@ -216,9 +224,32 @@ class _ElementID:
|
|
|
216
224
|
array: int # VARS, TMPS, CONSTS, RESULT
|
|
217
225
|
index: int # index into the array
|
|
218
226
|
|
|
227
|
+
def to_str(self, var_indices: Sequence[int], consts: NDArrayNumeric) -> str:
|
|
228
|
+
if self.array == _VARS:
|
|
229
|
+
return f'var[{var_indices[self.index]}]'
|
|
230
|
+
elif self.array == _TMPS:
|
|
231
|
+
return f'tmp[{self.index}]'
|
|
232
|
+
elif self.array == _CONSTS:
|
|
233
|
+
return str(consts.item(self.index))
|
|
234
|
+
elif self.array == _RESULT:
|
|
235
|
+
return f'result[{self.index}]'
|
|
236
|
+
else:
|
|
237
|
+
return f'?[{self.index}]'
|
|
238
|
+
|
|
219
239
|
|
|
220
240
|
@dataclass
|
|
221
241
|
class _Instruction:
|
|
222
242
|
operation: Callable
|
|
223
243
|
args: Sequence[_ElementID]
|
|
224
244
|
dest: _ElementID
|
|
245
|
+
|
|
246
|
+
def to_str(self, var_indices: Sequence[int], consts: NDArrayNumeric) -> str:
|
|
247
|
+
symbol: str
|
|
248
|
+
if self.operation is multiply:
|
|
249
|
+
symbol = 'mul'
|
|
250
|
+
elif self.operation == sum:
|
|
251
|
+
symbol = 'sum'
|
|
252
|
+
else:
|
|
253
|
+
symbol = '<?>'
|
|
254
|
+
args: str = ' '.join(elem.to_str(var_indices, consts) for elem in self.args)
|
|
255
|
+
return f'{self.dest.to_str(var_indices, consts)} = {symbol} {args}'
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
from __future__ import annotations
|
|
2
2
|
|
|
3
|
+
import ctypes as ct
|
|
3
4
|
from dataclasses import dataclass
|
|
4
5
|
from typing import Sequence, Optional, Tuple, List, Dict
|
|
5
6
|
|
|
6
7
|
import llvmlite.binding as llvm
|
|
7
8
|
import llvmlite.ir as ir
|
|
8
9
|
import numpy as np
|
|
9
|
-
import ctypes as ct
|
|
10
10
|
|
|
11
11
|
from .support.circuit_analyser import CircuitAnalysis, analyze_circuit
|
|
12
12
|
from .support.input_vars import InputVars, InferVars, infer_input_vars
|
|
@@ -126,6 +126,12 @@ class LLVMRawProgramWithArrays(LLVMRawProgram):
|
|
|
126
126
|
instructions: np.ndarray
|
|
127
127
|
consts: np.ndarray
|
|
128
128
|
|
|
129
|
+
def dump(self, *, prefix: str = '', indent: str = ' ', show_instructions: bool = True) -> None:
|
|
130
|
+
super().dump(prefix=prefix, indent=indent)
|
|
131
|
+
print(f'{prefix}LLVM byte code size = {len(self.instructions)}')
|
|
132
|
+
if show_instructions:
|
|
133
|
+
self.dump_llvm_program(prefix=prefix, indent=indent)
|
|
134
|
+
|
|
129
135
|
def __post_init__(self):
|
|
130
136
|
self._set_globals(self.instructions, _SET_INSTRUCTIONS_FUNCTION_NAME)
|
|
131
137
|
self._set_globals(self.consts, _SET_CONSTS_FUNCTION_NAME)
|
|
@@ -200,7 +206,7 @@ def _make_llvm_program(
|
|
|
200
206
|
consts_global.global_constant = True
|
|
201
207
|
consts_global.initializer = ir.Constant(consts_array_type, const_values)
|
|
202
208
|
data_idx_0 = ir.Constant(data_idx_type, 0)
|
|
203
|
-
consts: ir.Value = builder.gep(consts_global, [data_idx_0,
|
|
209
|
+
consts: ir.Value = builder.gep(consts_global, [data_idx_0, data_idx_0])
|
|
204
210
|
|
|
205
211
|
# Put bytecode into the LLVM module
|
|
206
212
|
instructions_array_type = ir.ArrayType(byte_type, len(byte_code))
|
|
@@ -218,7 +224,7 @@ def _make_llvm_program(
|
|
|
218
224
|
|
|
219
225
|
instructions_ptr_type = byte_type.as_pointer()
|
|
220
226
|
instructions_global = ir.GlobalVariable(module, instructions_ptr_type, name='instructions')
|
|
221
|
-
instructions_global.initializer =ir.Constant(instructions_ptr_type, None)
|
|
227
|
+
instructions_global.initializer = ir.Constant(instructions_ptr_type, None)
|
|
222
228
|
instructions: ir.Value = builder.load(instructions_global)
|
|
223
229
|
|
|
224
230
|
interp = _InterpBuilder(builder, type_info, inst_idx_type, data_idx_bytes, num_args_bytes, consts, instructions)
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"-O3"
|
|
16
16
|
],
|
|
17
17
|
"include_dirs": [
|
|
18
|
-
"/private/var/folders/y6/nj790rtn62lfktb1sh__79hc0000gn/T/build-env-
|
|
18
|
+
"/private/var/folders/y6/nj790rtn62lfktb1sh__79hc0000gn/T/build-env-x_3zb_va/lib/python3.12/site-packages/numpy/_core/include"
|
|
19
19
|
],
|
|
20
20
|
"name": "ck.circuit_compiler.support.circuit_analyser._circuit_analyser_cy",
|
|
21
21
|
"sources": [
|
|
Binary file
|
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
import ctypes as ct
|
|
2
2
|
from dataclasses import dataclass
|
|
3
3
|
from enum import Enum
|
|
4
|
-
from typing import Callable, Tuple, Optional
|
|
4
|
+
from typing import Callable, Tuple, Optional, List
|
|
5
5
|
|
|
6
6
|
import llvmlite.binding as llvm
|
|
7
7
|
import llvmlite.ir as ir
|
|
@@ -172,6 +172,23 @@ class LLVMRawProgram(RawProgram):
|
|
|
172
172
|
'opt': self.opt,
|
|
173
173
|
}
|
|
174
174
|
|
|
175
|
+
def dump(self, *, prefix: str = '', indent: str = ' ', show_instructions: bool = True) -> None:
|
|
176
|
+
super().dump(prefix=prefix, indent=indent)
|
|
177
|
+
print(f'{prefix}optimisation level = {self.opt}')
|
|
178
|
+
if show_instructions:
|
|
179
|
+
self.dump_llvm_program(prefix=prefix, indent=indent)
|
|
180
|
+
|
|
181
|
+
def dump_llvm_program(self, *, prefix: str = '', indent: str = ' ') -> None:
|
|
182
|
+
if self.llvm_program is None:
|
|
183
|
+
print(f'{prefix}LLVM program: unavailable')
|
|
184
|
+
else:
|
|
185
|
+
llvm_program: List[str] = self.llvm_program.split('\n')
|
|
186
|
+
print(f'{prefix}LLVM program size = {len(llvm_program)}')
|
|
187
|
+
print(f'{prefix}LLVM program:')
|
|
188
|
+
next_prefix: str = prefix + indent
|
|
189
|
+
for line in llvm_program:
|
|
190
|
+
print(f'{next_prefix}{line}')
|
|
191
|
+
|
|
175
192
|
def __setstate__(self, state):
|
|
176
193
|
"""
|
|
177
194
|
Support for pickle.
|
|
@@ -15,7 +15,7 @@
|
|
|
15
15
|
"-O3"
|
|
16
16
|
],
|
|
17
17
|
"include_dirs": [
|
|
18
|
-
"/private/var/folders/y6/nj790rtn62lfktb1sh__79hc0000gn/T/build-env-
|
|
18
|
+
"/private/var/folders/y6/nj790rtn62lfktb1sh__79hc0000gn/T/build-env-x_3zb_va/lib/python3.12/site-packages/numpy/_core/include"
|
|
19
19
|
],
|
|
20
20
|
"name": "ck.pgm_compiler.support.circuit_table._circuit_table_cy",
|
|
21
21
|
"sources": [
|
|
Binary file
|
ck/program/raw_program.py
CHANGED
|
@@ -21,7 +21,11 @@ class RawProgram:
|
|
|
21
21
|
A raw program is returned by a circuit compiler to provide execution of
|
|
22
22
|
the function defined by a compiled circuit.
|
|
23
23
|
|
|
24
|
-
A `RawProgram` is a `Callable
|
|
24
|
+
A `RawProgram` is a `Callable`; given an array of input variable values,
|
|
25
|
+
return a numpy array of result values. Calling a RawProgram is not necessarily
|
|
26
|
+
an efficient method for executing a program as buffers are reallocated for
|
|
27
|
+
each call. Alternatively, a `RawProgram` can be wrapped in a `ProgramBuffer`
|
|
28
|
+
for computationally efficient memory buffer reuse.
|
|
25
29
|
|
|
26
30
|
Fields:
|
|
27
31
|
function: is a function of three ctypes arrays, returning nothing.
|
|
@@ -40,19 +44,33 @@ class RawProgram:
|
|
|
40
44
|
number_of_results: int
|
|
41
45
|
var_indices: Sequence[int]
|
|
42
46
|
|
|
43
|
-
def __call__(self,
|
|
47
|
+
def __call__(self, var_values: NDArrayNumeric | Sequence[int | float] | int | float) -> NDArrayNumeric:
|
|
44
48
|
"""
|
|
45
|
-
Call the raw program as a function
|
|
49
|
+
Call the raw program as a function. This method will allocate numpy arrays of type `self.dtype`
|
|
50
|
+
for input, temporary, and output values. If `var_values` is a numpy array of the needed
|
|
51
|
+
dtype then it will be used directly.
|
|
52
|
+
|
|
53
|
+
Args:
|
|
54
|
+
var_values: the input variable values. This can be a numpy array, a Python sequence of
|
|
55
|
+
floats or int, or a single float or int. The number of input values must equal
|
|
56
|
+
`self.number_of_vars`.
|
|
57
|
+
|
|
58
|
+
Returns:
|
|
59
|
+
a numpy array of result values with shape `(self.number_of_results,)`.
|
|
46
60
|
"""
|
|
47
61
|
array_vars: NDArrayNumeric
|
|
48
62
|
if isinstance(vars, np.ndarray):
|
|
49
|
-
|
|
63
|
+
if var_values.dtype != self.dtype:
|
|
64
|
+
array_vars = var_values.astype(self.dtype)
|
|
65
|
+
else:
|
|
66
|
+
array_vars = self.dtype
|
|
67
|
+
elif isinstance(vars, (int, float)):
|
|
68
|
+
array_vars = np.array([var_values], dtype=self.dtype)
|
|
50
69
|
else:
|
|
51
|
-
array_vars = np.array(
|
|
70
|
+
array_vars = np.array(var_values, dtype=self.dtype)
|
|
71
|
+
|
|
52
72
|
if array_vars.shape != (self.number_of_vars,):
|
|
53
73
|
raise ValueError(f'input array incorrect shape: got {array_vars.shape} expected ({self.number_of_vars},)')
|
|
54
|
-
if array_vars.dtype != self.dtype:
|
|
55
|
-
raise ValueError(f'input array incorrect dtype: got {array_vars.dtype} expected {self.dtype}')
|
|
56
74
|
|
|
57
75
|
array_tmps: NDArrayNumeric = np.zeros(self.number_of_tmps, dtype=self.dtype)
|
|
58
76
|
array_outs: NDArrayNumeric = np.zeros(self.number_of_results, dtype=self.dtype)
|
|
@@ -64,3 +82,18 @@ class RawProgram:
|
|
|
64
82
|
|
|
65
83
|
self.function(c_array_vars, c_array_tmps, c_array_outs)
|
|
66
84
|
return array_outs
|
|
85
|
+
|
|
86
|
+
def dump(self, *, prefix: str = '', indent: str = ' ') -> None:
|
|
87
|
+
"""
|
|
88
|
+
Print a dump of the PGM.
|
|
89
|
+
This is intended for demonstration and debugging purposes.
|
|
90
|
+
|
|
91
|
+
Args:
|
|
92
|
+
prefix: optional prefix for indenting all lines.
|
|
93
|
+
indent: additional prefix to use for extra indentation.
|
|
94
|
+
"""
|
|
95
|
+
print(f'{prefix}{self.__class__.__name__}')
|
|
96
|
+
print(f'{prefix}signature = [{self.number_of_vars}] -> [{self.number_of_results}]')
|
|
97
|
+
print(f'{prefix}temps = {self.number_of_tmps}')
|
|
98
|
+
print(f'{prefix}dtype = {self.dtype}')
|
|
99
|
+
print(f'{prefix}var_indices = {self.var_indices}')
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from ck import example
|
|
2
|
+
from ck.pgm import RVMap
|
|
3
|
+
from ck.pgm_circuit.wmc_program import WMCProgram
|
|
4
|
+
from ck.pgm_compiler import NamedPGMCompiler
|
|
5
|
+
|
|
6
|
+
# create the example "Cancer" Bayesian network
|
|
7
|
+
pgm = example.Cancer()
|
|
8
|
+
|
|
9
|
+
# compile the PGM and construct an object for probabilistic queries
|
|
10
|
+
wmc = WMCProgram(NamedPGMCompiler.ACE(pgm))
|
|
11
|
+
|
|
12
|
+
# provide easy access to the random variables - not needed but simplifies this demo
|
|
13
|
+
rvs = RVMap(pgm)
|
|
14
|
+
|
|
15
|
+
# get the probability of having cancer given that pollution is high
|
|
16
|
+
pr = wmc.probability(rvs.cancer('True'), condition=rvs.pollution('high'))
|
|
17
|
+
|
|
18
|
+
print('probability of having cancer given that pollution is high =', pr)
|
|
File without changes
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
from ck import example
|
|
2
|
+
from ck.pgm import RVMap
|
|
3
|
+
from ck.pgm_circuit.wmc_program import WMCProgram
|
|
4
|
+
from ck.pgm_compiler import DEFAULT_PGM_COMPILER
|
|
5
|
+
|
|
6
|
+
# create the example "Cancer" Bayesian network
|
|
7
|
+
pgm = example.Cancer()
|
|
8
|
+
|
|
9
|
+
# compile the PGM and construct an object for probabilistic queries
|
|
10
|
+
wmc = WMCProgram(DEFAULT_PGM_COMPILER(pgm))
|
|
11
|
+
|
|
12
|
+
# provide easy access to the random variables - not needed but simplifies this demo
|
|
13
|
+
rvs = RVMap(pgm)
|
|
14
|
+
|
|
15
|
+
# get the probability of having cancer given that pollution is high
|
|
16
|
+
pr = wmc.probability(rvs.cancer('True'), condition=rvs.pollution('high'))
|
|
17
|
+
|
|
18
|
+
print('probability of having cancer given that pollution is high =', pr)
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
from ck.circuit import Circuit
|
|
2
|
+
from ck.circuit_compiler import NamedCircuitCompiler
|
|
3
|
+
|
|
4
|
+
|
|
5
|
+
def main() -> None:
|
|
6
|
+
cct = Circuit()
|
|
7
|
+
a, b, c, d = cct.new_vars(4)
|
|
8
|
+
top = a * b + c * d + 56.23
|
|
9
|
+
|
|
10
|
+
for compiler in NamedCircuitCompiler:
|
|
11
|
+
raw_program = compiler(top)
|
|
12
|
+
raw_program.dump()
|
|
13
|
+
print()
|
|
14
|
+
|
|
15
|
+
|
|
16
|
+
if __name__ == '__main__':
|
|
17
|
+
main()
|
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
Metadata-Version: 2.4
|
|
2
2
|
Name: compiled-knowledge
|
|
3
|
-
Version: 4.0.
|
|
3
|
+
Version: 4.0.0a23
|
|
4
4
|
Summary: A Python package for compiling and querying discrete probabilistic graphical models.
|
|
5
5
|
Author-email: Barry Drake <barry@compiledknowledge.org>
|
|
6
6
|
License-Expression: MIT
|
|
@@ -1,3 +1,8 @@
|
|
|
1
|
+
compiled_knowledge-4.0.0a23.dist-info/RECORD,,
|
|
2
|
+
compiled_knowledge-4.0.0a23.dist-info/WHEEL,sha256=V1loQ6TpxABu1APUg0MoTRBOzSKT5xVc3skizX-ovCU,136
|
|
3
|
+
compiled_knowledge-4.0.0a23.dist-info/top_level.txt,sha256=Cf8DAfd2vcnLiA7HlxoduOzV0Q-8surE3kzX8P9qdks,12
|
|
4
|
+
compiled_knowledge-4.0.0a23.dist-info/METADATA,sha256=gDe8xbB6tGtwczawUBO3R8EHKau7yc8dzdDEpZXfPTk,1788
|
|
5
|
+
compiled_knowledge-4.0.0a23.dist-info/licenses/LICENSE.txt,sha256=-LmkmqXKYojmS3zDxXAeTbsA82fnHA0KaRvpfIoEdjA,1068
|
|
1
6
|
ck_demos/all_demos.py,sha256=tqnMFbW6t1F4ksErf6QYTz9XtvbfayWl35lD3Bjm47E,2468
|
|
2
7
|
ck_demos/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
3
8
|
ck_demos/circuit/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
@@ -16,6 +21,7 @@ ck_demos/utils/compare.py,sha256=bJ8WTLJ0DRe7k9B7E2KD75IlWWBf6aSq7VRhYQjSmb0,496
|
|
|
16
21
|
ck_demos/programs/demo_program_none.py,sha256=Qjrk_gzRi4TtBlszqgaDwn0q5Kd9eZnbOY7MKImLEDs,461
|
|
17
22
|
ck_demos/programs/demo_program_buffer.py,sha256=DxlzFVuE7U2XJeb5LtFmDW3kix3I1a1dowvT-XfjAm4,626
|
|
18
23
|
ck_demos/programs/demo_program_multi.py,sha256=iWqhN6g9Yfinkb6m9pHuh-sN4pJlCUcb93TPdTJJfN8,600
|
|
24
|
+
ck_demos/programs/demo_raw_program_dump.py,sha256=Zx3LH84IndSSnvoMH0VnCbpwjCvukpKSlSjtaC6XLQ0,353
|
|
19
25
|
ck_demos/programs/demo_raw_program_llvm.py,sha256=ktUUMi5_wEWdrWf6Sz9U2JpmHPVijX-eiyS5hl-W9gM,502
|
|
20
26
|
ck_demos/programs/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
21
27
|
ck_demos/programs/demo_raw_program_interpreted.py,sha256=__zHAcaxCJ_EFqgfa8C0gatvjzH-clKGMwdN9KbyNxQ,507
|
|
@@ -38,6 +44,7 @@ ck_demos/circuit_compiler/show_llvm_program.py,sha256=tlL3TRScb_Mqc-Mbj0M2-WCSPS
|
|
|
38
44
|
ck_demos/ace/demo_ace.py,sha256=wsWETo1r2GG86Mzo2VD4J1TZiVlvYjVC5A-WMs-zcsw,1409
|
|
39
45
|
ck_demos/ace/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
40
46
|
ck_demos/ace/copy_ace_to_ck.py,sha256=t0Wg1-8_f9YPlEbBp-YGJ0N3u7a3ZIqcI70MOeV5Kgo,288
|
|
47
|
+
ck_demos/ace/simple_ace_demo.py,sha256=VqvudQfvlYqgLZTKGAfuhBQt5U_vkBuhKT7LuonEN3w,646
|
|
41
48
|
ck_demos/pgm/show_examples.py,sha256=-35_E9psOX16VvqArZUr9uoUYD6_AmiE3nLEny9REes,436
|
|
42
49
|
ck_demos/pgm/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
43
50
|
ck_demos/pgm/demo_pgm_dump_stress.py,sha256=urWeg4K7Fe718mPybN7gcWLbfcQt9XxgetwWUOmDmLc,267
|
|
@@ -48,11 +55,8 @@ ck_demos/pgm_inference/demo_inferencing_wmc_student.py,sha256=R2dajAh4-bb9YFWhQr
|
|
|
48
55
|
ck_demos/pgm_inference/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
49
56
|
ck_demos/pgm_inference/demo_inferencing_mpe_cancer.py,sha256=hS9U2kyqjFgJ8jnVBtT3Tk6qCTkuknNiTlImMejV_6A,1554
|
|
50
57
|
ck_demos/pgm_inference/demo_inferencing_wmc_and_mpe_sprinkler.py,sha256=-q4Z1Fzf7-BuwVFTFXdGRY-zUNrY-SAU7ooaov2o_lM,5128
|
|
51
|
-
|
|
52
|
-
|
|
53
|
-
compiled_knowledge-4.0.0a22.dist-info/top_level.txt,sha256=Cf8DAfd2vcnLiA7HlxoduOzV0Q-8surE3kzX8P9qdks,12
|
|
54
|
-
compiled_knowledge-4.0.0a22.dist-info/METADATA,sha256=SBeD1euhwu4b1MT7eVLdrS9F3eEkOiCemxxTwthgkWc,1788
|
|
55
|
-
compiled_knowledge-4.0.0a22.dist-info/licenses/LICENSE.txt,sha256=-LmkmqXKYojmS3zDxXAeTbsA82fnHA0KaRvpfIoEdjA,1068
|
|
58
|
+
ck_demos/getting_started/simple_demo.py,sha256=hiYscNnfkEwHCQ3ymXAswAYO5jAKR7cseb36pjzuus8,650
|
|
59
|
+
ck_demos/getting_started/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
56
60
|
ck/pgm.py,sha256=T7ZXWEgn93uh8mf-FxOgkPY8RgabBCUg2lpKoQmhGMU,117226
|
|
57
61
|
ck/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
58
62
|
ck/pgm_circuit/target_marginals_program.py,sha256=qWz9FkAFzt8YHLZJzPkpRnvDH76BXm-dcEWhoqCkrOw,3665
|
|
@@ -100,13 +104,13 @@ ck/example/sprinkler.py,sha256=t8RIiPorf3QXYyTXbxFkSrK1SsG5ALWmTfTsFUq2C_c,938
|
|
|
100
104
|
ck/example/diamond_square.py,sha256=HqGqmInYTpAsCBuz3s8FuhT2Jnc11L3wGCTgJDmFLjw,2722
|
|
101
105
|
ck/example/rain.py,sha256=kLTU_9f_-_yy0ymPnS9-cbFVT6fYyCanDgszk3vQOgc,1187
|
|
102
106
|
ck/example/cancer.py,sha256=-FnLbfb9yxriLl97N5BDZ0VrDZ5UnOWlT-Ep_tzO6QI,1698
|
|
103
|
-
ck/circuit/_circuit_cy.c,sha256=
|
|
104
|
-
ck/circuit/_circuit_cy.pyx,sha256=
|
|
107
|
+
ck/circuit/_circuit_cy.c,sha256=kermLDIHi6yS0k1TOUJyem1DpI8xsldsRnzsMsYU-Tw,1704292
|
|
108
|
+
ck/circuit/_circuit_cy.pyx,sha256=mER1HK5yyf4UAj9ibn7fUQNyXwoxwxp7PClULPhY9B4,26995
|
|
105
109
|
ck/circuit/__init__.py,sha256=B1jwDE_Xb6hOQE8DecjaTVotOnDxJaT7jsvPfGDXqCU,401
|
|
106
110
|
ck/circuit/_circuit_cy.pxd,sha256=ZcW8xjw4oGQqD5gwz73GXc1H8NxpdAswFWzc2CUWWcA,1025
|
|
107
|
-
ck/circuit/_circuit_py.py,sha256=
|
|
111
|
+
ck/circuit/_circuit_py.py,sha256=hADjCFDC1LJKUdyiKZzNLFt7ZkUNJ0IYwEYRj594K4g,27495
|
|
108
112
|
ck/circuit/tmp_const.py,sha256=wgi4P3xrTRLPXNMmWYpYaJWlm-lekQOdxg4rkXZC3Wk,2298
|
|
109
|
-
ck/circuit/_circuit_cy.cpython-312-darwin.so,sha256=
|
|
113
|
+
ck/circuit/_circuit_cy.cpython-312-darwin.so,sha256=7-UJeNOgci0GpUay5i5nO4cs9xO7rgbTv1EyTxOSdeA,335296
|
|
110
114
|
ck/sampling/wmc_metropolis_sampler.py,sha256=jfXb-MG0jAoMyepgq9zel2amqK-gmYrCtKuxJStl8VY,6305
|
|
111
115
|
ck/sampling/wmc_direct_sampler.py,sha256=Pkv-u4GjN3npBrcQ92raoTrEIel1vpiDoE8LrlcfYJE,7094
|
|
112
116
|
ck/sampling/sampler_support.py,sha256=SQytIhr3qlIcNjYu9cF7sAxhjiXFLlCwPlsMIOJVH1c,9510
|
|
@@ -139,11 +143,11 @@ ck/pgm_compiler/support/named_compiler_maker.py,sha256=Qz8a9gwY46Q3dtRCZEZ2czq5z
|
|
|
139
143
|
ck/pgm_compiler/support/circuit_table/__init__.py,sha256=1kWjAZR5Rj6PYNdbCEbuyE2VtIDQU4Qf-3HPFzBlezs,562
|
|
140
144
|
ck/pgm_compiler/support/circuit_table/_circuit_table_cy.pyx,sha256=Fsjw8P9clKQioqlLyr1JirUK5oYkeotpDMy5sMo7Khk,11683
|
|
141
145
|
ck/pgm_compiler/support/circuit_table/_circuit_table_py.py,sha256=OZJC-JGX3ovCSv7nJtNYq7735KZ2eb4TQOlZdZbhPmk,10983
|
|
142
|
-
ck/pgm_compiler/support/circuit_table/_circuit_table_cy.c,sha256=
|
|
143
|
-
ck/pgm_compiler/support/circuit_table/_circuit_table_cy.cpython-312-darwin.so,sha256=
|
|
146
|
+
ck/pgm_compiler/support/circuit_table/_circuit_table_cy.c,sha256=I99NbkY8OWiHNG978hQgxQW75qVEXHNyUlglOcccFJA,714044
|
|
147
|
+
ck/pgm_compiler/support/circuit_table/_circuit_table_cy.cpython-312-darwin.so,sha256=I_voa6GdQ_Quk96PoYhOJt1sbjuvXeKCcFmHeHxEAh4,165096
|
|
144
148
|
ck/pgm_compiler/ace/ace.py,sha256=An83dHxE_gQFcEs6H5qgm0PlNFnJSGGuvLJNC2H3hGU,10098
|
|
145
149
|
ck/pgm_compiler/ace/__init__.py,sha256=5HWep-yL1Mr6z5VWEaIYpLumCdeso85J-l_-hQaVusM,96
|
|
146
|
-
ck/program/raw_program.py,sha256=
|
|
150
|
+
ck/program/raw_program.py,sha256=aUYLEcK8mstDspz6M9wOE1W7TrnDNBmJjPtfIVA3CLw,4158
|
|
147
151
|
ck/program/program_buffer.py,sha256=IHwAHTKIaUlhcbNFTuSxPWKyExIsOxxX6ffUn4KfheU,5485
|
|
148
152
|
ck/program/__init__.py,sha256=Rifdxk-l6cCjXLpwc6Q0pVXNDsllAwaFlRqRx3cURho,107
|
|
149
153
|
ck/program/program.py,sha256=ohsnE0CEy8O4q8uGB_YEjoJKAPhY1Mz_a08Z7fy7TLw,4047
|
|
@@ -151,20 +155,20 @@ ck/circuit_compiler/llvm_compiler.py,sha256=SFhfrthrDuAYUjH_DYRD7FBU8eg2db5T4QGB
|
|
|
151
155
|
ck/circuit_compiler/circuit_compiler.py,sha256=bb-SG8xrcp4y2nCNB0GIWcgOufEsR2Nb52qtrLMHTVs,992
|
|
152
156
|
ck/circuit_compiler/__init__.py,sha256=eRN6chBEt64PK5e6EFGyBNZBn6BXhXb6R3m12zPA1Qg,130
|
|
153
157
|
ck/circuit_compiler/named_circuit_compilers.py,sha256=paKyG876tdG_bdSHJU6KW5HxQrutmV_T80GPpz8A65s,2227
|
|
154
|
-
ck/circuit_compiler/interpret_compiler.py,sha256=
|
|
155
|
-
ck/circuit_compiler/llvm_vm_compiler.py,sha256
|
|
156
|
-
ck/circuit_compiler/cython_vm_compiler/cython_vm_compiler.py,sha256=
|
|
158
|
+
ck/circuit_compiler/interpret_compiler.py,sha256=tZirNkAOe7evvray4-wOqO-KdaI39qRFEI0xD6IRBY0,8531
|
|
159
|
+
ck/circuit_compiler/llvm_vm_compiler.py,sha256=rM_6F5st3k9X5K1_MwzKJwDhQo1794vooPJ5yKrgSX8,21648
|
|
160
|
+
ck/circuit_compiler/cython_vm_compiler/cython_vm_compiler.py,sha256=GdtBkipud8vylXYArOJvZ-10U9L_PL0oJrkyrnFGH2Q,4345
|
|
157
161
|
ck/circuit_compiler/cython_vm_compiler/__init__.py,sha256=ks0sISOJ-XHIHgHnESyFsheNWvcSJQkbsrj1wVlnzTE,48
|
|
158
|
-
ck/circuit_compiler/cython_vm_compiler/_compiler.pyx,sha256=
|
|
159
|
-
ck/circuit_compiler/cython_vm_compiler/_compiler.cpython-312-darwin.so,sha256=
|
|
160
|
-
ck/circuit_compiler/cython_vm_compiler/_compiler.c,sha256=
|
|
161
|
-
ck/circuit_compiler/support/llvm_ir_function.py,sha256=
|
|
162
|
+
ck/circuit_compiler/cython_vm_compiler/_compiler.pyx,sha256=RssdkoAcB3Ahes8xisqFy0PQyOPmC3GLEC2xR-miQaE,12898
|
|
163
|
+
ck/circuit_compiler/cython_vm_compiler/_compiler.cpython-312-darwin.so,sha256=vZH0jjcVIutMnWnJn3895m4ord0Gragcz_TxJvxN2VQ,163488
|
|
164
|
+
ck/circuit_compiler/cython_vm_compiler/_compiler.c,sha256=npJssDTeghG-2xRm_yIOA_-O4fWNMmJQsYyFbLuYYKs,857789
|
|
165
|
+
ck/circuit_compiler/support/llvm_ir_function.py,sha256=1uC4gAu2g1nl9lycMN2sM7FMI_h4iJG_ufZ3Gc3rMA8,8361
|
|
162
166
|
ck/circuit_compiler/support/input_vars.py,sha256=EZrvyhD9XVtf5GuDBluFNWhAOVixP7-_ETxAHLTpBcs,4664
|
|
163
167
|
ck/circuit_compiler/support/__init__.py,sha256=47DEQpj8HBSa-_TImW-5JCeuQeRkm5NMpJWZG3hSuFU,0
|
|
164
168
|
ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_cy.pyx,sha256=a0fKmkwRNscJmy6qoO2AOqJYmHYptrQmkRSrDg3G-wg,3233
|
|
165
169
|
ck/circuit_compiler/support/circuit_analyser/__init__.py,sha256=WhNwfg7GHVeI4k_m7owPGWxX0MyZg_wtcp2MA07qbWg,523
|
|
166
|
-
ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_cy.cpython-312-darwin.so,sha256=
|
|
167
|
-
ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_cy.c,sha256=
|
|
170
|
+
ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_cy.cpython-312-darwin.so,sha256=8LZvLB-ijTyXMb1TS6OYFR1fzCfAFlAbZyF2bDBsJSI,104936
|
|
171
|
+
ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_cy.c,sha256=zI2h572_2OGeEE-6Wtuj8RCUw_a6lUEjUPegh_YUb20,438223
|
|
168
172
|
ck/circuit_compiler/support/circuit_analyser/_circuit_analyser_py.py,sha256=CMdXV6Rot5CCoK1UsurQdGK0UOx_09B6V7mCc_6-gfI,2993
|
|
169
173
|
ck/in_out/render_net.py,sha256=VePvN6aYWuzEkW-Hv-qGT9QneOvsnrBMmS_KYueuj2I,4970
|
|
170
174
|
ck/in_out/render_bugs.py,sha256=c39KbaD4gEiauFsZq2KUhDEEa-3cuY5kuvz97pEWVpw,3272
|
|
File without changes
|
{compiled_knowledge-4.0.0a22.dist-info → compiled_knowledge-4.0.0a23.dist-info}/licenses/LICENSE.txt
RENAMED
|
File without changes
|
{compiled_knowledge-4.0.0a22.dist-info → compiled_knowledge-4.0.0a23.dist-info}/top_level.txt
RENAMED
|
File without changes
|