dag.cpp 7.2 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 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 75 76 77 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 128 129 130 131 132 133 134 135 136 137 138 139
#include "common/inner_common.h"
#include "framework/dag.h"
#include "op/op.h"
#include "framework/predictor_metric.h" // PredictorMetric

namespace baidu {
namespace paddle_serving {
namespace predictor {

Dag::Dag() {
    _index_nodes.clear();
    _name_nodes.clear();
    _stages.clear();
}

Dag::~Dag() {
    deinit();
}

int Dag::deinit() {
    for (std::vector<DagStage*>::iterator iter = _stages.begin(); iter != _stages.end(); ++iter) {
        if (*iter != NULL) {
            delete *iter;
        }
    }
    _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(FATAL) << "Failed to get_op, op type[" << node->type << "]";
                    return -1;
                }
                op->delete_config(conf);
                OpRepository::instance().return_op(node->type, op);
            }
            delete node;
        }
    }
    _index_nodes.clear();
    _name_nodes.clear();
    return 0;
}

EdgeMode Dag::parse_mode(std::string& mode) {
    if (mode == "RO") {
        return RO;
    } else if (mode == "RW") {
        return RW;
    } else {
        return UNKNOWN;
    }
}

// [@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
int Dag::init(const char* path, const char* file, const std::string& name) {
    comcfg::Configure conf;
    if (conf.load(path, file) != 0) {
        LOG(FATAL) << "Failed load conf from" 
            << path << "/" << file << " in dag: "
            << name;
        return ERR_INTERNAL_FAILURE;
    }

    return init(conf, name);
}

int Dag::init(const comcfg::Configure& conf, const std::string& name) {
    _dag_name = name;
    _index_nodes.clear();
    _name_nodes.clear();
    for (uint32_t i = 0; i < conf["Node"].size(); i++) {
        DagNode* node = new (std::nothrow) DagNode();
        if (node == NULL) {
            LOG(ERROR) << "Failed create new dag node";
            return ERR_MEM_ALLOC_FAILURE;
        }
        node->id = i + 1; // 0 is reserved for begginer-op
        node->name = conf["Node"][i]["name"].to_cstr();
        node->type = conf["Node"][i]["type"].to_cstr();
        uint32_t depend_size = conf["Node"][i]["Depend"].size();
        for (uint32_t j = 0; j < depend_size; j++) {
            const comcfg::ConfigUnit& depend = 
                conf["Node"][i]["Depend"][j];
            std::string name = depend["name"].to_cstr();
            std::string mode = depend["mode"].to_cstr();
            node->depends.insert(
                    std::make_pair(name, parse_mode(mode)));
        }
        Op* op = OpRepository::instance().get_op(node->type);
        if (op == NULL) {
            LOG(FATAL) << "Failed to get_op, op type[" << node->type << "]";
            return ERR_INTERNAL_FAILURE;
        }
        // node->conf could be NULL
        node->conf = op->create_config(conf["Node"][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(FATAL) << "Topo sort dag[" << _dag_name << "] failed!";
        return ERR_INTERNAL_FAILURE;
    }

    if (FLAGS_el_log_level == 16) {
W
wangguibao 已提交
140 141
        LOG(INFO) << "DAG: " << _dag_name << noflush;
        LOG(INFO) << ", Op Num: " << _index_nodes.size();
W
wangguibao 已提交
142 143
        for (uint32_t nid = 0; nid < _index_nodes.size(); nid++) {
            DagNode* node = _index_nodes[nid];
W
wangguibao 已提交
144
            LOG(INFO) 
W
wangguibao 已提交
145 146
                << ", OP-" << node->id << "-" << node->name << "-" 
                << node->type << noflush;
W
wangguibao 已提交
147
            LOG(INFO) << " depends: " << node->depends.size() << noflush;
W
wangguibao 已提交
148 149 150

            boost::unordered_map<std::string, EdgeMode>::iterator it;
            for (it = node->depends.begin(); it != node->depends.end(); it++) {
W
wangguibao 已提交
151
                LOG(INFO) << " " << it->first << " " << it->second << noflush;
W
wangguibao 已提交
152 153
            }
        }
W
wangguibao 已提交
154
        LOG(INFO) << "";
W
wangguibao 已提交
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 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
    }

    return ERR_OK;
}

uint32_t Dag::nodes_size() {
    return _index_nodes.size();
}

const DagNode* Dag::node_by_id(uint32_t id) {
    return _index_nodes[id];
}

const DagNode* Dag::node_by_id(uint32_t id) const {
    return _index_nodes[id];
}

const DagNode* Dag::node_by_name(std::string& name) {
    return _name_nodes[name];
}

const DagNode* Dag::node_by_name(const std::string& name) const {
    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;
}

uint32_t Dag::stage_size() {
    return _stages.size();
}

const DagStage* Dag::stage_by_index(uint32_t index) {
    return _stages[index];
}
    
int Dag::topo_sort() {
    // TODO ƽ
    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; 
        _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;
    }
    return ERR_OK;
}

void Dag::regist_metric(const std::string& service_name) {
    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(FATAL) << "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);
        }
    } 
}

} // predictor
} // paddle_serving
} // baidu