|
| 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 | +#include "./utils.h" |
| 20 | + |
| 21 | +namespace tvm { |
| 22 | +namespace meta_schedule { |
| 23 | + |
| 24 | +/**************** Context Manager ****************/ |
| 25 | + |
| 26 | +class ProfilerInternal { |
| 27 | + public: |
| 28 | + static void EnterScope(Profiler ctx) { ctx.EnterWithScope(); } |
| 29 | + static void ExitScope(Profiler ctx) { ctx.ExitWithScope(); } |
| 30 | +}; |
| 31 | + |
| 32 | +void Profiler::EnterWithScope() { |
| 33 | + Optional<Profiler>& ctx = ProfilerThreadLocalStore::Get()->ctx; |
| 34 | + CHECK(!ctx.defined()) << "ValueError: Nested Profiler context managers are not allowed"; |
| 35 | + ctx = *this; |
| 36 | +} |
| 37 | + |
| 38 | +void Profiler::ExitWithScope() { |
| 39 | + Optional<Profiler>& ctx = ProfilerThreadLocalStore::Get()->ctx; |
| 40 | + ICHECK(ctx.defined()); |
| 41 | + ctx = NullOpt; |
| 42 | +} |
| 43 | + |
| 44 | +/**************** Profiler ****************/ |
| 45 | + |
| 46 | +Profiler::Profiler() { |
| 47 | + ObjectPtr<ProfilerNode> n = make_object<ProfilerNode>(); |
| 48 | + data_ = n; |
| 49 | +} |
| 50 | + |
| 51 | +ScopedTimer ProfilerNode::TimeScope(String name) { |
| 52 | + return ScopedTimer([name, tick = std::chrono::high_resolution_clock::now()]() -> void { |
| 53 | + Optional<Profiler> profiler = ProfilerThreadLocalStore::Get()->ctx; |
| 54 | + if (profiler.defined()) { |
| 55 | + Map<String, FloatImm>& stats = profiler.value()->stats; |
| 56 | + double duration = std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 57 | + std::chrono::high_resolution_clock::now() - tick) |
| 58 | + .count() / |
| 59 | + 1e9 / 60; |
| 60 | + if (stats.find(name) != stats.end()) { |
| 61 | + stats.Set(name, FloatImm(DataType::Float(64), stats.at(name)->value + duration)); |
| 62 | + } else { |
| 63 | + stats.Set(name, FloatImm(DataType::Float(64), duration)); |
| 64 | + } |
| 65 | + } |
| 66 | + }); |
| 67 | +} |
| 68 | + |
| 69 | +void ProfilerNode::StartContextTimer(String name) { |
| 70 | + stack.push_back(std::make_pair(name, std::chrono::high_resolution_clock::now())); |
| 71 | +} |
| 72 | + |
| 73 | +void ProfilerNode::EndContextTimer() { |
| 74 | + ICHECK(stack.size() > 0) << "There is no timer context running!"; |
| 75 | + String name = stack.back().first; |
| 76 | + double duration = std::chrono::duration_cast<std::chrono::nanoseconds>( |
| 77 | + std::chrono::high_resolution_clock::now() - stack.back().second) |
| 78 | + .count() / |
| 79 | + 1e9 / 60; |
| 80 | + if (stats.find(name) != stats.end()) { |
| 81 | + stats.Set(name, FloatImm(DataType::Float(64), stats.at(name)->value + duration)); |
| 82 | + } else { |
| 83 | + stats.Set(name, FloatImm(DataType::Float(64), duration)); |
| 84 | + } |
| 85 | + stack.pop_back(); |
| 86 | +} |
| 87 | + |
| 88 | +TVM_REGISTER_NODE_TYPE(ProfilerNode); |
| 89 | +TVM_REGISTER_GLOBAL("meta_schedule.Profiler").set_body_typed([]() -> Profiler { |
| 90 | + return Profiler(); |
| 91 | +}); |
| 92 | +TVM_REGISTER_GLOBAL("meta_schedule.ProfilerEnterScope") |
| 93 | + .set_body_typed(ProfilerInternal::EnterScope); |
| 94 | +TVM_REGISTER_GLOBAL("meta_schedule.ProfilerExitScope").set_body_typed(ProfilerInternal::ExitScope); |
| 95 | +TVM_REGISTER_GLOBAL("meta_schedule.ProfilerStartContextTimer") |
| 96 | + .set_body_method<Profiler>(&ProfilerNode::StartContextTimer); |
| 97 | +TVM_REGISTER_GLOBAL("meta_schedule.ProfilerEndContextTimer") |
| 98 | + .set_body_method<Profiler>(&ProfilerNode::EndContextTimer); |
| 99 | +TVM_REGISTER_GLOBAL("meta_schedule.ProfilerGet").set_body_method<Profiler>(&ProfilerNode::Get); |
| 100 | + |
| 101 | +} // namespace meta_schedule |
| 102 | +} // namespace tvm |
0 commit comments