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 26 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629
  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_MULTI_THREADS, WARP_PERSPECTIVE_NCHW4) {
  193. using Param = WarpPerspective::Param;
  194. auto convert_true_format = [](const TensorLayout& layout) {
  195. if (layout.ndim == 4)
  196. return layout
  197. .reshape(
  198. {layout[0], layout[1] / 4, layout[2], layout[3], 4})
  199. .dimshuffle({0, 1, 4, 2, 3});
  200. else
  201. return layout;
  202. };
  203. WarpPerspective::Param param;
  204. auto extra_impl = [&param, this,
  205. convert_true_format](const TensorNDArray& tensors) {
  206. auto warp_perspective = handle()->create_operator<WarpPerspective>();
  207. warp_perspective->param() = param;
  208. warp_perspective->param().format = Param::Format::NCHW;
  209. TensorNDArray nchw_tensors;
  210. for (size_t i = 0; i < tensors.size(); ++i) {
  211. auto layout = tensors[i].layout;
  212. if (layout.dtype.enumv() == DTypeEnum::QuantizedS8)
  213. layout.dtype = dtype::Int8();
  214. if (layout.ndim == 5) {
  215. layout = layout.reshape({layout[0], layout[1] * layout[4],
  216. layout[2], layout[3]});
  217. }
  218. nchw_tensors.emplace_back(malloc(layout.span().dist_byte()),
  219. layout);
  220. }
  221. TensorNDArray nchw4_tensors;
  222. for (size_t i = 0; i < tensors.size(); ++i) {
  223. auto layout = convert_true_format(nchw_tensors[i].layout);
  224. nchw4_tensors.emplace_back(tensors[i].raw_ptr, std::move(layout));
  225. }
  226. auto workspace_size = warp_perspective->get_workspace_in_bytes(
  227. tensors[0].layout, tensors[1].layout, tensors[2].layout);
  228. dt_byte* workspace_ptr = static_cast<dt_byte*>(malloc(workspace_size));
  229. Workspace workspace{workspace_ptr, workspace_size};
  230. auto relayout = handle()->create_operator<RelayoutForward>();
  231. relayout->exec(nchw4_tensors[0], nchw_tensors[0]);
  232. relayout->exec(nchw4_tensors[1], nchw_tensors[1]);
  233. warp_perspective->exec(nchw_tensors[0], nchw_tensors[1],
  234. nchw_tensors[2], workspace);
  235. relayout->exec(nchw_tensors[2], nchw4_tensors[2]);
  236. free(workspace_ptr);
  237. for (auto&& tensor : nchw_tensors) {
  238. free(tensor.raw_ptr);
  239. }
  240. };
  241. Checker<WarpPerspectiveForward> checker(handle());
  242. WarpPerspectiveMatRNG rng;
  243. checker.set_rng(1, &rng);
  244. checker.set_dtype(0, dtype::QuantizedS8(0.1f));
  245. checker.set_dtype(2, dtype::QuantizedS8(0.1f));
  246. checker.set_extra_opr_impl(extra_impl);
  247. for (auto bmode : {WarpPerspective::BorderMode::WRAP,
  248. WarpPerspective::BorderMode::REFLECT,
  249. WarpPerspective::BorderMode::REPLICATE,
  250. WarpPerspective::BorderMode::CONSTANT}) {
  251. param.border_val = 0.3f;
  252. param.bmode = bmode;
  253. param.imode = Param::InterpolationMode::LINEAR;
  254. param.format = Param::Format::NCHW4;
  255. checker.set_param(param);
  256. checker.execs({{2, 1, 10, 11, 4}, {2, 3, 3}, {2, 1, 11, 12, 4}});
  257. checker.execs({{20, 300, 10, 11, 4}, {20, 3, 3}, {20, 300, 11, 12, 4}});
  258. checker.execs(
  259. {{2200, 3, 10, 11, 4}, {2200, 3, 3}, {2200, 3, 11, 12, 4}});
  260. checker.execs({{1, 25, 25, 25, 4}, {1, 3, 3}, {1, 25, 25, 510, 4}});
  261. checker.execs({{1, 25, 25, 510, 4}, {1, 3, 3}, {1, 25, 25, 25, 4}});
  262. checker.execs({{1, 25, 25, 25, 4}, {1, 3, 3}, {1, 25, 51, 51, 4}});
  263. checker.execs({{1, 25, 51, 51, 4}, {1, 3, 3}, {1, 25, 25, 25, 4}});
  264. break;
  265. }
  266. }
  267. TEST_F(NAIVE_MULTI_THREADS, WARP_PERSPECTIVE) {
  268. Checker<WarpPerspective> checker(handle(), false);
  269. WarpPerspective::Param param;
  270. param.bmode = WarpPerspective::Param::BorderMode::BORDER_REFLECT;
  271. param.imode = WarpPerspective::Param::InterpolationMode::LINEAR;
  272. param.format = WarpPerspective::Param::Format::NCHW;
  273. checker.set_param(param).exect(
  274. Testcase{TensorValue({1, 1, 3, 3}, dtype::Uint8{},
  275. {131, 255, 180, 245, 8, 0, 10, 3, 178}),
  276. TensorValue({1, 3, 3}, dtype::Float32{},
  277. {1.2f, 1.2f, 0.6f, -1.05f, -2.0f, -0.7f, 1.3f,
  278. 1.5f, 3.0f}),
  279. {}},
  280. Testcase{{},
  281. {},
  282. TensorValue({1, 1, 2, 2}, dtype::Uint8{},
  283. {156, 183, 181, 195})});
  284. checker.set_param(param).exect(
  285. Testcase{TensorValue({1, 1, 3, 3},
  286. dtype::Quantized8Asymm{
  287. 1.4f, static_cast<uint8_t>(127)},
  288. {131, 255, 180, 245, 8, 0, 10, 3, 178}),
  289. TensorValue({1, 3, 3}, dtype::Float32{},
  290. {1.2f, 1.2f, 0.6f, -1.05f, -2.0f, -0.7f, 1.3f,
  291. 1.5f, 3.0f}),
  292. {}},
  293. Testcase{{},
  294. {},
  295. TensorValue({1, 1, 2, 2},
  296. dtype::Quantized8Asymm{
  297. 1.4f, static_cast<uint8_t>(127)},
  298. {156, 183, 181, 195})});
  299. }
  300. TEST_F(NAIVE_MULTI_THREADS, WARP_PERSPECTIVE_FORWARD_HWCD4) {
  301. auto handle_multi_thread = handle();
  302. Checker<WarpPerspective> checker(handle(), false);
  303. TensorFormat img_fmt =
  304. Image2DPack4TensorFormat::make(2, handle_multi_thread);
  305. checker.set_fmt(0, img_fmt).set_fmt(2, img_fmt);
  306. for (auto dtype : std::vector<DType>{
  307. dtype::Float32(), dtype::Float16(), dtype::QuantizedS8(4.3f),
  308. dtype::Quantized8Asymm(2.4f, static_cast<uint8_t>(10))}) {
  309. for (auto bmode : {WarpPerspective::BorderMode::WRAP,
  310. WarpPerspective::BorderMode::REFLECT,
  311. WarpPerspective::BorderMode::CONSTANT,
  312. WarpPerspective::BorderMode::REPLICATE,
  313. WarpPerspective::BorderMode::CONSTANT}) {
  314. WarpPerspectiveMatRNG rng;
  315. checker.set_rng(1, &rng);
  316. WarpPerspective::Param param;
  317. param.border_val = 0.3f;
  318. param.bmode = bmode;
  319. param.imode = param::WarpPerspective::InterpolationMode::LINEAR;
  320. param.format = param::WarpPerspective::Format::NHWCD4;
  321. if (dtype == dtype::Float16()) {
  322. //! if exists error, the value of a result pixel maybe another
  323. //! pixel in the origin image, so we just consider the avg error
  324. checker.set_epsilon(2e-1);
  325. checker.set_max_avg_error(1e-2);
  326. }
  327. checker.set_param(param);
  328. checker.set_dtype(0, dtype);
  329. checker.set_dtype(2, dtype);
  330. if (dtype.category() == DTypeCategory::FLOAT) {
  331. checker.set_dtype(1, dtype);
  332. } else {
  333. checker.set_dtype(1, dtype::Float32());
  334. }
  335. checker.execs({{2, 10, 1, 11, 4}, {2, 3, 3}, {2, 11, 1, 12, 4}});
  336. checker.execs({{22, 10, 1, 11, 4}, {22, 3, 3}, {22, 11, 1, 12, 4}});
  337. }
  338. }
  339. #if MEGDNN_TEST_ASAN
  340. //! asan detect nan will make test failed
  341. #else
  342. // nan case
  343. NanMatRNG rng_nan;
  344. UniformFloatRNG rng_zero(0, 0);
  345. //! NanMatRng not support float16, I have to reset dtype to Float32
  346. checker.set_dtype(0, dtype::Float32())
  347. .set_dtype(1, dtype::Float32())
  348. .set_dtype(2, dtype::Float32());
  349. for (auto rng : std::vector<RNG*>{&rng_nan, &rng_zero}) {
  350. param::WarpPerspective param;
  351. param.bmode = param::WarpPerspective::BorderMode::CONSTANT;
  352. param.imode = param::WarpPerspective::InterpolationMode::LINEAR;
  353. param.format = param::WarpPerspective::Format::NHWCD4;
  354. checker.set_rng(1, rng);
  355. param.border_val = 1.737;
  356. checker.set_param(param);
  357. checker.exec({{10, 10, 1, 11, 4}, {10, 3, 3}, {10, 12, 1, 13, 4}});
  358. }
  359. #endif
  360. }
  361. #if MEGDNN_WITH_BENCHMARK
  362. namespace {
  363. void benchmark_impl(const typename WarpPerspective::Param& param,
  364. std::vector<SmallVector<TensorShape>> shapes, size_t RUNS,
  365. TaskExecutorConfig&& multi_thread_config,
  366. TaskExecutorConfig&& single_thread_config) {
  367. std::vector<float> multi_thread_times, single_thread_times;
  368. {
  369. auto multi_thread_hanle =
  370. create_cpu_handle(0, true, &multi_thread_config);
  371. auto benchmarker =
  372. Benchmarker<WarpPerspective>(multi_thread_hanle.get());
  373. benchmarker.set_times(RUNS).set_display(false).set_param(param);
  374. for (auto shape : shapes) {
  375. multi_thread_times.push_back(benchmarker.exec(shape) / RUNS);
  376. }
  377. }
  378. {
  379. auto single_thread_handle =
  380. create_cpu_handle(0, true, &single_thread_config);
  381. auto benchmarker =
  382. Benchmarker<WarpPerspective>(single_thread_handle.get());
  383. benchmarker.set_times(RUNS).set_display(false).set_param(param);
  384. for (auto shape : shapes) {
  385. single_thread_times.push_back(benchmarker.exec(shape) / RUNS);
  386. }
  387. }
  388. printf("Benchmark : Multi threads %zu, ", multi_thread_config.nr_thread);
  389. printf("core_ids:");
  390. for (size_t i = 0; i < multi_thread_config.affinity_core_set.size(); i++) {
  391. printf("%zu ", multi_thread_config.affinity_core_set[i]);
  392. }
  393. printf(", Single thread core_id %zu\n",
  394. single_thread_config.affinity_core_set[0]);
  395. for (size_t i = 0; i < shapes.size(); i++) {
  396. auto shape = shapes[i];
  397. printf("Case: ");
  398. for (auto sh : shape)
  399. printf("%s ", sh.to_string().c_str());
  400. printf("%zu threads time: %f,\n single thread time: "
  401. "%f. spead up = %f, speedup/cores=%f\n",
  402. multi_thread_config.nr_thread, multi_thread_times[i],
  403. single_thread_times[i],
  404. single_thread_times[i] / multi_thread_times[i],
  405. single_thread_times[i] / multi_thread_times[i] /
  406. multi_thread_config.nr_thread);
  407. }
  408. }
  409. } // namespace
  410. TEST_F(NAIVE_BENCHMARK_MULTI_THREADS, BENCHMARK_WARP_PERSPECTIVE) {
  411. constexpr size_t RUNS = 50;
  412. using BMode = param::WarpPerspective::BorderMode;
  413. using IMode = param::WarpPerspective::InterpolationMode;
  414. WarpPerspective::Param param;
  415. param.border_val = 0.3f;
  416. param.format = param::WarpPerspective::Format::NCHW;
  417. param.imode = IMode::INTER_LINEAR;
  418. param.bmode = BMode::REPLICATE;
  419. std::vector<SmallVector<TensorShape>> shapes;
  420. auto bench_case = [&](size_t N, size_t H, size_t W, size_t C) {
  421. SmallVector<TensorShape> shape{
  422. {N, C, H, W}, {N, 3, 3}, {N, C, 224, 224}};
  423. shapes.push_back(shape);
  424. };
  425. bench_case(1, 700, 490, 10);
  426. bench_case(1, 700, 490, 20);
  427. bench_case(1, 700, 490, 30);
  428. bench_case(1, 500, 334, 10);
  429. bench_case(1, 500, 334, 20);
  430. bench_case(1, 500, 334, 30);
  431. bench_case(1, 140, 144, 10);
  432. bench_case(1, 140, 144, 20);
  433. bench_case(1, 140, 114, 30);
  434. printf("Benchmark warp perspective\n");
  435. benchmark_impl(param, shapes, RUNS, {4, {4, 5, 6, 7}}, {1, {4}});
  436. benchmark_impl(param, shapes, RUNS, {4, {4, 5, 6, 7}}, {1, {7}});
  437. benchmark_impl(param, shapes, RUNS, {2, {4, 5}}, {1, {4}});
  438. }
  439. #endif
  440. TEST_F(NAIVE, WARP_PERSPECTIVE_BFLOAT16) {
  441. Checker<WarpPerspective> checker(handle(), false);
  442. WarpPerspective::Param p;
  443. p.bmode = WarpPerspective::Param::BorderMode::BORDER_REFLECT;
  444. p.imode = WarpPerspective::Param::InterpolationMode::LINEAR;
  445. p.format = WarpPerspective::Param::Format::NCHW;
  446. auto extra_impl = extra_impl_helper<WarpPerspective>(handle(), p);
  447. checker.set_param(p)
  448. .set_epsilon(1e-1)
  449. .set_dtype(0, dtype::BFloat16())
  450. .set_dtype(1, dtype::Float32())
  451. .set_dtype(2, dtype::BFloat16())
  452. .set_extra_opr_impl(extra_impl)
  453. .execs({{1, 1, 3, 3}, {1, 3, 3}, {1, 1, 2, 2}})
  454. .execs({{1000, 2, 10, 11}, {1000, 3, 3}, {1000, 2, 12, 13}});
  455. }
  456. TEST_F(NAIVE, WARP_PERSPECTIVE_BACKWARD_DATA_BFLOAT16) {
  457. Checker<WarpPerspectiveBackwardData> checker(handle(), false);
  458. WarpPerspectiveBackwardData::Param p;
  459. p.bmode = WarpPerspectiveBackwardData::Param::BorderMode::BORDER_REFLECT;
  460. p.imode = WarpPerspectiveBackwardData::Param::InterpolationMode::LINEAR;
  461. p.format = WarpPerspectiveBackwardData::Param::Format::NCHW;
  462. auto extra_impl =
  463. extra_impl_helper<WarpPerspectiveBackwardData>(handle(), p);
  464. checker.set_param(p)
  465. .set_dtype(0, dtype::Float32())
  466. .set_dtype(1, dtype::BFloat16())
  467. .set_dtype(2, dtype::BFloat16())
  468. .set_extra_opr_impl(extra_impl)
  469. .set_epsilon(1e-1)
  470. .execs({{1, 3, 3}, {1, 1, 2, 2}, {1, 1, 3, 3}});
  471. }
  472. TEST_F(NAIVE, WARP_PERSPECTIVE_BACKWARD_MAT_BFLOAT16) {
  473. Checker<WarpPerspectiveBackwardMat> checker(handle(), false);
  474. WarpPerspectiveBackwardMat::Param p;
  475. p.bmode = WarpPerspectiveBackwardMat::Param::BorderMode::BORDER_REFLECT;
  476. p.imode = WarpPerspectiveBackwardMat::Param::InterpolationMode::LINEAR;
  477. p.format = WarpPerspectiveBackwardMat::Param::Format::NCHW;
  478. p.border_val = 0.3f;
  479. auto extra_impl =
  480. extra_impl_helper<WarpPerspectiveBackwardMat>(handle(), p);
  481. checker.set_param(p)
  482. .set_dtype(0, dtype::BFloat16())
  483. .set_dtype(1, dtype::Float32())
  484. .set_dtype(2, dtype::BFloat16())
  485. .set_dtype(3, dtype::Float32())
  486. .set_extra_opr_impl(extra_impl)
  487. .set_epsilon(1e-1)
  488. .execs({{1000, 3, 11, 12},
  489. {1000, 3, 3},
  490. {1000, 3, 10, 11},
  491. {1000, 3, 3}});
  492. }
  493. TEST_F(NAIVE, WARP_PERSPECTIVE_NCHW64) {
  494. using Param = WarpPerspective::Param;
  495. auto convert_true_format = [](const TensorLayout& layout) {
  496. if (layout.ndim == 4)
  497. return layout
  498. .reshape({layout[0], layout[1] / 64, layout[2], layout[3],
  499. 64})
  500. .dimshuffle({0, 1, 4, 2, 3});
  501. else
  502. return layout;
  503. };
  504. WarpPerspective::Param param;
  505. auto extra_impl = [&param, this,
  506. convert_true_format](const TensorNDArray& tensors) {
  507. auto warp_perspective = handle()->create_operator<WarpPerspective>();
  508. warp_perspective->param() = param;
  509. warp_perspective->param().format = Param::Format::NCHW;
  510. TensorNDArray nchw_tensors;
  511. for (size_t i = 0; i < tensors.size(); ++i) {
  512. auto layout = tensors[i].layout;
  513. if (layout.dtype.enumv() == DTypeEnum::QuantizedS4)
  514. layout.dtype = dtype::QuantizedS4();
  515. if (layout.ndim == 5) {
  516. layout = layout.reshape({layout[0], layout[1] * layout[4],
  517. layout[2], layout[3]});
  518. }
  519. nchw_tensors.emplace_back(malloc(layout.span().dist_byte()),
  520. layout);
  521. }
  522. TensorNDArray nchw64_tensors;
  523. for (size_t i = 0; i < tensors.size(); ++i) {
  524. auto layout = convert_true_format(nchw_tensors[i].layout);
  525. nchw64_tensors.emplace_back(tensors[i].raw_ptr, std::move(layout));
  526. }
  527. auto workspace_size = warp_perspective->get_workspace_in_bytes(
  528. tensors[0].layout, tensors[1].layout, tensors[2].layout);
  529. dt_byte* workspace_ptr = static_cast<dt_byte*>(malloc(workspace_size));
  530. Workspace workspace{workspace_ptr, workspace_size};
  531. auto relayout = handle()->create_operator<RelayoutForward>();
  532. relayout->exec(nchw64_tensors[0], nchw_tensors[0]);
  533. relayout->exec(nchw64_tensors[1], nchw_tensors[1]);
  534. warp_perspective->exec(nchw_tensors[0], nchw_tensors[1],
  535. nchw_tensors[2], workspace);
  536. relayout->exec(nchw_tensors[2], nchw64_tensors[2]);
  537. free(workspace_ptr);
  538. for (auto&& tensor : nchw_tensors) {
  539. free(tensor.raw_ptr);
  540. }
  541. };
  542. Checker<WarpPerspectiveForward> checker(handle());
  543. WarpPerspectiveMatRNG rng;
  544. checker.set_rng(1, &rng);
  545. checker.set_dtype(0, dtype::QuantizedS4(0.1f));
  546. checker.set_dtype(2, dtype::QuantizedS4(0.1f));
  547. checker.set_extra_opr_impl(extra_impl);
  548. for (auto bmode : {WarpPerspective::BorderMode::WRAP,
  549. WarpPerspective::BorderMode::REFLECT,
  550. WarpPerspective::BorderMode::REPLICATE,
  551. WarpPerspective::BorderMode::CONSTANT}) {
  552. param.border_val = 0.3f;
  553. param.bmode = bmode;
  554. param.imode = Param::InterpolationMode::LINEAR;
  555. param.format = Param::Format::NCHW64;
  556. checker.set_param(param);
  557. checker.execs({{2, 1, 10, 10, 64}, {2, 3, 3}, {2, 1, 10, 12, 64}});
  558. checker.execs(
  559. {{20, 30, 10, 12, 64}, {20, 3, 3}, {20, 30, 11, 12, 64}});
  560. checker.execs(
  561. {{220, 3, 10, 10, 64}, {220, 3, 3}, {220, 3, 10, 12, 64}});
  562. checker.execs({{1, 25, 25, 24, 64}, {1, 3, 3}, {1, 25, 25, 510, 64}});
  563. checker.execs({{1, 25, 25, 510, 64}, {1, 3, 3}, {1, 25, 25, 24, 64}});
  564. checker.execs({{1, 25, 25, 24, 64}, {1, 3, 3}, {1, 25, 51, 50, 64}});
  565. checker.execs({{1, 25, 51, 50, 64}, {1, 3, 3}, {1, 25, 25, 24, 64}});
  566. }
  567. }
  568. // vim: syntax=cpp.doxygen

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