matrix_mul.cpp 21.8 KB
Newer Older
M
Megvii Engine Team 已提交
1
#include "test/common/matrix_mul.h"
2 3 4 5 6 7 8 9 10
#include "test/armv7/fixture.h"
#include "test/common/benchmarker.h"
#include "test/common/checker.h"
#include "test/common/rng.h"

using namespace megdnn;
using namespace test;

TEST_F(ARMV7, MATRIX_MUL) {
M
Megvii Engine Team 已提交
11 12 13
    matrix_mul::check_matrix_mul(
            dtype::Float32{}, dtype::Float32{}, dtype::Float32{}, handle(),
            "ARMV7_F32");
14 15 16 17 18
}

TEST_F(ARMV7, MATRIX_MUL_MK4) {
    matrix_mul::check_matrix_mul(
            dtype::Float32{}, dtype::Float32{}, dtype::Float32{}, handle(),
19
            "ARMV7_F32_MK4_4x8", param::MatrixMul::Format::MK4, 1);
20 21
}

22 23 24 25 26 27
TEST_F(ARMV7, MATRIX_MUL_PACK_MK4) {
    matrix_mul::check_matrix_mul(
            dtype::Float32{}, dtype::Float32{}, dtype::Float32{}, handle(),
            "ARMV7_F32_MK4_PACK_4X12", param::MatrixMul::Format::MK4, 1);
}

28 29 30 31 32 33
TEST_F(ARMV7, MATRIX_MUL_MK4_INT8) {
    std::vector<matrix_mul::TestArg> args;
    for (size_t m : {1, 2, 3, 4, 5, 7, 10, 11})
        for (size_t n : {1, 2, 3, 4, 5, 8, 16, 24, 25, 32})
            for (size_t k : {1, 2, 3, 4, 5, 6, 7, 8, 16, 32, 33, 34})
                args.emplace_back(m, n, k, 0);
M
Megvii Engine Team 已提交
34 35 36 37
    matrix_mul::check_matrix_mul(
            dtype::Int8{}, dtype::Int8{}, dtype::Int32{}, handle(),
            "ARMV7_INT8X8X32_MK4_4X2X16", param::MatrixMul::Format::MK4, 1, 1e-3,
            std::move(args));
38 39 40
}

TEST_F(ARMV7, MATRIX_MUL_INT8x8x16_K4x8x8) {
M
Megvii Engine Team 已提交
41 42 43
    matrix_mul::check_matrix_mul(
            dtype::Int8{}, dtype::Int8{}, dtype::Int16{}, handle(),
            "ARMV7_INT8X8X16_K4X8X8");
44 45
}

46
TEST_F(ARMV7, MATRIX_MUL_INT8x8x16_K8x8x4) {
M
Megvii Engine Team 已提交
47 48 49
    matrix_mul::check_matrix_mul(
            dtype::Int8{}, dtype::Int8{}, dtype::Int16{}, handle(),
            "ARMV7_INT8X8X16_K8X8X4");
50 51
}

52
TEST_F(ARMV7, MATRIX_MUL_INT8x8x16_MK4_K8x8x4) {
M
Megvii Engine Team 已提交
53 54 55
    matrix_mul::check_matrix_mul(
            dtype::Int8{}, dtype::Int8{}, dtype::Int16{}, handle(),
            "ARMV7_INT8X8X16_MK4_K8X8X4", param::MatrixMul::Format::MK4, 1);
56 57
}

58
TEST_F(ARMV7, MATRIX_MUL_INT16x16x32) {
M
Megvii Engine Team 已提交
59 60 61
    matrix_mul::check_matrix_mul(
            dtype::Int16{}, dtype::Int16{}, dtype::Int32{}, handle(),
            "ARMV7_INT16X16X32_K12X4X1");
62 63 64
}

