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

5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  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. namespace mindspore::serving {
  26. PYBIND11_MODULE(_mindspore_serving, m) {
  27. // avoid as numpy object memory copy in PyTensor::AsPythonData
  28. py::class_<TensorBase, TensorBasePtr>(m, "Tensor_");
  29. py::class_<PyPreprocessStorage, std::shared_ptr<PyPreprocessStorage>>(m, "PreprocessStorage_")
  30. .def(py::init<>())
  31. .def_static("get_instance", &PyPreprocessStorage::Instance)
  32. .def("register", &PyPreprocessStorage::Register)
  33. .def("get_pycpp_preprocess_info", &PyPreprocessStorage::GetPyCppPreprocessInfo);
  34. py::class_<PyPostprocessStorage, std::shared_ptr<PyPostprocessStorage>>(m, "PostprocessStorage_")
  35. .def(py::init<>())
  36. .def_static("get_instance", &PyPostprocessStorage::Instance)
  37. .def("register", &PyPostprocessStorage::Register)
  38. .def("get_pycpp_postprocess_info", &PyPostprocessStorage::GetPyCppPostprocessInfo);
  39. py::enum_<PredictPhaseTag>(m, "PredictPhaseTag_")
  40. .value("kPredictPhaseTag_Input", PredictPhaseTag::kPredictPhaseTag_Input)
  41. .value("kPredictPhaseTag_Preproces", PredictPhaseTag::kPredictPhaseTag_Preproces)
  42. .value("kPredictPhaseTag_Predict", PredictPhaseTag::kPredictPhaseTag_Predict)
  43. .value("kPredictPhaseTag_Postprocess", PredictPhaseTag::kPredictPhaseTag_Postprocess)
  44. .export_values();
  45. py::class_<MethodSignature>(m, "MethodSignature_")
  46. .def(py::init<>())
  47. .def_readwrite("method_name", &MethodSignature::method_name)
  48. .def_readwrite("inputs", &MethodSignature::inputs)
  49. .def_readwrite("outputs", &MethodSignature::outputs)
  50. .def_readwrite("preprocess_name", &MethodSignature::preprocess_name)
  51. .def_readwrite("preprocess_inputs", &MethodSignature::preprocess_inputs)
  52. .def_readwrite("postprocess_name", &MethodSignature::postprocess_name)
  53. .def_readwrite("postprocess_inputs", &MethodSignature::postprocess_inputs)
  54. .def_readwrite("servable_name", &MethodSignature::servable_name)
  55. .def_readwrite("servable_inputs", &MethodSignature::servable_inputs)
  56. .def_readwrite("returns", &MethodSignature::returns);
  57. py::class_<RequestSpec>(m, "RequestSpec_")
  58. .def(py::init<>())
  59. .def_readwrite("servable_name", &RequestSpec::servable_name)
  60. .def_readwrite("version_number", &RequestSpec::version_number)
  61. .def_readwrite("method_name", &RequestSpec::method_name);
  62. py::class_<ServableMeta>(m, "ServableMeta_")
  63. .def(py::init<>())
  64. .def_readwrite("servable_name", &ServableMeta::servable_name)
  65. .def_readwrite("inputs_count", &ServableMeta::inputs_count)
  66. .def_readwrite("outputs_count", &ServableMeta::outputs_count)
  67. .def_readwrite("servable_file", &ServableMeta::servable_file)
  68. .def_readwrite("with_batch_dim", &ServableMeta::with_batch_dim)
  69. .def_readwrite("options", &ServableMeta::load_options)
  70. .def_readwrite("without_batch_dim_inputs", &ServableMeta::without_batch_dim_inputs)
  71. .def("set_model_format", &ServableMeta::SetModelFormat);
  72. py::class_<ServableSignature>(m, "ServableSignature_")
  73. .def(py::init<>())
  74. .def_readwrite("servable_meta", &ServableSignature::servable_meta)
  75. .def_readwrite("methods", &ServableSignature::methods);
  76. py::class_<PyServableStorage>(m, "ServableStorage_")
  77. .def_static("register_servable_input_output_info", &PyServableStorage::RegisterInputOutputInfo)
  78. .def_static("register_method", &PyServableStorage::RegisterMethod)
  79. .def_static("declare_servable", &PyServableStorage::DeclareServable);
  80. py::class_<TaskContext>(m, "TaskContext_").def(py::init<>());
  81. py::class_<TaskItem>(m, "TaskItem_")
  82. .def(py::init<>())
  83. .def_readwrite("task_type", &TaskItem::task_type)
  84. .def_readwrite("name", &TaskItem::name)
  85. .def_property_readonly("instance_list",
  86. [](const TaskItem &item) {
  87. py::tuple instances(item.instance_list.size());
  88. for (size_t i = 0; i < item.instance_list.size(); i++) {
  89. instances[i] = PyTensor::AsNumpyTuple(item.instance_list[i].data);
  90. }
  91. return instances;
  92. })
  93. .def_readwrite("context_list", &TaskItem::context_list);
  94. py::class_<PyWorker>(m, "Worker_")
  95. .def_static("start_servable", &PyWorker::StartServable)
  96. .def_static("start_servable_in_master", &PyWorker::StartServableInMaster)
  97. .def_static("get_batch_size", &PyWorker::GetBatchSize)
  98. .def_static("wait_and_clear", &PyWorker::WaitAndClear)
  99. .def_static("stop_and_clear", PyWorker::StopAndClear)
  100. .def_static("get_py_task", &PyWorker::GetPyTask, py::call_guard<py::gil_scoped_release>())
  101. .def_static("try_get_preprocess_py_task", &PyWorker::TryGetPreprocessPyTask)
  102. .def_static("try_get_postprocess_py_task", &PyWorker::TryGetPostprocessPyTask)
  103. .def_static("push_preprocess_result", &PyWorker::PushPreprocessPyResult)
  104. .def_static("push_preprocess_failed", &PyWorker::PushPreprocessPyFailed)
  105. .def_static("push_postprocess_result", &PyWorker::PushPostprocessPyResult)
  106. .def_static("push_postprocess_failed", &PyWorker::PushPostprocessPyFailed);
  107. py::class_<ServableContext, std::shared_ptr<ServableContext>>(m, "Context_")
  108. .def(py::init<>())
  109. .def_static("get_instance", &ServableContext::Instance)
  110. .def("set_device_type_str",
  111. [](ServableContext &context, const std::string &device_type) {
  112. auto status = context.SetDeviceTypeStr(device_type);
  113. if (status != SUCCESS) {
  114. MSI_LOG_EXCEPTION << "Raise failed: " << status.StatusMessage();
  115. }
  116. })
  117. .def("set_device_id", &ServableContext::SetDeviceId);
  118. py::class_<PyMaster, std::shared_ptr<PyMaster>>(m, "Master_")
  119. .def_static("start_grpc_server", &PyMaster::StartGrpcServer)
  120. .def_static("start_grpc_master_server", &PyMaster::StartGrpcMasterServer)
  121. .def_static("start_restful_server", &PyMaster::StartRestfulServer)
  122. .def_static("wait_and_clear", &PyMaster::WaitAndClear)
  123. .def_static("stop_and_clear", &PyMaster::StopAndClear);
  124. (void)py::module::import("atexit").attr("register")(py::cpp_function{[&]() -> void {
  125. Server::Instance().Clear();
  126. Worker::GetInstance().Clear();
  127. }});
  128. }
  129. } // namespace mindspore::serving

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