-
Notifications
You must be signed in to change notification settings - Fork 13
/
model_repository.cpp
168 lines (146 loc) · 5.56 KB
/
model_repository.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
// Copyright 2022 Xilinx Inc.
//
// 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 "proteus/core/model_repository.hpp"
#include <fcntl.h>
#include <google/protobuf/io/zero_copy_stream_impl.h>
#include <google/protobuf/text_format.h>
#include <cassert>
#include <filesystem>
#include <fstream>
#include "model_config.pb.h"
#include "proteus/core/manager.hpp"
#include "proteus/core/predict_api.hpp"
#include "proteus/core/worker_info.hpp"
#include "proteus/observation/logging.hpp"
namespace fs = std::filesystem;
namespace proteus {
// TODO(varunsh): get rid of this duplicate code with the one in grpc_internal
void mapProtoToParameters2(
const google::protobuf::Map<std::string, inference::InferParameter2>& params,
RequestParameters* parameters) {
using ParameterType = inference::InferParameter2::ParameterChoiceCase;
for (const auto& [key, value] : params) {
auto type = value.parameter_choice_case();
switch (type) {
case ParameterType::kBoolParam: {
parameters->put(key, value.bool_param());
break;
}
case ParameterType::kInt64Param: {
// TODO(varunsh): parameters should switch to uint64?
parameters->put(key, static_cast<int>(value.int64_param()));
break;
}
case ParameterType::kDoubleParam: {
parameters->put(key, value.double_param());
break;
}
case ParameterType::kStringParam: {
parameters->put(key, value.string_param());
break;
}
default: {
// if not set
break;
}
}
}
}
void ModelRepository::modelLoad(const std::string& model,
RequestParameters* parameters) {
repo_.modelLoad(model, parameters);
}
void ModelRepository::setRepository(const std::string& repository) {
repo_.setRepository(repository);
}
void ModelRepository::ModelRepositoryImpl::setRepository(
const std::string& repository_path) {
repository_ = repository_path;
}
void ModelRepository::ModelRepositoryImpl::modelLoad(
const std::string& model, RequestParameters* parameters) const {
const auto model_path = repository_ / model;
const auto config_path = model_path / "config.pbtxt";
// TODO(varunsh): support other versions than 1/
parameters->put("model", model_path / "1/saved_model");
inference::Config config;
int fileDescriptor = open(config_path.c_str(), O_RDONLY);
if (fileDescriptor < 0) {
throw std::runtime_error("Config file could not be opened");
}
google::protobuf::io::FileInputStream fileInput(fileDescriptor);
fileInput.SetCloseOnDelete(true);
if (!google::protobuf::TextFormat::Parse(&fileInput, &config)) {
throw std::runtime_error("Config file could not be parsed");
}
if (const auto& platform = config.platform();
platform == "tensorflow_graphdef") {
parameters->put("worker", "tfzendnn");
} else if (platform == "vitis_xmodel") {
parameters->put("worker", "xmodel");
} else {
throw std::runtime_error("Unknown platform");
}
mapProtoToParameters2(config.parameters(), parameters);
}
void UpdateListener::handleFileAction([[maybe_unused]] efsw::WatchID watchid,
const std::string& dir,
const std::string& filename,
efsw::Action action,
std::string oldFilename) {
Logger logger{Loggers::kServer};
if (filename == "config.pbtxt") {
if (action == efsw::Actions::Add) {
// arbitrary delay to make sure filesystem has settled
std::this_thread::sleep_for(std::chrono::milliseconds(100));
auto model = fs::path(dir).parent_path().filename();
// TODO(varunsh): replace with native client
RequestParameters params;
try {
ModelRepository::modelLoad(model, ¶ms);
Manager::getInstance().loadWorker(model, params);
} catch (const std::runtime_error& e) {
PROTEUS_LOG_INFO(logger, "Error loading " + model.string());
}
} else if (action == efsw::Actions::Delete) {
// arbitrary delay to make sure filesystem has settled
std::this_thread::sleep_for(std::chrono::milliseconds(100));
auto model = fs::path(dir).parent_path().filename();
// TODO(varunsh): replace with native client
Manager::getInstance().unloadWorker(model);
}
}
switch (action) {
case efsw::Actions::Add:
PROTEUS_LOG_DEBUG(
logger, "DIR (" + dir + ") FILE (" + filename + ") has event Added");
break;
case efsw::Actions::Delete:
PROTEUS_LOG_DEBUG(
logger, "DIR (" + dir + ") FILE (" + filename + ") has event Delete");
break;
case efsw::Actions::Modified:
PROTEUS_LOG_DEBUG(
logger, "DIR (" + dir + ") FILE (" + filename + ") has event Modified");
break;
case efsw::Actions::Moved:
PROTEUS_LOG_DEBUG(logger, "DIR (" + dir + ") FILE (" + filename +
") has event Moved from (" + oldFilename +
")");
break;
default:
PROTEUS_LOG_ERROR(logger, "Should never happen");
}
}
} // namespace proteus