test_configure.cpp 9.4 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 <sys/stat.h>
W
wangguibao 已提交
16
#include <sys/types.h>
W
wangguibao 已提交
17 18
#include <unistd.h>
#include <iostream>
W
wangguibao 已提交
19
#include "configure/include/configure_parser.h"
W
wangguibao 已提交
20 21 22
#include "configure/inferencer_configure.pb.h"
#include "configure/sdk_configure.pb.h"
#include "configure/server_configure.pb.h"
W
wangguibao 已提交
23 24 25 26 27 28 29 30 31 32 33 34 35 36

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 已提交
37 38 39 40 41 42 43 44
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;

45 46
using baidu::paddle_serving::configure::SigmoidConf;

W
wangguibao 已提交
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 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 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
const char *output_dir = "./conf/";
const char *model_toolkit_conf_file = "model_toolkit.prototxt";
const char *resource_conf_file = "resource.prototxt";
const char *workflow_conf_file = "workflow.prototxt";
const char *service_conf_file = "service.prototxt";
const char *sdk_conf_file = "predictors.prototxt";
const char *sigmoid_conf_file = "inferencer.prototxt";

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

  // This engine has a default version
  EngineDesc *engine = model_toolkit_conf.add_engines();
  engine->set_name("image_classification_resnet");
  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);

  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);
  resource_conf.set_model_toolkit_file("model_toolkit.prototxt");
  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;
  Workflow *workflow = workflow_conf.add_workflows();
  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");

  workflow = workflow_conf.add_workflows();
  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;
  infer_service_conf.set_port(0);
  InferService *infer_service = infer_service_conf.add_services();
  infer_service->set_name("ImageClassifyService");
  infer_service->add_workflows("workflow1");
  infer_service->add_workflows("workflow2");

  infer_service = infer_service_conf.add_services();
  infer_service->set_name("BuiltinDenseFormatService");
  infer_service->add_workflows("workflow2");

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

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

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

  return 0;
W
wangguibao 已提交
201 202
}

W
wangguibao 已提交
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 243 244 245 246 247 248 249 250 251 252 253 254 255
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;
  }

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

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

  return 0;
}
W
wangguibao 已提交
256

W
wangguibao 已提交
257 258 259 260 261
int main() {
  int ret = 0;
  struct stat stat_buf;
  if (stat(output_dir, &stat_buf) != 0) {
    int ret = mkdir("./conf", 0777);
W
wangguibao 已提交
262
    if (ret != 0) {
W
wangguibao 已提交
263 264
      std::cout << "mkdir ./conf fail" << std::endl;
      return -1;
W
wangguibao 已提交
265
    }
W
wangguibao 已提交
266 267 268
    if (stat("./conf", &stat_buf) != 0) {
      std::cout << "./conf not exist and creating it failed" << std::endl;
      return -1;
269
    }
W
wangguibao 已提交
270
  }
271

W
wangguibao 已提交
272 273 274 275 276
  ret = test_write_conf();
  if (ret != 0) {
    std::cout << "test_write_conf fail" << std::endl;
    return -1;
  }
W
wangguibao 已提交
277

W
wangguibao 已提交
278
  std::cout << "test_write_conf success" << std::endl;
W
wangguibao 已提交
279

W
wangguibao 已提交
280 281 282 283 284 285
  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;
W
wangguibao 已提交
286

W
wangguibao 已提交
287
  return 0;
W
wangguibao 已提交
288 289 290
}

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