-
Notifications
You must be signed in to change notification settings - Fork 72
change: default model_fn and predict_fn in default handler #51
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
6 commits
Select commit
Hold shift + click to select a range
2ec7e9e
change: default model_fn and predict_fn in default handler
80767f0
change: default model_fn and predict_fn in default handler
63f3d8f
import sys
29d9b9d
address comments regarding logging, error message, etc
990558a
add unit tests
cddda1e
modify test file
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -12,29 +12,39 @@ | |
# language governing permissions and limitations under the License. | ||
from __future__ import absolute_import | ||
|
||
import os | ||
import textwrap | ||
|
||
import torch | ||
|
||
from sagemaker_inference import content_types, decoder, default_inference_handler, encoder | ||
|
||
INFERENCE_ACCELERATOR_PRESENT_ENV = "SAGEMAKER_INFERENCE_ACCELERATOR_PRESENT" | ||
DEFAULT_MODEL_FILENAME = "model.pt" | ||
|
||
|
||
class DefaultPytorchInferenceHandler(default_inference_handler.DefaultInferenceHandler): | ||
VALID_CONTENT_TYPES = (content_types.JSON, content_types.NPY) | ||
|
||
def default_model_fn(self, model_dir): | ||
"""Loads a model. For PyTorch, a default function to load a model cannot be provided. | ||
Users should provide customized model_fn() in script. | ||
"""Loads a model. For PyTorch, a default function to load a model only if Elastic Inference is used. | ||
In other cases, users should provide customized model_fn() in script. | ||
|
||
Args: | ||
model_dir: a directory where model is saved. | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should the docstrings be updated? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. docstring updated |
||
Returns: A PyTorch model. | ||
""" | ||
raise NotImplementedError(textwrap.dedent(""" | ||
Please provide a model_fn implementation. | ||
See documentation for model_fn at https://github.com/aws/sagemaker-python-sdk | ||
""")) | ||
if os.getenv(INFERENCE_ACCELERATOR_PRESENT_ENV) == "true": | ||
model_path = os.path.join(model_dir, DEFAULT_MODEL_FILENAME) | ||
if not os.path.exists(model_path): | ||
raise FileNotFoundError("Failed to load model with default model_fn: missing file {}." | ||
.format(DEFAULT_MODEL_FILENAME)) | ||
return torch.jit.load(model_path) | ||
else: | ||
raise NotImplementedError(textwrap.dedent(""" | ||
Please provide a model_fn implementation. | ||
See documentation for model_fn at https://github.com/aws/sagemaker-python-sdk | ||
""")) | ||
|
||
def default_input_fn(self, input_data, content_type): | ||
"""A default input_fn that can handle JSON, CSV and NPZ formats. | ||
|
@@ -62,12 +72,20 @@ def default_predict_fn(self, data, model): | |
|
||
Returns: a prediction | ||
""" | ||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | ||
model.to(device) | ||
input_data = data.to(device) | ||
model.eval() | ||
with torch.no_grad(): | ||
output = model(input_data) | ||
if os.getenv(INFERENCE_ACCELERATOR_PRESENT_ENV) == "true": | ||
device = torch.device("cpu") | ||
model = model.to(device) | ||
input_data = data.to(device) | ||
model.eval() | ||
with torch.jit.optimized_execution(True, {"target_device": "eia:0"}): | ||
output = model(input_data) | ||
else: | ||
device = torch.device("cuda" if torch.cuda.is_available() else "cpu") | ||
model = model.to(device) | ||
input_data = data.to(device) | ||
model.eval() | ||
output = model(input_data) | ||
|
||
return output | ||
|
||
|
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
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.