Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions tensorflow/api_template.__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,8 @@ def _running_from_pip_package():
_plugin_dir = _os.path.join(_s, 'tensorflow-plugins')
if _os.path.exists(_plugin_dir):
_ll.load_library(_plugin_dir)
# Load Pluggable Device Library
_ll.load_pluggable_device_library(_plugin_dir)

# Add module aliases
if hasattr(_current_module, 'keras'):
Expand Down
2 changes: 2 additions & 0 deletions tensorflow/api_template_v1.__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,8 @@ def _running_from_pip_package():
_plugin_dir = _os.path.join(_s, 'tensorflow-plugins')
if _os.path.exists(_plugin_dir):
_ll.load_library(_plugin_dir)
# Load Pluggable Device Library
_ll.load_pluggable_device_library(_plugin_dir)

# Delete modules that should be hidden from dir().
# Don't fail if these modules are not available.
Expand Down
6 changes: 5 additions & 1 deletion tensorflow/c/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -678,7 +678,10 @@ tf_cc_test(
name = "c_api_experimental_test",
size = "medium",
srcs = ["c_api_experimental_test.cc"],
data = ["testdata/tf_record"],
data = [
"testdata/tf_record",
"//tensorflow/c/experimental/stream_executor/test:test_pluggable_device.so",
],
linkopts = select({
"//tensorflow:macos": ["-headerpad_max_install_names"],
"//conditions:default": [],
Expand All @@ -698,6 +701,7 @@ tf_cc_test(
"//tensorflow/core:protos_all_cc",
"//tensorflow/core:test",
"//tensorflow/core:test_main",
"//tensorflow/core/platform:resource_loader",
"@com_google_absl//absl/types:optional",
],
)
Expand Down
47 changes: 47 additions & 0 deletions tensorflow/c/c_api_experimental.cc
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ limitations under the License.
#include "tensorflow/core/graph/node_builder.h"
#include "tensorflow/core/platform/blocking_counter.h"
#include "tensorflow/core/platform/casts.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/init_main.h"
#include "tensorflow/core/platform/mutex.h"
#include "tensorflow/core/platform/net.h"
#include "tensorflow/core/platform/platform.h"
#include "tensorflow/core/platform/strcat.h"
Expand Down Expand Up @@ -630,6 +632,9 @@ void TF_DeleteShapeAndTypeListArray(TF_ShapeAndTypeList** shape_list_array,

namespace tensorflow {
Status TF_TensorToTensor(const TF_Tensor* src, Tensor* dst);

// Helpers for loadding a TensorFlow PluggableDevice plugin (a .so file).
Status LoadPluggableDeviceLibrary(const char* library_filename, void** result);
} // namespace tensorflow

void TFE_InferShapes(TFE_Op* tfe_op, TF_ShapeAndTypeList* input_shapes,
Expand Down Expand Up @@ -743,3 +748,45 @@ void TF_ImportGraphDefOptionsSetValidateColocationConstraints(
TF_ImportGraphDefOptions* opts, unsigned char enable) {
opts->opts.validate_colocation_constraints = enable;
}

// Load a Pluggable Device library.
// On success, returns the handle to library in result and return OK from the
// function. Otherwise return nullptr in result and error Status from the
// function.
//
// If `library_filename` has already been loaded, we return a cached handle.
// Device and Kernels/Ops are registered as globals when a library is loaded
// for the first time.
TF_Library* TF_LoadPluggableDeviceLibrary(const char* library_filename,
TF_Status* status) {
#if defined(IS_MOBILE_PLATFORM) || defined(IS_SLIM_BUILD)
status->status = tensorflow::errors::Unimplemented(
"PluggableDevice plugin functionality is not supported on mobile");
return nullptr;
#else
TF_Library* lib_handle = new TF_Library;
static tensorflow::mutex mu(tensorflow::LINKER_INITIALIZED);
Comment thread
sanjoy marked this conversation as resolved.
Resolved
static std::unordered_map<std::string, void*>* loaded_libs =
new std::unordered_map<std::string, void*>();
tensorflow::Env* env = tensorflow::Env::Default();
{
tensorflow::mutex_lock lock(mu);
auto it = loaded_libs->find(library_filename);
if (it != loaded_libs->end()) {
lib_handle->lib_handle = it->second;
} else {
status->status =
env->LoadDynamicLibrary(library_filename, &lib_handle->lib_handle);
if (!status->status.ok()) {
delete lib_handle;
return nullptr;
}
}
return lib_handle;
}
#endif
}

void TF_DeletePluggableDeviceLibraryHandle(TF_Library* lib_handle) {
delete lib_handle;
}
21 changes: 21 additions & 0 deletions tensorflow/c/c_api_experimental.h
Original file line number Diff line number Diff line change
Expand Up @@ -304,6 +304,27 @@ TF_CAPI_EXPORT extern void
TF_ImportGraphDefOptionsSetValidateColocationConstraints(
TF_ImportGraphDefOptions* opts, unsigned char enable);

// Load the library specified by library_filename and register the pluggable
Comment thread
penpornk marked this conversation as resolved.
Resolved
// device and related kernels present in that library. This function is not
// supported on embedded on mobile and embedded platforms and will fail if
// called.
//
// Pass "library_filename" to a platform-specific mechanism for dynamically
// loading a library. The rules for determining the exact location of the
// library are platform-specific and are not documented here.
//
// On success, returns the newly created library handle and places OK in status.
// The caller owns the library handle.
//
// On failure, returns nullptr and places an error status in status.
TF_CAPI_EXPORT extern TF_Library* TF_LoadPluggableDeviceLibrary(
const char* library_filename, TF_Status* status);

// Frees the memory associated with the library handle.
// Does NOT unload the library.
TF_CAPI_EXPORT extern void TF_DeletePluggableDeviceLibraryHandle(
TF_Library* lib_handle);

#ifdef __cplusplus
} /* end extern "C" */
#endif
Expand Down
18 changes: 18 additions & 0 deletions tensorflow/c/c_api_experimental_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ limitations under the License.
#include "tensorflow/core/lib/io/path.h"
#include "tensorflow/core/platform/env.h"
#include "tensorflow/core/platform/logging.h"
#include "tensorflow/core/platform/resource_loader.h"
#include "tensorflow/core/platform/test.h"
#include "tensorflow/core/protobuf/tensorflow_server.pb.h"

Expand Down Expand Up @@ -234,5 +235,22 @@ TEST_F(ShapeInferenceTest, InfersShapesFromInputTensors) {
TF_DeleteTensor(tensor_1X6);
}

TEST(CAPI_EXPERIMENTAL, LibraryPluggableDeviceLoadFunctions) {
#if !defined(TENSORFLOW_NO_SHARED_OBJECTS)
// Load the library.
TF_Status* status = TF_NewStatus();
string lib_path =
tensorflow::GetDataDependencyFilepath(tensorflow::io::JoinPath(
"tensorflow", "c", "experimental", "stream_executor", "test",
"test_pluggable_device.so"));
TF_Library* lib = TF_LoadPluggableDeviceLibrary(lib_path.c_str(), status);
TF_Code code = TF_GetCode(status);
string status_msg(TF_Message(status));
TF_DeleteStatus(status);
ASSERT_EQ(TF_OK, code) << status_msg;
TF_DeletePluggableDeviceLibraryHandle(lib);
#endif // !defined(TENSORFLOW_NO_SHARED_OBJECTS)
}

} // namespace
} // namespace tensorflow
13 changes: 13 additions & 0 deletions tensorflow/c/experimental/stream_executor/test/BUILD
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Description:
# test for stream_executor
load(
"//tensorflow:tensorflow.bzl",
"tf_cc_shared_object",
)

tf_cc_shared_object(
name = "test_pluggable_device.so",
srcs = ["test_pluggable_device.cc"],
visibility = ["//tensorflow/c:__subpackages__"],
deps = ["//tensorflow/c/experimental/stream_executor:stream_executor_hdrs"],
)
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
/* Copyright 2020 The TensorFlow Authors. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
==============================================================================*/

#include "tensorflow/c/experimental/stream_executor/stream_executor.h"

void SE_InitPlugin(SE_PlatformRegistrationParams* const params,
TF_Status* const status) {
params->platform->struct_size = SP_PLATFORM_STRUCT_SIZE;
params->platform->name = "GPU";
params->platform->type = "XGPU";
}
2 changes: 0 additions & 2 deletions tensorflow/core/framework/device_base.h
Original file line number Diff line number Diff line change
Expand Up @@ -173,7 +173,6 @@ class DeviceBase {
// Does not take ownership.
void set_eigen_cpu_device(Eigen::ThreadPoolDevice* d);


// Return the Allocator implementation to use based on the allocator
// attributes requested. See allocator.h for more details.
virtual Allocator* GetAllocator(AllocatorAttributes /*attr*/) {
Expand Down Expand Up @@ -204,7 +203,6 @@ class DeviceBase {

virtual const Eigen::ThreadPoolDevice* eigen_cpu_device();


// Caller owns the return value. The OpKernelContext calls this even
// for devices that do not implement an eigen_gpu_device. Overridden
// by GPU devices to return a derived type.
Expand Down
2 changes: 1 addition & 1 deletion tensorflow/python/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,9 +36,9 @@

# go/tf-wildcard-import
# pylint: disable=wildcard-import,g-bad-import-order,g-import-not-at-top
from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow

from tensorflow.python.eager import context
from tensorflow.python import pywrap_tensorflow as _pywrap_tensorflow
Comment thread
annarev marked this conversation as resolved.
Resolved

# pylint: enable=wildcard-import

Expand Down
17 changes: 17 additions & 0 deletions tensorflow/python/client/tf_session_wrapper.cc
Original file line number Diff line number Diff line change
Expand Up @@ -711,6 +711,18 @@ PYBIND11_MODULE(_pywrap_tf_session, m) {
},
py::return_value_policy::reference);

m.def(
"TF_LoadPluggableDeviceLibrary",
[](const char* library_filename) {
tensorflow::Safe_TF_StatusPtr status =
tensorflow::make_safe(TF_NewStatus());
auto output =
TF_LoadPluggableDeviceLibrary(library_filename, status.get());
tensorflow::MaybeRaiseRegisteredFromTFStatus(status.get());
return output;
},
py::return_value_policy::reference);

m.def("TF_GetOpList", [](TF_Library* lib_handle) {
TF_Buffer output_buffer = TF_GetOpList(lib_handle);
return tensorflow::PyoOrThrow(PyBytes_FromStringAndSize(
Expand All @@ -720,6 +732,11 @@ PYBIND11_MODULE(_pywrap_tf_session, m) {

m.def("TF_DeleteLibraryHandle", TF_DeleteLibraryHandle,
py::call_guard<py::gil_scoped_release>());

m.def("TF_PluggableDeviceLibraryHandle",
TF_DeletePluggableDeviceLibraryHandle,
py::call_guard<py::gil_scoped_release>());

m.def("TF_AddControlInput", TF_AddControlInput);
m.def(
"TF_AddInputList", [](TF_OperationDescription* desc, py::handle& inputs) {
Expand Down
17 changes: 14 additions & 3 deletions tensorflow/python/eager/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -1242,12 +1242,17 @@ def invoking_op_callbacks(self):
def invoking_op_callbacks(self, value):
self._thread_local_data.invoking_op_callbacks = value

def _initialize_physical_devices(self):
"""Get local devices visible to the system."""
def _initialize_physical_devices(self, reinitialize=False):
Comment thread
penpornk marked this conversation as resolved.
Resolved
"""Gets local devices visible to the system.

Args:
reinitialize: If True, reinitializes self._physical_devices so that
dynamic registered devices will also be visible to the python front-end.
"""
# We lazy initialize self._physical_devices since we do not want to do this
# the constructor since the backend may not be initialized yet.
with self._device_lock:
if self._physical_devices is not None:
if reinitialize is False and self._physical_devices is not None:
return

devs = pywrap_tfe.TF_ListPhysicalDevices()
Expand All @@ -1265,6 +1270,12 @@ def _initialize_physical_devices(self):

# Import device settings that may have been passed into the constructor
self._import_config()

def reinitialize_physical_devices(self):
"""Gets local devices visible to the system."""
# Reinitialize the physical device list after registering
# the pluggable device.
self._initialize_physical_devices(True)

def list_physical_devices(self, device_type=None):
"""List local devices visible to the system.
Expand Down
37 changes: 36 additions & 1 deletion tensorflow/python/framework/load_library.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
from tensorflow.python.client import pywrap_tf_session as py_tf
from tensorflow.python.util import deprecation
from tensorflow.python.util.tf_export import tf_export

from tensorflow.python.eager import context

@tf_export('load_op_library')
def load_op_library(library_filename):
Expand Down Expand Up @@ -158,6 +158,41 @@ def load_library(library_location):
'The file or folder to load kernel libraries from does not exist.',
library_location)

def load_pluggable_device_library(library_location):
"""Loads a TensorFlow PluggableDevice plugin.
"library_location" can be a path to a specific shared object, or a folder.
If it is a folder, all shared objects will be loaded. when the library is
loaded, devices/kernels registered in the library via StreamExecutor C API
and Kernel/Op Registration C API are made available in TensorFlow process.

Args:
library_location: Path to the plugin or folder of plugins.
Relative or absolute filesystem path to a dynamic library file or folder.

Raises:
OSError: When the file to be loaded is not found.
RuntimeError: when unable to load the library.
"""
if os.path.exists(library_location):
if os.path.isdir(library_location):
directory_contents = os.listdir(library_location)

pluggable_device_libraries = [
os.path.join(library_location, f) for f in directory_contents
if _is_shared_object(f)]
else:
pluggable_device_libraries = [library_location]

for lib in pluggable_device_libraries:
py_tf.TF_LoadPluggableDeviceLibrary(lib)
# Reinitialized physical devices list after plugin registration.
context.context().reinitialize_physical_devices()
else:
raise OSError(
errno.ENOENT,
'The file or folder to load pluggable device libraries from does\
not exist.',
library_location)

@tf_export('experimental.register_filesystem_plugin')
def register_filesystem_plugin(plugin_location):
Expand Down