resource.h 2.7 KB
Newer Older
W
wangguibao 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
// 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.

#pragma once
X
xulongteng 已提交
16
#include <memory>
W
wangguibao 已提交
17
#include <string>
G
guru4elephant 已提交
18
#include <vector>
X
xulongteng 已提交
19
#include "cube/cube-api/include/cube_api.h"
20
#include "kvdb/paddle_rocksdb.h"
W
wangguibao 已提交
21
#include "predictor/common/inner_common.h"
W
wangguibao 已提交
22
#include "predictor/framework/infer.h"
W
wangguibao 已提交
23
#include "predictor/framework/memory.h"
W
wangguibao 已提交
24 25 26 27 28

namespace baidu {
namespace paddle_serving {
namespace predictor {

G
guru4elephant 已提交
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
class PaddleGeneralModelConfig {
  PaddleGeneralModelConfig();

  ~PaddleGeneralModelConfig();

  void load_config(std::string);

 public:
  
  std::vector<int> _feed_type;  // 0 int64, 1 float
  std::vector<bool> _is_lod_feed;  // true lod tensor
  std::vector<int> _capacity;  //  capacity for each tensor
  /*
    feed_shape_ for feeded variable
    feed_shape_[i][j] represents the jth dim for ith input Tensor
    if is_lod_feed_[i] == False, feed_shape_[i][0] = -1
   */
  std::vector<std::vector<int>> _feed_shape;
};

W
wangguibao 已提交
49 50
class BaseRdDict;
struct DynamicResource {
W
wangguibao 已提交
51 52 53 54 55 56 57
  DynamicResource();

  ~DynamicResource();

  int initialize();

  int clear();
W
wangguibao 已提交
58 59 60
};

class Resource {
W
wangguibao 已提交
61
 public:
W
wangguibao 已提交
62 63 64 65 66 67 68
  Resource() {
    // Reference InferManager::instance() explicitly, to make sure static
    // instance of InferManager is constructed before that of Resource, and
    // destruct after that of Resource
    // See https://stackoverflow.com/a/335746/1513460
    InferManager::instance();
  }
W
wangguibao 已提交
69

W
wangguibao 已提交
70
  ~Resource() { finalize(); }
W
wangguibao 已提交
71

W
wangguibao 已提交
72 73 74 75
  static Resource& instance() {
    static Resource ins;
    return ins;
  }
W
wangguibao 已提交
76

W
wangguibao 已提交
77
  int initialize(const std::string& path, const std::string& file);
X
xulongteng 已提交
78
  int cube_initialize(const std::string& path, const std::string& file);
G
guru4elephant 已提交
79 80 81 82

  int general_model_initialize(
      const std::string& path, const std::string & file);

W
wangguibao 已提交
83
  int thread_initialize();
W
wangguibao 已提交
84

W
wangguibao 已提交
85
  int thread_clear();
W
wangguibao 已提交
86

W
wangguibao 已提交
87
  int reload();
W
wangguibao 已提交
88

W
wangguibao 已提交
89
  int finalize();
W
wangguibao 已提交
90

W
wangjiawei04 已提交
91 92
  std::shared_ptr<RocksDBWrapper> getDB();

W
wangguibao 已提交
93 94 95 96
  DynamicResource* get_dynamic_resource() {
    return reinterpret_cast<DynamicResource*>(
        THREAD_GETSPECIFIC(_tls_bspec_key));
  }
W
wangguibao 已提交
97

W
wangguibao 已提交
98 99
 private:
  int thread_finalize() { return 0; }
W
wangjiawei04 已提交
100
  std::shared_ptr<RocksDBWrapper> db;
G
guru4elephant 已提交
101
  std::shared_ptr<PaddleGeneralModelConfig> _config;
102

W
wangguibao 已提交
103
  THREAD_KEY_T _tls_bspec_key;
W
wangguibao 已提交
104 105
};

W
wangguibao 已提交
106 107 108
}  // namespace predictor
}  // namespace paddle_serving
}  // namespace baidu