paddle_engine.h 5.8 KB
Newer Older
Z
update  
zhangjun 已提交
1
// Copyright (c) 2021 PaddlePaddle Authors. All Rights Reserved.
Z
zhangjun 已提交
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
//
// 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

#include <pthread.h>
#include <fstream>
#include <map>
Z
zhangjun 已提交
20
#include <memory>
Z
zhangjun 已提交
21 22 23 24
#include <string>
#include <vector>
#include "core/configure/include/configure_parser.h"
#include "core/configure/inferencer_configure.pb.h"
Z
zhangjun 已提交
25
#include "core/predictor/common/utils.h"
Z
zhangjun 已提交
26
#include "core/predictor/framework/infer.h"
Z
zhangjun 已提交
27 28 29 30 31 32 33
#include "paddle_inference_api.h"  // NOLINT

namespace baidu {
namespace paddle_serving {
namespace inference {

using paddle_infer::Config;
Z
zhangjun 已提交
34
using paddle_infer::PrecisionType;
Z
zhangjun 已提交
35 36 37 38
using paddle_infer::Predictor;
using paddle_infer::Tensor;
using paddle_infer::CreatePredictor;

Z
zhangjun 已提交
39 40
DECLARE_int32(gpuid);

Z
zhangjun 已提交
41 42
static const int max_batch = 32;
static const int min_subgraph_size = 3;
Z
zhangjun 已提交
43

Z
update  
zhangjun 已提交
44 45
// Engine Base
class PaddleEngineBase {
Z
zhangjun 已提交
46
 public:
Z
update  
zhangjun 已提交
47
  virtual ~PaddleEngineBase() {}
Z
zhangjun 已提交
48
  virtual std::vector<std::string> GetInputNames() {
Z
zhangjun 已提交
49
    return _predictor->GetInputNames();
Z
zhangjun 已提交
50 51 52
  }

  virtual std::unique_ptr<Tensor> GetInputHandle(const std::string& name) {
Z
zhangjun 已提交
53
    return _predictor->GetInputHandle(name);
Z
zhangjun 已提交
54 55 56
  }

  virtual std::vector<std::string> GetOutputNames() {
Z
zhangjun 已提交
57
    return _predictor->GetOutputNames();
Z
zhangjun 已提交
58 59 60
  }

  virtual std::unique_ptr<Tensor> GetOutputHandle(const std::string& name) {
Z
zhangjun 已提交
61
    return _predictor->GetOutputHandle(name);
Z
zhangjun 已提交
62 63 64
  }

  virtual bool Run() {
Z
zhangjun 已提交
65
    if (!_predictor->Run()) {
Z
zhangjun 已提交
66 67 68 69 70 71
      LOG(ERROR) << "Failed call Run with paddle predictor";
      return false;
    }
    return true;
  }

Z
update  
zhangjun 已提交
72
  virtual int create(const configure::EngineDesc& conf) = 0;
Z
zhangjun 已提交
73

Z
update  
zhangjun 已提交
74 75
  virtual int clone(void* predictor) {
    if (predictor == NULL) {
Z
zhangjun 已提交
76 77 78
      LOG(ERROR) << "origin paddle Predictor is null.";
      return -1;
    }
Z
zhangjun 已提交
79 80
    Predictor* prep = static_cast<Predictor*>(predictor);
    _predictor = prep->Clone();
Z
update  
zhangjun 已提交
81 82
    if (_predictor.get() == NULL) {
      LOG(ERROR) << "fail to clone paddle predictor: " << predictor;
Z
zhangjun 已提交
83 84 85 86 87
      return -1;
    }
    return 0;
  }

Z
update  
zhangjun 已提交
88
  virtual void* get() { return _predictor.get(); }
Z
zhangjun 已提交
89 90

