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

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010
  1. #include "test/naive/fixture.h"
  2. #include "megdnn/oprs/cv.h"
  3. #include "megdnn/tensor_format.h"
  4. #include "test/common/benchmarker.h"
  5. #include "test/common/checker.h"
  6. #include "test/common/extra_impl_helper.h"
  7. #include "test/common/warp_perspective.h"
  8. using namespace megdnn;
  9. using namespace test;
  10. namespace {
  11. class NanMatRNG : public RNG {
  12. void gen(const TensorND& tensor_) override {
  13. auto& gen = RandomState::generator();
  14. std::uniform_real_distribution<dt_float32> pdist3(1.9f, 2.1f);
  15. std::uniform_real_distribution<dt_float32> pdist(0.9f, 1.1f);
  16. std::uniform_real_distribution<dt_float32> pdisth(0.4f, 0.6f);
  17. std::uniform_real_distribution<dt_float32> ndist(-1.1f, -0.9f);
  18. std::uniform_real_distribution<dt_float32> ndist3(-2.1f, -1.9f);
  19. std::uniform_real_distribution<dt_float32> ndisth(-0.6f, -0.4f);
  20. std::uniform_int_distribution<int> dice(0, 5);
  21. float* ptr = tensor_.ptr<dt_float32>();
  22. auto N = tensor_.layout.shape[0];
  23. for (size_t n = 0; n < N; ++n) {
  24. for (size_t i = 0; i < 9; ++i) {
  25. switch (dice(gen)) {
  26. case 0:
  27. ptr[i] = pdist3(gen);
  28. break;
  29. case 1:
  30. ptr[i] = pdist(gen);
  31. break;
  32. case 2:
  33. ptr[i] = pdisth(gen);
  34. break;
  35. case 3:
  36. ptr[i] = ndist(gen);
  37. break;
  38. case 4:
  39. ptr[i] = ndist3(gen);
  40. break;
  41. case 5:
  42. ptr[i] = ndisth(gen);
  43. break;
  44. }
  45. }
  46. ptr[6] = 1;
  47. ptr[7] = -1;
  48. ptr[8] = 5;
  49. ptr += 9;
  50. }
  51. }
  52. };
  53. } // namespace
  54. TEST_F(NAIVE, WARP_PERSPECTIVE_MULTI_SRC) {
  55. using Param = WarpPerspective::Param;
  56. WarpPerspective::Param param;
  57. auto extra_impl = [&param, this](const TensorNDArray& tensors) {
  58. //! split src
  59. TensorND src = tensors[0]; // n h w c
  60. size_t n = src.layout[0];
  61. TensorNDArray srcs; // n 个 1 h w c
  62. TensorLayoutArray srcs_layouts;
  63. for (size_t i = 0; i < n; i++) {
  64. TensorLayout ly;
  65. ly = TensorLayout{
  66. {1, src.layout[1], src.layout[2], src.layout[3]}, src.layout.dtype};
  67. srcs.emplace_back(malloc(ly.span().dist_byte()), ly);
  68. srcs_layouts.emplace_back(ly);
  69. }
  70. auto split = handle()->create_operator<SplitForward>();
  71. split->param().axis = 0;
  72. auto split_ws_size = split->get_workspace_in_bytes(src.layout, srcs_layouts);
  73. dt_byte* split_ws_ptr = static_cast<dt_byte*>(malloc(split_ws_size));
  74. Workspace split_ws{split_ws_ptr, split_ws_size};
  75. split->exec(src, srcs, split_ws);
  76. auto warp_perspective = handle()->create_operator<WarpPerspective>();
  77. warp_perspective->param() = param;
  78. auto warp_ws_size = warp_perspective->get_workspace_in_bytes(
  79. srcs_layouts, tensors[1].layout, tensors[2].layout);
  80. dt_byte* warp_ws_ptr = static_cast<dt_byte*>(malloc(warp_ws_size));
  81. Workspace warp_ws{warp_ws_ptr, warp_ws_size};
  82. warp_perspective->exec(srcs, tensors[1], tensors[2], warp_ws);
  83. free(split_ws_ptr);
  84. free(warp_ws_ptr);
  85. for (auto&& s : srcs) {
  86. free(s.raw_ptr());
  87. }
  88. };
  89. {
  90. // Float32
  91. Checker<WarpPerspectiveForward> checker(handle());
  92. WarpPerspectiveMatRNG rng;
  93. checker.set_rng(1, &rng);
  94. checker.set_extra_opr_impl(extra_impl);
  95. // NHWC
  96. for (auto bmode :
  97. {WarpPerspective::BorderMode::WRAP, WarpPerspective::BorderMode::REFLECT,
  98. WarpPerspective::BorderMode::REPLICATE,
  99. WarpPerspective::BorderMode::CONSTANT}) {
  100. param.border_val = 0.3f;
  101. param.bmode = bmode;
  102. param.imode = Param::InterpolationMode::LINEAR;
  103. param.format = Param::Format::NHWC;
  104. checker.set_param(param);
  105. checker.execs({{1, 2, 2, 4}, {1, 3, 3}, {1, 2, 2, 4}});
  106. checker.execs({{2, 10, 10, 4}, {2, 3, 3}, {2, 10, 12, 4}});
  107. checker.execs({{3, 25, 24, 8}, {3, 3, 3}, {3, 12, 10, 8}});
  108. checker.execs({{4, 33, 22, 16}, {4, 3, 3}, {4, 9, 12, 16}});
  109. }
  110. // NCHW
  111. for (auto bmode :
  112. {WarpPerspective::BorderMode::WRAP, WarpPerspective::BorderMode::REFLECT,
  113. WarpPerspective::BorderMode::REPLICATE,
  114. WarpPerspective::BorderMode::CONSTANT}) {
  115. param.border_val = 0.3f;
  116. param.bmode = bmode;
  117. param.imode = Param::InterpolationMode::LINEAR;
  118. param.format = Param::Format::NCHW;
  119. checker.set_param(param);
  120. checker.execs({{1, 4, 2, 2}, {1, 3, 3}, {1, 4, 2, 2}});
  121. checker.execs({{2, 4, 10, 10}, {2, 3, 3}, {2, 4, 10, 12}});
  122. checker.execs({{3, 8, 25, 24}, {3, 3, 3}, {3, 8, 12, 10}});
  123. checker.execs({{4, 16, 33, 22}, {4, 3, 3}, {4, 16, 9, 12}});
  124. }
  125. }
  126. {
  127. // Float16
  128. Checker<WarpPerspectiveForward> checker(handle());
  129. WarpPerspectiveMatRNG rng;
  130. checker.set_rng(1, &rng);
  131. checker.set_dtype(0, dtype::Float16());
  132. checker.set_dtype(2, dtype::Float16());
  133. checker.set_extra_opr_impl(extra_impl);
  134. // NHWC
  135. for (auto bmode :
  136. {WarpPerspective::BorderMode::WRAP, WarpPerspective::BorderMode::REFLECT,
  137. WarpPerspective::BorderMode::REPLICATE,
  138. WarpPerspective::BorderMode::CONSTANT}) {
  139. param.border_val = 0.3f;
  140. param.bmode = bmode;
  141. param.imode = Param::InterpolationMode::LINEAR;
  142. param.format = Param::Format::NHWC;
  143. checker.set_param(param);
  144. checker.execs({{1, 2, 2, 4}, {1, 3, 3}, {1, 2, 2, 4}});
  145. checker.execs({{2, 10, 10, 4}, {2, 3, 3}, {2, 10, 12, 4}});
  146. checker.execs({{3, 25, 24, 8}, {3, 3, 3}, {3, 12, 10, 8}});
  147. checker.execs({{4, 33, 22, 16}, {4, 3, 3}, {4, 9, 12, 16}});
  148. }
  149. // NCHW
  150. for (auto bmode :
  151. {WarpPerspective::BorderMode::WRAP, WarpPerspective::BorderMode::REFLECT,
  152. WarpPerspective::BorderMode::REPLICATE,
  153. WarpPerspective::BorderMode::CONSTANT}) {
  154. param.border_val = 0.3f;
  155. param.bmode = bmode;
  156. param.imode = Param::InterpolationMode::LINEAR;
  157. param.format = Param::Format::NCHW;
  158. checker.set_param(param);
  159. checker.execs({{1, 4, 2, 2}, {1, 3, 3}, {1, 4, 2, 2}});
  160. checker.execs({{2, 4, 10, 10}, {2, 3, 3}, {2, 4, 10, 12}});
  161. checker.execs({{3, 8, 25, 24}, {3, 3, 3}, {3, 8, 12, 10}});
  162. checker.execs({{4, 16, 33, 22}, {4, 3, 3}, {4, 16, 9, 12}});
  163. }
  164. }
  165. }
  166. TEST_F(NAIVE, WARP_PERSPECTIVE_MULTI_SRC_WITH_IDX) {
  167. using Param = WarpPerspective::Param;
  168. WarpPerspective::Param param;
  169. auto extra_impl = [&param, this](const TensorNDArray& tensors) {
  170. //! split src
  171. TensorND src = tensors[0]; // n h w c
  172. size_t n = src.layout[0];
  173. TensorNDArray srcs; // n 个 1 h w c
  174. TensorLayoutArray srcs_layouts;
  175. for (size_t i = 0; i < n; i++) {
  176. TensorLayout ly;
  177. ly = TensorLayout{
  178. {1, src.layout[1], src.layout[2], src.layout[3]}, src.layout.dtype};
  179. srcs.emplace_back(malloc(ly.span().dist_byte()), ly);
  180. srcs_layouts.emplace_back(ly);
  181. }
  182. auto split = handle()->create_operator<SplitForward>();
  183. split->param().axis = 0;
  184. auto split_ws_size = split->get_workspace_in_bytes(src.layout, srcs_layouts);
  185. dt_byte* split_ws_ptr = static_cast<dt_byte*>(malloc(split_ws_size));
  186. Workspace split_ws{split_ws_ptr, split_ws_size};
  187. split->exec(src, srcs, split_ws);
  188. auto warp_perspective = handle()->create_operator<WarpPerspective>();
  189. warp_perspective->param() = param;
  190. auto warp_ws_size = warp_perspective->get_workspace_in_bytes(
  191. srcs_layouts, tensors[1].layout, tensors[2].layout, tensors[3].layout);
  192. dt_byte* warp_ws_ptr = static_cast<dt_byte*>(malloc(warp_ws_size));
  193. Workspace warp_ws{warp_ws_ptr, warp_ws_size};
  194. warp_perspective->exec(srcs, tensors[1], tensors[2], tensors[3], warp_ws);
  195. free(split_ws_ptr);
  196. free(warp_ws_ptr);
  197. for (auto&& s : srcs) {
  198. free(s.raw_ptr());
  199. }
  200. };
  201. {
  202. // Float32
  203. Checker<WarpPerspectiveForward, WarpPerspectiveMatIdxProxy> checker(handle());
  204. WarpPerspectiveMatRNG rng;
  205. checker.set_rng(1, &rng);
  206. checker.set_dtype(0, dtype::Float32());
  207. checker.set_dtype(1, dtype::Float32());
  208. checker.set_dtype(2, dtype::Int32());
  209. checker.set_dtype(3, dtype::Float32());
  210. checker.set_extra_opr_impl(extra_impl);
  211. // NHWC
  212. for (auto bmode :
  213. {WarpPerspective::BorderMode::WRAP, WarpPerspective::BorderMode::REFLECT,
  214. WarpPerspective::BorderMode::REPLICATE,
  215. WarpPerspective::BorderMode::CONSTANT}) {
  216. param.border_val = 0.3f;
  217. param.bmode = bmode;
  218. param.imode = Param::InterpolationMode::LINEAR;
  219. param.format = Param::Format::NHWC;
  220. checker.set_param(param);
  221. UniformIntRNG idx_rng{0, 0};
  222. checker.set_rng(2, &idx_rng);
  223. checker.execs({{1, 2, 2, 4}, {1, 3, 3}, {1}, {1, 2, 2, 4}});
  224. idx_rng = UniformIntRNG{0, 1};
  225. checker.set_rng(2, &idx_rng);
  226. checker.execs({{2, 10, 10, 4}, {1, 3, 3}, {1}, {1, 10, 12, 4}});
  227. idx_rng = UniformIntRNG{0, 2};
  228. checker.set_rng(2, &idx_rng);
  229. checker.execs({{3, 25, 24, 8}, {2, 3, 3}, {2}, {2, 12, 10, 8}});
  230. checker.execs({{4, 33, 22, 16}, {2, 3, 3}, {2}, {2, 9, 12, 16}});
  231. }
  232. // NCHW
  233. for (auto bmode :
  234. {WarpPerspective::BorderMode::WRAP, WarpPerspective::BorderMode::REFLECT,
  235. WarpPerspective::BorderMode::REPLICATE,
  236. WarpPerspective::BorderMode::CONSTANT}) {
  237. param.border_val = 0.3f;
  238. param.bmode = bmode;
  239. param.imode = Param::InterpolationMode::LINEAR;
  240. param.format = Param::Format::NCHW;
  241. checker.set_param(param);
  242. UniformIntRNG idx_rng{0, 0};
  243. checker.set_rng(2, &idx_rng);
  244. checker.execs({{1, 4, 2, 2}, {1, 3, 3}, {1}, {1, 4, 2, 2}});
  245. idx_rng = UniformIntRNG{0, 1};
  246. checker.set_rng(2, &idx_rng);
  247. checker.execs({{2, 4, 10, 10}, {1, 3, 3}, {1}, {1, 4, 10, 12}});
  248. idx_rng = UniformIntRNG{0, 2};
  249. checker.set_rng(2, &idx_rng);
  250. checker.execs({{3, 8, 25, 24}, {2, 3, 3}, {2}, {2, 8, 12, 10}});
  251. checker.execs({{4, 16, 33, 22}, {2, 3, 3}, {2}, {2, 16, 9, 12}});
  252. }
  253. }
  254. {
  255. // Float16
  256. Checker<WarpPerspectiveForward, WarpPerspectiveMatIdxProxy> checker(handle());
  257. WarpPerspectiveMatRNG rng;
  258. checker.set_rng(1, &rng);
  259. checker.set_dtype(0, dtype::Float16());
  260. checker.set_dtype(1, dtype::Float32());
  261. checker.set_dtype(2, dtype::Int32());
  262. checker.set_dtype(3, dtype::Float16());
  263. checker.set_extra_opr_impl(extra_impl);
  264. // NHWC
  265. for (auto bmode :
  266. {WarpPerspective::BorderMode::WRAP, WarpPerspective::BorderMode::REFLECT,
  267. WarpPerspective::BorderMode::REPLICATE,
  268. WarpPerspective::BorderMode::CONSTANT}) {
  269. param.border_val = 0.3f;
  270. param.bmode = bmode;
  271. param.imode = Param::InterpolationMode::LINEAR;
  272. param.format = Param::Format::NHWC;
  273. checker.set_param(param);
  274. UniformIntRNG idx_rng{0, 0};
  275. checker.set_rng(2, &idx_rng);
  276. checker.execs({{1, 2, 2, 4}, {1, 3, 3}, {1}, {1, 2, 2, 4}});
  277. idx_rng = UniformIntRNG{0, 1};
  278. checker.set_rng(2, &idx_rng);
  279. checker.execs({{2, 10, 10, 4}, {1, 3, 3}, {1}, {1, 10, 12, 4}});
  280. idx_rng = UniformIntRNG{0, 2};
  281. checker.set_rng(2, &idx_rng);
  282. checker.execs({{3, 25, 24, 8}, {2, 3, 3}, {2}, {2, 12, 10, 8}});
  283. checker.execs({{4, 33, 22, 16}, {2, 3, 3}, {2}, {2, 9, 12, 16}});
  284. }
  285. // NCHW
  286. for (auto bmode :
  287. {WarpPerspective::BorderMode::WRAP, WarpPerspective::BorderMode::REFLECT,
  288. WarpPerspective::BorderMode::REPLICATE,
  289. WarpPerspective::BorderMode::CONSTANT}) {
  290. param.border_val = 0.3f;
  291. param.bmode = bmode;
  292. param.imode = Param::InterpolationMode::LINEAR;
  293. param.format = Param::Format::NCHW;
  294. checker.set_param(param);
  295. UniformIntRNG idx_rng{0, 0};
  296. checker.set_rng(2, &idx_rng);
  297. checker.execs({{1, 4, 2, 2}, {1, 3, 3}, {1}, {1, 4, 2, 2}});
  298. idx_rng = UniformIntRNG{0, 1};
  299. checker.set_rng(2, &idx_rng);
  300. checker.execs({{2, 4, 10, 10}, {1, 3, 3}, {1}, {1, 4, 10, 12}});
  301. idx_rng = UniformIntRNG{0, 2};
  302. checker.set_rng(2, &idx_rng);
  303. checker.execs({{3, 8, 25, 24}, {2, 3, 3}, {2}, {2, 8, 12, 10}});
  304. checker.execs({{4, 16, 33, 22}, {2, 3, 3}, {2}, {2, 16, 9, 12}});
  305. }
  306. }
  307. }
  308. TEST_F(NAIVE, WARP_PERSPECTIVE_NCHW4) {
  309. using Param = WarpPerspective::Param;
  310. auto convert_true_format = [](const TensorLayout& layout) {
  311. if (layout.ndim == 4)
  312. return layout.reshape({layout[0], layout[1] / 4, layout[2], layout[3], 4})
  313. .dimshuffle({0, 1, 4, 2, 3});
  314. else
  315. return layout;
  316. };
  317. WarpPerspective::Param param;
  318. auto extra_impl = [&param, this,
  319. convert_true_format](const TensorNDArray& tensors) {
  320. auto warp_perspective = handle()->create_operator<WarpPerspective>();
  321. warp_perspective->param() = param;
  322. warp_perspective->param().format = Param::Format::NCHW;
  323. TensorNDArray nchw_tensors;
  324. for (size_t i = 0; i < tensors.size(); ++i) {
  325. auto layout = tensors[i].layout;
  326. if (layout.dtype.enumv() == DTypeEnum::QuantizedS8)
  327. layout.dtype = dtype::Int8();
  328. if (layout.ndim == 5) {
  329. layout = layout.reshape(
  330. {layout[0], layout[1] * layout[4], layout[2], layout[3]});
  331. }
  332. nchw_tensors.emplace_back(malloc(layout.span().dist_byte()), layout);
  333. }
  334. TensorNDArray nchw4_tensors;
  335. for (size_t i = 0; i < tensors.size(); ++i) {
  336. auto layout = convert_true_format(nchw_tensors[i].layout);
  337. nchw4_tensors.emplace_back(tensors[i].raw_ptr(), std::move(layout));
  338. }
  339. auto workspace_size = warp_perspective->get_workspace_in_bytes(
  340. tensors[0].layout, tensors[1].layout, tensors[2].layout);
  341. dt_byte* workspace_ptr = static_cast<dt_byte*>(malloc(workspace_size));
  342. Workspace workspace{workspace_ptr, workspace_size};
  343. auto relayout = handle()->create_operator<RelayoutForward>();
  344. relayout->exec(nchw4_tensors[0], nchw_tensors[0]);
  345. relayout->exec(nchw4_tensors[1], nchw_tensors[1]);
  346. warp_perspective->exec(
  347. nchw_tensors[0], nchw_tensors[1], nchw_tensors[2], workspace);
  348. relayout->exec(nchw_tensors[2], nchw4_tensors[2]);
  349. free(workspace_ptr);
  350. for (auto&& tensor : nchw_tensors) {
  351. free(tensor.raw_ptr());
  352. }
  353. };
  354. Checker<WarpPerspectiveForward> checker(handle());
  355. WarpPerspectiveMatRNG rng;
  356. checker.set_rng(1, &rng);
  357. checker.set_dtype(0, dtype::QuantizedS8(0.1f));
  358. checker.set_dtype(2, dtype::QuantizedS8(0.1f));
  359. checker.set_extra_opr_impl(extra_impl);
  360. for (auto bmode :
  361. {WarpPerspective::BorderMode::WRAP, WarpPerspective::BorderMode::REFLECT,
  362. WarpPerspective::BorderMode::REPLICATE,
  363. WarpPerspective::BorderMode::CONSTANT}) {
  364. param.border_val = 0.3f;
  365. param.bmode = bmode;
  366. param.imode = Param::InterpolationMode::LINEAR;
  367. param.format = Param::Format::NCHW4;
  368. checker.set_param(param);
  369. checker.execs({{2, 1, 10, 11, 4}, {2, 3, 3}, {2, 1, 11, 12, 4}});
  370. checker.execs({{20, 300, 10, 11, 4}, {20, 3, 3}, {20, 300, 11, 12, 4}});
  371. checker.execs({{2200, 3, 10, 11, 4}, {2200, 3, 3}, {2200, 3, 11, 12, 4}});
  372. checker.execs({{1, 25, 25, 25, 4}, {1, 3, 3}, {1, 25, 25, 510, 4}});
  373. checker.execs({{1, 25, 25, 510, 4}, {1, 3, 3}, {1, 25, 25, 25, 4}});
  374. checker.execs({{1, 25, 25, 25, 4}, {1, 3, 3}, {1, 25, 51, 51, 4}});
  375. checker.execs({{1, 25, 51, 51, 4}, {1, 3, 3}, {1, 25, 25, 25, 4}});
  376. }
  377. }
  378. TEST_F(NAIVE, WARP_PERSPECTIVE) {
  379. Checker<WarpPerspective> checker(handle(), false);
  380. WarpPerspective::Param param;
  381. param.bmode = WarpPerspective::Param::BorderMode::BORDER_REFLECT;
  382. param.imode = WarpPerspective::Param::InterpolationMode::LINEAR;
  383. param.format = WarpPerspective::Param::Format::NCHW;
  384. checker.set_param(param).exect(
  385. Testcase{
  386. TensorValue(
  387. {1, 1, 3, 3}, dtype::Uint8{},
  388. {131, 255, 180, 245, 8, 0, 10, 3, 178}),
  389. TensorValue(
  390. {1, 3, 3}, dtype::Float32{},
  391. {1.2f, 1.2f, 0.6f, -1.05f, -2.0f, -0.7f, 1.3f, 1.5f, 3.0f}),
  392. {}},
  393. Testcase{
  394. {},
  395. {},
  396. TensorValue({1, 1, 2, 2}, dtype::Uint8{}, {156, 183, 181, 195})});
  397. checker.set_param(param).exect(
  398. Testcase{
  399. TensorValue(
  400. {1, 1, 3, 3},
  401. dtype::Quantized8Asymm{1.4f, static_cast<uint8_t>(127)},
  402. {131, 255, 180, 245, 8, 0, 10, 3, 178}),
  403. TensorValue(
  404. {1, 3, 3}, dtype::Float32{},
  405. {1.2f, 1.2f, 0.6f, -1.05f, -2.0f, -0.7f, 1.3f, 1.5f, 3.0f}),
  406. {}},
  407. Testcase{
  408. {},
  409. {},
  410. TensorValue(
  411. {1, 1, 2, 2},
  412. dtype::Quantized8Asymm{1.4f, static_cast<uint8_t>(127)},
  413. {156, 183, 181, 195})});
  414. }
  415. TEST_F(NAIVE, WARP_PERSPECTIVE_NCHW_QINT4) {
  416. Checker<WarpPerspective> checker(handle(), false);
  417. WarpPerspective::Param param;
  418. param.bmode = WarpPerspective::Param::BorderMode::BORDER_REFLECT;
  419. param.imode = WarpPerspective::Param::InterpolationMode::LINEAR;
  420. param.format = WarpPerspective::Param::Format::NCHW;
  421. std::vector<int> input_values = {-1, -3, -2, -2, 0, 0, 0, 0, -2},
  422. output_values = {-1, -2, -2, -2};
  423. checker.set_param(param).exect(
  424. Testcase{
  425. TensorValueLowbit4(
  426. {1, 1, 3, 3}, dtype::QuantizedS4(0.1), input_values),
  427. TensorValue(
  428. {1, 3, 3}, dtype::Float32{},
  429. {1.2f, 1.2f, 0.6f, -1.05f, -2.0f, -0.7f, 1.3f, 1.5f, 3.0f}),
  430. {}},
  431. Testcase{
  432. {},
  433. {},
  434. TensorValueLowbit4(
  435. {1, 1, 2, 2}, dtype::QuantizedS4(0.1), output_values)});
  436. }
  437. TEST_F(NAIVE, WARP_PERSPECTIVE_NCHW_QUINT4) {
  438. Checker<WarpPerspective> checker(handle(), false);
  439. WarpPerspective::Param param;
  440. param.bmode = WarpPerspective::Param::BorderMode::BORDER_REFLECT;
  441. param.imode = WarpPerspective::Param::InterpolationMode::LINEAR;
  442. param.format = WarpPerspective::Param::Format::NCHW;
  443. std::vector<int> input_values = {4, 13, 0, 0, 0, 0, 0, 0, 0},
  444. output_values = {6, 8, 8, 9};
  445. checker.set_param(param).exect(
  446. Testcase{
  447. TensorValueLowbit4(
  448. {1, 1, 3, 3}, dtype::Quantized4Asymm(0.1, 3), input_values),
  449. TensorValue(
  450. {1, 3, 3}, dtype::Float32{},
  451. {1.2f, 1.2f, 0.6f, -1.05f, -2.0f, -0.7f, 1.3f, 1.5f, 3.0f}),
  452. {}},
  453. Testcase{
  454. {},
  455. {},
  456. TensorValueLowbit4(
  457. {1, 1, 2, 2}, dtype::Quantized4Asymm(0.1, 3),
  458. output_values)});
  459. }
  460. TEST_F(NAIVE_MULTI_THREADS, WARP_PERSPECTIVE_NCHW4) {
  461. using Param = WarpPerspective::Param;
  462. auto convert_true_format = [](const TensorLayout& layout) {
  463. if (layout.ndim == 4)
  464. return layout.reshape({layout[0], layout[1] / 4, layout[2], layout[3], 4})
  465. .dimshuffle({0, 1, 4, 2, 3});
  466. else
  467. return layout;
  468. };
  469. WarpPerspective::Param param;
  470. auto extra_impl = [&param, this,
  471. convert_true_format](const TensorNDArray& tensors) {
  472. auto warp_perspective = handle()->create_operator<WarpPerspective>();
  473. warp_perspective->param() = param;
  474. warp_perspective->param().format = Param::Format::NCHW;
  475. TensorNDArray nchw_tensors;
  476. for (size_t i = 0; i < tensors.size(); ++i) {
  477. auto layout = tensors[i].layout;
  478. if (layout.dtype.enumv() == DTypeEnum::QuantizedS8)
  479. layout.dtype = dtype::Int8();
  480. if (layout.ndim == 5) {
  481. layout = layout.reshape(
  482. {layout[0], layout[1] * layout[4], layout[2], layout[3]});
  483. }
  484. nchw_tensors.emplace_back(malloc(layout.span().dist_byte()), layout);
  485. }
  486. TensorNDArray nchw4_tensors;
  487. for (size_t i = 0; i < tensors.size(); ++i) {
  488. auto layout = convert_true_format(nchw_tensors[i].layout);
  489. nchw4_tensors.emplace_back(tensors[i].raw_ptr(), std::move(layout));
  490. }
  491. auto workspace_size = warp_perspective->get_workspace_in_bytes(
  492. tensors[0].layout, tensors[1].layout, tensors[2].layout);
  493. dt_byte* workspace_ptr = static_cast<dt_byte*>(malloc(workspace_size));
  494. Workspace workspace{workspace_ptr, workspace_size};
  495. auto relayout = handle()->create_operator<RelayoutForward>();
  496. relayout->exec(nchw4_tensors[0], nchw_tensors[0]);
  497. relayout->exec(nchw4_tensors[1], nchw_tensors[1]);
  498. warp_perspective->exec(
  499. nchw_tensors[0], nchw_tensors[1], nchw_tensors[2], workspace);
  500. relayout->exec(nchw_tensors[2], nchw4_tensors[2]);
  501. free(workspace_ptr);
  502. for (auto&& tensor : nchw_tensors) {
  503. free(tensor.raw_ptr());
  504. }
  505. };
  506. Checker<WarpPerspectiveForward> checker(handle());
  507. WarpPerspectiveMatRNG rng;
  508. checker.set_rng(1, &rng);
  509. checker.set_dtype(0, dtype::QuantizedS8(0.1f));
  510. checker.set_dtype(2, dtype::QuantizedS8(0.1f));
  511. checker.set_extra_opr_impl(extra_impl);
  512. for (auto bmode :
  513. {WarpPerspective::BorderMode::WRAP, WarpPerspective::BorderMode::REFLECT,
  514. WarpPerspective::BorderMode::REPLICATE,
  515. WarpPerspective::BorderMode::CONSTANT}) {
  516. param.border_val = 0.3f;
  517. param.bmode = bmode;
  518. param.imode = Param::InterpolationMode::LINEAR;
  519. param.format = Param::Format::NCHW4;
  520. checker.set_param(param);
  521. checker.execs({{2, 1, 10, 11, 4}, {2, 3, 3}, {2, 1, 11, 12, 4}});
  522. checker.execs({{20, 300, 10, 11, 4}, {20, 3, 3}, {20, 300, 11, 12, 4}});
  523. checker.execs({{2200, 3, 10, 11, 4}, {2200, 3, 3}, {2200, 3, 11, 12, 4}});
  524. checker.execs({{1, 25, 25, 25, 4}, {1, 3, 3}, {1, 25, 25, 510, 4}});
  525. checker.execs({{1, 25, 25, 510, 4}, {1, 3, 3}, {1, 25, 25, 25, 4}});
  526. checker.execs({{1, 25, 25, 25, 4}, {1, 3, 3}, {1, 25, 51, 51, 4}});
  527. checker.execs({{1, 25, 51, 51, 4}, {1, 3, 3}, {1, 25, 25, 25, 4}});
  528. }
  529. }
  530. TEST_F(NAIVE_MULTI_THREADS, WARP_PERSPECTIVE) {
  531. Checker<WarpPerspective> checker(handle(), false);
  532. WarpPerspective::Param param;
  533. param.bmode = WarpPerspective::Param::BorderMode::BORDER_REFLECT;
  534. param.imode = WarpPerspective::Param::InterpolationMode::LINEAR;
  535. param.format = WarpPerspective::Param::Format::NCHW;
  536. checker.set_param(param).exect(
  537. Testcase{
  538. TensorValue(
  539. {1, 1, 3, 3}, dtype::Uint8{},
  540. {131, 255, 180, 245, 8, 0, 10, 3, 178}),
  541. TensorValue(
  542. {1, 3, 3}, dtype::Float32{},
  543. {1.2f, 1.2f, 0.6f, -1.05f, -2.0f, -0.7f, 1.3f, 1.5f, 3.0f}),
  544. {}},
  545. Testcase{
  546. {},
  547. {},
  548. TensorValue({1, 1, 2, 2}, dtype::Uint8{}, {156, 183, 181, 195})});
  549. checker.set_param(param).exect(
  550. Testcase{
  551. TensorValue(
  552. {1, 1, 3, 3},
  553. dtype::Quantized8Asymm{1.4f, static_cast<uint8_t>(127)},
  554. {131, 255, 180, 245, 8, 0, 10, 3, 178}),
  555. TensorValue(
  556. {1, 3, 3}, dtype::Float32{},
  557. {1.2f, 1.2f, 0.6f, -1.05f, -2.0f, -0.7f, 1.3f, 1.5f, 3.0f}),
  558. {}},
  559. Testcase{
  560. {},
  561. {},
  562. TensorValue(
  563. {1, 1, 2, 2},
  564. dtype::Quantized8Asymm{1.4f, static_cast<uint8_t>(127)},
  565. {156, 183, 181, 195})});
  566. }
  567. TEST_F(NAIVE_MULTI_THREADS, WARP_PERSPECTIVE_FORWARD_HWCD4) {
  568. auto handle_multi_thread = handle();
  569. Checker<WarpPerspective> checker(handle(), false);
  570. TensorFormat img_fmt = Image2DPack4TensorFormat::make(2, handle_multi_thread);
  571. checker.set_fmt(0, img_fmt).set_fmt(2, img_fmt);
  572. for (auto dtype : std::vector<DType>{
  573. dtype::Float32(), dtype::Float16(), dtype::QuantizedS8(4.3f),
  574. dtype::Quantized8Asymm(2.4f, static_cast<uint8_t>(10))}) {
  575. for (auto bmode :
  576. {WarpPerspective::BorderMode::WRAP, WarpPerspective::BorderMode::REFLECT,
  577. WarpPerspective::BorderMode::CONSTANT,
  578. WarpPerspective::BorderMode::REPLICATE,
  579. WarpPerspective::BorderMode::CONSTANT}) {
  580. WarpPerspectiveMatRNG rng;
  581. checker.set_rng(1, &rng);
  582. WarpPerspective::Param param;
  583. param.border_val = 0.3f;
  584. param.bmode = bmode;
  585. param.imode = param::WarpPerspective::InterpolationMode::LINEAR;
  586. param.format = param::WarpPerspective::Format::NHWCD4;
  587. if (dtype == dtype::Float16()) {
  588. //! if exists error, the value of a result pixel maybe another
  589. //! pixel in the origin image, so we just consider the avg error
  590. checker.set_epsilon(2e-1);
  591. checker.set_max_avg_error(1e-2);
  592. }
  593. checker.set_param(param);
  594. checker.set_dtype(0, dtype);
  595. checker.set_dtype(2, dtype);
  596. if (dtype.category() == DTypeCategory::FLOAT) {
  597. checker.set_dtype(1, dtype);
  598. } else {
  599. checker.set_dtype(1, dtype::Float32());
  600. }
  601. checker.execs({{2, 10, 1, 11, 4}, {2, 3, 3}, {2, 11, 1, 12, 4}});
  602. checker.execs({{22, 10, 1, 11, 4}, {22, 3, 3}, {22, 11, 1, 12, 4}});
  603. }
  604. }
  605. #if MEGDNN_TEST_ASAN
  606. //! asan detect nan will make test failed
  607. #else
  608. // nan case
  609. NanMatRNG rng_nan;
  610. UniformFloatRNG rng_zero(0, 0);
  611. //! NanMatRng not support float16, I have to reset dtype to Float32
  612. checker.set_dtype(0, dtype::Float32())
  613. .set_dtype(1, dtype::Float32())
  614. .set_dtype(2, dtype::Float32());
  615. for (auto rng : std::vector<RNG*>{&rng_nan, &rng_zero}) {
  616. param::WarpPerspective param;
  617. param.bmode = param::WarpPerspective::BorderMode::CONSTANT;
  618. param.imode = param::WarpPerspective::InterpolationMode::LINEAR;
  619. param.format = param::WarpPerspective::Format::NHWCD4;
  620. checker.set_rng(1, rng);
  621. param.border_val = 1.737;
  622. checker.set_param(param);
  623. checker.exec({{10, 10, 1, 11, 4}, {10, 3, 3}, {10, 12, 1, 13, 4}});
  624. }
  625. #endif
  626. }
  627. #if MEGDNN_WITH_BENCHMARK
  628. namespace {
  629. void benchmark_impl(
  630. const typename WarpPerspective::Param& param,
  631. std::vector<SmallVector<TensorShape>> shapes, size_t RUNS,
  632. TaskExecutorConfig&& multi_thread_config,
  633. TaskExecutorConfig&& single_thread_config) {
  634. std::vector<float> multi_thread_times, single_thread_times;
  635. {
  636. auto multi_thread_hanle = create_cpu_handle(0, true, &multi_thread_config);
  637. auto benchmarker = Benchmarker<WarpPerspective>(multi_thread_hanle.get());
  638. benchmarker.set_times(RUNS).set_display(false).set_param(param);
  639. for (auto shape : shapes) {
  640. multi_thread_times.push_back(benchmarker.exec(shape) / RUNS);
  641. }
  642. }
  643. {
  644. auto single_thread_handle = create_cpu_handle(0, true, &single_thread_config);
  645. auto benchmarker = Benchmarker<WarpPerspective>(single_thread_handle.get());
  646. benchmarker.set_times(RUNS).set_display(false).set_param(param);
  647. for (auto shape : shapes) {
  648. single_thread_times.push_back(benchmarker.exec(shape) / RUNS);
  649. }
  650. }
  651. printf("Benchmark : Multi threads %zu, ", multi_thread_config.nr_thread);
  652. printf("core_ids:");
  653. for (size_t i = 0; i < multi_thread_config.affinity_core_set.size(); i++) {
  654. printf("%zu ", multi_thread_config.affinity_core_set[i]);
  655. }
  656. printf(", Single thread core_id %zu\n", single_thread_config.affinity_core_set[0]);
  657. for (size_t i = 0; i < shapes.size(); i++) {
  658. auto shape = shapes[i];
  659. printf("Case: ");
  660. for (auto sh : shape)
  661. printf("%s ", sh.to_string().c_str());
  662. printf("%zu threads time: %f,\n single thread time: "
  663. "%f. spead up = %f, speedup/cores=%f\n",
  664. multi_thread_config.nr_thread, multi_thread_times[i],
  665. single_thread_times[i], single_thread_times[i] / multi_thread_times[i],
  666. single_thread_times[i] / multi_thread_times[i] /
  667. multi_thread_config.nr_thread);
  668. }
  669. }
  670. } // namespace
  671. TEST_F(NAIVE_BENCHMARK_MULTI_THREADS, BENCHMARK_WARP_PERSPECTIVE) {
  672. constexpr size_t RUNS = 50;
  673. using BMode = param::WarpPerspective::BorderMode;
  674. using IMode = param::WarpPerspective::InterpolationMode;
  675. WarpPerspective::Param param;
  676. param.border_val = 0.3f;
  677. param.format = param::WarpPerspective::Format::NCHW;
  678. param.imode = IMode::INTER_LINEAR;
  679. param.bmode = BMode::REPLICATE;
  680. std::vector<SmallVector<TensorShape>> shapes;
  681. auto bench_case = [&](size_t N, size_t H, size_t W, size_t C) {
  682. SmallVector<TensorShape> shape{{N, C, H, W}, {N, 3, 3}, {N, C, 224, 224}};
  683. shapes.push_back(shape);
  684. };
  685. bench_case(1, 700, 490, 10);
  686. bench_case(1, 700, 490, 20);
  687. bench_case(1, 700, 490, 30);
  688. bench_case(1, 500, 334, 10);
  689. bench_case(1, 500, 334, 20);
  690. bench_case(1, 500, 334, 30);
  691. bench_case(1, 140, 144, 10);
  692. bench_case(1, 140, 144, 20);
  693. bench_case(1, 140, 114, 30);
  694. printf("Benchmark warp perspective\n");
  695. benchmark_impl(param, shapes, RUNS, {4, {4, 5, 6, 7}}, {1, {4}});
  696. benchmark_impl(param, shapes, RUNS, {4, {4, 5, 6, 7}}, {1, {7}});
  697. benchmark_impl(param, shapes, RUNS, {2, {4, 5}}, {1, {4}});
  698. }
  699. #endif
  700. TEST_F(NAIVE, WARP_PERSPECTIVE_BFLOAT16) {
  701. Checker<WarpPerspective> checker(handle(), false);
  702. WarpPerspective::Param p;
  703. p.bmode = WarpPerspective::Param::BorderMode::BORDER_REFLECT;
  704. p.imode = WarpPerspective::Param::InterpolationMode::LINEAR;
  705. p.format = WarpPerspective::Param::Format::NCHW;
  706. auto extra_impl = extra_impl_helper<WarpPerspective>(handle(), p);
  707. checker.set_param(p)
  708. .set_epsilon(1e-1)
  709. .set_dtype(0, dtype::BFloat16())
  710. .set_dtype(1, dtype::Float32())
  711. .set_dtype(2, dtype::BFloat16())
  712. .set_extra_opr_impl(extra_impl)
  713. .execs({{1, 1, 3, 3}, {1, 3, 3}, {1, 1, 2, 2}})
  714. .execs({{1000, 2, 10, 11}, {1000, 3, 3}, {1000, 2, 12, 13}});
  715. }
  716. TEST_F(NAIVE, WARP_PERSPECTIVE_BACKWARD_DATA_BFLOAT16) {
  717. Checker<WarpPerspectiveBackwardData> checker(handle(), false);
  718. WarpPerspectiveBackwardData::Param p;
  719. p.bmode = WarpPerspectiveBackwardData::Param::BorderMode::BORDER_REFLECT;
  720. p.imode = WarpPerspectiveBackwardData::Param::InterpolationMode::LINEAR;
  721. p.format = WarpPerspectiveBackwardData::Param::Format::NCHW;
  722. auto extra_impl = extra_impl_helper<WarpPerspectiveBackwardData>(handle(), p);
  723. checker.set_param(p)
  724. .set_dtype(0, dtype::Float32())
  725. .set_dtype(1, dtype::BFloat16())
  726. .set_dtype(2, dtype::BFloat16())
  727. .set_extra_opr_impl(extra_impl)
  728. .set_epsilon(1e-1)
  729. .execs({{1, 3, 3}, {1, 1, 2, 2}, {1, 1, 3, 3}});
  730. }
  731. TEST_F(NAIVE, WARP_PERSPECTIVE_BACKWARD_MAT_BFLOAT16) {
  732. Checker<WarpPerspectiveBackwardMat> checker(handle(), false);
  733. WarpPerspectiveBackwardMat::Param p;
  734. p.bmode = WarpPerspectiveBackwardMat::Param::BorderMode::BORDER_REFLECT;
  735. p.imode = WarpPerspectiveBackwardMat::Param::InterpolationMode::LINEAR;
  736. p.format = WarpPerspectiveBackwardMat::Param::Format::NCHW;
  737. p.border_val = 0.3f;
  738. auto extra_impl = extra_impl_helper<WarpPerspectiveBackwardMat>(handle(), p);
  739. checker.set_param(p)
  740. .set_dtype(0, dtype::BFloat16())
  741. .set_dtype(1, dtype::Float32())
  742. .set_dtype(2, dtype::BFloat16())
  743. .set_dtype(3, dtype::Float32())
  744. .set_extra_opr_impl(extra_impl)
  745. .set_epsilon(1e-1)
  746. .execs({{1000, 3, 11, 12}, {1000, 3, 3}, {1000, 3, 10, 11}, {1000, 3, 3}});
  747. }
  748. TEST_F(NAIVE, WARP_PERSPECTIVE_NCHW64) {
  749. using Param = WarpPerspective::Param;
  750. auto convert_true_format = [](const TensorLayout& layout) {
  751. if (layout.ndim == 4) {
  752. TensorLayout ret{
  753. {layout[0], layout[1] / 64, layout[2], layout[3], 64},
  754. layout.dtype};
  755. return ret.dimshuffle({0, 1, 4, 2, 3});
  756. } else
  757. return layout;
  758. };
  759. WarpPerspective::Param param;
  760. auto extra_impl = [&param, this,
  761. convert_true_format](const TensorNDArray& tensors) {
  762. auto warp_perspective = handle()->create_operator<WarpPerspective>();
  763. warp_perspective->param() = param;
  764. warp_perspective->param().format = Param::Format::NCHW;
  765. TensorNDArray nchw_tensors;
  766. for (size_t i = 0; i < tensors.size(); ++i) {
  767. TensorLayout ly;
  768. auto layout = tensors[i].layout;
  769. if (tensors[i].layout.ndim == 5) {
  770. ly = TensorLayout{
  771. {layout[0], layout[1] * layout[4], layout[2], layout[3]},
  772. layout.dtype};
  773. } else {
  774. ly = layout;
  775. }
  776. nchw_tensors.emplace_back(malloc(ly.span().dist_byte()), ly);
  777. }
  778. TensorNDArray nchw64_tensors;
  779. for (size_t i = 0; i < tensors.size(); ++i) {
  780. auto layout = convert_true_format(nchw_tensors[i].layout);
  781. nchw64_tensors.emplace_back(tensors[i].raw_ptr(), std::move(layout));
  782. }
  783. auto workspace_size = warp_perspective->get_workspace_in_bytes(
  784. tensors[0].layout, tensors[1].layout, tensors[2].layout);
  785. dt_byte* workspace_ptr = static_cast<dt_byte*>(malloc(workspace_size));
  786. Workspace workspace{workspace_ptr, workspace_size};
  787. auto relayout = handle()->create_operator<RelayoutForward>();
  788. relayout->exec(nchw64_tensors[0], nchw_tensors[0]);
  789. relayout->exec(nchw64_tensors[1], nchw_tensors[1]);
  790. warp_perspective->exec(
  791. nchw_tensors[0], nchw_tensors[1], nchw_tensors[2], workspace);
  792. relayout->exec(nchw_tensors[2], nchw64_tensors[2]);
  793. free(workspace_ptr);
  794. for (auto&& tensor : nchw_tensors) {
  795. free(tensor.raw_ptr());
  796. }
  797. };
  798. Checker<WarpPerspectiveForward> checker(handle());
  799. WarpPerspectiveMatRNG rng;
  800. checker.set_rng(1, &rng);
  801. checker.set_dtype(0, dtype::QuantizedS4(0.1f));
  802. checker.set_dtype(2, dtype::QuantizedS4(0.1f));
  803. checker.set_extra_opr_impl(extra_impl);
  804. for (auto bmode :
  805. {WarpPerspective::BorderMode::WRAP, WarpPerspective::BorderMode::REFLECT,
  806. WarpPerspective::BorderMode::REPLICATE,
  807. WarpPerspective::BorderMode::CONSTANT}) {
  808. param.border_val = 0.3f;
  809. param.bmode = bmode;
  810. param.imode = Param::InterpolationMode::LINEAR;
  811. param.format = Param::Format::NCHW64;
  812. checker.set_param(param);
  813. checker.execs({{2, 1, 10, 10, 64}, {2, 3, 3}, {2, 1, 10, 12, 64}});
  814. checker.execs({{20, 3, 10, 12, 64}, {20, 3, 3}, {20, 3, 11, 12, 64}});
  815. checker.execs({{1, 3, 25, 24, 64}, {1, 3, 3}, {1, 3, 25, 51, 64}});
  816. checker.execs({{1, 3, 25, 51, 64}, {1, 3, 3}, {1, 3, 25, 24, 64}});
  817. checker.execs({{1, 3, 25, 24, 64}, {1, 3, 3}, {1, 3, 51, 50, 64}});
  818. checker.execs({{1, 3, 51, 50, 64}, {1, 3, 3}, {1, 3, 25, 24, 64}});
  819. }
  820. }
  821. TEST_F(NAIVE, WARP_PERSPECTIVE_NHWC) {
  822. using Param = WarpPerspective::Param;
  823. auto convert_true_format = [](const TensorLayout& layout) {
  824. if (layout.ndim == 4) {
  825. TensorLayout ret{
  826. {layout[0], layout[2], layout[3], layout[1]}, layout.dtype};
  827. return ret.dimshuffle({0, 3, 1, 2});
  828. } else
  829. return layout;
  830. };
  831. WarpPerspective::Param param;
  832. auto extra_impl = [&param, this,
  833. convert_true_format](const TensorNDArray& tensors) {
  834. auto warp_perspective = handle()->create_operator<WarpPerspective>();
  835. warp_perspective->param() = param;
  836. warp_perspective->param().format = Param::Format::NCHW;
  837. TensorNDArray nchw_tensors;
  838. for (size_t i = 0; i < tensors.size(); ++i) {
  839. TensorLayout ly;
  840. auto layout = tensors[i].layout;
  841. if (layout.ndim == 4) {
  842. ly = TensorLayout{
  843. {layout[0], layout[3], layout[1], layout[2]}, layout.dtype};
  844. } else {
  845. ly = layout;
  846. }
  847. nchw_tensors.emplace_back(malloc(ly.span().dist_byte()), ly);
  848. }
  849. TensorNDArray nhwc_tensors;
  850. for (size_t i = 0; i < tensors.size(); ++i) {
  851. auto layout = convert_true_format(nchw_tensors[i].layout);
  852. nhwc_tensors.emplace_back(tensors[i].raw_ptr(), std::move(layout));
  853. }
  854. auto workspace_size = warp_perspective->get_workspace_in_bytes(
  855. tensors[0].layout, tensors[1].layout, tensors[2].layout);
  856. dt_byte* workspace_ptr = static_cast<dt_byte*>(malloc(workspace_size));
  857. Workspace workspace{workspace_ptr, workspace_size};
  858. auto relayout = handle()->create_operator<RelayoutForward>();
  859. relayout->exec(nhwc_tensors[0], nchw_tensors[0]);
  860. relayout->exec(nhwc_tensors[1], nchw_tensors[1]);
  861. warp_perspective->exec(
  862. nchw_tensors[0], nchw_tensors[1], nchw_tensors[2], workspace);
  863. relayout->exec(nchw_tensors[2], nhwc_tensors[2]);
  864. free(workspace_ptr);
  865. for (auto&& tensor : nchw_tensors) {
  866. free(tensor.raw_ptr());
  867. }
  868. };
  869. {
  870. Checker<WarpPerspectiveForward> checker(handle());
  871. WarpPerspectiveMatRNG rng;
  872. checker.set_rng(1, &rng);
  873. checker.set_dtype(0, dtype::QuantizedS4(0.1f));
  874. checker.set_dtype(2, dtype::QuantizedS4(0.1f));
  875. checker.set_extra_opr_impl(extra_impl);
  876. for (auto bmode :
  877. {WarpPerspective::BorderMode::WRAP, WarpPerspective::BorderMode::REFLECT,
  878. WarpPerspective::BorderMode::REPLICATE,
  879. WarpPerspective::BorderMode::CONSTANT}) {
  880. param.border_val = 0.3f;
  881. param.bmode = bmode;
  882. param.imode = Param::InterpolationMode::LINEAR;
  883. param.format = Param::Format::NHWC;
  884. checker.set_param(param);
  885. checker.execs({{1, 2, 2, 4}, {1, 3, 3}, {1, 2, 2, 4}});
  886. checker.execs({{2, 10, 10, 4}, {2, 3, 3}, {2, 10, 12, 4}});
  887. checker.execs({{3, 25, 24, 8}, {3, 3, 3}, {3, 12, 10, 8}});
  888. checker.execs({{4, 33, 22, 16}, {4, 3, 3}, {4, 9, 12, 16}});
  889. }
  890. }
  891. {
  892. Checker<WarpPerspectiveForward> checker(handle());
  893. WarpPerspectiveMatRNG rng;
  894. checker.set_rng(1, &rng);
  895. checker.set_dtype(0, dtype::Quantized4Asymm(0.1f, 3));
  896. checker.set_dtype(2, dtype::Quantized4Asymm(0.1f, 3));
  897. checker.set_extra_opr_impl(extra_impl);
  898. for (auto bmode :
  899. {WarpPerspective::BorderMode::WRAP, WarpPerspective::BorderMode::REFLECT,
  900. WarpPerspective::BorderMode::REPLICATE,
  901. WarpPerspective::BorderMode::CONSTANT}) {
  902. param.border_val = 0.3f;
  903. param.bmode = bmode;
  904. param.imode = Param::InterpolationMode::LINEAR;
  905. param.format = Param::Format::NHWC;
  906. checker.set_param(param);
  907. checker.execs({{1, 2, 2, 4}, {1, 3, 3}, {1, 2, 2, 4}});
  908. checker.execs({{2, 10, 10, 4}, {2, 3, 3}, {2, 10, 12, 4}});
  909. checker.execs({{3, 25, 24, 8}, {3, 3, 3}, {3, 12, 10, 8}});
  910. checker.execs({{4, 33, 22, 16}, {4, 3, 3}, {4, 9, 12, 16}});
  911. }
  912. }
  913. }
  914. // vim: syntax=cpp.doxygen