2020-06-21 14:37:27 +08:00
|
|
|
/**
|
|
|
|
* Copyright 2020 Huawei Technologies Co., Ltd
|
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
#include "core/server.h"
|
|
|
|
#include <grpcpp/grpcpp.h>
|
|
|
|
#include <grpcpp/health_check_service_interface.h>
|
|
|
|
#include <grpcpp/ext/proto_server_reflection_plugin.h>
|
|
|
|
#include <string>
|
|
|
|
#include <map>
|
|
|
|
#include <vector>
|
|
|
|
#include <utility>
|
|
|
|
#include <memory>
|
2020-06-27 10:01:06 +08:00
|
|
|
#include <future>
|
2020-07-21 11:19:00 +08:00
|
|
|
#include <chrono>
|
2020-06-21 14:37:27 +08:00
|
|
|
|
2020-07-21 11:19:00 +08:00
|
|
|
#include "include/infer_log.h"
|
2020-06-21 14:37:27 +08:00
|
|
|
#include "serving/ms_service.grpc.pb.h"
|
|
|
|
#include "core/util/option_parser.h"
|
|
|
|
#include "core/version_control/version_controller.h"
|
|
|
|
#include "core/util/file_system_operation.h"
|
2020-07-21 11:19:00 +08:00
|
|
|
#include "core/serving_tensor.h"
|
2020-07-31 16:23:52 +08:00
|
|
|
#include "util/status.h"
|
2020-06-21 14:37:27 +08:00
|
|
|
|
|
|
|
using ms_serving::MSService;
|
|
|
|
using ms_serving::PredictReply;
|
|
|
|
using ms_serving::PredictRequest;
|
|
|
|
|
|
|
|
namespace mindspore {
|
|
|
|
namespace serving {
|
2020-07-21 11:19:00 +08:00
|
|
|
|
|
|
|
#define MSI_TIME_STAMP_START(name) auto time_start_##name = std::chrono::steady_clock::now();
|
|
|
|
#define MSI_TIME_STAMP_END(name) \
|
|
|
|
{ \
|
|
|
|
auto time_end_##name = std::chrono::steady_clock::now(); \
|
|
|
|
auto time_cost = std::chrono::duration<double, std::milli>(time_end_##name - time_start_##name).count(); \
|
|
|
|
MSI_LOG_INFO << #name " Time Cost " << time_cost << "ms ---------------------"; \
|
|
|
|
}
|
2020-06-21 14:37:27 +08:00
|
|
|
|
|
|
|
Status Session::CreatDeviceSession(const std::string &device, uint32_t device_id) {
|
2020-07-21 11:19:00 +08:00
|
|
|
session_ = inference::InferSession::CreateSession(device, device_id);
|
2020-06-21 14:37:27 +08:00
|
|
|
if (session_ == nullptr) {
|
2020-07-21 11:19:00 +08:00
|
|
|
MSI_LOG(ERROR) << "Creat Session Failed";
|
2020-06-21 14:37:27 +08:00
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
device_type_ = device;
|
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
Session &Session::Instance() {
|
|
|
|
static Session instance;
|
|
|
|
return instance;
|
|
|
|
}
|
|
|
|
|
2020-07-21 11:19:00 +08:00
|
|
|
Status Session::Predict(const PredictRequest &request, PredictReply &reply) {
|
|
|
|
if (!model_loaded_) {
|
|
|
|
MSI_LOG(ERROR) << "the model has not loaded";
|
2020-06-21 14:37:27 +08:00
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
if (session_ == nullptr) {
|
2020-07-21 11:19:00 +08:00
|
|
|
MSI_LOG(ERROR) << "the inference session has not be initialized";
|
2020-06-21 14:37:27 +08:00
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
2020-07-21 11:19:00 +08:00
|
|
|
MSI_LOG(INFO) << "run Predict";
|
2020-06-21 14:37:27 +08:00
|
|
|
|
2020-07-21 11:19:00 +08:00
|
|
|
ServingRequest serving_request(request);
|
|
|
|
ServingReply serving_reply(reply);
|
|
|
|
|
|
|
|
auto ret = session_->ExecuteModel(graph_id_, serving_request, serving_reply);
|
|
|
|
MSI_LOG(INFO) << "run Predict finished";
|
2020-07-31 16:23:52 +08:00
|
|
|
if (Status(ret) != SUCCESS) {
|
2020-07-21 11:19:00 +08:00
|
|
|
MSI_LOG(ERROR) << "execute model return failed";
|
2020-07-31 16:23:52 +08:00
|
|
|
return Status(ret);
|
2020-07-20 16:57:56 +08:00
|
|
|
}
|
2020-06-21 14:37:27 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status Session::Warmup(const MindSporeModelPtr model) {
|
|
|
|
if (session_ == nullptr) {
|
2020-07-21 11:19:00 +08:00
|
|
|
MSI_LOG(ERROR) << "The CreatDeviceSession should be called, before warmup";
|
2020-06-21 14:37:27 +08:00
|
|
|
return FAILED;
|
|
|
|
}
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
|
|
|
std::string file_name = model->GetModelPath() + '/' + model->GetModelName();
|
2020-07-21 11:19:00 +08:00
|
|
|
model_loaded_ = false;
|
|
|
|
MSI_TIME_STAMP_START(LoadModelFromFile)
|
|
|
|
auto ret = session_->LoadModelFromFile(file_name, graph_id_);
|
|
|
|
MSI_TIME_STAMP_END(LoadModelFromFile)
|
2020-07-31 16:23:52 +08:00
|
|
|
if (Status(ret) != SUCCESS) {
|
2020-07-21 11:19:00 +08:00
|
|
|
MSI_LOG(ERROR) << "Load graph model failed, file name is " << file_name.c_str();
|
2020-07-31 16:23:52 +08:00
|
|
|
return Status(ret);
|
2020-06-21 14:37:27 +08:00
|
|
|
}
|
2020-07-21 11:19:00 +08:00
|
|
|
model_loaded_ = true;
|
|
|
|
MSI_LOG(INFO) << "Session Warmup finished";
|
2020-06-21 14:37:27 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
Status Session::Clear() {
|
2020-07-21 11:19:00 +08:00
|
|
|
if (session_ != nullptr) {
|
|
|
|
session_->UnloadModel(graph_id_);
|
|
|
|
session_->FinalizeEnv();
|
|
|
|
session_ = nullptr;
|
|
|
|
}
|
2020-06-21 14:37:27 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace {
|
2020-06-27 10:01:06 +08:00
|
|
|
static const uint32_t uint32max = 0x7FFFFFFF;
|
|
|
|
std::promise<void> exit_requested;
|
|
|
|
|
2020-07-31 16:23:52 +08:00
|
|
|
void ClearEnv() { Session::Instance().Clear(); }
|
2020-06-27 10:01:06 +08:00
|
|
|
void HandleSignal(int sig) { exit_requested.set_value(); }
|
2020-06-21 14:37:27 +08:00
|
|
|
|
2020-07-31 16:23:52 +08:00
|
|
|
grpc::Status CreatGRPCStatus(Status status) {
|
|
|
|
switch (status) {
|
|
|
|
case SUCCESS:
|
|
|
|
return grpc::Status::OK;
|
|
|
|
case FAILED:
|
|
|
|
return grpc::Status::CANCELLED;
|
|
|
|
case INVALID_INPUTS:
|
|
|
|
return grpc::Status(grpc::StatusCode::INVALID_ARGUMENT, "The Predict Inputs do not match the Model Request!");
|
|
|
|
default:
|
|
|
|
return grpc::Status::CANCELLED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-21 14:37:27 +08:00
|
|
|
} // namespace
|
|
|
|
|
|
|
|
// Service Implement
|
|
|
|
class MSServiceImpl final : public MSService::Service {
|
|
|
|
grpc::Status Predict(grpc::ServerContext *context, const PredictRequest *request, PredictReply *reply) override {
|
|
|
|
std::lock_guard<std::mutex> lock(mutex_);
|
2020-07-21 11:19:00 +08:00
|
|
|
MSI_TIME_STAMP_START(Predict)
|
|
|
|
auto res = Session::Instance().Predict(*request, *reply);
|
|
|
|
MSI_TIME_STAMP_END(Predict)
|
2020-07-31 16:23:52 +08:00
|
|
|
if (res != inference::SUCCESS) {
|
|
|
|
return CreatGRPCStatus(res);
|
2020-06-21 14:37:27 +08:00
|
|
|
}
|
2020-07-21 11:19:00 +08:00
|
|
|
MSI_LOG(INFO) << "Finish call service Eval";
|
2020-06-21 14:37:27 +08:00
|
|
|
return grpc::Status::OK;
|
|
|
|
}
|
|
|
|
|
|
|
|
grpc::Status Test(grpc::ServerContext *context, const PredictRequest *request, PredictReply *reply) override {
|
2020-07-21 11:19:00 +08:00
|
|
|
MSI_LOG(INFO) << "TestService call";
|
2020-06-21 14:37:27 +08:00
|
|
|
return grpc::Status::OK;
|
|
|
|
}
|
|
|
|
std::mutex mutex_;
|
|
|
|
};
|
|
|
|
|
|
|
|
Status Server::BuildAndStart() {
|
|
|
|
// handle exit signal
|
|
|
|
signal(SIGINT, HandleSignal);
|
2020-07-01 14:52:34 +08:00
|
|
|
signal(SIGTERM, HandleSignal);
|
2020-06-21 14:37:27 +08:00
|
|
|
Status res;
|
|
|
|
auto option_args = Options::Instance().GetArgs();
|
|
|
|
std::string server_address = "0.0.0.0:" + std::to_string(option_args->grpc_port);
|
|
|
|
std::string model_path = option_args->model_path;
|
|
|
|
std::string model_name = option_args->model_name;
|
|
|
|
std::string device_type = option_args->device_type;
|
|
|
|
auto device_id = option_args->device_id;
|
|
|
|
res = Session::Instance().CreatDeviceSession(device_type, device_id);
|
|
|
|
if (res != SUCCESS) {
|
2020-07-21 11:19:00 +08:00
|
|
|
MSI_LOG(ERROR) << "creat session failed";
|
2020-06-21 14:37:27 +08:00
|
|
|
ClearEnv();
|
|
|
|
return res;
|
|
|
|
}
|
|
|
|
VersionController version_controller(option_args->poll_model_wait_seconds, model_path, model_name);
|
|
|
|
res = version_controller.Run();
|
|
|
|
if (res != SUCCESS) {
|
2020-07-21 11:19:00 +08:00
|
|
|
MSI_LOG(ERROR) << "load model failed";
|
2020-06-21 14:37:27 +08:00
|
|
|
ClearEnv();
|
|
|
|
return res;
|
|
|
|
}
|
2020-07-03 17:01:24 +08:00
|
|
|
MSServiceImpl ms_service;
|
2020-06-21 14:37:27 +08:00
|
|
|
grpc::EnableDefaultHealthCheckService(true);
|
|
|
|
grpc::reflection::InitProtoReflectionServerBuilderPlugin();
|
|
|
|
// Set the port is not reuseable
|
|
|
|
auto option = grpc::MakeChannelArgumentOption(GRPC_ARG_ALLOW_REUSEPORT, 0);
|
2020-07-01 14:52:34 +08:00
|
|
|
grpc::ServerBuilder serverBuilder;
|
|
|
|
serverBuilder.SetOption(std::move(option));
|
|
|
|
serverBuilder.SetMaxMessageSize(uint32max);
|
|
|
|
serverBuilder.AddListeningPort(server_address, grpc::InsecureServerCredentials());
|
2020-07-03 17:01:24 +08:00
|
|
|
serverBuilder.RegisterService(&ms_service);
|
2020-07-01 14:52:34 +08:00
|
|
|
std::unique_ptr<grpc::Server> server(serverBuilder.BuildAndStart());
|
2020-06-27 14:57:26 +08:00
|
|
|
if (server == nullptr) {
|
2020-07-21 11:19:00 +08:00
|
|
|
MSI_LOG(ERROR) << "The serving server create failed";
|
2020-06-27 14:57:26 +08:00
|
|
|
ClearEnv();
|
|
|
|
return FAILED;
|
|
|
|
}
|
2020-06-27 10:01:06 +08:00
|
|
|
auto grpc_server_run = [&server]() { server->Wait(); };
|
|
|
|
std::thread serving_thread(grpc_server_run);
|
2020-07-21 11:19:00 +08:00
|
|
|
MSI_LOG(INFO) << "MS Serving listening on " << server_address;
|
2020-06-27 10:01:06 +08:00
|
|
|
auto exit_future = exit_requested.get_future();
|
|
|
|
exit_future.wait();
|
|
|
|
ClearEnv();
|
|
|
|
server->Shutdown();
|
|
|
|
serving_thread.join();
|
2020-06-21 14:37:27 +08:00
|
|
|
return SUCCESS;
|
|
|
|
}
|
|
|
|
} // namespace serving
|
|
|
|
} // namespace mindspore
|