• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/* Copyright 2021 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15include "mlir/IR/OpBase.td"
16include "mlir/IR/PatternBase.td"
17include "mlir/Dialect/Arithmetic/IR/ArithmeticOps.td"
18include "mlir/Dialect/Func/IR/FuncOps.td"
19include "tensorflow/compiler/mlir/tfr/ir/tfr_ops.td"
20
21class Quantize<string value> : NativeCodeCall<"TFR::Quantize(" # value # ", $0, $1, $_builder)">;
22
23class HasStringAttr<string value> : AttrConstraint<
24    CPred<"$_self.cast<StringAttr>().getValue() == \"" # value # "\"">>;
25
26def QuantActRangeNonePattern :
27  Pattern<
28    (TFR_TFRQuantActRangeOp
29     (TFR_ConstOp HasStringAttr<"NONE">:$act), $scale, $zp),
30    [(TFR_ConstantTensorOp (Arith_ConstantOp ConstantAttr<I32Attr, "-128">)),
31     (TFR_ConstantTensorOp (Arith_ConstantOp ConstantAttr<I32Attr, "127">))]>;
32
33def QuantActRangeReluPattern :
34  Pattern<
35    (TFR_TFRQuantActRangeOp
36     (TFR_ConstOp HasStringAttr<"RELU">:$act),
37     (ConstantLikeMatcher F32Attr:$scale),
38     (ConstantLikeMatcher I64Attr:$zp)),
39    [(TFR_ConstantTensorOp (Arith_ConstantOp (Quantize<"0.0f"> $scale, $zp))),
40     (TFR_ConstantTensorOp (Arith_ConstantOp ConstantAttr<I32Attr, "127">))]>;
41
42def QuantActRangeRelu6Pattern :
43  Pattern<
44    (TFR_TFRQuantActRangeOp
45     (TFR_ConstOp HasStringAttr<"RELU6">:$act),
46     (ConstantLikeMatcher F32Attr:$scale),
47     (ConstantLikeMatcher I64Attr:$zp)),
48    [(TFR_ConstantTensorOp (Arith_ConstantOp (Quantize<"0.0f"> $scale, $zp))),
49     (TFR_ConstantTensorOp (Arith_ConstantOp (Quantize<"6.0f"> $scale, $zp)))]>;
50
51
52def QuantActRangeReluN1To1Pattern :
53  Pattern<
54    (TFR_TFRQuantActRangeOp
55     (TFR_ConstOp HasStringAttr<"RELU_N1_TO_1">:$act),
56     (ConstantLikeMatcher F32Attr:$scale),
57     (ConstantLikeMatcher I64Attr:$zp)),
58    [(TFR_ConstantTensorOp (Arith_ConstantOp (Quantize<"-1.0f"> $scale, $zp))),
59     (TFR_ConstantTensorOp (Arith_ConstantOp (Quantize<"1.0f"> $scale, $zp)))]>;
60