Skip to content

Commit 642ad12

Browse files
authored
add labels to examples (#1454)
1 parent b1c51da commit 642ad12

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

docs/tutorials/examples.md

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ output = model(data)
3535

3636
#### Complete - Float32
3737

38+
[//]: # (train_single_fp32_complete)
3839
```
3940
import torch
4041
import torchvision
@@ -78,9 +79,11 @@ torch.save({
7879
'optimizer_state_dict': optimizer.state_dict(),
7980
}, 'checkpoint.pth')
8081
```
82+
[//]: # (train_single_fp32_complete)
8183

8284
#### Complete - BFloat16
8385

86+
[//]: # (train_single_bf16_complete)
8487
```
8588
import torch
8689
import torchvision
@@ -125,13 +128,15 @@ torch.save({
125128
'optimizer_state_dict': optimizer.state_dict(),
126129
}, 'checkpoint.pth')
127130
```
131+
[//]: # (train_single_bf16_complete)
128132

129133
### Distributed Training
130134

131135
Distributed training with PyTorch DDP is accelerated by oneAPI Collective Communications Library Bindings for Pytorch\* (oneCCL Bindings for Pytorch\*). The extension supports FP32 and BF16 data types. More detailed information and examples are available at its [Github repo](https://github.com/intel/torch-ccl).
132136

133137
**Note:** When performing distributed training with BF16 data type, use oneCCL Bindings for Pytorch\*. Due to a PyTorch limitation, distributed training with BF16 data type with Intel® Extension for PyTorch\* is not supported.
134138

139+
[//]: # (train_ddp_complete)
135140
```
136141
import os
137142
import torch
@@ -189,6 +194,7 @@ torch.save({
189194
'optimizer_state_dict': optimizer.state_dict(),
190195
}, 'checkpoint.pth')
191196
```
197+
[//]: # (train_ddp_complete)
192198

193199
## Inference
194200

@@ -200,6 +206,7 @@ The `optimize` function of Intel® Extension for PyTorch\* applies optimizations
200206

201207
##### Resnet50
202208

209+
[//]: # (inf_rn50_imp_fp32)
203210
```
204211
import torch
205212
import torchvision.models as models
@@ -216,9 +223,11 @@ model = ipex.optimize(model)
216223
with torch.no_grad():
217224
model(data)
218225
```
226+
[//]: # (inf_rn50_imp_fp32)
219227

220228
##### BERT
221229

230+
[//]: # (inf_bert_imp_fp32)
222231
```
223232
import torch
224233
from transformers import BertModel
@@ -239,13 +248,15 @@ model = ipex.optimize(model)
239248
with torch.no_grad():
240249
model(data)
241250
```
251+
[//]: # (inf_bert_imp_fp32)
242252

243253
#### TorchScript Mode
244254

245255
We recommend you take advantage of Intel® Extension for PyTorch\* with [TorchScript](https://pytorch.org/docs/stable/jit.html) for further optimizations.
246256

247257
##### Resnet50
248258

259+
[//]: # (inf_rn50_ts_fp32)
249260
```
250261
import torch
251262
import torchvision.models as models
@@ -266,9 +277,11 @@ with torch.no_grad():
266277
267278
model(data)
268279
```
280+
[//]: # (inf_rn50_ts_fp32)
269281

270282
##### BERT
271283

284+
[//]: # (inf_bert_ts_fp32)
272285
```
273286
import torch
274287
from transformers import BertModel
@@ -293,11 +306,13 @@ with torch.no_grad():
293306
294307
model(data)
295308
```
309+
[//]: # (inf_bert_ts_fp32)
296310

297311
#### TorchDynamo Mode (Experimental, _NEW feature from 2.0.0_)
298312

299313
##### Resnet50
300314

315+
[//]: # (inf_rn50_dynamo_fp32)
301316
```
302317
import torch
303318
import torchvision.models as models
@@ -315,9 +330,11 @@ model = torch.compile(model, backend="ipex")
315330
with torch.no_grad():
316331
model(data)
317332
```
333+
[//]: # (inf_rn50_dynamo_fp32)
318334

319335
##### BERT
320336

337+
[//]: # (inf_bert_dynamo_fp32)
321338
```
322339
import torch
323340
from transformers import BertModel
@@ -339,6 +356,7 @@ model = torch.compile(model, backend="ipex")
339356
with torch.no_grad():
340357
model(data)
341358
```
359+
[//]: # (inf_bert_dynamo_fp32)
342360

343361
### BFloat16
344362

@@ -349,6 +367,7 @@ We recommend using Auto Mixed Precision (AMP) with BFloat16 data type.
349367

350368
##### Resnet50
351369

370+
[//]: # (inf_rn50_imp_bf16)
352371
```
353372
import torch
354373
import torchvision.models as models
@@ -366,9 +385,11 @@ with torch.no_grad():
366385
with torch.cpu.amp.autocast():
367386
model(data)
368387
```
388+
[//]: # (inf_rn50_imp_bf16)
369389

370390
##### BERT
371391

392+
[//]: # (inf_bert_imp_bf16)
372393
```
373394
import torch
374395
from transformers import BertModel
@@ -390,13 +411,15 @@ with torch.no_grad():
390411
with torch.cpu.amp.autocast():
391412
model(data)
392413
```
414+
[//]: # (inf_bert_imp_bf16)
393415

394416
#### TorchScript Mode
395417

396418
We recommend you take advantage of Intel® Extension for PyTorch\* with [TorchScript](https://pytorch.org/docs/stable/jit.html) for further optimizations.
397419

398420
##### Resnet50
399421

422+
[//]: # (inf_rn50_ts_bf16)
400423
```
401424
import torch
402425
import torchvision.models as models
@@ -417,9 +440,11 @@ with torch.no_grad():
417440
418441
model(data)
419442
```
443+
[//]: # (inf_rn50_ts_bf16)
420444

421445
##### BERT
422446

447+
[//]: # (inf_bert_ts_f16)
423448
```
424449
import torch
425450
from transformers import BertModel
@@ -445,6 +470,7 @@ with torch.no_grad():
445470
446471
model(data)
447472
```
473+
[//]: # (inf_bert_ts_f16)
448474

449475
### INT8
450476

@@ -465,6 +491,7 @@ Please follow the steps below to perform static calibration:
465491
7. Save the INT8 model into a `pt` file.
466492

467493

494+
[//]: # (int8_static)
468495
```
469496
import os
470497
import torch
@@ -494,6 +521,7 @@ with torch.no_grad():
494521
495522
traced_model.save("quantized_model.pt")
496523
```
524+
[//]: # (int8_static)
497525

498526
##### Dynamic Quantization
499527

@@ -507,6 +535,7 @@ Please follow the steps below to perform static calibration:
507535
6. Run inference to perform dynamic quantization.
508536
7. Save the INT8 model into a `pt` file.
509537

538+
[//]: # (int8_dynamic)
510539
```
511540
import os
512541
import torch
@@ -534,6 +563,7 @@ with torch.no_grad():
534563
535564
traced_model.save("quantized_model.pt")
536565
```
566+
[//]: # (int8_dynamic)
537567

538568
#### Deployment
539569

@@ -545,6 +575,7 @@ Follow the steps below:
545575
2. Load the INT8 model from the saved file.
546576
3. Run inference.
547577

578+
[//]: # (int8_deploy)
548579
```
549580
import torch
550581
#################### code changes ####################
@@ -559,6 +590,7 @@ data = torch.rand(<shape>)
559590
with torch.no_grad():
560591
model(data)
561592
```
593+
[//]: # (int8_deploy)
562594

563595
oneDNN provides [oneDNN Graph Compiler](https://github.com/oneapi-src/oneDNN/tree/dev-graph-preview4/doc#onednn-graph-compiler) as a prototype feature that could boost performance for selective topologies. No code change is required. Install <a class="reference external" href="installation.md#installation_onednn_graph_compiler">a binary</a> with this feature enabled. We verified this feature with `Bert-large`, `bert-base-cased`, `roberta-base`, `xlm-roberta-base`, `google-electra-base-generator` and `google-electra-base-discriminator`.
564596

@@ -572,6 +604,7 @@ The example code below works for all data types.
572604

573605
**example-app.cpp**
574606

607+
[//]: # (cppsdk_sample)
575608
```cpp
576609
#include <torch/script.h>
577610
#include <iostream>
@@ -593,9 +626,11 @@ int main(int argc, const char* argv[]) {
593626
return 0;
594627
}
595628
```
629+
[//]: # (cppsdk_sample)
596630
597631
**CMakeLists.txt**
598632
633+
[//]: # (cppsdk_cmake)
599634
```cmake
600635
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
601636
project(example-app)
@@ -607,6 +642,7 @@ target_link_libraries(example-app "${TORCH_LIBRARIES}")
607642
608643
set_property(TARGET example-app PROPERTY CXX_STANDARD 14)
609644
```
645+
[//]: # (cppsdk_cmake)
610646

611647
**Command for compilation**
612648

0 commit comments

Comments
 (0)