profiler.cc 9.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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"
16
#include <iomanip>
17 18 19 20 21 22 23 24
#include <map>
#include <string>
#include <utility>

namespace paddle {
namespace lite {
namespace profile {

25 26 27 28 29 30 31
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);
};
}

32 33 34 35 36 37 38 39
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>());
40 41
  if (ch.target == TargetType::kCUDA) {
#ifdef LITE_WITH_CUDA
42
    dispatch_t.reset(new DeviceTimer<TargetType::kCUDA>());
43 44 45 46 47
#else
    LOG(ERROR) << "The timer type specified as cuda is uninitialized, so the "
                  "default x86 timer is used instead.";
#endif
  } else {
48
    dispatch_t.reset(new DeviceTimer<TargetType::kHost>());
49
  }
50 51 52 53 54 55 56 57 58 59 60 61 62 63
}

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);
64 65 66 67
  units_.push_back(std::move(unit));
  return units_.size() - 1;
}

68 69 70 71 72 73
OpCharacter* Profiler::GetOpCharacter(const size_t index) {
  CHECK_LT(index, units_.size())
      << "The timer index in the profiler is out of range.";
  return &units_[index].Character();
}

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

80
void Profiler::StopTiming(Type type, const int index, KernelContext* ctx) {
81 82
  CHECK_LT(index, units_.size())
      << "The timer index in the profiler is out of range.";
83 84 85 86 87 88
  units_[index].Timer(type)->Stop(ctx);
#ifdef LITE_WITH_OPENCL
  units_[index].Timer(type)->CLStop(units_[index].character.op_type,
                                    units_[index].character.io_duration,
                                    units_[index].character.cl_event);
#endif
89 90
}

91
std::string Profiler::Summary(Type type, bool concise, size_t w) {
92 93 94
  using std::setw;
  using std::left;
  using std::fixed;
95
  using std::setprecision;
96
  STL::stringstream ss;
97 98 99
  std::string title;
  // Title.
  if (concise) {
100
    ss << "Timing cycle = " << units_.front().Timer(type)->LapTimes().Size()
101
       << std::endl;
102 103
    ss << "===== Concise " << TypeStr.find(type)->second
       << " Profiler Summary: " << name_ << ", Exclude " << w
104 105
       << " warm-ups =====" << std::endl;
  } else {
106 107
    ss << "===== Detailed " << TypeStr.find(type)->second
       << " Profiler Summary: " << name_ << ", Exclude " << w
108 109
       << " warm-ups =====" << std::endl;
  }
110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139
  ss << setw(20) << left << "OperatorType"
     << " " << setw(30) << left << "KerneAttr";
  if (!concise) {
    ss << " " << setw(24) << left << "KernelName";
  }
  ss << " " << setw(16) << left << "Remark";
  if (!concise) {
    ss << " " << setw(15) << left << "InDim"
       << " " << setw(15) << left << "FilterDim"
       << " " << setw(15) << left << "OutDim";
  }
  ss << " " << setw(7) << left << "Avg(ms)"
     << " " << setw(7) << left << "Min(ms)"
     << " " << setw(7) << left << "Max(ms)";
  if (!concise) {
    ss << " " << setw(7) << left << "Last(ms)";
  }
  ss << " " << setw(7) << left << "Avg(%)";
  if (!concise) {
    ss << " " << setw(7) << left << "GOPs"
       << " " << setw(7) << left << "GOPS";
  }
#ifdef LITE_WITH_OPENCL
  ss << " " << setw(9) << left << "clAvg(ms)"
     << " " << setw(9) << left << "clMin(ms)"
     << " " << setw(9) << left << "clMax(ms)"
     << " " << setw(9) << left << "clAvg(%)";
#endif
  ss << std::endl;

140
  // Profile information.
141 142 143
  if (concise) {
    std::map<OpCharacter, TimeInfo, decltype(op_comp)> summary(op_comp);
    for (auto& unit : units_) {
144
      auto ch = summary.find(unit.Character());
145
      if (ch != summary.end()) {
146 147 148
        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);
149 150 151 152 153
#ifdef LITE_WITH_OPENCL
        ch->second.cl_avg += unit.Timer(type)->CLLapTimes().Avg(w);
        ch->second.cl_min += unit.Timer(type)->CLLapTimes().Min(w);
        ch->second.cl_max += unit.Timer(type)->CLLapTimes().Max(w);
#endif
154
      } else {
155 156 157 158
        TimeInfo info;
        info.avg = unit.Timer(type)->LapTimes().Avg(w);
        info.min = unit.Timer(type)->LapTimes().Min(w);
        info.max = unit.Timer(type)->LapTimes().Max(w);
159 160 161 162 163
#ifdef LITE_WITH_OPENCL
        info.cl_avg = unit.Timer(type)->CLLapTimes().Avg(w);
        info.cl_min = unit.Timer(type)->CLLapTimes().Min(w);
        info.cl_max = unit.Timer(type)->CLLapTimes().Max(w);
#endif
164
        summary.insert({unit.Character(), info});
165 166
      }
    }
167

168 169
    // compute total time
    float total = 0.0;
170
    for (const auto& item : summary) {
171 172
      total += item.second.avg;
    }
173 174 175 176 177 178 179
#ifdef LITE_WITH_OPENCL
    float cl_total = 0.0;
    for (const auto& item : summary) {
      cl_total += item.second.cl_avg;
    }
#endif

180 181 182 183 184
    for (const auto& item : summary) {
      float percent = 0;
      if (total > 0) {
        percent = 100 * (item.second.avg / total);
      }
185
      // clang-format off
186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209
      ss << setw(20) << left << fixed << item.first.op_type
         << " " << setw(30) << left << fixed << item.first.kernel_attr
         << " " << setw(16) << left << fixed << item.first.remark
         << " " << setw(7) << left << fixed << setprecision(3)
         << item.second.avg
         << " " << setw(7) << left << fixed << setprecision(3)
         << item.second.min
         << " " << setw(7) << left << fixed << setprecision(3)
          << item.second.max
         << " " <<  setprecision(2) << percent << "%   ";
#ifdef LITE_WITH_OPENCL
      float cl_percent = 0;
      if (cl_total > 0) {
        cl_percent = 100 * (item.second.cl_avg / cl_total);
      }
      ss << " " << setw(9) << left << fixed << setprecision(3)
         << item.second.cl_avg
         << " " << setw(9) << left << fixed << setprecision(3)
         << item.second.cl_min
         << " " << setw(9) << left << fixed << setprecision(3)
         << item.second.cl_max
         << " " << left << fixed <<setprecision(2) << cl_percent << "%   ";
#endif
      ss << std::endl;
210 211 212
      // clang-format on
    }
  } else {
213
    float total = 0.0;
214
    for (auto& unit : units_) {
215
      const auto& times = unit.Timer(type)->LapTimes();
216 217
      total += times.Avg(w);
    }
218 219 220 221 222 223 224
#ifdef LITE_WITH_OPENCL
    float cl_total = 0.0;
    for (auto& unit : units_) {
      const auto& cl_times = unit.Timer(type)->CLLapTimes();
      cl_total += cl_times.Avg(w);
    }
#endif
225 226 227 228 229 230 231
    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);
      }
