dag_view.cpp 6.0 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.

W
wangguibao 已提交
15
#include "predictor/framework/dag_view.h"
W
wangguibao 已提交
16 17 18
#ifdef BCLOUD
#include <baidu/rpc/traceprintf.h>  // TRACEPRINTF
#else
W
wangguibao 已提交
19
#include <brpc/traceprintf.h>  // TRACEPRINTF
W
wangguibao 已提交
20
#endif
W
wangguibao 已提交
21
#include <string>
W
wangguibao 已提交
22 23
#include "predictor/common/inner_common.h"
#include "predictor/framework/op_repository.h"
W
wangguibao 已提交
24 25 26 27 28 29

namespace baidu {
namespace paddle_serving {
namespace predictor {

int DagView::init(Dag* dag, const std::string& service_name) {
W
wangguibao 已提交
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74
  _name = dag->name();
  _full_name = service_name + NAME_DELIMITER + dag->name();
  _bus = butil::get_object<Bus>();
  _bus->clear();
  uint32_t stage_size = dag->stage_size();
  // create tls stage view
  for (uint32_t si = 0; si < stage_size; si++) {
    const DagStage* stage = dag->stage_by_index(si);
    if (stage == NULL) {
      LOG(ERROR) << "Failed get stage by index:" << si;
      return ERR_INTERNAL_FAILURE;
    }
    ViewStage* vstage = butil::get_object<ViewStage>();
    if (vstage == NULL) {
      LOG(ERROR) << "Failed get vstage from object pool"
                 << "at:" << si;
      return ERR_MEM_ALLOC_FAILURE;
    }
    vstage->full_name = service_name + NAME_DELIMITER + stage->full_name;
    uint32_t node_size = stage->nodes.size();
    // create tls view node
    for (uint32_t ni = 0; ni < node_size; ni++) {
      DagNode* node = stage->nodes[ni];
      ViewNode* vnode = butil::get_object<ViewNode>();
      if (vnode == NULL) {
        LOG(ERROR) << "Failed get vnode at:" << ni;
        return ERR_MEM_ALLOC_FAILURE;
      }
      // factory type
      Op* op = OpRepository::instance().get_op(node->type);
      if (op == NULL) {
        LOG(ERROR) << "Failed get op with type:" << node->type;
        return ERR_INTERNAL_FAILURE;
      }

      // initialize a TLS op object
      if (op->init(_bus, dag, node->id, node->name, node->type, node->conf) !=
          0) {
        LOG(WARNING) << "Failed init op, type:" << node->type;
        return ERR_INTERNAL_FAILURE;
      }
      op->set_full_name(service_name + NAME_DELIMITER + node->full_name);
      vnode->conf = node;
      vnode->op = op;
      vstage->nodes.push_back(vnode);
W
wangguibao 已提交
75
    }
W
wangguibao 已提交
76 77
    _view.push_back(vstage);
  }
W
wangguibao 已提交
78

W
wangguibao 已提交
79
  return ERR_OK;
W
wangguibao 已提交
80 81 82
}

int DagView::deinit() {
W
wangguibao 已提交
83 84 85 86 87 88 89 90 91 92 93
  uint32_t stage_size = _view.size();
  for (uint32_t si = 0; si < stage_size; si++) {
    ViewStage* vstage = _view[si];
    uint32_t node_size = vstage->nodes.size();
    for (uint32_t ni = 0; ni < node_size; ni++) {
      ViewNode* vnode = vstage->nodes[ni];
      vnode->op->deinit();
      OpRepository::instance().return_op(vnode->op);
      vnode->reset();
      // clear item
      butil::return_object(vnode);
W
wangguibao 已提交
94
    }
W
wangguibao 已提交
95 96 97 98 99 100 101 102
    // clear vector
    vstage->nodes.clear();
    butil::return_object(vstage);
  }
  _view.clear();
  _bus->clear();
  butil::return_object(_bus);
  return ERR_OK;
W
wangguibao 已提交
103 104
}

W
wangguibao 已提交
105
int DagView::execute(butil::IOBufBuilder* debug_os) {
W
wangguibao 已提交
106 107 108 109 110 111 112 113
  uint32_t stage_size = _view.size();
  for (uint32_t si = 0; si < stage_size; si++) {
    TRACEPRINTF("start to execute stage[%u]", si);
    int errcode = execute_one_stage(_view[si], debug_os);
    TRACEPRINTF("finish to execute stage[%u]", si);
    if (errcode < 0) {
      LOG(ERROR) << "failed execute stage[" << _view[si]->debug();
      return errcode;
W
wangguibao 已提交
114
    }
W
wangguibao 已提交
115 116
  }
  return ERR_OK;
W
wangguibao 已提交
117 118 119 120 121 122
}

// The default execution strategy is in sequencing
// You can derive a subclass to implement this func.
// ParallelDagView maybe the one you want.
int DagView::execute_one_stage(ViewStage* vstage,
W
wangguibao 已提交
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140
                               butil::IOBufBuilder* debug_os) {
  butil::Timer stage_time(butil::Timer::STARTED);
  uint32_t node_size = vstage->nodes.size();
  for (uint32_t ni = 0; ni < node_size; ni++) {
    ViewNode* vnode = vstage->nodes[ni];
    DagNode* conf = vnode->conf;
    Op* op = vnode->op;
    TRACEPRINTF("start to execute op[%s]", op->name());
    int errcode = op->process(debug_os != NULL);
    TRACEPRINTF("finish to execute op[%s]", op->name());
    if (errcode < 0) {
      LOG(ERROR) << "Execute failed, Op:" << op->debug_string();
      return errcode;
    }

    if (errcode > 0) {
      LOG(INFO) << "Execute ignore, Op:" << op->debug_string();
      continue;
W
wangguibao 已提交
141
    }
W
wangguibao 已提交
142 143 144 145 146 147 148 149 150 151 152 153 154

    if (debug_os) {
      (*debug_os) << "{\"op_name\": \"" << op->name()
                  << "\", \"debug_str:\": \"" << op->debug_string()
                  << "\", \"time_info\": \"" << op->time_info() << "\"}";
    }

    // LOG(DEBUG) << "Execute succ, Op:" << op->debug_string();
  }
  stage_time.stop();
  PredictorMetric::GetInstance()->update_latency_metric(
      STAGE_METRIC_PREFIX + vstage->full_name, stage_time.u_elapsed());
  return ERR_OK;
W
wangguibao 已提交
155 156 157
}

int DagView::set_request_channel(Channel& request) {
W
wangguibao 已提交
158 159 160
  // Each workflow should get the very beginning
  // request (channel), and commit it to bus, for
  // the first stage ops consuming.
W
wangguibao 已提交
161

W
wangguibao 已提交
162
  request.share_to_bus(_bus);
W
wangguibao 已提交
163

W
wangguibao 已提交
164
  return ERR_OK;
W
wangguibao 已提交
165 166 167
}

const Channel* DagView::get_response_channel() const {
W
wangguibao 已提交
168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
  // Caller obtains response channel from bus, and
  // writes it to rpc response(protbuf/json)
  if (_view.size() < 1) {
    LOG(ERROR) << "invalid empty view stage!";
    return NULL;
  }

  ViewStage* last_stage = _view[_view.size() - 1];
  if (last_stage->nodes.size() != 1 || last_stage->nodes[0] == NULL) {
    LOG(ERROR) << "Invalid last stage, size[" << last_stage->nodes.size()
               << "] != 1";
    return NULL;
  }

  Op* last_op = last_stage->nodes[0]->op;
  if (last_op == NULL) {
    LOG(ERROR) << "Last op is NULL";
    return NULL;
  }
  return last_op->mutable_channel();
W
wangguibao 已提交
188 189
}

W
wangguibao 已提交
190 191 192
}  // namespace predictor
}  // namespace paddle_serving
}  // namespace baidu