• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
16 #include "tensorflow/c/experimental/grappler/grappler_internal.h"
17 #include "tensorflow/c/experimental/pluggable_profiler/pluggable_profiler_internal.h"
18 #include "tensorflow/c/experimental/stream_executor/stream_executor_internal.h"
19 #include "tensorflow/core/common_runtime/copy_tensor.h"
20 #include "tensorflow/core/common_runtime/device_factory.h"
21 #include "tensorflow/core/common_runtime/pluggable_device/pluggable_device_factory.h"
22 #include "tensorflow/core/common_runtime/pluggable_device/pluggable_device_util.h"
23 #include "tensorflow/core/platform/env.h"
24 #include "tensorflow/core/platform/errors.h"
25 #include "tensorflow/core/platform/logging.h"
26 #include "tensorflow/core/platform/status.h"
27 
28 namespace tensorflow {
29 
InitDeviceAndGraphModule(void * dso_handle)30 static Status InitDeviceAndGraphModule(void* dso_handle) {
31   void* dso_symbol_se;
32   void* dso_symbol_graph;
33   tensorflow::Env* env = tensorflow::Env::Default();
34 
35   Status status_se =
36       env->GetSymbolFromLibrary(dso_handle, "SE_InitPlugin", &dso_symbol_se);
37   Status status_graph =
38       env->GetSymbolFromLibrary(dso_handle, "TF_InitGraph", &dso_symbol_graph);
39 
40   // Raise error if neither device nor graph is found.
41   if (errors::IsNotFound(status_se) && errors::IsNotFound(status_graph)) {
42     return errors::NotFound(status_se.error_message() + " " +
43                             status_graph.error_message());
44   }
45 
46   if (status_se == OkStatus()) {
47     auto init_fn =
48         reinterpret_cast<stream_executor::SEInitPluginFn>(dso_symbol_se);
49 
50     string device_type, platform_name;
51     TF_RETURN_IF_ERROR(stream_executor::InitStreamExecutorPlugin(
52         init_fn, &device_type, &platform_name));
53 
54     DeviceFactory::Register(
55         device_type,
56         std::make_unique<PluggableDeviceFactory>(device_type, platform_name),
57         /*priority=*/220, /*is_pluggable_device=*/true);
58 
59     TF_RETURN_IF_ERROR(CopyTensor::Register(
60         DeviceType(device_type), DeviceType(device_type),
61         PluggableDeviceUtil::DeviceToDeviceCopy,
62         /*is_pluggable_device=*/true));  // Register the Copy tensor.
63   }
64 
65   if (status_graph == OkStatus()) {
66     auto init_fn =
67         reinterpret_cast<grappler::TFInitGraphPluginFn>(dso_symbol_graph);
68     TF_RETURN_IF_ERROR(grappler::InitGraphPlugin(init_fn));
69   }
70 
71   return OkStatus();
72 }
73 
74 typedef void (*TFKernelInitFn)();
InitKernelModule(void * dso_handle)75 static Status InitKernelModule(void* dso_handle) {
76   void* dso_symbol;
77   tensorflow::Env* env = tensorflow::Env::Default();
78 
79   TF_RETURN_IF_ERROR(
80       env->GetSymbolFromLibrary(dso_handle, "TF_InitKernel", &dso_symbol));
81   auto init_fn = reinterpret_cast<TFKernelInitFn>(dso_symbol);
82   init_fn();
83   return OkStatus();
84 }
85 
InitProfilerModule(void * dso_handle)86 static Status InitProfilerModule(void* dso_handle) {
87   void* dso_symbol;
88   tensorflow::Env* env = tensorflow::Env::Default();
89 
90   TF_RETURN_IF_ERROR(
91       env->GetSymbolFromLibrary(dso_handle, "TF_InitProfiler", &dso_symbol));
92   auto init_fn = reinterpret_cast<profiler::TFInitProfilerFn>(dso_symbol);
93   TF_RETURN_IF_ERROR(profiler::InitPluginProfiler(init_fn));
94   return OkStatus();
95 }
96 
RegisterPluggableDevicePlugin(void * dso_handle)97 Status RegisterPluggableDevicePlugin(void* dso_handle) {
98   // Step 1 Init Device/Graph Module.
99   TF_RETURN_IF_ERROR(InitDeviceAndGraphModule(dso_handle));
100 
101   // Step 2 Init Kernel Module.
102   TF_RETURN_IF_ERROR(InitKernelModule(dso_handle));
103 
104   // Step 3 Init Profiler Module. (Profiler support is optional.)
105   Status status = InitProfilerModule(dso_handle);
106   if (!status.ok()) {
107     VLOG(1) << "Failed to load pluggable profiler module due to "
108             << status.error_message();
109   }
110   return OkStatus();
111 }
112 
113 }  // namespace tensorflow
114