TEST_F(ARMV7, MATRIX_MUL_INT16x16x32_MK8) {
M
Megvii Engine Team 已提交
65 66 67
    matrix_mul::check_matrix_mul(
            dtype::Int16{}, dtype::Int16{}, dtype::Int32{}, handle(),
            "ARMV7_INT16X16X32_MK8_4X8", param::MatrixMul::Format::MK8, 1);
68 69 70 71
}

#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
TEST_F(ARMV7, MATRIX_MUL_FP16) {
M
Megvii Engine Team 已提交
72 73 74
    matrix_mul::check_matrix_mul(
            dtype::Float16{}, dtype::Float16{}, dtype::Float16{}, handle(),
            "AARCH32_F16_K4X16X1");
75 76 77 78
}
TEST_F(ARMV7, MATRIX_MUL_F16_MK8) {
    matrix_mul::check_matrix_mul(
            dtype::Float16{}, dtype::Float16{}, dtype::Float16{}, handle(),
79
            "AARCH32_F16_MK8_4X8", param::MatrixMul::Format::MK8, 1);
80 81 82
}
#endif

83
#if MGB_ENABLE_DOT
84
TEST_F(ARMV7, MATRIX_MUL_SDOT) {
M
Megvii Engine Team 已提交
85 86 87
    matrix_mul::check_matrix_mul(
            dtype::Int8(), dtype::Int8(), dtype::Int32(), handle(),
            "AARCH32_INT8_K6X8X4");
88 89 90 91
}

TEST_F(ARMV7, MATRIX_MUL_UDOT) {
    matrix_mul::check_matrix_mul(
92 93
            dtype::Quantized8Asymm(4.0f, static_cast<uint8_t>(10)),
            dtype::Quantized8Asymm(3.0f, static_cast<uint8_t>(54)),
94 95
            dtype::QuantizedS32(12.0f), handle(), "AARCH32_QUINT8_K4X8X4");
}
96 97 98 99 100 101 102

TEST_F(ARMV7, MATRIX_MUL_MK4_DOT_INT8) {
    std::vector<matrix_mul::TestArg> args;
    for (size_t m : {1, 2, 3, 4, 5, 7, 10, 11})
        for (size_t n : {1, 2, 3, 4, 5, 8, 16, 24, 25, 32})
            for (size_t k : {1, 2, 3, 4, 5, 6, 7, 8, 16, 32, 33, 34})
                args.emplace_back(m, n, k, 0);
M
Megvii Engine Team 已提交
103 104 105 106
    matrix_mul::check_matrix_mul(
            dtype::Int8{}, dtype::Int8{}, dtype::Int32{}, handle(),
            "AARCH32_INT8_MK4_8X4X4_DOTPROD", param::MatrixMul::Format::MK4_DOT, 1,
            1e-3, std::move(args));
107
}
108 109 110 111 112
#endif

#if MEGDNN_WITH_BENCHMARK

