1 /* Copyright 2021 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 16 #include "tensorflow/compiler/mlir/tosa/tf_tfl_passes.h" 17 18 #include "mlir/Dialect/Affine/Passes.h" // from @llvm-project 19 #include "mlir/Dialect/Tosa/Transforms/Passes.h" // from @llvm-project 20 #include "mlir/Transforms/Passes.h" // from @llvm-project 21 #include "tensorflow/compiler/mlir/lite/transforms/lift_tflite_flex_ops.h" 22 #include "tensorflow/compiler/mlir/lite/transforms/passes.h" 23 #include "tensorflow/compiler/mlir/tosa/transforms/passes.h" 24 25 namespace mlir { 26 namespace tosa { 27 createTFTFLtoTOSALegalizationPipeline(OpPassManager & pm,const TOSATFTFLLegalizationPipelineOptions & opts)28void createTFTFLtoTOSALegalizationPipeline( 29 OpPassManager& pm, const TOSATFTFLLegalizationPipelineOptions& opts) { 30 //---------------------------------------------------------------------------- 31 // Prepare TFL module for conversion 32 //---------------------------------------------------------------------------- 33 // Inline all functions into main and then delete the functions themselves. 34 pm.addPass(mlir::createInlinerPass()); 35 36 // Add pass to decompose TFLite mixed quantization to non-quantized variants. 37 pm.addPass(TFL::CreateDecomposeHybridQuantizationPass()); 38 39 // Now that there is only one function, run some MLIR passes on it. 40 pm.addPass(mlir::createCanonicalizerPass()); 41 pm.addPass(mlir::createCSEPass()); 42 43 pm.addPass(mlir::createLoopFusionPass()); 44 pm.addPass(mlir::createAffineScalarReplacementPass()); 45 46 //---------------------------------------------------------------------------- 47 // Perform main conversion. 48 //---------------------------------------------------------------------------- 49 pm.addPass(mlir::TFL::CreateLiftTfliteFlexOpsPass()); 50 pm.addPass(mlir::tosa::createFuseBiasTFPass()); 51 pm.addPass(mlir::tosa::createConvertTFLUint8Pass()); 52 if (opts.dequantize_tfl_softmax) { 53 pm.addPass(mlir::tosa::createDequantizeTFLSoftmaxPass()); 54 } 55 pm.addPass(mlir::tosa::createLegalizeTFTFLPass()); 56 57 //---------------------------------------------------------------------------- 58 // Post conversion cleanup. 59 //---------------------------------------------------------------------------- 60 pm.addPass(mlir::tosa::createTosaInferShapesPass()); 61 pm.addPass(mlir::tosa::createTosaMakeBroadcastablePass()); 62 // Inline the call/return basic blocks within TOSA control flow ops. 63 pm.addPass(mlir::createInlinerPass()); 64 // Clean up with DCE. 65 pm.addPass(mlir::createSymbolDCEPass()); 66 } 67 registerTFTFLtoTOSALegalizationPipeline()68void registerTFTFLtoTOSALegalizationPipeline() { 69 mlir::PassPipelineRegistration<TOSATFTFLLegalizationPipelineOptions>( 70 "tf-tfl-to-tosa-pipeline", 71 "TensorFlow / TensorFlow Lite to TOSA legalization pipeline", 72 createTFTFLtoTOSALegalizationPipeline); 73 } 74 75 } // namespace tosa 76 } // namespace mlir 77