test_configure.cpp 8.3 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"
W
wangguibao 已提交
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
#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 已提交
22 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;

W
wangguibao 已提交
31 32 33 34 35
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";
W
wangguibao 已提交
36
const std::string sdk_conf_file = "predictors.protxt";
W
wangguibao 已提交
37 38 39 40 41 42 43

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

    // This engine has a default version
W
wangguibao 已提交
44
    EngineDesc *engine = model_toolkit_conf.add_engines();
W
wangguibao 已提交
45
    engine->set_name("image_classification_resnet");
W
wangguibao 已提交
46 47 48 49 50 51 52
    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 已提交
53 54 55 56 57 58 59 60 61

    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 已提交
62
    resource_conf.set_model_toolkit_file("model_toolkit.prototxt");
W
wangguibao 已提交
63 64 65 66 67 68 69
    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 已提交
70
    Workflow *workflow = workflow_conf.add_workflows();
W
wangguibao 已提交
71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91
    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 已提交
92
    workflow = workflow_conf.add_workflows();
W
wangguibao 已提交
93 94 95 96 97 98 99 100 101 102 103 104 105
    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 已提交
106 107
    infer_service_conf.set_port(0);
    InferService *infer_service = infer_service_conf.add_services();
W
wangguibao 已提交
108
    infer_service->set_name("ImageClassifyService");
W
wangguibao 已提交
109 110
    infer_service->add_workflows("workflow1");
    infer_service->add_workflows("workflow2");
W
wangguibao 已提交
111

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

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

    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;
    }

W
wangguibao 已提交
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
    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 已提交
197 198 199 200 201 202 203
    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;
    }

W
wangguibao 已提交
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
    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: */