Views
No views yet
nn.BatchNorm2d and nn.Conv2d are fusednn.BatchNorm1d and nn.Linear are fusedFuseBatchNorm2dInConv2d and FuseBatchNorm1dInLinear soon to be available to use out-of-the-box with 🤗 Optimum, check it out: https://huggingface.co/docs/optimum/main/en/fx/optimization#the-transformation-guide .1from optimum.onnxruntime.modeling_ort import ORTModelForImageClassification
2from transformers import AutoFeatureExtractor
3
4from PIL import Image
5import requests
6
7preprocessor = AutoFeatureExtractor.from_pretrained("fxmarty/levit-256-onnx")
8ort_model = ORTModelForImageClassification.from_pretrained("fxmarty/levit-256-onnx")
9
10url = 'http://images.cocodataset.org/val2017/000000039769.jpg'
11image = Image.open(requests.get(url, stream=True).raw)
12
13inputs = preprocessor(images=image, return_tensors="pt")
14outputs = model(**inputs)
15
16predicted_class_idx = outputs.logits.argmax(-1).item()
17print("Predicted class:", model.config.id2label[predicted_class_idx])1from optimum.onnxruntime.modeling_ort import ORTModelForImageClassification
2from transformers import AutoModelForImageClassification
3
4pt_model = AutoModelForImageClassification.from_pretrained("facebook/levit-256")
5pt_model.eval()
6
7ort_model = ORTModelForImageClassification.from_pretrained("fxmarty/levit-256-onnx")
8
9inp = {"pixel_values": torch.rand(1, 3, 224, 224)}
10
11with torch.no_grad():
12 res = pt_model(**inp)
13res_ort = ort_model(**inp)
14
15assert torch.allclose(res.logits, res_ort.logits, atol=1e-4)PyTorch runtime:
{'latency_50': 22.3024695,
'latency_90': 23.1230725,
'latency_95': 23.2653985,
'latency_99': 23.60095705,
'latency_999': 23.865580469999998,
'latency_mean': 22.442956878923766,
'latency_std': 0.46544295612971265,
'nb_forwards': 446,
'throughput': 44.6}
Optimum-onnxruntime runtime:
{'latency_50': 9.302445,
'latency_90': 9.782875,
'latency_95': 9.9071944,
'latency_99': 11.084606999999997,
'latency_999': 12.035858692000001,
'latency_mean': 9.357703552853133,
'latency_std': 0.4018553286992142,
'nb_forwards': 1069,
'throughput': 106.9}
1from optimum.runs_base import TimeBenchmark
2
3from pprint import pprint
4
5time_benchmark_ort = TimeBenchmark(
6 model=ort_model,
7 batch_size=1,
8 input_length=224,
9 model_input_names={"pixel_values"},
10 warmup_runs=10,
11 duration=10
12)
13
14results_ort = time_benchmark_ort.execute()
15
16with torch.no_grad():
17 time_benchmark_pt = TimeBenchmark(
18 model=pt_model,
19 batch_size=1,
20 input_length=224,
21 model_input_names={"pixel_values"},
22 warmup_runs=10,
23 duration=10
24 )
25
26 results_pt = time_benchmark_pt.execute()
27
28print("PyTorch runtime:\n")
29pprint(results_pt)
30
31print("\nOptimum-onnxruntime runtime:\n")
32pprint(results_ort)