Skip to content

Commit b371bfc

Browse files
authored
update examples and examples doc (#1460)
1 parent 7d69b26 commit b371bfc

19 files changed

+82
-426
lines changed

docs/tutorials/examples.md

Lines changed: 2 additions & 392 deletions
Large diffs are not rendered by default.
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
cmake_minimum_required(VERSION 3.0 FATAL_ERROR)
2+
project(example-app)
3+
4+
find_package(IPEX REQUIRED)
5+
6+
add_executable(example-app example-app.cpp)
7+
target_link_libraries(example-app "${TORCH_LIBRARIES}")
8+
9+
set_property(TARGET example-app PROPERTY CXX_STANDARD 14)
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
#include <torch/script.h>
2+
#include <iostream>
3+
#include <memory>
4+
5+
int main(int argc, const char* argv[]) {
6+
torch::jit::script::Module module;
7+
try {
8+
module = torch::jit::load(argv[1]);
9+
}
10+
catch (const c10::Error& e) {
11+
std::cerr << "error loading the model\n";
12+
return -1;
13+
}
14+
15+
std::vector<torch::jit::IValue> inputs;
16+
torch::Tensor input = torch::rand({1,3,224,224});
17+
inputs.push_back(input);
18+
19+
at::Tensor output = module.forward(inputs).toTensor();
20+
std::cout << output_tensor.slice(/*dim=*/1, /*start=*/0, /*end=*/5) << std::endl;
21+
22+
return 0;
23+
}
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
#!/usr/bin/env python
2+
# encoding: utf-8
3+
4+
import torch
5+
import torchvision
6+
7+
model = torchvision.models.resnet50(pretrained=True)
8+
model.eval()
9+
10+
input = torch.rand(1, 3, 224, 224)
11+
model = torch.jit.trace(model, input, check_trace=False)
12+
13+
model.save('resnet50.pt')
14+
print("save mode to: resnet50.pt")

examples/cpu/inference/BERT_general_inference_script.py renamed to examples/cpu/inference/python/bert_general_inference_script.py

File renamed without changes.

examples/cpu/inference/BERT_imperative_mode_inference_bf16.py renamed to examples/cpu/inference/python/bert_imperative_mode_inference_bf16.py

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,5 @@
1414
model = ipex.optimize(model, dtype=torch.bfloat16)
1515
######################################################
1616

17-
with torch.no_grad():
18-
with torch.cpu.amp.autocast():
19-
model(data)
20-
17+
with torch.no_grad(), torch.cpu.amp.autocast():
18+
model(data)

examples/cpu/inference/BERT_imperative_mode_inference_fp32.py renamed to examples/cpu/inference/python/bert_imperative_mode_inference_fp32.py

File renamed without changes.

examples/cpu/inference/BERT_torchdynamo_mode_inference_fp32.py renamed to examples/cpu/inference/python/bert_torchdynamo_mode_inference_fp32.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,4 +16,4 @@
1616
######################################################
1717

1818
with torch.no_grad():
19-
model(data)
19+
model(data)

examples/cpu/inference/BERT_torchscript_mode_inference_bf16.py renamed to examples/cpu/inference/python/bert_torchscript_mode_inference_bf16.py

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@
1414
model = ipex.optimize(model, dtype=torch.bfloat16)
1515
######################################################
1616

17-
with torch.no_grad():
18-
with torch.cpu.amp.autocast():
19-
d = torch.randint(vocab_size, size=[batch_size, seq_length])
20-
model = torch.jit.trace(model, (d,), check_trace=False, strict=False)
21-
model = torch.jit.freeze(model)
17+
with torch.no_grad(), torch.cpu.amp.autocast():
18+
d = torch.randint(vocab_size, size=[batch_size, seq_length])
19+
model = torch.jit.trace(model, (d,), check_trace=False, strict=False)
20+
model = torch.jit.freeze(model)
2221

23-
model(data)
22+
model(data)

examples/cpu/inference/BERT_torchscript_mode_inference_fp32.py renamed to examples/cpu/inference/python/bert_torchscript_mode_inference_fp32.py

File renamed without changes.

0 commit comments

Comments
 (0)