resource.h 3.2 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
B
barrierye 已提交
16
#include <map>
X
xulongteng 已提交
17
#include <memory>
W
wangguibao 已提交
18
#include <string>
G
guru4elephant 已提交
19
#include <vector>
G
guru4elephant 已提交
20 21 22 23
#include "core/cube/cube-api/include/cube_api.h"
#include "core/predictor/common/inner_common.h"
#include "core/predictor/framework/infer.h"
#include "core/predictor/framework/memory.h"
W
wangguibao 已提交
24 25 26 27 28

namespace baidu {
namespace paddle_serving {
namespace predictor {

G
guru4elephant 已提交
29
class PaddleGeneralModelConfig {
G
guru4elephant 已提交
30 31
 public:
  PaddleGeneralModelConfig() {}
G
guru4elephant 已提交
32

G
guru4elephant 已提交
33
  ~PaddleGeneralModelConfig() {}
G
guru4elephant 已提交
34 35

 public:
36
  std::vector<std::string> _feed_name;
37
  std::vector<std::string> _feed_alias_name;
B
barrierye 已提交
38 39
  std::vector<int> _feed_type;      // 0 int64, 1 float
  std::vector<bool> _is_lod_feed;   // true lod tensor
40
  std::vector<bool> _is_lod_fetch;  // whether a fetch var is lod_tensor
B
barrierye 已提交
41 42 43 44 45 46
  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
                                     */
G
guru4elephant 已提交
47
  std::vector<std::vector<int>> _feed_shape;
48 49

  std::vector<std::string> _fetch_name;
50
  std::vector<std::string> _fetch_alias_name;
51
  std::vector<std::vector<int>> _fetch_shape;
52 53
  std::map<std::string, int> _fetch_name_to_index;
  std::map<std::string, int> _fetch_alias_name_to_index;
G
guru4elephant 已提交
54 55
};

W
wangguibao 已提交
56 57 58
class BaseRdDict;

class Resource {
W
wangguibao 已提交
59
 public:
W
wangguibao 已提交
60 61 62 63 64 65 66
  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 已提交
67

W
wangguibao 已提交
68
  ~Resource() { finalize(); }
W
wangguibao 已提交
69

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

W
wangguibao 已提交
75
  int initialize(const std::string& path, const std::string& file);
G
guru4elephant 已提交
76

77 78
  int general_model_initialize(const std::string& path,
                               const std::string& file);
G
guru4elephant 已提交
79

W
wangguibao 已提交
80
  int thread_initialize();
W
wangguibao 已提交
81

W
wangguibao 已提交
82
  int thread_clear();
W
wangguibao 已提交
83

W
wangguibao 已提交
84
  int reload();
W
wangguibao 已提交
85

W
wangguibao 已提交
86
  int finalize();
W
wangguibao 已提交
87

88 89
  std::vector<std::shared_ptr<PaddleGeneralModelConfig>>
  get_general_model_config();
G
guru4elephant 已提交
90 91

  void print_general_model_config(
92
      const std::shared_ptr<PaddleGeneralModelConfig>& config);
G
guru4elephant 已提交
93

W
wangjiawei04 已提交
94
  size_t get_cube_quant_bits();
W
wangguibao 已提交
95

W
wangguibao 已提交
96 97
 private:
  int thread_finalize() { return 0; }
98
  std::vector<std::shared_ptr<PaddleGeneralModelConfig>> _configs;
W
wangjiawei04 已提交
99
  std::string cube_config_fullpath;
W
wangjiawei04 已提交
100
  int cube_quant_bits;  // 0 if no empty
101

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

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