1 /* Copyright 2022 The TensorFlow Authors. All Rights Reserved.
2
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6
7 http://www.apache.org/licenses/LICENSE-2.0
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 #include <algorithm>
16 #include <string>
17 #include <tuple>
18 #include <utility>
19
20 #include "llvm/ADT/StringRef.h"
21 #include "mlir/IR/Builders.h" // from @llvm-project
22 #include "mlir/IR/BuiltinAttributes.h" // from @llvm-project
23 #include "mlir/IR/BuiltinTypes.h" // from @llvm-project
24 #include "mlir/IR/PatternMatch.h" // from @llvm-project
25 #include "mlir/Pass/Pass.h" // from @llvm-project
26 #include "mlir/Support/LogicalResult.h" // from @llvm-project
27 #include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project
28 #include "tensorflow/compiler/mlir/quantization/tensorflow/passes/passes.h"
29 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_dialect.h"
30 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h"
31
32 namespace mlir {
33 namespace quant {
34 namespace {
35
36 constexpr char kCustomAggregatorOpName[] = "tf.CustomAggregator";
37
38 class InsertCustomAggregationOpsPass
39 : public PassWrapper<InsertCustomAggregationOpsPass,
40 OperationPass<func::FuncOp>> {
41 public:
MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(InsertCustomAggregationOpsPass)42 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(InsertCustomAggregationOpsPass)
43
44 StringRef getArgument() const final {
45 // This is the argument used to refer to the pass in the textual format (on
46 // the commandline for example).
47 return "quant-insert-custom-aggregation-ops";
48 }
49
getDescription() const50 StringRef getDescription() const final {
51 // This is a brief description of the pass.
52 return "Insert custom aggregation ops for the calibration procedure";
53 }
54
getDependentDialects(DialectRegistry & registry) const55 void getDependentDialects(DialectRegistry ®istry) const override {
56 registry.insert<TF::TensorFlowDialect>();
57 }
58
59 void runOnOperation() override;
60 };
61
62 static PassRegistration<InsertCustomAggregationOpsPass> pass;
63
64 class AddCustomAggregationOp : public RewritePattern {
65 public:
66 // Does not take ownership of context, which must refer to a valid value that
67 // outlives this object.
AddCustomAggregationOp(MLIRContext * context)68 explicit AddCustomAggregationOp(MLIRContext *context)
69 : RewritePattern(MatchAnyOpTypeTag(), /*benefit=*/1, context) {}
70
matchAndRewrite(Operation * op,PatternRewriter & rewriter) const71 LogicalResult matchAndRewrite(Operation *op,
72 PatternRewriter &rewriter) const override {
73 // Return early if the given operator is the custom aggregator op.
74 if (op->getName().getStringRef() == kCustomAggregatorOpName)
75 return failure();
76
77 bool mutated = false;
78 for (Value input : op->getOperands()) {
79 Type element_type = getElementTypeOrSelf(input.getType());
80 // Non-float cases won't be calibrated.
81 if (!element_type.isF32()) {
82 continue;
83 }
84 // Skip when there is any already existing StatisticsOp found.
85 Operation *defining_op = input.getDefiningOp();
86 if (defining_op != nullptr &&
87 defining_op->getName().getStringRef() == kCustomAggregatorOpName) {
88 continue;
89 }
90
91 // Skip calibration when the given operand comes from a constant.
92 if (defining_op != nullptr && detail::isConstantLike(defining_op)) {
93 continue;
94 }
95
96 SmallVector<NamedAttribute, 1> attributes{
97 rewriter.getNamedAttr("id", rewriter.getStringAttr(""))};
98
99 // Insert custom aggregation op between operand and operator.
100 rewriter.setInsertionPointAfterValue(input);
101 // ID attribute will have empty value for now.
102 OperationState state(
103 op->getLoc(), kCustomAggregatorOpName, /*operands=*/ValueRange{input},
104 /*types=*/TypeRange{input.getType()}, /*attributes=*/attributes);
105 Operation *aggregator_op = Operation::create(state);
106 rewriter.insert(aggregator_op);
107 Value aggregator_op_result = aggregator_op->getOpResult(0);
108 input.replaceAllUsesWith(aggregator_op_result);
109 aggregator_op->replaceUsesOfWith(aggregator_op_result, input);
110
111 // Mark mutated.
112 mutated = true;
113 }
114
115 // Return failure when there is no matching operand.
116 return mutated ? success() : failure();
117 }
118 };
119
runOnOperation()120 void InsertCustomAggregationOpsPass::runOnOperation() {
121 MLIRContext *ctx = &getContext();
122 RewritePatternSet patterns(ctx);
123 func::FuncOp func = getOperation();
124
125 patterns.add<AddCustomAggregationOp>(ctx);
126 if (failed(applyPatternsAndFoldGreedily(func, std::move(patterns)))) {
127 func.emitError() << "quant-insert-custom-aggregation-ops failed.";
128 signalPassFailure();
129 }
130 }
131
132 } // namespace
133
134 std::unique_ptr<OperationPass<func::FuncOp>>
CreateInsertCustomAggregationOpsPass()135 CreateInsertCustomAggregationOpsPass() {
136 return std::make_unique<InsertCustomAggregationOpsPass>();
137 }
138
139 } // namespace quant
140 } // namespace mlir
141