Skip to content

Commit 71ca800

Browse files
committed
Add Halide Conv Layer Benchmark.
1 parent 3a0b816 commit 71ca800

9 files changed

+647
-0
lines changed

CMakeLists.txt

+8
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,14 @@ if(DEFINED IMAGE_PROCESSING_BENCHMARKS OR DEEP_LEARNING_BENCHMARKS OR OP_OPTIMIZ
8989
include_directories(${OpenCV_INCLUDE_DIRS})
9090
endif()
9191

92+
#-------------------------------------------------------------------------------
93+
# Find Halide
94+
#-------------------------------------------------------------------------------
95+
96+
if(DEFINED DEEP_LEARNING_BENCHMARKS)
97+
find_package(Halide REQUIRED)
98+
endif()
99+
92100
#-------------------------------------------------------------------------------
93101
# Find PNG
94102
#-------------------------------------------------------------------------------

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ $ cd bin && ./image-processing-benchmark <image path> <kernel name> <kernelmorph
6565

6666
## Deep Learning Benchmark
6767

68+
Currently, the deep learning benchmark includes the following frameworks or optimizers:
69+
70+
- Halide ([link](https://github.com/halide/Halide/blob/main/README_cmake.md#a-basic-cmake-project))
71+
72+
*NOTE: Please build Halide from source to achieve the best performance.*
73+
6874
| CMake Options | Default Value |
6975
| -------------- | ------------- |
7076
| `-DBUDDY_OPT_ATTR` | avx512f |
@@ -78,6 +84,7 @@ $ mkdir build && cd build
7884
$ cmake -G Ninja .. \
7985
-DDEEP_LEARNING_BENCHMARKS=ON \
8086
-DOpenCV_DIR=/PATH/TO/OPENCV/BUILD/ \
87+
-DCMAKE_PREFIX_PATH=/PATH/TO/Halide-install/ \
8188
-DBUDDY_MLIR_BUILD_DIR=/PATH/TO/BUDDY-MLIR/BUILD/
8289
$ ninja
8390
```
+1
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
add_subdirectory(Layers)
12
add_subdirectory(Models)
23
add_subdirectory(Ops)
34

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#-------------------------------------------------------------------------------
2+
# Generate Non-Schedule Version Conv Layer Static Library
3+
#-------------------------------------------------------------------------------
4+
5+
add_executable(conv_layer_generator conv_layer_generator.cpp)
6+
target_link_libraries(conv_layer_generator
7+
PRIVATE
8+
Halide::Generator)
9+
add_halide_library(conv_layer_nonschedule FROM conv_layer_generator)
10+
11+
#-------------------------------------------------------------------------------
12+
# Generate Auto-Schedule Version Conv Layer Static Library
13+
#-------------------------------------------------------------------------------
14+
15+
add_executable(conv_layer_autoschedule_generator conv_layer_generator-autoschedule.cpp)
16+
target_link_libraries(conv_layer_autoschedule_generator
17+
PRIVATE
18+
Halide::Generator)
19+
add_halide_library(conv_layer_autoschedule FROM conv_layer_autoschedule_generator
20+
AUTOSCHEDULER Halide::Mullapudi2016)
21+
22+
#-------------------------------------------------------------------------------
23+
# Generate Manually-Schedule Version Conv Layer Static Library
24+
#-------------------------------------------------------------------------------
25+
26+
add_executable(conv_layer_manually_generator conv_layer_generator-manually.cpp)
27+
target_link_libraries(conv_layer_manually_generator
28+
PRIVATE
29+
Halide::Generator)
30+
add_halide_library(conv_layer_manuallyschedule FROM conv_layer_manually_generator)
31+
32+
#-------------------------------------------------------------------------------
33+
# Halide ConvLayer Benchmark Target
34+
#-------------------------------------------------------------------------------
35+
36+
add_executable(halide-convlayer-benchmark
37+
Main.cpp
38+
HalideConvLayerBenchmark.cpp)
39+
40+
target_link_libraries(halide-convlayer-benchmark
41+
GoogleBenchmark
42+
Halide::ImageIO
43+
conv_layer_nonschedule
44+
conv_layer_manuallyschedule
45+
conv_layer_autoschedule)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
#include <chrono>
2+
#include <cstdio>
3+
4+
#include "conv_layer_nonschedule.h"
5+
#include "conv_layer_manuallyschedule.h"
6+
#include "conv_layer_autoschedule.h"
7+
#include <benchmark/benchmark.h>
8+
#include "HalideBuffer.h"
9+
10+
using namespace Halide::Runtime;
11+
12+
const int N = 5, CI = 128, CO = 128, W = 100, H = 80;
13+
14+
Buffer<float, 4> input(CI, W + 2, H + 2, N), input1(CI, W + 2, H + 2, N), input2(CI, W + 2, H + 2, N);
15+
Buffer<float, 4> filter(CO, 3, 3, CI), filter1(CO, 3, 3, CI), filter2(CO, 3, 3, CI);
16+
Buffer<float, 1> bias(CO), bias1(CO), bias2(CO);
17+
Buffer<float, 4> output(CO, W, H, N), output1(CO, W, H, N), output2(CO, W, H, N);
18+
19+
void initializeHalideConvLayerBenchmark(char **argv) {
20+
for (int c = 0; c < input.dim(3).extent(); c++) {
21+
for (int z = 0; z < input.channels(); z++) {
22+
for (int y = 0; y < input.height(); y++) {
23+
for (int x = 0; x < input.width(); x++) {
24+
input(x, y, z, c) = rand();
25+
input1(x, y, z, c) = input(x, y, z, c);
26+
input2(x, y, z, c) = input(x, y, z, c);
27+
}
28+
}
29+
}
30+
}
31+
32+
for (int c = 0; c < filter.dim(3).extent(); c++) {
33+
for (int z = 0; z < filter.channels(); z++) {
34+
for (int y = 0; y < filter.height(); y++) {
35+
for (int x = 0; x < filter.width(); x++) {
36+
filter(x, y, z, c) = rand();
37+
filter1(x, y, z, c) = filter(x, y, z, c);
38+
filter2(x, y, z, c) = filter(x, y, z, c);
39+
}
40+
}
41+
}
42+
}
43+
44+
for (int x = 0; x < bias.width(); x++) {
45+
bias(x) = rand();
46+
bias1(x) = bias(x);
47+
bias2(x) = bias(x);
48+
}
49+
50+
#ifdef _WIN32
51+
_putenv_s("HL_CUDA_JIT_MAX_REGISTERS", "256");
52+
#else
53+
setenv("HL_CUDA_JIT_MAX_REGISTERS", "256", 1);
54+
#endif
55+
}
56+
57+
static void Halide_ConvLayer_NonSchedule(benchmark::State &state) {
58+
for (auto _ : state) {
59+
for (int i = 0; i < state.range(0); ++i) {
60+
conv_layer_nonschedule(input, filter, bias, output);
61+
}
62+
}
63+
}
64+
65+
static void Halide_ConvLayer_MaunallySchedule(benchmark::State &state) {
66+
for (auto _ : state) {
67+
for (int i = 0; i < state.range(0); ++i) {
68+
conv_layer_manuallyschedule(input1, filter1, bias1, output1);
69+
}
70+
}
71+
}
72+
73+
static void Halide_ConvLayer_AutoSchedule(benchmark::State &state) {
74+
for (auto _ : state) {
75+
for (int i = 0; i < state.range(0); ++i) {
76+
conv_layer_autoschedule(input2, filter2, bias2, output2);
77+
}
78+
}
79+
}
80+
81+
// Register benchmarking function.
82+
void registerBenchmarkHalideConvLayer() {
83+
BENCHMARK(Halide_ConvLayer_NonSchedule)->Arg(1)->Unit(benchmark::kMillisecond);
84+
BENCHMARK(Halide_ConvLayer_MaunallySchedule)->Arg(1)->Unit(benchmark::kMillisecond);
85+
BENCHMARK(Halide_ConvLayer_AutoSchedule)->Arg(1)->Unit(benchmark::kMillisecond);
86+
}
87+
+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//===- Main.cpp -----------------------------------------------------------===//
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
//
15+
//===----------------------------------------------------------------------===//
16+
//
17+
// This is the main file of the Halide Conv Layer benchmark.
18+
//
19+
//===----------------------------------------------------------------------===//
20+
21+
#include <benchmark/benchmark.h>
22+
#include <stdexcept>
23+
24+
void initializeHalideConvLayerBenchmark(char **);
25+
26+
void registerBenchmarkHalideConvLayer();
27+
28+
// Run benchmarks.
29+
int main(int argc, char **argv) {
30+
if (argc != 1) {
31+
throw std::invalid_argument(
32+
"No arguments needed.\n");
33+
}
34+
35+
initializeHalideConvLayerBenchmark(argv);
36+
37+
// Register Benchmark Function.
38+
registerBenchmarkHalideConvLayer();
39+
40+
::benchmark::Initialize(&argc, argv);
41+
::benchmark::RunSpecifiedBenchmarks();
42+
43+
// Generate result.
44+
45+
return 0;
46+
}

0 commit comments

Comments
 (0)