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.

testutil.cpp 48 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814
  1. // Tencent is pleased to support the open source community by making ncnn available.
  2. //
  3. // Copyright (C) 2019 THL A29 Limited, a Tencent company. All rights reserved.
  4. //
  5. // Licensed under the BSD 3-Clause License (the "License"); you may not use this file except
  6. // in compliance with the License. You may obtain a copy of the License at
  7. //
  8. // https://opensource.org/licenses/BSD-3-Clause
  9. //
  10. // Unless required by applicable law or agreed to in writing, software distributed
  11. // under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR
  12. // CONDITIONS OF ANY KIND, either express or implied. See the License for the
  13. // specific language governing permissions and limitations under the License.
  14. #include "testutil.h"
  15. #include "cpu.h"
  16. #include "layer.h"
  17. #include "mat.h"
  18. #include "prng.h"
  19. #include <limits.h>
  20. #include <stdio.h>
  21. #include <stdlib.h>
  22. #if NCNN_VULKAN
  23. #include "command.h"
  24. #include "gpu.h"
  25. #endif // NCNN_VULKAN
  26. static struct prng_rand_t g_prng_rand_state;
  27. void SRAND(int seed)
  28. {
  29. prng_srand(seed, &g_prng_rand_state);
  30. }
  31. uint64_t RAND()
  32. {
  33. return prng_rand(&g_prng_rand_state);
  34. }
  35. float RandomFloat(float a, float b)
  36. {
  37. float random = ((float)RAND()) / (float)uint64_t(-1); //RAND_MAX;
  38. float diff = b - a;
  39. float r = random * diff;
  40. float v = a + r;
  41. // generate denormal as zero
  42. if (v < 0.0001 && v > -0.0001)
  43. v = 0.f;
  44. return v;
  45. }
  46. int RandomInt(int a, int b)
  47. {
  48. float random = ((float)RAND()) / (float)uint64_t(-1); //RAND_MAX;
  49. int diff = b - a;
  50. float r = random * diff;
  51. return a + (int)r;
  52. }
  53. signed char RandomS8()
  54. {
  55. return (signed char)RandomInt(-127, 127);
  56. }
  57. void Randomize(ncnn::Mat& m, float a, float b)
  58. {
  59. for (size_t i = 0; i < m.total(); i++)
  60. {
  61. m[i] = RandomFloat(a, b);
  62. }
  63. }
  64. void RandomizeInt(ncnn::Mat& m, int a, int b)
  65. {
  66. for (size_t i = 0; i < m.total(); i++)
  67. {
  68. ((int*)m)[i] = RandomInt(a, b);
  69. }
  70. }
  71. void RandomizeS8(ncnn::Mat& m)
  72. {
  73. for (size_t i = 0; i < m.total(); i++)
  74. {
  75. ((signed char*)m)[i] = RandomS8();
  76. }
  77. }
  78. ncnn::Mat RandomMat(int w, float a, float b)
  79. {
  80. ncnn::Mat m(w);
  81. Randomize(m, a, b);
  82. return m;
  83. }
  84. ncnn::Mat RandomMat(int w, int h, float a, float b)
  85. {
  86. ncnn::Mat m(w, h);
  87. Randomize(m, a, b);
  88. return m;
  89. }
  90. ncnn::Mat RandomMat(int w, int h, int c, float a, float b)
  91. {
  92. ncnn::Mat m(w, h, c);
  93. Randomize(m, a, b);
  94. return m;
  95. }
  96. ncnn::Mat RandomMat(int w, int h, int d, int c, float a, float b)
  97. {
  98. ncnn::Mat m(w, h, d, c);
  99. Randomize(m, a, b);
  100. return m;
  101. }
  102. ncnn::Mat RandomIntMat(int w)
  103. {
  104. ncnn::Mat m(w);
  105. RandomizeInt(m);
  106. return m;
  107. }
  108. ncnn::Mat RandomIntMat(int w, int h)
  109. {
  110. ncnn::Mat m(w, h);
  111. RandomizeInt(m);
  112. return m;
  113. }
  114. ncnn::Mat RandomIntMat(int w, int h, int c)
  115. {
  116. ncnn::Mat m(w, h, c);
  117. RandomizeInt(m);
  118. return m;
  119. }
  120. ncnn::Mat RandomIntMat(int w, int h, int d, int c)
  121. {
  122. ncnn::Mat m(w, h, d, c);
  123. RandomizeInt(m);
  124. return m;
  125. }
  126. ncnn::Mat RandomS8Mat(int w)
  127. {
  128. ncnn::Mat m(w, (size_t)1u);
  129. RandomizeS8(m);
  130. return m;
  131. }
  132. ncnn::Mat RandomS8Mat(int w, int h)
  133. {
  134. ncnn::Mat m(w, h, (size_t)1u);
  135. RandomizeS8(m);
  136. return m;
  137. }
  138. ncnn::Mat RandomS8Mat(int w, int h, int c)
  139. {
  140. ncnn::Mat m(w, h, c, (size_t)1u);
  141. RandomizeS8(m);
  142. return m;
  143. }
  144. ncnn::Mat RandomS8Mat(int w, int h, int d, int c)
  145. {
  146. ncnn::Mat m(w, h, d, c, (size_t)1u);
  147. RandomizeS8(m);
  148. return m;
  149. }
  150. ncnn::Mat scales_mat(const ncnn::Mat& mat, int m, int k, int ldx)
  151. {
  152. ncnn::Mat weight_scales(m);
  153. for (int i = 0; i < m; ++i)
  154. {
  155. float min = mat[0], _max = mat[0];
  156. const float* ptr = (const float*)(mat.data) + i * ldx;
  157. for (int j = 0; j < k; ++j)
  158. {
  159. if (min > ptr[j])
  160. {
  161. min = ptr[j];
  162. }
  163. if (_max < ptr[j])
  164. {
  165. _max = ptr[j];
  166. }
  167. }
  168. const float abs_min = abs(min), abs_max = abs(_max);
  169. weight_scales[i] = 127.f / (abs_min > abs_max ? abs_min : abs_max);
  170. }
  171. return weight_scales;
  172. }
  173. bool NearlyEqual(float a, float b, float epsilon)
  174. {
  175. if (a == b)
  176. return true;
  177. float diff = (float)fabs(a - b);
  178. if (diff <= epsilon)
  179. return true;
  180. // relative error
  181. return diff < epsilon * std::max(fabs(a), fabs(b));
  182. }
  183. int Compare(const ncnn::Mat& a, const ncnn::Mat& b, float epsilon)
  184. {
  185. #define CHECK_MEMBER(m) \
  186. if (a.m != b.m) \
  187. { \
  188. fprintf(stderr, #m " not match expect %d but got %d\n", (int)a.m, (int)b.m); \
  189. return -1; \
  190. }
  191. CHECK_MEMBER(dims)
  192. CHECK_MEMBER(w)
  193. CHECK_MEMBER(h)
  194. CHECK_MEMBER(d)
  195. CHECK_MEMBER(c)
  196. CHECK_MEMBER(elemsize)
  197. CHECK_MEMBER(elempack)
  198. #undef CHECK_MEMBER
  199. for (int q = 0; q < a.c; q++)
  200. {
  201. const ncnn::Mat ma = a.channel(q);
  202. const ncnn::Mat mb = b.channel(q);
  203. for (int z = 0; z < a.d; z++)
  204. {
  205. const ncnn::Mat da = ma.depth(z);
  206. const ncnn::Mat db = mb.depth(z);
  207. for (int i = 0; i < a.h; i++)
  208. {
  209. const float* pa = da.row(i);
  210. const float* pb = db.row(i);
  211. for (int j = 0; j < a.w; j++)
  212. {
  213. if (!NearlyEqual(pa[j], pb[j], epsilon))
  214. {
  215. fprintf(stderr, "value not match at c:%d d:%d h:%d w:%d expect %f but got %f\n", q, z, i, j, pa[j], pb[j]);
  216. return -1;
  217. }
  218. }
  219. }
  220. }
  221. }
  222. return 0;
  223. }
  224. int CompareMat(const ncnn::Mat& a, const ncnn::Mat& b, float epsilon)
  225. {
  226. ncnn::Option opt;
  227. opt.num_threads = 1;
  228. if (a.elempack != 1)
  229. {
  230. ncnn::Mat a1;
  231. ncnn::convert_packing(a, a1, 1, opt);
  232. return CompareMat(a1, b, epsilon);
  233. }
  234. if (b.elempack != 1)
  235. {
  236. ncnn::Mat b1;
  237. ncnn::convert_packing(b, b1, 1, opt);
  238. return CompareMat(a, b1, epsilon);
  239. }
  240. if (a.elemsize == 2u)
  241. {
  242. ncnn::Mat a32;
  243. cast_float16_to_float32(a, a32, opt);
  244. return CompareMat(a32, b, epsilon);
  245. }
  246. if (a.elemsize == 1u)
  247. {
  248. ncnn::Mat a32;
  249. cast_int8_to_float32(a, a32, opt);
  250. return CompareMat(a32, b, epsilon);
  251. }
  252. if (b.elemsize == 2u)
  253. {
  254. ncnn::Mat b32;
  255. cast_float16_to_float32(b, b32, opt);
  256. return CompareMat(a, b32, epsilon);
  257. }
  258. if (b.elemsize == 1u)
  259. {
  260. ncnn::Mat b32;
  261. cast_int8_to_float32(b, b32, opt);
  262. return CompareMat(a, b32, epsilon);
  263. }
  264. return Compare(a, b, epsilon);
  265. }
  266. int CompareMat(const std::vector<ncnn::Mat>& a, const std::vector<ncnn::Mat>& b, float epsilon)
  267. {
  268. if (a.size() != b.size())
  269. {
  270. fprintf(stderr, "output blob count not match %zu %zu\n", a.size(), b.size());
  271. return -1;
  272. }
  273. for (size_t i = 0; i < a.size(); i++)
  274. {
  275. if (CompareMat(a[i], b[i], epsilon))
  276. {
  277. fprintf(stderr, "output blob %zu not match\n", i);
  278. return -1;
  279. }
  280. }
  281. return 0;
  282. }
  283. static int convert_to_optimal_layout(const ncnn::Mat& a, ncnn::Mat& a4, const ncnn::Option& opt, const ncnn::Layer* op, int flag)
  284. {
  285. // clang-format off
  286. // *INDENT-OFF*
  287. #if NCNN_VFPV4
  288. if (opt.use_fp16_storage && ncnn::cpu_support_arm_vfpv4() && op->support_fp16_storage && !(flag & TEST_LAYER_DISABLE_AUTO_INPUT_CASTING))
  289. {
  290. ncnn::cast_float32_to_float16(a, a4, opt);
  291. }
  292. else
  293. #endif // NCNN_VFPV4
  294. #if NCNN_RVV
  295. if (opt.use_fp16_storage && ncnn::cpu_support_riscv_v() && ncnn::cpu_support_riscv_zfh() && op->support_fp16_storage && !(flag & TEST_LAYER_DISABLE_AUTO_INPUT_CASTING))
  296. {
  297. ncnn::cast_float32_to_float16(a, a4, opt);
  298. }
  299. else
  300. #endif // NCNN_RVV
  301. #if NCNN_BF16
  302. if (opt.use_bf16_storage && op->support_bf16_storage && !(flag & TEST_LAYER_DISABLE_AUTO_INPUT_CASTING))
  303. {
  304. ncnn::cast_float32_to_bfloat16(a, a4, opt);
  305. }
  306. else
  307. #endif // NCNN_BF16
  308. if (opt.use_fp16_storage && op->support_fp16_storage && !(flag & TEST_LAYER_DISABLE_AUTO_INPUT_CASTING))
  309. {
  310. ncnn::cast_float32_to_float16(a, a4, opt);
  311. }
  312. else
  313. {
  314. a4 = a;
  315. }
  316. // *INDENT-ON*
  317. // clang-format on
  318. if (opt.use_packing_layout && op->support_packing && !(flag & TEST_LAYER_DISABLE_AUTO_INPUT_PACKING))
  319. {
  320. // resolve dst_elempack
  321. int dims = a4.dims;
  322. int elemcount = 0;
  323. if (dims == 1) elemcount = a4.elempack * a4.w;
  324. if (dims == 2) elemcount = a4.elempack * a4.h;
  325. if (dims == 3 || dims == 4) elemcount = a4.elempack * a4.c;
  326. int elembits = a4.elembits();
  327. int dst_elempack = 1;
  328. if (elembits == 32)
  329. {
  330. #if NCNN_AVX512
  331. if (elemcount % 16 == 0 && ncnn::cpu_support_x86_avx512())
  332. dst_elempack = 16;
  333. else if (elemcount % 8 == 0 && ncnn::cpu_support_x86_avx())
  334. dst_elempack = 8;
  335. else if (elemcount % 4 == 0)
  336. dst_elempack = 4;
  337. #elif NCNN_AVX
  338. if (elemcount % 8 == 0 && ncnn::cpu_support_x86_avx())
  339. dst_elempack = 8;
  340. else if (elemcount % 4 == 0)
  341. dst_elempack = 4;
  342. #elif NCNN_RVV
  343. const int packn = ncnn::cpu_riscv_vlenb() / (elembits / 8);
  344. if (elemcount % packn == 0)
  345. dst_elempack = packn;
  346. #else
  347. if (elemcount % 4 == 0)
  348. dst_elempack = 4;
  349. #endif
  350. }
  351. if (elembits == 16)
  352. {
  353. #if NCNN_ARM82
  354. if (elemcount % 8 == 0 && ncnn::cpu_support_arm_asimdhp() && opt.use_fp16_arithmetic)
  355. dst_elempack = 8;
  356. else if (elemcount % 4 == 0)
  357. dst_elempack = 4;
  358. #elif NCNN_RVV
  359. const int packn = ncnn::cpu_riscv_vlenb() / 2;
  360. if (elemcount % packn == 0)
  361. dst_elempack = packn;
  362. #else
  363. if (elemcount % 4 == 0)
  364. dst_elempack = 4;
  365. #endif
  366. }
  367. if (elembits == 8)
  368. {
  369. #if NCNN_RVV
  370. const int packn = ncnn::cpu_riscv_vlenb() / 1;
  371. if (elemcount % packn == 0)
  372. dst_elempack = packn;
  373. #else
  374. if (elemcount % 8 == 0)
  375. dst_elempack = 8;
  376. #endif
  377. }
  378. if (flag & TEST_LAYER_ENABLE_FORCE_INPUT_PACK8)
  379. dst_elempack = 8;
  380. ncnn::Mat a4_packed;
  381. ncnn::convert_packing(a4, a4_packed, dst_elempack, opt);
  382. a4 = a4_packed;
  383. }
  384. return 0;
  385. }
  386. static int convert_to_vanilla_layout(const ncnn::Mat& c4, ncnn::Mat& c, const ncnn::Option& opt, const ncnn::Layer* op, int flag)
  387. {
  388. ncnn::Mat c4_unpacked;
  389. if (c4.elempack != 1)
  390. {
  391. ncnn::convert_packing(c4, c4_unpacked, 1, opt);
  392. }
  393. else
  394. {
  395. c4_unpacked = c4;
  396. }
  397. // clang-format off
  398. // *INDENT-OFF*
  399. #if NCNN_VFPV4
  400. if (opt.use_fp16_storage && ncnn::cpu_support_arm_vfpv4() && op->support_fp16_storage && c4_unpacked.elembits() == 16)
  401. {
  402. ncnn::cast_float16_to_float32(c4_unpacked, c, opt);
  403. }
  404. else
  405. #endif // NCNN_VFPV4
  406. #if NCNN_RVV
  407. if (opt.use_fp16_storage && ncnn::cpu_support_riscv_v() && ncnn::cpu_support_riscv_zfh() && op->support_fp16_storage && c4_unpacked.elembits() == 16)
  408. {
  409. ncnn::cast_float16_to_float32(c4_unpacked, c, opt);
  410. }
  411. else
  412. #endif // NCNN_RVV
  413. #if NCNN_BF16
  414. if (opt.use_bf16_storage && op->support_bf16_storage && c4_unpacked.elembits() == 16)
  415. {
  416. ncnn::cast_bfloat16_to_float32(c4_unpacked, c, opt);
  417. }
  418. else
  419. #endif // NCNN_BF16
  420. if (opt.use_fp16_storage && op->support_fp16_storage && c4_unpacked.elembits() == 16)
  421. {
  422. ncnn::cast_float16_to_float32(c4_unpacked, c, opt);
  423. }
  424. else
  425. {
  426. c = c4_unpacked;
  427. }
  428. // *INDENT-ON*
  429. // clang-format on
  430. return 0;
  431. }
  432. int test_layer_naive(int typeindex, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const std::vector<ncnn::Mat>& a, int top_blob_count, std::vector<ncnn::Mat>& b, void (*func)(ncnn::Layer*), int flag)
  433. {
  434. ncnn::Layer* op = ncnn::create_layer_naive(typeindex);
  435. if (func)
  436. {
  437. (*func)((ncnn::Layer*)op);
  438. }
  439. op->load_param(pd);
  440. if (op->one_blob_only && a.size() != 1)
  441. {
  442. fprintf(stderr, "layer with one_blob_only but consume multiple inputs\n");
  443. delete op;
  444. return -1;
  445. }
  446. ncnn::ModelBinFromMatArray mb(weights.data());
  447. op->load_model(mb);
  448. ncnn::Option opt;
  449. opt.num_threads = 1;
  450. opt.lightmode = false;
  451. opt.use_packing_layout = false;
  452. opt.use_fp16_packed = false;
  453. opt.use_fp16_storage = false;
  454. opt.use_fp16_arithmetic = false;
  455. opt.use_shader_pack8 = false;
  456. opt.use_image_storage = false;
  457. opt.use_bf16_storage = false;
  458. opt.use_vulkan_compute = false;
  459. op->create_pipeline(opt);
  460. b.resize(top_blob_count);
  461. if (op->support_inplace)
  462. {
  463. for (size_t i = 0; i < a.size(); i++)
  464. {
  465. b[i] = a[i].clone();
  466. }
  467. op->forward_inplace(b, opt);
  468. }
  469. else
  470. {
  471. op->forward(a, b, opt);
  472. }
  473. op->destroy_pipeline(opt);
  474. delete op;
  475. return 0;
  476. }
  477. int test_layer_cpu(int typeindex, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Option& _opt, const std::vector<ncnn::Mat>& a, int top_blob_count, std::vector<ncnn::Mat>& c, const std::vector<ncnn::Mat>& top_shapes, void (*func)(ncnn::Layer*), int flag)
  478. {
  479. ncnn::Layer* op = ncnn::create_layer_cpu(typeindex);
  480. if (!op->support_packing && _opt.use_packing_layout)
  481. {
  482. delete op;
  483. return 233;
  484. }
  485. if (!op->support_bf16_storage && !op->support_fp16_storage && (_opt.use_bf16_storage || _opt.use_fp16_arithmetic))
  486. {
  487. delete op;
  488. return 233;
  489. }
  490. if (func)
  491. {
  492. (*func)((ncnn::Layer*)op);
  493. }
  494. if (!top_shapes.empty())
  495. {
  496. op->bottom_shapes = a;
  497. op->top_shapes = top_shapes;
  498. }
  499. op->load_param(pd);
  500. if (op->one_blob_only && a.size() != 1)
  501. {
  502. fprintf(stderr, "layer with one_blob_only but consume multiple inputs\n");
  503. delete op;
  504. return -1;
  505. }
  506. ncnn::ModelBinFromMatArray mb(weights.data());
  507. op->load_model(mb);
  508. ncnn::Option opt = _opt;
  509. opt.num_threads = 1;
  510. opt.use_vulkan_compute = false;
  511. op->create_pipeline(opt);
  512. if (!op->support_packing && _opt.use_packing_layout)
  513. {
  514. op->destroy_pipeline(opt);
  515. delete op;
  516. return 233;
  517. }
  518. if (!op->support_bf16_storage && !op->support_fp16_storage && (_opt.use_bf16_storage || _opt.use_fp16_arithmetic))
  519. {
  520. op->destroy_pipeline(opt);
  521. delete op;
  522. return 233;
  523. }
  524. std::vector<ncnn::Mat> a4(a.size());
  525. for (size_t i = 0; i < a4.size(); i++)
  526. {
  527. convert_to_optimal_layout(a[i], a4[i], opt, op, flag);
  528. }
  529. c.resize(top_blob_count);
  530. if (op->support_inplace)
  531. {
  532. for (size_t i = 0; i < a4.size(); i++)
  533. {
  534. c[i] = a4[i].clone();
  535. }
  536. op->forward_inplace(c, opt);
  537. }
  538. else
  539. {
  540. op->forward(a4, c, opt);
  541. }
  542. for (size_t i = 0; i < c.size(); i++)
  543. {
  544. convert_to_vanilla_layout(c[i], c[i], opt, op, flag);
  545. }
  546. op->destroy_pipeline(opt);
  547. delete op;
  548. return 0;
  549. }
  550. #if NCNN_VULKAN
  551. int test_layer_gpu(int typeindex, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Option& _opt, const std::vector<ncnn::Mat>& a, int top_blob_count, std::vector<ncnn::Mat>& d, const std::vector<ncnn::Mat>& top_shapes, void (*func)(ncnn::Layer*), int flag)
  552. {
  553. if (!_opt.use_packing_layout)
  554. {
  555. // pack1 test is useless for gpu
  556. return 233;
  557. }
  558. ncnn::Layer* op = ncnn::create_layer_vulkan(typeindex);
  559. if (!op)
  560. {
  561. return 233;
  562. }
  563. op->load_param(pd);
  564. if (!op->support_vulkan)
  565. {
  566. delete op;
  567. return 233;
  568. }
  569. ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device();
  570. op->vkdev = vkdev;
  571. if (func)
  572. {
  573. (*func)((ncnn::Layer*)op);
  574. }
  575. if (!top_shapes.empty())
  576. {
  577. op->bottom_shapes = a;
  578. op->top_shapes = top_shapes;
  579. }
  580. if (op->one_blob_only && a.size() != 1)
  581. {
  582. fprintf(stderr, "layer with one_blob_only but consume multiple inputs\n");
  583. delete op;
  584. return -1;
  585. }
  586. ncnn::ModelBinFromMatArray mb(weights.data());
  587. op->load_model(mb);
  588. ncnn::VkWeightAllocator g_weight_vkallocator(vkdev);
  589. ncnn::VkWeightStagingAllocator g_weight_staging_vkallocator(vkdev);
  590. ncnn::VkAllocator* blob_vkallocator = vkdev->acquire_blob_allocator();
  591. ncnn::VkAllocator* staging_vkallocator = vkdev->acquire_staging_allocator();
  592. ncnn::Option opt = _opt;
  593. opt.num_threads = 1;
  594. opt.use_vulkan_compute = true;
  595. #if __APPLE__
  596. opt.use_image_storage = false;
  597. #endif
  598. opt.blob_vkallocator = blob_vkallocator;
  599. opt.workspace_vkallocator = blob_vkallocator;
  600. opt.staging_vkallocator = staging_vkallocator;
  601. if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
  602. if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;
  603. if (!vkdev->info.support_fp16_uniform()) opt.use_fp16_uniform = false;
  604. if (!vkdev->info.support_fp16_arithmetic()) opt.use_fp16_arithmetic = false;
  605. if (!vkdev->info.support_int8_packed()) opt.use_int8_packed = false;
  606. if (!vkdev->info.support_int8_storage()) opt.use_int8_storage = false;
  607. if (!vkdev->info.support_int8_uniform()) opt.use_int8_uniform = false;
  608. if (!vkdev->info.support_int8_arithmetic()) opt.use_int8_arithmetic = false;
  609. if (!vkdev->info.support_cooperative_matrix()) opt.use_cooperative_matrix = false;
  610. // FIXME fp16a may produce large error
  611. opt.use_fp16_arithmetic = false;
  612. op->create_pipeline(opt);
  613. if (!op->support_vulkan)
  614. {
  615. op->destroy_pipeline(opt);
  616. delete op;
  617. return 233;
  618. }
  619. {
  620. ncnn::VkTransfer cmd(vkdev);
  621. ncnn::Option opt_upload = opt;
  622. opt_upload.blob_vkallocator = &g_weight_vkallocator;
  623. opt_upload.workspace_vkallocator = &g_weight_vkallocator;
  624. opt_upload.staging_vkallocator = &g_weight_staging_vkallocator;
  625. op->upload_model(cmd, opt_upload);
  626. cmd.submit_and_wait();
  627. }
  628. d.resize(top_blob_count);
  629. {
  630. // forward
  631. ncnn::VkCompute cmd(vkdev);
  632. if (op->support_image_storage && opt.use_image_storage)
  633. {
  634. // upload
  635. std::vector<ncnn::VkImageMat> a_gpu(a.size());
  636. for (size_t i = 0; i < a_gpu.size(); i++)
  637. {
  638. cmd.record_upload(a[i], a_gpu[i], opt);
  639. }
  640. std::vector<ncnn::VkImageMat> d_gpu(top_blob_count);
  641. if (op->support_inplace)
  642. {
  643. op->forward_inplace(a_gpu, cmd, opt);
  644. d_gpu = a_gpu;
  645. }
  646. else
  647. {
  648. op->forward(a_gpu, d_gpu, cmd, opt);
  649. }
  650. // download
  651. for (size_t i = 0; i < d_gpu.size(); i++)
  652. {
  653. cmd.record_download(d_gpu[i], d[i], opt);
  654. }
  655. }
  656. else
  657. {
  658. // upload
  659. std::vector<ncnn::VkMat> a_gpu(a.size());
  660. for (size_t i = 0; i < a_gpu.size(); i++)
  661. {
  662. cmd.record_upload(a[i], a_gpu[i], opt);
  663. }
  664. std::vector<ncnn::VkMat> d_gpu(top_blob_count);
  665. if (op->support_inplace)
  666. {
  667. op->forward_inplace(a_gpu, cmd, opt);
  668. d_gpu = a_gpu;
  669. }
  670. else
  671. {
  672. op->forward(a_gpu, d_gpu, cmd, opt);
  673. }
  674. // download
  675. for (size_t i = 0; i < d_gpu.size(); i++)
  676. {
  677. cmd.record_download(d_gpu[i], d[i], opt);
  678. }
  679. }
  680. cmd.submit_and_wait();
  681. }
  682. op->destroy_pipeline(opt);
  683. delete op;
  684. vkdev->reclaim_blob_allocator(blob_vkallocator);
  685. vkdev->reclaim_staging_allocator(staging_vkallocator);
  686. g_weight_vkallocator.clear();
  687. g_weight_staging_vkallocator.clear();
  688. return 0;
  689. }
  690. #endif // NCNN_VULKAN
  691. int test_layer(int typeindex, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Option& _opt, const std::vector<ncnn::Mat>& a, int top_blob_count, const std::vector<ncnn::Mat>& top_shapes, float epsilon, void (*func)(ncnn::Layer*), int flag)
  692. {
  693. // naive
  694. std::vector<ncnn::Mat> b;
  695. {
  696. int ret = test_layer_naive(typeindex, pd, weights, a, top_blob_count, b, func, flag);
  697. if (ret != 233 && ret != 0)
  698. {
  699. fprintf(stderr, "test_layer_naive failed\n");
  700. return -1;
  701. }
  702. }
  703. // cpu
  704. {
  705. std::vector<ncnn::Mat> c;
  706. int ret = test_layer_cpu(typeindex, pd, weights, _opt, a, top_blob_count, c, std::vector<ncnn::Mat>(), func, flag);
  707. if (ret != 233 && (ret != 0 || CompareMat(b, c, epsilon) != 0))
  708. {
  709. fprintf(stderr, "test_layer_cpu failed\n");
  710. return -1;
  711. }
  712. }
  713. // cpu shape hint
  714. {
  715. std::vector<ncnn::Mat> c;
  716. int ret = test_layer_cpu(typeindex, pd, weights, _opt, a, top_blob_count, c, b, func, flag);
  717. if (ret != 233 && (ret != 0 || CompareMat(b, c, epsilon) != 0))
  718. {
  719. fprintf(stderr, "test_layer_cpu failed with shape hint\n");
  720. return -1;
  721. }
  722. }
  723. #if NCNN_VULKAN
  724. // gpu
  725. if (!(flag & TEST_LAYER_DISABLE_GPU_TESTING))
  726. {
  727. std::vector<ncnn::Mat> d;
  728. int ret = test_layer_gpu(typeindex, pd, weights, _opt, a, top_blob_count, d, std::vector<ncnn::Mat>(), func, flag);
  729. if (ret != 233 && (ret != 0 || CompareMat(b, d, epsilon) != 0))
  730. {
  731. fprintf(stderr, "test_layer_gpu failed\n");
  732. return -1;
  733. }
  734. }
  735. // gpu shape hint
  736. if (!(flag & TEST_LAYER_DISABLE_GPU_TESTING))
  737. {
  738. std::vector<ncnn::Mat> d;
  739. int ret = test_layer_gpu(typeindex, pd, weights, _opt, a, top_blob_count, d, b, func, flag);
  740. if (ret != 233 && (ret != 0 || CompareMat(b, d, epsilon) != 0))
  741. {
  742. fprintf(stderr, "test_layer_gpu failed with shape hint\n");
  743. return -1;
  744. }
  745. }
  746. #endif // NCNN_VULKAN
  747. return 0;
  748. }
  749. int test_layer_naive(int typeindex, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Mat& a, ncnn::Mat& b, void (*func)(ncnn::Layer*), int flag)
  750. {
  751. ncnn::Layer* op = ncnn::create_layer_naive(typeindex);
  752. if (func)
  753. {
  754. (*func)((ncnn::Layer*)op);
  755. }
  756. op->load_param(pd);
  757. ncnn::ModelBinFromMatArray mb(weights.data());
  758. op->load_model(mb);
  759. ncnn::Option opt;
  760. opt.num_threads = 1;
  761. opt.lightmode = false;
  762. opt.use_packing_layout = false;
  763. opt.use_fp16_packed = false;
  764. opt.use_fp16_storage = false;
  765. opt.use_fp16_arithmetic = false;
  766. opt.use_shader_pack8 = false;
  767. opt.use_image_storage = false;
  768. opt.use_bf16_storage = false;
  769. opt.use_vulkan_compute = false;
  770. op->create_pipeline(opt);
  771. if (op->support_inplace)
  772. {
  773. b = a.clone();
  774. op->forward_inplace(b, opt);
  775. }
  776. else
  777. {
  778. op->forward(a, b, opt);
  779. }
  780. op->destroy_pipeline(opt);
  781. delete op;
  782. return 0;
  783. }
  784. int test_layer_cpu(int typeindex, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Option& _opt, const ncnn::Mat& a, ncnn::Mat& c, const ncnn::Mat& top_shape, void (*func)(ncnn::Layer*), int flag)
  785. {
  786. ncnn::Layer* op = ncnn::create_layer_cpu(typeindex);
  787. if (!op->support_packing && _opt.use_packing_layout)
  788. {
  789. delete op;
  790. return 233;
  791. }
  792. if (!op->support_bf16_storage && !op->support_fp16_storage && (_opt.use_bf16_storage || _opt.use_fp16_arithmetic))
  793. {
  794. delete op;
  795. return 233;
  796. }
  797. if (func)
  798. {
  799. (*func)((ncnn::Layer*)op);
  800. }
  801. if (top_shape.dims)
  802. {
  803. op->bottom_shapes.resize(1);
  804. op->top_shapes.resize(1);
  805. op->bottom_shapes[0] = a;
  806. op->top_shapes[0] = top_shape;
  807. }
  808. op->load_param(pd);
  809. ncnn::ModelBinFromMatArray mb(weights.data());
  810. op->load_model(mb);
  811. ncnn::Option opt = _opt;
  812. opt.num_threads = 1;
  813. opt.use_vulkan_compute = false;
  814. op->create_pipeline(opt);
  815. if (!op->support_packing && _opt.use_packing_layout)
  816. {
  817. op->destroy_pipeline(opt);
  818. delete op;
  819. return 233;
  820. }
  821. if (!op->support_bf16_storage && !op->support_fp16_storage && (_opt.use_bf16_storage || _opt.use_fp16_arithmetic))
  822. {
  823. op->destroy_pipeline(opt);
  824. delete op;
  825. return 233;
  826. }
  827. ncnn::Mat a4;
  828. convert_to_optimal_layout(a, a4, opt, op, flag);
  829. if (op->support_inplace)
  830. {
  831. c = a4.clone();
  832. op->forward_inplace(c, opt);
  833. }
  834. else
  835. {
  836. op->forward(a4, c, opt);
  837. }
  838. convert_to_vanilla_layout(c, c, opt, op, flag);
  839. op->destroy_pipeline(opt);
  840. delete op;
  841. return 0;
  842. }
  843. #if NCNN_VULKAN
  844. int test_layer_gpu(int typeindex, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Option& _opt, const ncnn::Mat& a, ncnn::Mat& d, const ncnn::Mat& top_shape, void (*func)(ncnn::Layer*), int flag)
  845. {
  846. if (!_opt.use_packing_layout)
  847. {
  848. // pack1 test is useless for gpu
  849. return 233;
  850. }
  851. ncnn::Layer* op = ncnn::create_layer_vulkan(typeindex);
  852. if (!op)
  853. {
  854. return 233;
  855. }
  856. op->load_param(pd);
  857. if (!op->support_vulkan)
  858. {
  859. delete op;
  860. return 233;
  861. }
  862. ncnn::VulkanDevice* vkdev = ncnn::get_gpu_device();
  863. op->vkdev = vkdev;
  864. if (func)
  865. {
  866. (*func)((ncnn::Layer*)op);
  867. }
  868. if (top_shape.dims)
  869. {
  870. op->bottom_shapes.resize(1);
  871. op->top_shapes.resize(1);
  872. op->bottom_shapes[0] = a;
  873. op->top_shapes[0] = top_shape;
  874. }
  875. ncnn::ModelBinFromMatArray mb(weights.data());
  876. op->load_model(mb);
  877. ncnn::VkWeightAllocator g_weight_vkallocator(vkdev);
  878. ncnn::VkWeightStagingAllocator g_weight_staging_vkallocator(vkdev);
  879. ncnn::VkAllocator* blob_vkallocator = vkdev->acquire_blob_allocator();
  880. ncnn::VkAllocator* staging_vkallocator = vkdev->acquire_staging_allocator();
  881. ncnn::Option opt = _opt;
  882. opt.num_threads = 1;
  883. opt.use_vulkan_compute = true;
  884. #if __APPLE__
  885. opt.use_image_storage = false;
  886. #endif
  887. opt.blob_vkallocator = blob_vkallocator;
  888. opt.workspace_vkallocator = blob_vkallocator;
  889. opt.staging_vkallocator = staging_vkallocator;
  890. if (!vkdev->info.support_fp16_packed()) opt.use_fp16_packed = false;
  891. if (!vkdev->info.support_fp16_storage()) opt.use_fp16_storage = false;
  892. if (!vkdev->info.support_fp16_uniform()) opt.use_fp16_uniform = false;
  893. if (!vkdev->info.support_fp16_arithmetic()) opt.use_fp16_arithmetic = false;
  894. if (!vkdev->info.support_int8_packed()) opt.use_int8_packed = false;
  895. if (!vkdev->info.support_int8_storage()) opt.use_int8_storage = false;
  896. if (!vkdev->info.support_int8_uniform()) opt.use_int8_uniform = false;
  897. if (!vkdev->info.support_int8_arithmetic()) opt.use_int8_arithmetic = false;
  898. if (!vkdev->info.support_cooperative_matrix()) opt.use_cooperative_matrix = false;
  899. // FIXME fp16a may produce large error
  900. opt.use_fp16_arithmetic = false;
  901. op->create_pipeline(opt);
  902. if (!op->support_vulkan)
  903. {
  904. op->destroy_pipeline(opt);
  905. delete op;
  906. return 233;
  907. }
  908. {
  909. ncnn::VkTransfer cmd(vkdev);
  910. ncnn::Option opt_upload = opt;
  911. opt_upload.blob_vkallocator = &g_weight_vkallocator;
  912. opt_upload.workspace_vkallocator = &g_weight_vkallocator;
  913. opt_upload.staging_vkallocator = &g_weight_staging_vkallocator;
  914. op->upload_model(cmd, opt_upload);
  915. cmd.submit_and_wait();
  916. }
  917. {
  918. // forward
  919. ncnn::VkCompute cmd(vkdev);
  920. if (op->support_image_storage && opt.use_image_storage)
  921. {
  922. // upload
  923. ncnn::VkImageMat a_gpu;
  924. cmd.record_upload(a, a_gpu, opt);
  925. ncnn::VkImageMat d_gpu;
  926. if (op->support_inplace)
  927. {
  928. op->forward_inplace(a_gpu, cmd, opt);
  929. d_gpu = a_gpu;
  930. }
  931. else
  932. {
  933. op->forward(a_gpu, d_gpu, cmd, opt);
  934. }
  935. // download
  936. cmd.record_download(d_gpu, d, opt);
  937. }
  938. else
  939. {
  940. // upload
  941. ncnn::VkMat a_gpu;
  942. cmd.record_upload(a, a_gpu, opt);
  943. ncnn::VkMat d_gpu;
  944. if (op->support_inplace)
  945. {
  946. op->forward_inplace(a_gpu, cmd, opt);
  947. d_gpu = a_gpu;
  948. }
  949. else
  950. {
  951. op->forward(a_gpu, d_gpu, cmd, opt);
  952. }
  953. // download
  954. cmd.record_download(d_gpu, d, opt);
  955. }
  956. cmd.submit_and_wait();
  957. }
  958. op->destroy_pipeline(opt);
  959. delete op;
  960. vkdev->reclaim_blob_allocator(blob_vkallocator);
  961. vkdev->reclaim_staging_allocator(staging_vkallocator);
  962. g_weight_vkallocator.clear();
  963. g_weight_staging_vkallocator.clear();
  964. return 0;
  965. }
  966. #endif // NCNN_VULKAN
  967. int test_layer(int typeindex, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Option& _opt, const ncnn::Mat& a, const ncnn::Mat& top_shape, float epsilon, void (*func)(ncnn::Layer*), int flag)
  968. {
  969. // naive
  970. ncnn::Mat b;
  971. {
  972. int ret = test_layer_naive(typeindex, pd, weights, a, b, func, flag);
  973. if (ret != 233 && ret != 0)
  974. {
  975. fprintf(stderr, "test_layer_naive failed\n");
  976. return -1;
  977. }
  978. }
  979. // cpu
  980. {
  981. ncnn::Mat c;
  982. int ret = test_layer_cpu(typeindex, pd, weights, _opt, a, c, ncnn::Mat(), func, flag);
  983. if (ret != 233 && (ret != 0 || CompareMat(b, c, epsilon) != 0))
  984. {
  985. fprintf(stderr, "test_layer_cpu failed\n");
  986. return -1;
  987. }
  988. }
  989. // cpu shape hint
  990. {
  991. ncnn::Mat c;
  992. int ret = test_layer_cpu(typeindex, pd, weights, _opt, a, c, b, func, flag);
  993. if (ret != 233 && (ret != 0 || CompareMat(b, c, epsilon) != 0))
  994. {
  995. fprintf(stderr, "test_layer_cpu failed with shape hint\n");
  996. return -1;
  997. }
  998. }
  999. #if NCNN_VULKAN
  1000. // gpu
  1001. if (!(flag & TEST_LAYER_DISABLE_GPU_TESTING))
  1002. {
  1003. ncnn::Mat d;
  1004. int ret = test_layer_gpu(typeindex, pd, weights, _opt, a, d, ncnn::Mat(), func, flag);
  1005. if (ret != 233 && (ret != 0 || CompareMat(b, d, epsilon) != 0))
  1006. {
  1007. fprintf(stderr, "test_layer_gpu failed\n");
  1008. return -1;
  1009. }
  1010. }
  1011. // gpu shape hint
  1012. if (!(flag & TEST_LAYER_DISABLE_GPU_TESTING))
  1013. {
  1014. ncnn::Mat d;
  1015. int ret = test_layer_gpu(typeindex, pd, weights, _opt, a, d, b, func, flag);
  1016. if (ret != 233 && (ret != 0 || CompareMat(b, d, epsilon) != 0))
  1017. {
  1018. fprintf(stderr, "test_layer_gpu failed with shape hint\n");
  1019. return -1;
  1020. }
  1021. }
  1022. #endif // NCNN_VULKAN
  1023. return 0;
  1024. }
  1025. int test_layer_opt(const char* layer_type, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Option& opt, const std::vector<ncnn::Mat>& a, int top_blob_count, float epsilon, void (*func)(ncnn::Layer*), int flag)
  1026. {
  1027. // fp16 representation
  1028. std::vector<ncnn::Mat> a_fp16;
  1029. if (opt.use_bf16_storage && !(flag & TEST_LAYER_DISABLE_AUTO_INPUT_CASTING))
  1030. {
  1031. a_fp16.resize(a.size());
  1032. for (size_t j = 0; j < a.size(); j++)
  1033. {
  1034. ncnn::Mat tmp;
  1035. ncnn::cast_float32_to_bfloat16(a[j], tmp, opt);
  1036. ncnn::cast_bfloat16_to_float32(tmp, a_fp16[j], opt);
  1037. }
  1038. }
  1039. else if ((opt.use_fp16_packed || opt.use_fp16_storage) && !(flag & TEST_LAYER_DISABLE_AUTO_INPUT_CASTING))
  1040. {
  1041. a_fp16.resize(a.size());
  1042. for (size_t j = 0; j < a.size(); j++)
  1043. {
  1044. ncnn::Mat tmp;
  1045. ncnn::cast_float32_to_float16(a[j], tmp, opt);
  1046. ncnn::cast_float16_to_float32(tmp, a_fp16[j], opt);
  1047. }
  1048. }
  1049. else
  1050. {
  1051. a_fp16 = a;
  1052. }
  1053. std::vector<ncnn::Mat> weights_fp16;
  1054. float epsilon_fp16;
  1055. if (opt.use_bf16_storage)
  1056. {
  1057. weights_fp16.resize(weights.size());
  1058. for (size_t j = 0; j < weights.size(); j++)
  1059. {
  1060. if (weights[j].elembits() != 32)
  1061. {
  1062. weights_fp16[j] = weights[j];
  1063. continue;
  1064. }
  1065. ncnn::Mat tmp;
  1066. ncnn::cast_float32_to_bfloat16(weights[j], tmp, opt);
  1067. ncnn::cast_bfloat16_to_float32(tmp, weights_fp16[j], opt);
  1068. }
  1069. epsilon_fp16 = epsilon * 100; // 0.1
  1070. }
  1071. else if (opt.use_fp16_packed || opt.use_fp16_storage)
  1072. {
  1073. weights_fp16.resize(weights.size());
  1074. for (size_t j = 0; j < weights.size(); j++)
  1075. {
  1076. if (weights[j].elembits() != 32)
  1077. {
  1078. weights_fp16[j] = weights[j];
  1079. continue;
  1080. }
  1081. ncnn::Mat tmp;
  1082. ncnn::cast_float32_to_float16(weights[j], tmp, opt);
  1083. ncnn::cast_float16_to_float32(tmp, weights_fp16[j], opt);
  1084. }
  1085. epsilon_fp16 = epsilon * 100; // 0.1
  1086. }
  1087. else
  1088. {
  1089. weights_fp16 = weights;
  1090. epsilon_fp16 = epsilon;
  1091. }
  1092. if (opt.use_fp16_arithmetic)
  1093. {
  1094. epsilon_fp16 = epsilon * 1000; // 1.0
  1095. }
  1096. std::vector<ncnn::Mat> top_shapes;
  1097. int ret = test_layer(ncnn::layer_to_index(layer_type), pd, weights_fp16, opt, a_fp16, top_blob_count, top_shapes, epsilon_fp16, func, flag);
  1098. if (ret != 0)
  1099. {
  1100. fprintf(stderr, "test_layer %s failed use_packing_layout=%d use_fp16_packed=%d use_fp16_storage=%d use_fp16_arithmetic=%d use_shader_pack8=%d use_bf16_storage=%d use_image_storage=%d use_sgemm_convolution=%d use_winograd_convolution=%d\n", layer_type, opt.use_packing_layout, opt.use_fp16_packed, opt.use_fp16_storage, opt.use_fp16_arithmetic, opt.use_shader_pack8, opt.use_bf16_storage, opt.use_image_storage, opt.use_sgemm_convolution, opt.use_winograd_convolution);
  1101. return ret;
  1102. }
  1103. return 0;
  1104. }
  1105. int test_layer_opt(const char* layer_type, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Option& opt, const ncnn::Mat& a, float epsilon, void (*func)(ncnn::Layer*), int flag)
  1106. {
  1107. // fp16 representation
  1108. ncnn::Mat a_fp16;
  1109. if (opt.use_bf16_storage && !(flag & TEST_LAYER_DISABLE_AUTO_INPUT_CASTING))
  1110. {
  1111. ncnn::Mat tmp;
  1112. ncnn::cast_float32_to_bfloat16(a, tmp, opt);
  1113. ncnn::cast_bfloat16_to_float32(tmp, a_fp16, opt);
  1114. }
  1115. else if ((opt.use_fp16_packed || opt.use_fp16_storage) && !(flag & TEST_LAYER_DISABLE_AUTO_INPUT_CASTING))
  1116. {
  1117. ncnn::Mat tmp;
  1118. ncnn::cast_float32_to_float16(a, tmp, opt);
  1119. ncnn::cast_float16_to_float32(tmp, a_fp16, opt);
  1120. }
  1121. else
  1122. {
  1123. a_fp16 = a;
  1124. }
  1125. std::vector<ncnn::Mat> weights_fp16;
  1126. float epsilon_fp16;
  1127. if (opt.use_bf16_storage)
  1128. {
  1129. weights_fp16.resize(weights.size());
  1130. for (size_t j = 0; j < weights.size(); j++)
  1131. {
  1132. if (weights[j].elembits() != 32)
  1133. {
  1134. weights_fp16[j] = weights[j];
  1135. continue;
  1136. }
  1137. ncnn::Mat tmp;
  1138. ncnn::cast_float32_to_bfloat16(weights[j], tmp, opt);
  1139. ncnn::cast_bfloat16_to_float32(tmp, weights_fp16[j], opt);
  1140. }
  1141. epsilon_fp16 = epsilon * 100; // 0.1
  1142. }
  1143. else if (opt.use_fp16_packed || opt.use_fp16_storage)
  1144. {
  1145. weights_fp16.resize(weights.size());
  1146. for (size_t j = 0; j < weights.size(); j++)
  1147. {
  1148. if (weights[j].elembits() != 32)
  1149. {
  1150. weights_fp16[j] = weights[j];
  1151. continue;
  1152. }
  1153. ncnn::Mat tmp;
  1154. ncnn::cast_float32_to_float16(weights[j], tmp, opt);
  1155. ncnn::cast_float16_to_float32(tmp, weights_fp16[j], opt);
  1156. }
  1157. epsilon_fp16 = epsilon * 100; // 0.1
  1158. }
  1159. else
  1160. {
  1161. weights_fp16 = weights;
  1162. epsilon_fp16 = epsilon;
  1163. }
  1164. if (opt.use_fp16_arithmetic)
  1165. {
  1166. epsilon_fp16 = epsilon * 1000; // 1.0
  1167. }
  1168. ncnn::Mat top_shape;
  1169. int ret = test_layer(ncnn::layer_to_index(layer_type), pd, weights_fp16, opt, a_fp16, top_shape, epsilon_fp16, func, flag);
  1170. if (ret != 0)
  1171. {
  1172. fprintf(stderr, "test_layer %s failed use_packing_layout=%d use_fp16_packed=%d use_fp16_storage=%d use_fp16_arithmetic=%d use_shader_pack8=%d use_bf16_storage=%d use_image_storage=%d use_sgemm_convolution=%d use_winograd_convolution=%d\n", layer_type, opt.use_packing_layout, opt.use_fp16_packed, opt.use_fp16_storage, opt.use_fp16_arithmetic, opt.use_shader_pack8, opt.use_bf16_storage, opt.use_image_storage, opt.use_sgemm_convolution, opt.use_winograd_convolution);
  1173. return ret;
  1174. }
  1175. return 0;
  1176. }
  1177. int test_layer(const char* layer_type, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const std::vector<ncnn::Mat>& a, int top_blob_count, float epsilon, void (*func)(ncnn::Layer*), int flag)
  1178. {
  1179. // pack fp16p fp16s fp16a bf16s shader8 image
  1180. const int options[][7] = {
  1181. {0, 0, 0, 0, 0, 0, 0},
  1182. {0, 0, 1, 0, 0, 0, 0},
  1183. {0, 0, 1, 1, 1, 0, 0},
  1184. {1, 0, 0, 0, 0, 0, 0},
  1185. {1, 1, 0, 0, 1, 0, 0},
  1186. {1, 0, 1, 0, 0, 1, 0},
  1187. {1, 1, 1, 1, 0, 0, 0},
  1188. {1, 1, 1, 1, 1, 1, 1},
  1189. };
  1190. const int opt_count = sizeof(options) / sizeof(options[0]);
  1191. for (int i = 0; i < opt_count; i++)
  1192. {
  1193. ncnn::Option opt;
  1194. opt.num_threads = 1;
  1195. opt.use_packing_layout = options[i][0];
  1196. opt.use_fp16_packed = options[i][1];
  1197. opt.use_fp16_storage = options[i][2];
  1198. opt.use_fp16_arithmetic = options[i][3];
  1199. opt.use_bf16_storage = options[i][4];
  1200. opt.use_shader_pack8 = options[i][5];
  1201. opt.use_image_storage = options[i][6];
  1202. int ret = test_layer_opt(layer_type, pd, weights, opt, a, top_blob_count, epsilon, func, flag);
  1203. if (ret != 0)
  1204. return ret;
  1205. }
  1206. return 0;
  1207. }
  1208. int test_layer(const char* layer_type, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Mat& a, float epsilon, void (*func)(ncnn::Layer*), int flag)
  1209. {
  1210. // pack fp16p fp16s fp16a bf16s shader8 image
  1211. const int options[][7] = {
  1212. {0, 0, 0, 0, 0, 0, 0},
  1213. {0, 0, 1, 0, 0, 0, 0},
  1214. {0, 0, 1, 1, 1, 0, 0},
  1215. {1, 0, 0, 0, 0, 0, 0},
  1216. {1, 1, 0, 0, 1, 0, 0},
  1217. {1, 0, 1, 0, 0, 1, 0},
  1218. {1, 1, 1, 1, 0, 0, 0},
  1219. {1, 1, 1, 1, 1, 1, 1},
  1220. };
  1221. const int opt_count = sizeof(options) / sizeof(options[0]);
  1222. for (int i = 0; i < opt_count; i++)
  1223. {
  1224. ncnn::Option opt;
  1225. opt.num_threads = 1;
  1226. opt.use_packing_layout = options[i][0];
  1227. opt.use_fp16_packed = options[i][1];
  1228. opt.use_fp16_storage = options[i][2];
  1229. opt.use_fp16_arithmetic = options[i][3];
  1230. opt.use_bf16_storage = options[i][4];
  1231. opt.use_shader_pack8 = options[i][5];
  1232. opt.use_image_storage = options[i][6];
  1233. int ret = test_layer_opt(layer_type, pd, weights, opt, a, epsilon, func, flag);
  1234. if (ret != 0)
  1235. return ret;
  1236. }
  1237. return 0;
  1238. }
  1239. class TestOOMAllocator : public ncnn::UnlockedPoolAllocator
  1240. {
  1241. public:
  1242. TestOOMAllocator();
  1243. virtual void* fastMalloc(size_t size);
  1244. virtual void fastFree(void* ptr);
  1245. ncnn::Mutex lock;
  1246. int counter;
  1247. int failid;
  1248. };
  1249. TestOOMAllocator::TestOOMAllocator()
  1250. {
  1251. counter = 0;
  1252. failid = INT_MAX;
  1253. }
  1254. void* TestOOMAllocator::fastMalloc(size_t size)
  1255. {
  1256. lock.lock();
  1257. void* ptr;
  1258. if (counter == failid)
  1259. {
  1260. ptr = 0;
  1261. }
  1262. else
  1263. {
  1264. ptr = ncnn::UnlockedPoolAllocator::fastMalloc(size);
  1265. }
  1266. counter++;
  1267. lock.unlock();
  1268. return ptr;
  1269. }
  1270. void TestOOMAllocator::fastFree(void* ptr)
  1271. {
  1272. lock.lock();
  1273. ncnn::UnlockedPoolAllocator::fastFree(ptr);
  1274. lock.unlock();
  1275. }
  1276. int test_layer_oom_opt(const char* layer_type, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Option& _opt, const std::vector<ncnn::Mat>& a, int top_blob_count, int flag)
  1277. {
  1278. int typeindex = ncnn::layer_to_index(layer_type);
  1279. if (typeindex == -1)
  1280. return -1;
  1281. ncnn::Layer* op = ncnn::create_layer_cpu(typeindex);
  1282. if (!op->support_packing && _opt.use_packing_layout)
  1283. {
  1284. delete op;
  1285. return 233;
  1286. }
  1287. if (!op->support_bf16_storage && !op->support_fp16_storage && (_opt.use_bf16_storage || _opt.use_fp16_arithmetic))
  1288. {
  1289. delete op;
  1290. return 233;
  1291. }
  1292. op->load_param(pd);
  1293. if (op->one_blob_only && a.size() != 1)
  1294. {
  1295. fprintf(stderr, "layer with one_blob_only but consume multiple inputs\n");
  1296. delete op;
  1297. return -1;
  1298. }
  1299. ncnn::ModelBinFromMatArray mb(weights.data());
  1300. op->load_model(mb);
  1301. ncnn::Option opt = _opt;
  1302. opt.num_threads = 1;
  1303. opt.use_vulkan_compute = false;
  1304. op->create_pipeline(opt);
  1305. if (!op->support_packing && _opt.use_packing_layout)
  1306. {
  1307. op->destroy_pipeline(opt);
  1308. delete op;
  1309. return 233;
  1310. }
  1311. if (!op->support_bf16_storage && !op->support_fp16_storage && (_opt.use_bf16_storage || _opt.use_fp16_arithmetic))
  1312. {
  1313. op->destroy_pipeline(opt);
  1314. delete op;
  1315. return 233;
  1316. }
  1317. std::vector<ncnn::Mat> a4(a.size());
  1318. for (size_t i = 0; i < a4.size(); i++)
  1319. {
  1320. convert_to_optimal_layout(a[i], a4[i], opt, op, flag);
  1321. }
  1322. TestOOMAllocator test_oom_allocator;
  1323. opt.blob_allocator = &test_oom_allocator;
  1324. opt.workspace_allocator = &test_oom_allocator;
  1325. std::vector<ncnn::Mat> c;
  1326. c.resize(top_blob_count);
  1327. if (op->support_inplace)
  1328. {
  1329. for (size_t i = 0; i < a4.size(); i++)
  1330. {
  1331. c[i] = a4[i].clone();
  1332. }
  1333. op->forward_inplace(c, opt);
  1334. }
  1335. else
  1336. {
  1337. op->forward(a4, c, opt);
  1338. }
  1339. for (int i = 0; i < top_blob_count; i++)
  1340. {
  1341. c[i].release();
  1342. }
  1343. const int alloc_count = test_oom_allocator.counter;
  1344. for (int i = 0; i < alloc_count; i++)
  1345. {
  1346. test_oom_allocator.counter = 0;
  1347. test_oom_allocator.failid = i;
  1348. int ret = 0;
  1349. if (op->support_inplace)
  1350. {
  1351. for (size_t i = 0; i < a4.size(); i++)
  1352. {
  1353. c[i] = a4[i].clone();
  1354. }
  1355. ret = op->forward_inplace(c, opt);
  1356. }
  1357. else
  1358. {
  1359. ret = op->forward(a4, c, opt);
  1360. }
  1361. for (int i = 0; i < top_blob_count; i++)
  1362. {
  1363. c[i].release();
  1364. }
  1365. if (ret != -100)
  1366. {
  1367. fprintf(stderr, "oom not catched %d/%d\n", i, alloc_count);
  1368. op->destroy_pipeline(opt);
  1369. delete op;
  1370. return -1;
  1371. }
  1372. }
  1373. op->destroy_pipeline(opt);
  1374. delete op;
  1375. return 0;
  1376. }
  1377. int test_layer_oom_opt(const char* layer_type, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Option& _opt, const ncnn::Mat& a, int flag)
  1378. {
  1379. int typeindex = ncnn::layer_to_index(layer_type);
  1380. if (typeindex == -1)
  1381. return -1;
  1382. ncnn::Layer* op = ncnn::create_layer_cpu(typeindex);
  1383. if (!op->support_packing && _opt.use_packing_layout)
  1384. {
  1385. delete op;
  1386. return 233;
  1387. }
  1388. if (!op->support_bf16_storage && !op->support_fp16_storage && (_opt.use_bf16_storage || _opt.use_fp16_arithmetic))
  1389. {
  1390. delete op;
  1391. return 233;
  1392. }
  1393. op->load_param(pd);
  1394. ncnn::ModelBinFromMatArray mb(weights.data());
  1395. op->load_model(mb);
  1396. ncnn::Option opt = _opt;
  1397. opt.num_threads = 1;
  1398. opt.use_vulkan_compute = false;
  1399. op->create_pipeline(opt);
  1400. if (!op->support_packing && _opt.use_packing_layout)
  1401. {
  1402. op->destroy_pipeline(opt);
  1403. delete op;
  1404. return 233;
  1405. }
  1406. if (!op->support_bf16_storage && !op->support_fp16_storage && (_opt.use_bf16_storage || _opt.use_fp16_arithmetic))
  1407. {
  1408. op->destroy_pipeline(opt);
  1409. delete op;
  1410. return 233;
  1411. }
  1412. ncnn::Mat a4;
  1413. convert_to_optimal_layout(a, a4, opt, op, flag);
  1414. TestOOMAllocator test_oom_allocator;
  1415. opt.blob_allocator = &test_oom_allocator;
  1416. opt.workspace_allocator = &test_oom_allocator;
  1417. ncnn::Mat c;
  1418. if (op->support_inplace)
  1419. {
  1420. c = a4.clone();
  1421. op->forward_inplace(c, opt);
  1422. }
  1423. else
  1424. {
  1425. op->forward(a4, c, opt);
  1426. }
  1427. c.release();
  1428. const int alloc_count = test_oom_allocator.counter;
  1429. for (int i = 0; i < alloc_count; i++)
  1430. {
  1431. test_oom_allocator.counter = 0;
  1432. test_oom_allocator.failid = i;
  1433. int ret = 0;
  1434. if (op->support_inplace)
  1435. {
  1436. c = a4.clone();
  1437. ret = op->forward_inplace(c, opt);
  1438. }
  1439. else
  1440. {
  1441. ret = op->forward(a4, c, opt);
  1442. }
  1443. c.release();
  1444. if (ret != -100)
  1445. {
  1446. fprintf(stderr, "oom not catched %d/%d\n", i, alloc_count);
  1447. op->destroy_pipeline(opt);
  1448. delete op;
  1449. return -1;
  1450. }
  1451. }
  1452. op->destroy_pipeline(opt);
  1453. delete op;
  1454. return 0;
  1455. }
  1456. int test_layer_oom(const char* layer_type, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const std::vector<ncnn::Mat>& a, int top_blob_count, int flag)
  1457. {
  1458. // pack fp16p fp16s fp16a bf16s shader8 image
  1459. const int options[][7] = {
  1460. {0, 0, 0, 0, 0, 0, 0},
  1461. {0, 0, 1, 0, 0, 0, 0},
  1462. {0, 0, 1, 1, 1, 0, 0},
  1463. {1, 0, 0, 0, 0, 0, 0},
  1464. {1, 1, 0, 0, 1, 0, 0},
  1465. {1, 0, 1, 0, 0, 1, 0},
  1466. {1, 1, 1, 1, 0, 0, 0},
  1467. {1, 1, 1, 1, 1, 1, 1},
  1468. };
  1469. const int opt_count = sizeof(options) / sizeof(options[0]);
  1470. for (int i = 0; i < opt_count; i++)
  1471. {
  1472. ncnn::Option opt;
  1473. opt.num_threads = 1;
  1474. opt.use_packing_layout = options[i][0];
  1475. opt.use_fp16_packed = options[i][1];
  1476. opt.use_fp16_storage = options[i][2];
  1477. opt.use_fp16_arithmetic = options[i][3];
  1478. opt.use_bf16_storage = options[i][4];
  1479. opt.use_shader_pack8 = options[i][5];
  1480. opt.use_image_storage = options[i][6];
  1481. int ret = test_layer_oom_opt(layer_type, pd, weights, opt, a, top_blob_count, flag);
  1482. if (ret != 233 && ret != 0)
  1483. return ret;
  1484. }
  1485. return 0;
  1486. }
  1487. int test_layer_oom(const char* layer_type, const ncnn::ParamDict& pd, const std::vector<ncnn::Mat>& weights, const ncnn::Mat& a, int flag)
  1488. {
  1489. // pack fp16p fp16s fp16a bf16s shader8 image
  1490. const int options[][7] = {
  1491. {0, 0, 0, 0, 0, 0, 0},
  1492. {0, 0, 1, 0, 0, 0, 0},
  1493. {0, 0, 1, 1, 1, 0, 0},
  1494. {1, 0, 0, 0, 0, 0, 0},
  1495. {1, 1, 0, 0, 1, 0, 0},
  1496. {1, 0, 1, 0, 0, 1, 0},
  1497. {1, 1, 1, 1, 0, 0, 0},
  1498. {1, 1, 1, 1, 1, 1, 1},
  1499. };
  1500. const int opt_count = sizeof(options) / sizeof(options[0]);
  1501. for (int i = 0; i < opt_count; i++)
  1502. {
  1503. ncnn::Option opt;
  1504. opt.num_threads = 1;
  1505. opt.use_packing_layout = options[i][0];
  1506. opt.use_fp16_packed = options[i][1];
  1507. opt.use_fp16_storage = options[i][2];
  1508. opt.use_fp16_arithmetic = options[i][3];
  1509. opt.use_bf16_storage = options[i][4];
  1510. opt.use_shader_pack8 = options[i][5];
  1511. opt.use_image_storage = options[i][6];
  1512. int ret = test_layer_oom_opt(layer_type, pd, weights, opt, a, flag);
  1513. if (ret != 233 && ret != 0)
  1514. return ret;
  1515. }
  1516. return 0;
  1517. }