Skip to content

Commit bcc89ae

Browse files
authored
Merge pull request buddy-compiler#2 from axmat/onnxruntime
[examples][conv-opt] Add conv2d benchmark for ONNX Runtime
2 parents 41b7fdd + 760be85 commit bcc89ae

4 files changed

Lines changed: 134 additions & 1 deletion

File tree

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,7 @@
22
/build*
33

44
# vscode configurations
5-
/.vscode
5+
/.vscode
6+
7+
# ONNX models
8+
*.onnx

examples/conv-opt/comparison/README.md

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,14 @@ $ # n kernel size, default 3, alternative options: 3, 5, 7, 9
4343
$ # target, default 'llvm -mcpu=skylake-avx512', alternative options: 'cuda'
4444
```
4545

46+
- ONNX Runtime
47+
48+
```
49+
$ cd buddy-mlir/examples/conv-opt/comparison/
50+
$ python3 gen-conv-models.py
51+
$ python3 onnxruntime-conv2d.py
52+
```
53+
4654
## Difference and Performance Comparison
4755

4856
We proposed a coefficients broadcasting algorithm with strip mining strategy (CB-SM) to accelerate 2D convolutions,
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
import onnx
2+
from onnx import numpy_helper
3+
import numpy as np
4+
5+
# Filter
6+
sobel = {
7+
3: np.array([[1, 0, -1],
8+
[2, 0, -2],
9+
[1, 0, -1]], dtype='float32'),
10+
5: np.array([[2, 1, 0, -1, -2],
11+
[3, 2, 0, -2, -3],
12+
[4, 3, 0, -3, -4],
13+
[3, 2, 0, -2, -3],
14+
[2, 1, 0, -1, -2]], dtype='float32'),
15+
7: np.array([[3, 2, 1, 0, -1, -2, -3],
16+
[4, 3, 2, 0, -2, -3, -4],
17+
[5, 4, 3, 0, -3, -4, -5],
18+
[6, 5, 4, 0, -4, -5, -6],
19+
[5, 4, 3, 0, -3, -4, -5],
20+
[4, 3, 2, 0, -2, -3, -4],
21+
[3, 2, 1, 0, -1, -2, -3]], dtype='float32'),
22+
9: np.array([[4, 3, 2, 1, 0, -1, -2, -3, -4],
23+
[5, 4, 3, 2, 0, -2, -3, -4, -5],
24+
[6, 5, 4, 3, 0, -3, -4, -5, -6],
25+
[7, 6, 5, 4, 0, -4, -5, -6, -7],
26+
[8, 7, 6, 5, 0, -5, -6, -7, -8],
27+
[7, 6, 5, 4, 0, -4, -5, -6, -7],
28+
[6, 5, 4, 3, 0, -3, -4, -5, -6],
29+
[5, 4, 3, 2, 0, -2, -3, -4, -5],
30+
[4, 3, 2, 1, 0, -1, -2, -3, -4]], dtype='float32')
31+
}
32+
33+
def get_output_shape(i):
34+
if i == 3:
35+
return [1, 1, 2046, 2046]
36+
elif i == 5:
37+
return [1, 1, 2044, 2044]
38+
elif i == 7:
39+
return [1, 1, 2042, 2042]
40+
elif i == 9:
41+
return [1, 1, 2040, 2040]
42+
43+
def main():
44+
for i in range(3, 10, 2):
45+
# Filter
46+
w = sobel[i].reshape((1, 1, i, i))
47+
48+
# Input
49+
x = np.random.rand(1, 1, 2048, 2048).astype('float32')
50+
51+
# Initializer of the weight
52+
initializer_w = numpy_helper.from_array(w, 'w')
53+
54+
tensor_w = onnx.helper.make_tensor_value_info('w', onnx.TensorProto.FLOAT, [1, 1, i, i])
55+
tensor_x = onnx.helper.make_tensor_value_info('x', onnx.TensorProto.FLOAT, [1, 1, 2048, 2048])
56+
tensor_y = onnx.helper.make_tensor_value_info('y', onnx.TensorProto.FLOAT, get_output_shape(i))
57+
58+
# Create a node
59+
node_def = onnx.helper.make_node(
60+
'Conv',
61+
inputs=['x', 'w'],
62+
outputs=['y'],
63+
kernel_shape=[i, i]
64+
)
65+
66+
# Create the graph
67+
graph_def = onnx.helper.make_graph(
68+
[node_def],
69+
f'conv_{i}x{i}',
70+
[tensor_x],
71+
[tensor_y],
72+
[initializer_w]
73+
)
74+
75+
# Create the model
76+
model_def = onnx.helper.make_model(graph_def,
77+
producer_name='python_script',
78+
ir_version=6
79+
)
80+
model_def.opset_import[0].version = 10
81+
82+
# Check the model
83+
onnx.checker.check_model(model_def)
84+
85+
# Save the model
86+
onnx.save(model_def, f'conv_{i}x{i}.onnx')
87+
88+
if __name__ == "__main__":
89+
main()
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
import numpy as np
2+
import cv2
3+
import onnxruntime
4+
import time
5+
6+
def test_conv2d(img, filter_size):
7+
start = time.time()
8+
# Load the model
9+
model_path = f'conv_{filter_size}x{filter_size}.onnx'
10+
ort_session = onnxruntime.InferenceSession(model_path)
11+
# Run inference
12+
ort_inputs = {ort_session.get_inputs()[0].name: img}
13+
ort_outs = ort_session.run(None, ort_inputs)
14+
edge_detect = ort_outs[0]
15+
edge_detect = edge_detect.squeeze()
16+
end = time.time()
17+
print(f'conv {filter_size}x{filter_size} : {end - start}')
18+
return edge_detect
19+
20+
def main():
21+
img = cv2.imread('../images/YuTu2048.png',cv2.IMREAD_GRAYSCALE)
22+
# Convert the image to numpy array.
23+
img = np.array(img, dtype='float32')
24+
img = img.reshape((1, 1, img.shape[0], img.shape[1]))
25+
'''
26+
Perform the edget detection.
27+
'''
28+
for i in range(3, 10, 2):
29+
edge_detect = test_conv2d(img, i)
30+
cv2.imwrite(f'./onnxruntime-conv2d_{i}.png', edge_detect)
31+
32+
if __name__ == "__main__":
33+
main()

0 commit comments

Comments
 (0)