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/utils/session_utils.h"
16
17 #include "absl/status/status.h"
18 #include "llvm/ADT/SmallSet.h"
19 #include "llvm/ADT/StringRef.h"
20 #include "tensorflow/compiler/mlir/utils/string_container_utils.h"
21 #include "tensorflow/core/common_runtime/device_mgr.h"
22 #include "tensorflow/core/framework/device.h"
23 #include "tensorflow/core/framework/resource_var.h"
24
25 namespace mlir {
26 namespace tf_saved_model {
27
GetVariableName(TF::VarHandleOp var_handle_op)28 std::string GetVariableName(TF::VarHandleOp var_handle_op) {
29 // In some cases the shared_name attribute doesn't have the same
30 // tensor name in the model, so we first try to use the location
31 // then fallback to shared_name attribute.
32 if (auto loc = var_handle_op->getLoc().dyn_cast<NameLoc>())
33 return loc.getName().str();
34 return var_handle_op.shared_name().str();
35 }
36
GetVariableNames(llvm::ArrayRef<TF::VarHandleOp> var_handle_ops)37 std::vector<std::string> GetVariableNames(
38 llvm::ArrayRef<TF::VarHandleOp> var_handle_ops) {
39 std::vector<std::string> names;
40 names.reserve(var_handle_ops.size());
41 for (auto var_handle_op : var_handle_ops)
42 names.push_back(GetVariableName(var_handle_op));
43 return names;
44 }
45
GetVariableFromSession(mlir::TF::VarHandleOp var_handle_op,llvm::StringRef device_name,const tensorflow::DeviceMgr * mgr)46 tensorflow::Var* GetVariableFromSession(mlir::TF::VarHandleOp var_handle_op,
47 llvm::StringRef device_name,
48 const tensorflow::DeviceMgr* mgr) {
49 tensorflow::Device* device = nullptr;
50 if (!mgr || !mgr->LookupDevice(StringRefToView(device_name), &device).ok())
51 return nullptr;
52 tensorflow::Var* var_ptr = nullptr;
53 const auto& container = var_handle_op.container().str();
54 auto status = device->resource_manager()->Lookup(
55 (container.empty() ? device->resource_manager()->default_container()
56 : container),
57 var_handle_op.shared_name().str(), &var_ptr);
58 if (!device || !status.ok()) return nullptr;
59 return var_ptr;
60 }
61
GetResourcesFromSession(llvm::ArrayRef<TF::VarHandleOp> var_handle_ops,tensorflow::Session * session)62 absl::StatusOr<std::vector<tensorflow::Tensor>> GetResourcesFromSession(
63 llvm::ArrayRef<TF::VarHandleOp> var_handle_ops,
64 tensorflow::Session* session) {
65 if (!session)
66 return absl::Status(absl::StatusCode::kInvalidArgument,
67 "Null Session provided.");
68 std::vector<tensorflow::Tensor> resource_tensors;
69 auto variable_names = GetVariableNames(var_handle_ops);
70 if (variable_names.empty()) return resource_tensors;
71
72 auto status = session->Run({}, variable_names, {}, &resource_tensors);
73 if (!status.ok())
74 return absl::Status(absl::StatusCode::kInternal, status.error_message());
75 return resource_tensors;
76 }
77 } // namespace tf_saved_model
78 } // namespace mlir
79