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

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