|
| 1 | +/* Copyright 2017 The TensorFlow Authors. All Rights Reserved. |
| 2 | +
|
| 3 | +Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +you may not use this file except in compliance with the License. |
| 5 | +You may obtain a copy of the License at |
| 6 | +
|
| 7 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +
|
| 9 | +Unless required by applicable law or agreed to in writing, software |
| 10 | +distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +See the License for the specific language governing permissions and |
| 13 | +limitations under the License. |
| 14 | +==============================================================================*/ |
| 15 | + |
| 16 | +#ifndef TENSORFLOW_C_EAGER_C_API_H_ |
| 17 | +#define TENSORFLOW_C_EAGER_C_API_H_ |
| 18 | + |
| 19 | +// C API extensions to experiment with eager execution of kernels. |
| 20 | + |
| 21 | +#include "c_api.h" |
| 22 | + |
| 23 | +// Macro to control visibility of exported symbols in the shared library (.so, |
| 24 | +// .dylib, .dll). |
| 25 | +// This duplicates the TF_EXPORT macro definition in |
| 26 | +// tensorflow/core/platform/macros.h in order to keep this .h file independent |
| 27 | +// of any other includes.$a |
| 28 | +#ifdef SWIG |
| 29 | +#define TF_CAPI_EXPORT |
| 30 | +#else |
| 31 | +#if defined(COMPILER_MSVC) |
| 32 | +#ifdef TF_COMPILE_LIBRARY |
| 33 | +#define TF_CAPI_EXPORT __declspec(dllexport) |
| 34 | +#else |
| 35 | +#define TF_CAPI_EXPORT __declspec(dllimport) |
| 36 | +#endif // TF_COMPILE_LIBRARY |
| 37 | +#else |
| 38 | +#define TF_CAPI_EXPORT __attribute__((visibility("default"))) |
| 39 | +#endif // COMPILER_MSVC |
| 40 | +#endif // SWIG |
| 41 | + |
| 42 | +#ifdef __cplusplus |
| 43 | +extern "C" { |
| 44 | +#endif |
| 45 | + |
| 46 | +typedef struct TFE_ContextOptions TFE_ContextOptions; |
| 47 | + |
| 48 | +// Return a new options object. |
| 49 | +TF_CAPI_EXPORT extern TFE_ContextOptions* TFE_NewContextOptions(); |
| 50 | + |
| 51 | +// Set the config in TF_ContextOptions.options. |
| 52 | +// config should be a serialized tensorflow.ConfigProto proto. |
| 53 | +// If config was not parsed successfully as a ConfigProto, record the |
| 54 | +// error information in *status. |
| 55 | +TF_CAPI_EXPORT extern void TFE_ContextOptionsSetConfig( |
| 56 | + TFE_ContextOptions* options, const void* proto, size_t proto_len, |
| 57 | + TF_Status* status); |
| 58 | + |
| 59 | +// Controls how to act when we try to run an operation on a given device but |
| 60 | +// some input tensors are not on that device. |
| 61 | +typedef enum TFE_ContextDevicePlacementPolicy { |
| 62 | + // The default: running operations with input tensors on the wrong device will |
| 63 | + // fail. |
| 64 | + TFE_DEVICE_PLACEMENT_EXPLICIT = 0, |
| 65 | + // Copy the tensor to the right device but log a warning. |
| 66 | + TFE_DEVICE_PLACEMENT_WARN = 1, |
| 67 | + // Silently copy the tensor, which has a performance cost since the |
| 68 | + // operation will be blocked till the copy completes. |
| 69 | + TFE_DEVICE_PLACEMENT_SILENT = 2, |
| 70 | +} TFE_ContextDevicePlacementPolicy; |
| 71 | + |
| 72 | +TF_CAPI_EXPORT extern void TFE_ContextOptionsSetDevicePlacementPolicy( |
| 73 | + TFE_ContextOptions*, TFE_ContextDevicePlacementPolicy); |
| 74 | + |
| 75 | +// Destroy an options object. |
| 76 | +TF_CAPI_EXPORT extern void TFE_DeleteContextOptions(TFE_ContextOptions*); |
| 77 | + |
| 78 | +// "Context" under which operations/functions are executed. It encapsulates |
| 79 | +// things like the available devices, resource manager etc. |
| 80 | +// |
| 81 | +// TODO(ashankar): Merge with TF_Session? |
| 82 | +typedef struct TFE_Context TFE_Context; |
| 83 | + |
| 84 | +TF_CAPI_EXPORT extern TFE_Context* TFE_NewContext( |
| 85 | + const TFE_ContextOptions* opts, TF_Status* status); |
| 86 | +TF_CAPI_EXPORT extern void TFE_DeleteContext(TFE_Context* ctx, TF_Status* status); |
| 87 | +TF_CAPI_EXPORT extern TF_DeviceList* TFE_ContextListDevices(TFE_Context* ctx, |
| 88 | + TF_Status* status); |
| 89 | + |
| 90 | +// A handle to a tensor on a device. |
| 91 | +// |
| 92 | +// Like a TF_Tensor, a TFE_TensorHandle refers to a tensor with a value, shape, |
| 93 | +// type etc. Unlike a TF_Tensor, a TFE_TensorHandle may refer to such tensors |
| 94 | +// placed in memory of different devices or remote address spaces. |
| 95 | +typedef struct TFE_TensorHandle TFE_TensorHandle; |
| 96 | + |
| 97 | +TF_CAPI_EXPORT extern TFE_TensorHandle* TFE_NewTensorHandle(TF_Tensor* t, |
| 98 | + TF_Status* status); |
| 99 | +TF_CAPI_EXPORT extern void TFE_DeleteTensorHandle(TFE_TensorHandle* h); |
| 100 | +TF_CAPI_EXPORT extern TF_DataType TFE_TensorHandleDataType(TFE_TensorHandle* h); |
| 101 | +TF_CAPI_EXPORT extern int TFE_TensorHandleNumDims(TFE_TensorHandle* h); |
| 102 | +TF_CAPI_EXPORT extern int64_t TFE_TensorHandleDim(TFE_TensorHandle* h, int dim_index); |
| 103 | +TF_CAPI_EXPORT extern const char* TFE_TensorHandleDeviceName(TFE_TensorHandle* h); |
| 104 | +TF_CAPI_EXPORT extern TF_Tensor* TFE_TensorHandleResolve(TFE_TensorHandle* h, |
| 105 | + TF_Status* status); |
| 106 | + |
| 107 | +// Create a new TFE_TensorHandle with the same contents as 'h' but placed |
| 108 | +// in the memory of the device name 'device_name'. |
| 109 | +// If source and destination are the same device, then this creates a new handle |
| 110 | +// that shares the underlying buffer. Otherwise, it currently requires at least |
| 111 | +// one of the source or destination devices to be CPU (i.e., for the source or |
| 112 | +// destination tensor to be placed in host memory). |
| 113 | +TF_CAPI_EXPORT extern TFE_TensorHandle* TFE_TensorHandleCopyToDevice(TFE_TensorHandle* h, |
| 114 | + TFE_Context* ctx, |
| 115 | + const char* device_name, |
| 116 | + TF_Status* status); |
| 117 | + |
| 118 | +// Description of the TensorFlow op to execute. |
| 119 | +// |
| 120 | +// Assumes that the provided 'ctx' outlives the returned TFE_Op, i.e., |
| 121 | +// TFE_DeleteOp() is called before TFE_DeleteContext(). |
| 122 | +// |
| 123 | +// Very similar to TF_OperationDescription with some differences: |
| 124 | +// (1) TF_Output or TFE_TensorHandle* as arguments to TF_AddInput, |
| 125 | +// TF_AddInputList |
| 126 | +// (2) TF_ColocateWith, TF_AddControlInput etc. do not make sense. |
| 127 | +// (3) Implementation detail: Avoid use of NodeBuilder/NodeDefBuilder since |
| 128 | +// the additional sanity checks there seem unnecessary; |
| 129 | +typedef struct TFE_Op TFE_Op; |
| 130 | + |
| 131 | +TF_CAPI_EXPORT extern TFE_Op* TFE_NewOp(TFE_Context* ctx, const char* op_or_function_name, |
| 132 | + TF_Status* status); |
| 133 | +TF_CAPI_EXPORT extern void TFE_DeleteOp(TFE_Op* op); |
| 134 | + |
| 135 | +TF_CAPI_EXPORT extern void TFE_OpSetDevice(TFE_Op* op, const char* device_name, |
| 136 | + TF_Status* status); |
| 137 | + |
| 138 | +TF_CAPI_EXPORT extern void TFE_OpAddInput(TFE_Op* op, TFE_TensorHandle* h, TF_Status* status); |
| 139 | + |
| 140 | +TF_CAPI_EXPORT extern TF_AttrType TFE_OpGetAttrType(TFE_Op* op, const char* attr_name, |
| 141 | + unsigned char* is_list, TF_Status* status); |
| 142 | +// Get an attribute type given an op name; a fusion of TFE_NewOp and |
| 143 | +// TFE_OpGetAttrType for use from Python without the overhead of the individual |
| 144 | +// calls and memory management of TFE_Op. |
| 145 | +TF_CAPI_EXPORT extern TF_AttrType TFE_OpNameGetAttrType( |
| 146 | + TFE_Context* ctx, const char* op_or_function_name, const char* attr_name, |
| 147 | + unsigned char* is_list, TF_Status* status); |
| 148 | + |
| 149 | +TF_CAPI_EXPORT extern void TFE_OpSetAttrString(TFE_Op* op, const char* attr_name, |
| 150 | + const char* value); |
| 151 | +TF_CAPI_EXPORT extern void TFE_OpSetAttrInt(TFE_Op* op, const char* attr_name, int64_t value); |
| 152 | +TF_CAPI_EXPORT extern void TFE_OpSetAttrFloat(TFE_Op* op, const char* attr_name, float value); |
| 153 | +TF_CAPI_EXPORT extern void TFE_OpSetAttrBool(TFE_Op* op, const char* attr_name, |
| 154 | + unsigned char value); |
| 155 | +TF_CAPI_EXPORT extern void TFE_OpSetAttrType(TFE_Op* op, const char* attr_name, |
| 156 | + TF_DataType value); |
| 157 | +// If the number of dimensions is unknown, `num_dims` must be set to |
| 158 | +// -1 and `dims` can be null. If a dimension is unknown, the |
| 159 | +// corresponding entry in the `dims` array must be -1. |
| 160 | +TF_CAPI_EXPORT extern void TFE_OpSetAttrShape(TFE_Op* op, const char* attr_name, |
| 161 | + const int64_t* dims, const int num_dims, |
| 162 | + TF_Status* out_status); |
| 163 | + |
| 164 | +// Sets the attribute attr_name to be a function specified by 'function'. |
| 165 | +// |
| 166 | +// TODO(ashankar,iga): Add this functionality to the C API for graph |
| 167 | +// construction. Perhaps we want an AttrValueMap equivalent in the C API? |
| 168 | +TF_CAPI_EXPORT extern void TFE_OpSetAttrFunction(TFE_Op* op, |
| 169 | + const char* attr_name, |
| 170 | + const TFE_Op* value); |
| 171 | + |
| 172 | +TF_CAPI_EXPORT extern void TFE_OpSetAttrStringList(TFE_Op* op, const char* attr_name, |
| 173 | + const char** value, int num_values); |
| 174 | +TF_CAPI_EXPORT extern void TFE_OpSetAttrIntList(TFE_Op* op, const char* attr_name, |
| 175 | + const int64_t* values, int num_values); |
| 176 | +TF_CAPI_EXPORT extern void TFE_OpSetAttrFloatList(TFE_Op* op, const char* attr_name, |
| 177 | + const float* values, int num_values); |
| 178 | +TF_CAPI_EXPORT extern void TFE_OpSetAttrBoolList(TFE_Op* op, const char* attr_name, |
| 179 | + const unsigned char* values, int num_values); |
| 180 | +TF_CAPI_EXPORT extern void TFE_OpSetAttrTypeList(TFE_Op* op, const char* attr_name, |
| 181 | + const TF_DataType* values, int num_values); |
| 182 | +TF_CAPI_EXPORT extern void TFE_OpSetAttrShapeList(TFE_Op* op, const char* attr_name, |
| 183 | + const int64_t** dims, const int* num_dims, |
| 184 | + int num_values, TF_Status* out_status); |
| 185 | + |
| 186 | +// Execute the operation defined by 'op' and return handles to computed |
| 187 | +// tensors in 'retvals'. |
| 188 | +// |
| 189 | +// 'retvals' must point to a pre-allocated array of TFE_TensorHandle* |
| 190 | +// and '*num_retvals' should be set to the size of this array. |
| 191 | +// |
| 192 | +// On return, 'num_retvals' will be set to the actual number of outputs |
| 193 | +// returned by the operation. |
| 194 | +TF_CAPI_EXPORT extern void TFE_Execute(TFE_Op* op, TFE_TensorHandle** retvals, |
| 195 | + int* num_retvals, TF_Status* status); |
| 196 | + |
| 197 | +// Add a function (serialized FunctionDef protocol buffer) to ctx so |
| 198 | +// that it can be invoked using TFE_Execute. |
| 199 | +TF_CAPI_EXPORT extern void TFE_ContextAddFunctionDef(TFE_Context* ctx, |
| 200 | + const char* serialized_function_def, |
| 201 | + size_t size, TF_Status* status); |
| 202 | + |
| 203 | +#ifdef __cplusplus |
| 204 | +} /* end extern "C" */ |
| 205 | +#endif |
| 206 | + |
| 207 | +#ifdef __cplusplus |
| 208 | +// A workaround to ease conversion to and from numpy objects and |
| 209 | +// TFE_TensorHandle's. |
| 210 | +// |
| 211 | +// TODO(ashankar): Figure out an alternative scheme that precludes the need for |
| 212 | +// these API-boundary breaking methods. |
| 213 | +namespace tensorflow { |
| 214 | +class Tensor; |
| 215 | +} // namespace tensorflow |
| 216 | + |
| 217 | +const tensorflow::Tensor* TFE_TensorHandleUnderlyingTensorInHostMemory( |
| 218 | + TFE_TensorHandle* h, TF_Status* status); |
| 219 | +TFE_TensorHandle* TFE_NewTensorHandle(const tensorflow::Tensor& t); |
| 220 | +#endif |
| 221 | + |
| 222 | +#endif // TENSORFLOW_C_EAGER_C_API_H_ |
0 commit comments