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.

serving_py.cc 11 kB

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236
  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. #include <string>
  17. #include "python/worker/preprocess_py.h"
  18. #include "python/worker/postprocess_py.h"
  19. #include "python/worker/worker_py.h"
  20. #include "python/worker/servable_py.h"
  21. #include "python/tensor_py.h"
  22. #include "common/servable.h"
  23. #include "master/master_context.h"
  24. #include "worker/context.h"
  25. #include "python/master/master_py.h"
  26. #include "python/agent/agent_py.h"
  27. #include "common/exit_handle.h"
  28. #include "worker/distributed_worker/worker_agent.h"
  29. namespace mindspore::serving {
  30. void PyRegServable(pybind11::module *m_ptr) {
  31. auto &m = *m_ptr;
  32. // avoid as numpy object memory copy in PyTensor::AsPythonData
  33. py::class_<TensorBase, TensorBasePtr>(m, "Tensor_");
  34. py::class_<PyPreprocessStorage, std::shared_ptr<PyPreprocessStorage>>(m, "PreprocessStorage_")
  35. .def(py::init<>())
  36. .def_static("get_instance", &PyPreprocessStorage::Instance)
  37. .def("register", &PyPreprocessStorage::Register)
  38. .def("get_pycpp_preprocess_info", &PyPreprocessStorage::GetPyCppPreprocessInfo);
  39. py::class_<PyPostprocessStorage, std::shared_ptr<PyPostprocessStorage>>(m, "PostprocessStorage_")
  40. .def(py::init<>())
  41. .def_static("get_instance", &PyPostprocessStorage::Instance)
  42. .def("register", &PyPostprocessStorage::Register)
  43. .def("get_pycpp_postprocess_info", &PyPostprocessStorage::GetPyCppPostprocessInfo);
  44. py::enum_<PredictPhaseTag>(m, "PredictPhaseTag_")
  45. .value("kPredictPhaseTag_Input", PredictPhaseTag::kPredictPhaseTag_Input)
  46. .value("kPredictPhaseTag_Preproces", PredictPhaseTag::kPredictPhaseTag_Preproces)
  47. .value("kPredictPhaseTag_Predict", PredictPhaseTag::kPredictPhaseTag_Predict)
  48. .value("kPredictPhaseTag_Postprocess", PredictPhaseTag::kPredictPhaseTag_Postprocess)
  49. .export_values();
  50. py::class_<MethodSignature>(m, "MethodSignature_")
  51. .def(py::init<>())
  52. .def_readwrite("method_name", &MethodSignature::method_name)
  53. .def_readwrite("inputs", &MethodSignature::inputs)
  54. .def_readwrite("outputs", &MethodSignature::outputs)
  55. .def_readwrite("preprocess_name", &MethodSignature::preprocess_name)
  56. .def_readwrite("preprocess_inputs", &MethodSignature::preprocess_inputs)
  57. .def_readwrite("postprocess_name", &MethodSignature::postprocess_name)
  58. .def_readwrite("postprocess_inputs", &MethodSignature::postprocess_inputs)
  59. .def_readwrite("servable_name", &MethodSignature::servable_name)
  60. .def_readwrite("servable_inputs", &MethodSignature::servable_inputs)
  61. .def_readwrite("returns", &MethodSignature::returns);
  62. py::class_<RequestSpec>(m, "RequestSpec_")
  63. .def(py::init<>())
  64. .def_readwrite("servable_name", &RequestSpec::servable_name)
  65. .def_readwrite("version_number", &RequestSpec::version_number)
  66. .def_readwrite("method_name", &RequestSpec::method_name);
  67. py::class_<CommonServableMeta>(m, "CommonServableMeta_")
  68. .def(py::init<>())
  69. .def_readwrite("servable_name", &CommonServableMeta::servable_name)
  70. .def_readwrite("inputs_count", &CommonServableMeta::inputs_count)
  71. .def_readwrite("outputs_count", &CommonServableMeta::outputs_count)
  72. .def_readwrite("with_batch_dim", &CommonServableMeta::with_batch_dim)
  73. .def_readwrite("without_batch_dim_inputs", &CommonServableMeta::without_batch_dim_inputs);
  74. py::class_<LocalServableMeta>(m, "LocalServableMeta_")
  75. .def(py::init<>())
  76. .def_readwrite("servable_file", &LocalServableMeta::servable_file)
  77. .def_readwrite("options", &LocalServableMeta::load_options)
  78. .def("set_model_format", &LocalServableMeta::SetModelFormat);
  79. py::class_<DistributedServableMeta>(m, "DistributedServableMeta_")
  80. .def(py::init<>())
  81. .def_readwrite("rank_size", &DistributedServableMeta::rank_size)
  82. .def_readwrite("stage_size", &DistributedServableMeta::stage_size);
  83. py::class_<ServableMeta>(m, "ServableMeta_")
  84. .def(py::init<>())
  85. .def_readwrite("common_meta", &ServableMeta::common_meta)
  86. .def_readwrite("local_meta", &ServableMeta::local_meta)
  87. .def_readwrite("distributed_meta", &ServableMeta::distributed_meta);
  88. py::class_<ServableSignature>(m, "ServableSignature_")
  89. .def(py::init<>())
  90. .def_readwrite("servable_meta", &ServableSignature::servable_meta)
  91. .def_readwrite("methods", &ServableSignature::methods);
  92. py::class_<PyServableStorage>(m, "ServableStorage_")
  93. .def_static("register_servable_input_output_info", &PyServableStorage::RegisterInputOutputInfo)
  94. .def_static("register_method", &PyServableStorage::RegisterMethod)
  95. .def_static("declare_servable", &PyServableStorage::DeclareServable)
  96. .def_static("declare_distributed_servable", &PyServableStorage::DeclareDistributedServable);
  97. py::class_<OneRankConfig>(m, "OneRankConfig_")
  98. .def(py::init<>())
  99. .def_readwrite("device_id", &OneRankConfig::device_id)
  100. .def_readwrite("ip", &OneRankConfig::ip);
  101. py::class_<DistributedServableConfig>(m, "DistributedServableConfig_")
  102. .def(py::init<>())
  103. .def_readwrite("common_meta", &DistributedServableConfig::common_meta)
  104. .def_readwrite("distributed_meta", &DistributedServableConfig::distributed_meta)
  105. .def_readwrite("rank_table_content", &DistributedServableConfig::rank_table_content)
  106. .def_readwrite("rank_list", &DistributedServableConfig::rank_list);
  107. }
  108. void PyRegMaster(pybind11::module *m_ptr) {
  109. auto &m = *m_ptr;
  110. py::class_<PyMaster>(m, "Master_")
  111. .def_static("start_grpc_server", &PyMaster::StartGrpcServer)
  112. .def_static("start_grpc_master_server", &PyMaster::StartGrpcMasterServer)
  113. .def_static("start_restful_server", &PyMaster::StartRestfulServer)
  114. .def_static("wait_and_clear", &PyMaster::WaitAndClear)
  115. .def_static("stop_and_clear", &PyMaster::StopAndClear);
  116. }
  117. void PyRegWorker(pybind11::module *m_ptr) {
  118. auto &m = *m_ptr;
  119. py::class_<TaskContext>(m, "TaskContext_").def(py::init<>());
  120. py::class_<TaskItem>(m, "TaskItem_")
  121. .def(py::init<>())
  122. .def_readwrite("task_type", &TaskItem::task_type)
  123. .def_readwrite("name", &TaskItem::name)
  124. .def_property_readonly("instance_list",
  125. [](const TaskItem &item) {
  126. py::tuple instances(item.instance_list.size());
  127. for (size_t i = 0; i < item.instance_list.size(); i++) {
  128. instances[i] = PyTensor::AsNumpyTuple(item.instance_list[i]->data);
  129. }
  130. return instances;
  131. })
  132. .def_readwrite("context_list", &TaskItem::context_list);
  133. py::class_<PyWorker>(m, "Worker_")
  134. .def_static("start_servable", &PyWorker::StartServable)
  135. .def_static("start_servable_in_master", &PyWorker::StartServableInMaster)
  136. .def_static("start_distributed_servable", &PyWorker::StartDistributedServable)
  137. .def_static("start_distributed_servable_in_master", &PyWorker::StartDistributedServableInMaster)
  138. .def_static("get_batch_size", &PyWorker::GetBatchSize)
  139. .def_static("wait_and_clear", &PyWorker::WaitAndClear)
  140. .def_static("stop_and_clear", PyWorker::StopAndClear)
  141. .def_static("get_py_task", &PyWorker::GetPyTask, py::call_guard<py::gil_scoped_release>())
  142. .def_static("try_get_preprocess_py_task", &PyWorker::TryGetPreprocessPyTask)
  143. .def_static("try_get_postprocess_py_task", &PyWorker::TryGetPostprocessPyTask)
  144. .def_static("push_preprocess_result", &PyWorker::PushPreprocessPyResult)
  145. .def_static("push_preprocess_failed", &PyWorker::PushPreprocessPyFailed)
  146. .def_static("push_postprocess_result", &PyWorker::PushPostprocessPyResult)
  147. .def_static("push_postprocess_failed", &PyWorker::PushPostprocessPyFailed)
  148. .def_static("get_device_type", &PyWorker::GetDeviceType);
  149. py::class_<ServableContext, std::shared_ptr<ServableContext>>(m, "ServableContext_")
  150. .def(py::init<>())
  151. .def_static("get_instance", &ServableContext::Instance)
  152. .def("set_device_type_str",
  153. [](ServableContext &context, const std::string &device_type) {
  154. auto status = context.SetDeviceTypeStr(device_type);
  155. if (status != SUCCESS) {
  156. MSI_LOG_EXCEPTION << "Raise failed: " << status.StatusMessage();
  157. }
  158. })
  159. .def("set_device_id", &ServableContext::SetDeviceId);
  160. py::class_<MasterContext, std::shared_ptr<MasterContext>>(m, "MasterContext_")
  161. .def(py::init<>())
  162. .def_static("get_instance", &MasterContext::Instance)
  163. .def("set_max_enqueued_requests", &MasterContext::SetMaxEnqueuedRequests);
  164. }
  165. void PyRegWorkerAgent(pybind11::module *m_ptr) {
  166. auto &m = *m_ptr;
  167. py::class_<PyAgent>(m, "WorkerAgent_")
  168. .def_static("get_agents_config_from_worker", &PyAgent::GetAgentsConfigsFromWorker)
  169. .def_static("wait_and_clear", &PyAgent::WaitAndClear)
  170. .def_static("stop_and_clear", &PyAgent::StopAndClear)
  171. .def_static("notify_failed", &PyAgent::NotifyFailed)
  172. .def_static("startup_notify_exit", &PyAgent::StartupNotifyExit)
  173. .def_static("start_agent", &PyAgent::StartAgent);
  174. py::class_<AgentStartUpConfig>(m, "AgentStartUpConfig_")
  175. .def(py::init<>())
  176. .def_readwrite("rank_id", &AgentStartUpConfig::rank_id)
  177. .def_readwrite("device_id", &AgentStartUpConfig::device_id)
  178. .def_readwrite("model_file_name", &AgentStartUpConfig::model_file_name)
  179. .def_readwrite("group_file_name", &AgentStartUpConfig::group_file_name)
  180. .def_readwrite("rank_table_json_file_name", &AgentStartUpConfig::rank_table_json_file_name)
  181. .def_readwrite("agent_ip", &AgentStartUpConfig::agent_ip)
  182. .def_readwrite("agent_port", &AgentStartUpConfig::agent_port)
  183. .def_readwrite("worker_ip", &AgentStartUpConfig::worker_ip)
  184. .def_readwrite("worker_port", &AgentStartUpConfig::worker_port)
  185. .def_readwrite("common_meta", &AgentStartUpConfig::common_meta);
  186. }
  187. class PyExitSignalHandle {
  188. public:
  189. static void Start() { ExitSignalHandle::Instance().Start(); }
  190. static bool HasStopped() { return ExitSignalHandle::Instance().HasStopped(); }
  191. };
  192. // cppcheck-suppress syntaxError
  193. PYBIND11_MODULE(_mindspore_serving, m) {
  194. PyRegServable(&m);
  195. PyRegMaster(&m);
  196. PyRegWorker(&m);
  197. PyRegWorkerAgent(&m);
  198. py::class_<PyExitSignalHandle>(m, "ExitSignalHandle_")
  199. .def_static("start", &PyExitSignalHandle::Start)
  200. .def_static("has_stopped", &PyExitSignalHandle::HasStopped);
  201. (void)py::module::import("atexit").attr("register")(py::cpp_function{[&]() -> void {
  202. Server::Instance().Clear();
  203. Worker::GetInstance().Clear();
  204. WorkerAgent::Instance().Clear();
  205. }});
  206. }
  207. } // namespace mindspore::serving

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