1/* Copyright 2017 The TensorFlow Authors. All Rights Reserved.
2
3Licensed under the Apache License, Version 2.0 (the "License");
4you may not use this file except in compliance with the License.
5You may obtain a copy of the License at
6
7    http://www.apache.org/licenses/LICENSE-2.0
8
9Unless required by applicable law or agreed to in writing, software
10distributed under the License is distributed on an "AS IS" BASIS,
11WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12See the License for the specific language governing permissions and
13limitations under the License.
14==============================================================================*/
15
16#include "tensorflow/compiler/xla/service/interpreter/platform.h"
17
18#include <utility>
19
20#include "tensorflow/compiler/xla/service/interpreter/executor.h"
21#include "tensorflow/compiler/xla/service/interpreter/platform_id.h"
22#include "tensorflow/stream_executor/device_options.h"
23#include "tensorflow/stream_executor/lib/initialize.h"
24#include "tensorflow/stream_executor/lib/ptr_util.h"
25#include "tensorflow/stream_executor/lib/status.h"
26#include "tensorflow/stream_executor/lib/status_macros.h"
27#include "tensorflow/stream_executor/lib/stringprintf.h"
28#include "tensorflow/stream_executor/multi_platform_manager.h"
29#include "tensorflow/stream_executor/platform.h"
30
31namespace se = ::perftools::gputools;
32namespace sep = ::perftools::gputools::interpreter;
33
34namespace perftools {
35namespace gputools {
36namespace interpreter {
37
38InterpreterPlatform::InterpreterPlatform() : name_("Interpreter") {}
39
40InterpreterPlatform::~InterpreterPlatform() {}
41
42Platform::Id InterpreterPlatform::id() const { return kInterpreterPlatformId; }
43
44int InterpreterPlatform::VisibleDeviceCount() const { return 1; }
45
46const string& InterpreterPlatform::Name() const { return name_; }
47
48port::StatusOr<StreamExecutor*> InterpreterPlatform::ExecutorForDevice(
49    int ordinal) {
50  StreamExecutorConfig config;
51  config.ordinal = ordinal;
52  config.plugin_config = PluginConfig();
53  config.device_options = DeviceOptions::Default();
54  return GetExecutor(config);
55}
56
57port::StatusOr<StreamExecutor*>
58InterpreterPlatform::ExecutorForDeviceWithPluginConfig(
59    int device_ordinal, const PluginConfig& plugin_config) {
60  StreamExecutorConfig config;
61  config.ordinal = device_ordinal;
62  config.plugin_config = plugin_config;
63  config.device_options = DeviceOptions::Default();
64  return GetExecutor(config);
65}
66
67port::StatusOr<StreamExecutor*> InterpreterPlatform::GetExecutor(
68    const StreamExecutorConfig& config) {
69  return executor_cache_.GetOrCreate(
70      config, [&]() { return GetUncachedExecutor(config); });
71}
72
73port::StatusOr<std::unique_ptr<StreamExecutor>>
74InterpreterPlatform::GetUncachedExecutor(const StreamExecutorConfig& config) {
75  auto executor = port::MakeUnique<StreamExecutor>(
76      this, port::MakeUnique<InterpreterExecutor>(config.plugin_config));
77  auto init_status = executor->Init(config.ordinal, config.device_options);
78  if (!init_status.ok()) {
79    return port::Status{
80        port::error::INTERNAL,
81        port::Printf(
82            "failed initializing StreamExecutor for device ordinal %d: %s",
83            config.ordinal, init_status.ToString().c_str())};
84  }
85
86  return std::move(executor);
87}
88
89void InterpreterPlatform::RegisterTraceListener(
90    std::unique_ptr<TraceListener> listener) {
91  LOG(FATAL) << "not yet implemented: register executor trace listener";
92}
93
94void InterpreterPlatform::UnregisterTraceListener(TraceListener* listener) {
95  LOG(FATAL) << "not yet implemented: unregister executor trace listener";
96}
97
98static void InitializeInterpreterPlatform() {
99  std::unique_ptr<se::Platform> platform(new sep::InterpreterPlatform);
100  SE_CHECK_OK(se::MultiPlatformManager::RegisterPlatform(std::move(platform)));
101}
102
103}  // namespace interpreter
104}  // namespace gputools
105}  // namespace perftools
106
107REGISTER_MODULE_INITIALIZER(interpreter_platform,
108                            sep::InitializeInterpreterPlatform());
109
110DECLARE_MODULE_INITIALIZER(multi_platform_manager);
111
112// Note that module initialization sequencing is not supported in the
113// open-source project, so this will be a no-op there.
114REGISTER_MODULE_INITIALIZER_SEQUENCE(interpreter_platform,
115                                     multi_platform_manager);
116