pyhs3.tensorutils.merge_duplicate_nodes

pyhs3.tensorutils.merge_duplicate_nodes(output, keep=())[source]

Return output with every structurally identical subgraph built once.

Separately built HS3 objects often produce the same subexpression, for example the same parameter broadcast against the observable in several distributions, or the same x - m0 inside different functions. pyhs3.Model.log_prob() passes its graph through this function so that PyTensor receives each subexpression once. Distributions that are identical as HS3 objects are already shared while the model is built (see pyhs3.distributions.Distribution.structural_key()); this pass catches the duplicates below that level.

Two apply nodes are duplicates when they apply an equal Op to the same inputs, after those inputs have themselves been merged. Constants are the same input when their type, shape and value are equal. The sweep visits each node once in topological order, so one pass merges whole duplicate subgraphs, bottom up.

Not duplicates, and kept as they are:

  • expressions that are only mathematically equal (x * y and y * x, or exp(log(x)) and x): no algebra is applied; PyTensor’s canonicalisation rewrites handle those during compilation;

  • constants that differ in dtype (2.0 as float64 vs float32) or shape ([2.0] vs 2.0), even if their values print the same;

  • distinct graph inputs, even with the same name or the same value at evaluation time;

  • the constants in keep, each of which stays its own input, so that a caller can later replace it (and only it) in the merged graph.

PyTensor’s own merge rewrite finds the same duplicates during compilation, but it runs inside the rewriter, replacing one node at a time on a FunctionGraph that validates and notifies its features on every replacement, so its cost grows with the number of duplicates. This sweep visits each node once and is much cheaper on large graphs.

Graph inputs (parameters and observables) are kept as the same variable objects, so names and explicit_graph_inputs() are unchanged.

Examples

The same expression built twice becomes one subgraph:

>>> import pytensor.tensor as pt
>>> from pytensor.graph.traversal import io_toposort
>>> x, y = pt.dscalars("x", "y")
>>> output = pt.exp(x * y) + pt.exp(x * y)
>>> len(io_toposort([], [output]))  # 2 Mul, 2 Exp, 1 Add
5
>>> len(io_toposort([], [merge_duplicate_nodes(output)]))  # 1 Mul, 1 Exp, 1 Add
3

Operands in a different order are not a duplicate:

>>> output = pt.exp(x * y) + pt.exp(y * x)
>>> merge_duplicate_nodes(output) is output
True

Equal constants are one input, constants of another dtype are not:

>>> v = pt.dvector("v")
>>> same = (v + pt.constant(2.0)) * (v + pt.constant(2.0))
>>> len(io_toposort([], [same]))  # 2 broadcasts of 2.0, 2 Add, 1 Mul
5
>>> len(io_toposort([], [merge_duplicate_nodes(same)]))  # 1, 1, 1
3
>>> two64 = pt.constant(2.0, dtype="float64")
>>> two32 = pt.constant(2.0, dtype="float32")
>>> mixed = (v + two64) * (v + two32)
>>> merge_duplicate_nodes(mixed) is mixed
True
Parameters:
Return type:

TensorVar

Returns:

A variable computing the same value from the same inputs. It is output itself when the graph contains nothing to merge.