1 /* Copyright 2018 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/lite/delegates/flex/delegate_data.h"
16
17 #include "absl/memory/memory.h"
18 #include "tensorflow/core/common_runtime/device_factory.h"
19 #include "tensorflow/core/lib/core/status.h"
20
21 namespace tflite {
22 namespace flex {
DelegateData()23 DelegateData::DelegateData() {}
24
~DelegateData()25 DelegateData::~DelegateData() {}
26
Prepare(const tensorflow::SessionOptions & session_options)27 tensorflow::Status DelegateData::Prepare(
28 const tensorflow::SessionOptions& session_options) {
29 if (eager_context_) {
30 return tensorflow::Status();
31 }
32
33 std::vector<std::unique_ptr<tensorflow::Device>> devices;
34
35 TF_RETURN_IF_ERROR(tensorflow::DeviceFactory::AddDevices(
36 session_options, "/job:localhost/replica:0/task:0", &devices));
37
38 std::unique_ptr<tensorflow::DeviceMgr> device_mgr =
39 absl::make_unique<tensorflow::DeviceMgr>(std::move(devices));
40 // Note that Rendezvous is ref-counted so it will be automatically deleted.
41 tensorflow::Rendezvous* rendezvous =
42 new tensorflow::IntraProcessRendezvous(device_mgr.get());
43 eager_context_.reset(new tensorflow::EagerContext(
44 session_options,
45 tensorflow::ContextDevicePlacementPolicy::DEVICE_PLACEMENT_SILENT,
46 /*async=*/false, std::move(device_mgr), rendezvous));
47 return tensorflow::Status();
48 }
49
50 } // namespace flex
51 } // namespace tflite
52