main.cc.in 5.9 KB

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