|
| 1 | +# Copyright (c) Microsoft Corporation. |
| 2 | +# Licensed under the MIT License. |
| 3 | + |
| 4 | +"""Optimization for shape operations.""" |
| 5 | + |
| 6 | +from __future__ import annotations |
| 7 | + |
| 8 | +import onnxscript.ir as ir |
| 9 | +import onnxscript.rewriter.pattern as pattern |
| 10 | + |
| 11 | + |
| 12 | +class ExtractDim(pattern.RewriteRuleClassBase): |
| 13 | + def __init__(self): |
| 14 | + super().__init__(remove_nodes=False) |
| 15 | + |
| 16 | + """This is a pattern observed in causal mask generation that hinders fusion optimizations. |
| 17 | + It can be simplified away. |
| 18 | + """ |
| 19 | + |
| 20 | + def pattern(self, op, x, dim0, dim1, dim2, dim3): |
| 21 | + shape = op.Concat(dim0, dim1, dim2, dim3, axis=0) |
| 22 | + reshaped = op.Reshape(x, shape, allowzero=0) |
| 23 | + transposed = op.Transpose(reshaped, perm=[0, 2, 1, 3]) |
| 24 | + final_shape = op.Shape(transposed, _outputs=["final_shape"], start=0) |
| 25 | + final_dim = op.Slice(final_shape, [-2], [-1]) |
| 26 | + return final_dim |
| 27 | + |
| 28 | + def check(self, context, dim0, dim1, dim2, dim3, final_shape, **_) -> bool: |
| 29 | + # All of the dimensions should have shape [1] |
| 30 | + for dim in (dim0, dim1, dim2, dim3): |
| 31 | + if dim.shape is None or dim.shape.dims != (1,): |
| 32 | + return False |
| 33 | + |
| 34 | + # The Shape op should return the full shape, not a slice of the shape. |
| 35 | + shape_node = final_shape.producer() |
| 36 | + if "end" in shape_node.attributes: |
| 37 | + return False |
| 38 | + if "start" in shape_node.attributes: |
| 39 | + start_attr = shape_node.attributes["start"] |
| 40 | + return isinstance(start_attr, ir.Attr) and start_attr.value == 0 |
| 41 | + return True |
| 42 | + |
| 43 | + def rewrite(self, op, dim1, **_): |
| 44 | + return dim1 |
| 45 | + |
| 46 | + |
| 47 | +rules = pattern.RewriteRuleSet([ExtractDim.rule()]) |
0 commit comments