namespace {
113 114 115
void run_8x8x16_benchmark(
        const char* algo, Handle* handle,
        MatrixMul::Param::Format format = MatrixMul::Param::Format::DEFAULT) {
116 117 118 119 120 121 122 123 124 125 126 127
    constexpr size_t RUNS = 50;
    param::MatrixMul param;
    Benchmarker<MatrixMul> benchmarker_int(handle);
    Benchmarker<MatrixMul> benchmarker_int_kern_4x2x16(handle);
    benchmarker_int.set_before_exec_callback(
            AlgoChecker<MatrixMul>("ARM_COMMON_INT8X8X16"));
    benchmarker_int.set_times(RUNS)
            .set_dtype(0, dtype::Int8{})
            .set_dtype(1, dtype::Int8{})
            .set_dtype(2, dtype::Int16{})
            .set_param(param)
            .set_display(false);
128 129
    param::MatrixMul target_param;
    target_param.format = format;
M
Megvii Engine Team 已提交
130
    benchmarker_int_kern_4x2x16.set_before_exec_callback(AlgoChecker<MatrixMul>(algo));
131 132 133 134
    benchmarker_int_kern_4x2x16.set_times(RUNS)
            .set_dtype(0, dtype::Int8{})
            .set_dtype(1, dtype::Int8{})
            .set_dtype(2, dtype::Int16{})
135
            .set_param(target_param)
136 137 138 139 140 141
            .set_display(false);
    Benchmarker<MatrixMul> benchmarker_float(handle);
    benchmarker_float.set_display(false).set_times(RUNS);

    auto run = [&](size_t M, size_t N, size_t K) {
        auto int_used = benchmarker_int.exec({{M, K}, {K, N}, {}}) / RUNS;
142 143 144 145 146 147 148
        auto int_kern_used = 1e10;
        if (format == MatrixMul::Param::Format::MK4) {
            int_kern_used = benchmarker_int_kern_4x2x16.exec(
                                    {{M / 4, K / 4, 4, 4}, {K / 4, N, 4}, {}}) /
                            RUNS;
        } else {
            int_kern_used =
M
Megvii Engine Team 已提交
149
                    benchmarker_int_kern_4x2x16.exec({{M, K}, {K, N}, {}}) / RUNS;
150
        }
151 152 153 154 155 156 157 158 159
        auto float_used = benchmarker_float.exec({{M, K}, {K, N}, {}}) / RUNS;
        float computations = 2.f * M * K * N * 1e-6;
        printf("run: {%zu{M} %zu{K} %zu{N}} float: %f ms %f Gflops int: %f "
               "ms "
               "%f Gflops %s: %f ms %f Gflops "
               "speedup(%s/arm_common, %s/float): %f "
               "%f\n",
               M, K, N, float_used, computations / float_used, int_used,
               computations / int_used, algo, int_kern_used,
M
Megvii Engine Team 已提交
160 161
               computations / int_kern_used, algo, algo, int_used / int_kern_used,
               float_used / int_kern_used);
162 163 164
    };

    run(256, 12 * 24, 256);
165
    run(256, 256, 256);
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182

    //////////////////////// gemv //////////////////////////
    for (size_t M : {8, 64, 112, 256}) {
        for (size_t K : {8, 64, 112, 256}) {
            run(M, 1, K);
        }
    }

    //////////////////////// gemm //////////////////////////
    for (size_t M : {8, 64, 112, 256}) {
        for (size_t K : {8, 16, 32, 64, 112, 256}) {
            for (size_t N : {8, 64, 112, 256}) {
                run(M, N, K);
            }
        }
    }
}
183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201

void run_8x8x16_contrast(
        const char* algo0, const char* algo, Handle* handle,
        MatrixMul::Param::Format format = MatrixMul::Param::Format::DEFAULT) {
    constexpr size_t RUNS = 100;
    param::MatrixMul param;
    Benchmarker<MatrixMul> benchmarker_int(handle);
    Benchmarker<MatrixMul> benchmarker_int_kern_4x2x16(handle);
    benchmarker_int.set_before_exec_callback(AlgoChecker<MatrixMul>(algo0));

    benchmarker_int.set_times(RUNS)
            .set_dtype(0, dtype::Int8{})
            .set_dtype(1, dtype::Int8{})
            .set_dtype(2, dtype::Int16{})
            .set_param(param)
            .set_display(false);
    param::MatrixMul target_param;
    target_param.format = format;

M
Megvii Engine Team 已提交
202
    benchmarker_int_kern_4x2x16.set_before_exec_callback(AlgoChecker<MatrixMul>(algo));
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219
    benchmarker_int_kern_4x2x16.set_times(RUNS)
            .set_dtype(0, dtype::Int8{})
            .set_dtype(1, dtype::Int8{})
            .set_dtype(2, dtype::Int16{})
            .set_param(target_param)
            .set_display(false);

    auto run = [&](size_t M, size_t N, size_t K) {
        auto int_used = benchmarker_int.exec({{M, K}, {K, N}, {}}) / RUNS;
        auto int_kern_used = 1e10;
        double computation = 2.0f * M * N * K * 1e-6;
        if (format == MatrixMul::Param::Format::MK4) {
            int_kern_used = benchmarker_int_kern_4x2x16.exec(
                                    {{M / 4, K / 4, 4, 4}, {K / 4, N, 4}, {}}) /
                            RUNS;
        } else {
            int_kern_used =
M
Megvii Engine Team 已提交
220
                    benchmarker_int_kern_4x2x16.exec({{M, K}, {K, N}, {}}) / RUNS;
221 222 223
        }

        printf(" %f(%f)\t %f(%f)\t %f\n", int_used, computation / int_used,
M
Megvii Engine Team 已提交
224
               int_kern_used, computation / int_kern_used, int_used / int_kern_used);
225
    };
M
Megvii Engine Team 已提交
226 227
    printf("\nN\t K\t M\t %s ms(GFlops)\t %s ms(GFlops)\t SPEEDUP\n", algo0, algo);

228 229
    for (size_t M : {8}) {
        for (size_t K : {72}) {
M
Megvii Engine Team 已提交
230 231 232
            for (size_t N :
                 {8, 16, 32, 64, 72, 128, 256, 512, 1024, 4096, 8192, 16384, 32768,
                  65536}) {
233 234 235 236 237 238 239 240 241
                printf("%zu\t %zu\t %zu\t", N, K, M);
                run(M, N, K);
            }
        }
    }
    printf("512\t 512\t 512\t");
    run(512, 512, 512);
}

242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264
void run_16x16x32_benchmark(const char* algo, Handle* handle) {
    constexpr size_t RUNS = 50;
    param::MatrixMul param;
    Benchmarker<MatrixMul> benchmarker_int(handle);
    benchmarker_int.set_before_exec_callback(
            AlgoChecker<MatrixMul>("ARMV7_INT16X16X32_K12X4X1"));
    benchmarker_int.set_times(RUNS)
            .set_dtype(0, dtype::Int16{})
            .set_dtype(1, dtype::Int16{})
            .set_dtype(2, dtype::Int32{})
            .set_param(param)
            .set_display(false);
    Benchmarker<MatrixMul> benchmarker_float(handle);
    benchmarker_float.set_display(false).set_times(RUNS);

    auto run = [&](size_t M, size_t N, size_t K) {
        auto int_used = benchmarker_int.exec({{M, K}, {K, N}, {}}) / RUNS;
        auto float_used = benchmarker_float.exec({{M, K}, {K, N}, {}}) / RUNS;
        float computations = 2.f * M * K * N * 1e-6;
        printf("run: {%zu{M} %zu{K} %zu{N}} float: %f ms %f Gflops \n"
               "int: %f ms %f Gflops %s: \n"
               "speedup(%s/arm_common, %s/float): %f\n",
               M, K, N, float_used, computations / float_used, int_used,
M
Megvii Engine Team 已提交
265
               computations / int_used, algo, algo, algo, float_used / int_used);
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287
    };

    run(256, 12 * 24, 256);

    //////////////////////// gemv //////////////////////////
    for (size_t M : {8, 64, 112, 256}) {
        for (size_t K : {8, 64, 112, 256}) {
            run(M, 1, K);
        }
    }

    //////////////////////// gemm //////////////////////////
    for (size_t M : {8, 64, 112, 256}) {
        for (size_t K : {8, 16, 32, 64, 112, 256}) {
            for (size_t N :
                 {1, 2, 3, 4, 8, 64, 112, 113, 114, 115, 256, 257, 258, 259}) {
                run(M, N, K);
            }
        }
    }
}

288
#if MGB_ENABLE_DOT
289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
void run_8x8x32_benchmark(const char* algo, Handle* handle) {
    constexpr size_t RUNS = 50;
    param::MatrixMul param;
    Benchmarker<MatrixMul> benchmarker_int8(handle);
    benchmarker_int8.set_before_exec_callback(AlgoChecker<MatrixMul>(algo));
    benchmarker_int8.set_times(RUNS)
            .set_dtype(0, dtype::Int8{})
            .set_dtype(1, dtype::Int8{})
            .set_dtype(2, dtype::Int32{})
            .set_param(param)
            .set_display(false);
    Benchmarker<MatrixMul> benchmarker_float(handle);
    benchmarker_float.set_display(false).set_times(RUNS);

    auto run = [&](size_t M, size_t N, size_t K) {
        auto int_used = benchmarker_int8.exec({{M, K}, {K, N}, {}}) / RUNS;
        auto float_used = benchmarker_float.exec({{M, K}, {K, N}, {}}) / RUNS;
        float computations = 2.f * M * K * N * 1e-6;
        printf("run: {%zu{M} %zu{K} %zu{N}} float: %f ms %f Gflops \n"
               "int: %f ms %f Gflops %s: \n"
               "speedup(%s/arm_common, %s/float): %f\n",
               M, K, N, float_used, computations / float_used, int_used,
M
Megvii Engine Team 已提交
311
               computations / int_used, algo, algo, algo, float_used / int_used);
312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331
    };

    run(256, 12 * 24, 256);
    //////////////////////// gemm //////////////////////////
    for (size_t M : {8, 64, 112, 256}) {
        for (size_t K : {8, 16, 32, 64, 112, 256}) {
            for (size_t N : {113, 114, 115, 256, 1024}) {
                run(M, N, K);
            }
        }
    }
}

void run_8x8x32_quint_benchmark(Handle* handle) {
    constexpr size_t RUNS = 50;
    param::MatrixMul param;
    Benchmarker<MatrixMul> benchmarker_quint8_dot(handle);
    benchmarker_quint8_dot.set_before_exec_callback(
            AlgoChecker<MatrixMul>("AARCH32_QUINT8_K4X8X4"));
    benchmarker_quint8_dot.set_times(RUNS)
M
Megvii Engine Team 已提交
332 333
            .set_dtype(0, dtype::Quantized8Asymm(2.3f, static_cast<uint8_t>(20)))
            .set_dtype(1, dtype::Quantized8Asymm(3.1f, static_cast<uint8_t>(30)))
334
            .set_dtype(2, dtype::QuantizedS32(2.3f * 3.1f))
335 336 337 338 339 340 341
            .set_param(param)
            .set_display(false);

    Benchmarker<MatrixMul> benchmarker_quint8(handle);
    benchmarker_quint8.set_before_exec_callback(
            AlgoChecker<MatrixMul>("ARMV7_QUINT8_K4X8X8"));
    benchmarker_quint8.set_times(RUNS)
M
Megvii Engine Team 已提交
342 343
            .set_dtype(0, dtype::Quantized8Asymm(2.3f, static_cast<uint8_t>(20)))
            .set_dtype(1, dtype::Quantized8Asymm(3.1f, static_cast<uint8_t>(30)))
344
            .set_dtype(2, dtype::QuantizedS32(2.3f * 3.1f))
345 346 347 348
            .set_param(param)
            .set_display(false);

    auto run = [&](size_t M, size_t N, size_t K) {
M
Megvii Engine Team 已提交
349
        auto dot_used = benchmarker_quint8_dot.exec({{M, K}, {K, N}, {}}) / RUNS;
350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
        auto normal_used = benchmarker_quint8.exec({{M, K}, {K, N}, {}}) / RUNS;
        float computations = 2.f * M * K * N * 1e-6;
        printf("run: {%zu{M} %zu{K} %zu{N}} dot: %f ms %f Gflops \n"
               "normal: %f ms %f Gflops.speedup: %f\n",
               M, K, N, dot_used, computations / dot_used, normal_used,
               computations / normal_used, normal_used / dot_used);
    };

    run(256, 12 * 24, 256);
    //////////////////////// gemm //////////////////////////
    for (size_t M : {8, 64, 112, 256}) {
        for (size_t K : {8, 16, 32, 64, 112, 256}) {
            for (size_t N : {113, 114, 115, 256, 1024}) {
                run(M, N, K);
            }
        }
    }
}
#endif
}  // namespace

