You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

infer_tensor.h 6.8 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217
  1. /**
  2. * Copyright 2020 Huawei Technologies Co., Ltd
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. #ifndef MINDSPORE_INCLUDE_INFER_TENSOR_H_
  17. #define MINDSPORE_INCLUDE_INFER_TENSOR_H_
  18. #include <utility>
  19. #include <vector>
  20. #include <memory>
  21. #include <numeric>
  22. #include <map>
  23. #include <functional>
  24. #include "securec/include/securec.h"
  25. #include "include/infer_log.h"
  26. namespace mindspore {
  27. #define MS_API __attribute__((visibility("default")))
  28. namespace inference {
  29. enum DataType {
  30. kMSI_Unknown = 0,
  31. kMSI_Bool = 1,
  32. kMSI_Int8 = 2,
  33. kMSI_Int16 = 3,
  34. kMSI_Int32 = 4,
  35. kMSI_Int64 = 5,
  36. kMSI_Uint8 = 6,
  37. kMSI_Uint16 = 7,
  38. kMSI_Uint32 = 8,
  39. kMSI_Uint64 = 9,
  40. kMSI_Float16 = 10,
  41. kMSI_Float32 = 11,
  42. kMSI_Float64 = 12,
  43. };
  44. class InferTensorBase {
  45. public:
  46. InferTensorBase() = default;
  47. virtual ~InferTensorBase() = default;
  48. virtual DataType data_type() const = 0;
  49. virtual void set_data_type(DataType type) = 0;
  50. virtual std::vector<int64_t> shape() const = 0;
  51. virtual void set_shape(const std::vector<int64_t> &shape) = 0;
  52. virtual const void *data() const = 0;
  53. virtual size_t data_size() const = 0;
  54. virtual bool resize_data(size_t data_len) = 0;
  55. virtual void *mutable_data() = 0;
  56. bool set_data(const void *data, size_t data_len) {
  57. resize_data(data_len);
  58. if (mutable_data() == nullptr) {
  59. MSI_LOG_ERROR << "set data failed, data len " << data_len;
  60. return false;
  61. }
  62. if (data_size() != data_len) {
  63. MSI_LOG_ERROR << "set data failed, tensor current data size " << data_size() << " not match data len "
  64. << data_len;
  65. return false;
  66. }
  67. if (data_len == 0) {
  68. return true;
  69. }
  70. auto ret = memcpy_s(mutable_data(), data_size(), data, data_len);
  71. if (ret != 0) {
  72. MSI_LOG_ERROR << "Set data memcpy_s failed";
  73. return false;
  74. }
  75. return true;
  76. }
  77. int64_t ElementNum() const {
  78. std::vector<int64_t> shapex = shape();
  79. return std::accumulate(shapex.begin(), shapex.end(), 1LL, std::multiplies<int64_t>());
  80. }
  81. int GetTypeSize(DataType type) const {
  82. const std::map<DataType, size_t> type_size_map{
  83. {kMSI_Bool, sizeof(bool)}, {kMSI_Float64, sizeof(double)}, {kMSI_Int8, sizeof(int8_t)},
  84. {kMSI_Uint8, sizeof(uint8_t)}, {kMSI_Int16, sizeof(int16_t)}, {kMSI_Uint16, sizeof(uint16_t)},
  85. {kMSI_Int32, sizeof(int32_t)}, {kMSI_Uint32, sizeof(uint32_t)}, {kMSI_Int64, sizeof(int64_t)},
  86. {kMSI_Uint64, sizeof(uint64_t)}, {kMSI_Float16, sizeof(uint16_t)}, {kMSI_Float32, sizeof(float)},
  87. };
  88. auto it = type_size_map.find(type);
  89. if (it != type_size_map.end()) {
  90. return it->second;
  91. }
  92. return 0;
  93. }
  94. };
  95. class InferTensor : public InferTensorBase {
  96. public:
  97. DataType type_;
  98. std::vector<int64_t> shape_;
  99. std::vector<uint8_t> data_;
  100. public:
  101. InferTensor() = default;
  102. ~InferTensor() = default;
  103. InferTensor(DataType type, std::vector<int64_t> shape, const void *data, size_t data_len) {
  104. set_data_type(type);
  105. set_shape(shape);
  106. set_data(data, data_len);
  107. }
  108. void set_data_type(DataType type) override { type_ = type; }
  109. DataType data_type() const override { return type_; }
  110. void set_shape(const std::vector<int64_t> &shape) override { shape_ = shape; }
  111. std::vector<int64_t> shape() const override { return shape_; }
  112. const void *data() const override { return data_.data(); }
  113. size_t data_size() const override { return data_.size(); }
  114. bool resize_data(size_t data_len) override {
  115. data_.resize(data_len);
  116. return true;
  117. }
  118. void *mutable_data() override { return data_.data(); }
  119. };
  120. class InferImagesBase {
  121. public:
  122. InferImagesBase() = default;
  123. virtual ~InferImagesBase() = default;
  124. virtual size_t batch_size() const = 0;
  125. virtual bool get(size_t index, const void *&pic_buffer, uint32_t &pic_size) const = 0;
  126. virtual size_t input_index() const = 0; // the index of images as input in model
  127. };
  128. class RequestBase {
  129. public:
  130. RequestBase() = default;
  131. virtual ~RequestBase() = default;
  132. virtual size_t size() const = 0;
  133. virtual const InferTensorBase *operator[](size_t index) const = 0;
  134. };
  135. class ImagesRequestBase {
  136. public:
  137. ImagesRequestBase() = default;
  138. virtual ~ImagesRequestBase() = default;
  139. virtual size_t size() const = 0;
  140. virtual const InferImagesBase *operator[](size_t index) const = 0;
  141. };
  142. class ReplyBase {
  143. public:
  144. ReplyBase() = default;
  145. virtual ~ReplyBase() = default;
  146. virtual size_t size() const = 0;
  147. virtual InferTensorBase *operator[](size_t index) = 0;
  148. virtual const InferTensorBase *operator[](size_t index) const = 0;
  149. virtual InferTensorBase *add() = 0;
  150. virtual void clear() = 0;
  151. };
  152. class VectorInferTensorWrapReply : public ReplyBase {
  153. public:
  154. explicit VectorInferTensorWrapReply(std::vector<InferTensor> &tensor_list) : tensor_list_(tensor_list) {}
  155. ~VectorInferTensorWrapReply() = default;
  156. size_t size() const { return tensor_list_.size(); }
  157. InferTensorBase *operator[](size_t index) {
  158. if (index >= tensor_list_.size()) {
  159. MSI_LOG_ERROR << "visit invalid index " << index << " total size " << tensor_list_.size();
  160. return nullptr;
  161. }
  162. return &(tensor_list_[index]);
  163. }
  164. const InferTensorBase *operator[](size_t index) const {
  165. if (index >= tensor_list_.size()) {
  166. MSI_LOG_ERROR << "visit invalid index " << index << " total size " << tensor_list_.size();
  167. return nullptr;
  168. }
  169. return &(tensor_list_[index]);
  170. }
  171. InferTensorBase *add() {
  172. tensor_list_.push_back(InferTensor());
  173. return &(tensor_list_.back());
  174. }
  175. void clear() { tensor_list_.clear(); }
  176. std::vector<InferTensor> &tensor_list_;
  177. };
  178. class VectorInferTensorWrapRequest : public RequestBase {
  179. public:
  180. explicit VectorInferTensorWrapRequest(const std::vector<InferTensor> &tensor_list) : tensor_list_(tensor_list) {}
  181. ~VectorInferTensorWrapRequest() = default;
  182. size_t size() const { return tensor_list_.size(); }
  183. const InferTensorBase *operator[](size_t index) const {
  184. if (index >= tensor_list_.size()) {
  185. MSI_LOG_ERROR << "visit invalid index " << index << " total size " << tensor_list_.size();
  186. return nullptr;
  187. }
  188. return &(tensor_list_[index]);
  189. }
  190. const std::vector<InferTensor> &tensor_list_;
  191. };
  192. } // namespace inference
  193. } // namespace mindspore
  194. #endif // MINDSPORE_INCLUDE_INFER_TENSOR_H_