general_model.cpp 17.5 KB
Newer Older
G
guru4elephant 已提交
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.

G
guru4elephant 已提交
15
#include "core/general-client/include/general_model.h"
M
MRXLT 已提交
16
#include <fstream>
G
guru4elephant 已提交
17 18 19
#include "core/sdk-cpp/builtin_format.pb.h"
#include "core/sdk-cpp/include/common.h"
#include "core/sdk-cpp/include/predictor_sdk.h"
G
guru4elephant 已提交
20
#include "core/util/include/timer.h"
21 22 23
DEFINE_bool(profile_client, false, "");
DEFINE_bool(profile_server, false, "");

G
guru4elephant 已提交
24
using baidu::paddle_serving::Timer;
G
guru4elephant 已提交
25 26 27
using baidu::paddle_serving::predictor::general_model::Request;
using baidu::paddle_serving::predictor::general_model::Response;
using baidu::paddle_serving::predictor::general_model::Tensor;
28
// support: FLOAT32, INT64, INT32, UINT8, INT8, FLOAT16
S
ShiningZhang 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41
enum ProtoDataType {
  P_INT64 = 0,
  P_FLOAT32,
  P_INT32,
  P_FP64,
  P_INT16,
  P_FP16,
  P_BF16,
  P_UINT8,
  P_INT8,
  P_BOOL,
  P_COMPLEX64,
  P_COMPLEX128,
S
ShiningZhang 已提交
42
  P_STRING = 20,
S
ShiningZhang 已提交
43
};
44
std::once_flag gflags_init_flag;
M
MRXLT 已提交
45
namespace py = pybind11;
46

