From 85d5e5d3e45cb41822d55b3ee7055214ea63e879 Mon Sep 17 00:00:00 2001 From: nihuini Date: Sun, 5 Apr 2020 17:49:27 +0800 Subject: [PATCH] fix innerproduct vulkan pack8 and arm neon, disable packing_layout for int8 test --- src/layer/arm/innerproduct_arm.cpp | 1 + .../vulkan/shader/innerproduct_pack1to8.comp | 2 +- .../vulkan/shader/innerproduct_pack4to8.comp | 2 +- .../vulkan/shader/innerproduct_pack8to4.comp | 2 +- tests/test_innerproduct.cpp | 93 ++++++++++++------- tests/testutil.h | 7 +- 6 files changed, 69 insertions(+), 38 deletions(-) diff --git a/src/layer/arm/innerproduct_arm.cpp b/src/layer/arm/innerproduct_arm.cpp index 04924e4b6..5f8dd0276 100644 --- a/src/layer/arm/innerproduct_arm.cpp +++ b/src/layer/arm/innerproduct_arm.cpp @@ -106,6 +106,7 @@ int InnerProduct_arm::forward(const Mat& bottom_blob, Mat& top_blob, const Optio // pack1 { bottom_blob_flattened.w *= bottom_blob_flattened.elempack; + bottom_blob_flattened.cstep = bottom_blob_flattened.w; bottom_blob_flattened.elemsize = 4u; bottom_blob_flattened.elempack = 1; } diff --git a/src/layer/vulkan/shader/innerproduct_pack1to8.comp b/src/layer/vulkan/shader/innerproduct_pack1to8.comp index 5cf41ad30..30d2b89e8 100644 --- a/src/layer/vulkan/shader/innerproduct_pack1to8.comp +++ b/src/layer/vulkan/shader/innerproduct_pack1to8.comp @@ -77,7 +77,7 @@ void main() if (bias_term == 1) { - sum = buffer_ld8(bias_data, gz); + sum = buffer_ld8(bias_data, gx); } else { diff --git a/src/layer/vulkan/shader/innerproduct_pack4to8.comp b/src/layer/vulkan/shader/innerproduct_pack4to8.comp index 6edd17157..c7b51f514 100644 --- a/src/layer/vulkan/shader/innerproduct_pack4to8.comp +++ b/src/layer/vulkan/shader/innerproduct_pack4to8.comp @@ -77,7 +77,7 @@ void main() if (bias_term == 1) { - sum = buffer_ld8(bias_data, gz); + sum = buffer_ld8(bias_data, gx); } else { diff --git a/src/layer/vulkan/shader/innerproduct_pack8to4.comp b/src/layer/vulkan/shader/innerproduct_pack8to4.comp index 64446f889..59b5f2016 100644 --- a/src/layer/vulkan/shader/innerproduct_pack8to4.comp +++ b/src/layer/vulkan/shader/innerproduct_pack8to4.comp @@ -77,7 +77,7 @@ void main() if (bias_term == 1) { - sum = buffer_ld4(bias_data, gz); + sum = buffer_ld4(bias_data, gx); } else { diff --git a/tests/test_innerproduct.cpp b/tests/test_innerproduct.cpp index 344aec479..a8cfaa8a7 100644 --- a/tests/test_innerproduct.cpp +++ b/tests/test_innerproduct.cpp @@ -16,17 +16,15 @@ #include "layer/innerproduct.h" -static int test_innerproduct(int w, int h, int c, int outch, int bias) +static int test_innerproduct(const ncnn::Mat& a, int outch, int bias) { - ncnn::Mat a = RandomMat(w, h, c); - ncnn::ParamDict pd; pd.set(0, outch);// num_output pd.set(1, bias);// bias_term - pd.set(2, outch*w*h*c); + pd.set(2, outch*a.w*a.h*a.c); std::vector weights(bias ? 2 : 1); - weights[0] = RandomMat(outch*w*h*c); + weights[0] = RandomMat(outch*a.w*a.h*a.c); if (bias) weights[1] = RandomMat(outch); @@ -43,7 +41,7 @@ static int test_innerproduct(int w, int h, int c, int outch, int bias) int ret = test_layer("InnerProduct", pd, weights, opt, a); if (ret != 0) { - fprintf(stderr, "test_innerproduct failed w=%d h=%d c=%d outch=%d bias=%d\n", w, h, c, outch, bias); + fprintf(stderr, "test_innerproduct failed a.dims=%d a=(%d %d %d) outch=%d bias=%d\n", a.dims, a.w, a.h, a.c, outch, bias); } return ret; @@ -52,29 +50,55 @@ static int test_innerproduct(int w, int h, int c, int outch, int bias) static int test_innerproduct_0() { return 0 - || test_innerproduct(7, 3, 1, 1, 1) - || test_innerproduct(7, 3, 2, 2, 1) - || test_innerproduct(7, 3, 3, 3, 1) - || test_innerproduct(7, 3, 4, 4, 1) - || test_innerproduct(7, 3, 7, 7, 1) - || test_innerproduct(7, 3, 8, 8, 1) - || test_innerproduct(7, 3, 15, 15, 1) - || test_innerproduct(7, 3, 16, 16, 1) + || test_innerproduct(RandomMat(1, 3, 1), 1, 1) + || test_innerproduct(RandomMat(3, 2, 2), 2, 1) + || test_innerproduct(RandomMat(9, 3, 8), 7, 1) + || test_innerproduct(RandomMat(2, 2, 8), 8, 1) + || test_innerproduct(RandomMat(4, 3, 15), 8, 1) + || test_innerproduct(RandomMat(6, 2, 16), 16, 1) + || test_innerproduct(RandomMat(6, 2, 16), 7, 1) + || test_innerproduct(RandomMat(6, 2, 5), 16, 1) + ; +} + +static int test_innerproduct_1() +{ + return 0 + || test_innerproduct(RandomMat(1, 1), 1, 1) + || test_innerproduct(RandomMat(3, 2), 2, 1) + || test_innerproduct(RandomMat(9, 8), 7, 1) + || test_innerproduct(RandomMat(2, 8), 8, 1) + || test_innerproduct(RandomMat(4, 15), 8, 1) + || test_innerproduct(RandomMat(6, 16), 16, 1) + || test_innerproduct(RandomMat(6, 16), 7, 1) + || test_innerproduct(RandomMat(6, 5), 16, 1) ; } -static int test_innerproduct_int8(int w, int h, int c, int outch, int bias) +static int test_innerproduct_2() { - ncnn::Mat a = RandomMat(w, h, c); + return 0 + || test_innerproduct(RandomMat(1), 1, 1) + || test_innerproduct(RandomMat(2), 2, 1) + || test_innerproduct(RandomMat(8), 7, 1) + || test_innerproduct(RandomMat(8), 8, 1) + || test_innerproduct(RandomMat(15), 8, 1) + || test_innerproduct(RandomMat(16), 16, 1) + || test_innerproduct(RandomMat(16), 7, 1) + || test_innerproduct(RandomMat(5), 16, 1) + ; +} +static int test_innerproduct_int8(const ncnn::Mat& a, int outch, int bias) +{ ncnn::ParamDict pd; pd.set(0, outch);// num_output pd.set(1, bias);// bias_term - pd.set(2, outch*w*h*c); + pd.set(2, outch*a.w*a.h*a.c); pd.set(8, 1);// int8_scale_term std::vector weights(bias ? 4 : 3); - weights[0] = RandomMat(outch*w*h*c); + weights[0] = RandomMat(outch*a.w*a.h*a.c); if (bias) { weights[1] = RandomMat(outch); @@ -100,26 +124,26 @@ static int test_innerproduct_int8(int w, int h, int c, int outch, int bias) int ret = test_layer("InnerProduct", pd, weights, opt, a); if (ret != 0) { - fprintf(stderr, "test_innerproduct_int8 failed w=%d h=%d c=%d outch=%d bias=%d\n", w, h, c, outch, bias); + fprintf(stderr, "test_innerproduct_int8 failed a.dims=%d a=(%d %d %d) outch=%d bias=%d\n", a.dims, a.w, a.h, a.c, outch, bias); } return 0; } -static int test_innerproduct_1() +static int test_innerproduct_3() { return 0 - || test_innerproduct_int8(7, 3, 1, 1, 1) - || test_innerproduct_int8(7, 3, 2, 2, 1) - || test_innerproduct_int8(7, 3, 3, 3, 1) - || test_innerproduct_int8(7, 3, 3, 12, 1) - || test_innerproduct_int8(7, 3, 4, 4, 1) - || test_innerproduct_int8(7, 3, 7, 7, 1) - || test_innerproduct_int8(7, 3, 8, 3, 1) - || test_innerproduct_int8(7, 3, 8, 8, 1) - || test_innerproduct_int8(7, 3, 15, 15, 1) - || test_innerproduct_int8(7, 3, 16, 4, 1) - || test_innerproduct_int8(7, 3, 16, 16, 1) + || test_innerproduct_int8(RandomMat(1, 3, 1), 1, 1) + || test_innerproduct_int8(RandomMat(3, 2, 2), 2, 1) + || test_innerproduct_int8(RandomMat(5, 3, 3), 3, 1) + || test_innerproduct_int8(RandomMat(7, 2, 3), 12, 1) + || test_innerproduct_int8(RandomMat(9, 3, 4), 4, 1) + || test_innerproduct_int8(RandomMat(2, 2, 7), 7, 1) + || test_innerproduct_int8(RandomMat(4, 3, 8), 3, 1) + || test_innerproduct_int8(RandomMat(6, 2, 8), 8, 1) + || test_innerproduct_int8(RandomMat(8, 3, 15), 15, 1) + || test_innerproduct_int8(RandomMat(7, 2, 16), 4, 1) + || test_innerproduct_int8(RandomMat(6, 3, 16), 16, 1) ; } @@ -127,5 +151,10 @@ int main() { SRAND(7767517); - return test_innerproduct_0() || test_innerproduct_1(); + return 0 + || test_innerproduct_0() + || test_innerproduct_1() + || test_innerproduct_2() + || test_innerproduct_3() + ; } diff --git a/tests/testutil.h b/tests/testutil.h index f58f8a255..41720e9e6 100644 --- a/tests/testutil.h +++ b/tests/testutil.h @@ -215,6 +215,7 @@ int test_layer(int typeindex, const ncnn::ParamDict& pd, const std::vectorsupport_bf16_storage) opt.use_bf16_storage = false; if (opt.use_int8_inference) opt.use_bf16_storage = false; + if (opt.use_int8_inference) opt.use_packing_layout = false; #if NCNN_VULKAN ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device(); @@ -467,6 +468,7 @@ int test_layer(int typeindex, const ncnn::ParamDict& pd, const std::vectorsupport_bf16_storage) opt.use_bf16_storage = false; if (opt.use_int8_inference) opt.use_bf16_storage = false; + if (opt.use_int8_inference) opt.use_packing_layout = false; #if NCNN_VULKAN ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device(); @@ -732,8 +734,7 @@ int test_layer(const char* layer_type, const ncnn::ParamDict& pd, const std::vec int ret = test_layer(ncnn::layer_to_index(layer_type), pd, weights_fp16, opt, a_fp16, top_blob_count, top_shapes, epsilon_fp16, func); if (ret != 0) { - fprintf(stderr, "test_layer %s failed use_packing_layout=%d use_bf16_storage=%d\n", layer_type, opt.use_packing_layout, opt.use_bf16_storage); - return ret; + fprintf(stderr, "test_layer %s failed use_packing_layout=%d use_fp16_packed=%d use_shader_pack8=%d use_bf16_storage=%d\n", layer_type, opt.use_packing_layout, opt.use_fp16_packed, opt.use_shader_pack8, opt.use_bf16_storage); } } @@ -810,7 +811,7 @@ int test_layer(const char* layer_type, const ncnn::ParamDict& pd, const std::vec int ret = test_layer(ncnn::layer_to_index(layer_type), pd, weights_fp16, opt, a_fp16, top_shape, epsilon_fp16, func); if (ret != 0) { - fprintf(stderr, "test_layer %s failed use_packing_layout=%d use_bf16_storage=%d\n", layer_type, opt.use_packing_layout, opt.use_bf16_storage); + fprintf(stderr, "test_layer %s failed use_packing_layout=%d use_fp16_packed=%d use_shader_pack8=%d use_bf16_storage=%d\n", layer_type, opt.use_packing_layout, opt.use_fp16_packed, opt.use_shader_pack8, opt.use_bf16_storage); return ret; } }