Skip to content

Update Sagemaker Neo Notebooks examples to PyTorch 1.6 #2018

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 1 commit into from
Feb 23, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions sagemaker_neo_compilation_jobs/pytorch_torchvision/code/resnet18.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import numpy as np
import torch
import neopytorch
import torchvision.transforms as transforms
from PIL import Image # Training container doesn't have this package

Expand Down Expand Up @@ -55,24 +56,23 @@ def transform_fn(model, payload, request_content_type,
def model_fn(model_dir):

logger.info('model_fn')
with torch.neo.config(model_dir=model_dir, neo_runtime=True):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# The compiled model is saved as "compiled.pt"
model = torch.jit.load(os.path.join(model_dir, 'compiled.pt'))
model = model.to(device)

# It is recommended to run warm-up inference during model load
sample_input_path = os.path.join(model_dir, 'sample_input.pkl')
with open(sample_input_path, 'rb') as input_file:
model_input = pickle.load(input_file)
if torch.is_tensor(model_input):
model_input = model_input.to(device)
model(model_input)
elif isinstance(model_input, tuple):
model_input = (inp.to(device)
for inp in model_input if torch.is_tensor(inp))
model(*model_input)
else:
print("Only supports a torch tensor or a tuple of torch tensors")

return model
neopytorch.config(model_dir=model_dir, neo_runtime=True)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# The compiled model is saved as "compiled.pt"
model = torch.jit.load(os.path.join(model_dir, 'compiled.pt'), map_location=device)

# It is recommended to run warm-up inference during model load
sample_input_path = os.path.join(model_dir, 'sample_input.pkl')
with open(sample_input_path, 'rb') as input_file:
model_input = pickle.load(input_file)
if torch.is_tensor(model_input):
model_input = model_input.to(device)
model(model_input)
elif isinstance(model_input, tuple):
model_input = (inp.to(device)
for inp in model_input if torch.is_tensor(inp))
model(*model_input)
else:
print("Only supports a torch tensor or a tuple of torch tensors")

return model
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@
"data_shape = '{\"input0\":[1,3,224,224]}'\n",
"target_device = 'ml_c5'\n",
"framework = 'PYTORCH'\n",
"framework_version = '1.4.0'\n",
"framework_version = '1.6'\n",
"compiled_model_path = 's3://{}/{}/output'.format(bucket, compilation_job_name)"
]
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import numpy as np
import torch
import neopytorch
import torchvision.transforms as transforms
from PIL import Image # Training container doesn't have this package

Expand Down Expand Up @@ -55,24 +56,23 @@ def transform_fn(model, payload, request_content_type,
def model_fn(model_dir):

logger.info('model_fn')
with torch.neo.config(model_dir=model_dir, neo_runtime=True):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# The compiled model is saved as "compiled.pt"
model = torch.jit.load(os.path.join(model_dir, 'compiled.pt'))
model = model.to(device)
neopytorch.config(model_dir=model_dir, neo_runtime=True)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# The compiled model is saved as "compiled.pt"
model = torch.jit.load(os.path.join(model_dir, 'compiled.pt'), map_location=device)

# It is recommended to run warm-up inference during model load
sample_input_path = os.path.join(model_dir, 'sample_input.pkl')
with open(sample_input_path, 'rb') as input_file:
model_input = pickle.load(input_file)
if torch.is_tensor(model_input):
model_input = model_input.to(device)
model(model_input)
elif isinstance(model_input, tuple):
model_input = (inp.to(device)
for inp in model_input if torch.is_tensor(inp))
model(*model_input)
else:
print("Only supports a torch tensor or a tuple of torch tensors")
# It is recommended to run warm-up inference during model load
sample_input_path = os.path.join(model_dir, 'sample_input.pkl')
with open(sample_input_path, 'rb') as input_file:
model_input = pickle.load(input_file)
if torch.is_tensor(model_input):
model_input = model_input.to(device)
model(model_input)
elif isinstance(model_input, tuple):
model_input = (inp.to(device)
for inp in model_input if torch.is_tensor(inp))
model(*model_input)
else:
print("Only supports a torch tensor or a tuple of torch tensors")

return model
return model
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

import numpy as np
import torch
import neopytorch
import torchvision.transforms as transforms
from PIL import Image # Training container doesn't have this package

Expand Down Expand Up @@ -55,10 +56,9 @@ def transform_fn(model, payload, request_content_type,
def model_fn(model_dir):

logger.info('model_fn')
with torch.neo.config(model_dir=model_dir, neo_runtime=True):
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# The compiled model is saved as "compiled.pt"
model = torch.jit.load(os.path.join(model_dir, 'model.pth'))
model = model.to(device)
neopytorch.config(model_dir=model_dir, neo_runtime=True)
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
# The compiled model is saved as "compiled.pt"
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fix the comment - it says compiled.pt when the name used is model.pth

model = torch.jit.load(os.path.join(model_dir, 'model.pth'), map_location=device)

return model
return model
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
"metadata": {},
"outputs": [],
"source": [
"!~/anaconda3/envs/pytorch_p36/bin/pip install torch==1.4.0 torchvision==0.5.0"
"!~/anaconda3/envs/pytorch_p36/bin/pip install torch==1.6.0 torchvision==0.7.0"
]
},
{
Expand Down Expand Up @@ -127,7 +127,7 @@
"data_shape = '{\"input0\":[1,3,224,224]}'\n",
"target_device = 'ml_c5'\n",
"framework = 'pytorch'\n",
"framework_version = '1.4.0'\n",
"framework_version = '1.6'\n",
"compiled_model_path = 's3://{}/{}/output'.format(bucket, compilation_job_name)\n",
"\n",
"inference_image_uri = image_uris.retrieve(f'neo-{framework}', region, framework_version, instance_type=target_device)"
Expand Down