op.h 6.4 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
#ifndef BAIDU_PADDLE_SERVING_PREDICTOR_OP_H
#define BAIDU_PADDLE_SERVING_PREDICTOR_OP_H

#include <bvar/bvar.h> // bvar::LatencyRecorder
#include "common/inner_common.h"
#include "framework/channel.h"
#include "framework/op_repository.h"
#include "framework/predictor_metric.h" // PredictorMetric

namespace baidu {
namespace paddle_serving {
namespace predictor {

class Dag;

class Op {
public:
    Op() : _bus(NULL), 
        _dag(NULL), 
        _has_calc(false), 
        _has_init(false), 
        _timer(NULL) {}

    virtual ~Op() {}

    // ------对依赖OP的Channel/Data/Message数据获取接口-----

    // 获得依赖Op的Channel对象
    Channel* mutable_depend_channel(const std::string& op);

    // 获得依赖Op的Channel对象
    const Channel* get_depend_channel(const std::string& op) const;

    template<typename T>
    T* mutable_depend_argument(const std::string& op) {
        Channel* channel = mutable_depend_channel(op); 
        if (channel == NULL) {
            LOG(WARNING) << "cannot mutable channel of " << op 
                    << " in " << _name;
            return NULL;
        }

        OpChannel<T>* op_channel = 
            dynamic_cast<OpChannel<T>*>(channel);
        if (!op_channel) {
46
            LOG(ERROR) << "Cannot dynamic cast channel of op:" 
W
wangguibao 已提交
47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65
                << this->name() << " to type: " << typeid(T).name();
            return NULL;
        }

        return op_channel->data();
    }

    template<typename T>
    const T* get_depend_argument(const std::string& op) const {
        const Channel* channel = get_depend_channel(op);
        if (channel == NULL) {
            LOG(WARNING) << "cannot get read-only channel of " << op 
                    << " in " << _name;
            return NULL;
        }

        const OpChannel<T>* op_channel = 
            dynamic_cast<const OpChannel<T>*>(channel);
        if (!op_channel) {
66
            LOG(ERROR) << "Cannot dynamic cast channel of op:" 
W
wangguibao 已提交
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 140 141 142 143 144 145 146 147 148 149 150
                << this->name() << " to type: " << typeid(T).name();
            return NULL;
        }

        return op_channel->data();
    }

    // -----对当前OP的Channel/Data/Message数据获取接口----

    // 获得该OP的Protobuf message类型指针
    google::protobuf::Message* mutable_message();

    // 获得该OP的Protobuf message类型指针
    const google::protobuf::Message* get_message() const;

    // 获得该OP的模板类数据对象
    template<typename T> 
    T* mutable_data() {
        Channel* channel = mutable_channel();
        return (dynamic_cast<OpChannel<T>*>(channel))->data();
    }

    // 获得该OP的模板类数据对象
    template<typename T> 
    const T* get_data() const {
        const Channel* channel = get_channel();
        return (dynamic_cast<const OpChannel<T>*>(channel))->data(); 
    }

    // ---------------- 其它基类成员函数 ----------------

    int init(Bus* bus, Dag* dag, uint32_t id, const std::string& name,
            const std::string& type, void* conf);

    int deinit();

    int check_time(const char* tag);

    int process(bool debug);

    std::string time_info();

    // 获得输入对象
    const google::protobuf::Message* get_request_message();

    bool has_calc();

    const char* name() const;

    const std::string& full_name() const {
        return _full_name;
    }
    
    void set_full_name(const std::string full_name) {
        _full_name = full_name;
    }
    
    const std::string& type() const;

    uint32_t id() const;

    // --------------- Default implements ----------------

    virtual int custom_init() { return 0; }

    virtual int custom_deinit() { return 0; }

    virtual const std::string debug_string();

    // ------------------ OP Interface -------------------

    // 获得当前Op的Channel派生类对象
    virtual Channel* mutable_channel() = 0;

    // 获得当前Op的Channel派生类对象
    virtual const Channel* get_channel() const = 0;

    // 释放当前Op的Channel派生类对象
    virtual int release_channel() = 0;

    // 当前Op自定义inference函数接口
    virtual int inference() = 0;

    // ------------------ Conf Interface -------------------
W
wangguibao 已提交
151
    virtual void* create_config(const configure::DAGNode& conf) { return NULL; }
W
wangguibao 已提交
152 153 154 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
    
    virtual void delete_config(void* conf) { }

    virtual void set_config(void* conf) { return; }

    // ------------------ Metric Interface -------------------
    virtual void regist_metric() { return; }
     
private:
    bool is_mutable(const std::string& op);

    bool is_mutable(const std::string& op) const;

    bool is_readable(const std::string& op);

    bool is_readable(const std::string& op) const;

private:
    Bus* _bus;
    Dag* _dag;
    uint32_t _id;
    std::string _name;
    std::string _full_name; // service_workflow_stageindex_opname 
    std::string _type;
    bool _has_calc;
    bool _has_init;
    TimerFlow* _timer;
};

template<typename T>
class OpWithChannel : public Op {
public:
    typedef T DataType;
    typedef OpChannel<T> ChannelType;

    OpWithChannel() : _channel(NULL) {}

    virtual ~OpWithChannel() {}

    // ---------- Implements ----------

    Channel* mutable_channel() {
        if (_channel != NULL) {
            return _channel;
        } 

W
wangguibao 已提交
198
        _channel = butil::get_object<ChannelType>();
W
wangguibao 已提交
199
        if (!_channel) {
200
            LOG(ERROR) 
W
wangguibao 已提交
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215
                << "Failed mutable channel of type:"
                << typeid(T).name();
            return NULL;
        }
        _channel->init(this->id(), this->name());
        return _channel;
    }

    const Channel* get_channel() const {
        return _channel;
    }

    int release_channel() {
        if (_channel) {
            _channel->deinit();
W
wangguibao 已提交
216
            butil::return_object<ChannelType>(_channel);
W
wangguibao 已提交
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 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
        } 

        _channel = NULL; 
        return 0;
    }

    // ------------- Interface -------------

    // Op自定义inference接口
    virtual int inference() = 0;

private:
    ChannelType* _channel;
};

template<typename T, typename C>
class OpWithChannelAndConf : public OpWithChannel<T> {
public:
    void set_config(void* conf) {
        _conf = static_cast<C*>(conf);
    }

    C* get_self_config() { return _conf; }

    virtual void delete_config(void* conf) { delete static_cast<C*>(conf); }

private:
    C* _conf;
};

#define DECLARE_OP(OP_TYPE)         \
    OP_TYPE() {                     \
        REGISTER_OP(OP_TYPE);       \
    }                               \
    static OP_TYPE _s_##OP_TYPE     \

#define DEFINE_OP(OP_TYPE)          \
    OP_TYPE OP_TYPE::_s_##OP_TYPE   \

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

#endif