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

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

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