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_S_TIR_META_SCHEDULE_PROFILER_H_
20 #define TVM_S_TIR_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 s_tir {
38 namespace meta_schedule {
39 
40 class ScopedTimer {
41  public:
43  if (deferred_ != nullptr) {
44  deferred_();
45  }
46  }
47 
48  private:
49  friend class Profiler;
50 
51  explicit ScopedTimer(ffi::TypedFunction<void()> deferred) : deferred_(deferred) {}
52  ffi::TypedFunction<void()> deferred_;
53 };
54 
56 class ProfilerNode : public runtime::Object {
57  public:
59  std::unordered_map<std::string, double> stats_sec;
62 
63  static void RegisterReflection() {
64  namespace refl = tvm::ffi::reflection;
65  refl::ObjectDef<ProfilerNode>();
66  }
67 
68  static constexpr const bool _type_mutable = true;
69  TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.Profiler", ProfilerNode, runtime::Object);
70 
71  public:
73  ffi::Map<ffi::String, FloatImm> Get() const;
75  ffi::String Table() const;
76 };
77 
82 class Profiler : public runtime::ObjectRef {
83  public:
86 
90  void ExitWithScope();
92  static ffi::Optional<Profiler> Current();
98  static ScopedTimer TimedScope(ffi::String name);
99 };
100 
101 } // namespace meta_schedule
102 } // namespace s_tir
103 } // namespace tvm
104 
105 #endif // TVM_S_TIR_META_SCHEDULE_PROFILER_H_
A generic profiler.
Definition: profiler.h:56
std::unordered_map< std::string, double > stats_sec
The segments that are already profiled.
Definition: profiler.h:59
ffi::String Table() const
Return a summary of profiling results as table format.
ffi::Function total_timer
Counter for the total time used.
Definition: profiler.h:61
ffi::Map< ffi::String, FloatImm > Get() const
Get the internal stats of the running time.
TVM_FFI_DECLARE_OBJECT_INFO_FINAL("s_tir.meta_schedule.Profiler", ProfilerNode, runtime::Object)
static constexpr const bool _type_mutable
Definition: profiler.h:68
static void RegisterReflection()
Definition: profiler.h:63
Managed reference to ProfilerNode.
Definition: profiler.h:82
void EnterWithScope()
Entering the scope of the context manager.
void ExitWithScope()
Exiting the scope of the context manager.
static ffi::Optional< Profiler > Current()
Returns the current profiler.
TVM_FFI_DEFINE_OBJECT_REF_METHODS_NOTNULLABLE(Profiler, runtime::ObjectRef, ProfilerNode)
static ScopedTimer TimedScope(ffi::String name)
Profile the time usage in the given scope in the given name.
~ScopedTimer()
Definition: profiler.h:42
IRModule that holds the functions and type definitions.
Definition: repr_printer.h:91
tvm::relax::Function Function
Definition: transform.h:38
Performance counters for profiling via the PAPI library.
Definition: analyzer.h:37
A managed object in the TVM runtime.
Compilation target object.