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

5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184
  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. enum ServableType {
  71. kServableTypeUnknown = 0,
  72. kServableTypeLocal = 1,
  73. kServableTypeDistributed = 2,
  74. };
  75. struct CommonServableMeta {
  76. std::string servable_name;
  77. bool with_batch_dim = true; // whether there is batch dim in model's inputs/outputs
  78. std::vector<int> without_batch_dim_inputs;
  79. size_t inputs_count = 0;
  80. size_t outputs_count = 0;
  81. };
  82. struct MS_API LocalServableMeta {
  83. std::string servable_file; // file name
  84. ModelType model_format = ModelType::kUnknownType; // OM, MindIR
  85. std::map<std::string, std::string> load_options; // Acl options
  86. void SetModelFormat(const std::string &format);
  87. };
  88. struct DistributedServableMeta {
  89. size_t rank_size = 0;
  90. size_t stage_size = 0;
  91. };
  92. struct MS_API ServableMeta {
  93. ServableType servable_type = kServableTypeUnknown;
  94. CommonServableMeta common_meta;
  95. LocalServableMeta local_meta;
  96. DistributedServableMeta distributed_meta;
  97. std::string Repr() const;
  98. };
  99. struct ServableSignature {
  100. ServableMeta servable_meta;
  101. std::vector<MethodSignature> methods;
  102. Status Check() const;
  103. bool GetMethodDeclare(const std::string &method_name, MethodSignature *method);
  104. private:
  105. Status CheckPreprocessInput(const MethodSignature &method, size_t *pre) const;
  106. Status CheckPredictInput(const MethodSignature &method, size_t pre) const;
  107. Status CheckPostprocessInput(const MethodSignature &method, size_t pre, size_t *post) const;
  108. Status CheckReturn(const MethodSignature &method, size_t pre, size_t post) const;
  109. };
  110. class MS_API ServableStorage {
  111. public:
  112. void Register(const ServableSignature &def);
  113. Status RegisterMethod(const MethodSignature &method);
  114. bool GetServableDef(const std::string &model_name, ServableSignature *def) const;
  115. Status DeclareServable(ServableMeta servable);
  116. Status DeclareDistributedServable(ServableMeta servable);
  117. Status RegisterInputOutputInfo(const std::string &servable_name, size_t inputs_count, size_t outputs_count);
  118. std::vector<size_t> GetInputOutputInfo(const std::string &servable_name) const;
  119. void Clear();
  120. static ServableStorage &Instance();
  121. private:
  122. std::unordered_map<std::string, ServableSignature> servable_signatures_map_;
  123. };
  124. static inline LogStream &operator<<(LogStream &stream, PredictPhaseTag data_type) {
  125. switch (data_type) {
  126. case kPredictPhaseTag_Input:
  127. stream << "Input";
  128. break;
  129. case kPredictPhaseTag_Preproces:
  130. stream << "Preprocess";
  131. break;
  132. case kPredictPhaseTag_Predict:
  133. stream << "Predict";
  134. break;
  135. case kPredictPhaseTag_Postprocess:
  136. stream << "Postprocess";
  137. break;
  138. case kPredictPhaseTag_Output:
  139. stream << "Output";
  140. break;
  141. }
  142. return stream;
  143. }
  144. struct WorkerAgentSpec {
  145. std::string agent_address;
  146. uint32_t rank_id = 0;
  147. std::vector<TensorInfo> input_infos;
  148. std::vector<TensorInfo> output_infos;
  149. uint32_t batch_size = 0;
  150. };
  151. } // namespace mindspore::serving
  152. #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.