@@ -75,10 +75,16 @@ def process_batch(predictor, input_batch, optimal_batch_size):
75
75
return results , last_predictor_success , received_at , predicted_at
76
76
77
77
78
- def fetch_batch (main_index , predictor_sequence , optimal_batch_size ):
78
+ def fetch_batch (
79
+ main_index ,
80
+ predictor_sequence ,
81
+ optimal_batch_size ,
82
+ max_wait_time_for_batch_collection ,
83
+ ):
79
84
unique_id_wise_input_count = {}
80
85
input_batch = []
81
86
current_batch_length = 0
87
+ batch_collection_started_at = time .time ()
82
88
83
89
while current_batch_length < optimal_batch_size :
84
90
to_process = main_index .search (
@@ -98,9 +104,6 @@ def fetch_batch(main_index, predictor_sequence, optimal_batch_size):
98
104
},
99
105
)
100
106
101
- if not to_process : # No more items to process
102
- break
103
-
104
107
for unique_id , data in to_process .items ():
105
108
outputs = data [f"{ predictor_sequence - 1 } .outputs" ]
106
109
@@ -110,6 +113,22 @@ def fetch_batch(main_index, predictor_sequence, optimal_batch_size):
110
113
input_batch .extend (outputs )
111
114
current_batch_length += input_count
112
115
116
+ if current_batch_length == 0 :
117
+ time .sleep (max_wait_time_for_batch_collection / 2 )
118
+ continue
119
+
120
+ elif (
121
+ time .time () - batch_collection_started_at
122
+ < max_wait_time_for_batch_collection
123
+ and current_batch_length / optimal_batch_size < 0.9
124
+ ):
125
+ time .sleep (max_wait_time_for_batch_collection / 2 )
126
+ continue
127
+
128
+ else :
129
+ # finished collecting batch
130
+ break
131
+
113
132
return unique_id_wise_input_count , input_batch
114
133
115
134
@@ -156,7 +175,7 @@ def start_loop(
156
175
157
176
optimal_batch_size = predictor_info ["optimal_batch_size" ]
158
177
time_per_example = predictor_info ["time_per_example" ]
159
- max_wait_time_for_batch_collection = max (0.003 , time_per_example * 0.25 )
178
+ max_wait_time_for_batch_collection = max (0.003 , time_per_example * 0.51 )
160
179
161
180
_utils .logger .info (
162
181
f"""{ predictor_name }
@@ -167,23 +186,27 @@ def start_loop(
167
186
"""
168
187
)
169
188
170
- last_batch_collection_started_at = 0
171
-
172
189
while True :
173
190
"""
174
191
Set timedout_in_queue to True for all the predictions that have been in the queue for more than timeout_time seconds
175
192
and delete older than 7 seconds predictions that have finished prediction
176
193
"""
177
194
178
- _utils .MAIN_INDEX .search (
195
+ timedout_in_queue_unique_ids = _utils .MAIN_INDEX .search (
179
196
query = {
180
197
"-1.predicted_at" : 0 ,
181
198
"-1.received_at" : {"$lt" : time .time () - timeout_time },
182
199
"timedout_in_queue" : {"$ne" : True },
183
200
},
184
201
update = {"timedout_in_queue" : True },
202
+ select_keys = [],
185
203
)
186
204
205
+ if timedout_in_queue_unique_ids :
206
+ _utils .logger .warning (
207
+ f"{ _utils .MAIN_INDEX .count ()} in queue, set timedout_in_queue to True for { list (timedout_in_queue_unique_ids )} unique_ids"
208
+ )
209
+
187
210
_utils .MAIN_INDEX .delete (
188
211
query = {
189
212
"$and" : [
@@ -194,23 +217,12 @@ def start_loop(
194
217
)
195
218
196
219
unique_id_wise_input_count , input_batch = fetch_batch (
197
- _utils .MAIN_INDEX , predictor_sequence , optimal_batch_size
220
+ _utils .MAIN_INDEX ,
221
+ predictor_sequence ,
222
+ optimal_batch_size ,
223
+ max_wait_time_for_batch_collection ,
198
224
)
199
225
200
- current_batch_length = len (input_batch )
201
-
202
- if current_batch_length == 0 :
203
- time .sleep (max_wait_time_for_batch_collection )
204
- continue
205
-
206
- if (
207
- time .time () - last_batch_collection_started_at
208
- < max_wait_time_for_batch_collection
209
- and current_batch_length / optimal_batch_size < 0.9
210
- ):
211
- time .sleep (max_wait_time_for_batch_collection / 2 )
212
- continue
213
-
214
226
_utils .logger .debug (f"Processing batch { unique_id_wise_input_count } " )
215
227
216
228
results , last_predictor_success , received_at , predicted_at = process_batch (
@@ -223,11 +235,12 @@ def start_loop(
223
235
last_predictor_success ,
224
236
received_at ,
225
237
predicted_at ,
226
- current_batch_length ,
238
+ len ( input_batch ) ,
227
239
)
228
240
_utils .MAIN_INDEX .update (unique_id_wise_results )
241
+
229
242
_utils .logger .debug (
230
- f"Updated results predictor { predictor_sequence } : list({ unique_id_wise_results } ) "
243
+ f"Updated results predictor { predictor_sequence } : { list (unique_id_wise_results ) } "
231
244
)
232
245
233
246
last_batch_collection_started_at = time .time ()
0 commit comments