dag.cpp 10.3 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14
// 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.

G
guru4elephant 已提交
15
#include "core/predictor/framework/dag.h"
W
wangguibao 已提交
16 17
#include <string>
#include <vector>
G
guru4elephant 已提交
18 19 20
#include "core/predictor/common/inner_common.h"
#include "core/predictor/framework/predictor_metric.h"  // PredictorMetric
#include "core/predictor/op/op.h"
B
barrierye 已提交
21 22 23
#define BLOG(fmt, ...) \
  printf(              \
      "[%s:%s]:%d " fmt "\n", __FILE__, __FUNCTION__, __LINE__, ##__VA_ARGS__)
W
wangguibao 已提交
24 25 26 27 28 29

namespace baidu {
namespace paddle_serving {
namespace predictor {

Dag::Dag() {
W
wangguibao 已提交
30 31 32
  _index_nodes.clear();
  _name_nodes.clear();
  _stages.clear();
W
wangguibao 已提交
33 34
}

W
wangguibao 已提交
35
Dag::~Dag() { deinit(); }
W
wangguibao 已提交
36 37

int Dag::deinit() {
W
wangguibao 已提交
38 39 40 41 42
  for (std::vector<DagStage*>::iterator iter = _stages.begin();
       iter != _stages.end();
       ++iter) {
    if (*iter != NULL) {
      delete *iter;
W
wangguibao 已提交
43
    }
W
wangguibao 已提交
44 45 46 47 48 49 50 51 52 53 54 55 56 57
  }
  _stages.clear();

  for (std::vector<DagNode*>::iterator iter = _index_nodes.begin();
       iter != _index_nodes.end();
       ++iter) {
    DagNode* node = *iter;
    if (node != NULL) {
      void* conf = node->conf;
      if (conf != NULL) {
        Op* op = OpRepository::instance().get_op(node->type);
        if (op == NULL) {
          LOG(ERROR) << "Failed to get_op, op type[" << node->type << "]";
          return -1;
W
wangguibao 已提交
58
        }
W
wangguibao 已提交
59 60 61 62
        op->delete_config(conf);
        OpRepository::instance().return_op(node->type, op);
      }
      delete node;
W
wangguibao 已提交
63
    }
W
wangguibao 已提交
64 65 66 67
  }
  _index_nodes.clear();
  _name_nodes.clear();
  return 0;
W
wangguibao 已提交
68 69 70
}

EdgeMode Dag::parse_mode(std::string& mode) {
W
wangguibao 已提交
71 72 73 74 75 76 77
  if (mode == "RO") {
    return RO;
  } else if (mode == "RW") {
    return RW;
  } else {
    return UNKNOWN;
  }
W
wangguibao 已提交
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
}

// [@Node]
// name: preprocess
// type: ProcessorOp
// [.@Depend]
// name: StartupOp
// mode: RO
// [@Node]
// name: discret_extractor
// type: DiscretExtractOp
// [.@Depend]
// name: StartupOp
// mode: RO
// [.@Depend]
// name: preprocess
// mode: RW
// [@Node]
// name: dnn_inference
// type: PaddleV2InferenceOp
// [.@Depend]
// name: discret_extractor
// mode: RO
// [@Node]
// name: postprocess
// type: PostProcessOp
// [.@Depend]
// name: dnn_inference
// mode: RO
W
wangguibao 已提交
107
#if 0
W
wangguibao 已提交
108 109 110
int Dag::init(const char* path, const char* file, const std::string& name) {
    comcfg::Configure conf;
    if (conf.load(path, file) != 0) {
W
wangguibao 已提交
111
        LOG(ERROR) << "Failed load conf from"
W
wangguibao 已提交
112 113 114 115 116 117 118
            << path << "/" << file << " in dag: "
            << name;
        return ERR_INTERNAL_FAILURE;
    }

    return init(conf, name);
}
W
wangguibao 已提交
119
#endif
W
wangguibao 已提交
120

W
wangguibao 已提交
121
int Dag::init(const configure::Workflow& conf, const std::string& name) {
W
wangguibao 已提交
122 123 124 125 126 127 128 129
  _dag_name = name;
  _index_nodes.clear();
  _name_nodes.clear();
  for (uint32_t i = 0; i < conf.nodes_size(); i++) {
    DagNode* node = new (std::nothrow) DagNode();
    if (node == NULL) {
      LOG(ERROR) << "Failed create new dag node";
      return ERR_MEM_ALLOC_FAILURE;
W
wangguibao 已提交
130
    }
W
wangguibao 已提交
131 132 133 134 135 136 137 138 139 140
    node->id = i + 1;  // 0 is reserved for begginer-op
    node->name = conf.nodes(i).name();
    node->type = conf.nodes(i).type();
    uint32_t depend_size = conf.nodes(i).dependencies_size();
    for (uint32_t j = 0; j < depend_size; j++) {
      const configure::DAGNodeDependency& depend =
          conf.nodes(i).dependencies(j);
      std::string name = depend.name();
      std::string mode = depend.mode();
      node->depends.insert(std::make_pair(name, parse_mode(mode)));
W
wangguibao 已提交
141
    }
W
wangguibao 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164
    Op* op = OpRepository::instance().get_op(node->type);
    if (op == NULL) {
      LOG(ERROR) << "Failed to get_op, op type[" << node->type << "]";
      return ERR_INTERNAL_FAILURE;
    }
    // node->conf could be NULL
    node->conf = op->create_config(conf.nodes(i));
    OpRepository::instance().return_op(node->type, op);
    _name_nodes.insert(std::make_pair(node->name, node));
    _index_nodes.push_back(node);
  }

  if (topo_sort() != 0) {
    LOG(ERROR) << "Topo sort dag[" << _dag_name << "] failed!";
    return ERR_INTERNAL_FAILURE;
  }

  if (FLAGS_el_log_level == 16) {
    LOG(INFO) << "DAG: " << _dag_name;
    LOG(INFO) << ", Op Num: " << _index_nodes.size();
    for (uint32_t nid = 0; nid < _index_nodes.size(); nid++) {
      DagNode* node = _index_nodes[nid];
      LOG(INFO) << ", OP-" << node->id << "-" << node->name << "-"
W
wangguibao 已提交
165
                << node->type;
W
wangguibao 已提交
166
      LOG(INFO) << " depends: " << node->depends.size();
W
wangguibao 已提交
167

W
wangguibao 已提交
168 169 170 171
      boost::unordered_map<std::string, EdgeMode>::iterator it;
      for (it = node->depends.begin(); it != node->depends.end(); it++) {
        LOG(INFO) << " " << it->first << " " << it->second;
      }
W
wangguibao 已提交
172
    }
W
wangguibao 已提交
173 174
    LOG(INFO) << "";
  }
W
wangguibao 已提交
175

W
wangguibao 已提交
176
  return ERR_OK;
W
wangguibao 已提交
177 178
}

W
wangguibao 已提交
179
uint32_t Dag::nodes_size() { return _index_nodes.size(); }
W
wangguibao 已提交
180

W
wangguibao 已提交
181
const DagNode* Dag::node_by_id(uint32_t id) { return _index_nodes[id]; }
W
wangguibao 已提交
182

W
wangguibao 已提交
183
const DagNode* Dag::node_by_id(uint32_t id) const { return _index_nodes[id]; }
W
wangguibao 已提交
184 185

const DagNode* Dag::node_by_name(std::string& name) {
W
wangguibao 已提交
186
  return _name_nodes[name];
W
wangguibao 已提交
187 188 189
}

const DagNode* Dag::node_by_name(const std::string& name) const {
W
wangguibao 已提交
190 191 192 193 194 195 196
  boost::unordered_map<std::string, DagNode*>::const_iterator it;
  it = _name_nodes.find(name);
  if (it == _name_nodes.end()) {
    LOG(WARNING) << "Not found op by name:" << name;
    return NULL;
  }
  return it->second;
W
wangguibao 已提交
197 198
}

W
wangguibao 已提交
199 200 201
uint32_t Dag::stage_size() { return _stages.size(); }

const DagStage* Dag::stage_by_index(uint32_t index) { return _stages[index]; }
W
wangguibao 已提交
202 203

int Dag::topo_sort() {
W
wangguibao 已提交
204
  std::stringstream ss;
B
barrierye 已提交
205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
  uint32_t nodes_size = _index_nodes.size();
  std::vector<uint32_t> in_degree(nodes_size, 0);
  std::vector<std::vector<uint32_t>> in_egde(nodes_size);
  for (uint32_t nid = 0; nid < nodes_size; nid++) {
    in_degree[nid] += _index_nodes[nid]->depends.size();
    for (auto it = _index_nodes[nid]->depends.begin();
         it != _index_nodes[nid]->depends.end();
         ++it) {
      uint32_t pnid = Dag::node_by_name(it->first)->id -
                      1;  // 0 is reserved for begginer-op
      in_egde[pnid].push_back(nid);
      BLOG("inegde[%d]: %d", pnid, nid);
    }
  }
  for (int i = 0; i < in_degree.size(); ++i) {
    BLOG("(%s) in_degree[%d]: %d",
         _index_nodes[i]->name.c_str(),
         i,
         in_degree[i]);
  }
  int sorted_num = 0;
  DagStage* stage = new (std::nothrow) DagStage();
  if (stage == NULL) {
    LOG(ERROR) << "Invalid stage!";
    return ERR_MEM_ALLOC_FAILURE;
  }
  ss.str("");
  ss << _stages.size();
  stage->name = ss.str();
  stage->full_name = full_name() + NAME_DELIMITER + stage->name;
  BLOG("stage->full_name: %s", stage->full_name.c_str());
  for (uint32_t nid = 0; nid < nodes_size; ++nid) {
    if (in_degree[nid] == 0) {
      BLOG("nid: %d", nid);
      ++sorted_num;
      stage->nodes.push_back(_index_nodes[nid]);
      // assign stage number after stage created
      _index_nodes[nid]->stage = _stages.size();
      // assign dag node full name after stage created
      _index_nodes[nid]->full_name =
          stage->full_name + NAME_DELIMITER + _index_nodes[nid]->name;
W
wangguibao 已提交
246
    }
B
barrierye 已提交
247 248 249 250 251 252 253 254 255 256 257
  }

  if (stage->nodes.size() == 0) {
    LOG(ERROR) << "Invalid Dag!";
    return ERR_INTERNAL_FAILURE;
  }
  _stages.push_back(stage);

  while (sorted_num < nodes_size) {
    auto pre_nodes = _stages.back()->nodes;
    DagStage* stage = new (std::nothrow) DagStage();
W
wangguibao 已提交
258 259 260 261
    ss.str("");
    ss << _stages.size();
    stage->name = ss.str();
    stage->full_name = full_name() + NAME_DELIMITER + stage->name;
B
barrierye 已提交
262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
    BLOG("stage->full_name: %s", stage->full_name.c_str());
    for (uint32_t pi = 0; pi < pre_nodes.size(); ++pi) {
      uint32_t pnid = pre_nodes[pi]->id - 1;
      BLOG("pnid: %d", pnid);
      for (uint32_t ei = 0; ei < in_egde[pnid].size(); ++ei) {
        uint32_t nid = in_egde[pnid][ei];
        --in_degree[nid];
        BLOG("nid: %d, indeg: %d", nid, in_degree[nid]);
        if (in_degree[nid] == 0) {
          BLOG("nid: %d", nid);
          ++sorted_num;
          stage->nodes.push_back(_index_nodes[nid]);
          // assign stage number after stage created
          _index_nodes[nid]->stage = _stages.size();
          // assign dag node full name after stage created
          _index_nodes[nid]->full_name =
              stage->full_name + NAME_DELIMITER + _index_nodes[nid]->name;
        }
      }
    }
    if (stage->nodes.size() == 0) {
      LOG(ERROR) << "Invalid Dag!";
      return ERR_INTERNAL_FAILURE;
    }
W
wangguibao 已提交
286 287
    _stages.push_back(stage);
  }
B
barrierye 已提交
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308
  /*std::stringstream ss;*/
  // for (uint32_t nid = 0; nid < _index_nodes.size(); nid++) {
  // DagStage* stage = new (std::nothrow) DagStage();
  // if (stage == NULL) {
  // LOG(ERROR) << "Invalid stage!";
  // return ERR_MEM_ALLOC_FAILURE;
  //}
  // stage->nodes.push_back(_index_nodes[nid]);
  // ss.str("");
  // ss << _stages.size();
  // stage->name = ss.str();
  // stage->full_name = full_name() + NAME_DELIMITER + stage->name;
  // BLOG("stage->full_name: %s", stage->full_name.c_str());
  //_stages.push_back(stage);

  //// assign stage number after stage created
  //_index_nodes[nid]->stage = nid;
  //// assign dag node full name after stage created
  //_index_nodes[nid]->full_name =
  // stage->full_name + NAME_DELIMITER + _index_nodes[nid]->name;
  /*}*/
W
wangguibao 已提交
309
  return ERR_OK;
W
wangguibao 已提交
310 311 312
}

void Dag::regist_metric(const std::string& service_name) {
W
wangguibao 已提交
313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
  for (int stage_idx = 0; stage_idx < _stages.size(); ++stage_idx) {
    DagStage* stage = _stages[stage_idx];
    PredictorMetric::GetInstance()->regist_latency_metric(
        STAGE_METRIC_PREFIX + service_name + NAME_DELIMITER + stage->full_name);
    for (int node_idx = 0; node_idx < stage->nodes.size(); ++node_idx) {
      DagNode* node = stage->nodes[node_idx];
      PredictorMetric::GetInstance()->regist_latency_metric(
          OP_METRIC_PREFIX + service_name + NAME_DELIMITER + node->full_name);
      Op* op = OpRepository::instance().get_op(node->type);
      if (op == NULL) {
        LOG(ERROR) << "Failed to get_op, op type[" << node->type << "]";
        return;
      }
      op->set_full_name(service_name + NAME_DELIMITER + node->full_name);
      op->set_config(node->conf);
      op->regist_metric();
      OpRepository::instance().return_op(node->type, op);
    }
  }
W
wangguibao 已提交
332 333
}

W
wangguibao 已提交
334 335 336
}  // namespace predictor
}  // namespace paddle_serving
}  // namespace baidu