Skip to content

Commit 961ce54

Browse files
committed
Add benchmarks
1 parent 20be055 commit 961ce54

25 files changed

+482
-0
lines changed

CMakeLists.txt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,7 @@ include(HandleLLVMOptions)
160160

161161
set(IMEX_ENABLE_SYCL_RUNTIME 0 CACHE BOOL "Enable the Sycl Runtime")
162162
set(IMEX_ENABLE_L0_RUNTIME 0 CACHE BOOL "Enable the Level Zero Runtime")
163+
set(IMEX_ENABLE_BENCHMARK 0 CACHE BOOL "Enable the IMEX Benchmark (Depending on SYCL Runtime)")
163164

164165
# Normalize IMEX_ENABLE_SYCL_RUNTIME and IMEX_ENABLE_L0_RUNTIME
165166
# These values are expected to be passed as 0 or 1 to imex-runner.py
@@ -175,6 +176,13 @@ else ()
175176
set(IMEX_ENABLE_L0_RUNTIME 0)
176177
endif()
177178

179+
if (IMEX_ENABLE_BENCHMARK)
180+
set(IMEX_ENABLE_BENCHMARK 1)
181+
else ()
182+
set(IMEX_ENABLE_BENCHMARK 0)
183+
endif()
184+
185+
178186
# Check if mlir vulkan runner was configured from mlir
179187
# target mlir-vulkan-runner is only set if vulkan runner was enabled during
180188
# mlir configuration
@@ -217,6 +225,10 @@ add_subdirectory(lib)
217225
add_subdirectory(tools)
218226
add_subdirectory(test)
219227

228+
if((IMEX_ENABLE_BENCHMARK) AND (IMEX_ENABLE_SYCL_RUNTIME OR IMEX_ENABLE_L0_RUNTIME))
229+
add_subdirectory(benchmarks)
230+
endif()
231+
220232
option(IMEX_INCLUDE_DOCS "Generate build targets for the IMEX docs." ON)
221233
if (IMEX_INCLUDE_DOCS)
222234
add_subdirectory(docs)

benchmarks/CMakeLists.txt

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
add_subdirectory(relu)
2+
add_subdirectory(silu)
3+
add_subdirectory(softmax)
4+
add_subdirectory(transpose)
5+
6+
if(WIN32)
7+
set(MLIR_RUNNER_UTILS_DIR ${LLVM_BINARY_DIR}/bin)
8+
else()
9+
set(MLIR_RUNNER_UTILS_DIR ${LLVM_LIBRARY_DIR})
10+
endif()
11+
12+
configure_file(bench_imex.in ${IMEX_BINARY_DIR}/benchmarks/bench_imex @ONLY)
13+
file(COPY pipelines/linalg-to-gpu.pp DESTINATION ${IMEX_BINARY_DIR}/benchmarks/pipelines)

benchmarks/bench_imex.in

