debugger.hpp 4.3 KB
Newer Older
T
TianXiaogang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
// 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

C
chonwhite 已提交
17 18
#include <fstream>
#include <iostream>
T
TianXiaogang 已提交
19 20 21 22 23 24 25 26
#include <string>
#include <unordered_map>

#include "lite/core/tensor.h"

namespace paddle {
namespace lite {

C
chonwhite 已提交
27
// #define FPGA_PRINT_TENSOR
T
TianXiaogang 已提交
28 29 30 31 32 33 34 35 36

class Debugger {
 public:
  static Debugger& get_instance() {
    static Debugger s_instance;
    return s_instance;
  }

  void registerOutput(std::string op_type, zynqmp::Tensor* tensor) {
37 38
    if (op_config[op_type]) {
      tensor->saveToFile(op_type, true);
T
TianXiaogang 已提交
39 40 41
    }
  }

C
chonwhite 已提交
42 43 44 45 46 47 48 49 50
  void tick(std::string key) {
    float value = 0;
    if (tick_tock_map.count(key) > 0) {
      value += tick_tock_map[key] = value;
    }
  }

  void tock(std::string key) {}

C
chonwhite 已提交
51 52
  void setEnable(bool en) { enabled_ = en; }

T
TianXiaogang 已提交
53
 private:
C
chonwhite 已提交
54 55
  bool enabled_ = false;

T
TianXiaogang 已提交
56
  std::unordered_map<std::string, bool> op_config;
C
chonwhite 已提交
57
  std::unordered_map<std::string, float> tick_tock_map;
T
TianXiaogang 已提交
58 59
  Debugger() {
    op_config["concat"] = true;
60
    op_config["pooling"] = true;
T
TianXiaogang 已提交
61
    op_config["conv"] = true;
C
chonwhite 已提交
62
    op_config["dropout"] = true;
63 64
    op_config["dwconv"] = true;
    op_config["ew_add"] = true;
C
chonwhite 已提交
65
    op_config["ew_mul"] = true;
T
TianXiaogang 已提交
66
    op_config["crop"] = true;
67 68
    op_config["feed"] = true;
    op_config["fetch"] = true;
C
chonwhite 已提交
69 70
    op_config["fc"] = true;
    op_config["mul"] = true;
71 72 73 74 75 76
    op_config["boxes"] = true;
    op_config["scores"] = true;
    op_config["nms"] = true;
    op_config["pb_boxes"] = true;
    op_config["pb_variances"] = true;
    op_config["softmax"] = true;
C
chonwhite 已提交
77
    op_config["split"] = true;
T
TianXiaogang 已提交
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 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 127
  }
};

inline void chw_to_hwc(Tensor* t, float* dst) {
  int num = t->dims()[0];
  int channel = t->dims()[1];

  int height = 1;
  int width = 1;
  if (t->dims().size() > 2) {
    height = t->dims()[2];
  }
  if (t->dims().size() > 3) {
    width = t->dims()[3];
  }
  const float* chw_data = t->data<float>();
  float* hwc_data = dst;

  int chw = channel * height * width;
  int wc = width * channel;
  int index = 0;
  for (int n = 0; n < num; n++) {
    for (int c = 0; c < channel; c++) {
      for (int h = 0; h < height; h++) {
        for (int w = 0; w < width; w++) {
          hwc_data[n * chw + h * wc + w * channel + c] = chw_data[index];
          index++;
        }
      }
    }
  }
}

inline void read_from_file(lite::Tensor* t, const std::string& path) {
  std::ifstream file_stream;
  file_stream.open(path);
  if (!file_stream) {
    return;
  }
  float* data = t->mutable_data<float>();
  int num = t->numel();
  for (int i = 0; i < num; ++i) {
    float value = 0;
    file_stream >> value;
    data[i] = value;
  }
}

inline void save_float(float* data, const std::string& name, int len) {
  static int counter = 0;
C
chonwhite 已提交
128
  std::string old_string = paddle::lite::to_string(counter);
T
TianXiaogang 已提交
129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
  std::string new_string =
      std::string(3 - old_string.length(), '0') + old_string;

  std::string file = "arm_" + new_string + name;
  counter++;

  std::ofstream ofs;
  ofs.open(file);
  for (int i = 0; i < len; i++) {
    float value = data[i];
    ofs << value << std::endl;
  }
  ofs.close();
}

inline void save_tensor(lite::Tensor* t,
                        const std::string& name,
                        bool convert = true) {
  float* data = const_cast<float*>(t->data<float>());
  float* dst = new float[t->numel()];
  if (convert) {
    chw_to_hwc(t, dst);
    data = dst;
  }

  save_float(data, name, t->numel());
  delete[] dst;
}

inline void save_tensor(const lite::Tensor* t,
                        const std::string& name,
                        bool convert = true) {
  float* data = const_cast<float*>(t->data<float>());
  float* dst = new float[t->numel()];
  if (convert) {
    chw_to_hwc(const_cast<lite::Tensor*>(t), dst);
    data = dst;
  }
  save_float(data, name, t->numel());
  delete[] dst;
}
}  // namespace lite
}  // namespace paddle