• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/common_runtime/eager/context.h"
20 #include "tensorflow/core/lib/core/status.h"
21 
22 namespace tflite {
23 namespace flex {
DelegateData()24 DelegateData::DelegateData() {}
25 
~DelegateData()26 DelegateData::~DelegateData() {
27   if (eager_context_) eager_context_->Unref();
28 }
29 
Prepare(const tensorflow::SessionOptions & session_options)30 tensorflow::Status DelegateData::Prepare(
31     const tensorflow::SessionOptions& session_options) {
32   if (eager_context_) {
33     return tensorflow::Status();
34   }
35 
36   std::vector<std::unique_ptr<tensorflow::Device>> devices;
37 
38   TF_RETURN_IF_ERROR(tensorflow::DeviceFactory::AddDevices(
39       session_options, "/job:localhost/replica:0/task:0", &devices));
40 
41   auto device_mgr =
42       absl::make_unique<tensorflow::StaticDeviceMgr>(std::move(devices));
43   // Note that Rendezvous is ref-counted so it will be automatically deleted.
44   tensorflow::Rendezvous* rendezvous =
45       new tensorflow::IntraProcessRendezvous(device_mgr.get());
46   eager_context_ = new tensorflow::EagerContext(
47       session_options,
48       tensorflow::ContextDevicePlacementPolicy::DEVICE_PLACEMENT_SILENT,
49       /*async=*/false, device_mgr.release(), /*device_mgr_owned*/ true,
50       rendezvous, nullptr);
51   return tensorflow::Status();
52 }
53 
54 }  // namespace flex
55 }  // namespace tflite
56