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.

servable.h 4.1 kB

5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  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_SERVING_SERVABLE_H
  17. #define MINDSPORE_SERVING_SERVABLE_H
  18. #include <utility>
  19. #include <unordered_map>
  20. #include <string>
  21. #include <vector>
  22. #include <memory>
  23. #include <map>
  24. #include "common/serving_common.h"
  25. #include "worker/inference/inference.h"
  26. namespace mindspore::serving {
  27. enum PredictPhaseTag {
  28. kPredictPhaseTag_Input,
  29. kPredictPhaseTag_Preproces,
  30. kPredictPhaseTag_Predict,
  31. kPredictPhaseTag_Postprocess,
  32. kPredictPhaseTag_Output,
  33. };
  34. struct MethodSignature {
  35. std::string method_name;
  36. std::vector<std::string> inputs;
  37. std::vector<std::string> outputs;
  38. std::string preprocess_name;
  39. // the output index of the 'predict phase'
  40. std::vector<std::pair<PredictPhaseTag, uint64_t>> preprocess_inputs;
  41. std::string postprocess_name;
  42. std::vector<std::pair<PredictPhaseTag, uint64_t>> postprocess_inputs;
  43. std::string servable_name;
  44. std::vector<std::pair<PredictPhaseTag, uint64_t>> servable_inputs;
  45. std::vector<std::pair<PredictPhaseTag, uint64_t>> returns;
  46. };
  47. struct LoadServableSpec {
  48. std::string servable_directory;
  49. std::string servable_name;
  50. uint64_t version_number = 0;
  51. std::string Repr() const;
  52. };
  53. struct WorkerMethodInfo {
  54. std::string name;
  55. std::vector<std::string> input_names;
  56. };
  57. struct WorkerSpec {
  58. std::string servable_name;
  59. uint64_t version_number = 0;
  60. std::string worker_address;
  61. std::vector<WorkerMethodInfo> methods;
  62. std::string Repr() const;
  63. };
  64. struct RequestSpec {
  65. std::string servable_name;
  66. std::string method_name;
  67. uint64_t version_number = 0; // not specified
  68. std::string Repr() const;
  69. };
  70. struct MS_API ServableMeta {
  71. std::string servable_name;
  72. std::string servable_file; // file name
  73. ModelType model_format; // OM, MindIR
  74. bool with_batch_dim = true; // whether there is batch dim in model's inputs/outputs
  75. size_t inputs_count = 0;
  76. size_t outputs_count = 0;
  77. std::map<std::string, std::string> load_options; // Acl options
  78. std::vector<int> without_batch_dim_inputs;
  79. std::string Repr() const;
  80. void SetModelFormat(const std::string &format);
  81. };
  82. struct ServableSignature {
  83. ServableMeta servable_meta;
  84. std::vector<MethodSignature> methods;
  85. Status Check() const;
  86. bool GetMethodDeclare(const std::string &method_name, MethodSignature *method);
  87. };
  88. class MS_API ServableStorage {
  89. public:
  90. void Register(const ServableSignature &def);
  91. Status RegisterMethod(const MethodSignature &method);
  92. bool GetServableDef(const std::string &model_name, ServableSignature *def) const;
  93. void DeclareServable(const ServableMeta &servable);
  94. Status RegisterInputOutputInfo(const std::string &servable_name, size_t inputs_count, size_t outputs_count);
  95. std::vector<size_t> GetInputOutputInfo(const std::string &servable_name) const;
  96. void Clear();
  97. static ServableStorage &Instance();
  98. private:
  99. std::unordered_map<std::string, ServableSignature> servable_signatures_map_;
  100. };
  101. static inline LogStream &operator<<(LogStream &stream, PredictPhaseTag data_type) {
  102. switch (data_type) {
  103. case kPredictPhaseTag_Input:
  104. stream << "Input";
  105. break;
  106. case kPredictPhaseTag_Preproces:
  107. stream << "Preprocess";
  108. break;
  109. case kPredictPhaseTag_Predict:
  110. stream << "Predict";
  111. break;
  112. case kPredictPhaseTag_Postprocess:
  113. stream << "Postprocess";
  114. break;
  115. case kPredictPhaseTag_Output:
  116. stream << "Output";
  117. break;
  118. }
  119. return stream;
  120. }
  121. } // namespace mindspore::serving
  122. #endif // MINDSPORE_SERVING_SERVABLE_H

A lightweight and high-performance service module that helps MindSpore developers efficiently deploy online inference services in the production environment.