Skip to content

Commit 35f3201

Browse files
committed
Neuron Release Dec 20th, 2019
1 parent 4466207 commit 35f3201

11 files changed

Lines changed: 1118 additions & 4 deletions

docs/pytorch-neuron/README.md

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,16 @@
1-
# Pytorch Neuron
1+
# PyTorch Neuron
22

3-
We know how great Pytorch is and we are working on adding Neuron support- its coming very soon.
3+
## Table of Contents
44

5-
Contact at us at aws-neuron-support@amazon.com for more information.
5+
1. PyTorch Neuron Overview
6+
2. Getting started
7+
8+
## PyTorch Neuron Overview
9+
Neuron is integrated into PyTorch, and provides you with a familiar environment to run inference using Inferentia based instances.
10+
11+
## Getting started
12+
* [Tutorial: Using Neuron to run Resnet50 inference](./tutorial-compile-infer.md)
13+
* [Tutorial: Manual partitioning of Resnet50 in a Jupyter Notebook](./tutorial-manual-partitioning.md)
14+
15+
* [Reference: PyTorch-Neuron Compilation API](./api-compilation-python-api.md)
16+
* [Supported Operators](../../release-notes/neuron-cc-ops/neuron-cc-ops-pytorch.md)
Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
# Reference: PyTorch-Neuron trace python API
2+
3+
The PyTorch-Neuron trace Python API provides a method to generate pytorch models for execution on Inferentia, which can be serialized as torchscript. It is analogous to torch.jit.trace function in PyTorch
4+
5+
## Method
6+
7+
```
8+
import torch
9+
import torch_neuron
10+
torch.neuron.trace(model, example_inputs, compiler_args)
11+
```
12+
13+
## Description
14+
15+
The torch.neuron.trace method sends Neuron-supported operations to the Neuron-Compiler for compilation and embeds compiled artifacts in a torch script graph.
16+
17+
Compilation can be done on any EC2 machine with sufficient memory and compute resources. c5.4xlarge or larger is recommended.
18+
19+
The compiled graph can be saved using the torch.jit.save function and restored using torch.jit.load function for inference on Inf1 instances. During inference, the previously compiled artifacts will be loaded into the Neuron Runtime for inference execution.
20+
21+
Options can be passed to Neuron compiler via the compile function. See [Neuron Compiler CLI](https://github.com/aws/aws-neuron-sdk/blob/master/docs/neuron-cc/command-line-reference.md) for more information about compiler options.
22+
23+
## Arguments
24+
25+
* model: A Python function or torch.nn.Module that will be run with example_inputs arguments and returns to ``func`` must be tensors or (possibly nested) tuples that contain tensors. When a module is passed to torch.neuron.trace, only the forward method is run and traced
26+
* example_inputs: A tuple of example inputs that will be passed to the function while tracing. The resulting trace can be run with inputs of different types and shapes assuming the traced operations support those types and shapes. example_inputs may also be a single Tensor in which case it is automatically wrapped in a tuple.
27+
28+
## Keyword Arguments
29+
30+
* compiler_args: (Optional) List of strings representing neuron-cc compiler arguments. Note that these arguments apply to all subgraphs generated by whitelist partitioning. For example, use `compiler_args=['--num-neuroncores', '4']` to set number of NeuronCores per subgraph to 4. See [Neuron Compiler CLI](https://github.com/aws/aws-neuron-sdk/blob/master/docs/neuron-cc/command-line-reference.md) for more information about compiler options.
31+
* compiler_timeout (int, optional): Timeout in seconds for waiting neuron-cc to complete. Exceeding timeout will cause a `subprocess.TimeoutExpired` being raised
32+
* compiler_workdir (path-like, optional): Work directory used by neuron-cc. Useful for debugging and/or inspecting neuron-cc logs/IRs
33+
* check_trace (``bool``, optional): Check if the same inputs run through traced code produce the same outputs. Default: ``True``. You might want to disable this if, for example, your network contains non-deterministic ops or if you are sure that the network is correct despite a checker failure
34+
* check_inputs (list of tuples, optional): A list of tuples of input arguments that should be used to check the trace against what is expected. Each tuple is equivalent to a set of input arguments that would be specified in ``example_inputs``. For best results, pass in a set of checking inputs representative of the space of shapes and types of inputs you expect the network to see. If not specified, the original example_inputs are used for checking
35+
* check_tolerance (float, optional): Floating-point comparison tolerance to use in the checker procedure. This can be used to relax the checker strictness in the event that results diverge numerically for a known reason, such as operator fusion.
36+
37+
38+
## Returns
39+
40+
* If model is an nn.Module or is the forward method of an nn.Module:
41+
* If model is in evaluation mode (has property `training==False`), ``trace`` returns a :class:`ScriptModule` object with a single ``forward`` method containing the traced code.
42+
* Otherwise ``trace`` returns input argument `func` as-is.
43+
* If ``callable`` is a standalone function, ``trace`` returns `torch._C.Function` Model with compiled artifacts embedded.
44+
45+
## Example Usage
46+
47+
Example (tracing a function):
48+
49+
```
50+
import torch
51+
import torch_neuron
52+
53+
def foo(x, y):
54+
return 2 * x + y
55+
56+
# Run `foo` with the provided inputs and record the tensor operations
57+
traced_foo = torch.neuron.trace(foo, (torch.rand(3), torch.rand(3)))
58+
59+
# `traced_foo` can now be run with the TorchScript interpreter or saved
60+
# and loaded in a Python-free environment
61+
```
62+
63+
Example (tracing an existing module)::
64+
65+
```
66+
import torch
67+
import torch_neuron
68+
import torch.nn as nn
69+
70+
class Net(nn.Module):
71+
def __init__(self):
72+
super(Net, self).__init__()
73+
self.conv = nn.Conv2d(1, 1, 3)
74+
75+
def forward(self, x):
76+
return self.conv(x)
77+
78+
n = Net()
79+
n.eval()
80+
example_weight = torch.rand(1, 1, 3, 3)
81+
example_forward_input = torch.rand(1, 1, 3, 3)
82+
83+
# Trace a specific method and construct `ScriptModule` with
84+
# a single `forward` method
85+
module = torch.neuron.trace(n.forward, example_forward_input)
86+
87+
# Trace a module (implicitly traces `forward`) and construct a
88+
# `ScriptModule` with a single `forward` method
89+
module = torch.neuron.trace(n, example_forward_input)
90+
```
91+
92+
The following is an example usage of the compilation Python API, with default compilation arguments, using a pretrained torch.nn.Module (in this case :
93+
94+
```
95+
import torch
96+
import torch_neuron
97+
from torchvision import models
98+
99+
model = models.resnet50(pretrained=True)
100+
model.eval()
101+
102+
model_neuron = torch.neuron.trace(model, example_inputs=[image])
103+
model_neuron.save("resnet50_neuron.pt")
104+
```
105+
Lines changed: 260 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,260 @@
1+
# Tutorial: Getting started with torch-neuron (resnet-50 tutorial)
2+
3+
## Steps Overview:
4+
5+
1. Launch an EC2 compilation instance (recommended instance: c5.4xlarge or larger)
6+
2. Install Torch-Neuron and Neuron-Compiler on the Compilation Instance
7+
3. Compile the compute-graph on the compilation-instance, and copy the artifacts into the deployment-instance
8+
4. Install Torch-Neuron and Neuron-Runtime on Inf1 (deployment Instance)
9+
5. Run inference on the Inf1 instance
10+
11+
## Step 1: Launch EC2 compilation instance
12+
13+
A typical workflow with the Neuron SDK will be to compile trained ML models on a general compute instance (the compilation instance), and then distribute the artifacts to a fleet of Inf1 instances (the deployment instances) for inference execution. Neuron enables PyTorch for both of these steps.
14+
15+
1.1. Select an AMI of your choice. This may be may be Ubuntu 16.x, Ubuntu 18.x, Amazon Linux 2 based on the Deep Learning AMI (DLAMI).
16+
17+
1.2. Select and launch an EC2 instance
18+
19+
* A c5.4xlarge or larger is recommended. For this example we will use a c5.4xlarge.
20+
* Users may choose to compile and deploy on the same instance, in which case an inf1.6xlarge instance or larger is recommended. If you choose “launch instance” and search for “neuron” in the AWS EC2 console you will see a short list of the DLAMI images to select from.
21+
22+
## Step 2: Compilation instance installations
23+
24+
Install both Neuron Compiler and Torch-Neuron on the compilation instance.
25+
26+
2.1. Install Python3 virtual environment module if needed:
27+
28+
If using an Ubuntu DLAMI:
29+
30+
```
31+
# Ubuntu
32+
sudo apt-get update
33+
sudo apt-get install -y python3-venv g++
34+
```
35+
36+
Note: If you see the following errors during apt-get install, please wait a minute or so for background updates to finish and retry apt-get install:
37+
38+
```
39+
E: Could not get lock /var/lib/dpkg/lock-frontend - open (11: Resource temporarily unavailable)
40+
E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), is another process using it?
41+
```
42+
43+
If using Amazon Linux 2 DLAMI:
44+
45+
```
46+
# Amazon Linux 2
47+
sudo yum update
48+
sudo yum install -y python3 gcc-c++
49+
```
50+
51+
2.2. Create a tutorial folder and cd into it
52+
53+
```
54+
mkdir -p tutorial
55+
cd tutorial
56+
```
57+
58+
2.3. Setup a new Python virtual environment:
59+
60+
```
61+
python3 -m venv test_venv
62+
source test_venv/bin/activate
63+
pip install -U pip
64+
```
65+
66+
2.4. Modify Pip repository configurations to point to the Neuron repository.
67+
68+
```
69+
tee $VIRTUAL_ENV/pip.conf > /dev/null <<EOF
70+
[global]
71+
extra-index-url = https://pip.repos.neuron.amazonaws.com
72+
EOF
73+
```
74+
75+
2.5. Install Torch-Neuron and Neuron Compiler
76+
77+
```
78+
pip install torch-neuron
79+
```
80+
81+
```
82+
# Install compiler.
83+
# NOTE: please make sure tensorflow option is provided; this is not necessary for inference-only purposes.
84+
pip install neuron-cc[tensorflow]
85+
```
86+
87+
2.6 Install torchvision for the pretrained resnet50 model (we use no-deps here because we already have Neuron version of torch installed through torch-neuron)
88+
89+
```
90+
pip install pillow
91+
92+
# We use the --no-deps here to prevent torchvision installing standard torch
93+
pip install torchvision --no-deps
94+
```
95+
96+
## Step 3: Compile on compilation instance
97+
98+
A trained model must be compiled to Inferentia target before it can be deployed on Inf1 instances. In this step we compile the torchvision ResNet50 model and export it as a SavedModel which is in the torchscript format for PyTorch models.
99+
100+
3.1. Create a python script named `trace_resnet50.py` with the following content:
101+
102+
```
103+
import torch
104+
import numpy as np
105+
import os
106+
from urllib import request
107+
108+
from torchvision import models, transforms, datasets
109+
110+
import torch_neuron
111+
112+
## Create an image directory containing a small kitten
113+
os.makedirs("./images", exist_ok=True)
114+
request.urlretrieve("https://raw.githubusercontent.com/awslabs/mxnet-model-server/master/docs/images/kitten_small.jpg",
115+
"./images/kitten_small.jpg")
116+
117+
## Import our image and normalize it into a tensor
118+
normalize = transforms.Normalize(
119+
mean=[0.485, 0.456, 0.406],
120+
std=[0.229, 0.224, 0.225])
121+
122+
eval_dataset = datasets.ImageFolder(
123+
os.path.dirname('./'),
124+
transforms.Compose([
125+
transforms.Resize([224, 224]),
126+
transforms.ToTensor(),
127+
normalize,
128+
])
129+
)
130+
131+
image, _ = eval_dataset[0]
132+
image = torch.tensor(image.numpy()[np.newaxis, ...])
133+
134+
## Load a pretrained ResNet50 model
135+
model = models.resnet50(pretrained=True)
136+
137+
## Tell the model we are using it for evaluation (not training)
138+
model.eval()
139+
140+
model_neuron = torch.neuron.trace(model, example_inputs=[image])
141+
142+
model_neuron.save( "resnet50_neuron.pt" )
143+
```
144+
145+
146+
3.2. Run the compilation script, which will take a few minutes on c5.4xlarge. At the end of script execution, the compiled SavedModel is zipped as `resnet50_neuron.pt` in local directory:
147+
148+
```
149+
python trace_resnet50.py
150+
```
151+
152+
You should see:
153+
154+
```
155+
INFO:Neuron:compiling module ResNet with neuron-cc
156+
```
157+
158+
3.3 **WARNING**: If you run the inference script below on you CPU instance you will get output, but see this warning:
159+
160+
```
161+
[E neuron_op_impl.cpp:53] Warning: Tensor output are *** NOT CALCULATED *** during CPU
162+
execution and only indicate tensor shape
163+
```
164+
165+
This is an artifact of the way we trace a model on your compile instance. **Do not perform inference with a neuron traced model on a non neuron supported instance, results will not be calculated.**
166+
167+
3.4. If not compiling and inferring on the same instance, copy the compiled artifacts to the inference server:
168+
169+
```
170+
scp -i <PEM key file> ./resnet50_neuron.pt ubuntu@<instance DNS>:~/ # if Ubuntu-based AMI
171+
scp -i <PEM key file> ./resnet50_neuron.pt ec2-user@<instance DNS>:~/ # if using AML2-based AMI
172+
```
173+
174+
## Step 4: Deployment Instance Installations
175+
176+
On the instance you are going to use for inference, install Torch-Neuron and Neuron Runtime
177+
178+
4.1. Follow Step 2 above to install Torch-Neuron.
179+
180+
* Install neuron-cc[tensorflow] if compilation on inference instance is desired (see notes above on recommended Inf1 sizes for compilation)
181+
* Skip neuron-cc if compilation is not done on inference instance
182+
183+
4.2. Install the Neuron Runtime using instructions from [Getting started: Installing and Configuring Neuron-RTD](https://github.com/aws/aws-neuron-sdk/blob/master/docs/neuron-runtime/nrt_start.md).
184+
185+
186+
## Step 5: Run inference
187+
188+
In this step we run inference on inf1 instances using the model compiled in Step 3.
189+
190+
5.1. On the inf1, create a inference Python script named `infer_resnet50.py` with the following content:
191+
192+
193+
```
194+
import os
195+
import time
196+
import torch
197+
import torch_neuron
198+
import json
199+
import numpy as np
200+
201+
from urllib import request
202+
203+
from torchvision import models, transforms, datasets
204+
205+
## Create an image directory containing a small kitten
206+
os.makedirs("./images", exist_ok=True)
207+
request.urlretrieve("https://raw.githubusercontent.com/awslabs/mxnet-model-server/master/docs/images/kitten_small.jpg",
208+
"./images/kitten_small.jpg")
209+
210+
211+
## Fetch labels to output the top classifications
212+
request.urlretrieve("https://s3.amazonaws.com/deep-learning-models/image-models/imagenet_class_index.json","imagenet_class_index.json")
213+
idx2label = []
214+
215+
with open("imagenet_class_index.json", "r") as read_file:
216+
`class_idx `**`=`**` json`**`.`**`load``(``read_file``)`
217+
idx2label = [class_idx[str(k)][1] for k in range(len(class_idx))]
218+
219+
## Import a sample image and normalize it into a tensor
220+
normalize = transforms.Normalize(
221+
mean=[0.485, 0.456, 0.406],
222+
std=[0.229, 0.224, 0.225])
223+
224+
eval_dataset = datasets.ImageFolder(
225+
os.path.dirname("./"),
226+
transforms.Compose([
227+
transforms.Resize([224, 224]),
228+
transforms.ToTensor(),
229+
normalize,
230+
])
231+
)
232+
233+
image, _ = eval_dataset[0]
234+
image = torch.tensor(image.numpy()[np.newaxis, ...])
235+
236+
## Load model
237+
model_neuron = torch.jit.load( 'resnet50_neuron.pt' )
238+
239+
## Predict
240+
results = model_neuron( image )
241+
242+
# Get the top 5 results
243+
top5_idx = results[0].sort()[1][-5:]
244+
245+
# Lookup and print the top 5 labels
246+
top5_labels = [idx2label[idx] for idx in top5_idx]
247+
248+
print("Top 5 labels:\n {}".format(top5_labels) )
249+
```
250+
251+
252+
5.2. Run the inference:
253+
254+
```
255+
['tiger', 'lynx', 'tiger_cat', 'Egyptian_cat', 'tabby']
256+
```
257+
258+
## Step 6: Terminate instances
259+
260+
Don’t forget to terminate your instances (compile and inference) from the AWS console so that you don’t continue paying for them once you are done

0 commit comments

Comments
 (0)