1 /* Copyright 2020 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 #include "tensorflow/compiler/xla/python/profiler.h"
17
18 #include "pybind11/pybind11.h"
19 #include "tensorflow/compiler/xla/python/types.h"
20 #include "tensorflow/compiler/xla/status.h"
21 #include "tensorflow/core/profiler/lib/profiler_session.h"
22 #include "tensorflow/core/profiler/rpc/client/capture_profile.h"
23 #include "tensorflow/core/profiler/rpc/profiler_server.h"
24 #include "tensorflow/python/profiler/internal/traceme_wrapper.h"
25
26 namespace xla {
27
28 namespace py = pybind11;
29
30 namespace {
31 // Adds a trivial forwarding class so these Python bindings and TensorFlow's
32 // bindings of the same thing don't register the same class with pybind11.
33 class TraceMeWrapper : public tensorflow::profiler::TraceMeWrapper {
34 public:
35 using tensorflow::profiler::TraceMeWrapper::TraceMeWrapper;
36 };
37
DefaultPythonProfileOptions()38 tensorflow::ProfileOptions DefaultPythonProfileOptions() {
39 tensorflow::ProfileOptions options =
40 tensorflow::ProfilerSession::DefaultOptions();
41 options.set_python_tracer_level(1);
42 options.set_enable_hlo_proto(true);
43 return options;
44 }
45 } // namespace
46
BuildProfilerSubmodule(py::module * m)47 void BuildProfilerSubmodule(py::module* m) {
48 py::module profiler =
49 m->def_submodule("profiler", "TensorFlow profiler integration");
50 py::class_<tensorflow::profiler::ProfilerServer,
51 std::unique_ptr<tensorflow::profiler::ProfilerServer>>
52 profiler_server_class(profiler, "ProfilerServer");
53 profiler.def(
54 "start_server",
55 [](int port) -> std::unique_ptr<tensorflow::profiler::ProfilerServer> {
56 auto server = std::make_unique<tensorflow::profiler::ProfilerServer>();
57 server->StartProfilerServer(port);
58 return server;
59 },
60 py::arg("port"));
61
62 py::class_<tensorflow::ProfilerSession> profiler_session_class(
63 profiler, "ProfilerSession");
64 profiler_session_class
65 .def(py::init([]() {
66 return tensorflow::ProfilerSession::Create(
67 DefaultPythonProfileOptions());
68 }))
69 .def(py::init([](const tensorflow::ProfileOptions& options) {
70 return tensorflow::ProfilerSession::Create(options);
71 }))
72 .def("stop_and_export",
73 [](tensorflow::ProfilerSession* sess,
74 const std::string& tensorboard_dir) -> xla::Status {
75 tensorflow::profiler::XSpace xspace;
76 // Disables the ProfilerSession
77 TF_RETURN_IF_ERROR(sess->CollectData(&xspace));
78 return tensorflow::profiler::ExportToTensorBoard(xspace,
79 tensorboard_dir);
80 });
81
82 py::class_<tensorflow::ProfileOptions> profile_options_class(
83 profiler, "ProfileOptions");
84 profile_options_class.def(py::init(&DefaultPythonProfileOptions))
85 .def_property("include_dataset_ops",
86 &tensorflow::ProfileOptions::include_dataset_ops,
87 &tensorflow::ProfileOptions::set_include_dataset_ops)
88 .def_property("host_tracer_level",
89 &tensorflow::ProfileOptions::host_tracer_level,
90 &tensorflow::ProfileOptions::set_host_tracer_level)
91 .def_property("python_tracer_level",
92 &tensorflow::ProfileOptions::python_tracer_level,
93 &tensorflow::ProfileOptions::set_python_tracer_level)
94 .def_property("enable_hlo_proto",
95 &tensorflow::ProfileOptions::enable_hlo_proto,
96 &tensorflow::ProfileOptions::set_enable_hlo_proto)
97 .def_property("start_timestamp_ns",
98 &tensorflow::ProfileOptions::start_timestamp_ns,
99 &tensorflow::ProfileOptions::set_start_timestamp_ns)
100 .def_property("duration_ms", &tensorflow::ProfileOptions::duration_ms,
101 &tensorflow::ProfileOptions::set_duration_ms)
102 .def_property(
103 "repository_path", &tensorflow::ProfileOptions::repository_path,
104 [](tensorflow::ProfileOptions* options, const std::string& path) {
105 options->set_repository_path(path);
106 });
107
108 py::class_<TraceMeWrapper> traceme_class(profiler, "TraceMe",
109 py::module_local());
110 traceme_class.def(py::init<py::str, py::kwargs>())
111 .def("__enter__", [](py::object self) -> py::object { return self; })
112 .def("__exit__",
113 [](py::object self, const py::object& ex_type,
114 const py::object& ex_value,
115 const py::object& traceback) -> py::object {
116 py::cast<TraceMeWrapper*>(self)->Stop();
117 return py::none();
118 })
119 .def("set_metadata", &TraceMeWrapper::SetMetadata)
120 .def_static("is_enabled", &TraceMeWrapper::IsEnabled);
121 }
122
123 } // namespace xla
124