1 /* Copyright 2018 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 #ifndef TENSORFLOW_C_EAGER_C_API_EXPERIMENTAL_H_
16 #define TENSORFLOW_C_EAGER_C_API_EXPERIMENTAL_H_
17 
18 #include "tensorflow/c/c_api.h"
19 #include "tensorflow/c/eager/c_api.h"
20 
21 #ifdef __cplusplus
22 extern "C" {
23 #endif
24 
25 TF_CAPI_EXPORT extern void TFE_OpConsumeInput(TFE_Op* op, TFE_TensorHandle* h,
26                                               TF_Status* status);
27 
28 typedef struct TFE_ProfilerContext TFE_ProfilerContext;
29 
30 // A profiler which will start profiling when creating the object and will stop
31 // when the object is destroyed. It will profile all operations run under the
32 // given TFE_Context. Multiple instance of it can be created, but at most one
33 // of them will profile for each TFE_Context.
34 // Thread-safety: TFE_Profiler is thread-safe.
35 typedef struct TFE_Profiler TFE_Profiler;
36 
37 TF_CAPI_EXPORT extern TFE_Profiler* TFE_NewProfiler(TFE_ProfilerContext* ctx);
38 TF_CAPI_EXPORT extern bool TFE_ProfilerIsOk(TFE_Profiler* profiler);
39 TF_CAPI_EXPORT extern void TFE_DeleteProfiler(TFE_Profiler* profiler);
40 
41 // The output string is a binary string of tensorflow.tpu.Trace. User can write
42 // the string to file for offline analysis by tensorboard.
43 TF_CAPI_EXPORT extern void TFE_ProfilerSerializeToString(TFE_Context* ctx,
44                                                          TFE_Profiler* profiler,
45                                                          TF_Buffer* buf,
46                                                          TF_Status* status);
47 
48 // Return a new profiler context object.
49 TF_CAPI_EXPORT extern TFE_ProfilerContext* TFE_NewProfilerContext(void);
50 
51 // Set the eager context in TFE_ProfilerServerOptions
52 TF_CAPI_EXPORT extern void TFE_ProfilerContextSetEagerContext(
53     TFE_ProfilerContext* profiler_context, TFE_Context* eager_context);
54 
55 // Destroy a profiler context object.
56 TF_CAPI_EXPORT extern void TFE_DeleteProfilerContext(
57     TFE_ProfilerContext* profiler_context);
58 
59 // Start a profiler grpc server which listens to specified port. It will start
60 // the server on its own thread. It can be shutdown by terminating tensorflow.
61 // It can be used in both Eager mode and graph mode. Creating multiple profiler
62 // server is allowed. The service defined in
63 // tensorflow/contrib/tpu/profiler/tpu_profiler.proto. Please use
64 // tensorflow/contrib/tpu/profiler/capture_tpu_profile to capture tracable
65 // file following
66 // https://cloud.google.com/tpu/docs/cloud-tpu-tools#capture_trace.
67 TF_CAPI_EXPORT extern void TFE_StartProfilerServer(TFE_ProfilerContext* context,
68                                                    int port);
69 
70 // Enables only graph collection in RunMetadata on the functions executed from
71 // this context.
72 TF_CAPI_EXPORT extern void TFE_ContextEnableGraphCollection(TFE_Context* ctx);
73 
74 // Disables only graph collection in RunMetadata on the functions executed from
75 // this context.
76 TF_CAPI_EXPORT extern void TFE_ContextDisableGraphCollection(TFE_Context* ctx);
77 
78 // Send a grpc request to profiler server (service_addr) to perform on-demand
79 // profiling and save the result into logdir which can be visualized by
80 // TensorBoard. worker_list is the list of worker TPUs separated by ','. Set
81 // include_dataset_opts to false to profile longer traces. It will block the
82 // caller thread until receives tracing result.
83 // This API is designed for TensorBoard, for end user, please use
84 // tensorflow/contrib/tpu/profiler/capture_tpu_profile instead following
85 // https://cloud.google.com/tpu/docs/cloud-tpu-tools#capture_trace.
86 TF_CAPI_EXPORT extern bool TFE_ProfilerClientStartTracing(
87     const char* service_addr, const char* logdir, const char* worker_list,
88     bool include_dataset_ops, int duration_ms, int num_tracing_attempts);
89 
90 #ifdef __cplusplus
91 } /* end extern "C" */
92 #endif
93 
94 #endif  // TENSORFLOW_C_EAGER_C_API_EXPERIMENTAL_H_
95