 protected:
Z
update  
zhangjun 已提交
91
  std::shared_ptr<Predictor> _predictor;
Z
zhangjun 已提交
92 93
};

Z
update  
zhangjun 已提交
94 95
// Paddle Inference Engine
class PaddleInferenceEngine : public PaddleEngineBase {
Z
zhangjun 已提交
96
 public:
Z
update  
zhangjun 已提交
97 98 99
  int create(const configure::EngineDesc& engine_conf) {
    std::string model_path = engine_conf.model_dir();
    if (access(model_path.c_str(), F_OK) == -1) {
Z
zhangjun 已提交
100
      LOG(ERROR) << "create paddle predictor failed, path not exits: "
Z
update  
zhangjun 已提交
101
                 << model_path;
Z
zhangjun 已提交
102 103 104 105
      return -1;
    }

    Config config;
Z
update  
zhangjun 已提交
106
    // todo, auto config(zhangjun)
Z
zhangjun 已提交
107 108
    if (engine_conf.has_combined_model()) {
      if (!engine_conf.combined_model()) {
Z
zhangjun 已提交
109
        config.SetModel(model_path);
Z
update  
zhangjun 已提交
110 111 112 113 114 115 116
      } else {
        config.SetParamsFile(model_path + "/__params__");
        config.SetProgFile(model_path + "/__model__");
      }
    } else {
      config.SetParamsFile(model_path + "/__params__");
      config.SetProgFile(model_path + "/__model__");
Z
zhangjun 已提交
117
    }
Z
zhangjun 已提交
118

Z
zhangjun 已提交
119
    config.SwitchSpecifyInputNames(true);
Z
update  
zhangjun 已提交
120 121 122 123
    config.SetCpuMathLibraryNumThreads(1);
    if (engine_conf.has_use_gpu() && engine_conf.use_gpu()) {
      // 2000MB GPU memory
      config.EnableUseGpu(2000, FLAGS_gpuid);
Z
zhangjun 已提交
124
    }
Z
zhangjun 已提交
125

Z
update  
zhangjun 已提交
126
    if (engine_conf.has_use_trt() && engine_conf.use_trt()) {
Z
zhangjun 已提交
127 128 129
      if (!engine_conf.has_use_gpu() || !engine_conf.use_gpu()) {
        config.EnableUseGpu(2000, FLAGS_gpuid);
      }
Z
update  
zhangjun 已提交
130 131 132 133 134 135 136
      config.EnableTensorRtEngine(1 << 20,
                                  max_batch,
                                  min_subgraph_size,
                                  Config::Precision::kFloat32,
                                  false,
                                  false);
      LOG(INFO) << "create TensorRT predictor";
Z
zhangjun 已提交
137 138
    }

Z
zhangjun 已提交
139
    if (engine_conf.has_use_lite() && engine_conf.use_lite()) {
Z
update  
zhangjun 已提交
140
      config.EnableLiteEngine(PrecisionType::kFloat32, true);
Z
zhangjun 已提交
141 142
    }

Z
zhangjun 已提交
143
    if (engine_conf.has_use_xpu() && engine_conf.use_xpu()) {
Z
update  
zhangjun 已提交
144 145 146
      // 2 MB l3 cache
      config.EnableXpu(2 * 1024 * 1024);
    }
Z
zhangjun 已提交
147 148
    if (engine_conf.has_enable_ir_optimization() &&
        !engine_conf.enable_ir_optimization()) {
Z
zhangjun 已提交
149
      config.SwitchIrOptim(false);
Z
update  
zhangjun 已提交
150 151
    } else {
      config.SwitchIrOptim(true);
Z
zhangjun 已提交
152 153
    }

Z
zhangjun 已提交
154 155
    if (engine_conf.has_enable_memory_optimization() &&
        engine_conf.enable_memory_optimization()) {
Z
update  
zhangjun 已提交
156
      config.EnableMemoryOptim();
Z
zhangjun 已提交
157
    }
Z
zhangjun 已提交
158

Z
zhangjun 已提交
159
    if (engine_conf.has_encrypted_model() && engine_conf.encrypted_model()) {
Z
zhangjun 已提交
160 161
      // decrypt model
      std::string model_buffer, params_buffer, key_buffer;
Z
zhangjun 已提交
162 163 164
      predictor::ReadBinaryFile(model_path + "encrypt_model", &model_buffer);
      predictor::ReadBinaryFile(model_path + "encrypt_params", &params_buffer);
      predictor::ReadBinaryFile(model_path + "key", &key_buffer);
Z
zhangjun 已提交
165 166 167 168 169 170 171 172 173

      auto cipher = paddle::MakeCipher("");
      std::string real_model_buffer = cipher->Decrypt(model_buffer, key_buffer);
      std::string real_params_buffer =
          cipher->Decrypt(params_buffer, key_buffer);
      config.SetModelBuffer(&real_model_buffer[0],
                            real_model_buffer.size(),
                            &real_params_buffer[0],
                            real_params_buffer.size());
Z
zhangjun 已提交
174 175
    }

Z
zhangjun 已提交
176
    predictor::AutoLock lock(predictor::GlobalCreateMutex::instance());
Z
update  
zhangjun 已提交
177 178
    _predictor = CreatePredictor(config);
    if (NULL == _predictor.get()) {
Z
zhangjun 已提交
179
      LOG(ERROR) << "create paddle predictor failed, path: " << model_path;
Z
zhangjun 已提交
180 181
      return -1;
    }
Z
update  
zhangjun 已提交
182

Z
zhangjun 已提交
183
    VLOG(2) << "create paddle predictor sucess, path: " << model_path;
Z
zhangjun 已提交
184 185 186 187
    return 0;
  }
};

Z
update  
zhangjun 已提交
188
}  // namespace inference
Z
zhangjun 已提交
189 190
}  // namespace paddle_serving
}  // namespace baidu