|
| 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 | + |
0 commit comments