Skip to content

Commit aa22def

Browse files
Merge pull request #44747 from Intel-tensorflow:stream_executor_C_API_extension
PiperOrigin-RevId: 344156372 Change-Id: I036a76c7ccf5f25e45767b3bf3270cbed9a56830
2 parents 29bb0de + 9836146 commit aa22def

9 files changed

Lines changed: 207 additions & 114 deletions

File tree

‎tensorflow/c/BUILD‎

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -522,6 +522,7 @@ cc_library(
522522
":tf_datatype",
523523
":tf_status",
524524
":tf_tensor",
525+
"//tensorflow/c/experimental/stream_executor:stream_executor_hdrs",
525526
],
526527
)
527528

@@ -542,13 +543,17 @@ tf_cuda_library(
542543
] + select({
543544
"//tensorflow:android": [
544545
":c_api_internal",
546+
"//tensorflow/c/experimental/stream_executor:stream_executor_hdrs",
545547
"//tensorflow/core:portable_tensorflow_lib_lite",
546548
],
547549
"//conditions:default": [
548550
":c_api_internal",
549551
":tf_tensor",
552+
"//tensorflow/stream_executor:stream",
550553
"//tensorflow/core:framework",
551554
"//tensorflow/core:framework_lite",
555+
"//tensorflow/c/experimental/stream_executor:stream_executor",
556+
"//tensorflow/c/experimental/stream_executor:stream_executor_internal",
552557
],
553558
}),
554559
)

‎tensorflow/c/experimental/stream_executor/BUILD‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,21 @@ load(
77
"tf_cc_test",
88
)
99

10+
# buildifier: disable=same-origin-load
11+
load("//tensorflow:tensorflow.bzl", "filegroup")
12+
1013
package(
1114
licenses = ["notice"], # Apache 2.0
1215
)
1316

