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.

server.cc 7.2 kB

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  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 "core/server.h"
  17. #include <grpcpp/grpcpp.h>
  18. #include <grpcpp/health_check_service_interface.h>
  19. #include <grpcpp/ext/proto_server_reflection_plugin.h>
  20. #include <string>
  21. #include <map>
  22. #include <vector>
  23. #include <utility>
  24. #include <memory>
  25. #include <future>
  26. #include <chrono>
  27. #include "include/infer_log.h"
  28. #include "serving/ms_service.grpc.pb.h"
  29. #include "core/util/option_parser.h"
  30. #include "core/version_control/version_controller.h"
  31. #include "core/util/file_system_operation.h"
  32. #include "core/serving_tensor.h"
  33. #include "util/status.h"
  34. using ms_serving::MSService;
  35. using ms_serving::PredictReply;
  36. using ms_serving::PredictRequest;
  37. namespace mindspore {
  38. namespace serving {
  39. #define MSI_TIME_STAMP_START(name) auto time_start_##name = std::chrono::steady_clock::now();
  40. #define MSI_TIME_STAMP_END(name) \
  41. { \
  42. auto time_end_##name = std::chrono::steady_clock::now(); \
  43. auto time_cost = std::chrono::duration<double, std::milli>(time_end_##name - time_start_##name).count(); \
  44. MSI_LOG_INFO << #name " Time Cost " << time_cost << "ms ---------------------"; \
  45. }
  46. Status Session::CreatDeviceSession(const std::string &device, uint32_t device_id) {
  47. session_ = inference::InferSession::CreateSession(device, device_id);
  48. if (session_ == nullptr) {
  49. MSI_LOG(ERROR) << "Creat Session Failed";
  50. return FAILED;
  51. }
  52. device_type_ = device;
  53. return SUCCESS;
  54. }
  55. Session &Session::Instance() {
  56. static Session instance;
  57. return instance;
  58. }
  59. Status Session::Predict(const PredictRequest &request, PredictReply &reply) {
  60. if (!model_loaded_) {
  61. MSI_LOG(ERROR) << "the model has not loaded";
  62. return FAILED;
  63. }
  64. if (session_ == nullptr) {
  65. MSI_LOG(ERROR) << "the inference session has not be initialized";
  66. return FAILED;
  67. }
  68. std::lock_guard<std::mutex> lock(mutex_);
  69. MSI_LOG(INFO) << "run Predict";
  70. ServingRequest serving_request(request);
  71. ServingReply serving_reply(reply);
  72. auto ret = session_->ExecuteModel(graph_id_, serving_request, serving_reply);
  73. MSI_LOG(INFO) << "run Predict finished";
  74. if (Status(ret) != SUCCESS) {
  75. MSI_LOG(ERROR) << "execute model return failed";
  76. return Status(ret);
  77. }
  78. return SUCCESS;
  79. }
  80. Status Session::Warmup(const MindSporeModelPtr model) {
  81. if (session_ == nullptr) {
  82. MSI_LOG(ERROR) << "The CreatDeviceSession should be called, before warmup";
  83. return FAILED;
  84. }
  85. std::lock_guard<std::mutex> lock(mutex_);
  86. std::string file_name = model->GetModelPath() + '/' + model->GetModelName();
  87. model_loaded_ = false;
  88. MSI_TIME_STAMP_START(LoadModelFromFile)
  89. auto ret = session_->LoadModelFromFile(file_name, graph_id_);
  90. MSI_TIME_STAMP_END(LoadModelFromFile)
  91. if (Status(ret) != SUCCESS) {
  92. MSI_LOG(ERROR) << "Load graph model failed, file name is " << file_name.c_str();
  93. return Status(ret);
  94. }
  95. model_loaded_ = true;
  96. MSI_LOG(INFO) << "Session Warmup finished";
  97. return SUCCESS;
  98. }
  99. Status Session::Clear() {
  100. if (session_ != nullptr) {
  101. session_->UnloadModel(graph_id_);
  102. session_->FinalizeEnv();
  103. session_ = nullptr;
  104. }
  105. return SUCCESS;
  106. }
  107. namespace {
  108. static const uint32_t uint32max = 0x7FFFFFFF;
  109. std::promise<void> exit_requested;
  110. void ClearEnv() { Session::Instance().Clear(); }
  111. void HandleSignal(int sig) { exit_requested.set_value(); }
  112. grpc::Status CreatGRPCStatus(Status status) {
  113. switch (status) {
  114. case SUCCESS:
  115. return grpc::Status::OK;
  116. case FAILED:
  117. return grpc::Status::CANCELLED;
  118. case INVALID_INPUTS:
  119. return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "The Predict Inputs do not match the Model Request!");
  120. default:
  121. return grpc::Status::CANCELLED;
  122. }
  123. }
  124. } // namespace
  125. // Service Implement
  126. class MSServiceImpl final : public MSService::Service {
  127. grpc::Status Predict(grpc::ServerContext *context, const PredictRequest *request, PredictReply *reply) override {
  128. std::lock_guard<std::mutex> lock(mutex_);
  129. MSI_TIME_STAMP_START(Predict)
  130. auto res = Session::Instance().Predict(*request, *reply);
  131. MSI_TIME_STAMP_END(Predict)
  132. if (res != inference::SUCCESS) {
  133. return CreatGRPCStatus(res);
  134. }
  135. MSI_LOG(INFO) << "Finish call service Eval";
  136. return grpc::Status::OK;
  137. }
  138. grpc::Status Test(grpc::ServerContext *context, const PredictRequest *request, PredictReply *reply) override {
  139. MSI_LOG(INFO) << "TestService call";
  140. return grpc::Status::OK;
  141. }
  142. std::mutex mutex_;
  143. };
  144. Status Server::BuildAndStart() {
  145. // handle exit signal
  146. signal(SIGINT, HandleSignal);
  147. signal(SIGTERM, HandleSignal);
  148. Status res;
  149. auto option_args = Options::Instance().GetArgs();
  150. std::string server_address = "0.0.0.0:" + std::to_string(option_args->grpc_port);
  151. std::string model_path = option_args->model_path;
  152. std::string model_name = option_args->model_name;
  153. std::string device_type = option_args->device_type;
  154. auto device_id = option_args->device_id;
  155. res = Session::Instance().CreatDeviceSession(device_type, device_id);
  156. if (res != SUCCESS) {
  157. MSI_LOG(ERROR) << "creat session failed";
  158. ClearEnv();
  159. return res;
  160. }
  161. VersionController version_controller(option_args->poll_model_wait_seconds, model_path, model_name);
  162. res = version_controller.Run();
  163. if (res != SUCCESS) {
  164. MSI_LOG(ERROR) << "load model failed";
  165. ClearEnv();
  166. return res;
  167. }
  168. MSServiceImpl ms_service;
  169. grpc::EnableDefaultHealthCheckService(true);
  170. grpc::reflection::InitProtoReflectionServerBuilderPlugin();
  171. // Set the port is not reuseable
  172. auto option = grpc::MakeChannelArgumentOption(GRPC_ARG_ALLOW_REUSEPORT, 0);
  173. grpc::ServerBuilder serverBuilder;
  174. serverBuilder.SetOption(std::move(option));
  175. serverBuilder.SetMaxMessageSize(uint32max);
  176. serverBuilder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
  177. serverBuilder.RegisterService(&ms_service);
  178. std::unique_ptr<grpc::Server> server(serverBuilder.BuildAndStart());
  179. if (server == nullptr) {
  180. MSI_LOG(ERROR) << "The serving server create failed";
  181. ClearEnv();
  182. return FAILED;
  183. }
  184. auto grpc_server_run = [&server]() { server->Wait(); };
  185. std::thread serving_thread(grpc_server_run);
  186. MSI_LOG(INFO) << "MS Serving listening on " << server_address;
  187. auto exit_future = exit_requested.get_future();
  188. exit_future.wait();
  189. ClearEnv();
  190. server->Shutdown();
  191. serving_thread.join();
  192. return SUCCESS;
  193. }
  194. } // namespace serving
  195. } // namespace mindspore