tvm
profiler.h
Go to the documentation of this file.
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements. See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership. The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License. You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied. See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 #ifndef TVM_META_SCHEDULE_PROFILER_H_
20 #define TVM_META_SCHEDULE_PROFILER_H_
21 
22 #include <tvm/ffi/container/array.h>
23 #include <tvm/ffi/function.h>
24 #include <tvm/ffi/optional.h>
25 #include <tvm/ffi/reflection/registry.h>
26 #include <tvm/ffi/string.h>
27 #include <tvm/ir/module.h>
28 #include <tvm/runtime/object.h>
29 #include <tvm/target/target.h>
30 
31 #include <string>
32 #include <unordered_map>
33 #include <utility>
34 #include <vector>
35 
36 namespace tvm {
37 namespace meta_schedule {
38 
39 class ScopedTimer {
40  public:
42  if (deferred_ != nullptr) {
43  deferred_();
44  }
45  }
46 
47  private:
48  friend class Profiler;
49 
50  explicit ScopedTimer(ffi::TypedFunction<void()> deferred) : deferred_(deferred) {}
51  ffi::TypedFunction<void()> deferred_;
52 };
53 
55 class ProfilerNode : public runtime::Object {
56  public:
58  std::unordered_map<std::string, double> stats_sec;
61 
62  static void RegisterReflection() {
63  // `stats_sec` is not registered
64  // `total_timer` is not registered
65  }
66 
67  static constexpr const char* _type_key = "meta_schedule.Profiler";
69 
70  public:
72  Map<String, FloatImm> Get() const;
74  String Table() const;
75 };
76 
81 class Profiler : public runtime::ObjectRef {
82  public:
85 
89  void ExitWithScope();
91  static Optional<Profiler> Current();
97  static ScopedTimer TimedScope(String name);
98 };
99 
100 } // namespace meta_schedule
101 } // namespace tvm
102 
103 #endif // TVM_META_SCHEDULE_PROFILER_H_
A generic profiler.
Definition: profiler.h:55
static void RegisterReflection()
Definition: profiler.h:62
Map< String, FloatImm > Get() const
Get the internal stats of the running time.
String Table() const
Return a summary of profiling results as table format.
TVM_DECLARE_FINAL_OBJECT_INFO(ProfilerNode, runtime::Object)
std::unordered_map< std::string, double > stats_sec
The segments that are already profiled.
Definition: profiler.h:58
ffi::Function total_timer
Counter for the total time used.
Definition: profiler.h:60
static constexpr const char * _type_key
Definition: profiler.h:67
Managed reference to ProfilerNode.
Definition: profiler.h:81
static ScopedTimer TimedScope(String name)
Profile the time usage in the given scope in the given name.
void EnterWithScope()
Entering the scope of the context manager.
static Optional< Profiler > Current()
Returns the current profiler.
void ExitWithScope()
Exiting the scope of the context manager.
TVM_DEFINE_MUTABLE_NOTNULLABLE_OBJECT_REF_METHODS(Profiler, runtime::ObjectRef, ProfilerNode)
Definition: profiler.h:39
~ScopedTimer()
Definition: profiler.h:41
IRModule that holds the functions and type definitions.
tvm::relax::Function Function
Definition: transform.h:42
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
A managed object in the TVM runtime.
Compilation target object.