371
#if MGB_ENABLE_DOT
372 373 374 375 376 377
TEST_F(ARMV7, BENCHMARK_MATRIX_MUL_INT8x8x32_K6x8x4) {
    run_8x8x32_benchmark("AARCH32_INT8_K6X8X4", handle());
}
TEST_F(ARMV7, BENCHMARK_MATRIX_MUL_QUINT8x8x32_K4x8x4) {
    run_8x8x32_quint_benchmark(handle());
}
378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394

TEST_F(ARMV7, BENCHMARK_MATRIX_MUL_INT8x8x32_MK4_DOT) {
    constexpr size_t RUNS = 50;
    param::MatrixMul param;
    Benchmarker<MatrixMul> benchmarker_default(handle());
    benchmarker_default.set_times(RUNS)
            .set_dtype(0, dtype::Int8())
            .set_dtype(1, dtype::Int8())
            .set_dtype(2, dtype::Int32())
            .set_param(param)
            .set_display(false);
    benchmarker_default.set_before_exec_callback(
            AlgoChecker<MatrixMul>("AARCH32_INT8_K6X8X4"));

    param.format = MatrixMul::Param::Format::MK4_DOT;
    Benchmarker<MatrixMul> benchmarker_mk4_dot(handle());
    benchmarker_mk4_dot.set_before_exec_callback(
395
            AlgoChecker<MatrixMul>("AARCH32_INT8_MK4_8X4X4_DOTPROD"));
396 397 398 399 400 401 402 403
    benchmarker_mk4_dot.set_param(param)
            .set_dtype(0, dtype::Int8())
            .set_dtype(1, dtype::Int8())
            .set_dtype(2, dtype::Int32())
            .set_display(false)
            .set_times(RUNS);

    auto run = [&](size_t M, size_t N, size_t K) {
M
Megvii Engine Team 已提交
404 405 406 407
        auto default_used = benchmarker_default.exec({{M, K}, {K, N}, {}}) / RUNS;
        auto mk4_dot_used =
                benchmarker_mk4_dot.exec({{M / 4, K / 4, 4, 4}, {K / 4, N, 4}, {}}) /
                RUNS;
408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423
        float computations = 2.f * M * K * N * 1e-6;
        printf("run: {%zu{M} %zu{K} %zu{N}} default: %f ms %f Gflops mk4_dot: "
               "%f ms "
               "%f Gflops speedup: %f\n",
               M, K, N, default_used, computations / default_used, mk4_dot_used,
               computations / mk4_dot_used, default_used / mk4_dot_used);
    };

    for (size_t M = 4; M < 512; M *= 2) {
        for (size_t K = 4; K < 512; K *= 2) {
            for (size_t N : {4, 8, 33, 113, 128}) {
                run(M, N, K);
            }
        }
    }
}
424 425 426 427 428 429 430 431 432 433
#endif