Lines changed: 83 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,83 @@
1+
#!/bin/env bash
2+
export IMEX_ENABLE_PROFILING=1
3+
4+
5+
platform="$(uname -s)"
6+
case "${platform}" in
7+
Linux*) shlib_prefix="lib";;
8+
Darwin*) shlib_prefix="lib";;
9+
CYGWIN*) shlib_prefix="";;
10+
MINGW*) shlib_prefix="";;
11+
*) echo "UNKNOWN platform:${platform}" && exit -1
12+
esac
13+
14+
MLIR_RUNNER_UTILS=/home/cchen/.local/lib/libmlir_runner_utils.so
15+
MLIR_C_RUNNER_UTILS=/home/cchen/.local/lib/libmlir_c_runner_utils.so
16+
IMEX_SYCL_RUNTIME=/home/cchen/imex/frameworks.ai.mlir.mlir-extensions/build/lib/libsycl-runtime.so
17+
IMEX_L0_RUNTIME=/home/cchen/imex/frameworks.ai.mlir.mlir-extensions/build/lib/liblevel-zero-runtime.so
18+
BENCHMARK_ROOT=/home/cchen/imex/frameworks.ai.mlir.mlir-extensions/build/benchmarks
19+
IMEX_RUNNER=/home/cchen/imex/frameworks.ai.mlir.mlir-extensions/build/bin/imex-runner.py
20+
21+
# -l: using level-zero runtime
22+
# -s: using sycl runtime
23+
while getopts ':lsh' opt; do
24+
case "$opt" in
25+
l)
26+
echo "Running with level-zero runtime"
27+
RUNTIME="${IMEX_L0_RUNTIME}"
28+
RUNTIMENAME="L0"
29+
;;
30+
31+
s)
32+
echo "Running with sycl runtime"
33+
RUNTIME="${IMEX_SYCL_RUNTIME}"
34+
RUNTIMENAME="SYCL"
35+
;;
36+
?|h)
37+
echo "Usage: $(basename $0) [-l] [-s] arg"
38+
echo " -s: using sycl runtime"
39+
echo " -l: using level-zero runtime"
40+
echo " arg: path to a folder containing .mlir files or path to an mlir file"
41+
exit 1
42+
;;
43+
esac
44+
done
45+
shift "$(($OPTIND -1))"
46+
47+
[ -z "$RUNTIME" ] && echo "Please select a runtime. using '$(basename $0) -h' for more usage info" && exit 1
48+
49+
if [ "$#" -eq 0 ]; then
50+
TESTS=`find ${BENCHMARK_ROOT} -type f -name '*.mlir' | sort -n`
51+
elif [ "$#" -eq 1 ] && [ -d "$1" ]; then
52+
TESTS=`find ${BENCHMARK_ROOT}/$1 -type f -name '*.mlir' | sort -n`
53+
elif [ "$#" -eq 1 ] && [ -f "$1" ]; then
54+
FILE=$1
55+
EXT=${FILE#*.}
56+
if [[ "$EXT" -ne "mlir" ]]; then
57+
echo "Only take .mlir file" && exit -1
58+
fi
59+
TESTS=$1
60+
fi
61+
62+
echo "Run the following Tests:"
63+
echo -e "${TESTS}\n"
64+
65+
# clean up old results/reports first
66+
rm report.txt
67+
echo -e "\n================ Imex Perf ($RUNTIMENAME) @ $(date) ================\n" >> report.txt
68+
69+
for i in $TESTS; do
70+
test_name=$(basename -- "$i")
71+
echo -n "${test_name}: " >&2
72+
output=$(@Python3_EXECUTABLE@ $IMEX_RUNNER \
73+
--pass-pipeline-file=$BENCHMARK_ROOT/pipelines/linalg-to-gpu.pp \
74+
--runner mlir-cpu-runner -e main \
75+
--shared-libs=$MLIR_RUNNER_UTILS,$MLIR_C_RUNNER_UTILS,$RUNTIME\
76+
--entry-point-result=void -i $i)
77+
echo $output
78+
while IFS= read -r line; do
79+
if [[ $line == *"the kernel execution time"* ]]; then
80+
echo "${test_name}: $line"
81+
fi
82+
done <<< $output >> report.txt
83+
done

benchmarks/pipelines/linalg-to-gpu.pp

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
// linalg dialect to gpu dialect lowering pipeline
2+
// Ready for vulkan runner or narrow scope l0/sycl runner starting from GPU dialect.
3+
builtin.module(convert-tensor-to-linalg
4+
arith-bufferize
5+
func.func(empty-tensor-to-alloc-tensor
6+
eliminate-empty-tensors
7+
scf-bufferize
8+
shape-bufferize
9+
linalg-bufferize
10+
bufferization-bufferize
11+
tensor-bufferize)
12+
func-bufferize
13+
func.func(finalizing-bufferize
14+
convert-linalg-to-parallel-loops
15+
imex-add-outer-parallel-loop
16+
gpu-map-parallel-loops
17+
convert-parallel-loops-to-gpu)
18+
// insert-gpu-allocs pass can have client-api = opencl or vulkan args
19+
func.func(insert-gpu-allocs{client-api=opencl})
20+
canonicalize
21+
normalize-memrefs
22+
// Unstride memrefs does not seem to be needed.
23+
// func.func(unstride-memrefs)
24+
func.func(lower-affine)
25+
gpu-kernel-outlining
26+
canonicalize
27+
cse
28+
// The following set-spirv-* passes can have client-api = opencl or vulkan args
29+
set-spirv-capabilities{client-api=opencl}
30+
gpu.module(set-spirv-abi-attrs{client-api=opencl})
31+
canonicalize
32+
fold-memref-alias-ops
33+
imex-convert-gpu-to-spirv
34+
spirv.module(spirv-lower-abi-attrs
35+
spirv-update-vce)
36+
func.func(llvm-request-c-wrappers)
37+
serialize-spirv
38+
convert-gpu-to-gpux
39+
convert-func-to-llvm
40+
convert-math-to-llvm
41+
convert-gpux-to-llvm
42+
expand-strided-metadata
43+
lower-affine
44+
convert-memref-to-llvm
45+
reconcile-unrealized-casts)
46+
// End

benchmarks/relu/CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
file(STRINGS relu.shapes.in test_shapes)
3+
file(STRINGS relu.dtypes.in test_dtypes)
4+
5+
foreach(shape ${test_shapes})
6+
unset(mathes)
7+
unset(iterators)
8+
unset(maps)
9+
unset(id)
10+
11+
set(id, 0)
12+
string(REGEX MATCHALL x dims "${shape}" )
13+
list(APPEND iterators "\"parallel\"")
14+
list(APPEND maps "d0")
15+
16+
# prepare the map and iterator
17+
foreach(i ${dims})
18+
math(EXPR id "${id}+1")
19+
list(APPEND maps "d${id}")
20+
list(APPEND iterators "\"parallel\"")
21+
endforeach()
22+
list(JOIN maps ", " affine_map)
23+
list(JOIN iterators ", " iterator_types)
24+
25+
foreach(dtype ${test_dtypes})
26+
configure_file(relu.mlir.in ${IMEX_BINARY_DIR}/benchmarks/relu/relu_${shape}_${dtype}.mlir @ONLY)
27+
endforeach()
28+
endforeach()

benchmarks/relu/onednn/test_relu

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
--reset
2+
--alg=RELU
3+
--dir=FWD_I
4+
--engine=gpu
5+
--mode=PO # performance mode
6+
--tag=axb
7+
--perf-template=%alg%,%desc%,%engine%,%dt%,%0time%
8+
9+
# fp16
10+
--dt=f16
11+
1x160x160x120 50x640x20x15 512x640x20x15
12+
13+
# fp32
14+
--dt=f32
15+
1x160x160x120 50x640x20x15 512x640x20x15

benchmarks/relu/relu.dtypes.in

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
f16
2+
f32

benchmarks/relu/relu.mlir.in

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
#map = affine_map<(@affine_map@) -> (@affine_map@)>
2+
module attributes {torch.debug_module_name = "ReLU"} {
3+
func.func @forward(%arg0: tensor<@shape@x@dtype@>) -> tensor<@shape@x@dtype@> {
4+
%cst = arith.constant 0.000000e+00 : @dtype@
5+
%0 = tensor.empty() : tensor<@shape@x@dtype@>
6+
%1 = linalg.generic {indexing_maps = [#map, #map], iterator_types = [@iterator_types@]} ins(%arg0 : tensor<@shape@x@dtype@>) outs(%0 : tensor<@shape@x@dtype@>) {
7+
^bb0(%in: @dtype@, %out: @dtype@):
8+
%2 = arith.cmpf ugt, %in, %cst : @dtype@
9+
%3 = arith.select %2, %in, %cst : @dtype@
10+
linalg.yield %3 : @dtype@
11+
} -> tensor<@shape@x@dtype@>
12+
return %1 : tensor<@shape@x@dtype@>
13+
}
14+
func.func @main() {
15+
%0= arith.constant dense<1.3>:tensor<@shape@x@dtype@>
16+
%1 = call @forward(%0) : (tensor<@shape@x@dtype@>) -> tensor<@shape@x@dtype@>
17+
return
18+
}
19+
}

benchmarks/relu/relu.shapes.in

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
1x160x160x120
2+
50x640x20x15
3+
512x640x20x15

benchmarks/silu/CMakeLists.txt

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
2+
file(STRINGS silu.shapes.in test_shapes)
3+
file(STRINGS silu.dtypes.in test_dtypes)
4+
5+
foreach(shape ${test_shapes})
6+
unset(mathes)
7+
unset(iterators)
8+
unset(maps)
9+
unset(id)
10+
11+
set(id, 0)
12+
string(REGEX MATCHALL x dims "${shape}" )
13+
list(APPEND iterators "\"parallel\"")
14+
list(APPEND maps "d0")
15+
16+
# prepare the map and iterator
17+
foreach(i ${dims})
18+
math(EXPR id "${id}+1")
19+
list(APPEND maps "d${id}")
20+
list(APPEND iterators "\"parallel\"")
21+
endforeach()
22+
list(JOIN maps ", " affine_map)
23+
list(JOIN iterators ", " iterator_types)
24+
25+
foreach(dtype ${test_dtypes})
26+
configure_file(silu.mlir.in ${IMEX_BINARY_DIR}/benchmarks/silu/silu_${shape}_${dtype}.mlir @ONLY)
27+
endforeach()
28+
endforeach()

0 commit comments

Comments
 (0)