__init__.py 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  1. # Copyright (c) 2024 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. from __future__ import absolute_import
  15. import logging
  16. import os
  17. import platform
  18. import sys
  19. # Create a symbol link to tensorrt library.
  20. trt_directory = os.path.join(
  21. os.path.dirname(os.path.abspath(__file__)), "libs/third_libs/tensorrt/lib/"
  22. )
  23. if os.name != "nt" and os.path.exists(trt_directory):
  24. logging.basicConfig(level=logging.INFO)
  25. for trt_lib in [
  26. "libnvcaffe_parser.so",
  27. "libnvinfer_plugin.so",
  28. "libnvinfer.so",
  29. "libnvonnxparser.so",
  30. "libnvparsers.so",
  31. ]:
  32. dst = os.path.join(trt_directory, trt_lib)
  33. src = os.path.join(trt_directory, trt_lib + ".8")
  34. if not os.path.exists(dst):
  35. try:
  36. os.symlink(src, dst)
  37. logging.info(f"Create a symbolic link pointing to {src} named {dst}.")
  38. except OSError as e:
  39. logging.warning(
  40. f"Failed to create a symbolic link pointing to {src} by an unprivileged user. "
  41. "It may failed when you use Paddle TensorRT backend. "
  42. "Please use administator privilege to import ultra_infer at first time."
  43. )
  44. break
  45. # HACK: Reset the root logger config that got messed up by FD.
  46. root_logger = logging.getLogger()
  47. root_logger.level = logging.WARNING
  48. for handler in root_logger.handlers[:]:
  49. root_logger.removeHandler(handler)
  50. from .code_version import (
  51. enable_paddle_backend,
  52. enable_trt_backend,
  53. extra_version_info,
  54. git_version,
  55. version,
  56. with_gpu,
  57. )
  58. # Note(zhoushunjie): Fix the import order of paddle and ultra_infer library.
  59. # This solution will be removed it when the confilct of paddle and
  60. # ultra_infer is fixed.
  61. # Note(qiuyanjun): Add backward compatible for paddle 2.4.x
  62. sys_platform = platform.platform().lower()
  63. def get_paddle_version():
  64. paddle_version = ""
  65. try:
  66. import pkg_resources
  67. paddle_version = pkg_resources.require("paddlepaddle-gpu")[0].version.split(
  68. ".post"
  69. )[0]
  70. except:
  71. try:
  72. paddle_version = pkg_resources.require("paddlepaddle")[0].version.split(
  73. ".post"
  74. )[0]
  75. except:
  76. pass
  77. return paddle_version
  78. def should_import_paddle():
  79. if ("paddle2.4" in extra_version_info) or ("post24" in extra_version_info):
  80. paddle_version = get_paddle_version()
  81. if (
  82. paddle_version != ""
  83. and paddle_version <= "2.4.2"
  84. and paddle_version != "0.0.0"
  85. ):
  86. return True
  87. return False
  88. def should_set_tensorrt():
  89. if (
  90. with_gpu == "ON"
  91. and enable_paddle_backend == "ON"
  92. and enable_trt_backend == "ON"
  93. ):
  94. return True
  95. return False
  96. def tensorrt_is_avaliable():
  97. # Note(qiuyanjun): Only support linux now.
  98. found_trt_lib = False
  99. if ("linux" in sys_platform) and ("LD_LIBRARY_PATH" in os.environ.keys()):
  100. for lib_path in os.environ["LD_LIBRARY_PATH"].split(":"):
  101. if os.path.exists(os.path.join(lib_path, "libnvinfer.so")):
  102. found_trt_lib = True
  103. break
  104. return found_trt_lib
  105. try:
  106. # windows: no conflict between ultra_infer and paddle.
  107. # linux: must import paddle first to solve the conflict.
  108. # macos: still can not solve the conflict between ultra_infer and paddle,
  109. # due to the global flags redefined in paddle/paddle_inference so.
  110. # we got the error (ERROR: flag 'xxx' was defined more than once).
  111. if "linux" in sys_platform:
  112. if should_import_paddle():
  113. import paddle # need import paddle first for paddle2.4.x
  114. # check whether tensorrt in LD_LIBRARY_PATH for ultra_infer
  115. if should_set_tensorrt() and (not tensorrt_is_avaliable()):
  116. if os.path.exists(trt_directory):
  117. logging.info(
  118. "\n[WARNING] Can not find TensorRT lib in LD_LIBRARY_PATH for UltraInfer! \
  119. \n[WARNING] Please export [ YOUR CUSTOM TensorRT ] lib path to LD_LIBRARY_PATH first, or run the command: \
  120. \n[WARNING] Linux: 'export LD_LIBRARY_PATH=$(python -c 'from ultra_infer import trt_directory; print(trt_directory)'):$LD_LIBRARY_PATH'"
  121. )
  122. else:
  123. logging.info(
  124. "\n[WARNING] Can not find TensorRT lib in LD_LIBRARY_PATH for UltraInfer! \
  125. \n[WARNING] Please export [YOUR CUSTOM TensorRT] lib path to LD_LIBRARY_PATH first."
  126. )
  127. except:
  128. pass
  129. from .c_lib_wrap import (
  130. Backend,
  131. Device,
  132. FDDataType,
  133. ModelFormat,
  134. TensorInfo,
  135. get_default_cuda_directory,
  136. is_built_with_gpu,
  137. is_built_with_om,
  138. is_built_with_openvino,
  139. is_built_with_ort,
  140. is_built_with_paddle,
  141. is_built_with_trt,
  142. )
  143. def set_logger(enable_info=True, enable_warning=True):
  144. """Set behaviour of logger while using UltraInfer
  145. :param enable_info: (boolean)Whether to print out log level of INFO
  146. :param enable_warning: (boolean)Whether to print out log level of WARNING, recommend to set to True
  147. """
  148. from .c_lib_wrap import set_logger
  149. set_logger(enable_info, enable_warning)
  150. from . import c_lib_wrap as C
  151. from . import pipeline, text, ts, vision
  152. from .download import download, download_and_decompress, download_model, get_model_list
  153. from .model import UltraInferModel
  154. from .runtime import Runtime, RuntimeOption
  155. __version__ = version