-
Notifications
You must be signed in to change notification settings - Fork 77
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
[Python] Add initial support for asyncio #471
Open
VivekPanyam
wants to merge
1
commit into
master
Choose a base branch
from
python_async_infer
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 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 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 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,94 @@ | ||
# Copyright (c) 2021 UATC, LLC | ||
# | ||
# 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 os | ||
import six | ||
import numpy as np | ||
import unittest | ||
from testpath.tempdir import TemporaryDirectory | ||
|
||
from neuropod.loader import load_neuropod | ||
from neuropod.packagers import create_python_neuropod | ||
from neuropod.tests.utils import get_addition_model_spec | ||
|
||
ADDITION_MODEL_SOURCE = """ | ||
import sys | ||
|
||
def addition_model(x, y): | ||
return { | ||
"out": x + y | ||
} | ||
|
||
def get_model(_): | ||
return addition_model | ||
""" | ||
|
||
|
||
@unittest.skipIf(six.PY2, "Skipping asyncio test for Python 2") | ||
class TestAsync(unittest.TestCase): | ||
def package_simple_addition_model(self, test_dir, do_fail=False): | ||
neuropod_path = os.path.join(test_dir, "test_neuropod") | ||
model_code_dir = os.path.join(test_dir, "model_code") | ||
os.makedirs(model_code_dir) | ||
|
||
with open(os.path.join(model_code_dir, "addition_model.py"), "w") as f: | ||
f.write(ADDITION_MODEL_SOURCE) | ||
|
||
# `create_python_neuropod` runs inference with the test data immediately | ||
# after creating the neuropod. Raises a ValueError if the model output | ||
# does not match the expected output. | ||
create_python_neuropod( | ||
neuropod_path=neuropod_path, | ||
model_name="addition_model", | ||
data_paths=[], | ||
code_path_spec=[ | ||
{ | ||
"python_root": model_code_dir, | ||
"dirs_to_package": [""], # Package everything in the python_root | ||
} | ||
], | ||
entrypoint_package="addition_model", | ||
entrypoint="get_model", | ||
# Get the input/output spec along with test data | ||
**get_addition_model_spec(do_fail=do_fail) | ||
) | ||
|
||
return neuropod_path | ||
|
||
def test_async_inference(self): | ||
# Get an event loop | ||
import asyncio | ||
|
||
loop = asyncio.get_event_loop() | ||
|
||
with TemporaryDirectory() as test_dir: | ||
# Package a model | ||
path = self.package_simple_addition_model(test_dir) | ||
|
||
# Sample input data | ||
input_data = { | ||
"x": np.array([0.5], dtype=np.float32), | ||
"y": np.array([1.5], dtype=np.float32), | ||
} | ||
|
||
with load_neuropod(path) as model: | ||
# Async infer | ||
result = loop.run_until_complete(model.infer_async(input_data)) | ||
|
||
# Ensure the output is what we expect | ||
self.assertEqual(result["out"][0], 2.0) | ||
|
||
|
||
if __name__ == "__main__": | ||
unittest.main() |
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
close loop? Or use "run" that cares about it (I see it has "debug" that maybe good for test) https://docs.python.org/3/library/asyncio-task.html#running-an-asyncio-program