util.cc 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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/core/float16.h"
  15. #include "ultra_infer/runtime/backends/paddle/paddle_backend.h"
  16. namespace ultra_infer {
  17. paddle_infer::PlaceType ConvertFDDeviceToPlace(Device device) {
  18. if (device == Device::GPU) {
  19. return paddle_infer::PlaceType::kGPU;
  20. } else if (device == Device::KUNLUNXIN) {
  21. return paddle_infer::PlaceType::kXPU;
  22. }
  23. return paddle_infer::PlaceType::kCPU;
  24. }
  25. void ShareTensorFromFDTensor(paddle_infer::Tensor *tensor,
  26. FDTensor &fd_tensor) {
  27. std::vector<int> shape(fd_tensor.shape.begin(), fd_tensor.shape.end());
  28. tensor->Reshape(shape);
  29. auto place = ConvertFDDeviceToPlace(fd_tensor.device);
  30. if (fd_tensor.dtype == FDDataType::FP32) {
  31. if (place == paddle_infer::PlaceType::kGPU) {
  32. tensor->ShareExternalData(static_cast<const float *>(fd_tensor.Data()),
  33. shape, place);
  34. } else {
  35. tensor->CopyFromCpu(static_cast<const float *>(fd_tensor.Data()));
  36. }
  37. return;
  38. } else if (fd_tensor.dtype == FDDataType::INT32) {
  39. if (place == paddle_infer::PlaceType::kGPU) {
  40. tensor->ShareExternalData(static_cast<const int32_t *>(fd_tensor.Data()),
  41. shape, place);
  42. } else {
  43. tensor->CopyFromCpu(static_cast<const int32_t *>(fd_tensor.Data()));
  44. }
  45. return;
  46. } else if (fd_tensor.dtype == FDDataType::INT64) {
  47. if (place == paddle_infer::PlaceType::kGPU) {
  48. tensor->ShareExternalData(static_cast<const int64_t *>(fd_tensor.Data()),
  49. shape, place);
  50. } else {
  51. tensor->CopyFromCpu(static_cast<const int64_t *>(fd_tensor.Data()));
  52. }
  53. return;
  54. } else if (fd_tensor.dtype == FDDataType::INT8) {
  55. if (place == paddle_infer::PlaceType::kGPU) {
  56. tensor->ShareExternalData(static_cast<const int8_t *>(fd_tensor.Data()),
  57. shape, place);
  58. } else {
  59. tensor->CopyFromCpu(static_cast<const int8_t *>(fd_tensor.Data()));
  60. }
  61. return;
  62. } else if (fd_tensor.dtype == FDDataType::UINT8) {
  63. if (place == paddle_infer::PlaceType::kGPU) {
  64. tensor->ShareExternalData(static_cast<const uint8_t *>(fd_tensor.Data()),
  65. shape, place);
  66. } else {
  67. tensor->CopyFromCpu(static_cast<const uint8_t *>(fd_tensor.Data()));
  68. }
  69. return;
  70. }
  71. FDASSERT(false, "Unexpected data type(%s) while infer with PaddleBackend.",
  72. Str(fd_tensor.dtype).c_str());
  73. }
  74. void ShareOutTensorFromFDTensor(paddle_infer::Tensor *tensor,
  75. FDTensor &fd_tensor) {
  76. std::vector<int> shape(fd_tensor.shape.begin(), fd_tensor.shape.end());
  77. auto place = ConvertFDDeviceToPlace(fd_tensor.device);
  78. if (fd_tensor.dtype == FDDataType::FP32) {
  79. if (place == paddle_infer::PlaceType::kGPU) {
  80. tensor->ShareExternalData(static_cast<float *>(fd_tensor.MutableData()),
  81. shape, place);
  82. } else {
  83. tensor->CopyToCpu(static_cast<float *>(fd_tensor.MutableData()));
  84. }
  85. return;
  86. } else if (fd_tensor.dtype == FDDataType::INT32) {
  87. if (place == paddle_infer::PlaceType::kGPU) {
  88. tensor->ShareExternalData(static_cast<int32_t *>(fd_tensor.MutableData()),
  89. shape, place);
  90. } else {
  91. tensor->CopyToCpu(static_cast<int32_t *>(fd_tensor.MutableData()));
  92. }
  93. return;
  94. } else if (fd_tensor.dtype == FDDataType::INT64) {
  95. if (place == paddle_infer::PlaceType::kGPU) {
  96. tensor->ShareExternalData(static_cast<int64_t *>(fd_tensor.MutableData()),
  97. shape, place);
  98. } else {
  99. tensor->CopyToCpu(static_cast<int64_t *>(fd_tensor.MutableData()));
  100. }
  101. return;
  102. } else if (fd_tensor.dtype == FDDataType::INT8) {
  103. if (place == paddle_infer::PlaceType::kGPU) {
  104. tensor->ShareExternalData(static_cast<const int8_t *>(fd_tensor.Data()),
  105. shape, place);
  106. } else {
  107. tensor->CopyFromCpu(static_cast<const int8_t *>(fd_tensor.Data()));
  108. }
  109. return;
  110. } else if (fd_tensor.dtype == FDDataType::UINT8) {
  111. if (place == paddle_infer::PlaceType::kGPU) {
  112. tensor->ShareExternalData(static_cast<const uint8_t *>(fd_tensor.Data()),
  113. shape, place);
  114. } else {
  115. tensor->CopyFromCpu(static_cast<const uint8_t *>(fd_tensor.Data()));
  116. }
  117. return;
  118. }
  119. FDASSERT(false, "Unexpected data type(%s) while infer with PaddleBackend.",
  120. Str(fd_tensor.dtype).c_str());
  121. }
  122. void PaddleTensorToFDTensor(std::unique_ptr<paddle_infer::Tensor> &tensor,
  123. FDTensor *fd_tensor, bool copy_to_fd) {
  124. auto fd_dtype = PaddleDataTypeToFD(tensor->type());
  125. std::vector<int64_t> shape;
  126. auto tmp_shape = tensor->shape();
  127. shape.assign(tmp_shape.begin(), tmp_shape.end());
  128. if (copy_to_fd) {
  129. fd_tensor->Resize(shape, fd_dtype, tensor->name());
  130. if (fd_tensor->dtype == FDDataType::FP32) {
  131. tensor->CopyToCpu(static_cast<float *>(fd_tensor->MutableData()));
  132. return;
  133. } else if (fd_tensor->dtype == FDDataType::INT32) {
  134. tensor->CopyToCpu(static_cast<int32_t *>(fd_tensor->MutableData()));
  135. return;
  136. } else if (fd_tensor->dtype == FDDataType::INT64) {
  137. tensor->CopyToCpu(static_cast<int64_t *>(fd_tensor->MutableData()));
  138. return;
  139. } else if (fd_tensor->dtype == FDDataType::INT8) {
  140. tensor->CopyToCpu(static_cast<int8_t *>(fd_tensor->MutableData()));
  141. return;
  142. } else if (fd_tensor->dtype == FDDataType::UINT8) {
  143. tensor->CopyToCpu(static_cast<uint8_t *>(fd_tensor->MutableData()));
  144. return;
  145. }
  146. FDASSERT(false, "Unexpected data type(%s) while infer with PaddleBackend.",
  147. Str(fd_tensor->dtype).c_str());
  148. } else {
  149. paddle_infer::PlaceType place;
  150. int size = 0;
  151. // TODO(liqi): The tensor->data interface of paddle don't return device id
  152. // and don't support return void*.
  153. void *out_data = nullptr;
  154. if (fd_dtype == FDDataType::FP32) {
  155. out_data = tensor->data<float>(&place, &size);
  156. } else if (fd_dtype == FDDataType::INT32) {
  157. out_data = tensor->data<int>(&place, &size);
  158. } else if (fd_dtype == FDDataType::INT64) {
  159. out_data = tensor->data<int64_t>(&place, &size);
  160. } else if (fd_dtype == FDDataType::INT8) {
  161. out_data = tensor->data<int8_t>(&place, &size);
  162. } else if (fd_dtype == FDDataType::UINT8) {
  163. out_data = tensor->data<uint8_t>(&place, &size);
  164. } else {
  165. FDASSERT(
  166. false,
  167. "Unexpected data type(%s) while infer shared with PaddleBackend.",
  168. Str(fd_dtype).c_str());
  169. }
  170. Device device = Device::CPU;
  171. if (place == paddle_infer::PlaceType::kGPU) {
  172. device = Device::GPU;
  173. } else if (place == paddle_infer::PlaceType::kXPU) {
  174. device = Device::KUNLUNXIN;
  175. FDASSERT(false, "Currently, copy_to_fd=false, FDTensor SetExternalData "
  176. "is not support for Device::KUNLUNXIN now!")
  177. }
  178. fd_tensor->name = tensor->name();
  179. fd_tensor->SetExternalData(shape, fd_dtype, out_data, device);
  180. }
  181. }
  182. FDDataType PaddleDataTypeToFD(const paddle_infer::DataType &dtype) {
  183. auto fd_dtype = FDDataType::FP32;
  184. if (dtype == paddle_infer::FLOAT32) {
  185. fd_dtype = FDDataType::FP32;
  186. } else if (dtype == paddle_infer::INT64) {
  187. fd_dtype = FDDataType::INT64;
  188. } else if (dtype == paddle_infer::INT32) {
  189. fd_dtype = FDDataType::INT32;
  190. } else if (dtype == paddle_infer::UINT8) {
  191. fd_dtype = FDDataType::UINT8;
  192. } else if (dtype == paddle_infer::INT8) {
  193. fd_dtype = FDDataType::INT8;
  194. } else if (dtype == paddle_infer::FLOAT16) {
  195. fd_dtype = FDDataType::FP16;
  196. } else {
  197. FDASSERT(
  198. false,
  199. "Unexpected data type: %d while call CopyTensorToCpu in PaddleBackend.",
  200. int(dtype));
  201. }
  202. return fd_dtype;
  203. }
  204. FDDataType ReaderDataTypeToFD(int32_t dtype) {
  205. auto fd_dtype = FDDataType::FP32;
  206. if (dtype == 0) {
  207. fd_dtype = FDDataType::FP32;
  208. } else if (dtype == 1) {
  209. fd_dtype = FDDataType::FP64;
  210. } else if (dtype == 2) {
  211. fd_dtype = FDDataType::UINT8;
  212. } else if (dtype == 3) {
  213. fd_dtype = FDDataType::INT8;
  214. } else if (dtype == 4) {
  215. fd_dtype = FDDataType::INT32;
  216. } else if (dtype == 5) {
  217. fd_dtype = FDDataType::INT64;
  218. } else if (dtype == 6) {
  219. fd_dtype = FDDataType::FP16;
  220. } else {
  221. FDASSERT(false,
  222. "Unexpected data type: %d while call ReaderDataTypeToFD in "
  223. "PaddleBackend.",
  224. dtype);
  225. }
  226. return fd_dtype;
  227. }
  228. } // namespace ultra_infer