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.

inference.h 3.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586
  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_MS_SESSION_H
  17. #define MINDSPORE_INCLUDE_MS_SESSION_H
  18. #include <memory>
  19. #include <vector>
  20. #include <string>
  21. #include "include/infer_tensor.h"
  22. #include "include/infer_log.h"
  23. namespace mindspore {
  24. namespace inference {
  25. enum StatusCode { SUCCESS = 0, FAILED, INVALID_INPUTS };
  26. class Status {
  27. public:
  28. Status() : status_code_(FAILED) {}
  29. Status(enum StatusCode status_code, const std::string &status_msg = "")
  30. : status_code_(status_code), status_msg_(status_msg) {}
  31. bool IsSuccess() const { return status_code_ == SUCCESS; }
  32. enum StatusCode StatusCode() const { return status_code_; }
  33. std::string StatusMessage() const { return status_msg_; }
  34. bool operator==(const Status &other) const { return status_code_ == other.status_code_; }
  35. bool operator==(enum StatusCode other_code) const { return status_code_ == other_code; }
  36. bool operator!=(const Status &other) const { return status_code_ != other.status_code_; }
  37. bool operator!=(enum StatusCode other_code) const { return status_code_ != other_code; }
  38. operator bool() const = delete;
  39. Status &operator<(const LogStream &stream) noexcept __attribute__((visibility("default"))) {
  40. status_msg_ = stream.sstream_->str();
  41. return *this;
  42. }
  43. private:
  44. enum StatusCode status_code_;
  45. std::string status_msg_;
  46. };
  47. class MS_API InferSession {
  48. public:
  49. InferSession() = default;
  50. virtual ~InferSession() = default;
  51. virtual Status InitEnv(const std::string &device_type, uint32_t device_id) = 0;
  52. virtual Status FinalizeEnv() = 0;
  53. virtual Status LoadModelFromFile(const std::string &file_name, uint32_t &model_id) = 0;
  54. virtual Status UnloadModel(uint32_t model_id) = 0;
  55. // override this method to avoid request/reply data copy
  56. virtual Status ExecuteModel(uint32_t model_id, const RequestBase &request, ReplyBase &reply) = 0;
  57. virtual Status ExecuteModel(uint32_t model_id, const std::vector<InferTensor> &inputs,
  58. std::vector<InferTensor> &outputs) {
  59. VectorInferTensorWrapRequest request(inputs);
  60. VectorInferTensorWrapReply reply(outputs);
  61. return ExecuteModel(model_id, request, reply);
  62. }
  63. // default not support input data preprocess(decode, resize, crop, crop&paste, etc.)
  64. virtual Status ExecuteModel(uint32_t /*model_id*/,
  65. const ImagesRequestBase & /*images_inputs*/, // images for preprocess
  66. const RequestBase & /*request*/, ReplyBase & /*reply*/) {
  67. return FAILED;
  68. }
  69. virtual Status GetModelInputsInfo(uint32_t graph_id, std::vector<inference::InferTensor> *tensor_list) const {
  70. Status status(SUCCESS);
  71. return status;
  72. }
  73. static std::shared_ptr<InferSession> CreateSession(const std::string &device, uint32_t device_id);
  74. };
  75. } // namespace inference
  76. } // namespace mindspore
  77. #endif // MINDSPORE_INCLUDE_MS_SESSION_H