TEST_F(ARMV7, BENCHMARK_MATRIX_MUL_INT8x8x16_K4x2x16) {
    run_8x8x16_benchmark("ARMV7_INT8X8X16_K4X2X16", handle());
}

TEST_F(ARMV7, BENCHMARK_MATRIX_MUL_INT8x8x16_K4x8x8) {
    run_8x8x16_benchmark("ARMV7_INT8X8X16_K4X8X8", handle());
}

434 435 436 437
TEST_F(ARMV7, BENCHMARK_MATRIX_MUL_INT8x8x16_K8x8x4) {
    run_8x8x16_benchmark("ARMV7_INT8X8X16_K8X8X4", handle());
}

438
TEST_F(ARMV7, BENCHMARK_MATRIX_MUL_INT8x8x16_MK4_K4x8x8) {
M
Megvii Engine Team 已提交
439 440
    run_8x8x16_benchmark(
            "ARMV7_INT8X8X16_MK4_K8X8X4", handle(), MatrixMul::Param::Format::MK4);
441 442
}

443 444 445 446
TEST_F(ARMV7, BENCHMARK_MATRIX_MUL_INT16x16x32_K12x4x1) {
    run_16x16x32_benchmark("ARMV7_INT16X16X32_K12X4X1", handle());
}

447
TEST_F(ARMV7, BENCHMARK_MATRIX_MUL_INT8x8x16_K8x8x4_CONTRAST) {
M
Megvii Engine Team 已提交
448
    run_8x8x16_contrast("ARM_COMMON_INT8X8X16", "ARMV7_INT8X8X16_K8X8X4", handle());
449 450 451
}

