-
Notifications
You must be signed in to change notification settings - Fork 843
/
Model.java
240 lines (196 loc) · 7.49 KB
/
Model.java
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
package org.pytorch.serve.wlm;
import com.google.gson.JsonObject;
import java.io.File;
import java.util.Map;
import java.util.Objects;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.locks.ReentrantLock;
import org.apache.commons.io.FilenameUtils;
import org.pytorch.serve.archive.model.ModelArchive;
import org.pytorch.serve.job.Job;
import org.pytorch.serve.util.ConfigManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Model {
public static final String DEFAULT_DATA_QUEUE = "DATA_QUEUE";
public static final String MIN_WORKERS = "minWorkers";
public static final String MAX_WORKERS = "maxWorkers";
public static final String BATCH_SIZE = "batchSize";
public static final String MAX_BATCH_DELAY = "maxBatchDelay";
public static final String RESPONSE_TIMEOUT = "responseTimeout";
public static final String DEFAULT_VERSION = "defaultVersion";
public static final String MAR_NAME = "marName";
private static final Logger logger = LoggerFactory.getLogger(Model.class);
private ModelArchive modelArchive;
private int minWorkers;
private int maxWorkers;
private int batchSize;
private int maxBatchDelay;
private ReentrantLock lock;
private int responseTimeout;
private ModelVersionName modelVersionName;
private boolean isWorkflowModel;
// Total number of subsequent inference request failures
private AtomicInteger failedInfReqs;
// Per worker thread job queue. This separates out the control queue from data queue
private ConcurrentMap<String, LinkedBlockingDeque<Job>> jobsDb;
public Model(ModelArchive modelArchive, int queueSize) {
this.modelArchive = modelArchive;
batchSize = 1;
maxBatchDelay = 100;
jobsDb = new ConcurrentHashMap<>();
// Always have a queue for data
jobsDb.putIfAbsent(DEFAULT_DATA_QUEUE, new LinkedBlockingDeque<>(queueSize));
failedInfReqs = new AtomicInteger(0);
lock = new ReentrantLock();
modelVersionName =
new ModelVersionName(
this.modelArchive.getModelName(), this.modelArchive.getModelVersion());
}
public JsonObject getModelState(boolean isDefaultVersion) {
JsonObject modelInfo = new JsonObject();
modelInfo.addProperty(DEFAULT_VERSION, isDefaultVersion);
modelInfo.addProperty(MAR_NAME, FilenameUtils.getName(getModelUrl()));
modelInfo.addProperty(MIN_WORKERS, getMinWorkers());
modelInfo.addProperty(MAX_WORKERS, getMaxWorkers());
modelInfo.addProperty(BATCH_SIZE, getBatchSize());
modelInfo.addProperty(MAX_BATCH_DELAY, getMaxBatchDelay());
modelInfo.addProperty(RESPONSE_TIMEOUT, getResponseTimeout());
return modelInfo;
}
public void setModelState(JsonObject modelInfo) {
minWorkers = modelInfo.get(MIN_WORKERS).getAsInt();
maxWorkers = modelInfo.get(MAX_WORKERS).getAsInt();
maxBatchDelay = modelInfo.get(MAX_BATCH_DELAY).getAsInt();
responseTimeout = modelInfo.get(RESPONSE_TIMEOUT).getAsInt();
batchSize = modelInfo.get(BATCH_SIZE).getAsInt();
}
public String getModelName() {
return modelArchive.getModelName();
}
public ModelVersionName getModelVersionName() {
return modelVersionName;
}
public String getVersion() {
return modelArchive.getModelVersion();
}
public File getModelDir() {
return modelArchive.getModelDir();
}
public String getModelUrl() {
return modelArchive.getUrl();
}
public ModelArchive getModelArchive() {
return modelArchive;
}
public int getMinWorkers() {
return minWorkers;
}
public void setMinWorkers(int minWorkers) {
this.minWorkers = minWorkers;
}
public int getMaxWorkers() {
return maxWorkers;
}
public void setMaxWorkers(int maxWorkers) {
this.maxWorkers = maxWorkers;
}
public int getBatchSize() {
return batchSize;
}
public void setBatchSize(int batchSize) {
this.batchSize = batchSize;
}
public int getMaxBatchDelay() {
return maxBatchDelay;
}
public void setMaxBatchDelay(int maxBatchDelay) {
this.maxBatchDelay = maxBatchDelay;
}
public boolean isWorkflowModel() {
return isWorkflowModel;
}
public void setWorkflowModel(boolean workflowModel) {
isWorkflowModel = workflowModel;
}
public void addJob(String threadId, Job job) {
LinkedBlockingDeque<Job> blockingDeque = jobsDb.get(threadId);
if (blockingDeque == null) {
blockingDeque = new LinkedBlockingDeque<>();
jobsDb.put(threadId, blockingDeque);
}
blockingDeque.offer(job);
}
public void removeJobQueue(String threadId) {
if (!threadId.equals(DEFAULT_DATA_QUEUE)) {
jobsDb.remove(threadId);
}
}
public boolean addJob(Job job) {
return jobsDb.get(DEFAULT_DATA_QUEUE).offer(job);
}
public void addFirst(Job job) {
jobsDb.get(DEFAULT_DATA_QUEUE).addFirst(job);
}
public void pollBatch(String threadId, long waitTime, Map<String, Job> jobsRepo)
throws InterruptedException {
if (jobsRepo == null || threadId == null || threadId.isEmpty()) {
throw new IllegalArgumentException("Invalid input given provided");
}
if (!jobsRepo.isEmpty()) {
throw new IllegalArgumentException(
"The jobs repo provided contains stale jobs. Clear them!!");
}
LinkedBlockingDeque<Job> jobsQueue = jobsDb.get(threadId);
if (jobsQueue != null && !jobsQueue.isEmpty()) {
Job j = jobsQueue.poll(waitTime, TimeUnit.MILLISECONDS);
if (j != null) {
jobsRepo.put(j.getJobId(), j);
return;
}
}
try {
lock.lockInterruptibly();
long maxDelay = maxBatchDelay;
jobsQueue = jobsDb.get(DEFAULT_DATA_QUEUE);
Job j = jobsQueue.poll(Long.MAX_VALUE, TimeUnit.MILLISECONDS);
logger.trace("get first job: {}", Objects.requireNonNull(j).getJobId());
jobsRepo.put(j.getJobId(), j);
long begin = System.currentTimeMillis();
for (int i = 0; i < batchSize - 1; ++i) {
j = jobsQueue.poll(maxDelay, TimeUnit.MILLISECONDS);
if (j == null) {
break;
}
long end = System.currentTimeMillis();
maxDelay -= end - begin;
begin = end;
jobsRepo.put(j.getJobId(), j);
if (maxDelay <= 0) {
break;
}
}
logger.trace("sending jobs, size: {}", jobsRepo.size());
} finally {
if (lock.isHeldByCurrentThread()) {
lock.unlock();
}
}
}
public int incrFailedInfReqs() {
return failedInfReqs.incrementAndGet();
}
public void resetFailedInfReqs() {
failedInfReqs.set(0);
}
public int getResponseTimeout() {
return ConfigManager.getInstance().isDebug() ? Integer.MAX_VALUE : responseTimeout;
}
public void setResponseTimeout(int responseTimeout) {
this.responseTimeout = responseTimeout;
}
}