test_configure.cpp 9.1 KB
Newer Older
W
wangguibao 已提交
1 2 3 4
#include <sys/types.h>
#include <sys/stat.h>
#include <unistd.h>
#include <iostream>
W
wangguibao 已提交
5 6
#include "server_configure.pb.h"
#include "sdk_configure.pb.h"
7
#include "inferencer_configure.pb.h"
W
wangguibao 已提交
8 9 10 11 12 13 14 15 16 17 18 19 20 21 22
#include "configure_parser.h"

using baidu::paddle_serving::configure::EngineDesc;
using baidu::paddle_serving::configure::ModelToolkitConf;

using baidu::paddle_serving::configure::ResourceConf;

using baidu::paddle_serving::configure::DAGNodeDependency;
using baidu::paddle_serving::configure::DAGNode;
using baidu::paddle_serving::configure::Workflow;
using baidu::paddle_serving::configure::WorkflowConf;

using baidu::paddle_serving::configure::InferService;
using baidu::paddle_serving::configure::InferServiceConf;

W
wangguibao 已提交
23 24 25 26 27 28 29 30
using baidu::paddle_serving::configure::ConnectionConf;
using baidu::paddle_serving::configure::WeightedRandomRenderConf;
using baidu::paddle_serving::configure::NamingConf;
using baidu::paddle_serving::configure::RpcParameter;
using baidu::paddle_serving::configure::Predictor;
using baidu::paddle_serving::configure::VariantConf;
using baidu::paddle_serving::configure::SDKConf;

31 32
using baidu::paddle_serving::configure::SigmoidConf;

W
wangguibao 已提交
33 34 35 36 37
const std::string output_dir = "./conf/";
const std::string model_toolkit_conf_file = "model_toolkit.prototxt";
const std::string resource_conf_file = "resource.prototxt";
const std::string workflow_conf_file = "workflow.prototxt";
const std::string service_conf_file = "service.prototxt";
38 39
const std::string sdk_conf_file = "predictors.prototxt";
const std::string sigmoid_conf_file = "inferencer.prototxt";
W
wangguibao 已提交
40 41 42 43 44 45 46

