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

View File

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

View File

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