TEST_F(ARMV7, BENCHMARK_MATRIX_MUL_INT8x8x16_K4x8x8_CONTRAST) {
M
Megvii Engine Team 已提交
452
    run_8x8x16_contrast("ARM_COMMON_INT8X8X16", "ARMV7_INT8X8X16_K4X8X8", handle());
453 454 455
}

TEST_F(ARMV7, BENCHMARK_MATRIX_MUL_INT8x8x16_K4x8x8_K8x8x4_CONTRAST) {
M
Megvii Engine Team 已提交
456
    run_8x8x16_contrast("ARMV7_INT8X8X16_K4X8X8", "ARMV7_INT8X8X16_K8X8X4", handle());
457 458
}

459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
#if __ARM_FEATURE_FP16_VECTOR_ARITHMETIC
TEST_F(ARMV7, BENCHMARK_MATRIX_MUL_FP16) {
    constexpr size_t RUNS = 50;
    param::MatrixMul param;
    Benchmarker<MatrixMul> benchmarker_fp16(handle());
    benchmarker_fp16.set_times(RUNS)
            .set_dtype(0, dtype::Float16())
            .set_dtype(1, dtype::Float16())
            .set_dtype(2, dtype::Float16())
            .set_param(param)
            .set_display(false);
    Benchmarker<MatrixMul> benchmarker_float(handle());
    benchmarker_float.set_param(param).set_display(false).set_times(RUNS);

    auto run = [&](size_t M, size_t N, size_t K) {
        auto fp16_used = benchmarker_fp16.exec({{M, K}, {K, N}, {}}) / RUNS;
        auto float_used = benchmarker_float.exec({{M, K}, {K, N}, {}}) / RUNS;
        float computations = 2.f * M * K * N * 1e-6;
        printf("run: {%zu{M} %zu{K} %zu{N}} float: %f ms %f Gflops fp16: %f ms "
               "%f Gflops speedup: %f\n",
               M, K, N, float_used, computations / float_used, fp16_used,
               computations / fp16_used, float_used / fp16_used);
    };

    run(256, 12 * 24, 256);

    for (size_t M : {8, 64, 112, 256}) {
        for (size_t K : {8, 64, 112, 256}) {
            for (size_t N : {8, 64, 112, 256}) {
                run(M, N, K);
            }
        }
    }
}

