|
1 | 1 | # Large model inference on Inferentia2
|
2 | 2 |
|
3 |
| -This document briefs on serving the [Llama 2](https://huggingface.co/meta-llama) model on [AWS Inferentia2](https://aws.amazon.com/ec2/instance-types/inf2/) for text completion with [micro batching](https://github.com/pytorch/serve/tree/96450b9d0ab2a7290221f0e07aea5fda8a83efaf/examples/micro_batching) and [streaming response](https://github.com/pytorch/serve/blob/96450b9d0ab2a7290221f0e07aea5fda8a83efaf/docs/inference_api.md#curl-example-1) support. |
| 3 | +This folder briefs on serving the [Llama 2](https://huggingface.co/meta-llama) model on [AWS Inferentia2](https://aws.amazon.com/ec2/instance-types/inf2/) for text completion with TorchServe's features: |
4 | 4 |
|
5 |
| -Inferentia2 uses [Neuron SDK](https://aws.amazon.com/machine-learning/neuron/) which is built on top of PyTorch XLA stack. For large model inference [`transformers-neuronx`](https://github.com/aws-neuron/transformers-neuronx) package is used that takes care of model partitioning and running inference. |
6 |
| - |
7 |
| -**Note**: To run the model on an Inf2 instance, the model gets compiled as a preprocessing step. As part of the compilation process, to generate the model graph, a specific batch size is used. Following this, when running inference, we need to pass input which matches the batch size that was used during compilation. Model compilation and input padding to match compiled model batch size is taken care of by the [custom handler](inf2_handler.py) in this example. |
8 |
| - |
9 |
| -The batch size and micro batch size configurations are present in [model-config.yaml](model-config.yaml). The batch size indicates the maximum number of requests torchserve will aggregate and send to the custom handler within the batch delay. |
10 |
| -The batch size is chosen to be a relatively large value, say 16 since micro batching enables running the preprocess(tokenization) and inference steps in parallel on the micro batches. The micro batch size is the batch size used for the Inf2 model compilation. |
11 |
| -Since compilation batch size can influence compile time and also constrained by the Inf2 instance type, this is chosen to be a relatively smaller value, say 4. |
12 |
| - |
13 |
| -This example also demonstrates the utilization of neuronx cache to store inf2 model compilation artifacts using the `NEURONX_CACHE` and `NEURONX_DUMP_TO` environment variables in the custom handler. |
14 |
| -When the model is loaded for the first time, the model is compiled for the configured micro batch size and the compilation artifacts are saved to the neuronx cache. |
15 |
| -On subsequent model load, the compilation artifacts in the neuronx cache serves as `Ahead of Time(AOT)` compilation artifacts and significantly reduces the model load time. |
16 |
| -For convenience, the compiled model artifacts for this example are made available on the Torchserve model zoo: `s3://torchserve/mar_files/llama-2-13b-neuronx-b4`\ |
17 |
| -Instructions on how to use the AOT compiled model artifacts is shown below. |
18 |
| - |
19 |
| -### Step 1: Inf2 instance |
20 |
| - |
21 |
| -Get an Inf2 instance(Note: This example was tested on instance type:`inf2.24xlarge`), ssh to it, make sure to use the following DLAMI as it comes with PyTorch and necessary packages for AWS Neuron SDK pre-installed. |
22 |
| -DLAMI Name: ` Deep Learning AMI Neuron PyTorch 1.13 (Ubuntu 20.04) 20230720 Amazon Machine Image (AMI)` or higher. |
23 |
| - |
24 |
| -**Note**: The `inf2.24xlarge` instance consists of 6 neuron chips with 2 neuron cores each. The total accelerator memory is 192GB. |
25 |
| -Based on the configuration used in [model-config.yaml](model-config.yaml), with `tp_degree` set to 6, 3 of the 6 neuron chips are used, i.e 6 neuron cores. |
26 |
| -On loading the model, the accelerator memory consumed is 38.1GB (12.7GB per chip). |
27 |
| - |
28 |
| -### Step 2: Package Installations |
29 |
| - |
30 |
| -Follow the steps below to complete package installations |
31 |
| - |
32 |
| -```bash |
33 |
| -sudo apt-get update |
34 |
| -sudo apt-get upgrade |
35 |
| - |
36 |
| -# Activate Python venv |
37 |
| -source /opt/aws_neuron_venv_pytorch/bin/activate |
38 |
| - |
39 |
| -# Clone Torchserve git repository |
40 |
| -git clone https://github.com/pytorch/serve.git |
41 |
| -cd serve |
42 |
| - |
43 |
| -# Install dependencies |
44 |
| -python ts_scripts/install_dependencies.py --neuronx --environment=dev |
45 |
| - |
46 |
| -# Install torchserve and torch-model-archiver |
47 |
| -python ts_scripts/install_from_src.py |
48 |
| - |
49 |
| -# Navigate to `examples/large_models/inferentia2/llama2` directory |
50 |
| -cd examples/large_models/inferentia2/llama2/ |
51 |
| - |
52 |
| -# Install additional necessary packages |
53 |
| -python -m pip install -r requirements.txt |
54 |
| -``` |
55 |
| - |
56 |
| -### Step 3: Save the model artifacts compatible with `transformers-neuronx` |
57 |
| -In order to use the pre-compiled model artifacts, copy them from the model zoo using the command shown below and skip to **Step 5** |
58 |
| -```bash |
59 |
| -aws s3 cp s3://torchserve/mar_files/llama-2-13b-neuronx-b4/ llama-2-13b --recursive |
60 |
| -``` |
61 |
| - |
62 |
| -In order to download and compile the Llama2 model from scratch for support on Inf2:\ |
63 |
| -Request access to the Llama2 model\ |
64 |
| -https://huggingface.co/meta-llama/Llama-2-13b-hf |
65 |
| - |
66 |
| -Login to Huggingface |
67 |
| -```bash |
68 |
| -huggingface-cli login |
69 |
| -``` |
70 |
| - |
71 |
| -Run the `inf2_save_split_checkpoints.py` script |
72 |
| -```bash |
73 |
| -python ../util/inf2_save_split_checkpoints.py --model_name meta-llama/Llama-2-13b-hf --save_path './llama-2-13b-split' |
74 |
| -``` |
75 |
| - |
76 |
| - |
77 |
| -### Step 4: Package model artifacts |
78 |
| - |
79 |
| -```bash |
80 |
| -torch-model-archiver --model-name llama-2-13b --version 1.0 --handler inf2_handler.py -r requirements.txt --config-file model-config.yaml --archive-format no-archive |
81 |
| -mv llama-2-13b-split llama-2-13b |
82 |
| -``` |
83 |
| - |
84 |
| -### Step 5: Add the model artifacts to model store |
85 |
| - |
86 |
| -```bash |
87 |
| -mkdir model_store |
88 |
| -mv llama-2-13b model_store |
89 |
| -``` |
90 |
| - |
91 |
| -### Step 6: Start torchserve |
92 |
| - |
93 |
| -```bash |
94 |
| -torchserve --ncs --start --model-store model_store --ts-config config.properties |
95 |
| -``` |
96 |
| - |
97 |
| -### Step 7: Register model |
98 |
| - |
99 |
| -```bash |
100 |
| -curl -X POST "http://localhost:8081/models?url=llama-2-13b" |
101 |
| -``` |
102 |
| - |
103 |
| -### Step 8: Run inference |
104 |
| - |
105 |
| -```bash |
106 |
| -python test_stream_response.py |
107 |
| -``` |
108 |
| - |
109 |
| -### Step 9: Stop torchserve |
110 |
| - |
111 |
| -```bash |
112 |
| -torchserve --stop |
113 |
| -``` |
| 5 | +* demo1: [micro batching](https://github.com/pytorch/serve/tree/96450b9d0ab2a7290221f0e07aea5fda8a83efaf/examples/micro_batching) and [streaming response](https://github.com/pytorch/serve/blob/96450b9d0ab2a7290221f0e07aea5fda8a83efaf/docs/inference_api.md#curl-example-1) support in folder [streamer](streamer). |
| 6 | +* demo2: continuous batching support in folder [continuous_batching](continuous_batching) |
0 commit comments