232 233 234 235 236 237 238 239 240 241

#ifdef LITE_WITH_OPENCL
      const auto& cl_times = unit.Timer(type)->CLLapTimes();
      float cl_run = cl_times.Avg(w);
      float cl_percent = 0;
      if (cl_total > 0) {
        cl_percent = 100 * (cl_run / cl_total);
      }
#endif

242
      // clang-format off
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
      ss << setw(20) << left << fixed << unit.Character().op_type
         << " " << setw(30) << left << fixed << unit.Character().kernel_attr
         << " " << setw(24) << left << fixed
         << unit.Character().kernel_func_name
         << " " << setw(16) << left << fixed << unit.Character().remark
         << " " << setw(15) << left << fixed << unit.Character().input_shape
         << " " << setw(15) << left << fixed << unit.Character().filter_shape
         << " " << setw(15) << left << fixed << unit.Character().output_shape
         << " " << setw(7) << left << fixed << setprecision(3) << times.Avg(w)
         << " " << setw(7) << left << fixed << setprecision(3) << times.Min(w)
         << " " << setw(7) << left << fixed << setprecision(3) << times.Max(w)
         << " " << setw(7) << left << fixed << setprecision(3) << times.Last(w)
         << " " << left << setprecision(2) << percent << "%   "
         << " " << setw(7) << left << fixed << setprecision(2)
                << 1e-9f * unit.Character().macs
         << " " << setw(7) << left << fixed << setprecision(2)
                << 1e-6f * unit.Character().macs / times.Avg(w);
// clang-format on
#ifdef LITE_WITH_OPENCL
      ss << " " << setw(9) << left << fixed << setprecision(3)
         << cl_times.Avg(w) << " " << setw(9) << left << fixed
         << setprecision(3) << cl_times.Min(w) << " " << setw(9) << left
         << fixed << setprecision(3) << cl_times.Max(w) << " " << left
         << setprecision(2) << cl_percent << "%   ";
#endif
      ss << std::endl;
269 270 271 272 273 274 275 276
    }
  }
  return ss.str();
}

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