TEST_F(ARMV7, BENCHMARK_MATRIX_MUL_F16_MK8) {
    auto args = matrix_mul::get_benchmark_matmul_mk_packed_args(4);
    matrix_mul::benchmark_with_contrast(
M
Megvii Engine Team 已提交
497 498 499
            handle(), args, dtype::Float16{}, dtype::Float16{}, dtype::Float16{},
            "AARCH32_F16_MK8_4X8", param::MatrixMul::Format::MK8, dtype::Float16{},
            dtype::Float16{}, dtype::Float16{}, "AARCH32_F16_K4X16X1");
500 501 502 503 504 505
}
#endif

TEST_F(ARMV7, BENCHMARK_MATRIX_MUL_MK4) {
    auto args = matrix_mul::get_benchmark_matmul_mk_packed_args(8);
    matrix_mul::benchmark_with_contrast(
M
Megvii Engine Team 已提交
506 507 508
            handle(), args, dtype::Float32{}, dtype::Float32{}, dtype::Float32{},
            "ARMV7_F32_MK4_4x8", param::MatrixMul::Format::MK4, dtype::Float32{},
            dtype::Float32{}, dtype::Float32{});
509 510
}

511 512 513
TEST_F(ARMV7, BENCHMARK_MATRIX_MUL_PACK_MK4) {
    auto args = matrix_mul::get_benchmark_matmul_mk_packed_args(8);
    matrix_mul::benchmark_with_contrast(
M
Megvii Engine Team 已提交
514 515 516
            handle(), args, dtype::Float32{}, dtype::Float32{}, dtype::Float32{},
            "ARMV7_F32_MK4_PACK_4X12", param::MatrixMul::Format::MK4, dtype::Float32{},
            dtype::Float32{}, dtype::Float32{});
517 518
}

