Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions onnxscript/rewriter/rules/common/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
"div_by_1_rule",
"dropout_inference_rule",
"dropout_zero_rule",
"flatten_to_reshape_rule",
"fuse_batchnorm_into_conv_rule",
"fuse_batchnorm_into_conv_transpose_rule",
"fuse_batchnorm_into_gemm_rule",
Expand Down Expand Up @@ -44,6 +45,7 @@

from onnxscript.rewriter.rules.common._basic_rules import (
cast_cast_rule,
flatten_to_reshape_rule,
no_op_cast_rule,
no_op_expand_rule,
no_op_transpose_rule,
Expand Down
87 changes: 81 additions & 6 deletions onnxscript/rewriter/rules/common/_basic_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@

from typing import ClassVar, Sequence

import numpy as np

from onnxscript import ir
from onnxscript.rewriter import _ir_utils as ir_utils
from onnxscript.rewriter._basics import MatchResult
Expand Down Expand Up @@ -123,16 +125,37 @@ def pattern(self, op, x, shape_ignored, shape):
return op.Reshape(op.Reshape(x, shape_ignored), shape)

def rewrite(self, op, x: ir.Value, shape_ignored: ir.Value, shape: ir.Value):
return op.Reshape(x, shape)
new_shape = op.initializer(ir.Tensor(self._new_shape, name=shape.name))
return op.Reshape(x, new_shape, allowzero=self._allowzero)

def check(self, context, x, shape_ignored, shape) -> MatchResult:
check_result = MatchResult()
if shape_ignored.const_value is None:
return check_result.fail("Shape ignored is not a constant.")
if shape.const_value is None:

# Shape must be a constant.
if (np_shape := ir_utils.get_numpy_value(shape)) is None:
return check_result.fail("Shape is not a constant.")
if shape.const_value.numpy().min() <= 0:
return check_result.fail("Shape has non-positive values.")
# Convert to array to support assignment destination.
self._new_shape = np.array(np_shape, np_shape.dtype)

# Try to replace {0,-1} values in shape if reshape output is known.
if (reshape_output := context.output_values[0].shape) is not None:
for i, dim in enumerate(reshape_output):
if isinstance(dim, int) and dim > 0:
self._new_shape[i] = dim

# Constraints for shape.
self._allowzero = context.nodes[0].attributes.get_int("allowzero", 0)
if self._allowzero == 1 and any(self._new_shape == 0):
return check_result
if any(self._new_shape == 0) and any(self._new_shape < 0):
return check_result.fail("Shape cannot contain both 0 and -1 dimensions.")
elif np.count_nonzero(self._new_shape == 0) > 1:
return check_result.fail("Shape cannot contain more than one 0 dimension.")

# At this point, we can safely replace '0' with '-1'.
# Note allowzero is removed since at this point it does not have any effect.
self._allowzero = None
self._new_shape = np.where(self._new_shape == 0, -1, self._new_shape)
return check_result


Expand Down Expand Up @@ -279,6 +302,55 @@ def check(self, context, x, axes1, axes2) -> MatchResult:
return check_result


class Flatten2Reshape(RewriteRuleClassBase):
"""Convert ``Flatten(x)`` to Reshape."""

def pattern(self, op, x: ir.Value):
return op.Flatten(x)

def rewrite(self, op, x: ir.Value):
new_shape = op.initializer(ir.Tensor(self._new_shape, name=f"{x.name}/shape"))
return op.Reshape(x, new_shape)

def check(self, context, x: ir.Value) -> MatchResult:
check_result = MatchResult()
self._new_shape = np.array([-1, -1], "int64")

# Convert axis in a positive value if possible.
axis = context.root.attributes.get_int("axis", 1)
input_rank = None
if (input_shape := x.shape) is not None:
input_rank = len(input_shape)
if axis < 0:
axis += input_rank

# Compute reshape shape following axis attribute.
if axis == 0:
self._new_shape[0] = 1
elif axis == 1:
self._new_shape[0] = 0
elif axis == input_rank:
self._new_shape[1] = 1

# Try to update shape if output is known.
if (output_shape := context.output_values[0].shape) is not None:
for i, dim in enumerate(output_shape):
if isinstance(dim, int):
self._new_shape[i] = dim

# Try to update shape if input is known.
if input_shape is not None:
if all(isinstance(dim, int) for dim in input_shape[:axis]):
self._new_shape[0] = np.prod(input_shape[:axis])
if all(isinstance(dim, int) for dim in input_shape[axis:]):
self._new_shape[1] = np.prod(input_shape[axis:])

# Verify if it is possible to apply rule.
if np.count_nonzero(self._new_shape == -1) > 1:
return check_result.fail("Impossible to compute new shape.")
return check_result


# Create rule instances
cast_cast_rule = CastCast.rule()
no_op_cast_rule = CastIdentity.rule()
Expand All @@ -289,6 +361,7 @@ def check(self, context, x, axes1, axes2) -> MatchResult:
transpose_transpose_rule = TransposeTranspose.rule()
unsqueeze_unsqueeze_rule = UnsqueezeUnsqueeze.rule()
squeeze_reshape_1d_rule = SqueezeReshape.rule()
flatten_to_reshape_rule = Flatten2Reshape.rule()


def basic_optimization_rules() -> RewriteRuleSet:
Expand All @@ -311,6 +384,8 @@ def basic_optimization_rules() -> RewriteRuleSet:
cast_cast_rule,
no_op_cast_rule,
no_op_expand_rule,
# flatten_to_reshape_rule is order sensitive to reshape_reshape_rule
flatten_to_reshape_rule,
reshape_reshape_rule,
slice_split_rule,
no_op_transpose_rule,
Expand Down
Loading
Loading