Skip to content

Commit

Permalink
bring test cast packing
Browse files Browse the repository at this point in the history
  • Loading branch information
nihui committed Jan 3, 2024
1 parent 0d50b49 commit 624d46d
Show file tree
Hide file tree
Showing 3 changed files with 57 additions and 81 deletions.
4 changes: 2 additions & 2 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ ncnn_add_layer_test(BatchNorm)
ncnn_add_layer_test(Bias)
ncnn_add_layer_test(BinaryOp)
ncnn_add_layer_test(BNLL)
# ncnn_add_layer_test(Cast)
ncnn_add_layer_test(Cast)
ncnn_add_layer_test(CELU)
ncnn_add_layer_test(Clip)
ncnn_add_layer_test(Concat)
Expand Down Expand Up @@ -125,7 +125,7 @@ ncnn_add_layer_test(Mish)
ncnn_add_layer_test(MultiHeadAttention)
ncnn_add_layer_test(Noop)
ncnn_add_layer_test(Normalize)
# ncnn_add_layer_test(Packing)
ncnn_add_layer_test(Packing)
ncnn_add_layer_test(Padding)
ncnn_add_layer_test(Permute)
ncnn_add_layer_test(PixelShuffle)
Expand Down
27 changes: 12 additions & 15 deletions tests/test_cast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,6 @@ static int cast_cpu_naive(const ncnn::Mat& a, ncnn::Mat& b, int type_from, int t

ncnn::Option opt;
opt.num_threads = 1;
opt.use_vulkan_compute = false;
opt.use_int8_inference = false;
opt.use_packing_layout = false;

ncnn::Layer* op = ncnn::create_layer_naive("Cast");

Expand Down Expand Up @@ -61,7 +58,7 @@ static int test_cast_cpu(const ncnn::Mat& a, int type_from, int type_to)
opt.use_int8_inference = false;
opt.use_packing_layout = false;

ncnn::Layer* op = ncnn::create_layer("Cast");
ncnn::Layer* op = ncnn::create_layer_cpu("Cast");

op->load_param(pd);

Expand All @@ -75,7 +72,7 @@ static int test_cast_cpu(const ncnn::Mat& a, int type_from, int type_to)
cast_cpu_naive(a, a_fp16, 1, type_from);

ncnn::Mat b;
((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
cast_cpu_naive(a_fp16, b, type_from, type_to);

ncnn::Mat c;
op->forward(a_fp16, c, opt);
Expand Down Expand Up @@ -106,7 +103,7 @@ static int test_cast_cpu_packed(const ncnn::Mat& a, int type_from, int type_to)
opt.use_vulkan_compute = false;
opt.use_packing_layout = false;

ncnn::Layer* op = ncnn::create_layer("Cast");
ncnn::Layer* op = ncnn::create_layer_cpu("Cast");

op->load_param(pd);

Expand All @@ -120,7 +117,7 @@ static int test_cast_cpu_packed(const ncnn::Mat& a, int type_from, int type_to)
cast_cpu_naive(a, a_fp16, 1, type_from);

ncnn::Mat b;
((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
cast_cpu_naive(a_fp16, b, type_from, type_to);

ncnn::Mat a4;
ncnn::convert_packing(a, a4, 4, opt);
Expand Down Expand Up @@ -179,7 +176,7 @@ static int test_cast_gpu_fp16p(const ncnn::Mat& a, int type_from, int type_to)
if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;

ncnn::Layer* op = ncnn::create_layer("Cast");
ncnn::Layer* op = ncnn::create_layer_vulkan("Cast");

op->vkdev = vkdev;

Expand All @@ -202,7 +199,7 @@ static int test_cast_gpu_fp16p(const ncnn::Mat& a, int type_from, int type_to)
}

ncnn::Mat b;
((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
cast_cpu_naive(a_fp16, b, type_from, type_to);

ncnn::Mat d;

Expand Down Expand Up @@ -295,7 +292,7 @@ static int test_cast_gpu_fp16p_pack8(const ncnn::Mat& a, int type_from, int type
if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;

ncnn::Layer* op = ncnn::create_layer("Cast");
ncnn::Layer* op = ncnn::create_layer_vulkan("Cast");

op->vkdev = vkdev;

Expand All @@ -318,7 +315,7 @@ static int test_cast_gpu_fp16p_pack8(const ncnn::Mat& a, int type_from, int type
}

ncnn::Mat b;
((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
cast_cpu_naive(a_fp16, b, type_from, type_to);

ncnn::Mat d;

Expand Down Expand Up @@ -412,7 +409,7 @@ static int test_cast_gpu_image_fp16p(const ncnn::Mat& a, int type_from, int type
if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;

ncnn::Layer* op = ncnn::create_layer("Cast");
ncnn::Layer* op = ncnn::create_layer_vulkan("Cast");

op->vkdev = vkdev;

Expand All @@ -435,7 +432,7 @@ static int test_cast_gpu_image_fp16p(const ncnn::Mat& a, int type_from, int type
}

ncnn::Mat b;
((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
cast_cpu_naive(a_fp16, b, type_from, type_to);

ncnn::Mat d;

Expand Down Expand Up @@ -528,7 +525,7 @@ static int test_cast_gpu_image_fp16p_pack8(const ncnn::Mat& a, int type_from, in
if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;

ncnn::Layer* op = ncnn::create_layer("Cast");
ncnn::Layer* op = ncnn::create_layer_vulkan("Cast");

op->vkdev = vkdev;

Expand All @@ -551,7 +548,7 @@ static int test_cast_gpu_image_fp16p_pack8(const ncnn::Mat& a, int type_from, in
}

ncnn::Mat b;
((ncnn::Cast*)op)->ncnn::Cast::forward(a_fp16, b, opt);
cast_cpu_naive(a_fp16, b, type_from, type_to);

ncnn::Mat d;

Expand Down
107 changes: 43 additions & 64 deletions tests/test_packing.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,35 @@

#include "testutil.h"

static int packing_cpu_naive(const ncnn::Mat& a, ncnn::Mat& b, int out_elempack)
{
ncnn::ParamDict pd;
pd.set(0, out_elempack);

std::vector<ncnn::Mat> weights(0);

ncnn::Option opt;
opt.num_threads = 1;

ncnn::Layer* op = ncnn::create_layer_naive("Packing");

op->load_param(pd);

ncnn::ModelBinFromMatArray mb(weights.data());

op->load_model(mb);

op->create_pipeline(opt);

op->forward(a, b, opt);

op->destroy_pipeline(opt);

delete op;

return 0;
}

static int test_packing_cpu_fp32(const ncnn::Mat& a, int in_elempack, int out_elempack)
{
ncnn::ParamDict pd;
Expand All @@ -29,7 +58,7 @@ static int test_packing_cpu_fp32(const ncnn::Mat& a, int in_elempack, int out_el
opt.use_fp16_arithmetic = false;
opt.use_packing_layout = false;

ncnn::Layer* op = ncnn::create_layer("Packing");
ncnn::Layer* op = ncnn::create_layer_cpu("Packing");

op->load_param(pd);

Expand All @@ -43,7 +72,7 @@ static int test_packing_cpu_fp32(const ncnn::Mat& a, int in_elempack, int out_el
ncnn::convert_packing(a, ap, in_elempack, opt);

ncnn::Mat b;
((ncnn::Packing*)op)->ncnn::Packing::forward(ap, b, opt);
packing_cpu_naive(ap, b, out_elempack);

ncnn::Mat c;
op->forward(ap, c, opt);
Expand Down Expand Up @@ -76,7 +105,7 @@ static int test_packing_cpu_fp16(const ncnn::Mat& a, int in_elempack, int out_el
opt.use_fp16_arithmetic = true;
opt.use_packing_layout = false;

ncnn::Layer* op = ncnn::create_layer("Packing");
ncnn::Layer* op = ncnn::create_layer_cpu("Packing");

if (!op->support_fp16_storage)
{
Expand All @@ -99,7 +128,7 @@ static int test_packing_cpu_fp16(const ncnn::Mat& a, int in_elempack, int out_el
ncnn::convert_packing(a16, ap, in_elempack, opt);

ncnn::Mat b;
((ncnn::Packing*)op)->ncnn::Packing::forward(ap, b, opt);
packing_cpu_naive(ap, b, out_elempack);

ncnn::Mat c;
op->forward(ap, c, opt);
Expand Down Expand Up @@ -135,7 +164,7 @@ static int test_packing_cpu_int8(const ncnn::Mat& a, int in_elempack, int out_el
opt.use_fp16_arithmetic = false;
opt.use_packing_layout = false;

ncnn::Layer* op = ncnn::create_layer("Packing");
ncnn::Layer* op = ncnn::create_layer_cpu("Packing");

op->load_param(pd);

Expand All @@ -155,7 +184,7 @@ static int test_packing_cpu_int8(const ncnn::Mat& a, int in_elempack, int out_el
ncnn::convert_packing(a8, ap, in_elempack, opt);

ncnn::Mat b;
((ncnn::Packing*)op)->ncnn::Packing::forward(ap, b, opt);
packing_cpu_naive(ap, b, out_elempack);

ncnn::Mat c;
op->forward(ap, c, opt);
Expand Down Expand Up @@ -225,7 +254,7 @@ static int test_packing_gpu_buffer(const ncnn::Mat& a, int in_elempack, int out_
if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;

ncnn::Layer* op = ncnn::create_layer("Packing");
ncnn::Layer* op = ncnn::create_layer_vulkan("Packing");

op->vkdev = vkdev;

Expand All @@ -241,7 +270,7 @@ static int test_packing_gpu_buffer(const ncnn::Mat& a, int in_elempack, int out_
ncnn::convert_packing(a, ap, in_elempack, opt);

ncnn::Mat b;
((ncnn::Packing*)op)->ncnn::Packing::forward(ap, b, opt);
packing_cpu_naive(ap, b, out_elempack);

ncnn::Mat d;

Expand Down Expand Up @@ -312,7 +341,7 @@ static int test_packing_gpu_image(const ncnn::Mat& a, int in_elempack, int out_e
if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;

ncnn::Layer* op = ncnn::create_layer("Packing");
ncnn::Layer* op = ncnn::create_layer_vulkan("Packing");

op->vkdev = vkdev;

Expand All @@ -328,7 +357,7 @@ static int test_packing_gpu_image(const ncnn::Mat& a, int in_elempack, int out_e
ncnn::convert_packing(a, ap, in_elempack, opt);

ncnn::Mat b;
((ncnn::Packing*)op)->ncnn::Packing::forward(ap, b, opt);
packing_cpu_naive(ap, b, out_elempack);

ncnn::Mat d;

Expand Down Expand Up @@ -365,15 +394,6 @@ static int test_packing_gpu_image(const ncnn::Mat& a, int in_elempack, int out_e

static int test_packing_gpu_buffer2image(const ncnn::Mat& a, int in_elempack, int out_elempack)
{
ncnn::ParamDict pd;
pd.set(0, out_elempack);
pd.set(2, 1); // cast_type_from
pd.set(3, 1); // cast_type_to
pd.set(4, 0); // storage_type_from
pd.set(5, 1); // storage_type_to

std::vector<ncnn::Mat> weights(0);

ncnn::Option opt;
opt.num_threads = 1;
opt.use_vulkan_compute = true;
Expand All @@ -399,23 +419,11 @@ static int test_packing_gpu_buffer2image(const ncnn::Mat& a, int in_elempack, in
if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;

ncnn::Packing_vulkan* op = new ncnn::Packing_vulkan;

op->vkdev = vkdev;

op->load_param(pd);

ncnn::ModelBinFromMatArray mb(weights.data());

op->load_model(mb);

op->create_pipeline(opt);

ncnn::Mat ap;
ncnn::convert_packing(a, ap, in_elempack, opt);

ncnn::Mat b;
((ncnn::Packing*)op)->ncnn::Packing::forward(ap, b, opt);
packing_cpu_naive(ap, b, out_elempack);

ncnn::Mat d;

Expand All @@ -427,17 +435,13 @@ static int test_packing_gpu_buffer2image(const ncnn::Mat& a, int in_elempack, in
cmd.record_clone(ap, a_gpu, opt);

ncnn::VkImageMat d_gpu;
op->forward(a_gpu, d_gpu, cmd, opt);
vkdev->convert_packing(a_gpu, d_gpu, out_elempack, cmd, opt);

// download
cmd.record_clone(d_gpu, d, opt);

cmd.submit_and_wait();

op->destroy_pipeline(opt);

delete op;

vkdev->reclaim_blob_allocator(blob_vkallocator);
vkdev->reclaim_staging_allocator(staging_vkallocator);

Expand All @@ -452,15 +456,6 @@ static int test_packing_gpu_buffer2image(const ncnn::Mat& a, int in_elempack, in

static int test_packing_gpu_image2buffer(const ncnn::Mat& a, int in_elempack, int out_elempack)
{
ncnn::ParamDict pd;
pd.set(0, out_elempack);
pd.set(2, 1); // cast_type_from
pd.set(3, 1); // cast_type_to
pd.set(4, 1); // storage_type_from
pd.set(5, 0); // storage_type_to

std::vector<ncnn::Mat> weights(0);

ncnn::Option opt;
opt.num_threads = 1;
opt.use_vulkan_compute = true;
Expand All @@ -486,23 +481,11 @@ static int test_packing_gpu_image2buffer(const ncnn::Mat& a, int in_elempack, in
if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;

ncnn::Packing_vulkan* op = new ncnn::Packing_vulkan;

op->vkdev = vkdev;

op->load_param(pd);

ncnn::ModelBinFromMatArray mb(weights.data());

op->load_model(mb);

op->create_pipeline(opt);

ncnn::Mat ap;
ncnn::convert_packing(a, ap, in_elempack, opt);

ncnn::Mat b;
((ncnn::Packing*)op)->ncnn::Packing::forward(ap, b, opt);
packing_cpu_naive(ap, b, out_elempack);

ncnn::Mat d;

Expand All @@ -514,17 +497,13 @@ static int test_packing_gpu_image2buffer(const ncnn::Mat& a, int in_elempack, in
cmd.record_clone(ap, a_gpu, opt);

ncnn::VkMat d_gpu;
op->forward(a_gpu, d_gpu, cmd, opt);
vkdev->convert_packing(a_gpu, d_gpu, out_elempack, cmd, opt);

// download
cmd.record_clone(d_gpu, d, opt);

cmd.submit_and_wait();

op->destroy_pipeline(opt);

delete op;

vkdev->reclaim_blob_allocator(blob_vkallocator);
vkdev->reclaim_staging_allocator(staging_vkallocator);

Expand Down

0 comments on commit 624d46d

Please sign in to comment.