2020-03-19 16:48:09 +08:00
|
|
|
//===---- ONNXRewrite.td - Pattern Match Rewriting for ONNX --*- tablegen -===//
|
2020-02-21 22:28:24 +08:00
|
|
|
//
|
|
|
|
// Copyright 2019 The IBM Research Authors.
|
|
|
|
//
|
|
|
|
// =============================================================================
|
|
|
|
//
|
|
|
|
// Defines language-specific pattern match optimizations for ONNX using
|
|
|
|
// Declarative Rewrite Rules (DRR) specified using TableGen records.
|
|
|
|
//
|
2020-03-19 16:48:09 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2020-02-21 22:28:24 +08:00
|
|
|
|
|
|
|
#ifndef ONNX_REWRITE
|
|
|
|
#define ONNX_REWRITE
|
|
|
|
|
|
|
|
#ifndef OP_BASE
|
2020-03-20 22:40:51 +08:00
|
|
|
include "src/Dialect/ONNX/ONNXOps.td"
|
2020-02-21 22:28:24 +08:00
|
|
|
#endif // OP_BASE
|
|
|
|
|
|
|
|
/// Note: The DRR definition used for defining patterns is shown below:
|
|
|
|
///
|
|
|
|
/// class Pattern<
|
|
|
|
/// dag sourcePattern, list<dag> resultPatterns,
|
|
|
|
/// list<dag> additionalConstraints = [],
|
|
|
|
/// dag benefitsAdded = (addBenefit 0)
|
|
|
|
/// >;
|
|
|
|
|
2020-05-15 13:19:28 +08:00
|
|
|
def GetNullAttr :
|
|
|
|
NativeCodeCall<"Attribute()">;
|
|
|
|
|
2020-03-10 08:15:58 +08:00
|
|
|
// Create a StringAttr from a string.
|
|
|
|
class StringAttrOfValue<string val>:
|
|
|
|
NativeCodeCall<"$_builder.getStringAttr(\"" # val # "\")">;
|
|
|
|
|
2020-05-15 13:19:28 +08:00
|
|
|
// Create a DenseElementsAttr from an interger value.
|
2020-03-10 08:15:58 +08:00
|
|
|
// It seems Table-gen does not support `float` type, so we can not pass a float value.
|
|
|
|
class FloatAttrOfValue<int val>:
|
2020-05-15 13:19:28 +08:00
|
|
|
NativeCodeCall<"createDenseFloatAttrOfValue($_builder, $0, " # val # ")">;
|
2020-03-10 08:15:58 +08:00
|
|
|
|
|
|
|
// Create an ArrayAttr of IntergerAttr(s) of zero values.
|
2020-04-19 21:39:34 +08:00
|
|
|
// This function is used for padding attribute in Conv.
|
2020-03-10 08:15:58 +08:00
|
|
|
def createArrayAttrOfZerosFrom:
|
|
|
|
NativeCodeCall<"createArrayAttrOfZeros($_builder, $0)">;
|
|
|
|
|
|
|
|
// Pad a ArrayAttr with zeros.
|
|
|
|
//
|
|
|
|
// pads = [B1, B2, ... Bk, E1, E2, ..., Ek]
|
|
|
|
//
|
|
|
|
// becomes:
|
|
|
|
//
|
|
|
|
// pads = [0,... 0, B1, B2, ... Bk, 0,... 0, E1, E2, ..., Ek]
|
|
|
|
// |_____| |_____|
|
|
|
|
// nZeros nZeros
|
|
|
|
//
|
2020-04-19 21:39:34 +08:00
|
|
|
// This function is used for padding attribute in Conv.
|
2020-03-10 08:15:58 +08:00
|
|
|
class insertZerosForNonPaddedDims<int extensionLength>:
|
|
|
|
NativeCodeCall<"insertZerosForNonPaddedDims($_builder, $0,"
|
|
|
|
# extensionLength # ")">;
|
|
|
|
|
|
|
|
// Check whether an ArrayAttr contains non-zero values or not.
|
|
|
|
def HasNonZeroInArrayAttr: Constraint<CPred<"hasNonZeroInArrayAttr($_self)">,
|
|
|
|
"has non-zero elements">;
|
|
|
|
|
2020-03-30 14:23:14 +08:00
|
|
|
// Check that a StrAttr does not contain a specific value.
|
|
|
|
class IsNotStringAttrOfValue<string val>:
|
|
|
|
Constraint<CPred<"$0.cast<StringAttr>().getValue() != \"" # val # "\"">>;
|
|
|
|
|
2020-03-10 08:15:58 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
2020-03-30 14:23:14 +08:00
|
|
|
// Rewrite:
|
|
|
|
// %0 = onnx.ConvOp(%D : tensor<DShape>, %K)
|
|
|
|
// {pads = [b0, b1, ... bK, e0, e1, ..., eK]} ->
|
|
|
|
// tensor<OutShape>
|
|
|
|
//
|
|
|
|
// as:
|
|
|
|
// %0 = onnx.PadConstantValuePadOp(%D)
|
|
|
|
// {pads = [0, 0, b0, b1, ... bK, 0, 0, e0, e1, ..., eK]} ->
|
|
|
|
// tensor<DPaddedShape>
|
|
|
|
// %1 = onnx.Conv(%0 : tensor<DPaddedShape>, %K) {pads = [0, ..., 0]} ->
|
|
|
|
// tensor<OutShape>
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
def ConvOpPaddingPattern: Pat<
|
|
|
|
(ONNXConvOp:$res
|
|
|
|
$x,
|
|
|
|
$w, $b, $auto_pad, $dilation, $group, $kernel_shape,
|
|
|
|
$pads,
|
|
|
|
$strides),
|
|
|
|
(ONNXConvOp
|
2020-05-15 13:19:28 +08:00
|
|
|
|
|
|
|
(ONNXPadOp $x,
|
|
|
|
(ONNXConstantOp (GetNullAttr),
|
|
|
|
(insertZerosForNonPaddedDims<2> $pads)),
|
|
|
|
(ONNXConstantOp (GetNullAttr),
|
|
|
|
(FloatAttrOfValue<0> $res)),
|
|
|
|
(StringAttrOfValue<"constant">)),
|
|
|
|
|
|
|
|
|
2020-03-30 14:23:14 +08:00
|
|
|
$w, $b, $auto_pad, $dilation, $group, $kernel_shape,
|
|
|
|
(createArrayAttrOfZerosFrom $pads),
|
|
|
|
$strides),
|
|
|
|
[(HasNonZeroInArrayAttr:$pads), (IsNotStringAttrOfValue<"VALID"> $auto_pad)]
|
2020-03-10 08:15:58 +08:00
|
|
|
>;
|
|
|
|
|
2020-02-21 22:28:24 +08:00
|
|
|
#endif // ONNX_REWRITE
|