mirror of
https://github.com/PaddlePaddle/FastDeploy.git
synced 2025-10-06 00:57:33 +08:00
Support Poros Backend (#188)
* Add poros backend * Add torch lib * Add python3 lib * set c++ 14 for poros * fixed bugs * fixed grammer bugs * fixed grammer bugs * fixed code bugs * fixed code bugs * fixed CreatePorosValue bug * Add AtType2String for Log * fixed trt_option * fixed poros.cmake path * fixed grammer bug * fixed grammer bug * fixed ambiguous reference * fixed ambiguous reference * fixed reference error * fixed include files * rm ENABLE_TRT_BACKEND in poros * update CMakeLists.txt * fixed CMakeLists.txt * Add libtorch.so in CMakeLists.txt * Fixed CMakeLists.txt * Fixed CMakeLists.txt * Fixed copy bug * Fixed copy bug * Fixed copy bug * Fixed Cmake * Fixed Cmake * debug * debug * debug * debug * debug * debug * debug utils * debug utils * copy to cpu * rm log info * test share mem * test share mem * test share mem * test multi outputs * test multi outputs * test multi outputs * test multi outputs * test multi outputs * test multi outputs * test multi outputs * time cost * time cost * fixed bug * time collect * mem copy * mem copy * rm time log * rm share mem * fixed multi inputs bug * add set_input_dtypes func * add SetInputDtypes * fixed bug * fixed bug * fixed prewarm data order * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * debug * fixed bug * Add compile func * Add compile func * Add compile func * Add is_dynamic option * Add is_dynamic option * Add is_dynamic option * Add is_dynamic option * rm infer log * add cuda11.6 poros lib * fixed bug * fixed bug * fixed multi outputs * fixed multi outputs * fixed multi outputs * fixed multi outputs * fixed multi outputs * fixed multi outputs * fixed multi outputs * fixed multi outputs * fixed multi outputs * fixed multi outputs * fixed multi outputs * rm logs * test * test * test * add test log * add test log * add test log * add test log * support cpu * support cpu * support cpu * support cpu * support member variable definition * rm useless log * fixed name * resolve conflict * resolve conflict * resolve conflict * fixed cmake * add GetInputInfos&GetOutputInfos * add GetInputInfos&GetOutputInfos * fixed bug * fixed runtime.py * add compile func * add np * deal with comments * rm to_inter func * add property
This commit is contained in:
5
python/fastdeploy/c_lib_wrap.py.in
Normal file → Executable file
5
python/fastdeploy/c_lib_wrap.py.in
Normal file → Executable file
@@ -34,6 +34,11 @@ def is_built_with_trt() -> bool:
|
||||
def is_built_with_paddle() -> bool:
|
||||
return True if "@ENABLE_PADDLE_BACKEND@" == "ON" else False
|
||||
|
||||
|
||||
def is_built_with_poros() ->bool:
|
||||
return True if "@ENABLE_POROS_BACKEND@" == "ON" else False
|
||||
|
||||
|
||||
def is_built_with_openvino() ->bool:
|
||||
return True if "@ENABLE_OPENVINO_BACKEND@" == "ON" else False
|
||||
|
||||
|
@@ -13,6 +13,7 @@
|
||||
# limitations under the License.
|
||||
from __future__ import absolute_import
|
||||
import logging
|
||||
import numpy as np
|
||||
from . import ModelFormat
|
||||
from . import c_lib_wrap as C
|
||||
|
||||
@@ -28,8 +29,24 @@ class Runtime:
|
||||
"""
|
||||
|
||||
self._runtime = C.Runtime()
|
||||
self.runtime_option = runtime_option
|
||||
assert self._runtime.init(
|
||||
runtime_option._option), "Initialize Runtime Failed!"
|
||||
self.runtime_option._option), "Initialize Runtime Failed!"
|
||||
|
||||
def forward(self, *inputs):
|
||||
"""Inference with input data for poros
|
||||
|
||||
:param data: (list[str : numpy.ndarray])The input data list
|
||||
:return list of numpy.ndarray
|
||||
"""
|
||||
if self.runtime_option._option.model_format != ModelFormat.TORCHSCRIPT:
|
||||
raise Exception(
|
||||
"The forward function is only used for Poros backend, please call infer function"
|
||||
)
|
||||
inputs_dict = dict()
|
||||
for i in range(len(inputs)):
|
||||
inputs_dict["x" + str(i)] = inputs[i]
|
||||
return self.infer(inputs_dict)
|
||||
|
||||
def infer(self, data):
|
||||
"""Inference with input data.
|
||||
@@ -41,6 +58,27 @@ class Runtime:
|
||||
data, list), "The input data should be type of dict or list."
|
||||
return self._runtime.infer(data)
|
||||
|
||||
def compile(self, warm_datas):
|
||||
"""compile with prewarm data for poros
|
||||
|
||||
:param data: (list[str : numpy.ndarray])The prewarm data list
|
||||
:return TorchScript Model
|
||||
"""
|
||||
if self.runtime_option._option.model_format != ModelFormat.TORCHSCRIPT:
|
||||
raise Exception(
|
||||
"The compile function is only used for Poros backend, please call infer function"
|
||||
)
|
||||
assert isinstance(warm_datas,
|
||||
list), "The prewarm data should be type of list."
|
||||
for i in range(len(warm_datas)):
|
||||
warm_data = warm_datas[i]
|
||||
if isinstance(warm_data[0], np.ndarray):
|
||||
warm_data = list(data for data in warm_data)
|
||||
else:
|
||||
warm_data = list(data.numpy() for data in warm_data)
|
||||
warm_datas[i] = warm_data
|
||||
return self._runtime.compile(warm_datas, self.runtime_option._option)
|
||||
|
||||
def num_inputs(self):
|
||||
"""Get number of inputs of the loaded model.
|
||||
"""
|
||||
@@ -85,6 +123,65 @@ class RuntimeOption:
|
||||
def __init__(self):
|
||||
self._option = C.RuntimeOption()
|
||||
|
||||
@property
|
||||
def is_dynamic(self):
|
||||
"""Only for Poros backend
|
||||
|
||||
:param value: (bool)Whether to enable dynamic shape, default False
|
||||
"""
|
||||
return self._option.is_dynamic
|
||||
|
||||
@property
|
||||
def unconst_ops_thres(self):
|
||||
"""Only for Poros backend
|
||||
|
||||
:param value: (int)Minimum number of subgraph OPs, default 10
|
||||
"""
|
||||
return self._option.unconst_ops_thres
|
||||
|
||||
@property
|
||||
def long_to_int(self):
|
||||
"""Only for Poros backend
|
||||
|
||||
:param value: (bool)Whether to convert long dtype to int dtype, default True
|
||||
"""
|
||||
return self._option.long_to_int
|
||||
|
||||
@property
|
||||
def use_nvidia_tf32(self):
|
||||
"""Only for Poros backend
|
||||
|
||||
:param value: (bool)The calculation accuracy of tf32 mode exists on the A card, which can bring some performance improvements, default False
|
||||
"""
|
||||
return self._option.use_nvidia_tf32
|
||||
|
||||
@is_dynamic.setter
|
||||
def is_dynamic(self, value):
|
||||
assert isinstance(
|
||||
value, bool), "The value to set `is_dynamic` must be type of bool."
|
||||
self._option.is_dynamic = value
|
||||
|
||||
@unconst_ops_thres.setter
|
||||
def unconst_ops_thres(self, value):
|
||||
assert isinstance(
|
||||
value,
|
||||
int), "The value to set `unconst_ops_thres` must be type of int."
|
||||
self._option.unconst_ops_thres = value
|
||||
|
||||
@long_to_int.setter
|
||||
def long_to_int(self, value):
|
||||
assert isinstance(
|
||||
value,
|
||||
bool), "The value to set `long_to_int` must be type of bool."
|
||||
self._option.long_to_int = value
|
||||
|
||||
@use_nvidia_tf32.setter
|
||||
def use_nvidia_tf32(self, value):
|
||||
assert isinstance(
|
||||
value,
|
||||
bool), "The value to set `use_nvidia_tf32` must be type of bool."
|
||||
self._option.use_nvidia_tf32 = value
|
||||
|
||||
def set_model_path(self,
|
||||
model_path,
|
||||
params_path="",
|
||||
@@ -125,6 +222,11 @@ class RuntimeOption:
|
||||
"""
|
||||
return self._option.use_paddle_backend()
|
||||
|
||||
def use_poros_backend(self):
|
||||
"""Use Poros backend, support inference TorchScript model on CPU/Nvidia GPU.
|
||||
"""
|
||||
return self._option.use_poros_backend()
|
||||
|
||||
def use_ort_backend(self):
|
||||
"""Use ONNX Runtime backend, support inference Paddle/ONNX model on CPU/Nvidia GPU.
|
||||
"""
|
||||
@@ -235,7 +337,8 @@ class RuntimeOption:
|
||||
continue
|
||||
if hasattr(getattr(self._option, attr), "__call__"):
|
||||
continue
|
||||
message += " {} : {}\t\n".format(attr, getattr(self._option, attr))
|
||||
message += " {} : {}\t\n".format(attr,
|
||||
getattr(self._option, attr))
|
||||
message.strip("\n")
|
||||
message += ")"
|
||||
return message
|
||||
|
11
python/setup.py
Normal file → Executable file
11
python/setup.py
Normal file → Executable file
@@ -53,13 +53,16 @@ setup_configs["ENABLE_OPENVINO_BACKEND"] = os.getenv("ENABLE_OPENVINO_BACKEND",
|
||||
"OFF")
|
||||
setup_configs["ENABLE_PADDLE_BACKEND"] = os.getenv("ENABLE_PADDLE_BACKEND",
|
||||
"OFF")
|
||||
setup_configs["ENABLE_POROS_BACKEND"] = os.getenv("ENABLE_POROS_BACKEND",
|
||||
"OFF")
|
||||
setup_configs["ENABLE_VISION"] = os.getenv("ENABLE_VISION", "OFF")
|
||||
setup_configs["ENABLE_TEXT"] = os.getenv("ENABLE_TEXT", "OFF")
|
||||
setup_configs["ENABLE_TRT_BACKEND"] = os.getenv("ENABLE_TRT_BACKEND", "OFF")
|
||||
setup_configs["WITH_GPU"] = os.getenv("WITH_GPU", "OFF")
|
||||
setup_configs["BUILD_ON_JETSON"] = os.getenv("BUILD_ON_JETSON", "OFF")
|
||||
setup_configs["TRT_DIRECTORY"] = os.getenv("TRT_DIRECTORY", "UNDEFINED")
|
||||
setup_configs["CUDA_DIRECTORY"] = os.getenv("CUDA_DIRECTORY", "/usr/local/cuda")
|
||||
setup_configs["CUDA_DIRECTORY"] = os.getenv("CUDA_DIRECTORY",
|
||||
"/usr/local/cuda")
|
||||
setup_configs["LIBRARY_NAME"] = PACKAGE_NAME
|
||||
setup_configs["PY_LIBRARY_NAME"] = PACKAGE_NAME + "_main"
|
||||
setup_configs["OPENCV_DIRECTORY"] = os.getenv("OPENCV_DIRECTORY", "")
|
||||
@@ -89,7 +92,8 @@ extras_require = {}
|
||||
|
||||
# Default value is set to TRUE\1 to keep the settings same as the current ones.
|
||||
# However going forward the recomemded way to is to set this to False\0
|
||||
USE_MSVC_STATIC_RUNTIME = bool(os.getenv('USE_MSVC_STATIC_RUNTIME', '1') == '1')
|
||||
USE_MSVC_STATIC_RUNTIME = bool(
|
||||
os.getenv('USE_MSVC_STATIC_RUNTIME', '1') == '1')
|
||||
ONNX_NAMESPACE = os.getenv('ONNX_NAMESPACE', 'paddle2onnx')
|
||||
################################################################################
|
||||
# Version
|
||||
@@ -119,7 +123,8 @@ assert CMAKE, 'Could not find "cmake" executable!'
|
||||
@contextmanager
|
||||
def cd(path):
|
||||
if not os.path.isabs(path):
|
||||
raise RuntimeError('Can only cd to absolute path, got: {}'.format(path))
|
||||
raise RuntimeError('Can only cd to absolute path, got: {}'.format(
|
||||
path))
|
||||
orig_path = os.getcwd()
|
||||
os.chdir(path)
|
||||
try:
|
||||
|
Reference in New Issue
Block a user