aboutsummaryrefslogtreecommitdiffhomepage
path: root/tensorflow/contrib/lite/context.h
diff options
context:
space:
mode:
authorGravatar Yu-Cheng Ling <ycling@google.com>2018-03-07 17:42:08 -0800
committerGravatar TensorFlower Gardener <gardener@tensorflow.org>2018-03-07 17:46:14 -0800
commit5594bc3c43f6829b7ea77f96852c98fb41e4deb2 (patch)
treea7d2f5771c1758ec5a2fbf69ebe79c8bab6d9693 /tensorflow/contrib/lite/context.h
parent9cdfd3878935fb6c3c2a5da7f65ee0db6c751170 (diff)
TFLite: Delegate Buffer Handle interface
PiperOrigin-RevId: 188263046
Diffstat (limited to 'tensorflow/contrib/lite/context.h')
-rw-r--r--tensorflow/contrib/lite/context.h64
1 files changed, 61 insertions, 3 deletions
diff --git a/tensorflow/contrib/lite/context.h b/tensorflow/contrib/lite/context.h
index ed7f4515fa..d901b9f065 100644
--- a/tensorflow/contrib/lite/context.h
+++ b/tensorflow/contrib/lite/context.h
@@ -29,6 +29,7 @@ limitations under the License.
#ifndef TENSORFLOW_CONTRIB_LITE_CONTEXT_H_
#define TENSORFLOW_CONTRIB_LITE_CONTEXT_H_
+#include <stdbool.h>
#include <stdint.h>
#include <stdlib.h>
@@ -40,6 +41,7 @@ typedef enum { kTfLiteOk = 0, kTfLiteError = 1 } TfLiteStatus;
// Forward declare so GetNode can use this is in Context.
typedef struct _TfLiteRegistration TfLiteRegistration;
+typedef struct _TfLiteDelegate TfLiteDelegate;
#define kOptionalTensor (-1)
@@ -57,6 +59,10 @@ typedef struct {
#endif
} TfLiteIntArray;
+// Given the size (number of elements) in a TfLiteIntArray, calculate its size
+// in bytes.
+int TfLiteIntArrayGetSizeInBytes(int size);
+
// Create a array of a given `size` (uninitialized entries).
// This returns a pointer, that you must free using TfLiteIntArrayFree().
TfLiteIntArray* TfLiteIntArrayCreate(int size);
@@ -162,6 +168,11 @@ typedef enum {
kTfLiteDynamic,
} TfLiteAllocationType;
+// The delegates should use zero or positive integers to represent handles.
+// -1 is reserved from unallocated status.
+typedef int TfLiteDelegateBufferHandle;
+const TfLiteDelegateBufferHandle kTfLiteNullBufferHandle = -1;
+
// An tensor in the interpreter system which is a wrapper around a buffer of
// data including a dimensionality (or NULL if not currently defined).
typedef struct {
@@ -194,6 +205,22 @@ typedef struct {
// Null-terminated name of this tensor.
const char* name;
+
+ // The delegate which knows how to handle `delegate_buffer_handle`.
+ // WARNING: This is an experimental interface that is subject to change.
+ TfLiteDelegate* delegate;
+
+ // An integer buffer handle that can be handled by `delegate`.
+ // The value is valid only when delegate is not null.
+ // WARNING: This is an experimental interface that is subject to change.
+ TfLiteDelegateBufferHandle delegate_buffer_handle;
+
+ // If the delegate uses its own buffer (e.g. GPU memory), the delegate is
+ // responsible to set data_is_stale to true.
+ // `delegate->CopyFromBufferHandle` can be called to copy the data from
+ // delegate buffer.
+ // WARNING: This is an // experimental interface that is subject to change.
+ bool data_is_stale;
} TfLiteTensor;
// Free memory of tensor `t`;
@@ -234,6 +261,11 @@ typedef struct {
// WARNING: This is an experimental interface that is subject to change.
const void* custom_initial_data;
int custom_initial_data_size;
+
+ // The pointer to the delegate. This is non-null only when the node is
+ // created by calling `interpreter.ModifyGraphWithDelegate`.
+ // WARNING: This is an experimental interface that is subject to change.
+ TfLiteDelegate* delegate;
} TfLiteNode;
typedef struct TfLiteContext {
@@ -287,7 +319,7 @@ typedef struct TfLiteContext {
// does not take ownership of `nodes_to_replace`.
TfLiteStatus (*ReplaceSubgraphsWithDelegateKernels)(
struct TfLiteContext*, TfLiteRegistration registration,
- const TfLiteIntArray* nodes_to_replace);
+ const TfLiteIntArray* nodes_to_replace, TfLiteDelegate* delegate);
// TODO(ahentz): we should create a more general mechanism for this sort of
// library-global objects.
@@ -338,19 +370,45 @@ typedef struct _TfLiteRegistration {
} TfLiteRegistration;
// WARNING: This is an experimental interface that is subject to change.
-typedef struct {
+typedef struct _TfLiteDelegate {
// Data that delegate needs to identify itself. This data is owned by the
// delegate. The delegate is owned in the user code, so the delegate is
// responsible for doing this when it is destroyed.
void* data_;
+
// Invoked by ModifyGraphWithDelegate. This prepare is called, giving the
// delegate a view of the current graph through TfLiteContext*. It typically
// will look at the nodes and call ReplaceSubgraphsWithDelegateKernels()
// to ask the TensorFlow lite runtime to create macro-nodes to represent
// delegated subgraphs of the original graph.
- TfLiteStatus (*Prepare)(TfLiteContext* context, void* data);
+ TfLiteStatus (*Prepare)(TfLiteContext* context, TfLiteDelegate* delegate);
+
+ // Copy the data from delegate buffer handle to raw memory.
+ // This can be null if the delegate doesn't use its own buffer.
+ TfLiteStatus (*CopyFromBufferHandle)(
+ TfLiteDelegate* delegate,
+ TfLiteDelegateBufferHandle delegate_buffer_handle, void* data, int size);
+
+ // Copy the data from raw memory to delegate buffer handle.
+ // This can be null if the delegate doesn't use its own buffer.
+ TfLiteStatus (*CopyToBufferHandle)(
+ TfLiteDelegate* delegate,
+ TfLiteDelegateBufferHandle delegate_buffer_handle, void* data, int size);
+
+ // Free the Delegate Buffer Handle. Note: This only frees the handle, but
+ // this doesn't release the underlying resource (e.g. textures). The
+ // resources are either owned by application layer or the delegate.
+ // This can be null if the delegate doesn't use its own buffer.
+ void (*FreeBufferHandle)(TfLiteDelegate* delegate,
+ TfLiteDelegateBufferHandle* handle);
} TfLiteDelegate;
+// WARNING: This is an experimental interface that is subject to change.
+typedef struct {
+ TfLiteDelegate* delegate;
+ TfLiteIntArray* nodes_to_replace;
+} TfLiteDelegateParams;
+
#ifdef __cplusplus
} // extern "C"
#endif // __cplusplus