Avoid c11 usage in TFLite's C API

Dno't redefine typedefs, but rather use forward declarations
and explicit struct keywords where necessary. Note that this
style was already used for the TfLiteContext type.

PiperOrigin-RevId: 261148927
This commit is contained in:
Jared Duke 2019-08-01 10:33:09 -07:00 committed by TensorFlower Gardener
parent 17a13fbcf1
commit 49b1b6e614
3 changed files with 39 additions and 36 deletions

View File

@ -51,7 +51,11 @@ typedef enum {
kTfLiteMaxExternalContexts = 4
} TfLiteExternalContextType;
// Forward declare so dependent structs and methods can reference these types
// prior to the struct definitions.
struct TfLiteContext;
struct TfLiteDelegate;
struct TfLiteRegistration;
// An external context is a collection of information unrelated to the TF Lite
// framework, but useful to a subset of the ops. TF Lite knows very little
@ -63,10 +67,6 @@ typedef struct {
TfLiteStatus (*Refresh)(struct TfLiteContext* context);
} TfLiteExternalContext;
// Forward declare so GetNode can use this is in Context.
typedef struct _TfLiteRegistration TfLiteRegistration;
typedef struct _TfLiteDelegate TfLiteDelegate;
#define kOptionalTensor (-1)
// Fixed size list of integers. Used for dimensions and inputs/outputs tensor
@ -330,7 +330,7 @@ typedef struct {
// The delegate which knows how to handle `buffer_handle`.
// WARNING: This is an experimental interface that is subject to change.
TfLiteDelegate* delegate;
struct TfLiteDelegate* delegate;
// An integer buffer handle that can be handled by `delegate`.
// The value is valid only when delegate is not null.
@ -405,7 +405,7 @@ typedef struct {
// 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;
struct TfLiteDelegate* delegate;
} TfLiteNode;
typedef struct TfLiteContext {
@ -451,15 +451,15 @@ typedef struct TfLiteContext {
// Get a Tensor node by node_index.
// WARNING: This is an experimental interface that is subject to change.
TfLiteStatus (*GetNodeAndRegistration)(struct TfLiteContext*, int node_index,
TfLiteNode** node,
TfLiteRegistration** registration);
TfLiteStatus (*GetNodeAndRegistration)(
struct TfLiteContext*, int node_index, TfLiteNode** node,
struct TfLiteRegistration** registration);
// Replace ops with one or more stub delegate operations. This function
// does not take ownership of `nodes_to_replace`.
TfLiteStatus (*ReplaceNodeSubsetsWithDelegateKernels)(
struct TfLiteContext*, TfLiteRegistration registration,
const TfLiteIntArray* nodes_to_replace, TfLiteDelegate* delegate);
struct TfLiteContext*, struct TfLiteRegistration registration,
const TfLiteIntArray* nodes_to_replace, struct TfLiteDelegate* delegate);
// Number of threads that are recommended to subsystems like gemmlowp and
// eigen.
@ -484,7 +484,7 @@ typedef struct TfLiteContext {
void* profiler;
} TfLiteContext;
typedef struct _TfLiteRegistration {
typedef struct TfLiteRegistration {
// Initializes the op from serialized data.
// If a built-in op:
// `buffer` is the op's params data (TfLiteLSTMParams*).
@ -560,7 +560,7 @@ typedef enum {
} TfLiteDelegateFlags;
// WARNING: This is an experimental interface that is subject to change.
typedef struct _TfLiteDelegate {
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.
@ -571,20 +571,21 @@ typedef struct _TfLiteDelegate {
// will look at the nodes and call ReplaceNodeSubsetsWithDelegateKernels()
// to ask the TensorFlow lite runtime to create macro-nodes to represent
// delegated subgraphs of the original graph.
TfLiteStatus (*Prepare)(TfLiteContext* context, TfLiteDelegate* delegate);
TfLiteStatus (*Prepare)(TfLiteContext* context,
struct TfLiteDelegate* delegate);
// Copy the data from delegate buffer handle into raw memory of the given
// 'tensor'. This cannot be null. The delegate is allowed to allocate the raw
// bytes as long as it follows the rules for kTfLiteDynamic tensors.
TfLiteStatus (*CopyFromBufferHandle)(TfLiteContext* context,
TfLiteDelegate* delegate,
struct TfLiteDelegate* delegate,
TfLiteBufferHandle buffer_handle,
TfLiteTensor* tensor);
// Copy the data from raw memory of the given 'tensor' to delegate buffer
// handle. This can be null if the delegate doesn't use its own buffer.
TfLiteStatus (*CopyToBufferHandle)(TfLiteContext* context,
TfLiteDelegate* delegate,
struct TfLiteDelegate* delegate,
TfLiteBufferHandle buffer_handle,
TfLiteTensor* tensor);
@ -592,7 +593,8 @@ typedef struct _TfLiteDelegate {
// 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)(TfLiteContext* context, TfLiteDelegate* delegate,
void (*FreeBufferHandle)(TfLiteContext* context,
struct TfLiteDelegate* delegate,
TfLiteBufferHandle* handle);
// Bitmask flags. See the comments in `TfLiteDelegateFlags`.

View File

@ -51,7 +51,11 @@ typedef enum {
kTfLiteMaxExternalContexts = 4
} TfLiteExternalContextType;
// Forward declare so dependent structs and methods can reference these types
// prior to the struct definitions.
struct TfLiteContext;
struct TfLiteDelegate;
struct TfLiteRegistration;
// An external context is a collection of information unrelated to the TF Lite
// framework, but useful to a subset of the ops. TF Lite knows very little
@ -63,10 +67,6 @@ typedef struct {
TfLiteStatus (*Refresh)(struct TfLiteContext* context);
} TfLiteExternalContext;
// Forward declare so GetNode can use this is in Context.
typedef struct _TfLiteRegistration TfLiteRegistration;
typedef struct _TfLiteDelegate TfLiteDelegate;
#define kOptionalTensor (-1)
// Fixed size list of integers. Used for dimensions and inputs/outputs tensor
@ -330,7 +330,7 @@ typedef struct {
// The delegate which knows how to handle `buffer_handle`.
// WARNING: This is an experimental interface that is subject to change.
TfLiteDelegate* delegate;
struct TfLiteDelegate* delegate;
// An integer buffer handle that can be handled by `delegate`.
// The value is valid only when delegate is not null.
@ -405,7 +405,7 @@ typedef struct {
// 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;
struct TfLiteDelegate* delegate;
} TfLiteNode;
typedef struct TfLiteContext {
@ -451,15 +451,15 @@ typedef struct TfLiteContext {
// Get a Tensor node by node_index.
// WARNING: This is an experimental interface that is subject to change.
TfLiteStatus (*GetNodeAndRegistration)(struct TfLiteContext*, int node_index,
TfLiteNode** node,
TfLiteRegistration** registration);
TfLiteStatus (*GetNodeAndRegistration)(
struct TfLiteContext*, int node_index, TfLiteNode** node,
struct TfLiteRegistration** registration);
// Replace ops with one or more stub delegate operations. This function
// does not take ownership of `nodes_to_replace`.
TfLiteStatus (*ReplaceNodeSubsetsWithDelegateKernels)(
struct TfLiteContext*, TfLiteRegistration registration,
const TfLiteIntArray* nodes_to_replace, TfLiteDelegate* delegate);
struct TfLiteContext*, struct TfLiteRegistration registration,
const TfLiteIntArray* nodes_to_replace, struct TfLiteDelegate* delegate);
// Number of threads that are recommended to subsystems like gemmlowp and
// eigen.
@ -484,7 +484,7 @@ typedef struct TfLiteContext {
void* profiler;
} TfLiteContext;
typedef struct _TfLiteRegistration {
typedef struct TfLiteRegistration {
// Initializes the op from serialized data.
// If a built-in op:
// `buffer` is the op's params data (TfLiteLSTMParams*).
@ -560,7 +560,7 @@ typedef enum {
} TfLiteDelegateFlags;
// WARNING: This is an experimental interface that is subject to change.
typedef struct _TfLiteDelegate {
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.
@ -571,20 +571,21 @@ typedef struct _TfLiteDelegate {
// will look at the nodes and call ReplaceNodeSubsetsWithDelegateKernels()
// to ask the TensorFlow lite runtime to create macro-nodes to represent
// delegated subgraphs of the original graph.
TfLiteStatus (*Prepare)(TfLiteContext* context, TfLiteDelegate* delegate);
TfLiteStatus (*Prepare)(TfLiteContext* context,
struct TfLiteDelegate* delegate);
// Copy the data from delegate buffer handle into raw memory of the given
// 'tensor'. This cannot be null. The delegate is allowed to allocate the raw
// bytes as long as it follows the rules for kTfLiteDynamic tensors.
TfLiteStatus (*CopyFromBufferHandle)(TfLiteContext* context,
TfLiteDelegate* delegate,
struct TfLiteDelegate* delegate,
TfLiteBufferHandle buffer_handle,
TfLiteTensor* tensor);
// Copy the data from raw memory of the given 'tensor' to delegate buffer
// handle. This can be null if the delegate doesn't use its own buffer.
TfLiteStatus (*CopyToBufferHandle)(TfLiteContext* context,
TfLiteDelegate* delegate,
struct TfLiteDelegate* delegate,
TfLiteBufferHandle buffer_handle,
TfLiteTensor* tensor);
@ -592,7 +593,8 @@ typedef struct _TfLiteDelegate {
// 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)(TfLiteContext* context, TfLiteDelegate* delegate,
void (*FreeBufferHandle)(TfLiteContext* context,
struct TfLiteDelegate* delegate,
TfLiteBufferHandle* handle);
// Bitmask flags. See the comments in `TfLiteDelegateFlags`.

View File

@ -26,8 +26,7 @@ limitations under the License.
// automatically move <Python.h> before <locale>.
#include <Python.h>
struct _TfLiteDelegate;
typedef struct _TfLiteDelegate TfLiteDelegate;
struct TfLiteDelegate;
// We forward declare TFLite classes here to avoid exposing them to SWIG.
namespace tflite {