|
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137 |
- /**
- * \file dnn/test/cuda/local_share.cpp
- * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
- *
- * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
- *
- * Unless required by applicable law or agreed to in writing,
- * software distributed under the License is distributed on an
- * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- */
-
- #include "megdnn/oprs/nn.h"
-
- #include "src/common/utils.h"
- #include "test/common/checker.h"
- #include "test/common/convolution.h"
- #include "test/common/tensor.h"
- #include "test/common/workspace_wrapper.h"
- #include "test/cuda/benchmark.h"
- #include "test/cuda/fixture.h"
- #include "test/cuda/utils.h"
-
- using namespace megdnn;
- using namespace test;
-
- namespace {
- struct LocalShareArgs {
- size_t b, c, f, p, s, h, w, sg;
- };
-
- std::vector<LocalShareArgs> get_local_share_conv_1x1_args_lar_bs() {
- std::vector<LocalShareArgs> ret;
- // clang-format off
- for (size_t b : {32, 64}) {
- for (size_t c : {32, 16, 8}) {
- for (size_t f : {1}) {
- for (int p : {0}) {
- for (size_t s : {1, 2}) {
- for (size_t h : {8, 16}) {
- for (size_t w : {2, 3, 4, 5, 6, 7, 8, 9, 10, 16, 24, 32, 33}) {
- for (size_t sg : {3, 2}) {
- size_t ho = infer_conv_shape(h, f, s, p);
- size_t wo = infer_conv_shape(w, f, s, p);
- if (ho % sg != 0 || wo % sg != 0)
- continue;
- ret.emplace_back(LocalShareArgs{b, c, f, static_cast<size_t>(p),
- s, h, w, sg});
- } } } } } } } }
- // clang-format on
- return ret;
- }
-
- std::vector<LocalShareArgs> get_local_share_conv_3x3_args_lar_bs() {
- std::vector<LocalShareArgs> ret;
- // clang-format off
- for (size_t b : {32, 64}) {
- for (size_t c : {32, 16, 8}) {
- for (size_t f : {3}) {
- for (int p : {static_cast<int>(f / 2), 0}) {
- for (size_t s : {1, 2}) {
- for (size_t h : {8, 16}) {
- for (size_t w : {3, 4, 5, 6, 7, 8, 9, 10, 16, 24, 32, 33}) {
- for (size_t sg : {3, 2}) {
- size_t ho = infer_conv_shape(h, f, s, p);
- size_t wo = infer_conv_shape(w, f, s, p);
- if (ho % sg != 0 || wo % sg != 0)
- continue;
- ret.emplace_back(LocalShareArgs{b, c, f, static_cast<size_t>(p),
- s, h, w, sg});
- } } } } } } } }
- // clang-format on
- return ret;
- }
-
- std::vector<LocalShareArgs> get_local_share_conv_5x5_args_lar_bs() {
- std::vector<LocalShareArgs> ret;
- // clang-format off
- for (size_t b : {32, 64}) {
- for (size_t c : {32, 16, 8}) {
- for (size_t f : {5}) {
- for (int p : {static_cast<int>(f / 2), 0}) {
- for (size_t s : {1, 2}) {
- for (size_t h : {8, 16}) {
- for (size_t w : {8, 9, 10, 16, 24, 32, 33}) {
- for (size_t sg : {3, 2}) {
- size_t ho = infer_conv_shape(h, f, s, p);
- size_t wo = infer_conv_shape(w, f, s, p);
- if (ho % sg != 0 || wo % sg != 0)
- continue;
- ret.emplace_back(LocalShareArgs{b, c, f, static_cast<size_t>(p), s,
- h, w, sg});
- } } } } } } } }
- // clang-format on
- return ret;
- }
-
- std::vector<LocalShareArgs> get_local_share_conv_7x7_args_lar_bs() {
- std::vector<LocalShareArgs> ret;
- // clang-format off
- for (size_t b : {32, 64}) {
- for (size_t c : {32, 16, 8}) {
- for (size_t f : {7}) {
- for (int p : {static_cast<int>(f / 2), 0}) {
- for (size_t s : {1, 2}) {
- for (size_t h : {8, 16}) {
- for (size_t w : {8, 9, 10, 16, 24, 32, 33}) {
- for (size_t sg : {3, 2}) {
- size_t ho = infer_conv_shape(h, f, s, p);
- size_t wo = infer_conv_shape(w, f, s, p);
- if (ho % sg != 0 || wo % sg != 0)
- continue;
- ret.emplace_back(LocalShareArgs{b, c, f, static_cast<size_t>(p), s,
- h, w, sg});
- } } } } } } } }
- // clang-format on
- return ret;
- }
-
- std::vector<LocalShareArgs> get_local_share_conv_small_image(size_t kernel_size) {
- size_t f = kernel_size;
- std::vector<LocalShareArgs> ret;
- // clang-format off
- for (size_t b : {8, 16, 32, 48, 64}) {
- for (size_t c : {8, 16, 32, 48, 64, 96, 128}) {
- for (int p : {static_cast<int>(f / 2), 0}) {
- for (size_t s : {1, 2}) {
- for (size_t h : {12}) {
- for (size_t w : {12}) {
- for (size_t sg : {3, 2}) {
- size_t ho = infer_conv_shape(h, f, s, p);
- size_t wo = infer_conv_shape(w, f, s, p);
- if (ho % sg != 0 || wo % sg != 0)
- continue;
- ret.emplace_back(LocalShareArgs{b, c, f, static_cast<size_t>(p), s,
- h, w, sg});
- } } } } } } }
- // clang-format on
- return ret;
- }
-
- std::vector<LocalShareArgs> get_local_share_conv_small_image() {
- std::vector<LocalShareArgs> ret = get_local_share_conv_small_image(3);
- auto ret1 = get_local_share_conv_small_image(5);
- auto ret2 = get_local_share_conv_small_image(7);
- ret.insert(ret.begin(), ret1.begin(), ret1.end());
- ret.insert(ret.begin(), ret2.begin(), ret2.end());
- return ret;
- }
-
- void test_local_share_bwd_data_implicit_gemm(size_t kernel_size, Handle* handle) {
- Checker<LocalShareBackwardData> checker(handle);
- bool require_algo = false;
- checker.set_before_exec_callback(AlgoChecker<LocalShareBackwardData>(
- "LOCAL_SHARE_IMPLICIT_GEMM", &require_algo));
- using Param = LocalShare::Param;
- ConstValue const_0{0};
- auto args = get_local_share_conv_small_image(kernel_size);
- for (auto&& arg : args) {
- static_cast<void>(arg);
- size_t b = arg.b, c = arg.c, f = arg.f, p = arg.p, s = arg.s, h = arg.h,
- w = arg.w, sg = arg.sg;
- size_t ho = infer_conv_shape(h, f, s, p), wo = infer_conv_shape(w, f, s, p);
- Param param;
- param.stride_h = param.stride_w = s;
- param.pad_h = param.pad_w = p;
- param.spatial_groups_h = param.spatial_groups_w = sg;
- checker.set_param(param);
- checker.set_rng(2, &const_0);
- TensorShape diff{b, c, ho, wo}, filter{sg, sg, 4, f, f, c}, grad{b, 4, h, w};
- checker.execs({filter, diff, grad});
- diff = TensorShape{b, c, ho, wo}, filter = TensorShape{sg, sg, 8, f, f, c};
- grad = {b, 8, h, w};
- checker.exec({filter, diff, grad});
- }
- }
- } // namespace
-
- TEST_F(CUDA, LOCAL_SHARE_FORWARD_1x1_LAR_BS) {
- require_compute_capability(6, 0);
- Checker<LocalShare> checker(handle_cuda());
- bool require_algo = false;
- checker.set_before_exec_callback(AlgoChecker<LocalShare>(
- "LOCAL_SHARE_CHWN_BATCH_SIZE_AWARE", &require_algo));
- using Param = LocalShare::Param;
- auto args = get_local_share_conv_1x1_args_lar_bs();
- for (auto&& arg : args) {
- size_t b = arg.b, c = arg.c, f = arg.f, p = arg.p, s = arg.s, h = arg.h,
- w = arg.w, sg = arg.sg;
- Param param;
- param.stride_h = param.stride_w = s;
- param.pad_h = param.pad_w = p;
- param.spatial_groups_h = param.spatial_groups_w = sg;
- checker.set_param(param);
- TensorShape src{b, 4, h, w}, filter{sg, sg, 4, f, f, c};
- checker.execs({src, filter, {}});
- src = TensorShape{b, 8, h, w}, filter = TensorShape{sg, sg, 8, f, f, c};
- checker.exec({src, filter, {}});
- }
- }
-
- TEST_F(CUDA, LOCAL_SHARE_FORWARD_3x3_LAR_BS) {
- require_compute_capability(6, 0);
- Checker<LocalShare> checker(handle_cuda());
- bool require_algo = false;
- checker.set_before_exec_callback(AlgoChecker<LocalShare>(
- "LOCAL_SHARE_CHWN_BATCH_SIZE_AWARE", &require_algo));
- using Param = LocalShare::Param;
- auto args = get_local_share_conv_3x3_args_lar_bs();
- ConstValue const_1{1};
- for (auto&& arg : args) {
- size_t b = arg.b, c = arg.c, f = arg.f, p = arg.p, s = arg.s, h = arg.h,
- w = arg.w, sg = arg.sg;
- Param param;
- param.stride_h = param.stride_w = s;
- param.pad_h = param.pad_w = p;
- param.spatial_groups_h = param.spatial_groups_w = sg;
- checker.set_param(param);
- TensorShape src{b, 4, h, w}, filter{sg, sg, 4, f, f, c};
- checker.execs({src, filter, {}});
- src = TensorShape{b, 8, h, w}, filter = TensorShape{sg, sg, 8, f, f, c};
- checker.exec({src, filter, {}});
- }
- }
-
- TEST_F(CUDA, LOCAL_SHARE_FORWARD_5x5_LAR_BS) {
- require_compute_capability(6, 0);
- Checker<LocalShare> checker(handle_cuda());
- bool require_algo = false;
- checker.set_before_exec_callback(AlgoChecker<LocalShare>(
- "LOCAL_SHARE_CHWN_BATCH_SIZE_AWARE", &require_algo));
- using Param = LocalShare::Param;
- auto args = get_local_share_conv_5x5_args_lar_bs();
- for (auto&& arg : args) {
- size_t b = arg.b, c = arg.c, f = arg.f, p = arg.p, s = arg.s, h = arg.h,
- w = arg.w, sg = arg.sg;
- Param param;
- param.stride_h = param.stride_w = s;
- param.pad_h = param.pad_w = p;
- param.spatial_groups_h = param.spatial_groups_w = sg;
- checker.set_param(param);
- TensorShape src{b, 4, h, w}, filter{sg, sg, 4, f, f, c};
- checker.execs({src, filter, {}});
- src = TensorShape{b, 8, h, w}, filter = TensorShape{sg, sg, 8, f, f, c};
- checker.exec({src, filter, {}});
- }
- }
-
- TEST_F(CUDA, LOCAL_SHARE_FORWARD_7x7_LAR_BS) {
- require_compute_capability(6, 0);
- Checker<LocalShare> checker(handle_cuda());
- bool require_algo = false;
- checker.set_before_exec_callback(AlgoChecker<LocalShare>(
- "LOCAL_SHARE_CHWN_BATCH_SIZE_AWARE", &require_algo));
- using Param = LocalShare::Param;
- auto args = get_local_share_conv_7x7_args_lar_bs();
- for (auto&& arg : args) {
- size_t b = arg.b, c = arg.c, f = arg.f, p = arg.p, s = arg.s, h = arg.h,
- w = arg.w, sg = arg.sg;
- Param param;
- param.stride_h = param.stride_w = s;
- param.pad_h = param.pad_w = p;
- param.spatial_groups_h = param.spatial_groups_w = sg;
- checker.set_param(param);
- TensorShape src{b, 4, h, w}, filter{sg, sg, 4, f, f, c};
- checker.execs({src, filter, {}});
- src = TensorShape{b, 8, h, w}, filter = TensorShape{sg, sg, 8, f, f, c};
- checker.exec({src, filter, {}});
- }
- }
-
- TEST_F(CUDA, LOCAL_SHARE_BATCHED_MATMUL) {
- Checker<LocalShare> checker(handle_cuda());
- bool require_algo = false;
- checker.set_before_exec_callback(
- AlgoChecker<LocalShare>("LOCAL_SHARE_BATCHED_MATMUL", &require_algo));
- using Param = LocalShare::Param;
- auto args = convolution::get_args();
- for (size_t sg : {2, 3}) {
- for (auto&& arg : args) {
- if (arg.param.sparse != LocalShare::Param::Sparse::DENSE)
- continue;
- if (arg.param.format != LocalShare::Param::Format::NCHW)
- continue;
- if (arg.param.dilate_h != 1 || arg.param.dilate_w != 1)
- continue;
- Param param;
- param.sparse = arg.param.sparse;
- param.stride_h = arg.param.stride_h, param.stride_w = arg.param.stride_w;
- param.pad_h = arg.param.pad_h, param.pad_w = arg.param.pad_w;
- param.dilate_h = arg.param.dilate_h, param.dilate_w = arg.param.dilate_w;
- param.spatial_groups_h = param.spatial_groups_w = sg;
- size_t ho = infer_conv_shape(
- arg.src[2], arg.filter[2], param.stride_h, param.pad_h);
- size_t wo = infer_conv_shape(
- arg.src[3], arg.filter[3], param.stride_w, param.pad_w);
- if (ho % sg != 0 || wo % sg != 0)
- continue;
- TensorShape filter{
- sg, sg, arg.filter[1], arg.filter[2], arg.filter[3], arg.filter[0]};
- checker.set_param(param);
- checker.exec({arg.src, filter, {}});
- }
- }
- }
-
- TEST_F(CUDA, GROUP_LOCAL_SHARE_BATCHED_MATMUL) {
- Checker<LocalShare> checker(handle_cuda());
- bool require_algo = false;
- checker.set_before_exec_callback(
- AlgoChecker<LocalShare>("LOCAL_SHARE_BATCHED_MATMUL", &require_algo));
- using Param = LocalShare::Param;
- auto args = convolution::get_args();
- for (size_t sg : {2, 3}) {
- for (auto&& arg : args) {
- if (arg.param.sparse != LocalShare::Param::Sparse::DENSE)
- continue;
- if (arg.param.format != LocalShare::Param::Format::NCHW)
- continue;
- if (arg.param.dilate_h != 1 || arg.param.dilate_w != 1)
- continue;
- if (arg.filter.ndim != 4)
- continue;
- Param param;
- param.sparse = Param::Sparse::GROUP;
- param.stride_h = arg.param.stride_h, param.stride_w = arg.param.stride_w;
- param.pad_h = arg.param.pad_h, param.pad_w = arg.param.pad_w;
- param.dilate_h = arg.param.dilate_h, param.dilate_w = arg.param.dilate_w;
- param.spatial_groups_h = param.spatial_groups_w = sg;
- size_t ho = infer_conv_shape(
- arg.src[2], arg.filter[2], param.stride_h, param.pad_h);
- size_t wo = infer_conv_shape(
- arg.src[3], arg.filter[3], param.stride_w, param.pad_w);
- if (ho % sg != 0 || wo % sg != 0)
- continue;
- size_t nr_groups = 3;
- TensorShape filter{
- nr_groups, sg, sg, arg.filter[1], arg.filter[2],
- arg.filter[3], arg.filter[0]};
- TensorShape src{arg.src[0], arg.src[1] * nr_groups, arg.src[2], arg.src[3]};
- checker.set_param(param);
- checker.exec({src, filter, {}});
- }
- }
- }
-
- TEST_F(CUDA, LOCAL_SHARE_FORWARD_SMALL_IMAGE_GENERAL) {
- require_compute_capability(6, 0);
- Checker<LocalShare> checker(handle_cuda());
- bool require_algo = false;
- checker.set_before_exec_callback(AlgoChecker<LocalShare>(
- "LOCAL_SHARE_CHWN_BATCH_SIZE_AWARE_SMALL_IMAGE", &require_algo));
- using Param = LocalShare::Param;
- auto args = convolution::get_args();
- for (size_t sg : {2, 3}) {
- for (auto&& arg : args) {
- if (arg.param.sparse != LocalShare::Param::Sparse::DENSE)
- continue;
- if (arg.param.format != LocalShare::Param::Format::NCHW)
- continue;
- if (arg.param.dilate_h != 1 || arg.param.dilate_w != 1)
- continue;
- Param param;
- param.stride_h = arg.param.stride_h, param.stride_w = arg.param.stride_w;
- param.pad_h = arg.param.pad_h, param.pad_w = arg.param.pad_w;
- param.dilate_h = arg.param.dilate_h, param.dilate_w = arg.param.dilate_w;
- param.spatial_groups_h = param.spatial_groups_w = sg;
- size_t ho = infer_conv_shape(
- arg.src[2], arg.filter[2], param.stride_h, param.pad_h);
- size_t wo = infer_conv_shape(
- arg.src[3], arg.filter[3], param.stride_w, param.pad_w);
- if (ho % sg != 0 || wo % sg != 0)
- continue;
- arg.filter[1] = arg.filter[1] + (4 - arg.filter[1] % 4);
- arg.src[1] = arg.filter[1];
- TensorShape filter{
- sg, sg, arg.filter[1], arg.filter[2], arg.filter[3], arg.filter[0]};
- checker.set_param(param);
- checker.exec({arg.src, filter, {}});
- }
- }
- }
-
- TEST_F(CUDA, LOCAL_SHARE_FORWARD_SMALL_IMAGE_SPECIAL) {
- require_compute_capability(6, 0);
- Checker<LocalShare> checker(handle_cuda());
- bool require_algo = false;
- checker.set_before_exec_callback(AlgoChecker<LocalShare>(
- "LOCAL_SHARE_CHWN_BATCH_SIZE_AWARE_SMALL_IMAGE", &require_algo));
- using Param = LocalShare::Param;
- auto args = get_local_share_conv_small_image();
- for (auto&& arg : args) {
- size_t b = arg.b, c = arg.c, f = arg.f, p = arg.p, s = arg.s, h = arg.h,
- w = arg.w, sg = arg.sg;
- Param param;
- param.stride_h = param.stride_w = s;
- param.pad_h = param.pad_w = p;
- param.spatial_groups_h = param.spatial_groups_w = sg;
- checker.set_param(param);
- TensorShape src{b, 4, h, w}, filter{sg, sg, 4, f, f, c};
- checker.execs({src, filter, {}});
- src = TensorShape{b, 8, h, w}, filter = TensorShape{sg, sg, 8, f, f, c};
- checker.exec({src, filter, {}});
- }
- }
-
- TEST_F(CUDA, LOCAL_SHARE_BWD_DATA_IMPLICIT_GEMM_GENERAL) {
- require_compute_capability(6, 0);
- Checker<LocalShareBackwardData> checker(handle_cuda());
- bool require_algo = false;
- checker.set_before_exec_callback(AlgoChecker<LocalShareBackwardData>(
- "LOCAL_SHARE_IMPLICIT_GEMM", &require_algo));
- using Param = LocalShare::Param;
- auto args = convolution::get_args();
- ConstValue const_0{0};
- for (size_t sg : {2, 3}) {
- for (auto&& arg : args) {
- if (arg.param.sparse != LocalShare::Param::Sparse::DENSE)
- continue;
- if (arg.param.format != LocalShare::Param::Format::NCHW)
- continue;
- if (arg.param.dilate_h != 1 || arg.param.dilate_w != 1)
- continue;
- Param param;
- param.stride_h = arg.param.stride_h, param.stride_w = arg.param.stride_w;
- param.pad_h = arg.param.pad_h, param.pad_w = arg.param.pad_w;
- param.dilate_h = arg.param.dilate_h, param.dilate_w = arg.param.dilate_w;
- param.spatial_groups_h = param.spatial_groups_w = sg;
- size_t ho = infer_conv_shape(
- arg.src[2], arg.filter[2], param.stride_h, param.pad_h);
- size_t wo = infer_conv_shape(
- arg.src[3], arg.filter[3], param.stride_w, param.pad_w);
- if (ho % sg != 0 || wo % sg != 0)
- continue;
- arg.filter[0] = arg.filter[0] + (4 - arg.filter[0] % 4);
- TensorShape filter{
- sg, sg, arg.filter[1], arg.filter[2], arg.filter[3], arg.filter[0]};
- TensorShape diff{arg.src[0], arg.filter[0], ho, wo};
- checker.set_param(param);
- checker.set_rng(2, &const_0);
- checker.exec({filter, diff, arg.src});
- }
- }
- }
-
- TEST_F(CUDA, LOCAL_SHARE_BWD_DATA_IMPLICIT_GEMM_SPECIAL_PART1) {
- require_compute_capability(6, 0);
- test_local_share_bwd_data_implicit_gemm(3, handle_cuda());
- }
-
- TEST_F(CUDA, LOCAL_SHARE_BWD_DATA_IMPLICIT_GEMM_SPECIAL_PART2) {
- require_compute_capability(6, 0);
- test_local_share_bwd_data_implicit_gemm(5, handle_cuda());
- }
-
- TEST_F(CUDA, LOCAL_SHARE_BWD_DATA_IMPLICIT_GEMM_SPECIAL_PART3) {
- require_compute_capability(6, 0);
- test_local_share_bwd_data_implicit_gemm(7, handle_cuda());
- }
-
- TEST_F(CUDA, LOCAL_SHARE_BWD_DATA_BATCHED_MATMUL) {
- Checker<LocalShareBackwardData> checker(handle_cuda());
- bool require_algo = false;
- checker.set_before_exec_callback(AlgoChecker<LocalShareBackwardData>(
- "LOCAL_SHARE_BATCHED_MATMUL", &require_algo));
- using Param = LocalShare::Param;
- auto args = convolution::get_args();
- ConstValue const_0{0};
- for (size_t sg : {2, 3}) {
- for (auto&& arg : args) {
- if (arg.param.sparse != LocalShare::Param::Sparse::DENSE)
- continue;
- if (arg.param.format != LocalShare::Param::Format::NCHW)
- continue;
- if (arg.param.dilate_h != 1 || arg.param.dilate_w != 1)
- continue;
- Param param;
- param.stride_h = arg.param.stride_h, param.stride_w = arg.param.stride_w;
- param.pad_h = arg.param.pad_h, param.pad_w = arg.param.pad_w;
- param.dilate_h = arg.param.dilate_h, param.dilate_w = arg.param.dilate_w;
- param.spatial_groups_h = param.spatial_groups_w = sg;
- size_t ho = infer_conv_shape(
- arg.src[2], arg.filter[2], param.stride_h, param.pad_h);
- size_t wo = infer_conv_shape(
- arg.src[3], arg.filter[3], param.stride_w, param.pad_w);
- if (ho % sg != 0 || wo % sg != 0)
- continue;
- TensorShape filter{
- sg, sg, arg.filter[1], arg.filter[2], arg.filter[3], arg.filter[0]};
- TensorShape diff{arg.src[0], arg.filter[0], ho, wo};
- checker.set_rng(2, &const_0);
- checker.set_param(param);
- checker.exec({filter, diff, arg.src});
- }
- }
- }
-
- TEST_F(CUDA, GROUP_LOCAL_SHARE_BWD_DATA_BATCHED_MATMUL) {
- Checker<LocalShareBackwardData> checker(handle_cuda());
- bool require_algo = false;
- checker.set_before_exec_callback(AlgoChecker<LocalShareBackwardData>(
- "LOCAL_SHARE_BATCHED_MATMUL", &require_algo));
- using Param = LocalShare::Param;
- auto args = convolution::get_args();
- ConstValue const_0{0};
- for (size_t sg : {2, 3}) {
- for (auto&& arg : args) {
- if (arg.param.sparse != LocalShare::Param::Sparse::DENSE)
- continue;
- if (arg.param.format != LocalShare::Param::Format::NCHW)
- continue;
- if (arg.param.dilate_h != 1 || arg.param.dilate_w != 1)
- continue;
- Param param;
- param.sparse = Param::Sparse::GROUP;
- param.stride_h = arg.param.stride_h, param.stride_w = arg.param.stride_w;
- param.pad_h = arg.param.pad_h, param.pad_w = arg.param.pad_w;
- param.dilate_h = arg.param.dilate_h, param.dilate_w = arg.param.dilate_w;
- param.spatial_groups_h = param.spatial_groups_w = sg;
- size_t ho = infer_conv_shape(
- arg.src[2], arg.filter[2], param.stride_h, param.pad_h);
- size_t wo = infer_conv_shape(
- arg.src[3], arg.filter[3], param.stride_w, param.pad_w);
- if (ho % sg != 0 || wo % sg != 0)
- continue;
- size_t nr_groups = 3;
- TensorShape filter{
- nr_groups, sg, sg, arg.filter[1], arg.filter[2],
- arg.filter[3], arg.filter[0]};
- TensorShape diff{arg.src[0], arg.filter[0] * nr_groups, ho, wo};
- TensorShape grad{
- arg.src[0], arg.src[1] * nr_groups, arg.src[2], arg.src[3]};
- checker.set_rng(2, &const_0);
- checker.set_param(param);
- checker.exec({filter, diff, grad});
- }
- }
- }
-
- TEST_F(CUDA, LOCAL_SHARE_BWD_FILTER_IMPLICIT_GEMM_GENERAL) {
- require_compute_capability(6, 0);
- Checker<LocalShareBackwardFilter> checker(handle_cuda());
- bool require_algo = false;
- checker.set_before_exec_callback(AlgoChecker<LocalShareBackwardFilter>(
- "LOCAL_SHARE_IMPLICIT_GEMM", &require_algo));
- using Param = LocalShare::Param;
- auto args = convolution::get_args();
- ConstValue const_0{0};
- for (size_t sg : {2, 3}) {
- for (auto&& arg : args) {
- if (arg.param.sparse != LocalShare::Param::Sparse::DENSE)
- continue;
- if (arg.param.format != LocalShare::Param::Format::NCHW)
- continue;
- if (arg.param.dilate_h != 1 || arg.param.dilate_w != 1)
- continue;
- Param param;
- param.stride_h = arg.param.stride_h, param.stride_w = arg.param.stride_w;
- param.pad_h = arg.param.pad_h, param.pad_w = arg.param.pad_w;
- param.dilate_h = arg.param.dilate_h, param.dilate_w = arg.param.dilate_w;
- param.spatial_groups_h = param.spatial_groups_w = sg;
- size_t ho = infer_conv_shape(
- arg.src[2], arg.filter[2], param.stride_h, param.pad_h);
- size_t wo = infer_conv_shape(
- arg.src[3], arg.filter[3], param.stride_w, param.pad_w);
- if (ho % sg != 0 || wo % sg != 0)
- continue;
- arg.src[0] = arg.src[0] + (4 - arg.src[0] % 4);
- TensorShape grad{
- sg, sg, arg.filter[1], arg.filter[2], arg.filter[3], arg.filter[0]};
- TensorShape diff{arg.src[0], arg.filter[0], ho, wo};
- checker.set_param(param);
- checker.set_rng(2, &const_0);
- checker.exec({arg.src, diff, grad});
- }
- }
- }
-
- TEST_F(CUDA, LOCAL_SHARE_BWD_FILTER_IMPLICIT_GEMM_SPECIAL) {
- require_compute_capability(6, 0);
- Checker<LocalShareBackwardFilter> checker(handle_cuda());
- bool require_algo = false;
- checker.set_before_exec_callback(AlgoChecker<LocalShareBackwardFilter>(
- "LOCAL_SHARE_IMPLICIT_GEMM", &require_algo));
- using Param = LocalShare::Param;
- ConstValue const_0{0};
- auto args = get_local_share_conv_small_image();
- for (auto&& arg : args) {
- static_cast<void>(arg);
- size_t b = arg.b, c = arg.c, f = arg.f, p = arg.p, s = arg.s, h = arg.h,
- w = arg.w, sg = arg.sg;
- size_t ho = infer_conv_shape(h, f, s, p), wo = infer_conv_shape(w, f, s, p);
- Param param;
- param.stride_h = param.stride_w = s;
- param.pad_h = param.pad_w = p;
- param.spatial_groups_h = param.spatial_groups_w = sg;
- checker.set_param(param);
- checker.set_rng(2, &const_0);
- TensorShape diff{b, c, ho, wo}, grad{sg, sg, 4, f, f, c}, src{b, 4, h, w};
- checker.execs({src, diff, grad});
- src = {b, 8, h, w};
- diff = TensorShape{b, c, ho, wo}, grad = TensorShape{sg, sg, 8, f, f, c};
- checker.exec({src, diff, grad});
- }
- }
-
- TEST_F(CUDA, LOCAL_SHARE_BWD_FILTER_BATCHED_MATMUL) {
- Checker<LocalShareBackwardFilter> checker(handle_cuda());
- bool require_algo = false;
- checker.set_before_exec_callback(AlgoChecker<LocalShareBackwardFilter>(
- "LOCAL_SHARE_BATCHED_MATMUL", &require_algo));
- using Param = LocalShare::Param;
- auto args = convolution::get_args();
- ConstValue const_0{0};
- for (size_t sg : {2, 3}) {
- for (auto&& arg : args) {
- if (arg.param.sparse != LocalShare::Param::Sparse::DENSE)
- continue;
- if (arg.param.format != LocalShare::Param::Format::NCHW)
- continue;
- if (arg.param.dilate_h != 1 || arg.param.dilate_w != 1)
- continue;
- Param param;
- param.stride_h = arg.param.stride_h, param.stride_w = arg.param.stride_w;
- param.pad_h = arg.param.pad_h, param.pad_w = arg.param.pad_w;
- param.dilate_h = arg.param.dilate_h, param.dilate_w = arg.param.dilate_w;
- param.spatial_groups_h = param.spatial_groups_w = sg;
- size_t ho = infer_conv_shape(
- arg.src[2], arg.filter[2], param.stride_h, param.pad_h);
- size_t wo = infer_conv_shape(
- arg.src[3], arg.filter[3], param.stride_w, param.pad_w);
- if (ho % sg != 0 || wo % sg != 0)
- continue;
- TensorShape grad{
- sg, sg, arg.filter[1], arg.filter[2], arg.filter[3], arg.filter[0]};
- TensorShape diff{arg.src[0], arg.filter[0], ho, wo};
- checker.set_rng(2, &const_0);
- checker.set_param(param);
- checker.exec({arg.src, diff, grad});
- }
- }
- }
-
- TEST_F(CUDA, GROUP_LOCAL_SHARE_BWD_FILTER_BATCHED_MATMUL) {
- Checker<LocalShareBackwardFilter> checker(handle_cuda());
- bool require_algo = false;
- checker.set_before_exec_callback(AlgoChecker<LocalShareBackwardFilter>(
- "LOCAL_SHARE_BATCHED_MATMUL", &require_algo));
- using Param = LocalShare::Param;
- auto args = convolution::get_args();
- ConstValue const_0{0};
- for (size_t sg : {2, 3}) {
- for (auto&& arg : args) {
- if (arg.param.sparse != LocalShare::Param::Sparse::DENSE)
- continue;
- if (arg.param.format != LocalShare::Param::Format::NCHW)
- continue;
- if (arg.param.dilate_h != 1 || arg.param.dilate_w != 1)
- continue;
- Param param;
- param.sparse = Param::Sparse::GROUP;
- param.stride_h = arg.param.stride_h, param.stride_w = arg.param.stride_w;
- param.pad_h = arg.param.pad_h, param.pad_w = arg.param.pad_w;
- param.dilate_h = arg.param.dilate_h, param.dilate_w = arg.param.dilate_w;
- param.spatial_groups_h = param.spatial_groups_w = sg;
- size_t ho = infer_conv_shape(
- arg.src[2], arg.filter[2], param.stride_h, param.pad_h);
- size_t wo = infer_conv_shape(
- arg.src[3], arg.filter[3], param.stride_w, param.pad_w);
- if (ho % sg != 0 || wo % sg != 0)
- continue;
- size_t nr_groups = 3;
- TensorShape grad{
- nr_groups, sg, sg, arg.filter[1], arg.filter[2],
- arg.filter[3], arg.filter[0]};
- TensorShape diff{arg.src[0], arg.filter[0] * nr_groups, ho, wo};
- TensorShape src{arg.src[0], arg.src[1] * nr_groups, arg.src[2], arg.src[3]};
- checker.set_rng(2, &const_0);
- checker.set_param(param);
- checker.exec({src, diff, grad});
- }
- }
- }
-
- #if MEGDNN_WITH_BENCHMARK
- TEST_F(CUDA, BENCHMARK_LOCAL_SHARE_BWD_FILTER) {
- CUBenchmarker<LocalShareBackwardFilter> bencher(handle_cuda());
- size_t RUNS = 1000;
- bencher.set_display(false).set_times(RUNS);
- std::unique_ptr<OprProxy<LocalShareBackwardFilter>> proxy{
- new OprProxy<LocalShareBackwardFilter>{true}};
- bencher.set_proxy(proxy);
-
- LocalShare::Param param;
- NormalRNG rng;
-
- auto run = [&](size_t batch, size_t ic, size_t ih, size_t iw, size_t oc, size_t f,
- size_t s, size_t sg) {
- param.pad_h = f / 2;
- param.pad_w = f / 2;
- param.stride_h = s;
- param.stride_w = s;
- param.spatial_groups_h = sg;
- param.spatial_groups_w = sg;
-
- TensorShape src = {batch, ic, ih, iw}, grad = {sg, sg, ic, f, f, oc};
- size_t ho = infer_conv_shape(ih, f, s, f / 2);
- size_t wo = infer_conv_shape(iw, f, s, f / 2);
- TensorShape diff = {batch, oc, ho, wo};
-
- float flo = 2.0 * batch * oc * ho * wo * ic * f * f / (1e12);
-
- bencher.set_param(param)
- .set_dtype(0, dtype::Float32())
- .set_dtype(1, dtype::Float32())
- .set_dtype(2, dtype::Float32())
- .set_rng(0, &rng)
- .set_rng(1, &rng);
- bencher.proxy()->target_execution_policy.algo.reset();
- auto time_in_ms = bencher.execs({src, diff, grad}) / RUNS;
-
- printf("src=%s, diff=%s, grad=%s, float32: %.2fms "
- "%.2fTFlops\n",
- src.to_string().c_str(), diff.to_string().c_str(),
- grad.to_string().c_str(), time_in_ms, (flo / (time_in_ms * 1e-3)));
- };
- // stride = 1
- run(32, 128, 24, 24, 128, 1, 1, 3);
- run(32, 256, 12, 12, 256, 1, 1, 3);
-
- // stride = 2
- run(32, 256, 12, 12, 512, 1, 2, 3);
- run(32, 512, 6, 6, 1024, 1, 2, 3);
-
- // stride = 1
- run(32, 128, 24, 24, 128, 3, 1, 3);
- run(32, 256, 12, 12, 256, 3, 1, 3);
-
- // stride = 2
- run(32, 128, 24, 24, 256, 3, 2, 3);
- run(32, 256, 12, 12, 512, 3, 2, 3);
-
- // stride = 1
- run(64, 128, 24, 24, 128, 1, 1, 3);
- run(64, 256, 12, 12, 256, 1, 1, 3);
-
- // stride = 2
- run(64, 256, 12, 12, 512, 1, 2, 3);
- run(64, 512, 6, 6, 1024, 1, 2, 3);
-
- // stride = 1
- run(64, 128, 24, 24, 128, 3, 1, 3);
- run(64, 256, 12, 12, 256, 3, 1, 3);
-
- // stride = 2
- run(64, 128, 24, 24, 256, 3, 2, 3);
- run(64, 256, 12, 12, 512, 3, 2, 3);
- }
-
- TEST_F(CUDA, BENCHMARK_GROUP_LOCAL_SHARE_FORWARD) {
- CUBenchmarker<LocalShare> bencher(handle_cuda());
- size_t RUNS = 1000;
- bencher.set_display(false).set_times(RUNS);
- std::unique_ptr<OprProxy<LocalShareForward>> proxy{
- new OprProxy<LocalShareForward>{true}};
- bencher.set_proxy(proxy);
-
- LocalShare::Param param;
- NormalRNG rng;
-
- auto run = [&](size_t batch, size_t ic, size_t ih, size_t iw, size_t oc, size_t f,
- size_t s, size_t sg) {
- param.pad_h = f / 2;
- param.pad_w = f / 2;
- param.stride_h = s;
- param.stride_w = s;
- param.spatial_groups_h = sg;
- param.spatial_groups_w = sg;
- param.sparse = LocalShare::Param::Sparse::GROUP;
-
- TensorShape src = {1, batch * ic, ih, iw},
- filter = {batch, sg, sg, ic, f, f, oc};
- size_t ho = infer_conv_shape(ih, f, s, f / 2);
- size_t wo = infer_conv_shape(iw, f, s, f / 2);
-
- float flo = 2.0 * batch * oc * ho * wo * ic * f * f / (1e12);
-
- bencher.set_param(param)
- .set_dtype(0, dtype::Float32())
- .set_dtype(1, dtype::Float32())
- .set_dtype(2, dtype::Float32())
- .set_rng(0, &rng)
- .set_rng(1, &rng);
- bencher.proxy()->target_execution_policy.algo.reset();
- auto time_in_ms = bencher.execs({src, filter, {}}) / RUNS;
- ;
-
- printf("src=%s, filter=%s, float32: %.2fms %.2fTFlops\n",
- src.to_string().c_str(), filter.to_string().c_str(), time_in_ms,
- (flo / (time_in_ms * 1e-3)));
- };
- // stride = 1
- run(32, 128, 24, 24, 128, 1, 1, 3);
- run(32, 256, 12, 12, 256, 1, 1, 3);
-
- // stride = 2
- run(32, 256, 12, 12, 512, 1, 2, 3);
- run(32, 512, 6, 6, 1024, 1, 2, 3);
-
- // stride = 1
- run(64, 128, 24, 24, 128, 1, 1, 3);
- run(64, 256, 12, 12, 256, 1, 1, 3);
-
- // stride = 2
- run(64, 256, 12, 12, 512, 1, 2, 3);
- run(64, 512, 6, 6, 1024, 1, 2, 3);
- }
-
- TEST_F(CUDA, BENCHMARK_LOCAL_SHARE_BWD_DATA) {
- CUBenchmarker<LocalShareBackwardData> bencher(handle_cuda());
- size_t RUNS = 1000;
- bencher.set_display(false).set_times(RUNS);
- std::unique_ptr<OprProxy<LocalShareBackwardData>> proxy{
- new OprProxy<LocalShareBackwardData>{true}};
- bencher.set_proxy(proxy);
-
- LocalShare::Param param;
- NormalRNG rng;
-
- auto run = [&](size_t batch, size_t ic, size_t ih, size_t iw, size_t oc, size_t f,
- size_t s, size_t sg) {
- param.pad_h = f / 2;
- param.pad_w = f / 2;
- param.stride_h = s;
- param.stride_w = s;
- param.spatial_groups_h = sg;
- param.spatial_groups_w = sg;
-
- TensorShape grad = {batch, ic, ih, iw}, filter = {sg, sg, ic, f, f, oc};
- size_t ho = infer_conv_shape(ih, f, s, f / 2);
- size_t wo = infer_conv_shape(iw, f, s, f / 2);
- TensorShape diff = {batch, oc, ho, wo};
-
- float flo = 2.0 * batch * oc * ho * wo * ic * f * f / (1e12);
-
- bencher.set_param(param)
- .set_dtype(0, dtype::Float32())
- .set_dtype(1, dtype::Float32())
- .set_dtype(2, dtype::Float32())
- .set_rng(0, &rng)
- .set_rng(1, &rng);
- bencher.proxy()->target_execution_policy.algo.reset();
- auto time_in_ms = bencher.execs({filter, diff, grad}) / RUNS;
-
- printf("filter=%s, diff=%s, grad=%s, float32: %.2fms "
- "%.2fTFlops\n",
- filter.to_string().c_str(), diff.to_string().c_str(),
- grad.to_string().c_str(), time_in_ms, (flo / (time_in_ms * 1e-3)));
- };
- // stride = 1
- run(32, 128, 24, 24, 128, 1, 1, 3);
- run(32, 256, 12, 12, 256, 1, 1, 3);
-
- // stride = 2
- run(32, 256, 12, 12, 512, 1, 2, 3);
- run(32, 512, 6, 6, 1024, 1, 2, 3);
-
- // stride = 1
- run(32, 128, 24, 24, 128, 3, 1, 3);
- run(32, 256, 12, 12, 256, 3, 1, 3);
-
- // stride = 2
- run(32, 128, 24, 24, 256, 3, 2, 3);
- run(32, 256, 12, 12, 512, 3, 2, 3);
-
- // stride = 1
- run(64, 128, 24, 24, 128, 1, 1, 3);
- run(64, 256, 12, 12, 256, 1, 1, 3);
-
- // stride = 2
- run(64, 256, 12, 12, 512, 1, 2, 3);
- run(64, 512, 6, 6, 1024, 1, 2, 3);
-
- // stride = 1
- run(64, 128, 24, 24, 128, 3, 1, 3);
- run(64, 256, 12, 12, 256, 3, 1, 3);
-
- // stride = 2
- run(64, 128, 24, 24, 256, 3, 2, 3);
- run(64, 256, 12, 12, 512, 3, 2, 3);
- }
-
- TEST_F(CUDA, BENCHMARK_LOCAL_SHARE_FORWARD_BOTTLENECK) {
- CUBenchmarker<LocalShare> bencher(handle_cuda());
- CUBenchmarker<Convolution> bencher_conv(handle_cuda());
- size_t RUNS = 1000;
- bencher.set_display(false).set_times(RUNS);
- std::unique_ptr<OprProxy<LocalShareForward>> proxy{
- new OprProxy<LocalShareForward>{true}};
- bencher.set_proxy(proxy);
-
- bencher_conv.set_display(false).set_times(RUNS);
- std::unique_ptr<OprProxy<Convolution>> conv_proxy{new OprProxy<Convolution>{true}};
- bencher_conv.set_proxy(conv_proxy);
-
- LocalShare::Param param;
- Convolution::Param conv_param;
- NormalRNG rng;
-
- auto run = [&](size_t batch, size_t ic, size_t ih, size_t iw, size_t oc, size_t f,
- size_t s, size_t sg) {
- param.pad_h = f / 2;
- param.pad_w = f / 2;
- param.stride_h = s;
- param.stride_w = s;
- param.spatial_groups_h = sg;
- param.spatial_groups_w = sg;
-
- conv_param.pad_h = f / 2;
- conv_param.pad_w = f / 2;
- conv_param.stride_h = s;
- conv_param.stride_w = s;
-
- TensorShape src = {batch, ic, ih, iw}, filter = {sg, sg, ic, f, f, oc};
- size_t ho = infer_conv_shape(ih, f, s, f / 2);
- size_t wo = infer_conv_shape(iw, f, s, f / 2);
-
- float flo = 2.0 * batch * oc * ho * wo * ic * f * f / (1e12);
-
- bencher.set_param(param)
- .set_dtype(0, dtype::Float32())
- .set_dtype(1, dtype::Float32())
- .set_dtype(2, dtype::Float32())
- .set_rng(0, &rng)
- .set_rng(1, &rng);
- bencher.proxy()->target_execution_policy.algo.reset();
- auto time_in_ms = bencher.execs({src, filter, {}}) / RUNS;
-
- bencher_conv.set_param(conv_param);
- bencher_conv.proxy()->target_execution_policy.algo.reset();
- auto time_in_ms_conv = bencher_conv.execs({src, {oc, ic, f, f}, {}}) / RUNS;
-
- printf("src=%s, filter=%s, float32: %.2fms %.2fTFlops, "
- "conv(float32): %.2fms %.2fTFlops, local_share/conv=%.2f\n",
- src.to_string().c_str(), filter.to_string().c_str(), time_in_ms,
- (flo / (time_in_ms * 1e-3)), time_in_ms_conv,
- (flo / (time_in_ms_conv * 1e-3)), time_in_ms / time_in_ms_conv);
- };
- // stride = 1
- run(32, 128, 24, 24, 128, 1, 1, 3);
- run(32, 256, 12, 12, 256, 1, 1, 3);
-
- // stride = 2
- run(32, 256, 12, 12, 512, 1, 2, 3);
- run(32, 512, 6, 6, 1024, 1, 2, 3);
-
- // stride = 1
- run(32, 128, 24, 24, 128, 3, 1, 3);
- run(32, 256, 12, 12, 256, 3, 1, 3);
-
- // stride = 2
- run(32, 128, 24, 24, 256, 3, 2, 3);
- run(32, 256, 12, 12, 512, 3, 2, 3);
-
- // stride = 1
- run(64, 128, 24, 24, 128, 1, 1, 3);
- run(64, 256, 12, 12, 256, 1, 1, 3);
-
- // stride = 2
- run(64, 256, 12, 12, 512, 1, 2, 3);
- run(64, 512, 6, 6, 1024, 1, 2, 3);
-
- // stride = 1
- run(64, 128, 24, 24, 128, 3, 1, 3);
- run(64, 256, 12, 12, 256, 3, 1, 3);
-
- // stride = 2
- run(64, 128, 24, 24, 256, 3, 2, 3);
- run(64, 256, 12, 12, 512, 3, 2, 3);
- }
-
- TEST_F(CUDA, BENCHMARK_LOCAL_SHARE_FORWARD_FROM_RESEARCH) {
- CUBenchmarker<LocalShare> bencher(handle_cuda());
- CUBenchmarker<Convolution> bencher_conv(handle_cuda());
- size_t RUNS = 1000;
- bencher.set_display(false).set_times(RUNS);
- std::unique_ptr<OprProxy<LocalShareForward>> proxy{
- new OprProxy<LocalShareForward>{true}};
- bencher.set_proxy(proxy);
-
- bencher_conv.set_display(false).set_times(RUNS);
- std::unique_ptr<OprProxy<Convolution>> conv_proxy{new OprProxy<Convolution>{true}};
- bencher_conv.set_proxy(conv_proxy);
-
- LocalShare::Param param;
- Convolution::Param conv_param;
- NormalRNG rng;
-
- auto run = [&](size_t batch, size_t ic, size_t ih, size_t iw, size_t oc, size_t f,
- size_t s, size_t sg) {
- param.pad_h = f / 2;
- param.pad_w = f / 2;
- param.stride_h = s;
- param.stride_w = s;
- param.spatial_groups_h = sg;
- param.spatial_groups_w = sg;
-
- conv_param.pad_h = f / 2;
- conv_param.pad_w = f / 2;
- conv_param.stride_h = s;
- conv_param.stride_w = s;
-
- TensorShape src = {batch, ic, ih, iw}, filter = {sg, sg, ic, f, f, oc};
- size_t ho = infer_conv_shape(ih, f, s, f / 2);
- size_t wo = infer_conv_shape(iw, f, s, f / 2);
-
- float flo = 2.0 * batch * oc * ho * wo * ic * f * f / (1e12);
-
- bencher.set_param(param)
- .set_dtype(0, dtype::Float32())
- .set_dtype(1, dtype::Float32())
- .set_dtype(2, dtype::Float32())
- .set_rng(0, &rng)
- .set_rng(1, &rng);
- bencher.proxy()->target_execution_policy.algo.reset();
- auto time_in_ms = bencher.execs({src, filter, {}}) / RUNS;
-
- bencher_conv.set_param(conv_param);
- bencher_conv.proxy()->target_execution_policy.algo.reset();
- auto time_in_ms_conv = bencher_conv.execs({src, {oc, ic, f, f}, {}}) / RUNS;
-
- printf("src=%s, filter=%s, float32: %.2fms %.2fTFlops, "
- "conv(float32): %.2fms %.2fTFlops, local_share/conv=%.2f\n",
- src.to_string().c_str(), filter.to_string().c_str(), time_in_ms,
- (flo / (time_in_ms * 1e-3)), time_in_ms_conv,
- (flo / (time_in_ms_conv * 1e-3)), time_in_ms / time_in_ms_conv);
- };
- // stride = 1
- run(64, 128, 24, 24, 128, 1, 1, 3);
- run(64, 256, 12, 12, 256, 1, 1, 3);
- run(64, 512, 6, 6, 512, 1, 1, 3);
- run(64, 1024, 3, 3, 1024, 1, 1, 3);
-
- // stride = 2
- run(64, 128, 24, 24, 256, 1, 2, 3);
- run(64, 256, 12, 12, 512, 1, 2, 3);
- run(64, 512, 6, 6, 1024, 1, 2, 3);
-
- // stride = 1
- run(64, 128, 24, 24, 128, 3, 1, 3);
- run(64, 256, 12, 12, 256, 3, 1, 3);
- run(64, 512, 6, 6, 512, 3, 1, 3);
- run(64, 1024, 3, 3, 1024, 3, 1, 3);
-
- // stride = 2
- run(64, 128, 24, 24, 256, 3, 2, 3);
- run(64, 256, 12, 12, 512, 3, 2, 3);
- run(64, 512, 6, 6, 1024, 3, 2, 3);
- }
-
- TEST_F(CUDA, BENCHMARK_LOCAL_SHARE_FORWARD) {
- require_compute_capability(6, 0);
- CUBenchmarker<LocalShare> bencher(handle_cuda());
- CUBenchmarker<Convolution> bencher_conv(handle_cuda());
- size_t RUNS = 200;
- bencher.set_display(false).set_times(RUNS);
- std::unique_ptr<OprProxy<LocalShareForward>> proxy{
- new OprProxy<LocalShareForward>{true}};
- bencher.set_proxy(proxy);
-
- bencher_conv.set_display(false).set_times(RUNS);
- std::unique_ptr<OprProxy<Convolution>> conv_proxy{new OprProxy<Convolution>{true}};
- bencher_conv.set_proxy(conv_proxy);
-
- LocalShare::Param param;
- Convolution::Param conv_param;
- NormalRNG rng;
-
- auto run = [&](size_t batch, size_t ic, size_t ih, size_t iw, size_t oc, size_t f,
- size_t s, size_t sg) {
- param.pad_h = f / 2;
- param.pad_w = f / 2;
- param.stride_h = s;
- param.stride_w = s;
- param.spatial_groups_h = sg;
- param.spatial_groups_w = sg;
-
- conv_param.pad_h = f / 2;
- conv_param.pad_w = f / 2;
- conv_param.stride_h = s;
- conv_param.stride_w = s;
-
- TensorShape src = {batch, ic, ih, iw}, filter = {sg, sg, ic, f, f, oc};
- size_t ho = infer_conv_shape(ih, f, s, f / 2);
- size_t wo = infer_conv_shape(iw, f, s, f / 2);
-
- float flo = 2.0 * batch * oc * ho * wo * ic * f * f / (1e12);
-
- bencher.set_param(param)
- .set_dtype(0, dtype::Float32())
- .set_dtype(1, dtype::Float32())
- .set_dtype(2, dtype::Float32())
- .set_rng(0, &rng)
- .set_rng(1, &rng);
- bencher.proxy()->target_execution_policy.algo.reset();
- auto time_in_ms = bencher.execs({src, filter, {}}) / RUNS;
-
- bencher_conv.set_param(conv_param);
- bencher_conv.proxy()->target_execution_policy.algo.reset();
- auto time_in_ms_conv = bencher_conv.execs({src, {oc, ic, f, f}, {}}) / RUNS;
-
- printf("src=%s, filter=%s, float32: %.2fms %.2fTFlops, "
- "conv(float32): %.2fms %.2fTFlops, local_share/conv=%.2f\n",
- src.to_string().c_str(), filter.to_string().c_str(), time_in_ms,
- (flo / (time_in_ms * 1e-3)), time_in_ms_conv,
- (flo / (time_in_ms_conv * 1e-3)), time_in_ms / time_in_ms_conv);
- };
- run(64, 256, 48, 48, 256, 7, 1, 3);
- run(64, 128, 24, 24, 128, 7, 1, 3);
- run(64, 256, 12, 12, 256, 7, 1, 3);
- run(64, 512, 6, 6, 512, 7, 1, 3);
-
- run(64, 256, 48, 48, 256, 5, 1, 3);
- run(64, 128, 24, 24, 128, 5, 1, 3);
- run(64, 256, 12, 12, 256, 5, 1, 3);
- run(64, 512, 6, 6, 512, 5, 1, 3);
-
- run(32, 64, 96, 96, 256, 7, 2, 3);
- run(32, 128, 24, 24, 128, 7, 2, 3);
- run(32, 256, 12, 12, 256, 7, 2, 3);
-
- run(32, 64, 96, 96, 256, 5, 2, 3);
- run(32, 128, 24, 24, 128, 5, 2, 3);
- run(32, 256, 12, 12, 256, 5, 2, 3);
- }
- #endif
-
- // vim: syntax=cpp.doxygen
|