Skip to content

Commit da2a2e0

Browse files
agunapalmreso
andauthored
update PyTorch 2.x examples to use PyTorch >=2.3 (#3111)
* Updated SAM Fast and aot_compile example * Updated Diffusion Fast example * Updated GPT Fast example * Updated GPT Fast example --------- Co-authored-by: Matthias Reso <[email protected]>
1 parent 3d23fc3 commit da2a2e0

File tree

10 files changed

+26
-72
lines changed

10 files changed

+26
-72
lines changed

examples/large_models/diffusion_fast/README.md

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,7 @@ The example has been tested on A10, A100 as well as H100.
2121
Install dependencies and upgrade torch to nightly build (currently required)
2222
```
2323
git clone https://github.com/huggingface/diffusion-fast.git
24-
pip install --pre torch --index-url https://download.pytorch.org/whl/nightly/cu121 --ignore-installed -y
25-
pip install accelerate transformers peft
24+
pip install accelerate transformers diffusers peft
2625
pip install --no-cache-dir git+https://github.com/pytorch-labs/ao@54bcd5a10d0abbe7b0c045052029257099f83fd9
2726
pip install pandas matplotlib seaborn
2827
```

examples/large_models/gpt_fast/README.md

+6-8
Original file line numberDiff line numberDiff line change
@@ -18,20 +18,18 @@ The examples has been tested on A10, A100 as well as H100.
1818

1919
#### Pre-requisites
2020

21+
- PyTorch 2.3
22+
- CUDA >= 11.8
23+
2124
`cd` to the example folder `examples/large_models/gpt_fast`
2225

23-
Install dependencies and upgrade torch to nightly build (currently required)
26+
Install dependencies
2427
```
2528
git clone https://github.com/pytorch-labs/gpt-fast/
29+
cd gpt-fast
2630
git checkout f44ef4eb55b54ec4c452b669eee409421adabd60
2731
pip install sentencepiece huggingface_hub
28-
pip uninstall torchtext torchdata torch torchvision torchaudio -y
29-
pip install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cu121 --ignore-installed
30-
```
31-
32-
You can also install PyTorch nightlies using the below command
33-
```
34-
python ./ts_scripts/install_dependencies.py --cuda=cu121 --nightly_torch
32+
cd ..
3533
```
3634

3735
### Step 1: Download and convert the weights

examples/large_models/gpt_fast/model_config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ maxBatchDelay: 200
55
responseTimeout: 300
66
deviceType: "gpu"
77
handler:
8-
converted_ckpt_dir: "checkpoints/meta-llama/Llama-2-7b-hf/model.pth"
8+
converted_ckpt_dir: "checkpoints/meta-llama/Llama-2-7b-chat-hf/model.pth"
99
max_new_tokens: 50
1010
compile: true
1111
fx_graph_cache: True

examples/large_models/segment_anything_fast/README.md

+8-14
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ Details on how this is achieved can be found in this [blog](https://pytorch.org/
1515

1616
#### Pre-requisites
1717

18-
Needs python 3.10
18+
- Needs python 3.10
19+
- PyTorch >= 2.3.0
1920

2021
`cd` to the example folder `examples/large_models/segment_anything_fast`
2122

@@ -24,8 +25,6 @@ Install `Segment Anything Fast` by running
2425
chmod +x install_segment_anything_fast.sh
2526
source install_segment_anything_fast.sh
2627
```
27-
Segment Anything Fast needs the nightly version of PyTorch. Hence the script is uninstalling PyTorch, its domain libraries and installing the nightly version of PyTorch.
28-
2928

3029
### Step 1: Download the weights
3130

@@ -47,26 +46,21 @@ Example:
4746
- For `A100` : `process_batch_size=16`
4847

4948

50-
### Step 2: Generate mar or tgz file
51-
52-
```
53-
torch-model-archiver --model-name sam-fast --version 1.0 --handler custom_handler.py --config-file model-config.yaml --archive-format tgz
54-
```
55-
56-
### Step 3: Add the tgz file to model store
49+
### Step 2: Generate model archive
5750

5851
```
5952
mkdir model_store
60-
mv sam-fast.tar.gz model_store
53+
torch-model-archiver --model-name sam-fast --version 1.0 --handler custom_handler.py --config-file model-config.yaml --archive-format no-archive --export-path model_store -f
54+
mv sam_vit_h_4b8939.pth model_store/sam-fast/
6155
```
6256

63-
### Step 4: Start torchserve
57+
### Step 3: Start torchserve
6458

6559
```
66-
torchserve --start --ncs --model-store model_store --models sam-fast.tar.gz
60+
torchserve --start --ncs --model-store model_store --models sam-fast
6761
```
6862

69-
### Step 5: Run inference
63+
### Step 4: Run inference
7064

7165
```
7266
python inference.py

examples/large_models/segment_anything_fast/custom_handler.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import base64
22
import io
33
import logging
4+
import os
45
import pickle
56

67
import cv2
@@ -23,6 +24,7 @@ def __init__(self):
2324

2425
def initialize(self, ctx):
2526
properties = ctx.system_properties
27+
model_dir = properties.get("model_dir")
2628
self.device = "cpu"
2729
if torch.cuda.is_available() and properties.get("gpu_id") is not None:
2830
self.map_location = "cuda"
@@ -32,7 +34,9 @@ def initialize(self, ctx):
3234
torch.cuda.set_device(self.device)
3335

3436
model_type = ctx.model_yaml_config["handler"]["model_type"]
35-
sam_checkpoint = ctx.model_yaml_config["handler"]["sam_checkpoint"]
37+
sam_checkpoint = os.path.join(
38+
model_dir, ctx.model_yaml_config["handler"]["sam_checkpoint"]
39+
)
3640
process_batch_size = ctx.model_yaml_config["handler"]["process_batch_size"]
3741

3842
self.model = sam_model_fast_registry[model_type](checkpoint=sam_checkpoint)

examples/large_models/segment_anything_fast/install_segment_anything_fast.sh

-12
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
#!/bin/bash
22

3-
# Uninstall torchtext, torchdata, torch, torchvision, and torchaudio
4-
pip uninstall torchtext torchdata torch torchvision torchaudio -y
5-
6-
# Install nightly PyTorch and torchvision from the specified index URL
7-
pip install --pre torch torchvision torchaudio --index-url https://download.pytorch.org/whl/nightly/cu121 --ignore-installed
8-
9-
# Optional: Display the installed PyTorch and torchvision versions
10-
python -c "import torch; print('PyTorch version:', torch.__version__)"
11-
python -c "import torchvision; print('torchvision version:', torchvision.__version__)"
12-
13-
echo "PyTorch and torchvision updated successfully!"
14-
153
# Install the segment-anything-fast package from GitHub
164
pip install git+https://github.com/pytorch-labs/segment-anything-fast.git
175

examples/large_models/segment_anything_fast/model-config.yaml

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,5 +2,5 @@ responseTimeout: 300
22
handler:
33
profile: true
44
model_type: "vit_h"
5-
sam_checkpoint: "/home/ubuntu/serve/examples/large_models/segment_anything_fast/sam_vit_h_4b8939.pth"
5+
sam_checkpoint: "sam_vit_h_4b8939.pth"
66
process_batch_size: 8

examples/pt2/README.md

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
## PyTorch 2.x integration
22

3-
PyTorch 2.0 brings more compiler options to PyTorch, for you that should mean better perf either in the form of lower latency or lower memory consumption.
3+
PyTorch 2.x brings more compiler options to PyTorch, for you that should mean better perf either in the form of lower latency or lower memory consumption.
44

55
We strongly recommend you leverage newer hardware so for GPUs that would be an Ampere architecture. You'll get even more benefits from using server GPU deployments like A10G and A100 vs consumer cards. But you should expect to see some speedups for any Volta or Ampere architecture.
66

@@ -16,7 +16,7 @@ pip install torchserve-nightly torch-model-archiver-nightly
1616

1717
## torch.compile
1818

19-
PyTorch 2.0 supports several compiler backends and you pick which one you want by passing in an optional file `model_config.yaml` during your model packaging
19+
PyTorch 2.x supports several compiler backends and you pick which one you want by passing in an optional file `model_config.yaml` during your model packaging
2020

2121
```yaml
2222
pt2: "inductor"

examples/pt2/torch_export_aot_compile/README.md

+2-14
Original file line numberDiff line numberDiff line change
@@ -7,24 +7,12 @@ To understand when to use `torch._export.aot_compile`, please refer to this [sec
77

88
### Pre-requisites
99

10-
- `PyTorch >= 2.3.0` (or PyTorch nightlies)
11-
- `CUDA 12.1`
10+
- `PyTorch >= 2.3.0`
11+
- `CUDA >= 11.8`
1212

1313
Change directory to the examples directory
1414
Ex: `cd examples/pt2/torch_export_aot_compile`
1515

16-
Install PyTorch 2.3 nightlies by running
17-
```
18-
chmod +x install_pytorch_nightlies.sh
19-
source install_pytorch_nightlies.sh
20-
```
21-
22-
You can also achieve this by installing TorchServe dependencies with the `nightly_torch` flag
23-
```
24-
python ts_scripts/install_dependencies.py --cuda=cu121 --nightly_torch
25-
```
26-
27-
2816
### Create a Torch exported model with AOTInductor
2917

3018
The model is saved with `.so` extension

examples/pt2/torch_export_aot_compile/install_pytorch_nightlies.sh

-17
This file was deleted.

0 commit comments

Comments
 (0)