• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #ifndef TENSORFLOW_PYTHON_PROFILER_INTERNAL_TRACEME_WRAPPER_
16 #define TENSORFLOW_PYTHON_PROFILER_INTERNAL_TRACEME_WRAPPER_
17 
18 #include <string>
19 #include <utility>
20 
21 #include "absl/strings/str_cat.h"
22 #include "absl/strings/string_view.h"
23 #include "pybind11/pytypes.h"
24 #include "tensorflow/core/platform/macros.h"
25 #include "tensorflow/core/platform/types.h"
26 #include "tensorflow/core/profiler/lib/traceme.h"
27 
28 namespace tensorflow {
29 namespace profiler {
30 
31 // Wraps TraceMe with an interface that takes python types.
32 class TraceMeWrapper {
33  public:
34   // pybind11::str and pybind11::kwargs are taken by const reference to avoid
35   // python reference-counting overhead.
TraceMeWrapper(const pybind11::str & name,const pybind11::kwargs & kwargs)36   TraceMeWrapper(const pybind11::str& name, const pybind11::kwargs& kwargs)
37       : traceme_(
38             [&]() {
39               std::string name_and_metadata(name);
40               if (!kwargs.empty()) {
41                 AppendMetadata(&name_and_metadata, kwargs);
42               }
43               return name_and_metadata;
44             },
45             /*level=*/1) {}
46 
47   // pybind11::kwargs is taken by const reference to avoid python
48   // reference-counting overhead.
SetMetadata(const pybind11::kwargs & kwargs)49   void SetMetadata(const pybind11::kwargs& kwargs) {
50     if (TF_PREDICT_FALSE(!kwargs.empty())) {
51       traceme_.AppendMetadata([&]() {
52         std::string metadata;
53         AppendMetadata(&metadata, kwargs);
54         return metadata;
55       });
56     }
57   }
58 
Stop()59   void Stop() { traceme_.Stop(); }
60 
IsEnabled()61   static bool IsEnabled() { return tensorflow::profiler::TraceMe::Active(); }
62 
63  private:
64   // Converts kwargs to strings and appends them to name encoded as TraceMe
65   // metadata.
AppendMetadata(std::string * name,const pybind11::kwargs & kwargs)66   static void AppendMetadata(std::string* name,
67                              const pybind11::kwargs& kwargs) {
68     name->push_back('#');
69     for (const auto& kv : kwargs) {
70       absl::StrAppend(name, std::string(pybind11::str(kv.first)), "=",
71                       std::string(pybind11::str(kv.second)), ",");
72     }
73     name->back() = '#';
74   }
75 
76   tensorflow::profiler::TraceMe traceme_;
77 };
78 
79 }  // namespace profiler
80 }  // namespace tensorflow
81 
82 #endif  // TENSORFLOW_PYTHON_PROFILER_INTERNAL_TRACEME_WRAPPER_
83