File size: 9,801 Bytes
cd2355c
 
278fc7f
 
 
 
 
7645d86
278fc7f
 
 
 
 
 
 
5e1823c
 
 
 
 
278fc7f
 
5e1823c
278fc7f
 
 
 
 
 
 
5e1823c
278fc7f
 
 
 
 
 
 
5e1823c
278fc7f
 
 
 
 
 
 
5e1823c
278fc7f
 
 
 
 
 
 
5e1823c
278fc7f
 
 
 
 
 
 
5e1823c
278fc7f
 
 
 
 
 
 
 
5e1823c
278fc7f
 
7645d86
 
278fc7f
 
 
 
5e1823c
278fc7f
 
7645d86
 
278fc7f
 
 
 
 
 
 
867b5a3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7645d86
 
cd2355c
7645d86
 
ad676d5
7645d86
cd2355c
7645d86
ad676d5
7645d86
cd2355c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
278fc7f
cd2355c
7645d86
 
 
 
 
ad676d5
7645d86
 
 
cd2355c
 
 
 
 
 
 
 
 
 
 
278fc7f
7645d86
cd2355c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
278fc7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7645d86
278fc7f
 
7645d86
ad676d5
7645d86
 
278fc7f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7645d86
 
ad676d5
7645d86
cd2355c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
278fc7f
 