int test_write_conf()
{
    // model_toolkit conf
    ModelToolkitConf model_toolkit_conf;

    // This engine has a default version
W
wangguibao 已提交
47
    EngineDesc *engine = model_toolkit_conf.add_engines();
W
wangguibao 已提交
48
    engine->set_name("image_classification_resnet");
W
wangguibao 已提交
49 50 51 52 53 54 55
    engine->set_type("FLUID_CPU_NATIVE_DIR");
    engine->set_reloadable_meta("./data/model/paddle/fluid_time_file");
    engine->set_reloadable_type("timestamp_ne");
    engine->set_model_data_path("./data/model/paddle/fluid/SE_ResNeXt50_32x4d");
    engine->set_runtime_thread_num(0);
    engine->set_batch_infer_size(0);
    engine->set_enable_batch_align(0);
W
wangguibao 已提交
56 57 58 59 60 61 62 63 64

    int ret = baidu::paddle_serving::configure::write_proto_conf(&model_toolkit_conf, output_dir, model_toolkit_conf_file);
    if (ret != 0) {
        return ret;
    }

    // resource conf
    ResourceConf resource_conf;
    resource_conf.set_model_toolkit_path(output_dir);
W
wangguibao 已提交
65
    resource_conf.set_model_toolkit_file("model_toolkit.prototxt");
W
wangguibao 已提交
66 67 68 69 70 71 72
    ret = baidu::paddle_serving::configure::write_proto_conf(&resource_conf, output_dir, resource_conf_file);
    if (ret != 0) {
        return ret;
    }

    // workflow entries conf
    WorkflowConf workflow_conf;
W
wangguibao 已提交
73
    Workflow *workflow = workflow_conf.add_workflows();
W
wangguibao 已提交
74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94
    workflow->set_name("workflow1");
    workflow->set_workflow_type("Sequence");

    DAGNode *dag_node = workflow->add_nodes();
    dag_node->set_name("image_reader_op");
    dag_node->set_type("ReaderOp");

    dag_node = workflow->add_nodes();
    dag_node->set_name("imag_classify_op");
    dag_node->set_type("ClassifyOp");
    DAGNodeDependency *node_dependency = dag_node->add_dependencies();
    node_dependency->set_name("image_reader_op");
    node_dependency->set_mode("RO");

    dag_node = workflow->add_nodes();
    dag_node->set_name("write_json_op");
    dag_node->set_type("WriteOp");
    node_dependency = dag_node->add_dependencies();
    node_dependency->set_name("image_classify_op");
    node_dependency->set_mode("RO");

W
wangguibao 已提交
95
    workflow = workflow_conf.add_workflows();
W
wangguibao 已提交
96 97 98 99 100 101 102 103 104 105 106 107 108
    workflow->set_name("workflow2");
    workflow->set_workflow_type("Sequence");

    dag_node = workflow->add_nodes();
    dag_node->set_name("dense_op");
    dag_node->set_type("DenseOp");

    ret = baidu::paddle_serving::configure::write_proto_conf(&workflow_conf, output_dir, workflow_conf_file);
    if (ret != 0) {
        return ret;
    }

    InferServiceConf infer_service_conf;
W
wangguibao 已提交
109 110
    infer_service_conf.set_port(0);
    InferService *infer_service = infer_service_conf.add_services();
W
wangguibao 已提交
111
    infer_service->set_name("ImageClassifyService");
W
wangguibao 已提交
112 113
    infer_service->add_workflows("workflow1");
    infer_service->add_workflows("workflow2");
W
wangguibao 已提交
114

W
wangguibao 已提交
115
    infer_service = infer_service_conf.add_services();
W
wangguibao 已提交
116
    infer_service->set_name("BuiltinDenseFormatService");
W
wangguibao 已提交
117
    infer_service->add_workflows("workflow2");
W
wangguibao 已提交
118 119 120 121 122

    ret = baidu::paddle_serving::configure::write_proto_conf(&infer_service_conf, output_dir, service_conf_file);
    if (ret != 0) {
        return ret;
    }
W
wangguibao 已提交
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 151 152 153 154 155 156 157 158 159 160 161 162 163 164

    SDKConf sdk_conf;
    VariantConf *default_variant_conf = sdk_conf.mutable_default_variant_conf();
    default_variant_conf->set_tag("default");

    ConnectionConf *connection_conf = default_variant_conf->mutable_connection_conf();
    connection_conf->set_connect_timeout_ms(2000);
    connection_conf->set_rpc_timeout_ms(20000);
    connection_conf->set_connect_retry_count(2);
    connection_conf->set_max_connection_per_host(100);
    connection_conf->set_hedge_request_timeout_ms(-1);
    connection_conf->set_hedge_fetch_retry_count(2);
    connection_conf->set_connection_type("pooled");

    NamingConf *naming_conf = default_variant_conf->mutable_naming_conf();
    naming_conf->set_cluster_filter_strategy("Default");
    naming_conf->set_load_balance_strategy("la");

    RpcParameter *rpc_parameter = default_variant_conf->mutable_rpc_parameter();
    rpc_parameter->set_compress_type(0);
    rpc_parameter->set_package_size(20);
    rpc_parameter->set_protocol("baidu_std");
    rpc_parameter->set_max_channel_per_request(3);

    Predictor *predictor = sdk_conf.add_predictors();
    predictor->set_name("ximage");
    predictor->set_service_name("baidu.paddle_serving.predictor.image_classification.ImageClassifyService");
    predictor->set_endpoint_router("WeightedRandomRender");

    WeightedRandomRenderConf *weighted_random_render_conf = predictor->mutable_weighted_random_render_conf();
    weighted_random_render_conf->set_variant_weight_list("50");

    VariantConf *variant_conf = predictor->add_variants();
    variant_conf->set_tag("var1");
    naming_conf = variant_conf->mutable_naming_conf();
    naming_conf->set_cluster("list://127.0.0.1:8010");

    ret = baidu::paddle_serving::configure::write_proto_conf(&sdk_conf, output_dir, sdk_conf_file);
    if (ret != 0) {
        return ret;
    }

165 166 167 168 169 170 171 172 173 174 175 176
    SigmoidConf sigmoid_conf;
    sigmoid_conf.set_dnn_model_path("data/dnn_model");
    sigmoid_conf.set_sigmoid_w_file("data/dnn_model/_sigmoid_.w_0");
    sigmoid_conf.set_sigmoid_b_file("data/dnn_model/_sigmoid_.b_0");
    sigmoid_conf.set_exp_max_input(0.75);
    sigmoid_conf.set_exp_min_input(0.25);

    ret = baidu::paddle_serving::configure::write_proto_conf(&sigmoid_conf, output_dir, sigmoid_conf_file);
    if (ret != 0) {
        return ret;
    }

W
wangguibao 已提交
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
    return 0;
}

