forked from triton-inference-server/core
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.h
326 lines (266 loc) · 10.9 KB
/
server.h
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
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
// Copyright 2018-2022, NVIDIA CORPORATION & AFFILIATES. All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
// * Redistributions of source code must retain the above copyright
// notice, this list of conditions and the following disclaimer.
// * Redistributions in binary form must reproduce the above copyright
// notice, this list of conditions and the following disclaimer in the
// documentation and/or other materials provided with the distribution.
// * Neither the name of NVIDIA CORPORATION nor the names of its
// contributors may be used to endorse or promote products derived
// from this software without specific prior written permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS ``AS IS'' AND ANY
// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
// IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
// PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
// OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#pragma once
#include <stddef.h>
#include <stdint.h>
#include <atomic>
#include <map>
#include <string>
#include <thread>
#include <vector>
#include "backend_manager.h"
#include "infer_parameter.h"
#include "model_config.pb.h"
#include "model_repository_manager.h"
#include "rate_limiter.h"
#include "response_cache.h"
#include "status.h"
#include "triton/common/model_config.h"
namespace triton { namespace core {
class Model;
class InferenceRequest;
enum class ModelControlMode { MODE_NONE, MODE_POLL, MODE_EXPLICIT };
enum class RateLimitMode { RL_EXEC_COUNT, RL_OFF };
// Readiness status for the inference server.
enum class ServerReadyState {
// The server is in an invalid state and will likely not response
// correctly to any requests.
SERVER_INVALID,
// The server is initializing.
SERVER_INITIALIZING,
// The server is ready and accepting requests.
SERVER_READY,
// The server is exiting and will not respond to requests.
SERVER_EXITING,
// The server did not initialize correctly.
SERVER_FAILED_TO_INITIALIZE
};
// Inference server information.
class InferenceServer {
public:
// Construct an inference server.
InferenceServer();
// Initialize the server. Return true on success, false otherwise.
Status Init();
// Stop the server. Return true if all models are unloaded, false
// if exit timeout occurs. If 'force' is true attempt to stop the
// server even if it is not in a ready state.
Status Stop(const bool force = false);
// Check the model repository for changes and update server state
// based on those changes.
Status PollModelRepository();
// Server health
Status IsLive(bool* live);
Status IsReady(bool* ready);
// Model health
Status ModelIsReady(
const std::string& model_name, const int64_t model_version, bool* ready);
// Return the ready versions of specific model
Status ModelReadyVersions(
const std::string& model_name, std::vector<int64_t>* versions);
// Return the ready versions of all models
Status ModelReadyVersions(
std::map<std::string, std::vector<int64_t>>* model_versions);
/// Get the index of all models in all repositories.
/// \param ready_only If true return only index of models that are ready.
/// \param index Returns the index.
/// \return error status.
Status RepositoryIndex(
const bool ready_only,
std::vector<ModelRepositoryManager::ModelIndex>* index);
// Inference. If Status::Success is returned then this function has
// taken ownership of the request object and so 'request' will be
// nullptr. If non-success is returned then the caller still retains
// ownership of 'request'.
Status InferAsync(std::unique_ptr<InferenceRequest>& request);
// Load the corresponding model. Reload the model if it has been loaded.
Status LoadModel(
const std::unordered_map<
std::string, std::vector<const InferenceParameter*>>& models);
// Unload the corresponding model.
Status UnloadModel(
const std::string& model_name, const bool unload_dependents);
// Print backends and models summary
Status PrintBackendAndModelSummary();
// Register model repository path and associated mappings
Status RegisterModelRepository(
const std::string& repository,
const std::unordered_map<std::string, std::string>& model_mapping);
// Unregister model repository path.
Status UnregisterModelRepository(const std::string& repository);
// Return the server version.
const std::string& Version() const { return version_; }
// Return the server extensions.
const std::vector<const char*>& Extensions() const { return extensions_; }
// Get / set the ID of the server.
const std::string& Id() const { return id_; }
void SetId(const std::string& id) { id_ = id; }
// Get / set the model repository path
const std::set<std::string>& ModelRepositoryPaths() const
{
return model_repository_paths_;
}
void SetModelRepositoryPaths(const std::set<std::string>& p)
{
model_repository_paths_ = p;
}
// Get / set model control mode.
ModelControlMode GetModelControlMode() const { return model_control_mode_; }
void SetModelControlMode(ModelControlMode m) { model_control_mode_ = m; }
// Get / set the startup models
const std::set<std::string>& StartupModels() const { return startup_models_; }
void SetStartupModels(const std::set<std::string>& m) { startup_models_ = m; }
// Get / set strict model configuration enable.
bool StrictModelConfigEnabled() const { return strict_model_config_; }
void SetStrictModelConfigEnabled(bool e) { strict_model_config_ = e; }
// Get / set rate limiter mode.
RateLimitMode RateLimiterMode() const { return rate_limit_mode_; }
void SetRateLimiterMode(RateLimitMode m) { rate_limit_mode_ = m; }
// Get / set rate limit resource counts
const RateLimiter::ResourceMap& RateLimiterResources() const
{
return rate_limit_resource_map_;
}
void SetRateLimiterResources(const RateLimiter::ResourceMap& rm)
{
rate_limit_resource_map_ = rm;
}
// Get / set the pinned memory pool byte size.
int64_t PinnedMemoryPoolByteSize() const { return pinned_memory_pool_size_; }
void SetPinnedMemoryPoolByteSize(int64_t s)
{
pinned_memory_pool_size_ = std::max((int64_t)0, s);
}
// Get / set the response cache byte size.
uint64_t ResponseCacheByteSize() const { return response_cache_byte_size_; }
void SetResponseCacheByteSize(uint64_t s)
{
response_cache_byte_size_ = s;
response_cache_enabled_ = (s > 0) ? true : false;
}
bool ResponseCacheEnabled() const { return response_cache_enabled_; }
// Get / set CUDA memory pool size
const std::map<int, uint64_t>& CudaMemoryPoolByteSize() const
{
return cuda_memory_pool_size_;
}
void SetCudaMemoryPoolByteSize(const std::map<int, uint64_t>& s)
{
cuda_memory_pool_size_ = s;
}
// Get / set the minimum support CUDA compute capability.
double MinSupportedComputeCapability() const
{
return min_supported_compute_capability_;
}
void SetMinSupportedComputeCapability(double c)
{
min_supported_compute_capability_ = c;
}
// Get / set strict readiness enable.
bool StrictReadinessEnabled() const { return strict_readiness_; }
void SetStrictReadinessEnabled(bool e) { strict_readiness_ = e; }
// Get / set the server exit timeout, in seconds.
int32_t ExitTimeoutSeconds() const { return exit_timeout_secs_; }
void SetExitTimeoutSeconds(int32_t s) { exit_timeout_secs_ = std::max(0, s); }
void SetBufferManagerThreadCount(unsigned int c)
{
buffer_manager_thread_count_ = c;
}
void SetModelLoadThreadCount(unsigned int c) { model_load_thread_count_ = c; }
// Set a backend command-line configuration
void SetBackendCmdlineConfig(
const triton::common::BackendCmdlineConfigMap& bc)
{
backend_cmdline_config_map_ = bc;
}
void SetHostPolicyCmdlineConfig(
const triton::common::HostPolicyCmdlineConfigMap& hp)
{
host_policy_map_ = hp;
}
void SetRepoAgentDir(const std::string& d) { repoagent_dir_ = d; }
// Return the requested model object.
Status GetModel(
const std::string& model_name, const int64_t model_version,
std::shared_ptr<Model>* model)
{
// Allow model retrival while server exiting to provide graceful
// completion of inference sequence that spans multiple requests.
if ((ready_state_ != ServerReadyState::SERVER_READY) &&
(ready_state_ != ServerReadyState::SERVER_EXITING)) {
return Status(Status::Code::UNAVAILABLE, "Server not ready");
}
return model_repository_manager_->GetModel(
model_name, model_version, model);
}
// Get the Backend Manager
const std::shared_ptr<TritonBackendManager>& BackendManager()
{
return backend_manager_;
}
// Return the pointer to RateLimiter object.
std::shared_ptr<RateLimiter> GetRateLimiter() { return rate_limiter_; }
// Return the pointer to response cache object.
std::shared_ptr<RequestResponseCache> GetResponseCache()
{
return response_cache_;
}
private:
const std::string version_;
std::string id_;
std::vector<const char*> extensions_;
std::set<std::string> model_repository_paths_;
std::set<std::string> startup_models_;
ModelControlMode model_control_mode_;
bool strict_model_config_;
bool strict_readiness_;
uint32_t exit_timeout_secs_;
uint32_t buffer_manager_thread_count_;
uint32_t model_load_thread_count_;
uint64_t pinned_memory_pool_size_;
uint64_t response_cache_byte_size_;
bool response_cache_enabled_;
std::map<int, uint64_t> cuda_memory_pool_size_;
double min_supported_compute_capability_;
triton::common::BackendCmdlineConfigMap backend_cmdline_config_map_;
triton::common::HostPolicyCmdlineConfigMap host_policy_map_;
std::string repoagent_dir_;
RateLimitMode rate_limit_mode_;
RateLimiter::ResourceMap rate_limit_resource_map_;
// Current state of the inference server.
ServerReadyState ready_state_;
// Number of in-flight, non-inference requests. During shutdown we
// attempt to wait for all in-flight non-inference requests to
// complete before exiting (also wait for in-flight inference
// requests but that is determined by model shared_ptr).
std::atomic<uint64_t> inflight_request_counter_;
std::shared_ptr<RateLimiter> rate_limiter_;
std::unique_ptr<ModelRepositoryManager> model_repository_manager_;
std::shared_ptr<TritonBackendManager> backend_manager_;
std::shared_ptr<RequestResponseCache> response_cache_;
};
}} // namespace triton::core