867b5a3
7645d86
278fc7f
7645d86
cd2355c
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
DEPLOY_TEXT = f"""

# 🚀 Deployment Tips

A collection of powerful models is valuable, but ultimately, you need to be able to use them effectively. 
This tab is dedicated to providing guidance and code snippets for performing inference with leaderboard models on Intel platforms.

Below is a table of open-source software options for inference, along with the supported Intel hardware platforms. 
A 🚀 indicates that inference with the associated software package is supported on the hardware. We hope this information 
helps you choose the best option for your specific use case. Happy building!

<div style="display: flex; justify-content: center;">
<table border="1">
  <tr>
    <th>Inference Software</th>
    <th>Gaudi</th>
    <th>Xeon</th>
    <th>GPU Max</th>
    <th>Arc GPU</th>
    <th>Core Ultra</th>
  </tr>
  <tr>
    <td>Optimum Habana</td>
    <td>🚀</td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
  </tr>
  <tr>
    <td>Intel Extension for PyTorch</td>
    <td></td>
    <td>🚀</td>
    <td>🚀</td>
    <td>🚀</td>
    <td></td>
  </tr>
  <tr>
    <td>Intel Extension for Transformers</td>
    <td></td>
    <td>🚀</td>
    <td>🚀</td>
    <td>🚀</td>
    <td></td>
  </tr>
  <tr>
    <td>OpenVINO</td>
    <td></td>
    <td>🚀</td>
    <td>🚀</td>
    <td>🚀</td>
    <td>🚀</td>
  </tr>
  <tr>
    <td>BigDL</td>
    <td></td>
    <td>🚀</td>
    <td>🚀</td>
    <td>🚀</td>
    <td>🚀</td>
  </tr>
    <tr>
    <td>NPU Acceleration Library</td>
    <td></td>
    <td></td>
    <td></td>
    <td></td>
    <td>🚀</td>
  </tr>
</tr>
    <tr>
    <td>PyTorch</td>
    <td>🚀</td>
    <td>🚀</td>
    <td></td>
    <td></td>
    <td>🚀</td>
  </tr>
</tr>
    <tr>
    <td>Tensorflow</td>
    <td>🚀</td>
    <td>🚀</td>
    <td></td>
    <td></td>
    <td>🚀</td>
</tr>
</table>
</div>

<hr>

# Intel® Gaudi® Accelerators
The Intel Gaudi 2 accelerator is Intel's most capable deep learning chip. You can learn about Gaudi 2 [here](https://habana.ai/products/gaudi2/).

Intel Gaudi Software supports PyTorch and DeepSpeed for accelerating LLM training and inference. 
The Intel Gaudi Software graph compiler will optimize the execution of the operations accumulated in the graph 
(e.g. operator fusion, data layout management, parallelization, pipelining and memory management, 
and graph-level optimizations).

Optimum Habana provides covenient functionality for various tasks. Below is a command line snippet to run inference on Gaudi with meta-llama/Llama-2-7b-hf.

👍[Optimum Habana GitHub](https://github.com/huggingface/optimum-habana)

The "run_generation.py" script below can be found [here on GitHub](https://github.com/huggingface/optimum-habana/tree/main/examples/text-generation)

```bash
python run_generation.py \
--model_name_or_path meta-llama/Llama-2-7b-hf \
--use_hpu_graphs \
--use_kv_cache \
--max_new_tokens 100 \
--do_sample \
--batch_size 2 \
--prompt "Hello world" "How are you?"

```

<hr>

# Intel® Max Series GPU
The Intel® Data Center GPU Max Series is Intel's highest performing, highest density, general-purpose discrete GPU, which packs over 100 billion transistors into one package and contains up to 128 Xe Cores--Intel's foundational GPU compute building block. You can learn more about this GPU [here](https://www.intel.com/content/www/us/en/products/details/discrete-gpus/data-center-gpu/max-series.html).

### INT4 Inference (GPU) with Intel Extension for Transformers and Intel Extension for Python
Intel® Extension for Transformers is an innovative toolkit designed to accelerate GenAI/LLM everywhere with the optimal performance of Transformer-based models on various Intel platforms, including Intel Gaudi2, Intel CPU, and Intel GPU.

👍 [Intel Extension for Transformers GitHub](https://github.com/intel/intel-extension-for-transformers)

Intel® Extension for PyTorch* extends PyTorch* with up-to-date features optimizations for an extra performance boost on Intel hardware. Optimizations take advantage of Intel® Advanced Vector Extensions 512 (Intel® AVX-512) Vector Neural Network Instructions (VNNI) and Intel® Advanced Matrix Extensions (Intel® AMX) on Intel CPUs as well as Intel Xe Matrix Extensions (XMX) AI engines on Intel discrete GPUs. Moreover, Intel® Extension for PyTorch* provides easy GPU acceleration for Intel discrete GPUs through the PyTorch* xpu device.

👍 [Intel Extension for PyTorch GitHub](https://github.com/intel/intel-extension-for-pytorch)

```python
import intel_extension_for_pytorch as ipex
from intel_extension_for_transformers.transformers.modeling import AutoModelForCausalLM
from transformers import AutoTokenizer

device_map = "xpu"
model_name ="Qwen/Qwen-7B"
tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
prompt = "When winter becomes spring, the flowers..."
inputs = tokenizer(prompt, return_tensors="pt").input_ids.to(device_map)

model = AutoModelForCausalLM.from_pretrained(model_name, trust_remote_code=True,
                                              device_map=device_map, load_in_4bit=True)

model = ipex.optimize_transformers(model, inplace=True, dtype=torch.float16, woq=True, device=device_map)

output = model.generate(inputs)
```
<hr>

# Intel® Xeon® CPUs
The Intel® Xeon® CPUs have the most built-in accelerators of any CPU on the market, including Advanced Matrix Extensions (AMX) to accelerate matrix multiplication in deep learning training and inference. Learn more about the Xeon CPUs [here](https://www.intel.com/content/www/us/en/products/details/processors/xeon.html).

### Optimum Intel and Intel Extension for PyTorch (no quantization)
🤗 Optimum Intel is the interface between the 🤗 Transformers and Diffusers libraries and the different tools and libraries provided by Intel to accelerate end-to-end pipelines on Intel architectures.

👍 [Optimum Intel GitHub](https://github.com/huggingface/optimum-intel)

Requires installing/updating optimum `pip install --upgrade-strategy eager optimum[ipex]`

```python
from optimum.intel import IPEXModelForCausalLM
from transformers import AutoTokenizer, pipeline

model = IPEXModelForCausalLM.from_pretrained(model_id)
tokenizer = AutoTokenizer.from_pretrained(model_id)
pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)
results = pipe("A fisherman at sea...")
```

### Intel® Extension for PyTorch - Mixed Precision (fp32 and bf16)

```python
import torch
import intel_extension_for_pytorch as ipex
import transformers

model= transformers.AutoModelForCausalLM(model_name_or_path).eval()

dtype = torch.float # or torch.bfloat16
model = ipex.llm.optimize(model, dtype=dtype)

# generation inference loop
with torch.inference_mode():
    model.generate()
```

### Intel® Extension for Transformers - INT4 Inference (CPU)
```python
from transformers import AutoTokenizer
from intel_extension_for_transformers.transformers import AutoModelForCausalLM
model_name = "Intel/neural-chat-7b-v3-1"     
prompt = "When winter becomes spring, the flowers..."

tokenizer = AutoTokenizer.from_pretrained(model_name, trust_remote_code=True)
inputs = tokenizer(prompt, return_tensors="pt").input_ids

model = AutoModelForCausalLM.from_pretrained(model_name, load_in_4bit=True)
outputs = model.generate(inputs)


```

<hr>

# Intel® Core Ultra (NPUs and iGPUs)
Intel® Core™ Ultra Processors are optimized for premium thin and powerful laptops, featuring 3D performance hybrid architecture, advanced AI capabilities, and available with built-in Intel® Arc™ GPU. Learn more about Intel Core Ultra [here](https://www.intel.com/content/www/us/en/products/details/processors/core-ultra.html). For now, there is support for smaller models like [TinyLama-1.1B](https://huggingface.co/TinyLlama/TinyLlama-1.1B-Chat-v1.0).

### Intel® NPU Acceleration Library
The Intel® NPU Acceleration Library is a Python library designed to boost the efficiency of your applications by leveraging the power of the Intel Neural Processing Unit (NPU) to perform high-speed computations on compatible hardware.

👍 [Intel NPU Acceleration Library GitHub](https://github.com/intel/intel-npu-acceleration-library)

```python
from transformers import AutoTokenizer, TextStreamer, AutoModelForCausalLM
import intel_npu_acceleration_library
import torch

model_id = "TinyLlama/TinyLlama-1.1B-Chat-v1.0"

model = AutoModelForCausalLM.from_pretrained(model_id, use_cache=True).eval()
tokenizer = AutoTokenizer.from_pretrained(model_id, use_default_system_prompt=True)
tokenizer.pad_token_id = tokenizer.eos_token_id
streamer = TextStreamer(tokenizer, skip_special_tokens=True)

print("Compile model for the NPU")
model = intel_npu_acceleration_library.compile(model, dtype=torch.int8)

query = input("Ask something: ")
prefix = tokenizer(query, return_tensors="pt")["input_ids"]

generation_kwargs = dict(
   input_ids=prefix,
   streamer=streamer,
   do_sample=True,
   top_k=50,
   top_p=0.9,
   max_new_tokens=512,
)

print("Run inference")
_ = model.generate(**generation_kwargs)
```

### OpenVINO Tooling with Optimum Intel
OpenVINO™ is an open-source toolkit for optimizing and deploying AI inference.

👍 [OpenVINO GitHub](https://github.com/openvinotoolkit/openvino)

```python
from optimum.intel import OVModelForCausalLM
from transformers import AutoTokenizer, pipeline

model_id = "helenai/gpt2-ov"
model = OVModelForCausalLM.from_pretrained(model_id)
tokenizer = AutoTokenizer.from_pretrained(model_id)

pipe = pipeline("text-generation", model=model, tokenizer=tokenizer)

pipe("In the spring, beautiful flowers bloom...")

```

<hr>

# Intel® Arc GPUs
You can learn more about Arc GPUs [here](https://www.intel.com/content/www/us/en/products/details/discrete-gpus/arc.html).

Code snippets coming soon!

"""