|
| 1 | +from pytensor.compile import optdb |
| 2 | +from pytensor.graph import node_rewriter |
| 3 | +from pytensor.graph.basic import applys_between |
| 4 | +from pytensor.graph.rewriting.basic import out2in |
| 5 | +from pytensor.tensor.basic import as_tensor, constant |
| 6 | +from pytensor.tensor.blockwise import Blockwise, BlockwiseWithCoreShape |
| 7 | +from pytensor.tensor.rewriting.shape import ShapeFeature |
| 8 | + |
| 9 | + |
| 10 | +@node_rewriter([Blockwise]) |
| 11 | +def introduce_explicit_core_shape_blockwise(fgraph, node): |
| 12 | + """Introduce the core shape of a Blockwise. |
| 13 | +
|
| 14 | + We wrap Blockwise graphs into a BlockwiseWithCoreShape OpFromGraph |
| 15 | + that has an extra "non-functional" input that represents the core shape of the Blockwise variable. |
| 16 | + This core_shape is used by the numba backend to pre-allocate the output array. |
| 17 | +
|
| 18 | + If available, the core shape is extracted from the shape feature of the graph, |
| 19 | + which has a higher change of having been simplified, optimized, constant-folded. |
| 20 | + If missing, we fall back to the op._supp_shape_from_params method. |
| 21 | +
|
| 22 | + This rewrite is required for the numba backend implementation of Blockwise. |
| 23 | +
|
| 24 | + Example |
| 25 | + ------- |
| 26 | +
|
| 27 | + .. code-block:: python |
| 28 | +
|
| 29 | + import pytensor |
| 30 | + import pytensor.tensor as pt |
| 31 | +
|
| 32 | + x = pt.tensor("x", shape=(5, None, None)) |
| 33 | + outs = pt.linalg.svd(x, compute_uv=True) |
| 34 | + pytensor.dprint(outs) |
| 35 | + # Blockwise{SVD{full_matrices=True, compute_uv=True}, (m,n)->(m,m),(k),(n,n)}.0 [id A] |
| 36 | + # └─ x [id B] |
| 37 | + # Blockwise{SVD{full_matrices=True, compute_uv=True}, (m,n)->(m,m),(k),(n,n)}.1 [id A] |
| 38 | + # └─ ··· |
| 39 | + # Blockwise{SVD{full_matrices=True, compute_uv=True}, (m,n)->(m,m),(k),(n,n)}.2 [id A] |
| 40 | + # └─ ··· |
| 41 | +
|
| 42 | + # After the rewrite, note the new 3 core shape inputs |
| 43 | + fn = pytensor.function([x], outs, mode="NUMBA") |
| 44 | + fn.dprint(print_type=False) |
| 45 | + # [Blockwise{SVD{full_matrices=True, compute_uv=True}, (m,n)->(m,m),(k),(n,n)}].0 [id A] 6 |
| 46 | + # ├─ x [id B] |
| 47 | + # ├─ MakeVector{dtype='int64'} [id C] 5 |
| 48 | + # │ ├─ Shape_i{1} [id D] 2 |
| 49 | + # │ │ └─ x [id B] |
| 50 | + # │ └─ Shape_i{1} [id D] 2 |
| 51 | + # │ └─ ··· |
| 52 | + # ├─ MakeVector{dtype='int64'} [id E] 4 |
| 53 | + # │ └─ Minimum [id F] 3 |
| 54 | + # │ ├─ Shape_i{1} [id D] 2 |
| 55 | + # │ │ └─ ··· |
| 56 | + # │ └─ Shape_i{2} [id G] 0 |
| 57 | + # │ └─ x [id B] |
| 58 | + # └─ MakeVector{dtype='int64'} [id H] 1 |
| 59 | + # ├─ Shape_i{2} [id G] 0 |
| 60 | + # │ └─ ··· |
| 61 | + # └─ Shape_i{2} [id G] 0 |
| 62 | + # └─ ··· |
| 63 | + # [Blockwise{SVD{full_matrices=True, compute_uv=True}, (m,n)->(m,m),(k),(n,n)}].1 [id A] 6 |
| 64 | + # └─ ··· |
| 65 | + # [Blockwise{SVD{full_matrices=True, compute_uv=True}, (m,n)->(m,m),(k),(n,n)}].2 [id A] 6 |
| 66 | + # └─ ··· |
| 67 | + """ |
| 68 | + op: Blockwise = node.op # type: ignore[annotation-unchecked] |
| 69 | + batch_ndim = op.batch_ndim(node) |
| 70 | + |
| 71 | + shape_feature: ShapeFeature | None = getattr(fgraph, "shape_feature", None) # type: ignore[annotation-unchecked] |
| 72 | + if shape_feature: |
| 73 | + core_shapes = [ |
| 74 | + [shape_feature.get_shape(out, i) for i in range(batch_ndim, out.type.ndim)] |
| 75 | + for out in node.outputs |
| 76 | + ] |
| 77 | + else: |
| 78 | + input_shapes = [tuple(inp.shape) for inp in node.inputs] |
| 79 | + core_shapes = [ |
| 80 | + out_shape[batch_ndim:] |
| 81 | + for out_shape in op.infer_shape(None, node, input_shapes) |
| 82 | + ] |
| 83 | + |
| 84 | + core_shapes = [ |
| 85 | + as_tensor(core_shape) if len(core_shape) else constant([], dtype="int64") |
| 86 | + for core_shape in core_shapes |
| 87 | + ] |
| 88 | + |
| 89 | + if any( |
| 90 | + isinstance(node.op, Blockwise) |
| 91 | + for node in applys_between(node.inputs, core_shapes) |
| 92 | + ): |
| 93 | + # If Blockwise shows up in the shape graph we can't introduce the core shape |
| 94 | + return None |
| 95 | + |
| 96 | + return BlockwiseWithCoreShape( |
| 97 | + [*node.inputs, *core_shapes], |
| 98 | + node.outputs, |
| 99 | + destroy_map=op.destroy_map, |
| 100 | + )(*node.inputs, *core_shapes, return_list=True) |
| 101 | + |
| 102 | + |
| 103 | +optdb.register( |
| 104 | + introduce_explicit_core_shape_blockwise.__name__, |
| 105 | + out2in(introduce_explicit_core_shape_blockwise), |
| 106 | + "numba", |
| 107 | + position=100, |
| 108 | +) |
0 commit comments