Allow freezing of FunctionGraph for hashing#1908
Allow freezing of FunctionGraph for hashing#1908jessegrabowski wants to merge 6 commits intopymc-devs:mainfrom
Conversation
ricardoV94
left a comment
There was a problem hiding this comment.
Why did you not go all out?
If you already deduplicate and do internal hash-cons you are one step away from getting hashing for free across different FunctionGraphs. Just do the hash-cons globally. Then FrozenFunctionGrahp([x, y], [foo(x, y)] is equal to another functiongraph if and only if fgraph.outputs == other_fgraph.outputs. No need for recursive hashing or expensive equal_computations.
As it stands you are not doing much better sneaking a default MergeOptimizer at __init__ and adding a FunctionGraph class that has no replace mode.
And cheap hashing/ equality is not just a nice to have, it's really valuable to not slow down compilation. In some of my benchmarks on previous work, some graphs could spend inordinate time on equality checks.
Comments regardless of whether we go:
- Don't create
FrozenFunctionGraphas a subclass ofFrozenGraph, let's push the general principle, shared abstract classes, no-subclass of actually realized objects. Then you don't needcheck_frozen, the methods just don't exist for the frozen subclass. - You could create a frozenApply that uses
tuplefor input/outputs instead oflist. That will help ensuring the immutability because all our current rewrite machinery works on the idea of overriding entries in those lists. Accidentally trying to mutate a graph would 99% fail there.
305e26e to
08609fe
Compare
There was a problem hiding this comment.
This is starting to look good, how are you feeling about it?
Notes:
- Add a
FrozenFunctionGraph.unfreeze(), that yields aFunctionGraph? - Really try to avoid the FrozenConstant stuff
- Ops with inner graph (at least the ones you touched now) should only have a FrozenFunctionGraph internally (not a mutable one as well). Maybe that's already the case.
We need some follow-up issues open:
- Optimizing OpFromGraph: There should be an explicit rewrite that creates a new OpFromGraph with its updated frozen graph, (so it is also reflected immediately in dprint). We should never do any further rewrites of the internal fgraph during compilation.
- Scan/Minimize/Root: Use the new FrozenFunctionGraph as well. This should immediately address #1601
- When compiling OpFromGraph in jitted contexts we should try to avoid recreating inner numba/jax functions when the same OFG is compiled multiple times in a function, this will likely speedup compilation. In the C-backend that already happens due to the caching of
_fn. That's how we can deliver on the promised compilations speedups and it's specially relevant for a library likepytensor-mlthat may want to chains hundreds of the same "LayerOp"s in sequence
| def clone(self, **kwargs): | ||
| return self | ||
|
|
||
| def equals(self, other): |
There was a problem hiding this comment.
Why in the base class. For instance the np.array_equal looks very tensor oriented, but we have types like Slice, RNG, ...
There was a problem hiding this comment.
I think this commit was needed in an intermediate form, but now can simply be dropped.
| return self.data | ||
|
|
||
|
|
||
| class FrozenConstant(Constant): |
There was a problem hiding this comment.
Again sounds like it's specializing on numerical array types. How does MergeOptimizer find that two constants are equal for merging? Can we reuse that logic?
I wouldn't expect we need a FrozenConstant class in the end, since Constants are frozen by our standards already. The challenge here is more finding whether a new constant was already seen before?
| from pytensor.graph.op import HasInnerGraph | ||
|
|
||
| new_op = self.op | ||
| if isinstance(new_op, HasInnerGraph) and clone_inner_graph: |
There was a problem hiding this comment.
check the boolean clone_inner_graph first which is cheaper than the isinstance
| resolved_outputs.append(mapped) | ||
| self.outputs: tuple[Variable, ...] = tuple(resolved_outputs) | ||
|
|
||
| self._structural_hash: int = hash(tuple(var_hash[o] for o in self.outputs)) |
There was a problem hiding this comment.
May be right, but why do we need to hash intermediate variables? Can't we just hash the outputs?
There was a problem hiding this comment.
I was just erring on the side of caution. Can you think of a case where two graphs with different inputs would lead to different outputs?
There was a problem hiding this comment.
You mean with equal inputs? But no, if we implement it correctly it shouldn't happen
| # Hash match but output identity mismatch — likely a hash collision | ||
| # or interning bug. Fall back to structural comparison. | ||
| import warnings | ||
|
|
There was a problem hiding this comment.
Why? If it's a bug raise. You already showed the outputs are different
|
|
||
| if isinstance(x.type, TensorType) and x.type.ndim == 0: | ||
| return scalar_from_tensor(x) | ||
| elif isinstance(x, Constant) and isinstance(x.type, ScalarType): |
There was a problem hiding this comment.
Bug due to FrozenConstants? I really don't think we should have those, because they'll lack all the attributes that ScalarVariables have, and they will fail isinstance(x, ScalarVariable) checks
| @@ -4140,38 +4116,17 @@ def prepare_node(self, node, storage_map, compute_map, impl): | |||
| def __eq__(self, other): | |||
| if self is other: | |||
There was a problem hiding this comment.
can't we have regular __props__ based equality/hashing now?
| self.outputs_type = tuple(output.type for output in self.outputs) | ||
| self.nin = len(inputs) | ||
| self.nout = len(outputs) | ||
| fgraph = FunctionGraph(inputs, outputs, clone=clone_graph) |
There was a problem hiding this comment.
Can't we build a frozen fgraph immediately? This is doing double effort. Also we can get ride of the clone_graph now
There was a problem hiding this comment.
most of the make_node methods do graph mutations. For example, Composite has this logic to flatten a sequence of nested Composites using rebuild_collect_shared, which wants to do a clone with new inputs eventually.
I could try to rip out/update this machinery too? I am already working on an unfreeze method as a work-around (you brought this up in another comment), but idk if we judge this as too expensive.
There was a problem hiding this comment.
The nested thing is also an eager rewrite mistake, it should be its own rewrite unless somehow compilation fails if Composite has another Composite inside (which I don't think so, because ScalarLoop can definitely handle other Composite/ScalarLoop inside)
There was a problem hiding this comment.
agreed i was thinking the same thing, but not sure it's for this PR.
Anyway we will still have to unfreeze the fgraph at some point, whether during the rewrite or during init. We already have local_inline_composite_constants that mutates Composites and will need to unfreeze -> modify -> refreeze.
There was a problem hiding this comment.
I just mean no point in having two FunctionGraph inside Composite
There was a problem hiding this comment.
yep I'm on the same page
| @@ -4273,12 +4209,6 @@ def __str__(self): | |||
|
|
|||
| @property | |||
There was a problem hiding this comment.
Remove the property, if it already exists just store it as self.fgraph
| e = x + y * x | ||
|
|
||
| op1 = OpFromGraph([x, y], [e]) | ||
| op2 = OpFromGraph([x, y], [e]) |
There was a problem hiding this comment.
Can you also test with distinct x, y variables? They should still be identical since there are just nominal/dummies
| return FrozenFunctionGraph(self.inputs, self.outputs) | ||
|
|
||
|
|
||
| class FrozenFunctionGraph: |
There was a problem hiding this comment.
They could inherit from a shared base-class. Then for instance x_funcify_FunctionGraph can dispatch on the base class, since they don't care whether it is a Frozen or Regular FunctionGraph?
Closes #1606
LLM disclosure: this PR made heavy use of Claude in the planning and first cut stages, though I was heavily involved. Still, the code should be subject to extra scrutiny as a result.
The purpose of the PR is to refactor Ops with inner graphs to allow comparison. The linked issue has an exhaustive discussion of the factors at play. There was an attempt in the aesara days to attack this, but it was perhaps too aggressive: it cons-hashed all Apply nodes, which necessitated changes across the codebase. @ricardoV94 suggested a weakref dict approach for subgraphs. This is implemented at the Op level. The plan is for Ops that have inner graphs (
Composite,ScalarLoop,Scan,OpFromGraph, etc) to have a_cacheclass attribute, and implement the op-specific logic for caching, pickling, unpickling, etc. It didn't look super generalizable to me at first blush, but we can argue about it maybe.Changes to
FunctionGraph:FunctionGraphnow has a methodfreezethat returns aFrozenFunctionGraph.FrozenFunctionGraphdoes cons-hashing of Apply nodes within its scope onlyFrozenFunctionGraphswith the same inner graph with evaluate to equal, but theirApplynodes won't be references to the same objects (this is the "conservatism" of my approach)Specific implementation details:
structural_hashof aFrozenFunctionGraphis built from a list of 3-tuples:(name, type, inputs), plus the outputs. For constants,inputsis replaced with the hash of the input data.FrozenFunctionGraphsis done by comparing hashes, then falling back toequal_computationif the hash misses.A consequence of the cons-hashing in this approach is that the inner graph is de-duplicated when we call
fg.freeze(). So aMergeOptimizerpass is no longer required. Usage is demonstrated on theCompositeOp. If we like the approach I can move forward with refactoring other Ops, but I wanted to stop here and discuss the approach.Code example:
Result: