profiler.h 3.9 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.

#pragma once
16
#include <map>
17 18 19 20
#include <memory>
#include <string>
#include <vector>
#include "lite/core/profile/timer.h"
21
#include "lite/core/tensor.h"
22 23 24
#ifdef LITE_WITH_OPENCL
#include "lite/backends/opencl/cl_include.h"
#endif
25 26 27 28 29

namespace paddle {
namespace lite {
namespace profile {

30 31 32 33 34 35 36 37
enum class Type {
  kUnk = 0,
  kCreate,
  kDispatch,
};

extern std::map<Type, std::string> TypeStr;

38 39 40 41
struct TimeInfo {
  float avg;
  float min;
  float max;
42 43 44 45 46
#ifdef LITE_WITH_OPENCL
  float cl_avg;
  float cl_min;
  float cl_max;
#endif
47 48 49 50
};

struct OpCharacter {
  TargetType target;
51
  void* op_lite{nullptr};
52 53
  std::string op_type{std::string("N/A")};
  std::string kernel_name{std::string("N/A")};
54 55
  std::string kernel_attr{std::string("N/A")};
  std::string kernel_func_name{std::string("N/A")};
56
  std::string remark{std::string("N/A")};
57 58 59 60 61 62 63 64 65 66 67 68

  std::string input_shape{"N/A"};
  std::string output_shape{"N/A"};
  std::string filter_shape{"N/A"};

  float macs{0};
  float macs_ps{0};

  float io_duration{0};

#ifdef LITE_WITH_OPENCL
  cl::Event cl_event{};
69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
  std::string global_work_size{"N/A"};
  std::string local_work_size{"N/A"};

  std::string NDRangeToStr(const cl::NDRange& range) {
    std::string range_str{""};
    const size_t range_size = 3;
    for (size_t i = 0; i < range_size /*range.size()*/; ++i) {
      LOG(INFO) << "range[" << i << "]:" << std::to_string(range[i]);
      range_str += std::to_string(range[i]);
      if (i != range_size - 1) {
        range_str += ",";
      }
    }
    return range_str;
  }
84 85 86 87 88 89 90 91 92 93 94 95 96 97 98
#else
  void* cl_event{nullptr};
#endif

  std::string DimToStr(const paddle::lite::DDimLite& dim) {
    if (!dim.size()) return "NotImpl";
    std::string dim_str{""};
    for (size_t i = 0; i < dim.size(); ++i) {
      dim_str += std::to_string(dim[i]);
      if (i != dim.size() - 1) {
        dim_str += "x";
      }
    }
    return dim_str;
  }
99 100 101 102 103 104 105

  std::string str() {
    std::string str{""};
    str += kernel_name + "/" + kernel_func_name + "/" + remark + "/" +
           input_shape + "/" + filter_shape + "/" + output_shape;
    return str;
  }
106 107
};

108 109 110 111
class StatisUnit final {
 public:
  explicit StatisUnit(const OpCharacter& ch);
  lite::profile::Timer* Timer(Type type);
112 113 114
  OpCharacter& Character() { return character; }

  OpCharacter character;
115 116 117 118

 protected:
  std::unique_ptr<lite::profile::Timer> create_t;
  std::unique_ptr<lite::profile::Timer> dispatch_t;
119 120 121 122 123 124 125
};

class Profiler final {
 public:
  Profiler() = default;
  explicit Profiler(const std::string& name) : name_(name) {}
  int NewTimer(const OpCharacter& ch);
126
  void StartTiming(Type type, const int index, KernelContext* ctx);
127
  void StopTiming(Type type, const int index, KernelContext* ctx);
128
  std::string Summary(Type type, bool concise = true, size_t warm_up = 10);
129
  int GetKernelFuncCalledTimes(const std::string& op_type,
130
                               const std::string& kernel_attr,
131 132
                               const std::string& kernel_func_name);
  float GetKernelFuncSummaryGOPs(const std::string& op_type,
133
                                 const std::string& kernel_attr,
134
                                 const std::string& kernel_func_name);
135
  OpCharacter* GetOpCharacter(const size_t index);
136 137 138 139 140 141 142 143 144

 private:
  std::string name_{std::string("N/A")};
  std::vector<StatisUnit> units_;
};

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