G
guru4elephant 已提交
47 48 49
namespace baidu {
namespace paddle_serving {
namespace general_model {
50
using configure::GeneralModelConfig;
G
guru4elephant 已提交
51

52 53
void PredictorClient::init_gflags(std::vector<std::string> argv) {
  std::call_once(gflags_init_flag, [&]() {
54
#ifndef BCLOUD
M
MRXLT 已提交
55
    FLAGS_logtostderr = true;
56
#endif
M
MRXLT 已提交
57 58 59 60
    argv.insert(argv.begin(), "dummy");
    int argc = argv.size();
    char **arr = new char *[argv.size()];
    std::string line;
H
HexToString 已提交
61
    for (size_t i = 0; i < argv.size(); ++i) {
M
MRXLT 已提交
62 63 64 65 66 67 68
      arr[i] = &argv[i][0];
      line += argv[i];
      line += ' ';
    }
    google::ParseCommandLineFlags(&argc, &arr, true);
    VLOG(2) << "Init commandline: " << line;
  });
69 70
}

H
HexToString 已提交
71
int PredictorClient::init(const std::vector<std::string> &conf_file) {
72 73
  try {
    GeneralModelConfig model_config;
H
HexToString 已提交
74
    if (configure::read_proto_conf(conf_file[0].c_str(), &model_config) != 0) {
75
      LOG(ERROR) << "Failed to load general model config"
H
HexToString 已提交
76
                 << ", file path: " << conf_file[0];
77 78
      return -1;
    }
H
HexToString 已提交
79

80 81 82 83
    _feed_name_to_idx.clear();
    _fetch_name_to_idx.clear();
    _shape.clear();
    int feed_var_num = model_config.feed_var_size();
H
HexToString 已提交
84
    _feed_name.clear();
H
HexToString 已提交
85
    VLOG(2) << "feed var num: " << feed_var_num;
86 87
    for (int i = 0; i < feed_var_num; ++i) {
      _feed_name_to_idx[model_config.feed_var(i).alias_name()] = i;
H
HexToString 已提交
88 89 90
      VLOG(2) << "feed [" << i << "]"
              << " name: " << model_config.feed_var(i).name();
      _feed_name.push_back(model_config.feed_var(i).name());
91 92
      VLOG(2) << "feed alias name: " << model_config.feed_var(i).alias_name()
              << " index: " << i;
93
      std::vector<int> tmp_feed_shape;
M
MRXLT 已提交
94 95
      VLOG(2) << "feed"
              << "[" << i << "] shape:";
96 97
      for (int j = 0; j < model_config.feed_var(i).shape_size(); ++j) {
        tmp_feed_shape.push_back(model_config.feed_var(i).shape(j));
M
MRXLT 已提交
98
        VLOG(2) << "shape[" << j << "]: " << model_config.feed_var(i).shape(j);
99 100
      }
      _type.push_back(model_config.feed_var(i).feed_type());
M
MRXLT 已提交
101 102 103
      VLOG(2) << "feed"
              << "[" << i
              << "] feed type: " << model_config.feed_var(i).feed_type();
104
      _shape.push_back(tmp_feed_shape);
G
guru4elephant 已提交
105 106
    }

H
HexToString 已提交
107
    if (conf_file.size() > 1) {
H
HexToString 已提交
108
      model_config.Clear();
H
HexToString 已提交
109 110
      if (configure::read_proto_conf(conf_file[conf_file.size() - 1].c_str(),
                                     &model_config) != 0) {
H
HexToString 已提交
111
        LOG(ERROR) << "Failed to load general model config"
H
HexToString 已提交
112
                   << ", file path: " << conf_file[conf_file.size() - 1];
H
HexToString 已提交
113 114 115 116 117
        return -1;
      }
    }
    int fetch_var_num = model_config.fetch_var_size();
    VLOG(2) << "fetch_var_num: " << fetch_var_num;
118 119
    for (int i = 0; i < fetch_var_num; ++i) {
      _fetch_name_to_idx[model_config.fetch_var(i).alias_name()] = i;
M
MRXLT 已提交
120 121
      VLOG(2) << "fetch [" << i << "]"
              << " alias name: " << model_config.fetch_var(i).alias_name();
122 123
      _fetch_name_to_var_name[model_config.fetch_var(i).alias_name()] =
          model_config.fetch_var(i).name();
124 125
      _fetch_name_to_type[model_config.fetch_var(i).alias_name()] =
          model_config.fetch_var(i).fetch_type();
126
    }
M
MRXLT 已提交
127
  } catch (std::exception &e) {
128 129
    LOG(ERROR) << "Failed load general model config" << e.what();
    return -1;
G
guru4elephant 已提交
130
  }
131
  return 0;
G
guru4elephant 已提交
132 133
}

M
MRXLT 已提交
134 135
void PredictorClient::set_predictor_conf(const std::string &conf_path,
                                         const std::string &conf_file) {
G
guru4elephant 已提交
136 137 138
  _predictor_path = conf_path;
  _predictor_conf = conf_file;
}
139 140 141
int PredictorClient::destroy_predictor() {
  _api.thrd_finalize();
  _api.destroy();
B
barrierye 已提交
142
  return 0;
143 144
}

M
MRXLT 已提交
145
int PredictorClient::create_predictor_by_desc(const std::string &sdk_desc) {
G
guru4elephant 已提交
146 147 148 149
  if (_api.create(sdk_desc) != 0) {
    LOG(ERROR) << "Predictor Creation Failed";
    return -1;
  }
D
dongdaxiang 已提交
150
  // _api.thrd_initialize();
B
barrierye 已提交
151
  return 0;
G
guru4elephant 已提交
152 153
}

G
guru4elephant 已提交
154
int PredictorClient::create_predictor() {
G
guru4elephant 已提交
155 156
  VLOG(2) << "Predictor path: " << _predictor_path
          << " predictor file: " << _predictor_conf;
G
guru4elephant 已提交
157 158 159 160
  if (_api.create(_predictor_path.c_str(), _predictor_conf.c_str()) != 0) {
    LOG(ERROR) << "Predictor Creation Failed";
    return -1;
  }
D
dongdaxiang 已提交
161
  // _api.thrd_initialize();
B
barrierye 已提交
162
  return 0;
G
guru4elephant 已提交
163 164
}

M
MRXLT 已提交
165
int PredictorClient::numpy_predict(
H
HexToString 已提交
166
    const std::vector<py::array_t<float>> &float_feed,
M
MRXLT 已提交
167 168
    const std::vector<std::string> &float_feed_name,
    const std::vector<std::vector<int>> &float_shape,
W
wangjiawei04 已提交
169
    const std::vector<std::vector<int>> &float_lod_slot_batch,
170 171 172 173 174 175 176 177
    const std::vector<py::array_t<int32_t>> &int32_feed,
    const std::vector<std::string> &int32_feed_name,
    const std::vector<std::vector<int>> &int32_shape,
    const std::vector<std::vector<int>> &int32_lod_slot_batch,
    const std::vector<py::array_t<int64_t>> &int64_feed,
    const std::vector<std::string> &int64_feed_name,
    const std::vector<std::vector<int>> &int64_shape,
    const std::vector<std::vector<int>> &int64_lod_slot_batch,
H
HexToString 已提交
178
    const std::vector<std::string> &string_feed,
H
HexToString 已提交
179 180 181
    const std::vector<std::string> &string_feed_name,
    const std::vector<std::vector<int>> &string_shape,
    const std::vector<std::vector<int>> &string_lod_slot_batch,
M
MRXLT 已提交
182 183
    const std::vector<std::string> &fetch_name,
    PredictorRes &predict_res_batch,
184 185
    const int &pid,
    const uint64_t log_id) {
M
MRXLT 已提交
186 187 188 189 190 191 192 193 194 195
  predict_res_batch.clear();
  Timer timeline;
  int64_t preprocess_start = timeline.TimeStampUS();

  _api.thrd_initialize();
  std::string variant_tag;
  _predictor = _api.fetch_predictor("general_model", &variant_tag);
  predict_res_batch.set_variant_tag(variant_tag);
  VLOG(2) << "fetch general model predictor done.";
  VLOG(2) << "float feed name size: " << float_feed_name.size();
196 197
  VLOG(2) << "int feed name size: " << int32_feed_name.size();
  VLOG(2) << "int feed name size: " << int64_feed_name.size();
H
HexToString 已提交
198
  VLOG(2) << "string feed name size: " << string_feed_name.size();
M
MRXLT 已提交
199 200
  VLOG(2) << "max body size : " << brpc::fLU64::FLAGS_max_body_size;
  Request req;
201
  req.set_log_id(log_id);
M
MRXLT 已提交
202 203 204 205
  for (auto &name : fetch_name) {
    req.add_fetch_var_names(name);
  }

H
HexToString 已提交
206
  int vec_idx = 0;
H
HexToString 已提交
207 208
  // batch is already in Tensor.
  std::vector<Tensor *> tensor_vec;
M
MRXLT 已提交
209

H
HexToString 已提交
210 211 212
  for (auto &name : float_feed_name) {
    tensor_vec.push_back(req.add_tensor());
  }
H
HexToString 已提交
213

214 215 216 217 218
  for (auto &name : int32_feed_name) {
    tensor_vec.push_back(req.add_tensor());
  }

  for (auto &name : int64_feed_name) {
H
HexToString 已提交
219 220
    tensor_vec.push_back(req.add_tensor());
  }
M
MRXLT 已提交
221

H
HexToString 已提交
222 223 224
  for (auto &name : string_feed_name) {
    tensor_vec.push_back(req.add_tensor());
  }
H
HexToString 已提交
225

H
HexToString 已提交
226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245
  vec_idx = 0;
  for (auto &name : float_feed_name) {
    int idx = _feed_name_to_idx[name];
    if (idx >= tensor_vec.size()) {
      LOG(ERROR) << "idx > tensor_vec.size()";
      return -1;
    }
    VLOG(2) << "prepare float feed " << name << " idx " << idx;
    int nbytes = float_feed[vec_idx].nbytes();
    void *rawdata_ptr = (void *)(float_feed[vec_idx].data(0));
    int total_number = float_feed[vec_idx].size();
    Tensor *tensor = tensor_vec[idx];

    VLOG(2) << "prepare float feed " << name << " shape size "
            << float_shape[vec_idx].size();
    for (uint32_t j = 0; j < float_shape[vec_idx].size(); ++j) {
      tensor->add_shape(float_shape[vec_idx][j]);
    }
    for (uint32_t j = 0; j < float_lod_slot_batch[vec_idx].size(); ++j) {
      tensor->add_lod(float_lod_slot_batch[vec_idx][j]);
M
MRXLT 已提交
246
    }
H
HexToString 已提交
247
    tensor->set_elem_type(P_FLOAT32);
H
HexToString 已提交
248

H
HexToString 已提交
249 250
    tensor->set_name(_feed_name[idx]);
    tensor->set_alias_name(name);
M
MRXLT 已提交
251

H
HexToString 已提交
252 253 254 255
    tensor->mutable_float_data()->Resize(total_number, 0);
    memcpy(tensor->mutable_float_data()->mutable_data(), rawdata_ptr, nbytes);
    vec_idx++;
  }
M
MRXLT 已提交
256

H
HexToString 已提交
257
  vec_idx = 0;
258
  for (auto &name : int32_feed_name) {
H
HexToString 已提交
259 260 261 262
    int idx = _feed_name_to_idx[name];
    if (idx >= tensor_vec.size()) {
      LOG(ERROR) << "idx > tensor_vec.size()";
      return -1;
M
MRXLT 已提交
263
    }
H
HexToString 已提交
264
    Tensor *tensor = tensor_vec[idx];
265 266 267
    int nbytes = int32_feed[vec_idx].nbytes();
    void *rawdata_ptr = (void *)(int32_feed[vec_idx].data(0));
    int total_number = int32_feed[vec_idx].size();
M
MRXLT 已提交
268

269 270
    for (uint32_t j = 0; j < int32_shape[vec_idx].size(); ++j) {
      tensor->add_shape(int32_shape[vec_idx][j]);
H
HexToString 已提交
271
    }
272 273
    for (uint32_t j = 0; j < int32_lod_slot_batch[vec_idx].size(); ++j) {
      tensor->add_lod(int32_lod_slot_batch[vec_idx][j]);
H
HexToString 已提交
274 275 276 277 278
    }
    tensor->set_elem_type(_type[idx]);
    tensor->set_name(_feed_name[idx]);
    tensor->set_alias_name(name);

279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302
    tensor->mutable_int_data()->Resize(total_number, 0);
    memcpy(tensor->mutable_int_data()->mutable_data(), rawdata_ptr, nbytes);
    vec_idx++;
  }


  // Individual INT_64 feed data of int_input to tensor_content
  vec_idx = 0;
  for (auto &name : int64_feed_name) {
    int idx = _feed_name_to_idx[name];
    if (idx >= tensor_vec.size()) {
      LOG(ERROR) << "idx > tensor_vec.size()";
      return -1;
    }
    Tensor *tensor = tensor_vec[idx];
    int nbytes = int64_feed[vec_idx].nbytes();
    void *rawdata_ptr = (void *)(int64_feed[vec_idx].data(0));
    int total_number = int64_feed[vec_idx].size();

    for (uint32_t j = 0; j < int64_shape[vec_idx].size(); ++j) {
      tensor->add_shape(int64_shape[vec_idx][j]);
    }
    for (uint32_t j = 0; j < int64_lod_slot_batch[vec_idx].size(); ++j) {
      tensor->add_lod(int64_lod_slot_batch[vec_idx][j]);
H
HexToString 已提交
303
    }
304 305 306 307 308 309
    tensor->set_elem_type(_type[idx]);
    tensor->set_name(_feed_name[idx]);
    tensor->set_alias_name(name);

    tensor->mutable_int64_data()->Resize(total_number, 0);
    memcpy(tensor->mutable_int64_data()->mutable_data(), rawdata_ptr, nbytes);
H
HexToString 已提交
310 311
    vec_idx++;
  }
H
HexToString 已提交
312

S
ShiningZhang 已提交
313 314
  // Add !P_STRING feed data of string_input to tensor_content
  // UINT8 INT8 FLOAT16
H
HexToString 已提交
315 316 317 318 319 320 321
  vec_idx = 0;
  for (auto &name : string_feed_name) {
    int idx = _feed_name_to_idx[name];
    if (idx >= tensor_vec.size()) {
      LOG(ERROR) << "idx > tensor_vec.size()";
      return -1;
    }
S
ShiningZhang 已提交
322 323 324 325 326 327 328 329 330 331 332 333
    Tensor *tensor = tensor_vec[idx];

    for (uint32_t j = 0; j < string_shape[vec_idx].size(); ++j) {
      tensor->add_shape(string_shape[vec_idx][j]);
    }
    for (uint32_t j = 0; j < string_lod_slot_batch[vec_idx].size(); ++j) {
      tensor->add_lod(string_lod_slot_batch[vec_idx][j]);
    }
    tensor->set_name(_feed_name[idx]);
    tensor->set_alias_name(name);

    if (_type[idx] != P_STRING) {
334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
      tensor->set_elem_type(_type[idx]);
      tensor->set_tensor_content(string_feed[vec_idx]);
    } else {
      tensor->set_elem_type(P_STRING);
      const int string_shape_size = string_shape[vec_idx].size();
      // string_shape[vec_idx] = [1];cause numpy has no datatype of string.
      // we pass string via vector<vector<string> >.
      if (string_shape_size != 1) {
        LOG(ERROR) << "string_shape_size should be 1-D, but received is : "
                   << string_shape_size;
        return -1;
      }
      switch (string_shape_size) {
        case 1: {
          tensor->add_data(string_feed[vec_idx]);
          break;
        }
H
HexToString 已提交
351 352
      }
    }
H
HexToString 已提交
353
    vec_idx++;
M
MRXLT 已提交
354 355 356 357 358 359 360 361 362 363
  }

  int64_t preprocess_end = timeline.TimeStampUS();
  int64_t client_infer_start = timeline.TimeStampUS();
  Response res;

  int64_t client_infer_end = 0;
  int64_t postprocess_start = 0;
  int64_t postprocess_end = 0;

S
ShiningZhang 已提交
364 365
  if (FLAGS_profile_server) {
    req.set_profile_server(true);
M
MRXLT 已提交
366 367 368 369 370 371 372 373 374 375 376 377
  }

  res.Clear();
  if (_predictor->inference(&req, &res) != 0) {
    LOG(ERROR) << "failed call predictor with req: " << req.ShortDebugString();
    return -1;
  } else {
    client_infer_end = timeline.TimeStampUS();
    postprocess_start = client_infer_end;
    VLOG(2) << "get model output num";
    uint32_t model_num = res.outputs_size();
    VLOG(2) << "model num: " << model_num;
B
barrierye 已提交
378 379 380
    for (uint32_t m_idx = 0; m_idx < model_num; ++m_idx) {
      VLOG(2) << "process model output index: " << m_idx;
      auto output = res.outputs(m_idx);
B
barrierye 已提交
381 382
      ModelRes model;
      model.set_engine_name(output.engine_name());
H
HexToString 已提交
383 384 385
      // 在ResponseOp处,已经按照fetch_name对输出数据进行了处理
      // 所以,输出的数据与fetch_name是严格对应的,按顺序处理即可。
      for (int idx = 0; idx < output.tensor_size(); ++idx) {
B
barrierye 已提交
386
        // int idx = _fetch_name_to_idx[name];
H
HexToString 已提交
387 388
        const std::string name = output.tensor(idx).alias_name();
        model._tensor_alias_names.push_back(name);
H
HexToString 已提交
389
        int shape_size = output.tensor(idx).shape_size();
B
barrierye 已提交
390 391
        VLOG(2) << "fetch var " << name << " index " << idx << " shape size "
                << shape_size;
B
barrierye 已提交
392 393
        model._shape_map[name].resize(shape_size);
        for (int i = 0; i < shape_size; ++i) {
H
HexToString 已提交
394
          model._shape_map[name][i] = output.tensor(idx).shape(i);
B
barrierye 已提交
395
        }
H
HexToString 已提交
396
        int lod_size = output.tensor(idx).lod_size();
B
barrierye 已提交
397 398 399
        if (lod_size > 0) {
          model._lod_map[name].resize(lod_size);
          for (int i = 0; i < lod_size; ++i) {
H
HexToString 已提交
400
            model._lod_map[name][i] = output.tensor(idx).lod(i);
B
barrierye 已提交
401
          }
402 403
        }

H
HexToString 已提交
404
        if (_fetch_name_to_type[name] == P_INT64) {
M
MRXLT 已提交
405
          VLOG(2) << "ferch var " << name << "type int64";
H
HexToString 已提交
406
          int size = output.tensor(idx).int64_data_size();
W
WangXi 已提交
407
          model._int64_value_map[name] = std::vector<int64_t>(
H
HexToString 已提交
408 409
              output.tensor(idx).int64_data().begin(),
              output.tensor(idx).int64_data().begin() + size);
H
HexToString 已提交
410
        } else if (_fetch_name_to_type[name] == P_FLOAT32) {
B
barrierye 已提交
411
          VLOG(2) << "fetch var " << name << "type float";
H
HexToString 已提交
412
          int size = output.tensor(idx).float_data_size();
W
WangXi 已提交
413
          model._float_value_map[name] = std::vector<float>(
H
HexToString 已提交
414 415
              output.tensor(idx).float_data().begin(),
              output.tensor(idx).float_data().begin() + size);
H
HexToString 已提交
416
        } else if (_fetch_name_to_type[name] == P_INT32) {
M
MRXLT 已提交
417
          VLOG(2) << "fetch var " << name << "type int32";
H
HexToString 已提交
418
          int size = output.tensor(idx).int_data_size();
M
MRXLT 已提交
419
          model._int32_value_map[name] = std::vector<int32_t>(
H
HexToString 已提交
420 421
              output.tensor(idx).int_data().begin(),
              output.tensor(idx).int_data().begin() + size);
S
ShiningZhang 已提交
422 423 424 425 426 427 428 429 430
        } else if (_fetch_name_to_type[name] == P_UINT8) {
          VLOG(2) << "fetch var " << name << "type uint8";
          model._string_value_map[name] = output.tensor(idx).tensor_content();
        } else if (_fetch_name_to_type[name] == P_INT8) {
          VLOG(2) << "fetch var " << name << "type int8";
          model._string_value_map[name] = output.tensor(idx).tensor_content();
        } else if (_fetch_name_to_type[name] == P_FP16) {
          VLOG(2) << "fetch var " << name << "type float16";
          model._string_value_map[name] = output.tensor(idx).tensor_content();
M
MRXLT 已提交
431
        }
M
MRXLT 已提交
432
      }
B
barrierye 已提交
433
      predict_res_batch.add_model_res(std::move(model));
M
MRXLT 已提交
434
    }
435
    postprocess_end = timeline.TimeStampUS();
M
MRXLT 已提交
436 437
  }

M
MRXLT 已提交
438 439 440
  if (FLAGS_profile_client) {
    std::ostringstream oss;
    oss << "PROFILE\t"
M
MRXLT 已提交
441
        << "pid:" << pid << "\t"
M
MRXLT 已提交
442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458
        << "prepro_0:" << preprocess_start << " "
        << "prepro_1:" << preprocess_end << " "
        << "client_infer_0:" << client_infer_start << " "
        << "client_infer_1:" << client_infer_end << " ";
    if (FLAGS_profile_server) {
      int op_num = res.profile_time_size() / 2;
      for (int i = 0; i < op_num; ++i) {
        oss << "op" << i << "_0:" << res.profile_time(i * 2) << " ";
        oss << "op" << i << "_1:" << res.profile_time(i * 2 + 1) << " ";
      }
    }

    oss << "postpro_0:" << postprocess_start << " ";
    oss << "postpro_1:" << postprocess_end;

    fprintf(stderr, "%s\n", oss.str().c_str());
  }
D
dongdaxiang 已提交
459 460

  _api.thrd_clear();
S
ShiningZhang 已提交
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483

  std::ostringstream oss;
  oss << "[client]"
      << "logid=" << log_id <<",";
  if (FLAGS_profile_client) {
    double pre_cost = (preprocess_end - preprocess_start) / 1000.0;
    double infer_cost = (client_infer_end - client_infer_start) / 1000.0;
    double post_cost = (postprocess_end - postprocess_start) / 1000.0;
    oss << "client_pre_cost=" << pre_cost << "ms,"
        << "client_infer_cost=" << infer_cost << "ms,"
        << "client_post_cost=" << post_cost << "ms,";
  }
  double client_cost = (postprocess_end - preprocess_start) / 1000.0;
  oss << "client_cost=" << client_cost << "ms,";

  int op_num = res.profile_time_size() / 2;
  if (FLAGS_profile_server) {
    for (int i = 0; i < op_num - 1; ++i) {
      double t = (res.profile_time(i * 2 + 1)
                 - res.profile_time(i * 2)) / 1000.0;
      oss << "op" << i << "=" << t << "ms,";
    }
  }
484 485 486 487 488 489
  if (op_num > 0) {
    int i = op_num - 1;
    double server_cost = (res.profile_time(i * 2 + 1)
                 - res.profile_time(i * 2)) / 1000.0;
    oss << "server_cost=" << server_cost << "ms.";
  }
S
ShiningZhang 已提交
490
  LOG(INFO) << oss.str();
M
MRXLT 已提交
491
  return 0;
M
MRXLT 已提交
492
}
G
guru4elephant 已提交
493 494 495
}  // namespace general_model
}  // namespace paddle_serving
}  // namespace baidu