engine.h 2.4 KB
Newer Older
Y
Yan Chunwei 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
/* Copyright (c) 2018 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

#include "paddle/fluid/framework/framework.pb.h"

namespace paddle {
namespace inference {

Y
Yan Chunwei 已提交
22 23 24
struct Buffer;
enum class DeviceType { UNK = -1, CPU, GPU };

Y
Yan Chunwei 已提交
25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50
/*
 * EngineBase is the base class of all inference engines. An inference engine
 * takes a paddle program as input, and outputs the result in fluid Tensor
 * format. It can be used to optimize performance of computation sub-blocks, for
 * example, break down the original block into sub-blocks and execute each
 * sub-blocks in different engines.
 *
 * For example:
 *   When inference, the resnet50 model can put most of the model into subgraph
 * and run it on a TensorRT engine.
 *
 * There are several engines such as TensorRT and other frameworks, so an
 * EngineBase is put forward to give an unified interface for all the
 * different engine implemention.
 */
class EngineBase {
 public:
  using DescType = ::paddle::framework::proto::BlockDesc;

  // Build the model and do some preparation, for example, in TensorRT, run
  // createInferBuilder, buildCudaEngine.
  virtual void Build(const DescType& paddle_model) = 0;

  // Execute the engine, that will run the inference network.
  virtual void Execute(int batch_size) = 0;

Y
Yan Chunwei 已提交
51 52 53 54 55
  // Return the IO buffer that allocated in engine. One can read/write directly
  // on the buffer. If the buffer's buffer is nullptr, one can also allocate
  // memory and maintain it outside the engine.
  virtual Buffer& buffer(const std::string& name) = 0;

Y
Yan Chunwei 已提交
56 57 58
  virtual ~EngineBase() {}
};  // class EngineBase

Y
Yan Chunwei 已提交
59 60 61 62 63 64 65
struct Buffer {
  void* buffer{nullptr};               // buffer should be allocated only once.
  int max_size;                        // buffer allocated space.
  int size;                            // data size.
  DeviceType device{DeviceType::UNK};  // tells which device this buffer is on.
};

Y
Yan Chunwei 已提交
66 67
}  // namespace inference
}  // namespace paddle