profiler.cc 11.3 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
namespace {
auto op_comp = [](const OpCharacter& c1, const OpCharacter& c2) {
27 28 29
  // compare for unique key of map
  return (c1.kernel_name + c1.kernel_func_name <
          c2.kernel_name + c2.kernel_func_name);
30
};
31
}  // namespace
32

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

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

69 70 71 72 73 74
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();
}

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

81
void Profiler::StopTiming(Type type, const int index, KernelContext* ctx) {
82 83
  CHECK_LT(index, units_.size())
      << "The timer index in the profiler is out of range.";
84 85 86 87 88 89
  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
90 91
}

92
int Profiler::GetKernelFuncCalledTimes(const std::string& op_type,
93
                                       const std::string& kernel_attr,
94 95 96 97
                                       const std::string& kernel_func_name) {
  int count = 0;
  for (size_t i = 0; i < units_.size(); ++i) {
    if ((units_[i].character.kernel_func_name == kernel_func_name) &&
98 99
        (units_[i].character.kernel_attr == kernel_attr) &&
        (units_[i].character.op_type == op_type)) {
100 101 102 103 104 105 106
      ++count;
    }
  }
  return count;
}

float Profiler::GetKernelFuncSummaryGOPs(const std::string& op_type,
107
                                         const std::string& kernel_attr,
108 109 110 111
                                         const std::string& kernel_func_name) {
  float GOPs = 0;
  for (size_t i = 0; i < units_.size(); ++i) {
    if ((units_[i].character.kernel_func_name == kernel_func_name) &&
112 113
        (units_[i].character.kernel_attr == kernel_attr) &&
        (units_[i].character.op_type == op_type)) {
114 115 116 117 118 119
      GOPs += units_[i].character.macs;
    }
  }
  return GOPs * 1e-9f;
}

120
std::string Profiler::Summary(Type type, bool concise, size_t w) {
121 122 123
  using std::setw;
  using std::left;
  using std::fixed;
124
  using std::setprecision;
125
  STL::stringstream ss;
126 127 128
  std::string title;
  // Title.
  if (concise) {
129
    ss << "Timing cycle = " << units_.front().Timer(type)->LapTimes().Size()
130
       << std::endl;
131 132
    ss << "===== Concise " << TypeStr.find(type)->second
       << " Profiler Summary: " << name_ << ", Exclude " << w
133 134
       << " warm-ups =====" << std::endl;
  } else {
135 136
    ss << "===== Detailed " << TypeStr.find(type)->second
       << " Profiler Summary: " << name_ << ", Exclude " << w
137 138
       << " warm-ups =====" << std::endl;
  }
139
  ss << setw(20) << left << "OperatorType"
140 141
     << " " << setw(30) << left << "KerneAttr(Place)"
     << " " << setw(24) << left << "KernelFuncName";
142
  if (!concise) {
143 144
    ss << " " << setw(26) << left << "Remark"
       << " " << setw(15) << left << "InDim"
145 146 147 148 149 150 151 152 153
       << " " << 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)";
  }
154 155
  ss << " " << setw(7) << left << "Avg(%)"
     << " " << setw(7) << left << "GOPs";
156
  if (!concise) {
157 158 159 160
    ss << " " << setw(7) << left << "GOPS";
  }
  if (concise) {
    ss << " " << setw(11) << left << "CalledTimes";
161 162 163 164 165 166
  }
#ifdef LITE_WITH_OPENCL
  ss << " " << setw(9) << left << "clAvg(ms)"
     << " " << setw(9) << left << "clMin(ms)"
     << " " << setw(9) << left << "clMax(ms)"
     << " " << setw(9) << left << "clAvg(%)";
167 168 169 170
  if (!concise) {
    ss << " " << setw(12) << left << "GlobalWorkSize"
       << " " << setw(12) << left << "LocalWorkSize";
  }
171 172 173
#endif
  ss << std::endl;

174
  // Profile information.
175 176 177
  if (concise) {
    std::map<OpCharacter, TimeInfo, decltype(op_comp)> summary(op_comp);
    for (auto& unit : units_) {
178
      auto ch = summary.find(unit.Character());
179
      if (ch != summary.end()) {
180 181 182
        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);
183 184 185 186 187
#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
188
      } else {
189 190 191 192
        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);
193 194 195 196 197
#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
198
        summary.insert({unit.Character(), info});
199 200
      }
    }
201

202 203
    // compute total time
    float total = 0.0;
204
    for (const auto& item : summary) {
205 206
      total += item.second.avg;
    }
207 208 209 210 211 212 213
#ifdef LITE_WITH_OPENCL
    float cl_total = 0.0;
    for (const auto& item : summary) {
      cl_total += item.second.cl_avg;
    }
#endif

214 215 216 217 218
    for (const auto& item : summary) {
      float percent = 0;
      if (total > 0) {
        percent = 100 * (item.second.avg / total);
      }
219
      // clang-format off
220 221
      ss << setw(20) << left << fixed << item.first.op_type
         << " " << setw(30) << left << fixed << item.first.kernel_attr
222
         << " " << setw(24) << left << fixed << item.first.kernel_func_name
223 224 225 226 227
         << " " << setw(7) << left << fixed << setprecision(3)
         << item.second.avg
         << " " << setw(7) << left << fixed << setprecision(3)
         << item.second.min
         << " " << setw(7) << left << fixed << setprecision(3)
228 229 230 231
         << item.second.max
         << " " << setprecision(2) << percent << "%   "
         << " " << setw(7) << left << fixed << setprecision(3)
         << GetKernelFuncSummaryGOPs(item.first.op_type,
232
                                     item.first.kernel_attr,
233 234 235
                                     item.first.kernel_func_name)
         << " " << setw(11) << left << fixed
         << GetKernelFuncCalledTimes(item.first.op_type,
236
                                     item.first.kernel_attr,
237
                                     item.first.kernel_func_name);
238 239 240 241 242 243 244 245 246 247 248
#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
249
         << " " << left << fixed << setprecision(2) << cl_percent << "%   ";
250 251
#endif
      ss << std::endl;
252 253 254
      // clang-format on
    }
  } else {
255
    float total = 0.0;
256
    for (auto& unit : units_) {
257
      const auto& times = unit.Timer(type)->LapTimes();
258 259
      total += times.Avg(w);
    }
260 261 262 263 264 265 266
#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
267 268 269 270 271 272 273
    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);
      }
274 275 276 277 278 279 280 281 282 283

#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

284
      // clang-format off
285 286 287 288
      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
289
         << " " << setw(26) << left << fixed << unit.Character().remark
290 291 292 293 294 295 296 297
         << " " << 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 << "%   "
298
         << " " << setw(7) << left << fixed << setprecision(3)
299 300 301 302 303 304 305 306 307
                << 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
308 309 310 311
         << setprecision(2) << cl_percent << "%   "
         << " " << setw(12) << left << fixed
         << unit.Character().global_work_size << " " << setw(12) << left
         << fixed << unit.Character().local_work_size;
312 313
#endif
      ss << std::endl;
314 315 316 317 318 319 320 321
    }
  }
  return ss.str();
}

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