FastDeploy  latest
Fast & Easy to Deploy!
backend.h
1 // Copyright (c) 2022 PaddlePaddle Authors. All Rights Reserved.
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 #pragma once
16 
17 #include <iostream>
18 #include <memory>
19 #include <string>
20 #include <vector>
21 
22 #include "fastdeploy/core/fd_tensor.h"
23 #include "fastdeploy/core/fd_type.h"
25 #include "fastdeploy/benchmark/benchmark.h"
26 
27 namespace fastdeploy {
28 
31 struct TensorInfo {
32  std::string name;
33  std::vector<int> shape;
34  FDDataType dtype;
35 
36  friend std::ostream& operator<<(std::ostream& output,
37  const TensorInfo& info) {
38  output << "TensorInfo(name: " << info.name << ", shape: [";
39  for (size_t i = 0; i < info.shape.size(); ++i) {
40  if (i == info.shape.size() - 1) {
41  output << info.shape[i];
42  } else {
43  output << info.shape[i] << ", ";
44  }
45  }
46  output << "], dtype: " << Str(info.dtype) << ")";
47  return output;
48  }
49 };
50 
51 class BaseBackend {
52  public:
53  bool initialized_ = false;
54 
55  BaseBackend() {}
56  virtual ~BaseBackend() = default;
57 
58  virtual bool Initialized() const { return initialized_; }
59 
60  virtual bool Init(const RuntimeOption& option) {
61  FDERROR << "Not Implement for "
62  << option.backend << " in "
63  << option.device << "."
64  << std::endl;
65  return false;
66  }
67 
68  // Get number of inputs of the model
69  virtual int NumInputs() const = 0;
70  // Get number of outputs of the model
71  virtual int NumOutputs() const = 0;
72  // Get information of input tensor
73  virtual TensorInfo GetInputInfo(int index) = 0;
74  // Get information of output tensor
75  virtual TensorInfo GetOutputInfo(int index) = 0;
76  // Get information of all the input tensors
77  virtual std::vector<TensorInfo> GetInputInfos() = 0;
78  // Get information of all the output tensors
79  virtual std::vector<TensorInfo> GetOutputInfos() = 0;
80 
81  // if copy_to_fd is true, copy memory data to FDTensor
82  // else share memory to FDTensor(only Paddle、ORT、TRT、OpenVINO support it)
83  virtual bool Infer(std::vector<FDTensor>& inputs,
84  std::vector<FDTensor>* outputs,
85  bool copy_to_fd = true) = 0;
86  // Optional: For those backends which can share memory
87  // while creating multiple inference engines with same model file
88  virtual std::unique_ptr<BaseBackend> Clone(RuntimeOption& runtime_option,
89  void* stream = nullptr,
90  int device_id = -1) {
91  FDERROR << "Clone no support " << runtime_option.backend << " " << stream << " " << device_id << std::endl;
92  return nullptr;
93  }
94 
95  benchmark::BenchmarkOption benchmark_option_;
96  benchmark::BenchmarkResult benchmark_result_;
97 };
98 
151 #define RUNTIME_PROFILE_LOOP_BEGIN(base_loop) \
152  __RUNTIME_PROFILE_LOOP_BEGIN(benchmark_option_, (base_loop))
153 #define RUNTIME_PROFILE_LOOP_END \
154  __RUNTIME_PROFILE_LOOP_END(benchmark_result_)
155 #define RUNTIME_PROFILE_LOOP_H2D_D2H_BEGIN \
156  __RUNTIME_PROFILE_LOOP_H2D_D2H_BEGIN(benchmark_option_, 1)
157 #define RUNTIME_PROFILE_LOOP_H2D_D2H_END \
158  __RUNTIME_PROFILE_LOOP_H2D_D2H_END(benchmark_result_)
159 
160 } // namespace fastdeploy
Option object used when create a new Runtime object.
Definition: runtime_option.h:40
A brief file description. More details.
std::string name
Name of tensor.
Definition: backend.h:32
Information of Tensor.
Definition: backend.h:31
FDDataType dtype
Data type of tensor.
Definition: backend.h:34
std::vector< int > shape
Shape of tensor.
Definition: backend.h:33
All C++ FastDeploy APIs are defined inside this namespace.
Definition: option.h:16
Result object used to record the time of runtime after benchmark profiling is done.
Definition: results.h:21