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 30 31 32 33 34
  if (c1.kernel_func_name == "NotImpl" && c2.kernel_func_name == "NotImpl") {
    return (c1.target < c2.target) || (c1.op_type < c2.op_type) ||
           (c1.kernel_name < c2.kernel_name) || (c1.remark < c2.remark);
  } else {  // compare with ch.kernel_func_name
    return (c1.target < c2.target) || (c1.op_type < c2.op_type) ||
           (c1.kernel_name < c2.kernel_name) ||
           (c1.kernel_func_name < c2.kernel_func_name);
  }
35
};
36
}  // namespace
37

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

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);
70 71 72 73
  units_.push_back(std::move(unit));
  return units_.size() - 1;
}

74 75 76 77 78 79
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();
}

80
void Profiler::StartTiming(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
  units_[index].Timer(type)->Start(ctx);
84 85
}

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

97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
int Profiler::GetKernelFuncCalledTimes(const std::string& op_type,
                                       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) &&
        (units_[i].character.kernel_func_name != "NotImpl")) {
      ++count;
    } else if ((units_[i].character.kernel_func_name == "NotImpl") &&
               (units_[i].character.op_type == op_type)) {
      ++count;
    }
  }
  return count;
}

float Profiler::GetKernelFuncSummaryGOPs(const std::string& op_type,
                                         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) &&
        (units_[i].character.kernel_func_name != "NotImpl")) {
      GOPs += units_[i].character.macs;
    } else if ((units_[i].character.kernel_func_name == "NotImpl") &&
               (units_[i].character.op_type == op_type)) {
      GOPs += units_[i].character.macs;
    }
  }
  return GOPs * 1e-9f;
}

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

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

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

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

#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

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

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