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