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 "tensorflow/compiler/mlir/tensorflow/transforms/mark_initialized_variables.h"
16
17 #include <string>
18 #include <vector>
19
20 #include "llvm/ADT/SmallVector.h"
21 #include "mlir/IR/Block.h" // from @llvm-project
22 #include "mlir/IR/BuiltinAttributes.h" // from @llvm-project
23 #include "mlir/IR/MLIRContext.h" // from @llvm-project
24 #include "mlir/Pass/Pass.h" // from @llvm-project
25 #include "tensorflow/compiler/mlir/tensorflow/ir/tf_ops_n_z.h"
26 #include "tensorflow/compiler/mlir/tensorflow/utils/session_utils.h"
27 #include "tensorflow/core/common_runtime/device_mgr.h"
28 #include "tensorflow/core/framework/device_base.h"
29 #include "tensorflow/core/framework/rendezvous.h"
30 #include "tensorflow/core/framework/resource_var.h"
31 #include "tensorflow/core/framework/tensor.h"
32 #include "tensorflow/core/public/session.h"
33
34 namespace mlir {
35 namespace tf_saved_model {
36
37 // Returns true if the variable 'var_handle_op' is initialized in 'session'.
IsVariableInitialized(mlir::TF::VarHandleOp var_handle_op,llvm::StringRef device_name,const tensorflow::DeviceMgr * mgr,tensorflow::Session * session)38 bool IsVariableInitialized(mlir::TF::VarHandleOp var_handle_op,
39 llvm::StringRef device_name,
40 const tensorflow::DeviceMgr* mgr,
41 tensorflow::Session* session) {
42 auto* var_ptr = GetVariableFromSession(var_handle_op, device_name, mgr);
43 if (!var_ptr) return false;
44 auto* tensor = var_ptr->tensor();
45 bool is_initialized = tensor && tensor->IsInitialized();
46 var_ptr->Unref();
47 return is_initialized;
48 }
49
MarkInitializedVariablesInFunction(FuncOp function,tensorflow::Session * session,mlir::MLIRContext * context)50 LogicalResult MarkInitializedVariablesInFunction(FuncOp function,
51 tensorflow::Session* session,
52 mlir::MLIRContext* context) {
53 if (!session || !llvm::hasSingleElement(function)) return success();
54 Block& block = function.front();
55
56 const tensorflow::DeviceMgr* mgr = nullptr;
57 auto status = session->LocalDeviceManager(&mgr);
58 if (!status.ok())
59 return function->emitError("failed to fetch device manager: " +
60 status.error_message());
61
62 // Fetch all varHandleOp in the function.
63 llvm::SmallVector<TF::VarHandleOp, 4> var_ops;
64 for (auto var_handle_op : block.getOps<TF::VarHandleOp>())
65 var_ops.emplace_back(var_handle_op);
66
67 // Get resources from Session.
68 auto resource_tensors_or = GetResourcesFromSession(var_ops, session);
69 if (!resource_tensors_or.ok())
70 return function->emitError(resource_tensors_or.status().message().data());
71
72 for (auto var_and_tensor : llvm::zip(var_ops, resource_tensors_or.value())) {
73 auto& var_op = std::get<0>(var_and_tensor);
74 auto& resource_tensor = std::get<1>(var_and_tensor);
75 bool is_variable_initialized = false;
76 if (resource_tensor.dtype() != tensorflow::DT_RESOURCE) {
77 is_variable_initialized = true;
78 } else {
79 auto handle = resource_tensor.scalar<tensorflow::ResourceHandle>()();
80 is_variable_initialized =
81 IsVariableInitialized(var_op, handle.device(), mgr, session);
82 }
83 var_op->setAttr("_is_initialized",
84 BoolAttr::get(context, is_variable_initialized));
85 }
86 return success();
87 }
88 } // namespace tf_saved_model
89 } // namespace mlir
90