c_api_experimental.h 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. /* Copyright 2018 The TensorFlow Authors. All Rights Reserved.
  2. Licensed under the Apache License, Version 2.0 (the "License");
  3. you may not use this file except in compliance with the License.
  4. You may obtain a copy of the License at
  5. http://www.apache.org/licenses/LICENSE-2.0
  6. Unless required by applicable law or agreed to in writing, software
  7. distributed under the License is distributed on an "AS IS" BASIS,
  8. WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  9. See the License for the specific language governing permissions and
  10. limitations under the License.
  11. ==============================================================================*/
  12. #ifndef TENSORFLOW_C_C_API_EXPERIMENTAL_H_
  13. #define TENSORFLOW_C_C_API_EXPERIMENTAL_H_
  14. #include <stddef.h>
  15. #include <stdint.h>
  16. #include "tensorflow/c/c_api.h"
  17. #include "tensorflow/c/eager/c_api.h"
  18. // --------------------------------------------------------------------------
  19. // Experimental C API for TensorFlow.
  20. //
  21. // The API here is subject to changes in the future.
  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(_WIN32)
  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 // _WIN32
  40. #endif // SWIG
  41. #ifdef __cplusplus
  42. extern "C" {
  43. #endif
  44. // When `enable` is true, set
  45. // tensorflow.ConfigProto.OptimizerOptions.global_jit_level to ON_1, and also
  46. // set XLA flag values to prepare for XLA compilation. Otherwise set
  47. // global_jit_level to OFF.
  48. //
  49. // This and the next API are syntax sugar over TF_SetConfig(), and is used by
  50. // clients that cannot read/write the tensorflow.ConfigProto proto.
  51. // TODO: Migrate to TF_CreateConfig() below.
  52. TF_CAPI_EXPORT extern void TF_EnableXLACompilation(TF_SessionOptions* options,
  53. unsigned char enable);
  54. // Set XLA's internal BuildXlaOpsPassFlags.tf_xla_enable_lazy_compilation to the
  55. // value of 'enabled'. Also returns the original value of that flag.
  56. //
  57. // Use in tests to allow XLA to fallback to TF classic. This has global effect.
  58. TF_CAPI_EXPORT unsigned char TF_SetXlaEnableLazyCompilation(
  59. unsigned char enable);
  60. TF_CAPI_EXPORT unsigned char TF_SetTfXlaCpuGlobalJit(unsigned char enable);
  61. // Sets XLA's auto jit mode according to the specified string, which is parsed
  62. // as if passed in XLA_FLAGS. This has global effect.
  63. TF_CAPI_EXPORT void TF_SetXlaAutoJitMode(const char* mode);
  64. // Sets XLA's minimum cluster size. This has global effect.
  65. TF_CAPI_EXPORT void TF_SetXlaMinClusterSize(int size);
  66. // Gets/Sets TF/XLA flag for whether(true) or not(false) to disable constant
  67. // folding. This is for testing to ensure that XLA is being tested rather than
  68. // Tensorflow's CPU implementation through constant folding.
  69. TF_CAPI_EXPORT unsigned char TF_GetXlaConstantFoldingDisabled();
  70. TF_CAPI_EXPORT void TF_SetXlaConstantFoldingDisabled(
  71. unsigned char should_enable);
  72. // Create a serialized tensorflow.ConfigProto proto, where:
  73. //
  74. // a) ConfigProto.optimizer_options.global_jit_level is set to ON_1 if
  75. // `enable_xla_compilation` is non-zero, and OFF otherwise.
  76. // b) ConfigProto.gpu_options.allow_growth is set to `gpu_memory_allow_growth`.
  77. // c) ConfigProto.device_count is set to `num_cpu_devices`.
  78. TF_CAPI_EXPORT extern TF_Buffer* TF_CreateConfig(
  79. unsigned char enable_xla_compilation, unsigned char gpu_memory_allow_growth,
  80. unsigned int num_cpu_devices);
  81. // Create a serialized tensorflow.RunOptions proto, where RunOptions.trace_level
  82. // is set to FULL_TRACE if `enable_full_trace` is non-zero, and NO_TRACE
  83. // otherwise.
  84. TF_CAPI_EXPORT extern TF_Buffer* TF_CreateRunOptions(
  85. unsigned char enable_full_trace);
  86. // Returns the graph content in a human-readable format, with length set in
  87. // `len`. The format is subject to change in the future.
  88. // The returned string is heap-allocated, and caller should call free() on it.
  89. TF_CAPI_EXPORT extern const char* TF_GraphDebugString(TF_Graph* graph,
  90. size_t* len);
  91. // Returns the function content in a human-readable format, with length set in
  92. // `len`. The format is subject to change in the future.
  93. // The returned string is heap-allocated, and caller should call free() on it.
  94. //
  95. // Do not return const char*, because some foreign language binding
  96. // (e.g. swift) cannot then call free() on the returned pointer.
  97. TF_CAPI_EXPORT extern char* TF_FunctionDebugString(TF_Function* func,
  98. size_t* len);
  99. // On success, dequeues a tensor from a TF-managed FifoQueue given by
  100. // `tensor_id`, associated with `session`. There must be a graph node named
  101. // "fifo_queue_dequeue_<tensor_id>", to be executed by this API call.
  102. // Caller must call TF_DeleteTensor() over the returned tensor. If the queue is
  103. // empty, this call is blocked.
  104. //
  105. // Tensors are enqueued via the corresponding TF enqueue op.
  106. // TODO(hongm): Add support for `timeout_ms`.
  107. TF_CAPI_EXPORT extern TF_Tensor* TF_DequeueNamedTensor(TF_Session* session,
  108. int tensor_id,
  109. TF_Status* status);
  110. // On success, enqueues `tensor` into a TF-managed FifoQueue given by
  111. // `tensor_id`, associated with `session`. There must be a graph node named
  112. // "fifo_queue_enqueue_<tensor_id>", to be executed by this API call. It reads
  113. // from a placeholder node "arg_tensor_enqueue_<tensor_id>".
  114. //
  115. // `tensor` is still owned by the caller. This call will be blocked if the queue
  116. // has reached its capacity, and will be unblocked when the queued tensors again
  117. // drop below the capacity due to dequeuing.
  118. //
  119. // Tensors are dequeued via the corresponding TF dequeue op.
  120. // TODO(hongm): Add support for `timeout_ms`.
  121. TF_CAPI_EXPORT extern void TF_EnqueueNamedTensor(TF_Session* session,
  122. int tensor_id,
  123. TF_Tensor* tensor,
  124. TF_Status* status);
  125. // Create a serialized tensorflow.ServerDef proto.
  126. TF_Buffer* TFE_GetServerDef(const char* text_proto, TF_Status* status);
  127. TF_CAPI_EXPORT extern void TF_MakeInternalErrorStatus(TF_Status* status,
  128. const char* errMsg);
  129. // TF_NewCheckpointReader() return the CheckpointReader that can be use to
  130. // investigate or load the variable from the checkpoint file
  131. typedef struct TF_CheckpointReader TF_CheckpointReader;
  132. TF_CAPI_EXPORT extern TF_CheckpointReader* TF_NewCheckpointReader(
  133. const char* filename, TF_Status* status);
  134. TF_CAPI_EXPORT extern void TF_DeleteCheckpointReader(
  135. TF_CheckpointReader* reader);
  136. TF_CAPI_EXPORT extern int TF_CheckpointReaderHasTensor(
  137. TF_CheckpointReader* reader, const char* name);
  138. // Get the variable name at the given index
  139. TF_CAPI_EXPORT extern const char* TF_CheckpointReaderGetVariable(
  140. TF_CheckpointReader* reader, int index);
  141. // Get the number of variable in the checkpoint
  142. TF_CAPI_EXPORT extern int TF_CheckpointReaderSize(TF_CheckpointReader* reader);
  143. // Get the DataType of a variable
  144. TF_CAPI_EXPORT extern TF_DataType TF_CheckpointReaderGetVariableDataType(
  145. TF_CheckpointReader* reader, const char* name);
  146. // Read the shape of a variable and write to `dims`
  147. TF_CAPI_EXPORT extern void TF_CheckpointReaderGetVariableShape(
  148. TF_CheckpointReader* reader, const char* name, int64_t* dims, int num_dims,
  149. TF_Status* status);
  150. // Get the number of dimension of a variable
  151. TF_CAPI_EXPORT extern int TF_CheckpointReaderGetVariableNumDims(
  152. TF_CheckpointReader* reader, const char* name);
  153. // Load the weight of a variable
  154. TF_CAPI_EXPORT extern TF_Tensor* TF_CheckpointReaderGetTensor(
  155. TF_CheckpointReader* reader, const char* name, TF_Status* status);
  156. // TF_NewAttrBuilder() returns an object that you can set attributes on as
  157. // though it were an op. This allows querying properties of that op for
  158. // type-checking purposes like if the op will run on a particular device type.
  159. typedef struct TF_AttrBuilder TF_AttrBuilder;
  160. TF_CAPI_EXPORT extern TF_AttrBuilder* TF_NewAttrBuilder(const char* op_name);
  161. TF_CAPI_EXPORT extern void TF_DeleteAttrBuilder(TF_AttrBuilder* builder);
  162. TF_CAPI_EXPORT extern void TF_AttrBuilderSetType(TF_AttrBuilder* builder,
  163. const char* attr_name,
  164. TF_DataType value);
  165. TF_CAPI_EXPORT extern void TF_AttrBuilderSetTypeList(TF_AttrBuilder* builder,
  166. const char* attr_name,
  167. const TF_DataType* values,
  168. int num_values);
  169. // Checks the tensorflow::NodeDef built via the methods above to see if it can
  170. // run on device_type.
  171. TF_CAPI_EXPORT extern void TF_AttrBuilderCheckCanRunOnDevice(
  172. TF_AttrBuilder* builder, const char* device_type, TF_Status* status);
  173. // For argument number input_index, fetch the corresponding number_attr that
  174. // needs to be updated with the argument length of the input list.
  175. // Returns nullptr if there is any problem like op_name is not found, or the
  176. // argument does not support this attribute type.
  177. TF_CAPI_EXPORT extern const char* TF_GetNumberAttrForOpListInput(
  178. const char* op_name, int input_index, TF_Status* status);
  179. // Returns 1 if the op is stateful, 0 otherwise. The return value is undefined
  180. // if the status is not ok.
  181. TF_CAPI_EXPORT extern int TF_OpIsStateful(const char* op_type,
  182. TF_Status* status);
  183. // Platform specific initialization routine. Very few platforms actually require
  184. // this to be called.
  185. TF_CAPI_EXPORT void TF_InitMain(const char* usage, int* argc, char*** argv);
  186. // Platform-specific implementation to return an unused port. (This should used
  187. // in tests only.)
  188. TF_CAPI_EXPORT int TF_PickUnusedPortOrDie(void);
  189. // Fast path method that makes constructing a single scalar tensor require less
  190. // overhead and copies.
  191. TF_CAPI_EXPORT extern TFE_TensorHandle* TFE_NewTensorHandleFromScalar(
  192. TF_DataType data_type, void* data, size_t len, TF_Status* status);
  193. // Specify the server_def that enables collective ops.
  194. // This is different to the above function in that it doesn't create remote
  195. // contexts, and remotely executing ops is not possible. It just enables
  196. // communication for collective ops.
  197. TF_CAPI_EXPORT extern void TFE_EnableCollectiveOps(TFE_Context* ctx,
  198. const void* proto,
  199. size_t proto_len,
  200. TF_Status* status);
  201. // Aborts all ongoing collectives with the specified status. After abortion,
  202. // subsequent collectives will error with this status immediately. To reset the
  203. // collectives, create a new EagerContext.
  204. //
  205. // This is intended to be used when a peer failure is detected.
  206. TF_CAPI_EXPORT extern void TFE_AbortCollectiveOps(TFE_Context* ctx,
  207. TF_Status* status);
  208. // Checks the health of collective ops peers. Explicit health check is needed in
  209. // multi worker collective ops to detect failures in the cluster. If a peer is
  210. // down, collective ops may hang.
  211. TF_CAPI_EXPORT extern void TFE_CollectiveOpsCheckPeerHealth(
  212. TFE_Context* ctx, const char* task, int64_t timeout_in_ms,
  213. TF_Status* status);
  214. // Information about the shape of a Tensor and its type.
  215. struct TF_ShapeAndType {
  216. // Number of dimensions. -1 indicates unknown rank.
  217. int num_dims;
  218. // Array of dimensions. -1 indicates unknown dim.
  219. int64_t* dims;
  220. // The data type. May be 0 to denote unknown type.
  221. TF_DataType dtype;
  222. };
  223. typedef struct TF_ShapeAndType TF_ShapeAndType;
  224. // A list of TF_ShapeAndType elements..
  225. struct TF_ShapeAndTypeList {
  226. int num_items;
  227. TF_ShapeAndType* items;
  228. };
  229. typedef struct TF_ShapeAndTypeList TF_ShapeAndTypeList;
  230. // API for manipulating TF_ShapeAndTypeList objects.
  231. //
  232. TF_CAPI_EXPORT extern TF_ShapeAndTypeList* TF_NewShapeAndTypeList(
  233. int num_shapes);
  234. TF_CAPI_EXPORT extern void TF_ShapeAndTypeListSetShape(
  235. TF_ShapeAndTypeList* shape_list, int index, const int64_t* dims,
  236. int num_dims);
  237. TF_CAPI_EXPORT extern void TF_ShapeAndTypeListSetUnknownShape(
  238. TF_ShapeAndTypeList* shape_list, int index);
  239. TF_CAPI_EXPORT extern void TF_ShapeAndTypeListSetDtype(
  240. TF_ShapeAndTypeList* shape_list, int index, TF_DataType dtype);
  241. TF_CAPI_EXPORT extern void TF_DeleteShapeAndTypeList(
  242. TF_ShapeAndTypeList* shape_list);
  243. TF_CAPI_EXPORT extern void TF_DeleteShapeAndTypeListArray(
  244. TF_ShapeAndTypeList** shape_list_array, int num_items);
  245. // Infer shapes for the given `op`. The arguments mimic the arguments of the
  246. // `shape_inference::InferenceContext` constructor. Note the following:
  247. // - The inputs of the `op` are not used for shape inference. So, it is
  248. // OK to not have the inputs properly set in `op`. See `input_tensors`
  249. // if you want shape inference to consider the input tensors of the
  250. // op for shape inference.
  251. // - The types need not be set in `input_shapes` as it is not used.
  252. // - The number of `input_tensors` should be the same as the number of items
  253. // in `input_shapes`.
  254. //
  255. // The results are returned in `output_shapes` and
  256. // `output_resource_shapes_and_types`. The caller is responsible for freeing the
  257. // memory in these buffers by calling `TF_DeleteShapeAndTypeList`.
  258. TF_CAPI_EXPORT extern void TFE_InferShapes(
  259. TFE_Op* op, TF_ShapeAndTypeList* input_shapes, TF_Tensor** input_tensors,
  260. TF_ShapeAndTypeList* input_tensor_as_shapes,
  261. TF_ShapeAndTypeList** input_resource_shapes_and_types,
  262. TF_ShapeAndTypeList** output_shapes,
  263. TF_ShapeAndTypeList*** output_resource_shapes_and_types, TF_Status* status);
  264. TF_CAPI_EXPORT extern void
  265. TF_ImportGraphDefOptionsSetValidateColocationConstraints(
  266. TF_ImportGraphDefOptions* opts, unsigned char enable);
  267. // Load the library specified by library_filename and register the pluggable
  268. // device and related kernels present in that library. This function is not
  269. // supported on embedded on mobile and embedded platforms and will fail if
  270. // called.
  271. //
  272. // Pass "library_filename" to a platform-specific mechanism for dynamically
  273. // loading a library. The rules for determining the exact location of the
  274. // library are platform-specific and are not documented here.
  275. //
  276. // On success, returns the newly created library handle and places OK in status.
  277. // The caller owns the library handle.
  278. //
  279. // On failure, returns nullptr and places an error status in status.
  280. TF_CAPI_EXPORT extern TF_Library* TF_LoadPluggableDeviceLibrary(
  281. const char* library_filename, TF_Status* status);
  282. // Frees the memory associated with the library handle.
  283. // Does NOT unload the library.
  284. TF_CAPI_EXPORT extern void TF_DeletePluggableDeviceLibraryHandle(
  285. TF_Library* lib_handle);
  286. #ifdef __cplusplus
  287. } /* end extern "C" */
  288. #endif
  289. #endif // TENSORFLOW_C_C_API_EXPERIMENTAL_H_
粤ICP备19079148号