Skip to content

Commit 992958f

Browse files
committed
update batching logic
Signed-off-by: Praneeth Bedapudi <praneeth@bpraneeth.com>
1 parent f3be219 commit 992958f

File tree

3 files changed

+41
-19
lines changed

3 files changed

+41
-19
lines changed

fastdeploy/_loop.py

Lines changed: 39 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -75,44 +75,65 @@ def process_batch(predictor, input_batch, optimal_batch_size):
7575
return results, last_predictor_success, received_at, predicted_at
7676

7777

78+
to_process = {}
79+
current_sum_of_to_process = 0
80+
81+
7882
def fetch_batch(
7983
main_index,
8084
predictor_sequence,
8185
optimal_batch_size,
8286
max_wait_time_for_batch_collection,
8387
):
88+
global to_process
89+
global current_sum_of_to_process
90+
8491
unique_id_wise_input_count = {}
8592
input_batch = []
8693
current_batch_length = 0
8794
batch_collection_started_at = time.time()
8895
last_input_received_at = time.time()
8996

9097
while current_batch_length < optimal_batch_size:
91-
to_process = main_index.search(
92-
query={
93-
"-1.predicted_at": 0, # prediction not yet done
94-
"last_predictor_success": True, # last predictor success
95-
"last_predictor_sequence": predictor_sequence
96-
- 1, # last predictor sequence
97-
"timedout_in_queue": {"$ne": True}, # not timedout in queue
98-
},
99-
n=optimal_batch_size,
100-
select_keys=[f"{predictor_sequence - 1}.outputs"],
101-
update={
102-
"last_predictor_sequence": predictor_sequence, # set last predictor sequence to current predictor sequence
103-
"last_predictor_success": None, # reset last predictor success
104-
f"{predictor_sequence}.received_at": time.time(), # set received at to current time
105-
},
106-
)
98+
if current_sum_of_to_process < optimal_batch_size:
99+
to_process.update(
100+
main_index.search(
101+
query={
102+
"-1.predicted_at": 0, # prediction not yet done
103+
"last_predictor_success": True, # last predictor success
104+
"last_predictor_sequence": predictor_sequence - 1, # last predictor sequence
105+
"timedout_in_queue": {"$ne": True}, # not timedout in queue
106+
},
107+
n=optimal_batch_size,
108+
select_keys=[f"{predictor_sequence - 1}.outputs"],
109+
update={
110+
"last_predictor_sequence": predictor_sequence, # set last predictor sequence to current predictor sequence
111+
"last_predictor_success": None, # reset last predictor success
112+
f"{predictor_sequence}.received_at": time.time(), # set received at to current time
113+
},
114+
)
115+
)
107116

108117
for unique_id, data in to_process.items():
118+
if current_batch_length > optimal_batch_size * 0.8:
119+
break
109120
outputs = data[f"{predictor_sequence - 1}.outputs"]
110121
input_count = len(outputs)
111122
unique_id_wise_input_count[unique_id] = input_count
112123
input_batch.extend(outputs)
113124
current_batch_length += input_count
114125
last_input_received_at = time.time()
115126

127+
for unique_id in unique_id_wise_input_count.keys():
128+
try:
129+
del to_process[unique_id]
130+
except:
131+
pass
132+
133+
current_sum_of_to_process = sum(
134+
len(v[f"{predictor_sequence - 1}.outputs"]) for v in to_process.values()
135+
)
136+
116137
if current_batch_length == 0:
117138
if time.time() - last_input_received_at > 5:
118139
time.sleep(0.05)
@@ -133,8 +154,9 @@ def fetch_batch(
133154
break
134155

135156
_utils.logger.info(
136-
f"Fetched batch {[v for v in unique_id_wise_input_count.values()]}"
157+
f"Fetched batch {unique_id_wise_input_count} with {current_sum_of_to_process} remaining in memory, to_process: {len(to_process)}"
137158
)
159+
138160
return unique_id_wise_input_count, input_batch
139161

140162

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
EMAIL = "praneeth@bpraneeth.com"
1919
AUTHOR = "BEDAPUDI PRANEETH"
2020
REQUIRES_PYTHON = ">=3.6.0"
21-
VERSION = "3.0.30"
21+
VERSION = "3.0.31"
2222

2323
# What packages are required for this module to be executed?
2424
REQUIRED = ["falcon", "liteindex==0.0.3.2.dev6", "zstandard", "gunicorn[gevent]", "msgpack"]

testing/benchmark.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ def generate_payload(self):
7575
else: # function
7676
if self._loaded_function is None:
7777
self._load_function()
78-
return self._loaded_function()
78+
return self._loaded_function()[:self.request_batch_size]
7979

8080
def run(self):
8181
# Handle Ctrl+C gracefully

0 commit comments

Comments
 (0)