GeezerVison is showing github.com — Change PyObject sort to be pickled form instead of reference · mjdhasan/egglog-python@1fef16a
File treeExpand file treeCollapse file tree Open diff view settings __snapshots__/test_bindings
Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: .gitignore +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -85,3 +85,4 @@ inlined | 85 | 85 | visualizer.tgz | 86 | 86 | package | 87 | 87 | .mypy_cache/ | | 88 | +*.json |
Collapse file Copy file name to clipboardExpand all lines: Cargo.lock +7Lines changed: 7 additions & 0 deletions Collapse file Copy file name to clipboardExpand all lines: Cargo.toml +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -29,6 +29,7 @@ lalrpop-util = { version = "0.22", features = ["lexer"] } | 29 | 29 | ordered-float = "5" | 30 | 30 | uuid = { version = "1.18", features = ["v4"] } | 31 | 31 | rayon = "1.11" | | 32 | +base64 = "0.22.1" | 32 | 33 |
| 33 | 34 | # Use patched version of egglog in experimental | 34 | 35 | [patch.'https://github.com/egraphs-good/egglog'] | |
Collapse file Copy file name to clipboardExpand all lines: docs/reference/python-integration.md +31-19Lines changed: 31 additions & 19 deletions - Display the source diff
- Display the rich diff
| Original file line number | Diff line number | Diff line change |
|---|
@@ -96,43 +96,55 @@ We define a custom "primitive sort" (i.e. a builtin type) for `PyObject`s. This | 96 | 96 |
| 97 | 97 | ### Saving Python Objects | 98 | 98 |
| 99 | | -To create an expression of type `PyObject`, we call the call the constructor with any Python object. It will | 100 | | -save a reference to the object: | | 99 | +To create an expression of type `PyObject`, call the constructor with any Python object. The value is immediately | | 100 | +serialized with `cloudpickle.dumps`, and the serialized bytes (base64 encoded when printed) are what get stored | | 101 | +inside the e-graph. This means the e-graph keeps a snapshot of the object rather than a live reference. | 101 | 102 |
| 102 | 103 | ```{code-cell} python | 103 | | -PyObject(1) | 104 | | -``` | 105 | | - | 106 | | -We see that this as saved internally as a pointer to the Python object. For hashable objects like `int` we store two integers, a hash of the type and a has of the value. | | 104 | +from dataclasses import dataclass | 107 | 105 |
| 108 | | -We can also store unhashable objects in the e-graph like lists. | | 106 | +@dataclass | | 107 | +class MyObject: | | 108 | + a: int = 10 | 109 | 109 |
| 110 | | -```{code-cell} python | 111 | | -lst = PyObject([1, 2, 3]) | 112 | | -lst | | 110 | +PyObject(MyObject()) | 113 | 111 | ``` | 114 | 112 |
| 115 | | -We see that this is stored with one number, simply the `id` of the object. | | 113 | +The new serialization approach works for both hashable and unhashable Python values, and no longer depends on | | 114 | +their `id()`. Subsequent inserts of equal values round-trip through `cloudpickle` so the e-graph can identify and | | 115 | +merge them by value. | 116 | 116 |
| 117 | | -```{admonition} Mutable Objects | 118 | | -:class: warning | | 117 | +```{admonition} Serialization requirements | | 118 | +:class: note | 119 | 119 |
| 120 | | -While it is possible to store unhashable objects in the e-graph, you have to be careful defining any rules which create new unhashable objects. If each time a rule is run, it creates a new object, then the e-graph will never saturate. | 121 | | -
| 122 | | -Creating hashable objects is safer, since while the rule might create new Python objects each time it executes, they should have the same hash, i.e. be equal, so that the e-graph can saturate. | | 120 | +`PyObject` relies on `cloudpickle`. Any object you store must be serializable by `cloudpickle.dumps`; objects such | | 121 | +as open file handles, generators, or extension types that `cloudpickle` cannot handle will raise an error when you | | 122 | +try to construct a `PyObject`. | 123 | 123 | ``` | 124 | 124 |
| 125 | 125 | ### Retrieving Python Objects | 126 | 126 |
| 127 | | -Like other primitives, we can retrieve the Python object from the e-graph by using the `.value` property: | | 127 | +Like other primitives, we can retrieve a Python object by using the `.value` property. Deserialization happens on | | 128 | +every access, so you receive a fresh copy each time rather than the original object. | 128 | 129 |
| 129 | 130 | ```{code-cell} python | 130 | | -assert lst.value == [1, 2, 3] | | 131 | +original = {"count": 1} | | 132 | +expr = PyObject(original) | | 133 | +
| | 134 | +restored = expr.value | | 135 | +assert restored == original | | 136 | +assert restored is not original | | 137 | +
| | 138 | +# Mutating the copy does not affect the stored value. | | 139 | +restored["count"] = 2 | | 140 | +assert expr.value == {"count": 1} | 131 | 141 | ``` | 132 | 142 |
| 133 | 143 | ### Builtin methods | 134 | 144 |
| 135 | | -Currently, we only support a few methods on `PyObject`s, but we plan to add more in the future. | | 145 | +Currently, we only support a few methods on `PyObject`s, but we plan to add more in the future. Each builtin | | 146 | +deserializes its inputs, performs the operation in Python, and then serializes the result back into a new | | 147 | +`PyObject`, so previously stored values remain unchanged. | 136 | 148 |
| 137 | 149 | Conversion to/from a string: | 138 | 150 |
| |
Collapse file Copy file name to clipboardExpand all lines: pyproject.toml +1Lines changed: 1 addition & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -37,6 +37,7 @@ array = [ | 37 | 37 | "numba>=0.59.1", | 38 | 38 | "llvmlite>=0.42.0", | 39 | 39 | "numpy>2", | | 40 | + "cloudpickle>=3", | 40 | 41 | ] | 41 | 42 | dev = [ | 42 | 43 | "ruff", | |
Collapse file Copy file name to clipboardExpand all lines: python/egglog/bindings.pyi +1-9Lines changed: 1 addition & 9 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -54,7 +54,6 @@ __all__ = [ | 54 | 54 | "PrintOverallStatistics", | 55 | 55 | "PrintSize", | 56 | 56 | "Push", | 57 | | - "PyObjectSort", | 58 | 57 | "Relation", | 59 | 58 | "Repeat", | 60 | 59 | "Rewrite", | @@ -105,17 +104,10 @@ class SerializedEGraph: | 105 | 104 | def map_ops(self, map: dict[str, str]) -> None: ... | 106 | 105 | def split_classes(self, egraph: EGraph, ops: set[str]) -> None: ... | 107 | 106 |
| 108 | | -@final | 109 | | -class PyObjectSort: | 110 | | - def __init__(self) -> None: ... | 111 | | - def store(self, __o: object, /) -> _Expr: ... | 112 | | - def load(self, __e: _Expr, /) -> object: ... | 113 | | - | 114 | 107 | @final | 115 | 108 | class EGraph: | 116 | 109 | def __init__( | 117 | 110 | self, | 118 | | - py_object_sort: PyObjectSort | None = None, | 119 | 111 | /, | 120 | 112 | *, | 121 | 113 | fact_directory: str | Path | None = None, | @@ -142,7 +134,7 @@ class EGraph: | 142 | 134 | def value_to_rational(self, v: Value) -> Fraction: ... | 143 | 135 | def value_to_bigint(self, v: Value) -> int: ... | 144 | 136 | def value_to_bigrat(self, v: Value) -> Fraction: ... | 145 | | - def value_to_pyobject(self, py_object_sort: PyObjectSort, v: Value) -> object: ... | | 137 | + def value_to_pyobject(self, v: Value) -> object: ... | 146 | 138 | def value_to_map(self, v: Value) -> dict[Value, Value]: ... | 147 | 139 | def value_to_multiset(self, v: Value) -> list[Value]: ... | 148 | 140 | def value_to_vec(self, v: Value) -> list[Value]: ... | |
Collapse file Copy file name to clipboardExpand all lines: python/egglog/builtins.py +7-12Lines changed: 7 additions & 12 deletions | Original file line number | Diff line number | Diff line change |
|---|
| 13 | 13 | from types import FunctionType, MethodType | 14 | 14 | from typing import TYPE_CHECKING, Generic, Protocol, TypeAlias, TypeVar, cast, overload | 15 | 15 |
| | 16 | +import cloudpickle | 16 | 17 | from typing_extensions import TypeVarTuple, Unpack, deprecated | 17 | 18 |
| 18 | 19 | from .conversion import convert, converter, get_type_args, resolve_literal | @@ -984,12 +985,15 @@ def value(self) -> object: | 984 | 985 | expr = cast("RuntimeExpr", self).__egg_typed_expr__.expr | 985 | 986 | if not isinstance(expr, PyObjectDecl): | 986 | 987 | raise ExprValueError(self, "PyObject(x)") | 987 | | - return expr.value | | 988 | + return cloudpickle.loads(expr.pickled) | 988 | 989 |
| 989 | 990 | __match_args__ = ("value",) | 990 | 991 |
| 991 | 992 | def __init__(self, value: object) -> None: ... | 992 | 993 |
| | 994 | + @method(egg_fn="py-call") | | 995 | + def __call__(self, *args: object) -> PyObject: ... | | 996 | + | 993 | 997 | @method(egg_fn="py-from-string") | 994 | 998 | @classmethod | 995 | 999 | def from_string(cls, s: StringLike) -> PyObject: ... | @@ -1023,24 +1027,15 @@ class PyObjectFunction(Protocol): | 1023 | 1027 | def __call__(self, *__args: PyObject) -> PyObject: ... | 1024 | 1028 |
| 1025 | 1029 |
| | 1030 | +@deprecated("use PyObject(fn) directly") | 1026 | 1031 | def py_eval_fn(fn: Callable) -> PyObjectFunction: | 1027 | 1032 | """ | 1028 | 1033 | Takes a python callable and maps it to a callable which takes and returns PyObjects. | 1029 | 1034 |
| 1030 | 1035 | It translates it to a call which uses `py_eval` to call the function, passing in the | 1031 | 1036 | args as locals, and using the globals from function. | 1032 | 1037 | """ | 1033 | | - | 1034 | | - def inner(*__args: PyObject, __fn: Callable = fn) -> PyObject: | 1035 | | - new_kvs: list[object] = [] | 1036 | | - eval_str = "__fn(" | 1037 | | - for i, arg in enumerate(__args): | 1038 | | - new_kvs.extend((f"__arg_{i}", arg)) | 1039 | | - eval_str += f"__arg_{i}, " | 1040 | | - eval_str += ")" | 1041 | | - return py_eval(eval_str, PyObject({"__fn": __fn}).dict_update(*new_kvs), getattr(__fn, "__globals__", {})) | 1042 | | - | 1043 | | - return inner | | 1038 | + return PyObject(fn) | 1044 | 1039 |
| 1045 | 1040 |
| 1046 | 1041 | @function(builtin=True, egg_fn="py-exec") | |
Collapse file Copy file name to clipboardExpand all lines: python/egglog/declarations.py +1-17Lines changed: 1 addition & 17 deletions | Original file line number | Diff line number | Diff line change |
|---|
@@ -617,23 +617,7 @@ class LetRefDecl: | 617 | 617 |
| 618 | 618 | @dataclass(frozen=True) | 619 | 619 | class PyObjectDecl: | 620 | | - value: object | 621 | | - | 622 | | - def __hash__(self) -> int: | 623 | | - """Tries using the hash of the value, if unhashable use the ID.""" | 624 | | - try: | 625 | | - return hash((type(self.value), self.value)) | 626 | | - except TypeError: | 627 | | - return id(self.value) | 628 | | - | 629 | | - def __eq__(self, other: object) -> bool: | 630 | | - if not isinstance(other, PyObjectDecl): | 631 | | - return False | 632 | | - return self.parts == other.parts | 633 | | - | 634 | | - @property | 635 | | - def parts(self) -> tuple[type, object]: | 636 | | - return (type(self.value), self.value) | | 620 | + pickled: bytes | 637 | 621 |
| 638 | 622 |
| 639 | 623 | LitType: TypeAlias = int | str | float | bool | None | |
Collapse file Copy file name to clipboardExpand all lines: python/egglog/deconstruct.py +2-1Lines changed: 2 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 8 | 8 | from functools import partial | 9 | 9 | from typing import TYPE_CHECKING, TypeVar, overload | 10 | 10 |
| | 11 | +import cloudpickle | 11 | 12 | from typing_extensions import TypeVarTuple, Unpack | 12 | 13 |
| 13 | 14 | from .declarations import * | @@ -64,7 +65,7 @@ def get_literal_value(x: object) -> object: | 64 | 65 | case LitDecl(v): | 65 | 66 | return v | 66 | 67 | case PyObjectDecl(obj): | 67 | | - return obj | | 68 | + return cloudpickle.loads(obj) | 68 | 69 | case PartialCallDecl(call): | 69 | 70 | fn, args = _deconstruct_call_decl(x.__egg_decls_thunk__, call) | 70 | 71 | if not args: | |
Collapse file Copy file name to clipboardExpand all lines: python/egglog/egraph.py +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -839,7 +839,7 @@ class EGraph: | 839 | 839 | _token_stack: list[EGraph] = field(default_factory=list, repr=False) | 840 | 840 |
| 841 | 841 | def __post_init__(self, seminaive: bool, save_egglog_string: bool) -> None: | 842 | | - egraph = bindings.EGraph(GLOBAL_PY_OBJECT_SORT, seminaive=seminaive, record=save_egglog_string) | | 842 | + egraph = bindings.EGraph(seminaive=seminaive, record=save_egglog_string) | 843 | 843 | self._state = EGraphState(egraph) | 844 | 844 |
| 845 | 845 | def _add_decls(self, *decls: DeclerationsLike) -> None: | |
|