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.7 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  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. memcpy_s(mutable_data(), data_size(), data, data_len);
  71. return true;
  72. }
  73. int64_t ElementNum() const {
  74. std::vector<int64_t> shapex = shape();
  75. return std::accumulate(shapex.begin(), shapex.end(), 1LL, std::multiplies<int64_t>());
  76. }
  77. int GetTypeSize(DataType type) const {
  78. const std::map<DataType, size_t> type_size_map{
  79. {kMSI_Bool, sizeof(bool)}, {kMSI_Float64, sizeof(double)}, {kMSI_Int8, sizeof(int8_t)},
  80. {kMSI_Uint8, sizeof(uint8_t)}, {kMSI_Int16, sizeof(int16_t)}, {kMSI_Uint16, sizeof(uint16_t)},
  81. {kMSI_Int32, sizeof(int32_t)}, {kMSI_Uint32, sizeof(uint32_t)}, {kMSI_Int64, sizeof(int64_t)},
  82. {kMSI_Uint64, sizeof(uint64_t)}, {kMSI_Float16, sizeof(uint16_t)}, {kMSI_Float32, sizeof(float)},
  83. };
  84. auto it = type_size_map.find(type);
  85. if (it != type_size_map.end()) {
  86. return it->second;
  87. }
  88. return 0;
  89. }
  90. };
  91. class InferTensor : public InferTensorBase {
  92. public:
  93. DataType type_;
  94. std::vector<int64_t> shape_;
  95. std::vector<uint8_t> data_;
  96. public:
  97. InferTensor() = default;
  98. ~InferTensor() = default;
  99. InferTensor(DataType type, std::vector<int64_t> shape, const void *data, size_t data_len) {
  100. set_data_type(type);
  101. set_shape(shape);
  102. set_data(data, data_len);
  103. }
  104. void set_data_type(DataType type) override { type_ = type; }
  105. DataType data_type() const override { return type_; }
  106. void set_shape(const std::vector<int64_t> &shape) override { shape_ = shape; }
  107. std::vector<int64_t> shape() const override { return shape_; }
  108. const void *data() const override { return data_.data(); }
  109. size_t data_size() const override { return data_.size(); }
  110. bool resize_data(size_t data_len) override {
  111. data_.resize(data_len);
  112. return true;
  113. }
  114. void *mutable_data() override { return data_.data(); }
  115. };
  116. class InferImagesBase {
  117. public:
  118. InferImagesBase() = default;
  119. virtual ~InferImagesBase() = default;
  120. virtual size_t batch_size() const = 0;
  121. virtual bool get(size_t index, const void *&pic_buffer, uint32_t &pic_size) const = 0;
  122. virtual size_t input_index() const = 0; // the index of images as input in model
  123. };
  124. class RequestBase {
  125. public:
  126. RequestBase() = default;
  127. virtual ~RequestBase() = default;
  128. virtual size_t size() const = 0;
  129. virtual const InferTensorBase *operator[](size_t index) const = 0;
  130. };
  131. class ImagesRequestBase {
  132. public:
  133. ImagesRequestBase() = default;
  134. virtual ~ImagesRequestBase() = default;
  135. virtual size_t size() const = 0;
  136. virtual const InferImagesBase *operator[](size_t index) const = 0;
  137. };
  138. class ReplyBase {
  139. public:
  140. ReplyBase() = default;
  141. virtual ~ReplyBase() = default;
  142. virtual size_t size() const = 0;
  143. virtual InferTensorBase *operator[](size_t index) = 0;
  144. virtual const InferTensorBase *operator[](size_t index) const = 0;
  145. virtual InferTensorBase *add() = 0;
  146. virtual void clear() = 0;
  147. };
  148. class VectorInferTensorWrapReply : public ReplyBase {
  149. public:
  150. explicit VectorInferTensorWrapReply(std::vector<InferTensor> &tensor_list) : tensor_list_(tensor_list) {}
  151. ~VectorInferTensorWrapReply() = default;
  152. size_t size() const { return tensor_list_.size(); }
  153. InferTensorBase *operator[](size_t index) {
  154. if (index >= tensor_list_.size()) {
  155. MSI_LOG_ERROR << "visit invalid index " << index << " total size " << tensor_list_.size();
  156. return nullptr;
  157. }
  158. return &(tensor_list_[index]);
  159. }
  160. const InferTensorBase *operator[](size_t index) const {
  161. if (index >= tensor_list_.size()) {
  162. MSI_LOG_ERROR << "visit invalid index " << index << " total size " << tensor_list_.size();
  163. return nullptr;
  164. }
  165. return &(tensor_list_[index]);
  166. }
  167. InferTensorBase *add() {
  168. tensor_list_.push_back(InferTensor());
  169. return &(tensor_list_.back());
  170. }
  171. void clear() { tensor_list_.clear(); }
  172. std::vector<InferTensor> &tensor_list_;
  173. };
  174. class VectorInferTensorWrapRequest : public RequestBase {
  175. public:
  176. explicit VectorInferTensorWrapRequest(const std::vector<InferTensor> &tensor_list) : tensor_list_(tensor_list) {}
  177. ~VectorInferTensorWrapRequest() = default;
  178. size_t size() const { return tensor_list_.size(); }
  179. const InferTensorBase *operator[](size_t index) const {
  180. if (index >= tensor_list_.size()) {
  181. MSI_LOG_ERROR << "visit invalid index " << index << " total size " << tensor_list_.size();
  182. return nullptr;
  183. }
  184. return &(tensor_list_[index]);
  185. }
  186. const std::vector<InferTensor> &tensor_list_;
  187. };
  188. } // namespace inference
  189. } // namespace mindspore
  190. #endif // MINDSPORE_INCLUDE_INFER_TENSOR_H_