profiler.cc 4.6 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 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59
int Profiler::NewTimer(const OpCharacter& ch) {
  StatisUnit unit;
  unit.character = ch;
  if (ch.target == TargetType::kCUDA) {
#ifdef LITE_WITH_CUDA
    unit.timer.reset(new DeviceTimer<TargetType::kCUDA>());
#else
    LOG(ERROR) << "The timer type specified as cuda is uninitialized, so the "
                  "default x86 timer is used instead.";
#endif
  } else {
    unit.timer.reset(new DeviceTimer<TargetType::kHost>());
  }
  units_.push_back(std::move(unit));
  return units_.size() - 1;
}

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

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

60 61 62 63
std::string Profiler::Summary(bool concise, size_t w) {
  using std::setw;
  using std::left;
  using std::fixed;
64
  STL::stringstream ss;
65 66 67 68
  std::string title;
  // Title.
  if (concise) {
    ss << "Timing cycle = " << units_.front().timer->LapTimes().Size()
69
       << std::endl;
70 71 72 73 74 75 76 77 78 79 80 81 82 83
    ss << "===== Concise Profiler Summary: " << name_ << ", Exclude " << w
       << " warm-ups =====" << std::endl;
  } else {
    ss << "===== Detailed Profiler Summary: " << name_ << ", Exclude " << w
       << " 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)"
     << " " << setw(12) << left << "Last (ms)" << std::endl;
  // Profile information.
84 85 86 87 88
  if (concise) {
    std::map<OpCharacter, TimeInfo, decltype(op_comp)> summary(op_comp);
    for (auto& unit : units_) {
      auto ch = summary.find(unit.character);
      if (ch != summary.end()) {
89 90 91
        ch->second.avg += unit.timer->LapTimes().Avg(w);
        ch->second.min += unit.timer->LapTimes().Min(w);
        ch->second.max += unit.timer->LapTimes().Max(w);
92
      } else {
93 94 95
        TimeInfo info({unit.timer->LapTimes().Avg(w),
                       unit.timer->LapTimes().Min(w),
                       unit.timer->LapTimes().Max(w)});
96 97 98 99 100
        summary.insert({unit.character, info});
      }
    }
    for (const auto& item : summary) {
      // clang-format off
101 102 103 104 105 106 107
      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         \
         << " " << std::endl;
108 109 110 111 112
      // clang-format on
    }
  } else {
    for (auto& unit : units_) {
      // clang-format off
113 114 115 116 117 118 119
      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 << unit.timer->LapTimes().Avg(w)  \
         << " " << setw(12) << left << fixed << unit.timer->LapTimes().Min(w)  \
         << " " << setw(12) << left << fixed << unit.timer->LapTimes().Max(w)  \
         << " " << setw(12) << left << fixed << unit.timer->LapTimes().Last(w) \
120 121 122 123 124 125 126 127 128 129
         << std::endl;
      // clang-format on
    }
  }
  return ss.str();
}

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