profiler.h 2.8 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 25 26

namespace paddle {
namespace lite {
namespace profile {

27 28 29 30 31 32 33 34
enum class Type {
  kUnk = 0,
  kCreate,
  kDispatch,
};

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

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

struct OpCharacter {
  TargetType target;
48
  void* op_lite{nullptr};
49 50
  std::string op_type{std::string("N/A")};
  std::string kernel_name{std::string("N/A")};
51 52
  std::string kernel_attr{std::string("N/A")};
  std::string kernel_func_name{std::string("N/A")};
53
  std::string remark{std::string("N/A")};
54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80

  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{};
#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;
  }
81 82
};

83 84 85 86
class StatisUnit final {
 public:
  explicit StatisUnit(const OpCharacter& ch);
  lite::profile::Timer* Timer(Type type);
87 88 89
  OpCharacter& Character() { return character; }

  OpCharacter character;
90 91 92 93

 protected:
  std::unique_ptr<lite::profile::Timer> create_t;
  std::unique_ptr<lite::profile::Timer> dispatch_t;
94 95 96 97 98 99 100
};

class Profiler final {
 public:
  Profiler() = default;
  explicit Profiler(const std::string& name) : name_(name) {}
  int NewTimer(const OpCharacter& ch);
101
  void StartTiming(Type type, const int index, KernelContext* ctx);
102
  void StopTiming(Type type, const int index, KernelContext* ctx);
103
  std::string Summary(Type type, bool concise = true, size_t warm_up = 10);
104
  OpCharacter* GetOpCharacter(const size_t index);
105 106 107 108 109 110 111 112 113

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

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