Files
ncnn/tests/test_gemm_3.cpp
nihui 41f031e045 reduce redundant tests that do not contribute to coverage (#7042)
* adjust tests
* opt shader coverage op
* update gpu_coverage.cpp atomicor coalesce
2026-10-05 08:57:20 +08:00

781 lines
37 KiB
C++

// Copyright 2024 Tencent
// SPDX-License-Identifier: BSD-3-Clause
#include "testutil.h"
#if NCNN_INT8
static void RandomizeA(ncnn::Mat& m, int transA, float absmax)
{
if (transA == 0)
{
const int h = m.dims == 3 ? m.c : m.h;
for (int i = 0; i < h; i++)
{
float* p = m.dims == 3 ? m.channel(i) : m.row(i);
float randabsmax = RandomFloat(absmax * 0.5f, absmax);
randabsmax = ncnn::float16_to_float32(ncnn::float32_to_float16(randabsmax));
randabsmax = ncnn::bfloat16_to_float32(ncnn::float32_to_bfloat16(randabsmax));
for (int j = 0; j < m.w; j++)
{
p[j] = RandomFloat(-randabsmax, randabsmax);
}
// set random a and b
p[RandomInt(0, m.w - 1)] = -randabsmax;
p[RandomInt(0, m.w - 1)] = randabsmax;
// drop 0.45 ~ 0.55
for (int j = 0; j < m.w; j++)
{
float v = p[j] * (127.f / randabsmax);
float vv = fabs(v - (int)v);
float hp = ncnn::float16_to_float32(ncnn::float32_to_float16(p[j]));
float hv = hp * (127.f / randabsmax);
float hvv = fabs(hv - (int)hv);
float bp = ncnn::bfloat16_to_float32(ncnn::float32_to_bfloat16(p[j]));
float bv = bp * (127.f / randabsmax);
float bvv = fabs(bv - (int)bv);
while ((vv > 0.45f && vv < 0.55f) || (hvv > 0.45f && hvv < 0.55f) || (bvv > 0.45f && bvv < 0.55f))
{
p[j] = RandomFloat(-randabsmax, randabsmax);
v = p[j] * (127.f / randabsmax);
vv = fabs(v - (int)v);
hp = ncnn::float16_to_float32(ncnn::float32_to_float16(p[j]));
hv = hp * (127.f / randabsmax);
hvv = fabs(hv - (int)hv);
bp = ncnn::bfloat16_to_float32(ncnn::float32_to_bfloat16(p[j]));
bv = bp * (127.f / randabsmax);
bvv = fabs(bv - (int)bv);
}
}
}
}
else // if (transA == 1)
{
std::vector<float> randabsmaxes(m.w);
for (int j = 0; j < m.w; j++)
{
float randabsmax = RandomFloat(absmax * 0.5f, absmax);
randabsmax = ncnn::float16_to_float32(ncnn::float32_to_float16(randabsmax));
randabsmax = ncnn::bfloat16_to_float32(ncnn::float32_to_bfloat16(randabsmax));
randabsmaxes[j] = randabsmax;
}
const int h = m.dims == 3 ? m.c : m.h;
for (int i = 0; i < h; i++)
{
float* p = m.dims == 3 ? m.channel(i) : m.row(i);
for (int j = 0; j < m.w; j++)
{
const float randabsmax = randabsmaxes[j];
p[j] = RandomFloat(-randabsmax, randabsmax);
}
// drop 0.45 ~ 0.55
for (int j = 0; j < m.w; j++)
{
const float randabsmax = randabsmaxes[j];
float v = p[j] * (127.f / randabsmax);
float vv = fabs(v - (int)v);
float hp = ncnn::float16_to_float32(ncnn::float32_to_float16(p[j]));
float hv = hp * (127.f / randabsmax);
float hvv = fabs(hv - (int)hv);
float bp = ncnn::bfloat16_to_float32(ncnn::float32_to_bfloat16(p[j]));
float bv = bp * (127.f / randabsmax);
float bvv = fabs(bv - (int)bv);
while ((vv > 0.45f && vv < 0.55f) || (hvv > 0.45f && hvv < 0.55f) || (bvv > 0.45f && bvv < 0.55f))
{
p[j] = RandomFloat(-randabsmax, randabsmax);
v = p[j] * (127.f / randabsmax);
vv = fabs(v - (int)v);
hp = ncnn::float16_to_float32(ncnn::float32_to_float16(p[j]));
hv = hp * (127.f / randabsmax);
hvv = fabs(hv - (int)hv);
bp = ncnn::bfloat16_to_float32(ncnn::float32_to_bfloat16(p[j]));
bv = bp * (127.f / randabsmax);
bvv = fabs(bv - (int)bv);
}
}
}
for (int j = 0; j < m.w; j++)
{
const int randi0 = RandomInt(0, h - 1);
const int randi1 = RandomInt(0, h - 1);
float* p0 = m.dims == 3 ? m.channel(randi0) : m.row(randi0);
float* p1 = m.dims == 3 ? m.channel(randi1) : m.row(randi1);
const float randabsmax = randabsmaxes[j];
// set random a and b
p0[j] = -randabsmax;
p1[j] = randabsmax;
}
}
}
static void RandomizeB(ncnn::Mat& m, float absmax)
{
absmax = ncnn::float16_to_float32(ncnn::float32_to_float16(absmax));
absmax = ncnn::bfloat16_to_float32(ncnn::float32_to_bfloat16(absmax));
const int h = m.dims == 3 ? m.c : m.h;
float* p = m;
for (int i = 0; i < h; i++)
{
float* p = m.dims == 3 ? m.channel(i) : m.row(i);
for (int j = 0; j < m.w; j++)
{
p[j] = RandomFloat(-absmax, absmax);
// drop 0.45 ~ 0.55
float v = p[j] * (127.f / absmax);
float vv = fabs(v - (int)v);
float hp = ncnn::float16_to_float32(ncnn::float32_to_float16(p[j]));
float hv = hp * (127.f / absmax);
float hvv = fabs(hv - (int)hv);
float bp = ncnn::bfloat16_to_float32(ncnn::float32_to_bfloat16(p[j]));
float bv = bp * (127.f / absmax);
float bvv = fabs(bv - (int)bv);
while ((vv > 0.45f && vv < 0.55f) || (hvv > 0.45f && hvv < 0.55f) || (bvv > 0.45f && bvv < 0.55f))
{
p[j] = RandomFloat(-absmax, absmax);
v = p[j] * (127.f / absmax);
vv = fabs(v - (int)v);
hp = ncnn::float16_to_float32(ncnn::float32_to_float16(p[j]));
hv = hp * (127.f / absmax);
hvv = fabs(hv - (int)hv);
bp = ncnn::bfloat16_to_float32(ncnn::float32_to_bfloat16(p[j]));
bv = bp * (127.f / absmax);
bvv = fabs(bv - (int)bv);
}
}
}
// set random a and b
if (m.dims == 3)
{
m.channel(RandomInt(0, h - 1))[RandomInt(0, m.w - 1)] = -absmax;
m.channel(RandomInt(0, h - 1))[RandomInt(0, m.w - 1)] = absmax;
}
else
{
m.row(RandomInt(0, h - 1))[RandomInt(0, m.w - 1)] = -absmax;
m.row(RandomInt(0, h - 1))[RandomInt(0, m.w - 1)] = absmax;
}
}
static int test_gemm_int8(int M, int N, int K, float alpha, int transA, int transB, int output_elemtype, int output_transpose, int constantA, int constantB, int output_N1M, int flag = 0)
{
ncnn::ParamDict pd;
pd.set(0, alpha);
pd.set(1, 1.f); // beta
pd.set(2, transA);
pd.set(3, transB);
pd.set(4, constantA);
pd.set(5, constantB);
pd.set(6, 1);
pd.set(7, M);
pd.set(8, N);
pd.set(9, K);
pd.set(10, -1);
pd.set(11, output_N1M);
pd.set(13, output_elemtype);
pd.set(14, output_transpose);
pd.set(18, 2); // int8_scale_term
std::vector<ncnn::Mat> weights;
if (constantA) weights.push_back(transA ? RandomS8Mat(M, K) : RandomS8Mat(K, M));
if (constantB) weights.push_back(transB ? RandomS8Mat(K, N) : RandomS8Mat(N, K));
if (constantA) weights.push_back(RandomMat(M, 10.f, 20.f));
if (constantB) weights.push_back(RandomMat(1, 10.f, 20.f));
std::vector<ncnn::Mat> a;
if (!constantA)
{
a.push_back(transA ? (output_N1M ? ncnn::Mat(M, 1, K) : ncnn::Mat(M, K)) : (output_N1M ? ncnn::Mat(K, 1, M) : ncnn::Mat(K, M)));
RandomizeA(a[a.size() - 1], transA, 10.f);
}
if (!constantB)
{
a.push_back(transB ? (output_N1M ? ncnn::Mat(K, 1, N) : ncnn::Mat(K, N)) : (output_N1M ? ncnn::Mat(N, 1, K) : ncnn::Mat(N, K)));
RandomizeB(a[a.size() - 1], 10.f);
}
ncnn::Option opt;
opt.num_threads = 1;
opt.use_packing_layout = true;
opt.use_fp16_packed = false;
opt.use_fp16_storage = false;
opt.use_fp16_arithmetic = false;
opt.use_bf16_packed = false;
opt.use_bf16_storage = false;
int ret = test_layer_opt("Gemm", pd, weights, opt, a, 1, 0.001, flag);
if (ret != 0)
{
fprintf(stderr, "test_gemm_int8 failed M=%d N=%d K=%d alpha=%f transA=%d transB=%d output_elemtype=%d output_transpose=%d constantA=%d constantB=%d output_N1M=%d\n", M, N, K, alpha, transA, transB, output_elemtype, output_transpose, constantA, constantB, output_N1M);
}
return ret;
}
static int test_gemm_int8_bias(int M, int N, int K, const ncnn::Mat& C, float alpha, float beta, int transA, int transB, int output_elemtype, int output_transpose, int constantA, int constantB, int constantC, bool use_bf16 = false, int output_N1M = 0, int flag = 0)
{
int broadcast_type_C = 0;
if (C.dims == 1 && C.w == 1)
{
// scalar
broadcast_type_C = 0;
}
if (C.dims == 1 && C.w == M)
{
// M
// auto broadcast from h to w is the ncnn-style convention
broadcast_type_C = 1;
}
if (C.dims == 1 && C.w == N)
{
// N
broadcast_type_C = 4;
}
if (C.dims == 2 && C.w == 1 && C.h == M)
{
// Mx1
broadcast_type_C = 2;
}
if (C.dims == 2 && C.w == N && C.h == M)
{
// MxN
broadcast_type_C = 3;
}
if (C.dims == 2 && C.w == N && C.h == 1)
{
// 1xN
broadcast_type_C = 4;
}
ncnn::ParamDict pd;
pd.set(0, alpha);
pd.set(1, beta);
pd.set(2, transA);
pd.set(3, transB);
pd.set(4, constantA);
pd.set(5, constantB);
pd.set(6, constantC);
pd.set(7, M);
pd.set(8, N);
pd.set(9, K);
pd.set(10, broadcast_type_C);
pd.set(11, output_N1M);
// pd.set(12, 1); // output_elempack
pd.set(13, output_elemtype);
pd.set(14, output_transpose);
pd.set(18, 2); // int8_scale_term
std::vector<ncnn::Mat> weights;
if (constantA) weights.push_back(transA ? RandomS8Mat(M, K) : RandomS8Mat(K, M));
if (constantB) weights.push_back(transB ? RandomS8Mat(K, N) : RandomS8Mat(N, K));
if (constantC) weights.push_back(C);
if (constantA) weights.push_back(RandomMat(M, 10.f, 20.f));
if (constantB) weights.push_back(RandomMat(1, 10.f, 20.f));
std::vector<ncnn::Mat> a;
if (!constantA)
{
a.push_back(transA ? (output_N1M ? ncnn::Mat(M, 1, K) : ncnn::Mat(M, K)) : (output_N1M ? ncnn::Mat(K, 1, M) : ncnn::Mat(K, M)));
RandomizeA(a[a.size() - 1], transA, 10.f);
}
if (!constantB)
{
a.push_back(transB ? (output_N1M ? ncnn::Mat(K, 1, N) : ncnn::Mat(K, N)) : (output_N1M ? ncnn::Mat(N, 1, K) : ncnn::Mat(N, K)));
RandomizeB(a[a.size() - 1], 10.f);
}
if (!constantC) a.push_back(C);
ncnn::Option opt;
opt.num_threads = 1;
opt.use_packing_layout = true;
opt.use_fp16_packed = false;
opt.use_fp16_storage = false;
opt.use_fp16_arithmetic = false;
opt.use_bf16_packed = use_bf16;
opt.use_bf16_storage = use_bf16;
int ret = test_layer_opt("Gemm", pd, weights, opt, a, 1, 0.001, flag);
if (ret != 0)
{
fprintf(stderr, "test_gemm_int8_bias failed M=%d N=%d K=%d C.dims=%d C=(%d %d %d) alpha=%f beta=%f transA=%d transB=%d output_elemtype=%d output_transpose=%d constantA=%d constantB=%d constantC=%d use_bf16=%d output_N1M=%d\n", M, N, K, C.dims, C.w, C.h, C.c, alpha, beta, transA, transB, output_elemtype, output_transpose, constantA, constantB, constantC, use_bf16, output_N1M);
}
return ret;
}
static int test_gemm_int8_fp16s(int M, int N, int K, float alpha, int transA, int transB, int output_elemtype, int output_transpose, int constantA, int constantB, int output_N1M)
{
ncnn::ParamDict pd;
pd.set(0, alpha);
pd.set(1, 1.f); // beta
pd.set(2, transA);
pd.set(3, transB);
pd.set(4, constantA);
pd.set(5, constantB);
pd.set(6, 1);
pd.set(7, M);
pd.set(8, N);
pd.set(9, K);
pd.set(10, -1);
pd.set(11, output_N1M);
pd.set(13, output_elemtype);
pd.set(14, output_transpose);
pd.set(18, 2); // int8_scale_term
std::vector<ncnn::Mat> weights;
if (constantA) weights.push_back(transA ? RandomS8Mat(M, K) : RandomS8Mat(K, M));
if (constantB) weights.push_back(transB ? RandomS8Mat(K, N) : RandomS8Mat(N, K));
if (constantA) weights.push_back(RandomMat(M, 10.f, 20.f));
if (constantB) weights.push_back(RandomMat(1, 10.f, 20.f));
std::vector<ncnn::Mat> a;
if (!constantA)
{
a.push_back(transA ? (output_N1M ? ncnn::Mat(M, 1, K) : ncnn::Mat(M, K)) : (output_N1M ? ncnn::Mat(K, 1, M) : ncnn::Mat(K, M)));
RandomizeA(a[a.size() - 1], transA, 10.f);
}
if (!constantB)
{
a.push_back(transB ? (output_N1M ? ncnn::Mat(K, 1, N) : ncnn::Mat(K, N)) : (output_N1M ? ncnn::Mat(N, 1, K) : ncnn::Mat(N, K)));
RandomizeB(a[a.size() - 1], 10.f);
}
ncnn::Option opt;
opt.num_threads = 1;
opt.use_packing_layout = true;
opt.use_fp16_packed = false;
opt.use_fp16_storage = true;
opt.use_fp16_arithmetic = false;
opt.use_bf16_packed = false;
opt.use_bf16_storage = false;
float epsilon = 0.001;
int ret = test_layer_opt("Gemm", pd, weights, opt, a, 1, epsilon);
if (ret != 0)
{
fprintf(stderr, "test_gemm_int8_fp16s failed M=%d N=%d K=%d alpha=%f transA=%d transB=%d output_elemtype=%d output_transpose=%d constantA=%d constantB=%d output_N1M=%d\n", M, N, K, alpha, transA, transB, output_elemtype, output_transpose, constantA, constantB, output_N1M);
return ret;
}
return 0;
}
static int test_gemm_int8_bf16s(int M, int N, int K, float alpha, int transA, int transB, int output_elemtype, int output_transpose, int constantA, int constantB, int output_N1M)
{
ncnn::ParamDict pd;
pd.set(0, alpha);
pd.set(1, 1.f); // beta
pd.set(2, transA);
pd.set(3, transB);
pd.set(4, constantA);
pd.set(5, constantB);
pd.set(6, 1);
pd.set(7, M);
pd.set(8, N);
pd.set(9, K);
pd.set(10, -1);
pd.set(11, output_N1M);
pd.set(13, output_elemtype);
pd.set(14, output_transpose);
pd.set(18, 2); // int8_scale_term
std::vector<ncnn::Mat> weights;
if (constantA) weights.push_back(transA ? RandomS8Mat(M, K) : RandomS8Mat(K, M));
if (constantB) weights.push_back(transB ? RandomS8Mat(K, N) : RandomS8Mat(N, K));
if (constantA) weights.push_back(RandomMat(M, 10.f, 20.f));
if (constantB) weights.push_back(RandomMat(1, 10.f, 20.f));
std::vector<ncnn::Mat> a;
if (!constantA)
{
a.push_back(transA ? (output_N1M ? ncnn::Mat(M, 1, K) : ncnn::Mat(M, K)) : (output_N1M ? ncnn::Mat(K, 1, M) : ncnn::Mat(K, M)));
RandomizeA(a[a.size() - 1], transA, 10.f);
}
if (!constantB)
{
a.push_back(transB ? (output_N1M ? ncnn::Mat(K, 1, N) : ncnn::Mat(K, N)) : (output_N1M ? ncnn::Mat(N, 1, K) : ncnn::Mat(N, K)));
RandomizeB(a[a.size() - 1], 10.f);
}
ncnn::Option opt;
opt.num_threads = 1;
opt.use_packing_layout = true;
opt.use_fp16_packed = false;
opt.use_fp16_storage = false;
opt.use_fp16_arithmetic = false;
opt.use_bf16_packed = true;
opt.use_bf16_storage = true;
float epsilon = 0.001;
int ret = test_layer_opt("Gemm", pd, weights, opt, a, 1, epsilon);
if (ret != 0)
{
fprintf(stderr, "test_gemm_int8_bf16s failed M=%d N=%d K=%d alpha=%f transA=%d transB=%d output_elemtype=%d output_transpose=%d constantA=%d constantB=%d output_N1M=%d\n", M, N, K, alpha, transA, transB, output_elemtype, output_transpose, constantA, constantB, output_N1M);
return ret;
}
return 0;
}
static int test_gemm_0(int M, int N, int K, int constant_flag = 0)
{
return 0
|| test_gemm_int8(M, N, K, 2.1f, 0, 1, 0, 0, 0, 0, 0)
|| test_gemm_int8(M, N, K, 3.1f, 1, 1, 0, 0, 0, 0, 0)
|| test_gemm_int8(M, N, K, 4.1f, 0, 0, 0, 0, 0, 0, 1)
|| test_gemm_int8(M, N, K, 5.1f, 1, 0, 0, 0, 0, 0, 1)
|| test_gemm_int8(M, N, K, 0.2f, 0, 1, 0, 0, 1, 0, 1, constant_flag)
|| test_gemm_int8(M, N, K, 0.3f, 1, 1, 0, 0, 1, 0, 1, constant_flag)
|| test_gemm_int8(M, N, K, 0.4f, 0, 0, 0, 0, 0, 1, 0, constant_flag)
|| test_gemm_int8(M, N, K, 0.5f, 0, 1, 0, 0, 0, 1, 0, constant_flag)
|| test_gemm_int8(M, N, K, 1.2f, 0, 1, 0, 0, 1, 1, 0, constant_flag)
|| test_gemm_int8(M, N, K, 1.3f, 1, 1, 0, 0, 1, 1, 1, constant_flag)
|| test_gemm_int8(M, N, K, 1.4f, 0, 0, 0, 0, 1, 0, 0, constant_flag)
|| test_gemm_int8(M, N, K, 1.5f, 1, 0, 0, 0, 1, 0, 1, constant_flag)
|| test_gemm_int8(M, N, K, -1.2f, 0, 1, 0, 1, 1, 1, 0, constant_flag)
|| test_gemm_int8(M, N, K, -1.3f, 1, 1, 0, 1, 1, 1, 0, constant_flag)
|| test_gemm_int8(M, N, K, -1.4f, 0, 0, 0, 1, 1, 1, 1, constant_flag)
|| test_gemm_int8(M, N, K, -1.5f, 1, 0, 0, 1, 1, 1, 1, constant_flag)
|| test_gemm_int8(M, N, K, -2.0f, 0, 1, 0, 1, 0, 1, 1, constant_flag)
|| test_gemm_int8(M, N, K, -3.0f, 1, 1, 0, 1, 0, 1, 1, constant_flag)
|| test_gemm_int8(M, N, K, -4.0f, 0, 0, 0, 1, 1, 0, 0, constant_flag)
|| test_gemm_int8(M, N, K, -5.0f, 0, 1, 0, 1, 1, 0, 0, constant_flag)
|| test_gemm_int8(M, N, K, -2.1f, 0, 1, 0, 1, 0, 0, 0)
|| test_gemm_int8(M, N, K, -3.1f, 1, 1, 0, 1, 0, 0, 1)
|| test_gemm_int8(M, N, K, -4.1f, 0, 0, 0, 1, 0, 1, 0, constant_flag)
|| test_gemm_int8(M, N, K, -5.1f, 1, 0, 0, 1, 0, 1, 1, constant_flag)
|| test_gemm_int8_fp16s(M, N, K, 1.f, 0, 1, 0, 0, 0, 0, 0)
|| test_gemm_int8_fp16s(M, N, K, 1.f, 1, 0, 0, 1, 0, 0, 0);
}
// dynamic matrix geometries cover transposes, rank-three output and fp16 storage
static int test_gemm_dynamic_geometry(int M, int N, int K)
{
return 0
|| test_gemm_int8(M, N, K, 2.1f, 0, 1, 0, 0, 0, 0, 0)
|| test_gemm_int8(M, N, K, 3.1f, 1, 1, 0, 0, 0, 0, 0)
|| test_gemm_int8(M, N, K, 4.1f, 0, 0, 0, 0, 0, 0, 1)
|| test_gemm_int8(M, N, K, 5.1f, 1, 0, 0, 0, 0, 0, 1)
|| test_gemm_int8(M, N, K, -2.1f, 0, 1, 0, 1, 0, 0, 0)
|| test_gemm_int8(M, N, K, -3.1f, 1, 1, 0, 1, 0, 0, 1)
|| test_gemm_int8_fp16s(M, N, K, 1.f, 0, 1, 0, 0, 0, 0, 0)
|| test_gemm_int8_fp16s(M, N, K, 1.f, 1, 0, 0, 1, 0, 0, 0);
}
static int test_gemm_1(int M, int N, int K, int constant_flag = 0)
{
return 0
|| test_gemm_int8_bias(M, N, K, RandomMat(1), 2.1f, 0.5f, 0, 0, 0, 0, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(1), 2.1f, 0.5f, 0, 0, 1, 1, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(M), 3.1f, 0.6f, 0, 1, 2, 0, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(M), 3.1f, 0.6f, 0, 1, 3, 1, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(1, M), 4.1f, 0.7f, 1, 0, 0, 0, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(1, M), 4.1f, 0.7f, 1, 0, 1, 1, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, M), 5.1f, -0.8f, 1, 1, 2, 0, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, M), 5.1f, -0.8f, 1, 1, 3, 1, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, M), 1.f, 1.f, 1, 1, 0, 0, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, M), 1.f, 1.f, 1, 1, 1, 1, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, 1), 2.1f, -0.5f, 0, 0, 2, 0, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, 1), 2.1f, -0.5f, 0, 0, 3, 1, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, 1), 0.8f, 1.f, 0, 0, 0, 0, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(N), 0.8f, 1.f, 0, 0, 1, 1, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(N), 3.1f, -0.6f, 0, 1, 2, 0, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(N), 3.1f, -0.6f, 0, 1, 3, 1, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(1), 1.7f, -0.4f, 0, 0, 0, 0, 0, 0, 0, false, 1)
|| test_gemm_int8_bias(M, N, K, RandomMat(M), -1.3f, 0.6f, 1, 0, 0, 1, 0, 0, 0, false, 1)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, M), 0.8f, 0.5f, 0, 1, 0, 0, 0, 0, 0, false, 1)
|| test_gemm_int8_bias(M, N, K, RandomMat(1), -2.1f, 0.5f, 0, 0, 0, 0, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(1), -2.1f, 0.5f, 0, 0, 1, 1, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(M), -3.1f, 0.6f, 0, 1, 2, 0, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(M), -3.1f, 0.6f, 0, 1, 3, 1, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(1, M), -4.1f, 0.7f, 1, 0, 0, 0, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(1, M), -4.1f, 0.7f, 1, 0, 1, 1, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, M), -5.1f, -0.8f, 1, 1, 2, 0, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, M), -5.1f, -0.8f, 1, 1, 3, 1, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, M), 1.f, 1.f, 1, 1, 0, 0, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, M), 1.f, 1.f, 1, 1, 1, 1, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, 1), -2.1f, -0.5f, 0, 0, 2, 0, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, 1), -2.1f, -0.5f, 0, 0, 3, 1, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, 1), 0.8f, 1.f, 0, 0, 0, 0, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(N), 0.8f, 1.f, 0, 0, 1, 1, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(N), -3.1f, -0.6f, 0, 1, 2, 0, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(N), -3.1f, -0.6f, 0, 1, 3, 1, 1, 1, 1, false, 0, constant_flag);
}
// bias geometries cover output types, row and column layouts, and rank-three inputs
static int test_gemm_bias_geometry(int M, int N, int K)
{
return 0
|| test_gemm_int8_bias(M, N, K, RandomMat(1), 2.1f, 0.5f, 0, 0, 0, 0, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(1), 2.1f, 0.5f, 0, 0, 1, 1, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(M), 3.1f, 0.6f, 0, 1, 2, 0, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(M), 3.1f, 0.6f, 0, 1, 3, 1, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(1, M), 4.1f, 0.7f, 1, 0, 0, 0, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(1, M), 4.1f, 0.7f, 1, 0, 1, 1, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, M), 5.1f, -0.8f, 1, 1, 2, 0, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, M), 5.1f, -0.8f, 1, 1, 3, 1, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, 1), 2.1f, -0.5f, 0, 0, 2, 0, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, 1), 2.1f, -0.5f, 0, 0, 3, 1, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(1), 1.7f, -0.4f, 0, 0, 0, 0, 0, 0, 0, false, 1)
|| test_gemm_int8_bias(M, N, K, RandomMat(M), -1.3f, 0.6f, 1, 0, 0, 1, 0, 0, 0, false, 1)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, M), 0.8f, 0.5f, 0, 1, 0, 0, 0, 0, 0, false, 1);
}
// dynamic input geometries cover four transpose directions and fp16 storage
static int test_gemm_dynamic_pack_geometry(int M, int N, int K)
{
return 0
|| test_gemm_int8(M, N, K, 2.1f, 0, 1, 0, 0, 0, 0, 0)
|| test_gemm_int8(M, N, K, 3.1f, 1, 1, 0, 0, 0, 0, 0)
|| test_gemm_int8(M, N, K, 4.1f, 0, 0, 0, 0, 0, 0, 1)
|| test_gemm_int8(M, N, K, 5.1f, 1, 0, 0, 0, 0, 0, 1)
|| test_gemm_int8_fp16s(M, N, K, 1.f, 0, 1, 0, 0, 0, 0, 0)
|| test_gemm_int8_fp16s(M, N, K, 1.f, 1, 0, 0, 1, 0, 0, 0);
}
// bias geometries cover scalar, row, column, matrix and rank-three layouts
static int test_gemm_bias_pack_geometry(int M, int N, int K)
{
return 0
|| test_gemm_int8_bias(M, N, K, RandomMat(1), 2.1f, 0.5f, 0, 0, 0, 0, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(M), 3.1f, 0.6f, 0, 1, 2, 0, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(1, M), 4.1f, 0.7f, 1, 0, 0, 0, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, M), 5.1f, -0.8f, 1, 1, 3, 1, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, 1), 2.1f, -0.5f, 0, 0, 3, 1, 0, 0, 0)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, M), 0.8f, 0.5f, 0, 1, 0, 0, 0, 0, 0, false, 1);
}
// original cpu-only constant operands retain every packed geometry
static int test_gemm_constant_operands(int M, int N, int K, int constant_flag)
{
return 0
|| test_gemm_int8(M, N, K, 0.2f, 0, 1, 0, 0, 1, 0, 1, constant_flag)
|| test_gemm_int8(M, N, K, 0.3f, 1, 1, 0, 0, 1, 0, 1, constant_flag)
|| test_gemm_int8(M, N, K, 0.4f, 0, 0, 0, 0, 0, 1, 0, constant_flag)
|| test_gemm_int8(M, N, K, 0.5f, 0, 1, 0, 0, 0, 1, 0, constant_flag)
|| test_gemm_int8(M, N, K, 1.2f, 0, 1, 0, 0, 1, 1, 0, constant_flag)
|| test_gemm_int8(M, N, K, 1.3f, 1, 1, 0, 0, 1, 1, 1, constant_flag)
|| test_gemm_int8(M, N, K, 1.4f, 0, 0, 0, 0, 1, 0, 0, constant_flag)
|| test_gemm_int8(M, N, K, 1.5f, 1, 0, 0, 0, 1, 0, 1, constant_flag)
|| test_gemm_int8(M, N, K, -1.2f, 0, 1, 0, 1, 1, 1, 0, constant_flag)
|| test_gemm_int8(M, N, K, -1.3f, 1, 1, 0, 1, 1, 1, 0, constant_flag)
|| test_gemm_int8(M, N, K, -1.4f, 0, 0, 0, 1, 1, 1, 1, constant_flag)
|| test_gemm_int8(M, N, K, -1.5f, 1, 0, 0, 1, 1, 1, 1, constant_flag)
|| test_gemm_int8(M, N, K, -2.0f, 0, 1, 0, 1, 0, 1, 1, constant_flag)
|| test_gemm_int8(M, N, K, -3.0f, 1, 1, 0, 1, 0, 1, 1, constant_flag)
|| test_gemm_int8(M, N, K, -4.0f, 0, 0, 0, 1, 1, 0, 0, constant_flag)
|| test_gemm_int8(M, N, K, -5.0f, 0, 1, 0, 1, 1, 0, 0, constant_flag)
|| test_gemm_int8(M, N, K, -4.1f, 0, 0, 0, 1, 0, 1, 0, constant_flag)
|| test_gemm_int8(M, N, K, -5.1f, 1, 0, 0, 1, 0, 1, 1, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(1), -2.1f, 0.5f, 0, 0, 0, 0, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(1), -2.1f, 0.5f, 0, 0, 1, 1, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(M), -3.1f, 0.6f, 0, 1, 2, 0, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(M), -3.1f, 0.6f, 0, 1, 3, 1, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(1, M), -4.1f, 0.7f, 1, 0, 0, 0, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(1, M), -4.1f, 0.7f, 1, 0, 1, 1, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, M), -5.1f, -0.8f, 1, 1, 2, 0, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, M), -5.1f, -0.8f, 1, 1, 3, 1, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, M), 1.f, 1.f, 1, 1, 0, 0, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, M), 1.f, 1.f, 1, 1, 1, 1, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, 1), -2.1f, -0.5f, 0, 0, 2, 0, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, 1), -2.1f, -0.5f, 0, 0, 3, 1, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(N, 1), 0.8f, 1.f, 0, 0, 0, 0, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(N), 0.8f, 1.f, 0, 0, 1, 1, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(N), -3.1f, -0.6f, 0, 1, 2, 0, 1, 1, 1, false, 0, constant_flag)
|| test_gemm_int8_bias(M, N, K, RandomMat(N), -3.1f, -0.6f, 0, 1, 3, 1, 1, 1, 1, false, 0, constant_flag);
}
// packed matrix bias with unit beta covers vector blocks and scalar column tails
static int test_gemm_matrix_bias_unity_beta()
{
return 0
|| test_gemm_int8_bias(24, 47, 24, RandomMat(47, 24), 1.f, 1.f, 1, 1, 0, 0, 0, 0, 0)
|| test_gemm_int8_bias(24, 47, 24, RandomMat(47, 24), 1.f, 1.f, 1, 1, 1, 1, 0, 0, 0);
}
#endif // NCNN_INT8
int main()
{
SRAND(7767517);
#if NCNN_INT8
// the flag applies to constant operands; dynamic operands retain every matrix size
int ret = 0
|| test_gemm_0(1, 1, 1)
|| test_gemm_1(1, 1, 1)
|| test_gemm_dynamic_pack_geometry(1, 1, 23)
|| test_gemm_bias_pack_geometry(1, 1, 23)
|| test_gemm_constant_operands(1, 1, 47, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(1, 1, 47)
|| test_gemm_bias_pack_geometry(1, 1, 47)
|| test_gemm_dynamic_pack_geometry(1, 23, 1)
|| test_gemm_bias_pack_geometry(1, 23, 1)
|| test_gemm_dynamic_pack_geometry(23, 1, 1)
|| test_gemm_bias_pack_geometry(23, 1, 1)
|| test_gemm_dynamic_pack_geometry(1, 23, 23)
|| test_gemm_bias_pack_geometry(1, 23, 23)
|| test_gemm_dynamic_pack_geometry(23, 1, 23)
|| test_gemm_bias_pack_geometry(23, 1, 23)
|| test_gemm_dynamic_pack_geometry(1, 31, 1)
|| test_gemm_bias_pack_geometry(1, 31, 1)
|| test_gemm_dynamic_pack_geometry(31, 1, 1)
|| test_gemm_bias_pack_geometry(31, 1, 1)
|| test_gemm_constant_operands(1, 35, 1, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(1, 35, 1)
|| test_gemm_bias_pack_geometry(1, 35, 1)
|| test_gemm_constant_operands(35, 1, 1, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(35, 1, 1)
|| test_gemm_bias_pack_geometry(35, 1, 1)
|| test_gemm_dynamic_pack_geometry(1, 35, 47)
|| test_gemm_bias_pack_geometry(1, 35, 47)
|| test_gemm_dynamic_pack_geometry(35, 1, 47)
|| test_gemm_bias_pack_geometry(35, 1, 47)
|| test_gemm_constant_operands(1, 47, 1, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(1, 47, 1)
|| test_gemm_bias_pack_geometry(1, 47, 1)
|| test_gemm_constant_operands(47, 1, 1, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(47, 1, 1)
|| test_gemm_bias_pack_geometry(47, 1, 1)
|| test_gemm_constant_operands(2, 2, 2, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(2, 2, 2)
|| test_gemm_bias_pack_geometry(2, 2, 2)
|| test_gemm_constant_operands(3, 3, 3, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(3, 3, 3)
|| test_gemm_bias_pack_geometry(3, 3, 3)
|| test_gemm_dynamic_geometry(4, 4, 4)
|| test_gemm_bias_geometry(4, 4, 4)
|| test_gemm_constant_operands(5, 5, 5, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(5, 5, 5)
|| test_gemm_bias_pack_geometry(5, 5, 5)
|| test_gemm_constant_operands(6, 6, 6, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(6, 6, 6)
|| test_gemm_bias_pack_geometry(6, 6, 6)
|| test_gemm_constant_operands(7, 7, 7, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(7, 7, 7)
|| test_gemm_bias_pack_geometry(7, 7, 7)
|| test_gemm_constant_operands(7, 31, 3, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(7, 31, 3)
|| test_gemm_bias_pack_geometry(7, 31, 3)
|| test_gemm_constant_operands(31, 7, 3, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(31, 7, 3)
|| test_gemm_bias_pack_geometry(31, 7, 3)
|| test_gemm_constant_operands(8, 8, 8, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(8, 8, 8)
|| test_gemm_bias_pack_geometry(8, 8, 8)
|| test_gemm_dynamic_pack_geometry(12, 12, 23)
|| test_gemm_bias_pack_geometry(12, 12, 23)
|| test_gemm_dynamic_pack_geometry(12, 23, 12)
|| test_gemm_bias_pack_geometry(12, 23, 12)
|| test_gemm_dynamic_pack_geometry(23, 12, 12)
|| test_gemm_bias_pack_geometry(23, 12, 12)
|| test_gemm_dynamic_pack_geometry(12, 31, 12)
|| test_gemm_bias_pack_geometry(12, 31, 12)
|| test_gemm_dynamic_pack_geometry(31, 12, 12)
|| test_gemm_bias_pack_geometry(31, 12, 12)
|| test_gemm_dynamic_pack_geometry(15, 15, 15)
|| test_gemm_bias_pack_geometry(15, 15, 15)
|| test_gemm_dynamic_geometry(16, 16, 16)
|| test_gemm_bias_geometry(16, 16, 16)
|| test_gemm_constant_operands(19, 44, 7, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(19, 44, 7)
|| test_gemm_bias_pack_geometry(19, 44, 7)
|| test_gemm_constant_operands(44, 19, 7, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(44, 19, 7)
|| test_gemm_bias_pack_geometry(44, 19, 7)
|| test_gemm_dynamic_pack_geometry(20, 28, 7)
|| test_gemm_bias_pack_geometry(20, 28, 7)
|| test_gemm_dynamic_pack_geometry(28, 20, 7)
|| test_gemm_bias_pack_geometry(28, 20, 7)
|| test_gemm_dynamic_pack_geometry(23, 31, 1)
|| test_gemm_bias_pack_geometry(23, 31, 1)
|| test_gemm_dynamic_pack_geometry(31, 23, 1)
|| test_gemm_bias_pack_geometry(31, 23, 1)
|| test_gemm_0(23, 31, 23)
|| test_gemm_1(23, 31, 23)
|| test_gemm_dynamic_pack_geometry(31, 23, 23)
|| test_gemm_bias_pack_geometry(31, 23, 23)
|| test_gemm_constant_operands(24, 24, 47, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(24, 24, 47)
|| test_gemm_bias_pack_geometry(24, 24, 47)
|| test_gemm_constant_operands(24, 35, 24, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(24, 35, 24)
|| test_gemm_bias_pack_geometry(24, 35, 24)
|| test_gemm_constant_operands(35, 24, 24, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(35, 24, 24)
|| test_gemm_bias_pack_geometry(35, 24, 24)
|| test_gemm_constant_operands(24, 47, 24, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(24, 47, 24)
|| test_gemm_bias_pack_geometry(24, 47, 24)
|| test_gemm_constant_operands(47, 24, 24, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(47, 24, 24)
|| test_gemm_bias_pack_geometry(47, 24, 24)
|| test_gemm_constant_operands(31, 31, 31, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(31, 31, 31)
|| test_gemm_bias_pack_geometry(31, 31, 31)
|| test_gemm_constant_operands(32, 32, 9, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(32, 32, 9)
|| test_gemm_bias_pack_geometry(32, 32, 9)
|| test_gemm_dynamic_pack_geometry(35, 47, 48)
|| test_gemm_bias_pack_geometry(35, 47, 48)
|| test_gemm_0(47, 35, 48)
|| test_gemm_1(47, 35, 48)
|| test_gemm_0(35, 48, 47)
|| test_gemm_1(35, 48, 47)
|| test_gemm_0(48, 35, 47)
|| test_gemm_bias_geometry(48, 35, 47)
|| test_gemm_constant_operands(40, 40, 40, TEST_LAYER_DISABLE_GPU_TESTING)
|| test_gemm_dynamic_pack_geometry(40, 40, 40)
|| test_gemm_bias_pack_geometry(40, 40, 40)
|| test_gemm_dynamic_geometry(47, 48, 47)
|| test_gemm_bias_geometry(47, 48, 47)
|| test_gemm_dynamic_geometry(48, 47, 47)
|| test_gemm_bias_pack_geometry(48, 47, 47)
|| test_gemm_matrix_bias_unity_beta();
if (ret != 0)
return ret;
if (test_gemm_int8_bf16s(12, 23, 12, 1.f, 0, 1, 0, 0, 0, 0, 0)
|| test_gemm_int8_bf16s(12, 23, 12, 1.f, 1, 0, 0, 1, 0, 0, 0)
|| test_gemm_int8_bias(12, 23, 12, RandomMat(23), 1.7f, -0.4f, 0, 1, 0, 1, 0, 0, 0, true))
{
return -1;
}
#else
// test nothing for non-int8 build
#endif
return 0;
}