• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <string>
16 #include <vector>
17 
18 #include "mlir/IR/Block.h"  // from @llvm-project
19 #include "mlir/IR/BuiltinAttributes.h"  // from @llvm-project
20 #include "mlir/IR/MLIRContext.h"  // from @llvm-project
21 #include "mlir/Pass/Pass.h"  // from @llvm-project
22 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops_n_z.h"
23 #include "tensorflow/compiler/mlir/tensorflow/transforms/mark_initialized_variables.h"
24 #include "tensorflow/core/common_runtime/device_mgr.h"
25 #include "tensorflow/core/framework/device_base.h"
26 #include "tensorflow/core/framework/rendezvous.h"
27 #include "tensorflow/core/framework/resource_var.h"
28 #include "tensorflow/core/framework/tensor.h"
29 #include "tensorflow/core/public/session.h"
30 
31 namespace mlir {
32 namespace tf_saved_model {
33 namespace {
34 // Checks if a function has only one block.
CheckSingleBlockFunction(FuncOp function)35 mlir::LogicalResult CheckSingleBlockFunction(FuncOp function) {
36   if (!llvm::hasSingleElement(function)) {
37     return function.emitError()
38            << "expects function '" << function.getName()
39            << "' to have 1 block, got " << function.getBlocks().size();
40   }
41   return success();
42 }
43 
44 class MarkInitializedVariablesPass
45     : public PassWrapper<MarkInitializedVariablesPass, FunctionPass> {
46  public:
MarkInitializedVariablesPass(tensorflow::Session * session=nullptr)47   explicit MarkInitializedVariablesPass(tensorflow::Session* session = nullptr)
48       : session_(session) {}
49   void runOnFunction() override;
50 
51  private:
52   tensorflow::Session* session_;
53 };
54 
runOnFunction()55 void MarkInitializedVariablesPass::runOnFunction() {
56   // We use session to check for variables value. If it is null then
57   // nothing to do here.
58   if (!session_) return;
59   auto func_op = getFunction();
60   if (failed(CheckSingleBlockFunction(func_op))) return signalPassFailure();
61 
62   if (failed(MarkInitializedVariablesInFunction(func_op, session_,
63                                                 &getContext()))) {
64     return signalPassFailure();
65   }
66 }
67 }  // namespace
68 
CreateMarkInitializedVariablesPass(tensorflow::Session * session)69 std::unique_ptr<OperationPass<FuncOp>> CreateMarkInitializedVariablesPass(
70     tensorflow::Session* session) {
71   return std::make_unique<MarkInitializedVariablesPass>(session);
72 }
73 }  // namespace tf_saved_model
74 }  // namespace mlir
75