int test_read_conf()
{
    int ret = 0;

    ModelToolkitConf model_toolkit_conf;
    ret = baidu::paddle_serving::configure::read_proto_conf(output_dir, model_toolkit_conf_file, &model_toolkit_conf);
    if (ret != 0) {
        std::cout << "Read conf fail: " << model_toolkit_conf_file << std::endl;
        return -1;
    }

    ResourceConf resource_conf;
    ret = baidu::paddle_serving::configure::read_proto_conf(output_dir, resource_conf_file, &resource_conf);
    if (ret != 0) {
        std::cout << "Read conf fail: " << resource_conf_file << std::endl;
        return -1;
    }

    WorkflowConf workflow_conf;
    ret = baidu::paddle_serving::configure::read_proto_conf(output_dir, workflow_conf_file, &workflow_conf);
    if (ret != 0) {
        std::cout << "Read conf fail: " << workflow_conf_file << std::endl;
        return -1;
    }

    InferServiceConf service_conf;
    ret = baidu::paddle_serving::configure::read_proto_conf(output_dir, service_conf_file, &service_conf);
    if (ret != 0) {
        std::cout << "Read conf fail: " << service_conf_file << std::endl;
        return -1;
    }

W
wangguibao 已提交
212 213 214 215 216 217 218
    SDKConf sdk_conf;
    ret = baidu::paddle_serving::configure::read_proto_conf(output_dir, sdk_conf_file, &sdk_conf);
    if (ret != 0) {
        std::cout << "Read conf fail: " << sdk_conf_file << std::endl;
        return -1;
    }

219 220 221 222 223 224 225
    SigmoidConf sigmoid_conf;
    ret = baidu::paddle_serving::configure::read_proto_conf(output_dir, sigmoid_conf_file, &sigmoid_conf);
    if (ret != 0) {
        std::cout << "Read conf fail: " << sdk_conf_file << std::endl;
        return -1;
    }

W
wangguibao 已提交
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 261 262 263
    return 0;
}

int main()
{
    int ret = 0;
    struct stat stat_buf;
    if (stat(output_dir.c_str(), &stat_buf) != 0) {
        int ret = mkdir("./conf", 0777);
        if (ret != 0) {
            std::cout << "mkdir ./conf fail" << std::endl;
            return -1;
        }
        if (stat("./conf", &stat_buf) != 0) {
            std::cout << "./conf not exist and creating it failed" << std::endl;
            return -1;
        }
    }

    ret = test_write_conf();
    if (ret != 0) {
        std::cout << "test_write_conf fail" << std::endl;
        return -1;
    }

    std::cout << "test_write_conf success" << std::endl;

    ret = test_read_conf();
    if (ret != 0) {
        std::cout << "test_read_conf fail" << std::endl;
        return -1;
    }
    std::cout << "test_read_conf success" << std::endl;

    return 0;
}

/* vim: set expandtab ts=4 sw=4 sts=4 tw=100: */