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/core/tfrt/fallback/fallback_state.h"
16
17 #include "tensorflow/core/common_runtime/device_mgr.h"
18 #include "tensorflow/core/common_runtime/rendezvous_mgr.h"
19 #include "tensorflow/core/framework/device_factory.h"
20 #include "tensorflow/core/public/version.h"
21
22 namespace tensorflow {
23 namespace tfrt_stub {
24
Create(const SessionOptions & session_options,const tensorflow::FunctionDefLibrary & fdef_lib)25 StatusOr<std::unique_ptr<FallbackState>> FallbackState::Create(
26 const SessionOptions &session_options,
27 const tensorflow::FunctionDefLibrary &fdef_lib) {
28 // Create devices.
29 std::vector<std::unique_ptr<Device>> devices;
30 TF_RETURN_IF_ERROR(DeviceFactory::AddDevices(
31 session_options, "/job:localhost/replica:0/task:0", &devices));
32
33 return std::make_unique<FallbackState>(session_options, std::move(devices),
34 fdef_lib);
35 }
36
FallbackState(const SessionOptions & session_options,std::vector<std::unique_ptr<Device>> devices,const tensorflow::FunctionDefLibrary & fdef_lib)37 FallbackState::FallbackState(const SessionOptions &session_options,
38 std::vector<std::unique_ptr<Device>> devices,
39 const tensorflow::FunctionDefLibrary &fdef_lib)
40 : session_options_(session_options),
41 device_manager_(std::move(devices)),
42 func_lib_def_(OpRegistry::Global(), fdef_lib),
43 pflr_(&device_manager_, session_options.env, &session_options.config,
44 TF_GRAPH_DEF_VERSION, &func_lib_def_,
45 session_options.config.graph_options().optimizer_options(),
46 /*thread_pool=*/nullptr, /*parent=*/nullptr,
47 /*session_metadata=*/nullptr,
48 Rendezvous::Factory{
49 [](const int64, const DeviceMgr *device_mgr, Rendezvous **r) {
50 *r = new IntraProcessRendezvous(device_mgr);
51 return OkStatus();
52 }}) {
53 for (auto *d : device_manager_.ListDevices()) {
54 device_set_.AddDevice(d);
55 }
56
57 // client_device is the device for feed and fetch tensors.
58 device_set_.set_client_device(device_manager_.HostCPU());
59 }
60
61 StatusOr<std::unique_ptr<GraphExecutionState>>
CreateGraphExecutionState(GraphDef graph_def) const62 FallbackState::CreateGraphExecutionState(GraphDef graph_def) const {
63 // Create GraphExecutionState which contains the preprocessed graph including
64 // device information. The following code is adapted from
65 // http://cs?q=tensorflow/core/common_runtime/direct_session.cc:427%20at_cl:352783230
66
67 GraphExecutionStateOptions options;
68 options.device_set = &device_set_;
69 options.session_options = &session_options_;
70 options.session_handle = "tfrt_fallback_handle";
71
72 std::unique_ptr<GraphExecutionState> execution_state;
73 TF_RETURN_IF_ERROR(GraphExecutionState::MakeForBaseGraph(
74 std::move(graph_def), options, &execution_state));
75 return execution_state;
76 }
77
78 } // namespace tfrt_stub
79 } // namespace tensorflow
80