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

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