-
Notifications
You must be signed in to change notification settings - Fork 608
Prevent threads from being stuck in DynamicBatcher #1915
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
acb0f03
Prevent threads from being stuck in DynamicBatcher
cbensimon 2f54f24
Merge branch 'master' into patch-1
RobertLucian 2e5e6eb
Merge branch 'master' into patch-1
RobertLucian d2b1897
Enforce max batch size
RobertLucian f0df347
Don't use relative imports
RobertLucian 4420a71
Add test file for dynamic batcher for dev (to be added to test suite)
RobertLucian f41cf00
Merge branch 'master' into patch-1
RobertLucian ea75ffa
Rename thread_ids to sample_ids
RobertLucian d179a11
Convert manual dynamic_batcher_test to unit test
RobertLucian d4327cf
Missing word in python comment
RobertLucian e404227
Merge branch 'master' into patch-1
RobertLucian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
grpcio==1.32.0 | ||
boto3==1.14.53 | ||
google-cloud-storage==1.32.0 | ||
datadog==0.39.0 | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
pkg/cortex/serve/cortex_internal/lib/test/dynamic_batching_test.py
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
# Copyright 2021 Cortex Labs, 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. | ||
|
||
|
||
import threading as td | ||
import itertools | ||
import time | ||
|
||
import cortex_internal.lib.api.batching as batching | ||
|
||
|
||
class Predictor: | ||
def predict(self, payload): | ||
time.sleep(0.2) | ||
return payload | ||
|
||
|
||
def test_dynamic_batching_while_hitting_max_batch_size(): | ||
max_batch_size = 32 | ||
dynamic_batcher = batching.DynamicBatcher( | ||
Predictor(), max_batch_size=max_batch_size, batch_interval=0.1, test_mode=True | ||
) | ||
counter = itertools.count(1) | ||
event = td.Event() | ||
global_list = [] | ||
|
||
def submitter(): | ||
while not event.is_set(): | ||
global_list.append(dynamic_batcher.predict(payload=next(counter))) | ||
time.sleep(0.1) | ||
|
||
running_threads = [] | ||
for _ in range(128): | ||
thread = td.Thread(target=submitter, daemon=True) | ||
thread.start() | ||
running_threads.append(thread) | ||
|
||
time.sleep(60) | ||
event.set() | ||
|
||
# if this fails, then the submitter threads are getting stuck | ||
for thread in running_threads: | ||
thread.join(3.0) | ||
if thread.is_alive(): | ||
raise TimeoutError("thread", thread.getName(), "got stuck") | ||
|
||
sum1 = int(len(global_list) * (len(global_list) + 1) / 2) | ||
sum2 = sum(global_list) | ||
assert sum1 == sum2 | ||
|
||
# get the last 80% of batch lengths | ||
# we ignore the first 20% because it may take some time for all threads to start making requests | ||
batch_lengths = dynamic_batcher._test_batch_lengths | ||
batch_lengths = batch_lengths[int(len(batch_lengths) * 0.2) :] | ||
|
||
# verify that the batch size is always equal to the max batch size | ||
assert len(set(batch_lengths)) == 1 | ||
assert max_batch_size in batch_lengths | ||
|
||
|
||
def test_dynamic_batching_while_hitting_max_interval(): | ||
max_batch_size = 32 | ||
dynamic_batcher = batching.DynamicBatcher( | ||
Predictor(), max_batch_size=max_batch_size, batch_interval=1.0, test_mode=True | ||
) | ||
counter = itertools.count(1) | ||
event = td.Event() | ||
global_list = [] | ||
|
||
def submitter(): | ||
while not event.is_set(): | ||
global_list.append(dynamic_batcher.predict(payload=next(counter))) | ||
time.sleep(0.1) | ||
|
||
running_threads = [] | ||
for _ in range(2): | ||
thread = td.Thread(target=submitter, daemon=True) | ||
thread.start() | ||
running_threads.append(thread) | ||
|
||
time.sleep(30) | ||
event.set() | ||
|
||
# if this fails, then the submitter threads are getting stuck | ||
for thread in running_threads: | ||
thread.join(3.0) | ||
if thread.is_alive(): | ||
raise TimeoutError("thread", thread.getName(), "got stuck") | ||
|
||
sum1 = int(len(global_list) * (len(global_list) + 1) / 2) | ||
sum2 = sum(global_list) | ||
assert sum1 == sum2 | ||
|
||
# get the last 80% of batch lengths | ||
# we ignore the first 20% because it may take some time for all threads to start making requests | ||
batch_lengths = dynamic_batcher._test_batch_lengths | ||
batch_lengths = batch_lengths[int(len(batch_lengths) * 0.2) :] | ||
|
||
# verify that the batch size is always equal to the number of running threads | ||
assert len(set(batch_lengths)) == 1 | ||
assert len(running_threads) in batch_lengths |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.