519 520 521 522
TEST_F(ARMV7, BENCHMARK_MATRIX_MUL_INT16x16x32_MK8) {
    auto args = matrix_mul::get_benchmark_matmul_mk_packed_args(4);
    matrix_mul::benchmark_with_contrast(
            handle(), args, dtype::Int16{}, dtype::Int16{}, dtype::Int32{},
M
Megvii Engine Team 已提交
523 524
            "ARMV7_INT16X16X32_MK8_4X8", param::MatrixMul::Format::MK8, dtype::Int16{},
            dtype::Int16{}, dtype::Int32{});
525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552
}
TEST_F(ARMV7, BENCHMARK_MATRIX_MUL_INT32_MK_4X2X16) {
    constexpr size_t RUNS = 50;
    param::MatrixMul param;
    param.transposeA = false;
    param.transposeB = false;
    Benchmarker<MatrixMul> benchmarker(handle());
    Benchmarker<MatrixMul> benchmarker_mk4(handle());
    benchmarker.set_times(RUNS)
            .set_dtype(0, dtype::Int8{})
            .set_dtype(1, dtype::Int8{})
            .set_dtype(2, dtype::Int32{})
            .set_param(param)
            .set_display(false);
    benchmarker.set_before_exec_callback(
            AlgoChecker<MatrixMul>("ARMV7_INT8X8X32_K4X2X16"));

    param.format = MatrixMul::Param::Format::MK4;
    benchmarker_mk4.set_before_exec_callback(
            AlgoChecker<MatrixMul>("ARMV7_INT8X8X32_MK4_4X2X16"));
    benchmarker_mk4.set_times(RUNS)
            .set_dtype(0, dtype::Int8{})
            .set_dtype(1, dtype::Int8{})
            .set_dtype(2, dtype::Int32{})
            .set_param(param)
            .set_display(false);

    auto run = [&](size_t M, size_t N, size_t K) {
M
Megvii Engine Team 已提交
553 554
        auto mk_used =
                benchmarker_mk4.exec({{M / 4, K / 4, 4, 4}, {K / 4, N, 4}, {}}) / RUNS;
555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573
        auto default_used = benchmarker.exec({{M, K}, {K, N}, {}}) / RUNS;
        float computations = 2.f * M * K * N * 1e-6;
        printf("run: {%zu{M} %zu{K} %zu{N}} normal: %f ms %f Gflops mk4: %f ms "
               "%f Gflops speedup_vs_normal: %f\n",
               M, K, N, default_used, computations / default_used, mk_used,
               computations / mk_used, default_used / mk_used);
    };

    run(256, 256, 128);
    for (size_t k = 4; k <= 512; k *= 2) {
        for (size_t m = 4; m <= 512; m *= 2) {
            for (size_t n = 4; n <= 512; n *= 2) {
                run(m, n, k);
            }
        }
        std::cout << std::endl;
    }
}

574 575 576 577 578 579
TEST_F(ARMV7, BENCHMARK_MATRIX_MUL_ARMV7_F32) {
    auto args = matrix_mul::get_benchmark_matmul_args();
    matrix_mul::benchmark_single_algo(
            handle(), args, dtype::Float32{}, dtype::Float32{}, dtype::Float32{},
            "ARMV7_F32", param::MatrixMul::Format::DEFAULT);
}
580 581 582
#endif

// vim: syntax=cpp.doxygen