pd_tensor.h 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287
  1. // Copyright (c) 2021 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. ///
  15. /// \file pd_tensor.h
  16. ///
  17. /// \brief interface for paddle tensor
  18. ///
  19. /// \author paddle-infer@baidu.com
  20. /// \date 2021-04-21
  21. /// \since 2.1
  22. ///
  23. #pragma once
  24. #include "pd_common.h" // NOLINT
  25. typedef struct PD_Tensor PD_Tensor;
  26. typedef struct PD_OneDimArrayInt32 PD_OneDimArrayInt32;
  27. typedef struct PD_TwoDimArraySize PD_TwoDimArraySize;
  28. #ifdef __cplusplus
  29. extern "C" {
  30. #endif
  31. ///
  32. /// \brief Destroy the paddle tensor
  33. ///
  34. /// \param[in] pd_tensor tensor
  35. ///
  36. PADDLE_CAPI_EXPORT extern void PD_TensorDestroy(__pd_take PD_Tensor* pd_tensor);
  37. ///
  38. /// \brief Reset the shape of the tensor.
  39. /// Generally it's only used for the input tensor.
  40. /// Reshape must be called before calling PD_TensorMutableData*() or
  41. /// PD_TensorCopyFromCpu*()
  42. ///
  43. /// \param[in] pd_tensor tensor.
  44. /// \param[in] shape_size The size of shape.
  45. /// \param[in] shape The shape to set.
  46. ///
  47. PADDLE_CAPI_EXPORT extern void PD_TensorReshape(__pd_keep PD_Tensor* pd_tensor,
  48. size_t shape_size,
  49. int32_t* shape);
  50. ///
  51. /// \brief Get the memory pointer in CPU or GPU with 'float' data type.
  52. /// Please Reshape the tensor first before call this.
  53. /// It's usually used to get input data pointer.
  54. ///
  55. /// \param[in] pd_tensor tensor.
  56. /// \param[in] place The place of the tensor.
  57. /// \return Memory pointer of pd_tensor
  58. ///
  59. PADDLE_CAPI_EXPORT extern float* PD_TensorMutableDataFloat(
  60. __pd_keep PD_Tensor* pd_tensor, PD_PlaceType place);
  61. ///
  62. /// \brief Get the memory pointer in CPU or GPU with 'int64_t' data type.
  63. /// Please Reshape the tensor first before call this.
  64. /// It's usually used to get input data pointer.
  65. ///
  66. /// \param[in] pd_tensor tensor.
  67. /// \param[in] place The place of the tensor.
  68. /// \return Memory pointer of pd_tensor
  69. ///
  70. PADDLE_CAPI_EXPORT extern int64_t* PD_TensorMutableDataInt64(
  71. __pd_keep PD_Tensor* pd_tensor, PD_PlaceType place);
  72. ///
  73. /// \brief Get the memory pointer in CPU or GPU with 'int32_t' data type.
  74. /// Please Reshape the tensor first before call this.
  75. /// It's usually used to get input data pointer.
  76. ///
  77. /// \param[in] pd_tensor tensor.
  78. /// \param[in] place The place of the tensor.
  79. /// \return Memory pointer of pd_tensor
  80. ///
  81. PADDLE_CAPI_EXPORT extern int32_t* PD_TensorMutableDataInt32(
  82. __pd_keep PD_Tensor* pd_tensor, PD_PlaceType place);
  83. ///
  84. /// \brief Get the memory pointer in CPU or GPU with 'uint8_t' data type.
  85. /// Please Reshape the tensor first before call this.
  86. /// It's usually used to get input data pointer.
  87. ///
  88. /// \param[in] pd_tensor tensor.
  89. /// \param[in] place The place of the tensor.
  90. /// \return Memory pointer of pd_tensor
  91. ///
  92. PADDLE_CAPI_EXPORT extern uint8_t* PD_TensorMutableDataUint8(
  93. __pd_keep PD_Tensor* pd_tensor, PD_PlaceType place);
  94. ///
  95. /// \brief Get the memory pointer in CPU or GPU with 'int8_t' data type.
  96. /// Please Reshape the tensor first before call this.
  97. /// It's usually used to get input data pointer.
  98. ///
  99. /// \param[in] pd_tensor tensor.
  100. /// \param[in] place The place of the tensor.
  101. /// \return Memory pointer of pd_tensor
  102. ///
  103. PADDLE_CAPI_EXPORT extern int8_t* PD_TensorMutableDataInt8(
  104. __pd_keep PD_Tensor* pd_tensor, PD_PlaceType place);
  105. ///
  106. /// \brief Get the memory pointer directly.
  107. /// It's usually used to get the output data pointer.
  108. ///
  109. /// \param[in] pd_tensor tensor.
  110. /// \param[out] place To get the device type of the tensor.
  111. /// \param[out] size To get the data size of the tensor.
  112. /// \return The tensor data buffer pointer.
  113. ///
  114. PADDLE_CAPI_EXPORT extern float* PD_TensorDataFloat(
  115. __pd_keep PD_Tensor* pd_tensor, PD_PlaceType* place, int32_t* size);
  116. ///
  117. /// \brief Get the memory pointer directly.
  118. /// It's usually used to get the output data pointer.
  119. ///
  120. /// \param[in] pd_tensor tensor.
  121. /// \param[out] place To get the device type of the tensor.
  122. /// \param[out] size To get the data size of the tensor.
  123. /// \return The tensor data buffer pointer.
  124. ///
  125. PADDLE_CAPI_EXPORT extern int64_t* PD_TensorDataInt64(
  126. __pd_keep PD_Tensor* pd_tensor, PD_PlaceType* place, int32_t* size);
  127. ///
  128. /// \brief Get the memory pointer directly.
  129. /// It's usually used to get the output data pointer.
  130. ///
  131. /// \param[in] pd_tensor tensor.
  132. /// \param[out] place To get the device type of the tensor.
  133. /// \param[out] size To get the data size of the tensor.
  134. /// \return The tensor data buffer pointer.
  135. ///
  136. PADDLE_CAPI_EXPORT extern int32_t* PD_TensorDataInt32(
  137. __pd_keep PD_Tensor* pd_tensor, PD_PlaceType* place, int32_t* size);
  138. ///
  139. /// \brief Get the memory pointer directly.
  140. /// It's usually used to get the output data pointer.
  141. ///
  142. /// \param[in] pd_tensor tensor.
  143. /// \param[out] place To get the device type of the tensor.
  144. /// \param[out] size To get the data size of the tensor.
  145. /// \return The tensor data buffer pointer.
  146. ///
  147. PADDLE_CAPI_EXPORT extern uint8_t* PD_TensorDataUint8(
  148. __pd_keep PD_Tensor* pd_tensor, PD_PlaceType* place, int32_t* size);
  149. ///
  150. /// \brief Get the memory pointer directly.
  151. /// It's usually used to get the output data pointer.
  152. ///
  153. /// \param[in] pd_tensor tensor.
  154. /// \param[out] place To get the device type of the tensor.
  155. /// \param[out] size To get the data size of the tensor.
  156. /// \return The tensor data buffer pointer.
  157. ///
  158. PADDLE_CAPI_EXPORT extern int8_t* PD_TensorDataInt8(
  159. __pd_keep PD_Tensor* pd_tensor, PD_PlaceType* place, int32_t* size);
  160. ///
  161. /// \brief Copy the host memory to tensor data.
  162. /// It's usually used to set the input tensor data.
  163. /// \param[in] pd_tensor tensor.
  164. /// \param[in] data The pointer of the data, from which the tensor will copy.
  165. ///
  166. PADDLE_CAPI_EXPORT extern void PD_TensorCopyFromCpuFloat(
  167. __pd_keep PD_Tensor* pd_tensor, const float* data);
  168. ///
  169. /// \brief Copy the host memory to tensor data.
  170. /// It's usually used to set the input tensor data.
  171. /// \param[in] pd_tensor tensor.
  172. /// \param[in] data The pointer of the data, from which the tensor will copy.
  173. ///
  174. PADDLE_CAPI_EXPORT extern void PD_TensorCopyFromCpuInt64(
  175. __pd_keep PD_Tensor* pd_tensor, const int64_t* data);
  176. ///
  177. /// \brief Copy the host memory to tensor data.
  178. /// It's usually used to set the input tensor data.
  179. /// \param[in] pd_tensor tensor.
  180. /// \param[in] data The pointer of the data, from which the tensor will copy.
  181. ///
  182. PADDLE_CAPI_EXPORT extern void PD_TensorCopyFromCpuInt32(
  183. __pd_keep PD_Tensor* pd_tensor, const int32_t* data);
  184. ///
  185. /// \brief Copy the host memory to tensor data.
  186. /// It's usually used to set the input tensor data.
  187. /// \param[in] pd_tensor tensor.
  188. /// \param[in] data The pointer of the data, from which the tensor will copy.
  189. ///
  190. PADDLE_CAPI_EXPORT extern void PD_TensorCopyFromCpuUint8(
  191. __pd_keep PD_Tensor* pd_tensor, const uint8_t* data);
  192. ///
  193. /// \brief Copy the host memory to tensor data.
  194. /// It's usually used to set the input tensor data.
  195. /// \param[in] pd_tensor tensor.
  196. /// \param[in] data The pointer of the data, from which the tensor will copy.
  197. ///
  198. PADDLE_CAPI_EXPORT extern void PD_TensorCopyFromCpuInt8(
  199. __pd_keep PD_Tensor* pd_tensor, const int8_t* data);
  200. ///
  201. /// \brief Copy the tensor data to the host memory.
  202. /// It's usually used to get the output tensor data.
  203. /// \param[in] pd_tensor tensor.
  204. /// \param[out] data The tensor will copy the data to the address.
  205. ///
  206. PADDLE_CAPI_EXPORT extern void PD_TensorCopyToCpuFloat(
  207. __pd_keep PD_Tensor* pd_tensor, float* data);
  208. ///
  209. /// \brief Copy the tensor data to the host memory.
  210. /// It's usually used to get the output tensor data.
  211. /// \param[in] pd_tensor tensor.
  212. /// \param[out] data The tensor will copy the data to the address.
  213. ///
  214. PADDLE_CAPI_EXPORT extern void PD_TensorCopyToCpuInt64(
  215. __pd_keep PD_Tensor* pd_tensor, int64_t* data);
  216. ///
  217. /// \brief Copy the tensor data to the host memory.
  218. /// It's usually used to get the output tensor data.
  219. /// \param[in] pd_tensor tensor.
  220. /// \param[out] data The tensor will copy the data to the address.
  221. ///
  222. PADDLE_CAPI_EXPORT extern void PD_TensorCopyToCpuInt32(
  223. __pd_keep PD_Tensor* pd_tensor, int32_t* data);
  224. ///
  225. /// \brief Copy the tensor data to the host memory.
  226. /// It's usually used to get the output tensor data.
  227. /// \param[in] pd_tensor tensor.
  228. /// \param[out] data The tensor will copy the data to the address.
  229. ///
  230. PADDLE_CAPI_EXPORT extern void PD_TensorCopyToCpuUint8(
  231. __pd_keep PD_Tensor* pd_tensor, uint8_t* data);
  232. ///
  233. /// \brief Copy the tensor data to the host memory.
  234. /// It's usually used to get the output tensor data.
  235. /// \param[in] pd_tensor tensor.
  236. /// \param[out] data The tensor will copy the data to the address.
  237. ///
  238. PADDLE_CAPI_EXPORT extern void PD_TensorCopyToCpuInt8(
  239. __pd_keep PD_Tensor* pd_tensor, int8_t* data);
  240. ///
  241. /// \brief Get the tensor shape
  242. /// \param[in] pd_tensor tensor.
  243. /// \return The tensor shape.
  244. ///
  245. PADDLE_CAPI_EXPORT extern __pd_give PD_OneDimArrayInt32* PD_TensorGetShape(
  246. __pd_keep PD_Tensor* pd_tensor);
  247. ///
  248. /// \brief Set the tensor lod information
  249. /// \param[in] pd_tensor tensor.
  250. /// \param[in] lod lod information.
  251. ///
  252. PADDLE_CAPI_EXPORT extern void PD_TensorSetLod(
  253. __pd_keep PD_Tensor* pd_tensor, __pd_keep PD_TwoDimArraySize* lod);
  254. ///
  255. /// \brief Get the tensor lod information
  256. /// \param[in] pd_tensor tensor.
  257. /// \return the lod information.
  258. ///
  259. PADDLE_CAPI_EXPORT extern __pd_give PD_TwoDimArraySize* PD_TensorGetLod(
  260. __pd_keep PD_Tensor* pd_tensor);
  261. ///
  262. /// \brief Get the tensor name
  263. /// \param[in] pd_tensor tensor.
  264. /// \return the tensor name.
  265. ///
  266. PADDLE_CAPI_EXPORT extern const char* PD_TensorGetName(
  267. __pd_keep PD_Tensor* pd_tensor);
  268. ///
  269. /// \brief Get the tensor data type
  270. /// \param[in] pd_tensor tensor.
  271. /// \return the tensor data type.
  272. ///
  273. PADDLE_CAPI_EXPORT extern PD_DataType PD_TensorGetDataType(
  274. __pd_keep PD_Tensor* pd_tensor);
  275. #ifdef __cplusplus
  276. } // extern "C"
  277. #endif