17+
filegroup(
18+
name = "headers",
19+
srcs = [
20+
"stream_executor.h",
21+
],
22+
visibility = ["//tensorflow:__subpackages__"],
23+
)
24+
1425
cc_library(
1526
name = "stream_executor_hdrs",
1627
hdrs = ["stream_executor.h"],
@@ -49,9 +60,11 @@ cc_library(
4960
"stream_executor.h",
5061
"stream_executor_internal.h",
5162
],
63+
visibility = ["//tensorflow/c:__subpackages__"],
5264
deps = [
5365
"//tensorflow/c:c_api_macros",
5466
"//tensorflow/c:tf_status",
67+
"//tensorflow/c:tf_status_helper",
5568
"//tensorflow/stream_executor:executor_cache",
5669
"//tensorflow/stream_executor/lib",
5770
],

‎tensorflow/c/experimental/stream_executor/stream_executor.cc‎

Lines changed: 1 addition & 112 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,6 @@ limitations under the License.
2424
#include <string>
2525

2626
#include "tensorflow/c/experimental/stream_executor/stream_executor_internal.h"
27-
#include "tensorflow/c/tf_status_helper.h"
2827
#include "tensorflow/core/platform/env.h"
2928
#include "tensorflow/core/platform/errors.h"
3029
#include "tensorflow/core/platform/logging.h"
@@ -44,6 +43,7 @@ using tensorflow::StatusFromTF_Status;
4443

4544
namespace stream_executor {
4645
using tensorflow::StringPiece;
46+
using OwnedTFStatus = std::unique_ptr<TF_Status, TFStatusDeleter>;
4747

4848
namespace {
4949

@@ -188,41 +188,6 @@ port::Status ValidateSEPlatformRegistrationParams(
188188
}
189189
#undef VALIDATE_MEMBER
190190

191-
struct TFStatusDeleter {
192-
void operator()(TF_Status* s) const { TF_DeleteStatus(s); }
193-
};
194-
using OwnedTFStatus = std::unique_ptr<TF_Status, TFStatusDeleter>;
195-
196-
class CStream : public internal::StreamInterface {
197-
public:
198-
CStream(SP_Device* device, SP_StreamExecutor* stream_executor)
199-
: device_(device),
200-
stream_executor_(stream_executor),
201-
stream_handle_(nullptr) {}
202-
~CStream() override { Destroy(); }
203-
204-
port::Status Create() {
205-
OwnedTFStatus c_status(TF_NewStatus());
206-
stream_executor_->create_stream(device_, &stream_handle_, c_status.get());
207-
port::Status s = StatusFromTF_Status(c_status.get());
208-
return s;
209-
}
210-
211-
void Destroy() {
212-
if (stream_handle_ != nullptr) {
213-
stream_executor_->destroy_stream(device_, stream_handle_);
214-
stream_handle_ = nullptr;
215-
}
216-
}
217-
218-
SP_Stream Handle() { return stream_handle_; }
219-
220-
private:
221-
SP_Device* device_;
222-
SP_StreamExecutor* stream_executor_;
223-
SP_Stream stream_handle_;
224-
};
225-
226191
// Converts SE_EventStatus to Event::Status.
227192
Event::Status SEEventStatusToEventStatus(SE_EventStatus s) {
228193
switch (s) {
@@ -237,82 +202,6 @@ Event::Status SEEventStatusToEventStatus(SE_EventStatus s) {
237202
}
238203
}
239204

240-
class CEvent : public internal::EventInterface {
241-
public:
242-
CEvent(SP_Device* device, SP_StreamExecutor* stream_executor)
243-
: device_(device),
244-
stream_executor_(stream_executor),
245-
event_handle_(nullptr) {}
246-
~CEvent() override { Destroy(); }
247-
248-
port::Status Create() {
249-
OwnedTFStatus c_status(TF_NewStatus());
250-
stream_executor_->create_event(device_, &event_handle_, c_status.get());
251-
return StatusFromTF_Status(c_status.get());
252-
}
253-
254-
port::Status Record(SP_Stream stream_handle) {
255-
OwnedTFStatus c_status(TF_NewStatus());
256-
stream_executor_->record_event(device_, stream_handle, event_handle_,
257-
c_status.get());
258-
return StatusFromTF_Status(c_status.get());
259-
}
260-
261-
void Destroy() {
262-
if (event_handle_ != nullptr) {
263-
stream_executor_->destroy_event(device_, event_handle_);
264-
event_handle_ = nullptr;
265-
}
266-
}
267-
268-
SP_Event Handle() { return event_handle_; }
269-
270-
private:
271-
SP_Device* device_;
272-
SP_StreamExecutor* stream_executor_;
273-
SP_Event event_handle_;
274-
};
275-
276-
class CTimer : public internal::TimerInterface {
277-
public:
278-
CTimer(SP_Device* device, SP_StreamExecutor* stream_executor,
279-
SP_TimerFns* timer_fns)
280-
: device_(device),
281-
stream_executor_(stream_executor),
282-
timer_handle_(nullptr),
283-
timer_fns_(timer_fns) {}
284-
~CTimer() override { Destroy(); }
285-
286-
port::Status Create() {
287-
OwnedTFStatus c_status(TF_NewStatus());
288-
stream_executor_->create_timer(device_, &timer_handle_, c_status.get());
289-
return StatusFromTF_Status(c_status.get());
290-
}
291-
292-
void Destroy() {
293-
if (timer_handle_ != nullptr) {
294-
stream_executor_->destroy_timer(device_, timer_handle_);
295-
timer_handle_ = nullptr;
296-
}
297-
}
298-
299-
SP_Timer Handle() { return timer_handle_; }
300-
301-
uint64 Microseconds() const override {
302-
return timer_fns_->nanoseconds(timer_handle_) / 1000;
303-
}
304-
305-
uint64 Nanoseconds() const override {
306-
return timer_fns_->nanoseconds(timer_handle_);
307-
}
308-
309-
private:
310-
SP_Device* device_;
311-
SP_StreamExecutor* stream_executor_;
312-
SP_Timer timer_handle_;
313-
SP_TimerFns* timer_fns_;
314-
};
315-
316205
// Converts DeviceMemoryBase to a C struct.
317206
SP_DeviceMemoryBase DeviceMemoryBaseToC(const DeviceMemoryBase* mem) {
318207
SP_DeviceMemoryBase device_memory_base{SP_DEVICE_MEMORY_BASE_STRUCT_SIZE};

‎tensorflow/c/experimental/stream_executor/stream_executor_internal.h‎

Lines changed: 114 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ limitations under the License.
1919
#define TENSORFLOW_C_EXPERIMENTAL_STREAM_EXECUTOR_STREAM_EXECUTOR_INTERNAL_H_
2020

2121
#include "tensorflow/c/experimental/stream_executor/stream_executor.h"
22+
#include "tensorflow/c/tf_status_helper.h"
2223
#include "tensorflow/stream_executor/executor_cache.h"
2324
#include "tensorflow/stream_executor/lib/status.h"
2425
#include "tensorflow/stream_executor/platform.h"
@@ -37,6 +38,13 @@ port::Status InitStreamExecutorPlugin(void* dso_handle);
3738
// testing).
3839
port::Status InitStreamExecutorPlugin(SEInitPluginFn init_fn);
3940

41+
struct TFStatusDeleter {
42+
void operator()(TF_Status* s) const { TF_DeleteStatus(s); }
43+
};
44+
45+
// This file implements core stream executor base classes in terms of
46+
// the C API defined in stream_executor.h. A class "CSomething" represents a
47+
// "Something" that can be manipulated via calls in the C interface.
4048
class CPlatform : public Platform {
4149
public:
4250
explicit CPlatform(SP_Platform platform,
@@ -83,5 +91,111 @@ class CPlatform : public Platform {
8391
stream_executor::ExecutorCache executor_cache_;
8492
};
8593

94+
class CStream : public internal::StreamInterface {
95+
public:
96+
CStream(SP_Device* device, SP_StreamExecutor* stream_executor)
97+
: device_(device),
98+
stream_executor_(stream_executor),
99+
stream_handle_(nullptr) {}
100+
~CStream() override { Destroy(); }
101+
102+
port::Status Create() {
103+
std::unique_ptr<TF_Status, TFStatusDeleter> c_status(TF_NewStatus());
104+
stream_executor_->create_stream(device_, &stream_handle_, c_status.get());
105+
port::Status s = tensorflow::StatusFromTF_Status(c_status.get());
106+
return s;
107+
}
108+
109+
void Destroy() {
110+
if (stream_handle_ != nullptr) {
111+
stream_executor_->destroy_stream(device_, stream_handle_);
112+
stream_handle_ = nullptr;
113+
}
114+
}
115+
116+
SP_Stream Handle() { return stream_handle_; }
117+
118+
private:
119+
SP_Device* device_;
120+
SP_StreamExecutor* stream_executor_;
121+
SP_Stream stream_handle_;
122+
};
123+
124+
class CEvent : public internal::EventInterface {
125+
public:
126+
CEvent(SP_Device* device, SP_StreamExecutor* stream_executor)
127+
: device_(device),
128+
stream_executor_(stream_executor),
129+
event_handle_(nullptr) {}
130+
~CEvent() override { Destroy(); }
131+
132+
port::Status Create() {
133+
std::unique_ptr<TF_Status, TFStatusDeleter> c_status(TF_NewStatus());
134+
stream_executor_->create_event(device_, &event_handle_, c_status.get());
135+
return tensorflow::StatusFromTF_Status(c_status.get());
136+
}
137+
138+
port::Status Record(SP_Stream stream_handle) {
139+
std::unique_ptr<TF_Status, TFStatusDeleter> c_status(TF_NewStatus());
140+
stream_executor_->record_event(device_, stream_handle, event_handle_,
141+
c_status.get());
142+
return tensorflow::StatusFromTF_Status(c_status.get());
143+
}
144+
145+
void Destroy() {
146+
if (event_handle_ != nullptr) {
147+
stream_executor_->destroy_event(device_, event_handle_);
148+
event_handle_ = nullptr;
149+
}
150+
}
151+
152+
SP_Event Handle() { return event_handle_; }
153+
154+
private:
155+
SP_Device* device_;
156+
SP_StreamExecutor* stream_executor_;
157+
SP_Event event_handle_;
158+
};
159+
160+
class CTimer : public internal::TimerInterface {
161+
public:
162+
CTimer(SP_Device* device, SP_StreamExecutor* stream_executor,
163+
SP_TimerFns* timer_fns)
164+
: device_(device),
165+
stream_executor_(stream_executor),
166+
timer_handle_(nullptr),
167+
timer_fns_(timer_fns) {}
168+
~CTimer() override { Destroy(); }
169+
170+
port::Status Create() {
171+
std::unique_ptr<TF_Status, TFStatusDeleter> c_status(TF_NewStatus());
172+
stream_executor_->create_timer(device_, &timer_handle_, c_status.get());
173+
return tensorflow::StatusFromTF_Status(c_status.get());
174+
}
175+
176+
void Destroy() {
177+
if (timer_handle_ != nullptr) {
178+
stream_executor_->destroy_timer(device_, timer_handle_);
179+
timer_handle_ = nullptr;
180+
}
181+
}
182+
183+
SP_Timer Handle() { return timer_handle_; }
184+
185+
uint64 Microseconds() const override {
186+
return timer_fns_->nanoseconds(timer_handle_) / 1000;
187+
}
188+
189+
uint64 Nanoseconds() const override {
190+
return timer_fns_->nanoseconds(timer_handle_);
191+
}
192+
193+
private:
194+
SP_Device* device_;
195+
SP_StreamExecutor* stream_executor_;
196+
SP_Timer timer_handle_;
197+
SP_TimerFns* timer_fns_;
198+
};
199+
86200
} // namespace stream_executor
87201
#endif // TENSORFLOW_C_EXPERIMENTAL_STREAM_EXECUTOR_STREAM_EXECUTOR_INTERNAL_H_

‎tensorflow/c/kernels.cc‎

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,13 @@ limitations under the License.
2424
#include "tensorflow/core/framework/op_kernel.h"
2525
#include "tensorflow/core/framework/register_types.h"
2626
#include "tensorflow/core/framework/types.h"
27+
// Required for IS_MOBILE_PLATFORM definition
28+
#include "tensorflow/core/platform/platform.h"
2729
#include "tensorflow/core/platform/types.h"
30+
#if !defined(IS_MOBILE_PLATFORM) && !defined(IS_SLIM_BUILD)
31+
#include "tensorflow/c/experimental/stream_executor/stream_executor_internal.h"
32+
#include "tensorflow/stream_executor/stream.h"
33+
#endif // !defined(IS_MOBILE_PLATFORM) && !defined(IS_SLIM_BUILD)
2834

2935
// This file forms the basis of a stable ABI for third-party kernel
3036
// implementations. It is crucial that changes to this file are made cautiously
@@ -168,6 +174,35 @@ void TF_RegisterKernelBuilder(const char* name, TF_KernelBuilder* builder,
168174
TF_SetStatus(status, TF_OK, "");
169175
}
170176

177+
// This function is only for pluggable device.
178+
// It will return nullptr in all other cases.
179+
// This function is experimental and subject to change.
180+
SP_Stream TF_GetStream(TF_OpKernelContext* ctx, TF_Status* status) {
181+
#if defined(IS_MOBILE_PLATFORM) || defined(IS_SLIM_BUILD)
182+
status->status = tensorflow::errors::Unimplemented(
183+
"Accessing device stream is not supported on mobile. File a bug at "
184+
"https://github.com/tensorflow/tensorflow/issues if this feature is "
185+
"important to you");
186+
return nullptr;
187+
#else
188+
auto* cc_ctx = reinterpret_cast<::tensorflow::OpKernelContext*>(ctx);
189+
if (cc_ctx->op_device_context() == nullptr) { // CPU Device
190+
status->status = tensorflow::errors::FailedPrecondition(
191+
"Accessing device stream is not supported for a CPU device.");
192+
return nullptr;
193+
} else if (!cc_ctx->op_device_context()->IsPluggableDevice()) {
194+
status->status = tensorflow::errors::FailedPrecondition(
195+
"Accessing device stream is only supported for pluggable devices.");
196+
return nullptr;
197+
} else { // Is a PluggableDevice
198+
TF_SetStatus(status, TF_OK, "");
199+
auto c_stream = static_cast<stream_executor::CStream*>(
200+
cc_ctx->op_device_context()->stream()->implementation());
201+
return c_stream->Handle();
202+
}
203+
#endif // defined(IS_MOBILE_PLATFORM) || defined(IS_SLIM_BUILD)
204+
}
205+
171206
int TF_NumInputs(TF_OpKernelContext* ctx) {
172207
auto* cc_ctx = reinterpret_cast<::tensorflow::OpKernelContext*>(ctx);
173208
return cc_ctx->num_inputs();

0 commit comments

Comments
 (0)