profiler.cc 6.1 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
// Copyright (c) 2019 PaddlePaddle Authors. All Rights Reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include "lite/core/profile/profiler.h"
#include <map>
#include <string>
#include <utility>

namespace paddle {
namespace lite {
namespace profile {

24 25 26 27 28 29 30
namespace {
auto op_comp = [](const OpCharacter& c1, const OpCharacter& c2) {
  return (c1.target < c2.target) || (c1.op_type < c2.op_type) ||
         (c1.kernel_name < c2.kernel_name) || (c1.remark < c2.remark);
};
}

31 32 33 34 35 36 37 38
std::map<Type, std::string> TypeStr{
    {Type::kUnk, "Unknown"},
    {Type::kCreate, "Create"},
    {Type::kDispatch, "Dispatch"},
};

StatisUnit::StatisUnit(const OpCharacter& ch) : character(ch) {
  create_t.reset(new DeviceTimer<TargetType::kHost>());
39 40
  if (ch.target == TargetType::kCUDA) {
#ifdef LITE_WITH_CUDA
41
    dispatch_t.reset(new DeviceTimer<TargetType::kCUDA>());
42 43 44 45 46
#else
    LOG(ERROR) << "The timer type specified as cuda is uninitialized, so the "
                  "default x86 timer is used instead.";
#endif
  } else {
47
    dispatch_t.reset(new DeviceTimer<TargetType::kHost>());
48
  }
49 50 51 52 53 54 55 56 57 58 59 60 61 62
}

lite::profile::Timer* StatisUnit::Timer(Type type) {
  if (type == Type::kCreate) {
    return create_t.get();
  } else if (type == Type::kDispatch) {
    return dispatch_t.get();
  }
  LOG(FATAL) << "Timer cannot be returned for unknown platforms.";
  return nullptr;
}

int Profiler::NewTimer(const OpCharacter& ch) {
  StatisUnit unit(ch);
63 64 65 66
  units_.push_back(std::move(unit));
  return units_.size() - 1;
}

67
void Profiler::StartTiming(Type type, const int index, KernelContext* ctx) {
68 69
  CHECK_LT(index, units_.size())
      << "The timer index in the profiler is out of range.";
70
  units_[index].Timer(type)->Start(ctx);
71 72
}

73
float Profiler::StopTiming(Type type, const int index, KernelContext* ctx) {
74 75
  CHECK_LT(index, units_.size())
      << "The timer index in the profiler is out of range.";
76
  return units_[index].Timer(type)->Stop(ctx);
77 78
}

79
std::string Profiler::Summary(Type type, bool concise, size_t w) {
80 81 82
  using std::setw;
  using std::left;
  using std::fixed;
83
  STL::stringstream ss;
84 85 86
  std::string title;
  // Title.
  if (concise) {
87
    ss << "Timing cycle = " << units_.front().Timer(type)->LapTimes().Size()
88
       << std::endl;
89 90
    ss << "===== Concise " << TypeStr.find(type)->second
       << " Profiler Summary: " << name_ << ", Exclude " << w
91 92
       << " warm-ups =====" << std::endl;
  } else {
93 94
    ss << "===== Detailed " << TypeStr.find(type)->second
       << " Profiler Summary: " << name_ << ", Exclude " << w
95 96 97 98 99 100 101 102
       << " warm-ups =====" << std::endl;
  }
  ss << setw(25) << left << "Operator Type"
     << " " << setw(40) << left << "Kernel Name"
     << " " << setw(12) << left << "Remark"
     << " " << setw(12) << left << "Avg (ms)"
     << " " << setw(12) << left << "Min (ms)"
     << " " << setw(12) << left << "Max (ms)"
103 104
     << " " << setw(12) << left << "Last (ms)"
     << " " << setw(12) << left << "Percent (%)" << std::endl;
105
  // Profile information.
106 107 108
  if (concise) {
    std::map<OpCharacter, TimeInfo, decltype(op_comp)> summary(op_comp);
    for (auto& unit : units_) {
109
      auto ch = summary.find(unit.Character());
110
      if (ch != summary.end()) {
111 112 113
        ch->second.avg += unit.Timer(type)->LapTimes().Avg(w);
        ch->second.min += unit.Timer(type)->LapTimes().Min(w);
        ch->second.max += unit.Timer(type)->LapTimes().Max(w);
114
      } else {
115 116 117 118
        TimeInfo info({unit.Timer(type)->LapTimes().Avg(w),
                       unit.Timer(type)->LapTimes().Min(w),
                       unit.Timer(type)->LapTimes().Max(w)});
        summary.insert({unit.Character(), info});
119 120
      }
    }
121 122
    // compute total time
    float total = 0.0;
123
    for (const auto& item : summary) {
124 125 126 127 128 129 130
      total += item.second.avg;
    }
    for (const auto& item : summary) {
      float percent = 0;
      if (total > 0) {
        percent = 100 * (item.second.avg / total);
      }
131
      // clang-format off
132 133 134 135 136 137
      ss << setw(25) << left << fixed << item.first.op_type             \
         << " " << setw(40) << left << fixed << item.first.kernel_name  \
         << " " << setw(12) << left << fixed << item.first.remark       \
         << " " << setw(12) << left << fixed << item.second.avg         \
         << " " << setw(12) << left << fixed << item.second.min         \
         << " " << setw(12) << left << fixed << item.second.max         \
138
         << " " << setw(12) << left << fixed << percent << "%"          \
139
         << " " << std::endl;
140 141 142
      // clang-format on
    }
  } else {
143
    float total = 0.0;
144
    for (auto& unit : units_) {
145
      const auto& times = unit.Timer(type)->LapTimes();
146 147 148 149 150 151 152 153 154
      total += times.Avg(w);
    }
    for (auto& unit : units_) {
      const auto& times = unit.Timer(type)->LapTimes();
      float run = times.Avg(w);
      float percent = 0;
      if (total > 0) {
        percent = 100 * (run / total);
      }
155
      // clang-format off
156 157 158 159 160 161 162
      ss << setw(25) << left << fixed << unit.Character().op_type            \
         << " " << setw(40) << left << fixed << unit.Character().kernel_name \
         << " " << setw(12) << left << fixed << unit.Character().remark      \
         << " " << setw(12) << left << fixed << times.Avg(w)                 \
         << " " << setw(12) << left << fixed << times.Min(w)                 \
         << " " << setw(12) << left << fixed << times.Max(w)                 \
         << " " << setw(12) << left << fixed << times.Last(w)                \
163
          << " " << setw(12) << left << fixed << percent << "%"              \
164 165 166 167 168 169 170 171 172 173
         << std::endl;
      // clang-format on
    }
  }
  return ss.str();
}

}  // namespace profile
}  // namespace lite
}  // namespace paddle