1 /* Copyright 2020 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 "mlir/IR/BuiltinOps.h" // from @llvm-project 17 #include "mlir/Pass/Pass.h" // from @llvm-project 18 #include "mlir/Support/LogicalResult.h" // from @llvm-project 19 #include "tensorflow/compiler/mlir/tensorflow/transforms/lift_variables.h" 20 #include "tensorflow/core/public/session.h" 21 22 namespace mlir { 23 namespace { 24 25 // This pass takes care of finding all variables from the function arguments and 26 // converting them to the corresponding global tensors, that will be located out 27 // of function. Also it converts resource arguments from function types to the 28 // corresponding saved model arguments accordingly. 29 class LiftVariablesPass 30 : public PassWrapper<LiftVariablesPass, OperationPass<ModuleOp>> { 31 public: LiftVariablesPass(tensorflow::Session * session)32 explicit LiftVariablesPass(tensorflow::Session* session) 33 : session_(session) {} 34 runOnOperation()35 void runOnOperation() override { 36 ModuleOp module = getOperation(); 37 if (failed(tf_saved_model::LiftVariables(module, session_))) 38 signalPassFailure(); 39 } 40 41 private: 42 ::tensorflow::Session* session_; 43 }; 44 45 } // namespace 46 47 namespace tf_saved_model { 48 CreateLiftVariablesPass(tensorflow::Session * session)49std::unique_ptr<OperationPass<ModuleOp>> CreateLiftVariablesPass( 50 tensorflow::Session* session) { 51 return std::make_unique<LiftVariablesPass>(session); 52 } 53 54 } // namespace tf_saved_model 55 } // namespace mlir 56