main.cc.in 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. #include "ultra_infer/pybind/main.h"
  15. namespace ultra_infer {
  16. void BindFDTensor(pybind11::module&);
  17. void BindRuntime(pybind11::module&);
  18. void BindFDModel(pybind11::module&);
  19. void BindVision(pybind11::module&);
  20. void BindText(pybind11::module&);
  21. void BindPipeline(pybind11::module&);
  22. pybind11::dtype FDDataTypeToNumpyDataType(const FDDataType& fd_dtype) {
  23. pybind11::dtype dt;
  24. if (fd_dtype == FDDataType::INT32) {
  25. dt = pybind11::dtype::of<int32_t>();
  26. } else if (fd_dtype == FDDataType::INT64) {
  27. dt = pybind11::dtype::of<int64_t>();
  28. } else if (fd_dtype == FDDataType::FP32) {
  29. dt = pybind11::dtype::of<float>();
  30. } else if (fd_dtype == FDDataType::FP64) {
  31. dt = pybind11::dtype::of<double>();
  32. } else if (fd_dtype == FDDataType::UINT8) {
  33. dt = pybind11::dtype::of<uint8_t>();
  34. } else if (fd_dtype == FDDataType::INT8) {
  35. dt = pybind11::dtype::of<int8_t>();
  36. } else if (fd_dtype == FDDataType::FP16) {
  37. dt = pybind11::dtype::of<float16>();
  38. } else if (fd_dtype == FDDataType::BOOL) {
  39. dt = pybind11::dtype::of<bool>();
  40. } else {
  41. FDASSERT(false, "The function doesn't support data type of %s.",
  42. Str(fd_dtype).c_str());
  43. }
  44. return dt;
  45. }
  46. FDDataType NumpyDataTypeToFDDataType(const pybind11::dtype& np_dtype) {
  47. if (np_dtype.is(pybind11::dtype::of<int32_t>())) {
  48. return FDDataType::INT32;
  49. } else if (np_dtype.is(pybind11::dtype::of<int64_t>())) {
  50. return FDDataType::INT64;
  51. } else if (np_dtype.is(pybind11::dtype::of<float>())) {
  52. return FDDataType::FP32;
  53. } else if (np_dtype.is(pybind11::dtype::of<double>())) {
  54. return FDDataType::FP64;
  55. } else if (np_dtype.is(pybind11::dtype::of<uint8_t>())) {
  56. return FDDataType::UINT8;
  57. } else if (np_dtype.is(pybind11::dtype::of<int8_t>())) {
  58. return FDDataType::INT8;
  59. } else if (np_dtype.is(pybind11::dtype::of<float16>())) {
  60. return FDDataType::FP16;
  61. }
  62. FDASSERT(false,
  63. "NumpyDataTypeToFDDataType() only support "
  64. "int8/int32/int64/float32/float64/float16 now.");
  65. return FDDataType::FP32;
  66. }
  67. void PyArrayToTensor(pybind11::array& pyarray, FDTensor* tensor,
  68. bool share_buffer) {
  69. auto dtype = NumpyDataTypeToFDDataType(pyarray.dtype());
  70. std::vector<int64_t> data_shape;
  71. data_shape.insert(data_shape.begin(), pyarray.shape(),
  72. pyarray.shape() + pyarray.ndim());
  73. if (share_buffer) {
  74. tensor->SetExternalData(data_shape, dtype,
  75. pyarray.mutable_data());
  76. } else {
  77. tensor->Resize(data_shape, dtype);
  78. memcpy(tensor->MutableData(), pyarray.mutable_data(), pyarray.nbytes());
  79. }
  80. }
  81. void PyArrayToTensorList(std::vector<pybind11::array>& pyarrays, std::vector<FDTensor>* tensors,
  82. bool share_buffer) {
  83. tensors->resize(pyarrays.size());
  84. for(auto i = 0; i < pyarrays.size(); ++i) {
  85. PyArrayToTensor(pyarrays[i], &(*tensors)[i], share_buffer);
  86. }
  87. }
  88. pybind11::array TensorToPyArray(const FDTensor& tensor) {
  89. auto numpy_dtype = FDDataTypeToNumpyDataType(tensor.dtype);
  90. auto out = pybind11::array(numpy_dtype, tensor.shape);
  91. memcpy(out.mutable_data(), tensor.CpuData(), tensor.Nbytes());
  92. return out;
  93. }
  94. #ifdef ENABLE_VISION
  95. int NumpyDataTypeToOpenCvType(const pybind11::dtype& np_dtype) {
  96. if (np_dtype.is(pybind11::dtype::of<int32_t>())) {
  97. return CV_32S;
  98. } else if (np_dtype.is(pybind11::dtype::of<int8_t>())) {
  99. return CV_8S;
  100. } else if (np_dtype.is(pybind11::dtype::of<uint8_t>())) {
  101. return CV_8U;
  102. } else if (np_dtype.is(pybind11::dtype::of<float>())) {
  103. return CV_32F;
  104. } else {
  105. FDASSERT(
  106. false,
  107. "NumpyDataTypeToOpenCvType() only support int32/int8/uint8/float32 "
  108. "now.");
  109. }
  110. return CV_8U;
  111. }
  112. int NumpyDataTypeToOpenCvTypeV2(pybind11::array& pyarray) {
  113. if (pybind11::isinstance<pybind11::array_t<std::int32_t>>(pyarray)) {
  114. return CV_32S;
  115. } else if (pybind11::isinstance<pybind11::array_t<std::int8_t>>(pyarray)) {
  116. return CV_8S;
  117. } else if (pybind11::isinstance<pybind11::array_t<std::uint8_t>>(pyarray)) {
  118. return CV_8U;
  119. } else if (pybind11::isinstance<pybind11::array_t<std::float_t>>(pyarray)) {
  120. return CV_32F;
  121. } else {
  122. FDASSERT(
  123. false,
  124. "NumpyDataTypeToOpenCvTypeV2() only support int32/int8/uint8/float32 "
  125. "now.");
  126. }
  127. return CV_8U;
  128. }
  129. cv::Mat PyArrayToCvMat(pybind11::array& pyarray) {
  130. // auto cv_type = NumpyDataTypeToOpenCvType(pyarray.dtype());
  131. auto cv_type = NumpyDataTypeToOpenCvTypeV2(pyarray);
  132. FDASSERT(
  133. pyarray.ndim() == 3,
  134. "Require rank of array to be 3 with HWC format while converting it to "
  135. "cv::Mat.");
  136. int channel = *(pyarray.shape() + 2);
  137. int height = *(pyarray.shape());
  138. int width = *(pyarray.shape() + 1);
  139. return cv::Mat(height, width, CV_MAKETYPE(cv_type, channel),
  140. pyarray.mutable_data());
  141. }
  142. #endif
  143. PYBIND11_MODULE(@PY_LIBRARY_NAME@, m) {
  144. m.doc() =
  145. "Make programmer easier to deploy deeplearning model, save time to save "
  146. "the world!";
  147. m.def("set_logger", &SetLogger);
  148. BindFDTensor(m);
  149. BindRuntime(m);
  150. BindFDModel(m);
  151. #ifdef ENABLE_VISION
  152. auto vision_module =
  153. m.def_submodule("vision", "Vision module of UltraInfer.");
  154. BindVision(vision_module);
  155. auto pipeline_module =
  156. m.def_submodule("pipeline", "Pipeline module of UltraInfer.");
  157. BindPipeline(pipeline_module);
  158. #endif
  159. #ifdef ENABLE_TEXT
  160. auto text_module =
  161. m.def_submodule("text", "Text module of UltraInfer.");
  162. BindText(text_module);
  163. #endif
  164. }
  165. } // namespace ultra_infer