You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

warp_perspective.cpp 32 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755
  1. /**
  2. * \file dnn/test/naive/warp_perspective.cpp
  3. * MegEngine is Licensed under the Apache License, Version 2.0 (the "License")
  4. *
  5. * Copyright (c) 2014-2021 Megvii Inc. All rights reserved.
  6. *
  7. * Unless required by applicable law or agreed to in writing,
  8. * software distributed under the License is distributed on an
  9. * "AS IS" BASIS, WITHOUT ARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  10. */
  11. #include "test/naive/fixture.h"
  12. #include "megdnn/oprs/cv.h"
  13. #include "test/common/checker.h"
  14. #include "test/common/warp_perspective.h"
  15. #include "megdnn/tensor_format.h"
  16. #include "test/common/benchmarker.h"
  17. #include "test/common/extra_impl_helper.h"
  18. using namespace megdnn;
  19. using namespace test;
  20. namespace {
  21. class NanMatRNG : public RNG {
  22. void gen(const TensorND& tensor_) override {
  23. auto& gen = RandomState::generator();
  24. std::uniform_real_distribution<dt_float32> pdist3(1.9f, 2.1f);
  25. std::uniform_real_distribution<dt_float32> pdist(0.9f, 1.1f);
  26. std::uniform_real_distribution<dt_float32> pdisth(0.4f, 0.6f);
  27. std::uniform_real_distribution<dt_float32> ndist(-1.1f, -0.9f);
  28. std::uniform_real_distribution<dt_float32> ndist3(-2.1f, -1.9f);
  29. std::uniform_real_distribution<dt_float32> ndisth(-0.6f, -0.4f);
  30. std::uniform_int_distribution<int> dice(0, 5);
  31. float* ptr = tensor_.ptr<dt_float32>();
  32. auto N = tensor_.layout.shape[0];
  33. for (size_t n = 0; n < N; ++n) {
  34. for (size_t i = 0; i < 9; ++i) {
  35. switch (dice(gen)) {
  36. case 0:
  37. ptr[i] = pdist3(gen);
  38. break;
  39. case 1:
  40. ptr[i] = pdist(gen);
  41. break;
  42. case 2:
  43. ptr[i] = pdisth(gen);
  44. break;
  45. case 3:
  46. ptr[i] = ndist(gen);
  47. break;
  48. case 4:
  49. ptr[i] = ndist3(gen);
  50. break;
  51. case 5:
  52. ptr[i] = ndisth(gen);
  53. break;
  54. }
  55. }
  56. ptr[6] = 1;
  57. ptr[7] = -1;
  58. ptr[8] = 5;
  59. ptr += 9;
  60. }
  61. }
  62. };
  63. } // namespace
  64. TEST_F(NAIVE, WARP_PERSPECTIVE_NCHW4) {
  65. using Param = WarpPerspective::Param;
  66. auto convert_true_format = [](const TensorLayout& layout) {
  67. if (layout.ndim == 4)
  68. return layout
  69. .reshape(
  70. {layout[0], layout[1] / 4, layout[2], layout[3], 4})
  71. .dimshuffle({0, 1, 4, 2, 3});
  72. else
  73. return layout;
  74. };
  75. WarpPerspective::Param param;
  76. auto extra_impl = [&param, this,
  77. convert_true_format](const TensorNDArray& tensors) {
  78. auto warp_perspective = handle()->create_operator<WarpPerspective>();
  79. warp_perspective->param() = param;
  80. warp_perspective->param().format = Param::Format::NCHW;
  81. TensorNDArray nchw_tensors;
  82. for (size_t i = 0; i < tensors.size(); ++i) {
  83. auto layout = tensors[i].layout;
  84. if (layout.dtype.enumv() == DTypeEnum::QuantizedS8)
  85. layout.dtype = dtype::Int8();
  86. if (layout.ndim == 5) {
  87. layout = layout.reshape({layout[0], layout[1] * layout[4],
  88. layout[2], layout[3]});
  89. }
  90. nchw_tensors.emplace_back(malloc(layout.span().dist_byte()),
  91. layout);
  92. }
  93. TensorNDArray nchw4_tensors;
  94. for (size_t i = 0; i < tensors.size(); ++i) {
  95. auto layout = convert_true_format(nchw_tensors[i].layout);
  96. nchw4_tensors.emplace_back(tensors[i].raw_ptr, std::move(layout));
  97. }
  98. auto workspace_size = warp_perspective->get_workspace_in_bytes(
  99. tensors[0].layout, tensors[1].layout, tensors[2].layout);
  100. dt_byte* workspace_ptr = static_cast<dt_byte*>(malloc(workspace_size));
  101. Workspace workspace{workspace_ptr, workspace_size};
  102. auto relayout = handle()->create_operator<RelayoutForward>();
  103. relayout->exec(nchw4_tensors[0], nchw_tensors[0]);
  104. relayout->exec(nchw4_tensors[1], nchw_tensors[1]);
  105. warp_perspective->exec(nchw_tensors[0], nchw_tensors[1],
  106. nchw_tensors[2], workspace);
  107. relayout->exec(nchw_tensors[2], nchw4_tensors[2]);
  108. free(workspace_ptr);
  109. for (auto&& tensor : nchw_tensors) {
  110. free(tensor.raw_ptr);
  111. }
  112. };
  113. Checker<WarpPerspectiveForward> checker(handle());
  114. WarpPerspectiveMatRNG rng;
  115. checker.set_rng(1, &rng);
  116. checker.set_dtype(0, dtype::QuantizedS8(0.1f));
  117. checker.set_dtype(2, dtype::QuantizedS8(0.1f));
  118. checker.set_extra_opr_impl(extra_impl);
  119. for (auto bmode : {WarpPerspective::BorderMode::WRAP,
  120. WarpPerspective::BorderMode::REFLECT,
  121. WarpPerspective::BorderMode::REPLICATE,
  122. WarpPerspective::BorderMode::CONSTANT}) {
  123. param.border_val = 0.3f;
  124. param.bmode = bmode;
  125. param.imode = Param::InterpolationMode::LINEAR;
  126. param.format = Param::Format::NCHW4;
  127. checker.set_param(param);
  128. checker.execs({{2, 1, 10, 11, 4}, {2, 3, 3}, {2, 1, 11, 12, 4}});
  129. checker.execs({{20, 300, 10, 11, 4}, {20, 3, 3}, {20, 300, 11, 12, 4}});
  130. checker.execs(
  131. {{2200, 3, 10, 11, 4}, {2200, 3, 3}, {2200, 3, 11, 12, 4}});
  132. checker.execs({{1, 25, 25, 25, 4}, {1, 3, 3}, {1, 25, 25, 510, 4}});
  133. checker.execs({{1, 25, 25, 510, 4}, {1, 3, 3}, {1, 25, 25, 25, 4}});
  134. checker.execs({{1, 25, 25, 25, 4}, {1, 3, 3}, {1, 25, 51, 51, 4}});
  135. checker.execs({{1, 25, 51, 51, 4}, {1, 3, 3}, {1, 25, 25, 25, 4}});
  136. break;
  137. }
  138. }
  139. TEST_F(NAIVE, WARP_PERSPECTIVE) {
  140. Checker<WarpPerspective> checker(handle(), false);
  141. WarpPerspective::Param param;
  142. param.bmode = WarpPerspective::Param::BorderMode::BORDER_REFLECT;
  143. param.imode = WarpPerspective::Param::InterpolationMode::LINEAR;
  144. param.format = WarpPerspective::Param::Format::NCHW;
  145. checker.set_param(param).exect(
  146. Testcase{TensorValue({1, 1, 3, 3}, dtype::Uint8{},
  147. {131, 255, 180, 245, 8, 0, 10, 3, 178}),
  148. TensorValue({1, 3, 3}, dtype::Float32{},
  149. {1.2f, 1.2f, 0.6f, -1.05f, -2.0f, -0.7f, 1.3f,
  150. 1.5f, 3.0f}),
  151. {}},
  152. Testcase{{},
  153. {},
  154. TensorValue({1, 1, 2, 2}, dtype::Uint8{},
  155. {156, 183, 181, 195})});
  156. checker.set_param(param).exect(
  157. Testcase{TensorValue({1, 1, 3, 3},
  158. dtype::Quantized8Asymm{
  159. 1.4f, static_cast<uint8_t>(127)},
  160. {131, 255, 180, 245, 8, 0, 10, 3, 178}),
  161. TensorValue({1, 3, 3}, dtype::Float32{},
  162. {1.2f, 1.2f, 0.6f, -1.05f, -2.0f, -0.7f, 1.3f,
  163. 1.5f, 3.0f}),
  164. {}},
  165. Testcase{{},
  166. {},
  167. TensorValue({1, 1, 2, 2},
  168. dtype::Quantized8Asymm{
  169. 1.4f, static_cast<uint8_t>(127)},
  170. {156, 183, 181, 195})});
  171. }
  172. TEST_F(NAIVE, WARP_PERSPECTIVE_NCHW_QINT4) {
  173. Checker<WarpPerspective> checker(handle(), false);
  174. WarpPerspective::Param param;
  175. param.bmode = WarpPerspective::Param::BorderMode::BORDER_REFLECT;
  176. param.imode = WarpPerspective::Param::InterpolationMode::LINEAR;
  177. param.format = WarpPerspective::Param::Format::NCHW;
  178. std::vector<int> input_values = {-1, -3, -2, -2, 0, 0, 0, 0, -2},
  179. output_values = {-1, -2, -2, -2};
  180. checker.set_param(param).exect(
  181. Testcase{TensorValueLowbit4({1, 1, 3, 3}, dtype::QuantizedS4(0.1),
  182. input_values),
  183. TensorValue({1, 3, 3}, dtype::Float32{},
  184. {1.2f, 1.2f, 0.6f, -1.05f, -2.0f, -0.7f, 1.3f,
  185. 1.5f, 3.0f}),
  186. {}},
  187. Testcase{{},
  188. {},
  189. TensorValueLowbit4({1, 1, 2, 2}, dtype::QuantizedS4(0.1),
  190. output_values)});
  191. }
  192. TEST_F(NAIVE, WARP_PERSPECTIVE_NCHW_QUINT4) {
  193. Checker<WarpPerspective> checker(handle(), false);
  194. WarpPerspective::Param param;
  195. param.bmode = WarpPerspective::Param::BorderMode::BORDER_REFLECT;
  196. param.imode = WarpPerspective::Param::InterpolationMode::LINEAR;
  197. param.format = WarpPerspective::Param::Format::NCHW;
  198. std::vector<int> input_values = {4, 13, 0, 0, 0, 0, 0, 0, 0},
  199. output_values = {6, 8, 8, 9};
  200. checker.set_param(param).exect(
  201. Testcase{TensorValueLowbit4({1, 1, 3, 3},
  202. dtype::Quantized4Asymm(0.1, 3),
  203. input_values),
  204. TensorValue({1, 3, 3}, dtype::Float32{},
  205. {1.2f, 1.2f, 0.6f, -1.05f, -2.0f, -0.7f, 1.3f,
  206. 1.5f, 3.0f}),
  207. {}},
  208. Testcase{{},
  209. {},
  210. TensorValueLowbit4({1, 1, 2, 2},
  211. dtype::Quantized4Asymm(0.1, 3),
  212. output_values)});
  213. }
  214. TEST_F(NAIVE_MULTI_THREADS, WARP_PERSPECTIVE_NCHW4) {
  215. using Param = WarpPerspective::Param;
  216. auto convert_true_format = [](const TensorLayout& layout) {
  217. if (layout.ndim == 4)
  218. return layout
  219. .reshape(
  220. {layout[0], layout[1] / 4, layout[2], layout[3], 4})
  221. .dimshuffle({0, 1, 4, 2, 3});
  222. else
  223. return layout;
  224. };
  225. WarpPerspective::Param param;
  226. auto extra_impl = [&param, this,
  227. convert_true_format](const TensorNDArray& tensors) {
  228. auto warp_perspective = handle()->create_operator<WarpPerspective>();
  229. warp_perspective->param() = param;
  230. warp_perspective->param().format = Param::Format::NCHW;
  231. TensorNDArray nchw_tensors;
  232. for (size_t i = 0; i < tensors.size(); ++i) {
  233. auto layout = tensors[i].layout;
  234. if (layout.dtype.enumv() == DTypeEnum::QuantizedS8)
  235. layout.dtype = dtype::Int8();
  236. if (layout.ndim == 5) {
  237. layout = layout.reshape({layout[0], layout[1] * layout[4],
  238. layout[2], layout[3]});
  239. }
  240. nchw_tensors.emplace_back(malloc(layout.span().dist_byte()),
  241. layout);
  242. }
  243. TensorNDArray nchw4_tensors;
  244. for (size_t i = 0; i < tensors.size(); ++i) {
  245. auto layout = convert_true_format(nchw_tensors[i].layout);
  246. nchw4_tensors.emplace_back(tensors[i].raw_ptr, std::move(layout));
  247. }
  248. auto workspace_size = warp_perspective->get_workspace_in_bytes(
  249. tensors[0].layout, tensors[1].layout, tensors[2].layout);
  250. dt_byte* workspace_ptr = static_cast<dt_byte*>(malloc(workspace_size));
  251. Workspace workspace{workspace_ptr, workspace_size};
  252. auto relayout = handle()->create_operator<RelayoutForward>();
  253. relayout->exec(nchw4_tensors[0], nchw_tensors[0]);
  254. relayout->exec(nchw4_tensors[1], nchw_tensors[1]);
  255. warp_perspective->exec(nchw_tensors[0], nchw_tensors[1],
  256. nchw_tensors[2], workspace);
  257. relayout->exec(nchw_tensors[2], nchw4_tensors[2]);
  258. free(workspace_ptr);
  259. for (auto&& tensor : nchw_tensors) {
  260. free(tensor.raw_ptr);
  261. }
  262. };
  263. Checker<WarpPerspectiveForward> checker(handle());
  264. WarpPerspectiveMatRNG rng;
  265. checker.set_rng(1, &rng);
  266. checker.set_dtype(0, dtype::QuantizedS8(0.1f));
  267. checker.set_dtype(2, dtype::QuantizedS8(0.1f));
  268. checker.set_extra_opr_impl(extra_impl);
  269. for (auto bmode : {WarpPerspective::BorderMode::WRAP,
  270. WarpPerspective::BorderMode::REFLECT,
  271. WarpPerspective::BorderMode::REPLICATE,
  272. WarpPerspective::BorderMode::CONSTANT}) {
  273. param.border_val = 0.3f;
  274. param.bmode = bmode;
  275. param.imode = Param::InterpolationMode::LINEAR;
  276. param.format = Param::Format::NCHW4;
  277. checker.set_param(param);
  278. checker.execs({{2, 1, 10, 11, 4}, {2, 3, 3}, {2, 1, 11, 12, 4}});
  279. checker.execs({{20, 300, 10, 11, 4}, {20, 3, 3}, {20, 300, 11, 12, 4}});
  280. checker.execs(
  281. {{2200, 3, 10, 11, 4}, {2200, 3, 3}, {2200, 3, 11, 12, 4}});
  282. checker.execs({{1, 25, 25, 25, 4}, {1, 3, 3}, {1, 25, 25, 510, 4}});
  283. checker.execs({{1, 25, 25, 510, 4}, {1, 3, 3}, {1, 25, 25, 25, 4}});
  284. checker.execs({{1, 25, 25, 25, 4}, {1, 3, 3}, {1, 25, 51, 51, 4}});
  285. checker.execs({{1, 25, 51, 51, 4}, {1, 3, 3}, {1, 25, 25, 25, 4}});
  286. break;
  287. }
  288. }
  289. TEST_F(NAIVE_MULTI_THREADS, WARP_PERSPECTIVE) {
  290. Checker<WarpPerspective> checker(handle(), false);
  291. WarpPerspective::Param param;
  292. param.bmode = WarpPerspective::Param::BorderMode::BORDER_REFLECT;
  293. param.imode = WarpPerspective::Param::InterpolationMode::LINEAR;
  294. param.format = WarpPerspective::Param::Format::NCHW;
  295. checker.set_param(param).exect(
  296. Testcase{TensorValue({1, 1, 3, 3}, dtype::Uint8{},
  297. {131, 255, 180, 245, 8, 0, 10, 3, 178}),
  298. TensorValue({1, 3, 3}, dtype::Float32{},
  299. {1.2f, 1.2f, 0.6f, -1.05f, -2.0f, -0.7f, 1.3f,
  300. 1.5f, 3.0f}),
  301. {}},
  302. Testcase{{},
  303. {},
  304. TensorValue({1, 1, 2, 2}, dtype::Uint8{},
  305. {156, 183, 181, 195})});
  306. checker.set_param(param).exect(
  307. Testcase{TensorValue({1, 1, 3, 3},
  308. dtype::Quantized8Asymm{
  309. 1.4f, static_cast<uint8_t>(127)},
  310. {131, 255, 180, 245, 8, 0, 10, 3, 178}),
  311. TensorValue({1, 3, 3}, dtype::Float32{},
  312. {1.2f, 1.2f, 0.6f, -1.05f, -2.0f, -0.7f, 1.3f,
  313. 1.5f, 3.0f}),
  314. {}},
  315. Testcase{{},
  316. {},
  317. TensorValue({1, 1, 2, 2},
  318. dtype::Quantized8Asymm{
  319. 1.4f, static_cast<uint8_t>(127)},
  320. {156, 183, 181, 195})});
  321. }
  322. TEST_F(NAIVE_MULTI_THREADS, WARP_PERSPECTIVE_FORWARD_HWCD4) {
  323. auto handle_multi_thread = handle();
  324. Checker<WarpPerspective> checker(handle(), false);
  325. TensorFormat img_fmt =
  326. Image2DPack4TensorFormat::make(2, handle_multi_thread);
  327. checker.set_fmt(0, img_fmt).set_fmt(2, img_fmt);
  328. for (auto dtype : std::vector<DType>{
  329. dtype::Float32(), dtype::Float16(), dtype::QuantizedS8(4.3f),
  330. dtype::Quantized8Asymm(2.4f, static_cast<uint8_t>(10))}) {
  331. for (auto bmode : {WarpPerspective::BorderMode::WRAP,
  332. WarpPerspective::BorderMode::REFLECT,
  333. WarpPerspective::BorderMode::CONSTANT,
  334. WarpPerspective::BorderMode::REPLICATE,
  335. WarpPerspective::BorderMode::CONSTANT}) {
  336. WarpPerspectiveMatRNG rng;
  337. checker.set_rng(1, &rng);
  338. WarpPerspective::Param param;
  339. param.border_val = 0.3f;
  340. param.bmode = bmode;
  341. param.imode = param::WarpPerspective::InterpolationMode::LINEAR;
  342. param.format = param::WarpPerspective::Format::NHWCD4;
  343. if (dtype == dtype::Float16()) {
  344. //! if exists error, the value of a result pixel maybe another
  345. //! pixel in the origin image, so we just consider the avg error
  346. checker.set_epsilon(2e-1);
  347. checker.set_max_avg_error(1e-2);
  348. }
  349. checker.set_param(param);
  350. checker.set_dtype(0, dtype);
  351. checker.set_dtype(2, dtype);
  352. if (dtype.category() == DTypeCategory::FLOAT) {
  353. checker.set_dtype(1, dtype);
  354. } else {
  355. checker.set_dtype(1, dtype::Float32());
  356. }
  357. checker.execs({{2, 10, 1, 11, 4}, {2, 3, 3}, {2, 11, 1, 12, 4}});
  358. checker.execs({{22, 10, 1, 11, 4}, {22, 3, 3}, {22, 11, 1, 12, 4}});
  359. }
  360. }
  361. #if MEGDNN_TEST_ASAN
  362. //! asan detect nan will make test failed
  363. #else
  364. // nan case
  365. NanMatRNG rng_nan;
  366. UniformFloatRNG rng_zero(0, 0);
  367. //! NanMatRng not support float16, I have to reset dtype to Float32
  368. checker.set_dtype(0, dtype::Float32())
  369. .set_dtype(1, dtype::Float32())
  370. .set_dtype(2, dtype::Float32());
  371. for (auto rng : std::vector<RNG*>{&rng_nan, &rng_zero}) {
  372. param::WarpPerspective param;
  373. param.bmode = param::WarpPerspective::BorderMode::CONSTANT;
  374. param.imode = param::WarpPerspective::InterpolationMode::LINEAR;
  375. param.format = param::WarpPerspective::Format::NHWCD4;
  376. checker.set_rng(1, rng);
  377. param.border_val = 1.737;
  378. checker.set_param(param);
  379. checker.exec({{10, 10, 1, 11, 4}, {10, 3, 3}, {10, 12, 1, 13, 4}});
  380. }
  381. #endif
  382. }
  383. #if MEGDNN_WITH_BENCHMARK
  384. namespace {
  385. void benchmark_impl(const typename WarpPerspective::Param& param,
  386. std::vector<SmallVector<TensorShape>> shapes, size_t RUNS,
  387. TaskExecutorConfig&& multi_thread_config,
  388. TaskExecutorConfig&& single_thread_config) {
  389. std::vector<float> multi_thread_times, single_thread_times;
  390. {
  391. auto multi_thread_hanle =
  392. create_cpu_handle(0, true, &multi_thread_config);
  393. auto benchmarker =
  394. Benchmarker<WarpPerspective>(multi_thread_hanle.get());
  395. benchmarker.set_times(RUNS).set_display(false).set_param(param);
  396. for (auto shape : shapes) {
  397. multi_thread_times.push_back(benchmarker.exec(shape) / RUNS);
  398. }
  399. }
  400. {
  401. auto single_thread_handle =
  402. create_cpu_handle(0, true, &single_thread_config);
  403. auto benchmarker =
  404. Benchmarker<WarpPerspective>(single_thread_handle.get());
  405. benchmarker.set_times(RUNS).set_display(false).set_param(param);
  406. for (auto shape : shapes) {
  407. single_thread_times.push_back(benchmarker.exec(shape) / RUNS);
  408. }
  409. }
  410. printf("Benchmark : Multi threads %zu, ", multi_thread_config.nr_thread);
  411. printf("core_ids:");
  412. for (size_t i = 0; i < multi_thread_config.affinity_core_set.size(); i++) {
  413. printf("%zu ", multi_thread_config.affinity_core_set[i]);
  414. }
  415. printf(", Single thread core_id %zu\n",
  416. single_thread_config.affinity_core_set[0]);
  417. for (size_t i = 0; i < shapes.size(); i++) {
  418. auto shape = shapes[i];
  419. printf("Case: ");
  420. for (auto sh : shape)
  421. printf("%s ", sh.to_string().c_str());
  422. printf("%zu threads time: %f,\n single thread time: "
  423. "%f. spead up = %f, speedup/cores=%f\n",
  424. multi_thread_config.nr_thread, multi_thread_times[i],
  425. single_thread_times[i],
  426. single_thread_times[i] / multi_thread_times[i],
  427. single_thread_times[i] / multi_thread_times[i] /
  428. multi_thread_config.nr_thread);
  429. }
  430. }
  431. } // namespace
  432. TEST_F(NAIVE_BENCHMARK_MULTI_THREADS, BENCHMARK_WARP_PERSPECTIVE) {
  433. constexpr size_t RUNS = 50;
  434. using BMode = param::WarpPerspective::BorderMode;
  435. using IMode = param::WarpPerspective::InterpolationMode;
  436. WarpPerspective::Param param;
  437. param.border_val = 0.3f;
  438. param.format = param::WarpPerspective::Format::NCHW;
  439. param.imode = IMode::INTER_LINEAR;
  440. param.bmode = BMode::REPLICATE;
  441. std::vector<SmallVector<TensorShape>> shapes;
  442. auto bench_case = [&](size_t N, size_t H, size_t W, size_t C) {
  443. SmallVector<TensorShape> shape{
  444. {N, C, H, W}, {N, 3, 3}, {N, C, 224, 224}};
  445. shapes.push_back(shape);
  446. };
  447. bench_case(1, 700, 490, 10);
  448. bench_case(1, 700, 490, 20);
  449. bench_case(1, 700, 490, 30);
  450. bench_case(1, 500, 334, 10);
  451. bench_case(1, 500, 334, 20);
  452. bench_case(1, 500, 334, 30);
  453. bench_case(1, 140, 144, 10);
  454. bench_case(1, 140, 144, 20);
  455. bench_case(1, 140, 114, 30);
  456. printf("Benchmark warp perspective\n");
  457. benchmark_impl(param, shapes, RUNS, {4, {4, 5, 6, 7}}, {1, {4}});
  458. benchmark_impl(param, shapes, RUNS, {4, {4, 5, 6, 7}}, {1, {7}});
  459. benchmark_impl(param, shapes, RUNS, {2, {4, 5}}, {1, {4}});
  460. }
  461. #endif
  462. TEST_F(NAIVE, WARP_PERSPECTIVE_BFLOAT16) {
  463. Checker<WarpPerspective> checker(handle(), false);
  464. WarpPerspective::Param p;
  465. p.bmode = WarpPerspective::Param::BorderMode::BORDER_REFLECT;
  466. p.imode = WarpPerspective::Param::InterpolationMode::LINEAR;
  467. p.format = WarpPerspective::Param::Format::NCHW;
  468. auto extra_impl = extra_impl_helper<WarpPerspective>(handle(), p);
  469. checker.set_param(p)
  470. .set_epsilon(1e-1)
  471. .set_dtype(0, dtype::BFloat16())
  472. .set_dtype(1, dtype::Float32())
  473. .set_dtype(2, dtype::BFloat16())
  474. .set_extra_opr_impl(extra_impl)
  475. .execs({{1, 1, 3, 3}, {1, 3, 3}, {1, 1, 2, 2}})
  476. .execs({{1000, 2, 10, 11}, {1000, 3, 3}, {1000, 2, 12, 13}});
  477. }
  478. TEST_F(NAIVE, WARP_PERSPECTIVE_BACKWARD_DATA_BFLOAT16) {
  479. Checker<WarpPerspectiveBackwardData> checker(handle(), false);
  480. WarpPerspectiveBackwardData::Param p;
  481. p.bmode = WarpPerspectiveBackwardData::Param::BorderMode::BORDER_REFLECT;
  482. p.imode = WarpPerspectiveBackwardData::Param::InterpolationMode::LINEAR;
  483. p.format = WarpPerspectiveBackwardData::Param::Format::NCHW;
  484. auto extra_impl =
  485. extra_impl_helper<WarpPerspectiveBackwardData>(handle(), p);
  486. checker.set_param(p)
  487. .set_dtype(0, dtype::Float32())
  488. .set_dtype(1, dtype::BFloat16())
  489. .set_dtype(2, dtype::BFloat16())
  490. .set_extra_opr_impl(extra_impl)
  491. .set_epsilon(1e-1)
  492. .execs({{1, 3, 3}, {1, 1, 2, 2}, {1, 1, 3, 3}});
  493. }
  494. TEST_F(NAIVE, WARP_PERSPECTIVE_BACKWARD_MAT_BFLOAT16) {
  495. Checker<WarpPerspectiveBackwardMat> checker(handle(), false);
  496. WarpPerspectiveBackwardMat::Param p;
  497. p.bmode = WarpPerspectiveBackwardMat::Param::BorderMode::BORDER_REFLECT;
  498. p.imode = WarpPerspectiveBackwardMat::Param::InterpolationMode::LINEAR;
  499. p.format = WarpPerspectiveBackwardMat::Param::Format::NCHW;
  500. p.border_val = 0.3f;
  501. auto extra_impl =
  502. extra_impl_helper<WarpPerspectiveBackwardMat>(handle(), p);
  503. checker.set_param(p)
  504. .set_dtype(0, dtype::BFloat16())
  505. .set_dtype(1, dtype::Float32())
  506. .set_dtype(2, dtype::BFloat16())
  507. .set_dtype(3, dtype::Float32())
  508. .set_extra_opr_impl(extra_impl)
  509. .set_epsilon(1e-1)
  510. .execs({{1000, 3, 11, 12},
  511. {1000, 3, 3},
  512. {1000, 3, 10, 11},
  513. {1000, 3, 3}});
  514. }
  515. TEST_F(NAIVE, WARP_PERSPECTIVE_NCHW64) {
  516. using Param = WarpPerspective::Param;
  517. auto convert_true_format = [](const TensorLayout& layout) {
  518. if (layout.ndim == 4) {
  519. TensorLayout ret{
  520. {layout[0], layout[1] / 64, layout[2], layout[3], 64},
  521. layout.dtype};
  522. return ret.dimshuffle({0, 1, 4, 2, 3});
  523. } else
  524. return layout;
  525. };
  526. WarpPerspective::Param param;
  527. auto extra_impl = [&param, this,
  528. convert_true_format](const TensorNDArray& tensors) {
  529. auto warp_perspective = handle()->create_operator<WarpPerspective>();
  530. warp_perspective->param() = param;
  531. warp_perspective->param().format = Param::Format::NCHW;
  532. TensorNDArray nchw_tensors;
  533. for (size_t i = 0; i < tensors.size(); ++i) {
  534. TensorLayout ly;
  535. auto layout = tensors[i].layout;
  536. if (tensors[i].layout.ndim == 5) {
  537. ly = TensorLayout{{layout[0], layout[1] * layout[4], layout[2],
  538. layout[3]},
  539. layout.dtype};
  540. } else {
  541. ly = layout;
  542. }
  543. nchw_tensors.emplace_back(malloc(ly.span().dist_byte()), ly);
  544. }
  545. TensorNDArray nchw64_tensors;
  546. for (size_t i = 0; i < tensors.size(); ++i) {
  547. auto layout = convert_true_format(nchw_tensors[i].layout);
  548. nchw64_tensors.emplace_back(tensors[i].raw_ptr, std::move(layout));
  549. }
  550. auto workspace_size = warp_perspective->get_workspace_in_bytes(
  551. tensors[0].layout, tensors[1].layout, tensors[2].layout);
  552. dt_byte* workspace_ptr = static_cast<dt_byte*>(malloc(workspace_size));
  553. Workspace workspace{workspace_ptr, workspace_size};
  554. auto relayout = handle()->create_operator<RelayoutForward>();
  555. relayout->exec(nchw64_tensors[0], nchw_tensors[0]);
  556. relayout->exec(nchw64_tensors[1], nchw_tensors[1]);
  557. warp_perspective->exec(nchw_tensors[0], nchw_tensors[1],
  558. nchw_tensors[2], workspace);
  559. relayout->exec(nchw_tensors[2], nchw64_tensors[2]);
  560. free(workspace_ptr);
  561. for (auto&& tensor : nchw_tensors) {
  562. free(tensor.raw_ptr);
  563. }
  564. };
  565. Checker<WarpPerspectiveForward> checker(handle());
  566. WarpPerspectiveMatRNG rng;
  567. checker.set_rng(1, &rng);
  568. checker.set_dtype(0, dtype::QuantizedS4(0.1f));
  569. checker.set_dtype(2, dtype::QuantizedS4(0.1f));
  570. checker.set_extra_opr_impl(extra_impl);
  571. for (auto bmode : {WarpPerspective::BorderMode::WRAP,
  572. WarpPerspective::BorderMode::REFLECT,
  573. WarpPerspective::BorderMode::REPLICATE,
  574. WarpPerspective::BorderMode::CONSTANT}) {
  575. param.border_val = 0.3f;
  576. param.bmode = bmode;
  577. param.imode = Param::InterpolationMode::LINEAR;
  578. param.format = Param::Format::NCHW64;
  579. checker.set_param(param);
  580. checker.execs({{2, 1, 10, 10, 64}, {2, 3, 3}, {2, 1, 10, 12, 64}});
  581. checker.execs({{20, 3, 10, 12, 64}, {20, 3, 3}, {20, 3, 11, 12, 64}});
  582. checker.execs({{1, 3, 25, 24, 64}, {1, 3, 3}, {1, 3, 25, 51, 64}});
  583. checker.execs({{1, 3, 25, 51, 64}, {1, 3, 3}, {1, 3, 25, 24, 64}});
  584. checker.execs({{1, 3, 25, 24, 64}, {1, 3, 3}, {1, 3, 51, 50, 64}});
  585. checker.execs({{1, 3, 51, 50, 64}, {1, 3, 3}, {1, 3, 25, 24, 64}});
  586. }
  587. }
  588. TEST_F(NAIVE, WARP_PERSPECTIVE_NHWC) {
  589. using Param = WarpPerspective::Param;
  590. auto convert_true_format = [](const TensorLayout& layout) {
  591. if (layout.ndim == 4) {
  592. TensorLayout ret{{layout[0], layout[2], layout[3], layout[1]},
  593. layout.dtype};
  594. return ret.dimshuffle({0, 3, 1, 2});
  595. } else
  596. return layout;
  597. };
  598. WarpPerspective::Param param;
  599. auto extra_impl = [&param, this,
  600. convert_true_format](const TensorNDArray& tensors) {
  601. auto warp_perspective = handle()->create_operator<WarpPerspective>();
  602. warp_perspective->param() = param;
  603. warp_perspective->param().format = Param::Format::NCHW;
  604. TensorNDArray nchw_tensors;
  605. for (size_t i = 0; i < tensors.size(); ++i) {
  606. TensorLayout ly;
  607. auto layout = tensors[i].layout;
  608. if (layout.ndim == 4) {
  609. ly = TensorLayout{{layout[0], layout[3], layout[1], layout[2]},
  610. layout.dtype};
  611. } else {
  612. ly = layout;
  613. }
  614. nchw_tensors.emplace_back(malloc(ly.span().dist_byte()), ly);
  615. }
  616. TensorNDArray nhwc_tensors;
  617. for (size_t i = 0; i < tensors.size(); ++i) {
  618. auto layout = convert_true_format(nchw_tensors[i].layout);
  619. nhwc_tensors.emplace_back(tensors[i].raw_ptr, std::move(layout));
  620. }
  621. auto workspace_size = warp_perspective->get_workspace_in_bytes(
  622. tensors[0].layout, tensors[1].layout, tensors[2].layout);
  623. dt_byte* workspace_ptr = static_cast<dt_byte*>(malloc(workspace_size));
  624. Workspace workspace{workspace_ptr, workspace_size};
  625. auto relayout = handle()->create_operator<RelayoutForward>();
  626. relayout->exec(nhwc_tensors[0], nchw_tensors[0]);
  627. relayout->exec(nhwc_tensors[1], nchw_tensors[1]);
  628. warp_perspective->exec(nchw_tensors[0], nchw_tensors[1],
  629. nchw_tensors[2], workspace);
  630. relayout->exec(nchw_tensors[2], nhwc_tensors[2]);
  631. free(workspace_ptr);
  632. for (auto&& tensor : nchw_tensors) {
  633. free(tensor.raw_ptr);
  634. }
  635. };
  636. {
  637. Checker<WarpPerspectiveForward> checker(handle());
  638. WarpPerspectiveMatRNG rng;
  639. checker.set_rng(1, &rng);
  640. checker.set_dtype(0, dtype::QuantizedS4(0.1f));
  641. checker.set_dtype(2, dtype::QuantizedS4(0.1f));
  642. checker.set_extra_opr_impl(extra_impl);
  643. for (auto bmode : {WarpPerspective::BorderMode::WRAP,
  644. WarpPerspective::BorderMode::REFLECT,
  645. WarpPerspective::BorderMode::REPLICATE,
  646. WarpPerspective::BorderMode::CONSTANT}) {
  647. param.border_val = 0.3f;
  648. param.bmode = bmode;
  649. param.imode = Param::InterpolationMode::LINEAR;
  650. param.format = Param::Format::NHWC;
  651. checker.set_param(param);
  652. checker.execs({{1, 2, 2, 4}, {1, 3, 3}, {1, 2, 2, 4}});
  653. checker.execs({{2, 10, 10, 4}, {2, 3, 3}, {2, 10, 12, 4}});
  654. checker.execs({{3, 25, 24, 8}, {3, 3, 3}, {3, 12, 10, 8}});
  655. checker.execs({{4, 33, 22, 16}, {4, 3, 3}, {4, 9, 12, 16}});
  656. }
  657. }
  658. {
  659. Checker<WarpPerspectiveForward> checker(handle());
  660. WarpPerspectiveMatRNG rng;
  661. checker.set_rng(1, &rng);
  662. checker.set_dtype(0, dtype::Quantized4Asymm(0.1f, 3));
  663. checker.set_dtype(2, dtype::Quantized4Asymm(0.1f, 3));
  664. checker.set_extra_opr_impl(extra_impl);
  665. for (auto bmode : {WarpPerspective::BorderMode::WRAP,
  666. WarpPerspective::BorderMode::REFLECT,
  667. WarpPerspective::BorderMode::REPLICATE,
  668. WarpPerspective::BorderMode::CONSTANT}) {
  669. param.border_val = 0.3f;
  670. param.bmode = bmode;
  671. param.imode = Param::InterpolationMode::LINEAR;
  672. param.format = Param::Format::NHWC;
  673. checker.set_param(param);
  674. checker.execs({{1, 2, 2, 4}, {1, 3, 3}, {1, 2, 2, 4}});
  675. checker.execs({{2, 10, 10, 4}, {2, 3, 3}, {2, 10, 12, 4}});
  676. checker.execs({{3, 25, 24, 8}, {3, 3, 3}, {3, 12, 10, 8}});
  677. checker.execs({{4, 33, 22, 16}, {4, 3, 3}, {4, 9, 12, 16}});
  678. }
  679. }
  680. }
  681. // vim: syntax=cpp.doxygen

MegEngine 安装包中集成了使用 GPU 运行代码所需的 CUDA 环境,不用区分 CPU 和 GPU 版。 如果想要运行 GPU 程序,请确保机器本身配有 GPU 硬件设备并安装好驱动。 如果你想体验在云端 GPU 算力平台进行深度学习开发的感觉,欢迎访问 MegStudio 平台