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 <memory> 16 #include <string> 17 #include <utility> 18 19 #include "mlir/IR/BuiltinTypes.h" // from @llvm-project 20 #include "mlir/IR/MLIRContext.h" // from @llvm-project 21 #include "mlir/Support/LogicalResult.h" // from @llvm-project 22 #include "mlir/Transforms/GreedyPatternRewriteDriver.h" // from @llvm-project 23 #include "tensorflow/compiler/mlir/quantization/tensorflow/passes/passes.h" 24 #include "tensorflow/compiler/mlir/quantization/tensorflow/passes/utils.h" 25 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops.h" 26 27 namespace mlir::quant { 28 namespace { 29 30 // Applies optimization after quantization. 31 class OptimizePass 32 : public PassWrapper<OptimizePass, OperationPass<func::FuncOp>> { 33 public: MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(OptimizePass)34 MLIR_DEFINE_EXPLICIT_INTERNAL_INLINE_TYPE_ID(OptimizePass) 35 36 StringRef getArgument() const final { 37 // This is the argument used to refer to the pass in 38 // the textual format (on the commandline for example). 39 return "quant-optimize"; 40 } getDescription() const41 StringRef getDescription() const final { 42 // This is a brief description of the pass. 43 return "Applies optimization after quantization"; 44 } 45 46 void runOnOperation() override; 47 }; 48 49 #include "tensorflow/compiler/mlir/quantization/tensorflow/passes/optimize.inc" 50 runOnOperation()51void OptimizePass::runOnOperation() { 52 RewritePatternSet patterns(&getContext()); 53 populateWithGenerated(patterns); 54 auto func = getOperation(); 55 (void)applyPatternsAndFoldGreedily(func, std::move(patterns)); 56 } 57 58 } // namespace 59 CreateOptimizePass()60std::unique_ptr<OperationPass<func::FuncOp>> CreateOptimizePass() { 61 return std::make_unique<OptimizePass>(); 62 } 63 64 static PassRegistration<OptimizePass> pass; 65 66 } // namespace mlir::quant 67