resource.cpp 12.5 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.

G
guru4elephant 已提交
15
#include "core/predictor/framework/resource.h"
G
guru4elephant 已提交
16
#include <sstream>
W
wangguibao 已提交
17
#include <string>
G
guru4elephant 已提交
18 19
#include "core/predictor/common/inner_common.h"
#include "core/predictor/framework/kv_manager.h"
20 21 22
#ifdef BCLOUD
#include "aipe_sec_client.h"  // NOLINT
#endif
W
wangguibao 已提交
23 24 25 26
namespace baidu {
namespace paddle_serving {
namespace predictor {

W
wangguibao 已提交
27
using configure::ResourceConf;
G
guru4elephant 已提交
28
using configure::GeneralModelConfig;
X
xulongteng 已提交
29
using rec::mcube::CubeAPI;
W
wangguibao 已提交
30

31 32
std::vector<std::shared_ptr<PaddleGeneralModelConfig>>
Resource::get_general_model_config() {
H
HexToString 已提交
33
  return _configs;
G
guru4elephant 已提交
34 35 36
}

void Resource::print_general_model_config(
37
    const std::shared_ptr<PaddleGeneralModelConfig>& config) {
G
guru4elephant 已提交
38 39 40 41
  if (config == nullptr) {
    LOG(INFO) << "paddle general model config is not set";
    return;
  }
42
  LOG(INFO) << "Number of Feed Tensor: " << config->_feed_name.size();
G
guru4elephant 已提交
43
  std::ostringstream oss;
44 45 46 47 48 49 50
  LOG(INFO) << "Feed Name Info";
  for (auto& feed_name : config->_feed_name) {
    oss << feed_name << " ";
  }
  LOG(INFO) << oss.str();
  oss.clear();
  oss.str("");
G
guru4elephant 已提交
51
  LOG(INFO) << "Feed Type Info";
52
  for (auto& feed_type : config->_feed_type) {
G
guru4elephant 已提交
53 54 55 56 57 58 59 60 61 62 63 64 65 66 67
    oss << feed_type << " ";
  }
  LOG(INFO) << oss.str();
  oss.clear();
  oss.str("");
  LOG(INFO) << "Lod Type Info";

  for (auto is_lod : config->_is_lod_feed) {
    oss << is_lod << " ";
  }

  LOG(INFO) << oss.str();
  oss.clear();
  oss.str("");
  LOG(INFO) << "Capacity Info";
68
  for (auto& cap : config->_capacity) {
G
guru4elephant 已提交
69 70 71 72 73 74 75
    oss << cap << " ";
  }
  LOG(INFO) << oss.str();
  oss.clear();
  oss.str("");
  LOG(INFO) << "Feed Shape Info";
  int tensor_idx = 0;
76 77
  for (auto& shape : config->_feed_shape) {
    for (auto& dim : shape) {
G
guru4elephant 已提交
78 79 80 81 82 83 84 85
      oss << dim << " ";
    }
    LOG(INFO) << "Tensor[" << tensor_idx++ << "].shape: " << oss.str();
    oss.clear();
    oss.str("");
  }
}

W
wangguibao 已提交
86
int Resource::initialize(const std::string& path, const std::string& file) {
W
wangguibao 已提交
87 88 89 90 91 92 93 94 95 96 97 98 99
  ResourceConf resource_conf;
  if (configure::read_proto_conf(path, file, &resource_conf) != 0) {
    LOG(ERROR) << "Failed initialize resource from: " << path << "/" << file;
    return -1;
  }

  // mempool
  if (MempoolWrapper::instance().initialize() != 0) {
    LOG(ERROR) << "Failed proc initialized mempool wrapper";
    return -1;
  }
  LOG(WARNING) << "Successfully proc initialized mempool wrapper";

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
#ifdef WITH_AUTH
  std::string product_name_str = resource_conf.auth_product_name();
  std::string container_id_str = resource_conf.auth_container_id();

  char* product_name = new char[product_name_str.size() + 1];
  snprintf(product_name,
           product_name_str.size() + 1,
           "%s",
           product_name_str.c_str());
  char* container_id = new char[container_id_str.size() + 1];
  snprintf(container_id,
           container_id_str.size() + 1,
           "%s",
           container_id_str.c_str());

  aipe_auth_request request;
  request.product_name = product_name;
  request.container_id = container_id;
  request.request_ts = (int64_t)time(NULL);

  LOG(INFO) << "\nEasypack info"
            << "\nproduct name: " << request.product_name
            << "\ncontainer_id: " << request.container_id
            << "\nrequest time stamp: " << request.request_ts;

  aipe_auth_response response;
  response = check_auth(request);

  if (response.result == 0) {
    LOG(INFO) << "Authentication succeed.";
  } else {
    LOG(ERROR) << "Authentication failed. Error code: " << response.result;
    return -1;
  }
#endif

W
wangguibao 已提交
136
  if (FLAGS_enable_model_toolkit) {
H
HexToString 已提交
137
    size_t model_toolkit_num = resource_conf.model_toolkit_path_size();
H
HexToString 已提交
138 139 140 141 142 143 144 145 146 147 148
    // 此处暂时认为,每个model_toolkit仅包含一个engine
    // 故认为 model_toolkit_num == engine总数
    // 若以后出现model_toolkit仅包含多个engine
    // 则应先for循环统计engine总数,再set_taskexecutor_num
    // 切不可动态im::bsf::TaskExecutorVector<TaskT>::instance().resize
    // TaskExecutor是线程池,内含锁,在engine进程初始化时已开始work加锁循环运行了
    // 之后再resize内存搬运,会导致work使用原锁,而搬运后的TaskExecutor的锁内存已改变
    if (InferManager::instance().set_taskexecutor_num(model_toolkit_num) != 0) {
      LOG(ERROR) << "failed set_taskexecutor_num";
      return -1;
    }
H
HexToString 已提交
149
    std::shared_ptr<int> engine_index_ptr(new int(0));
150
    for (size_t mi = 0; mi < model_toolkit_num; ++mi) {
H
HexToString 已提交
151 152 153
      std::string model_toolkit_path = resource_conf.model_toolkit_path(mi);
      std::string model_toolkit_file = resource_conf.model_toolkit_file(mi);

H
HexToString 已提交
154 155 156
      if (InferManager::instance().proc_initialize(model_toolkit_path.c_str(),
                                                   model_toolkit_file.c_str(),
                                                   engine_index_ptr) != 0) {
H
HexToString 已提交
157
        LOG(ERROR) << "failed proc initialize modeltoolkit, config: "
158
                   << model_toolkit_path << "/" << model_toolkit_file;
H
HexToString 已提交
159 160 161 162 163 164
        return -1;
      }

      if (KVManager::instance().proc_initialize(
              model_toolkit_path.c_str(), model_toolkit_file.c_str()) != 0) {
        LOG(ERROR) << "Failed proc initialize kvmanager, config: "
165
                   << model_toolkit_path << "/" << model_toolkit_file;
H
HexToString 已提交
166
      }
W
wangguibao 已提交
167
    }
W
wangguibao 已提交
168 169
  }

W
wangjiawei04 已提交
170
  // init rocksDB or cube instance
W
wangjiawei04 已提交
171 172
  if (resource_conf.has_cube_config_file() &&
      resource_conf.has_cube_config_path()) {
W
wangjiawei04 已提交
173
    LOG(INFO) << "init cube client, path[ " << resource_conf.cube_config_path()
W
wangjiawei04 已提交
174 175 176 177 178
              << " ], config file [ " << resource_conf.cube_config_file()
              << " ].";
    rec::mcube::CubeAPI* cube = rec::mcube::CubeAPI::instance();
    std::string cube_config_fullpath = "./" + resource_conf.cube_config_path() +
                                       "/" + resource_conf.cube_config_file();
T
TeslaZhao 已提交
179 180 181 182 183
    this->_cube_config_fullpath = cube_config_fullpath;
    this->_cube_quant_bits = resource_conf.has_cube_quant_bits()
                                 ? resource_conf.cube_quant_bits()
                                 : 0;
    if (this->_cube_quant_bits != 0 && this->_cube_quant_bits != 8) {
W
wangjiawei04 已提交
184 185 186
      LOG(ERROR) << "Cube quant bits illegal! should be 0 or 8.";
      return -1;
    }
T
TeslaZhao 已提交
187
    if (this->_cube_quant_bits == 0) {
W
wangjiawei04 已提交
188 189
      LOG(INFO) << "cube quant mode OFF";
    } else {
T
TeslaZhao 已提交
190
      LOG(INFO) << "cube quant mode ON, quant bits: " << this->_cube_quant_bits;
W
wangjiawei04 已提交
191
    }
W
wangjiawei04 已提交
192
  }
W
wangjiawei04 已提交
193

W
wangguibao 已提交
194 195
  THREAD_SETSPECIFIC(_tls_bspec_key, NULL);
  return 0;
W
wangguibao 已提交
196 197
}

198 199 200
// model config
int Resource::general_model_initialize(const std::string& path,
                                       const std::string& file) {
T
TeslaZhao 已提交
201 202
  if (this->_cube_config_fullpath.size() != 0) {
    LOG(INFO) << "init cube by config file : " << this->_cube_config_fullpath;
W
wangjiawei04 已提交
203
    rec::mcube::CubeAPI* cube = rec::mcube::CubeAPI::instance();
T
TeslaZhao 已提交
204
    int ret = cube->init(this->_cube_config_fullpath.c_str());
W
wangjiawei04 已提交
205 206 207 208
    if (ret != 0) {
      LOG(ERROR) << "cube init error";
      return -1;
    }
W
wangjiawei04 已提交
209
  }
210 211
  VLOG(2) << "general model path: " << path;
  VLOG(2) << "general model file: " << file;
G
guru4elephant 已提交
212
  if (!FLAGS_enable_general_model) {
213 214
    LOG(ERROR) << "general model is not enabled";
    return -1;
G
guru4elephant 已提交
215
  }
216 217 218 219 220
  ResourceConf resource_conf;
  if (configure::read_proto_conf(path, file, &resource_conf) != 0) {
    LOG(ERROR) << "Failed initialize resource from: " << path << "/" << file;
    return -1;
  }
H
HexToString 已提交
221
  size_t general_model_num = resource_conf.general_model_path_size();
222
  for (size_t gi = 0; gi < general_model_num; ++gi) {
H
HexToString 已提交
223 224 225 226 227
    std::string general_model_path = resource_conf.general_model_path(gi);
    std::string general_model_file = resource_conf.general_model_file(gi);

    GeneralModelConfig model_config;
    if (configure::read_proto_conf(general_model_path.c_str(),
228 229 230 231
                                   general_model_file.c_str(),
                                   &model_config) != 0) {
      LOG(ERROR) << "Failed initialize model config from: "
                 << general_model_path << "/" << general_model_file;
H
HexToString 已提交
232 233 234 235 236 237 238 239 240 241 242 243
      return -1;
    }
    auto _config = std::make_shared<PaddleGeneralModelConfig>();
    int feed_var_num = model_config.feed_var_size();
    VLOG(2) << "load general model config";
    VLOG(2) << "feed var num: " << feed_var_num;
    _config->_feed_name.resize(feed_var_num);
    _config->_feed_alias_name.resize(feed_var_num);
    _config->_feed_type.resize(feed_var_num);
    _config->_is_lod_feed.resize(feed_var_num);
    _config->_capacity.resize(feed_var_num);
    _config->_feed_shape.resize(feed_var_num);
244
    for (int i = 0; i < feed_var_num; ++i) {
H
HexToString 已提交
245 246 247 248 249 250 251 252 253 254 255 256 257 258 259
      _config->_feed_name[i] = model_config.feed_var(i).name();
      _config->_feed_alias_name[i] = model_config.feed_var(i).alias_name();
      VLOG(2) << "feed var[" << i << "]: " << _config->_feed_name[i];
      VLOG(2) << "feed var[" << i << "]: " << _config->_feed_alias_name[i];
      _config->_feed_type[i] = model_config.feed_var(i).feed_type();
      VLOG(2) << "feed type[" << i << "]: " << _config->_feed_type[i];

      if (model_config.feed_var(i).is_lod_tensor()) {
        VLOG(2) << "var[" << i << "] is lod tensor";
        _config->_feed_shape[i] = {-1};
        _config->_is_lod_feed[i] = true;
      } else {
        VLOG(2) << "var[" << i << "] is tensor";
        _config->_capacity[i] = 1;
        _config->_is_lod_feed[i] = false;
260
        for (int j = 0; j < model_config.feed_var(i).shape_size(); ++j) {
H
HexToString 已提交
261 262 263 264 265
          int32_t dim = model_config.feed_var(i).shape(j);
          VLOG(2) << "var[" << i << "].shape[" << i << "]: " << dim;
          _config->_feed_shape[i].push_back(dim);
          _config->_capacity[i] *= dim;
        }
G
guru4elephant 已提交
266 267
      }
    }
268

H
HexToString 已提交
269 270 271 272 273
    int fetch_var_num = model_config.fetch_var_size();
    _config->_is_lod_fetch.resize(fetch_var_num);
    _config->_fetch_name.resize(fetch_var_num);
    _config->_fetch_alias_name.resize(fetch_var_num);
    _config->_fetch_shape.resize(fetch_var_num);
274
    for (int i = 0; i < fetch_var_num; ++i) {
H
HexToString 已提交
275 276 277 278 279 280 281 282 283 284
      _config->_fetch_name[i] = model_config.fetch_var(i).name();
      _config->_fetch_alias_name[i] = model_config.fetch_var(i).alias_name();
      _config->_fetch_name_to_index[_config->_fetch_name[i]] = i;
      _config->_fetch_alias_name_to_index[_config->_fetch_alias_name[i]] = i;
      if (model_config.fetch_var(i).is_lod_tensor()) {
        VLOG(2) << "fetch var[" << i << "] is lod tensor";
        _config->_fetch_shape[i] = {-1};
        _config->_is_lod_fetch[i] = true;
      } else {
        _config->_is_lod_fetch[i] = false;
285
        for (int j = 0; j < model_config.fetch_var(i).shape_size(); ++j) {
H
HexToString 已提交
286 287 288
          int dim = model_config.fetch_var(i).shape(j);
          _config->_fetch_shape[i].push_back(dim);
        }
289
      }
290
    }
H
HexToString 已提交
291
    _configs.push_back(std::move(_config));
292
  }
G
guru4elephant 已提交
293
  return 0;
G
guru4elephant 已提交
294 295
}

W
wangguibao 已提交
296
int Resource::thread_initialize() {
W
wangguibao 已提交
297 298 299 300 301 302 303 304 305 306 307 308 309 310 311
  // mempool
  if (MempoolWrapper::instance().thread_initialize() != 0) {
    LOG(ERROR) << "Failed thread initialized mempool wrapper";
    return -1;
  }
  LOG(WARNING) << "Successfully thread initialized mempool wrapper";

  // infer manager
  if (FLAGS_enable_model_toolkit &&
      InferManager::instance().thrd_initialize() != 0) {
    LOG(ERROR) << "Failed thrd initialized infer manager";
    return -1;
  }

  return 0;
W
wangguibao 已提交
312 313 314
}

int Resource::thread_clear() {
W
wangguibao 已提交
315 316 317 318 319 320 321 322 323 324 325 326 327
  // mempool
  if (MempoolWrapper::instance().thread_clear() != 0) {
    LOG(ERROR) << "Failed thread clear mempool wrapper";
    return -1;
  }

  // infer manager
  if (FLAGS_enable_model_toolkit &&
      InferManager::instance().thrd_clear() != 0) {
    LOG(ERROR) << "Failed thrd clear infer manager";
    return -1;
  }
  return 0;
W
wangguibao 已提交
328
}
T
TeslaZhao 已提交
329
size_t Resource::get_cube_quant_bits() { return this->_cube_quant_bits; }
W
wangguibao 已提交
330 331

int Resource::reload() {
W
wangguibao 已提交
332 333 334 335 336 337 338
  if (FLAGS_enable_model_toolkit && InferManager::instance().reload() != 0) {
    LOG(ERROR) << "Failed reload infer manager";
    return -1;
  }

  // other resource reload here...
  return 0;
W
wangguibao 已提交
339 340 341
}

int Resource::finalize() {
W
wangguibao 已提交
342 343 344 345 346
  if (FLAGS_enable_model_toolkit &&
      InferManager::instance().proc_finalize() != 0) {
    LOG(ERROR) << "Failed proc finalize infer manager";
    return -1;
  }
X
xulongteng 已提交
347 348 349 350
  if (CubeAPI::instance()->destroy() != 0) {
    LOG(ERROR) << "Destory cube api failed ";
    return -1;
  }
W
wangguibao 已提交
351
  THREAD_KEY_DELETE(_tls_bspec_key);
W
wangguibao 已提交
352

W
wangguibao 已提交
353
  return 0;
W
wangguibao 已提交
354 355
}

W
wangguibao 已提交
356 357 358
}  // namespace predictor
}  // namespace paddle_serving
}  // namespace baidu