Skip to content

Commit 253111e

Browse files
allenlavoietensorflower-gardener
authored andcommitted
Stop holding custom devices in TensorHandles
My hope is to not change the custom device API much, just clean up the implementation. Previously TensorHandles held Tensors which held the void* custom device tensor handle data. This required a bunch of special cases, mostly because the TensorHandle's device wasn't a physical device. Now EagerOperations still accept custom device TensorHandles, but deal with them before execution (either by copying them off the custom device or by executing the operation on a custom device). This means the rest of the runtime can assume TensorHandles are on physical devices, and gives custom device tensor handles some freedom to evolve. Rolling cl/350684489 forward with a fix for making packed tensors from custom device tensors. This requires one new custom device method to maintain the previous (accidental) functionality. PiperOrigin-RevId: 351406348 Change-Id: I9c2ffd40a687b06434fab40e2db9e90129b9f2b7
1 parent 0e43a67 commit 253111e

27 files changed

Lines changed: 524 additions & 432 deletions

‎tensorflow/c/eager/BUILD‎

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ tf_cuda_library(
5252
":immediate_execution_operation",
5353
":immediate_execution_tensor_handle",
5454
":immediate_execution_distributed_manager",
55-
":abstract_tensor_handle",
5655
":tfe_context_internal",
5756
":tfe_cancellation_manager_internal",
5857
":tfe_executor_internal",
@@ -73,6 +72,7 @@ tf_cuda_library(
7372
"//tensorflow/core/common_runtime/eager:context",
7473
"//tensorflow/core/common_runtime/eager:context_distributed_manager",
7574
"//tensorflow/core/common_runtime/eager:core",
75+
"//tensorflow/core/common_runtime/eager:custom_device",
7676
"//tensorflow/core/common_runtime/eager:eager_executor",
7777
"//tensorflow/core/common_runtime/eager:execute",
7878
"//tensorflow/core/common_runtime/eager:tensor_handle",
@@ -86,6 +86,7 @@ tf_cuda_library(
8686
],
8787
}) + [
8888
"@com_google_absl//absl/memory",
89+
":abstract_tensor_handle",
8990
"//tensorflow/core/common_runtime/eager:eager_operation",
9091
"//tensorflow/core/distributed_runtime/eager:remote_mgr",
9192
"//tensorflow/core/distributed_runtime/eager:cluster_function_library_runtime",
@@ -480,12 +481,17 @@ cc_library(
480481
visibility = [
481482
"//tensorflow:internal",
482483
],
483-
deps = [
484-
"//tensorflow/core:framework",
485-
"//tensorflow/core:protos_all_cc",
486-
"//tensorflow/core/platform:refcount",
487-
"//tensorflow/core/platform:status",
488-
],
484+
deps = select({
485+
"//tensorflow:android": [
486+
"//tensorflow/core:portable_tensorflow_lib_lite",
487+
],
488+
"//conditions:default": [
489+
"//tensorflow/core:framework",
490+
"//tensorflow/core:protos_all_cc",
491+
"//tensorflow/core/platform:refcount",
492+
"//tensorflow/core/platform:status",
493+
],
494+
}),
489495
)
490496

491497
cc_library(

‎tensorflow/c/eager/c_api.cc‎

Lines changed: 157 additions & 84 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,7 @@ limitations under the License.
4040
#include "tensorflow/core/common_runtime/device_mgr.h"
4141
#include "tensorflow/core/common_runtime/eager/attr_builder.h"
4242
#include "tensorflow/core/common_runtime/eager/context.h"
43+
#include "tensorflow/core/common_runtime/eager/custom_device.h"
4344
#include "tensorflow/core/common_runtime/eager/execute.h"
4445
#include "tensorflow/core/common_runtime/eager/tensor_handle.h"
4546
#include "tensorflow/core/common_runtime/function.h"
@@ -363,21 +364,29 @@ void* TFE_TensorHandleDevicePointer(TFE_TensorHandle* h, TF_Status* status) {
363364
status->status = tensorflow::errors::InvalidArgument("Invalid handle");
364365
return nullptr;
365366
}
366-
tensorflow::TensorHandle* handle =
367-
tensorflow::TensorHandleFromInterface(tensorflow::unwrap(h));
368-
if (VariantDeviceIsCustom(handle->device())) {
369-
const tensorflow::Tensor* t;
370-
status->status = handle->Tensor(&t);
371-
return t->data();
367+
tensorflow::ImmediateExecutionTensorHandle* unwrapped_handle =
368+
tensorflow::unwrap(h);
369+
// TODO(b/175427838): It would be nice to be able to use tensorflow::isa here.
370+
if (tensorflow::CustomDeviceTensorHandle::classof(unwrapped_handle)) {
371+
return tensorflow::down_cast<tensorflow::CustomDeviceTensorHandle*>(
372+
unwrapped_handle)
373+
->DevicePointer();
374+
}
375+
// TODO(b/175427838): It would be nice to be able to use tensorflow::isa here.
376+
if (!tensorflow::TensorHandle::classof(unwrapped_handle)) {
377+
status->status = tensorflow::errors::InvalidArgument("Invalid handle");
378+
return nullptr;
372379
}
380+
tensorflow::TensorHandle* handle =
381+
tensorflow::TensorHandleFromInterface(unwrapped_handle);
373382

374383
if (handle->Type() != tensorflow::TensorHandle::LOCAL) {
375384
status->status = tensorflow::errors::InvalidArgument(
376385
"TFE_TensorHandleDevicePointer may not be called on a ",
377386
handle->TypeString(), " tensor handle.");
378387
return nullptr;
379388
}
380-
tensorflow::Device* device(absl::get<tensorflow::Device*>(handle->device()));
389+
tensorflow::Device* device(handle->device());
381390
if (device != nullptr) {
382391
status->status = device->Sync();
383392
if (!status->status.ok()) {
@@ -393,6 +402,125 @@ void* TFE_TensorHandleDevicePointer(TFE_TensorHandle* h, TF_Status* status) {
393402
static_cast<const void*>(tensor->tensor_data().data()));
394403
}
395404

405+
namespace tensorflow {
406+
namespace {
407+
class CustomDeviceAPI : public tensorflow::CustomDevice {
408+
public:
409+
CustomDeviceAPI(TFE_Context* context, TFE_CustomDevice device, void* info,
410+
string name)
411+
: context_(context), device_(device), info_(info), name_(name) {}
412+
413+
~CustomDeviceAPI() override { device_.delete_device(info_); }
414+
415+
const string& name() override { return name_; }
416+
417+
tensorflow::Status CopyTensorToDevice(
418+
ImmediateExecutionTensorHandle* handle,
419+
ImmediateExecutionTensorHandle** result) override {
420+
handle->Ref();
421+
TF_Status status;
422+
TFE_TensorHandle* result_handle = device_.copy_tensor_to_device(
423+
context_, tensorflow::wrap(handle), &status, info_);
424+
handle->Release();
425+
if (!status.status.ok()) return status.status;
426+
*result = tensorflow::unwrap(result_handle);
427+
(*result)->Ref();
428+
TFE_DeleteTensorHandle(result_handle);
429+
return status.status;
430+
}
431+
432+
tensorflow::Status CopyTensorFromDevice(
433+
ImmediateExecutionTensorHandle* handle,
434+
const tensorflow::string& target_device_name,
435+
ImmediateExecutionTensorHandle** result) override {
436+
TF_Status status;
437+
handle->Ref();
438+
TFE_TensorHandle* result_handle = device_.copy_tensor_from_device(
439+
context_, tensorflow::wrap(handle), target_device_name.c_str(), &status,
440+
info_);
441+
handle->Release();
442+
if (!status.status.ok()) return status.status;
443+
*result = tensorflow::unwrap(result_handle);
444+
(*result)->Ref();
445+
TFE_DeleteTensorHandle(result_handle);
446+
return status.status;
447+
}
448+
449+
tensorflow::Status Execute(const ImmediateExecutionOperation* op,
450+
ImmediateExecutionTensorHandle** retvals,
451+
int* num_retvals) override {
452+
std::vector<TFE_TensorHandle*> outputs(*num_retvals);
453+
TF_Status status;
454+
device_.execute(tensorflow::wrap(op), num_retvals, outputs.data(), &status,
455+
info_);
456+
if (status.status.ok()) {
457+
for (int i = 0; i < *num_retvals; ++i) {
458+
retvals[i] = tensorflow::unwrap(outputs[i]);
459+
retvals[i]->Ref();
460+
TFE_DeleteTensorHandle(outputs[i]);
461+
}
462+
}
463+
return status.status;
464+
}
465+
466+
tensorflow::Status Pack(absl::Span<ImmediateExecutionTensorHandle*> handles,
467+
ImmediateExecutionTensorHandle** result) override {
468+
TF_Status status;
469+
*result = tensorflow::unwrap(device_.pack(context_,
470+
tensorflow::wrap(handles.data()),
471+
handles.size(), &status, info_));
472+
return status.status;
473+
}
474+
475+
private:
476+
TFE_Context* context_;
477+
TFE_CustomDevice device_;
478+
void* info_;
479+
string name_;
480+
};
481+
482+
// An adapter which wraps the shape/data produced by C custom devices and uses
483+
// it to implement custom device methods.
484+
class CAPICustomDeviceTensorHandle
485+
: public tensorflow::CustomDeviceTensorHandle {
486+
public:
487+
CAPICustomDeviceTensorHandle(tensorflow::ImmediateExecutionContext* context,
488+
tensorflow::CustomDevice* device,
489+
tensorflow::DataType dtype, void* data,
490+
size_t len, std::vector<tensorflow::int64> shape,
491+
void (*deallocator)(void* data, size_t len,
492+
void* arg),
493+
void* deallocator_arg)
494+
: tensorflow::CustomDeviceTensorHandle(context, device, dtype),
495+
data_(data),
496+
len_(len),
497+
shape_(shape),
498+
deallocator_(deallocator),
499+
deallocator_arg_(deallocator_arg) {}
500+
~CAPICustomDeviceTensorHandle() override {
501+
deallocator_(data_, len_, deallocator_arg_);
502+
}
503+
void* DevicePointer() const override { return data_; }
504+
Status NumDims(int* num_dims) const override {
505+
*num_dims = shape_.size();
506+
return Status::OK();
507+
}
508+
Status Dim(int dim_index, int64* dim) const override {
509+
*dim = shape_[dim_index];
510+
return Status::OK();
511+
}
512+
513+
private:
514+
void* const data_;
515+
size_t len_;
516+
std::vector<tensorflow::int64> shape_;
517+
void (*const deallocator_)(void* data, size_t len, void* arg);
518+
void* const deallocator_arg_;
519+
};
520+
521+
} // namespace
522+
} // namespace tensorflow
523+
396524
TFE_TensorHandle* TFE_NewTensorHandleFromDeviceMemory(
397525
TFE_Context* ctx, const char* device_name, TF_DataType dtype,
398526
const int64_t* dims, int num_dims, void* data, size_t len,
@@ -417,6 +545,12 @@ TFE_TensorHandle* TFE_NewTensorHandleFromDeviceMemory(
417545
for (int i = 0; i < num_dims; ++i) {
418546
dimvec[i] = static_cast<tensorflow::int64>(dims[i]);
419547
}
548+
if (custom_device != nullptr) {
549+
return tensorflow::wrap(new tensorflow::CAPICustomDeviceTensorHandle(
550+
context, custom_device,
551+
*reinterpret_cast<tensorflow::DataType*>(&dtype), data, len, dimvec,
552+
deallocator, deallocator_arg));
553+
}
420554

421555
// TODO(apassos) do we need to wrap the deallocator here to make sure to sync
422556
// the device?
@@ -427,13 +561,8 @@ TFE_TensorHandle* TFE_NewTensorHandleFromDeviceMemory(
427561
tensorflow::Tensor t(static_cast<tensorflow::DataType>(dtype),
428562
tensorflow::TensorShape(dimvec), buf);
429563
buf->Unref();
430-
if (custom_device == nullptr) {
431-
return tensorflow::wrap(tensorflow::TensorHandle::CreateLocalHandle(
432-
std::move(t), device, device, context));
433-
} else {
434-
return tensorflow::wrap(tensorflow::TensorHandle::CreateLocalHandle(
435-
std::move(t), custom_device, context));
436-
}
564+
return tensorflow::wrap(tensorflow::TensorHandle::CreateLocalHandle(
565+
std::move(t), device, device, context));
437566
}
438567

439568
// This function will block till the operation that produces `h` has
@@ -961,83 +1090,27 @@ void SetOpAttrValueScalar(TFE_Context* ctx, TFE_Op* op,
9611090
} // namespace tensorflow
9621091

9631092
namespace {
964-
class CustomDeviceAPI : public tensorflow::CustomDevice {
965-
public:
966-
CustomDeviceAPI(TFE_Context* context, TFE_CustomDevice device, void* info,
967-
string name)
968-
: context_(context), device_(device), info_(info), name_(name) {}
969-
970-
~CustomDeviceAPI() override { device_.delete_device(info_); }
971-
972-
const string& name() override { return name_; }
973-
974-
tensorflow::Status CopyTensorToDevice(
975-
tensorflow::TensorHandle* handle,
976-
tensorflow::TensorHandle** result) override {
977-
handle->Ref();
978-
TF_Status status;
979-
TFE_TensorHandle* result_handle = device_.copy_tensor_to_device(
980-
context_, tensorflow::wrap(handle), &status, info_);
981-
handle->Release();
982-
if (!status.status.ok()) return status.status;
983-
*result = tensorflow::TensorHandleFromInterface(
984-
tensorflow::unwrap(result_handle));
985-
(*result)->Ref();
986-
TFE_DeleteTensorHandle(result_handle);
987-
return status.status;
988-
}
989-
990-
tensorflow::Status CopyTensorFromDevice(
991-
tensorflow::TensorHandle* handle,
992-
const tensorflow::string& target_device_name,
993-
tensorflow::TensorHandle** result) override {
994-
TF_Status status;
995-
handle->Ref();
996-
TFE_TensorHandle* result_handle = device_.copy_tensor_from_device(
997-
context_, tensorflow::wrap(handle), target_device_name.c_str(), &status,
998-
info_);
999-
handle->Release();
1000-
if (!status.status.ok()) return status.status;
1001-
*result = tensorflow::TensorHandleFromInterface(
1002-
tensorflow::unwrap(result_handle));
1003-
(*result)->Ref();
1004-
TFE_DeleteTensorHandle(result_handle);
1005-
return status.status;
1006-
}
1007-
1008-
tensorflow::Status Execute(const tensorflow::EagerOperation* op,
1009-
tensorflow::TensorHandle** retvals,
1010-
int* num_retvals) override {
1011-
std::vector<TFE_TensorHandle*> outputs(*num_retvals);
1012-
TF_Status status;
1013-
device_.execute(tensorflow::wrap(op), num_retvals, outputs.data(), &status,
1014-
info_);
1015-
if (status.status.ok()) {
1016-
for (int i = 0; i < *num_retvals; ++i) {
1017-
retvals[i] = tensorflow::TensorHandleFromInterface(
1018-
tensorflow::unwrap(outputs[i]));
1019-
retvals[i]->Ref();
1020-
TFE_DeleteTensorHandle(outputs[i]);
1021-
}
1022-
}
1023-
return status.status;
1024-
}
1025-
1026-
private:
1027-
TFE_Context* context_;
1028-
TFE_CustomDevice device_;
1029-
void* info_;
1030-
string name_;
1031-
};
1093+
TFE_TensorHandle* DefaultCustomDevicePack(TFE_Context* context,
1094+
TFE_TensorHandle** handles,
1095+
int num_handles, TF_Status* status,
1096+
void* device_info) {
1097+
TF_SetStatus(status, TF_UNIMPLEMENTED,
1098+
"This custom device does not support packing tensors.");
1099+
return nullptr;
1100+
}
10321101
} // namespace
10331102

10341103
extern "C" {
10351104

10361105
void TFE_RegisterCustomDevice(TFE_Context* ctx, TFE_CustomDevice device,
10371106
const char* device_name, void* device_info,
10381107
TF_Status* status) {
1039-
auto custom_device =
1040-
std::make_unique<CustomDeviceAPI>(ctx, device, device_info, device_name);
1108+
// Fill in default values for optional functionality.
1109+
if (device.pack == nullptr) {
1110+
device.pack = &DefaultCustomDevicePack;
1111+
}
1112+
auto custom_device = std::make_unique<tensorflow::CustomDeviceAPI>(
1113+
ctx, device, device_info, device_name);
10411114
tensorflow::EagerContext* context =
10421115
tensorflow::ContextFromInterface(tensorflow::unwrap(ctx));
10431116
status->status =

‎tensorflow/c/eager/c_api_experimental.cc‎

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -613,8 +613,23 @@ TFE_TensorHandle* TFE_CreatePackedTensorHandle(TFE_Context* ctx,
613613
std::vector<tensorflow::TensorHandle*> tensor_handles;
614614
tensor_handles.reserve(*num_handles);
615615
for (int i = 0; i < *num_handles; ++i) {
616+
tensorflow::ImmediateExecutionTensorHandle* unwrapped_handle =
617+
tensorflow::unwrap(handles[i]);
618+
if (tensorflow::CustomDeviceTensorHandle::classof(unwrapped_handle)) {
619+
// One of the inputs we're trying to pack is on a custom device. We'll let
620+
// the first custom device we see handle all of the packing.
621+
auto* custom_device_handle =
622+
tensorflow::down_cast<tensorflow::CustomDeviceTensorHandle*>(
623+
unwrapped_handle);
624+
tensorflow::ImmediateExecutionTensorHandle* result;
625+
status->status = custom_device_handle->device()->Pack(
626+
absl::Span<tensorflow::ImmediateExecutionTensorHandle*>(
627+
tensorflow::unwrap(handles), *num_handles),
628+
&result);
629+
return tensorflow::wrap(result);
630+
}
616631
tensor_handles.push_back(
617-
tensorflow::TensorHandleFromInterface(tensorflow::unwrap(handles[i])));
632+
tensorflow::TensorHandleFromInterface(unwrapped_handle));
618633
}
619634
tensorflow::EagerContext* context =
620635
tensorflow::ContextFromInterface(tensorflow::unwrap(ctx));

0 commit comments

Comments
 (0)