Elffuss BraidHub

kikocis/ncnn public

master · 2.9 KiBRenderedRawPermalink
1### what is packing and why
2
3packing is the form of storing multiple short-sized values as one long-sized value.
4
5element packing is well mapped with the underlying simd register, which usually use one very wide register to store different types of values.
6
7|C|elemsize|elempack|
8|---|---|---|
9|double|8|1|
10|float|4|1|
11|int|4|1|
12|short|2|1|
13|signed char|1|1|
14
15|arm neon|elemsize|elempack|
16|---|---|---|
17|float64x2_t|16|2|
18|float32x4_t|16|4|
19|int32x4_t|16|4|
20|float16x4_t|8|4|
21|int8x8_t|8|8|
22
23Though the real count of values doubles when elempack is two, the wide-sized value is still treated as one value in the view of Mat structure. For example, we want to store 40 float values in Mat object, if elempack 1 is used, Mat width is then 40, while 10 if elempack 4 is used.
24
25|dims|w|h|c|cstep|elemsize|elempack|
26|---|---|---|---|---|---|---|
27|1|40|1|1|40|4|1|
28|1|10|1|1|10|16|4|
29
30### packing style convention
31
32In practice, elempack 1, 4, 8 are the most common cases. It is possible to use any other packing style in theory.
33
34The following table show the packing axis used in ncnn for different dimension.
35
36|dims|packing axis|shape before packing|shape after packing|
37|---|---|---|---|
38|1|w|w|w/elempack|
39|2|h|w, h|w, h/elempack|
40|3|c|w, h, c|w, h, c/elempack|
41
42If the packing axis dim is not evenly divisible by elempack, zero padding may be used.
43
44```
45outw = (w + elempack - 1) / elempack;
46```
47
48The following snippet shows the memory layout after elempack=4 on 3-dim Mat
49
50```
51// w=2 h=3 c=4 elempack=1
520 1
532 3
544 5
55
566 7
578 9
5810 11
59
6012 13
6114 15
6216 17
63
6418 19
6520 21
6622 23
67
68// w=2 h=3 c=1 elempack=4
69(0,6,12,18) (1,7,13,19)
70(2,8,14,20) (3,9,15,21)
71(4,10,16,22) (5,11,17,23)
72```
73
74### how to convert elempack
75
76There is a convenient wrapper function provided
77```
78// convert to elempack 4 if packing axis dim is evenly divisible by elempack
79// return the identity Mat otherwise
80ncnn::Mat a;
81ncnn::Mat a_packed;
82ncnn::convert_packing(a, a_packed, 4);
83if (a_packed.elempack == 4)
84{
85 // check if packing is successful
86}
87
88// convert to packing 1, aka unpacking, shall be always successful
89ncnn::Mat b;
90ncnn::Mat b_unpacked;
91ncnn::convert_packing(b, b_unpacked, 1);
92```
93
94### handle general interleaved data
95
96Here is an example of using convert packing to convert RGB interleaved data to planar
97
98**NOTE:** The following code is just presented to explain what packing is and the conversion process. Do not use it in production due to its poor performance. Do use ncnn::Mat::from_pixels()
99
100```cpp
101// rgb_interleaved_u8 is RGB RGB RGB ...
102// rgb_interleaved_u8.w = w;
103// rgb_interleaved_u8.h = h;
104// rgb_interleaved_u8.c = 1;
105// rgb_interleaved_u8.elemsize = 3;
106// rgb_interleaved_u8.elempack = 3;
107
108ncnn::Mat rgb_interleaved_u8(w, h, 1, 3, 3);
109ncnn::Mat rgb_planar_u8;
110
111ncnn::convert_packing(rgb_interleaved_u8, rgb_planar_u8, 1);
112
113// rgb_planar_u8 is now RRR ... GGG ... BBB ...
114// rgb_planar_u8.w = w;
115// rgb_planar_u8.h = h;
116// rgb_planar_u8.c = 3;
117// rgb_planar_u8.elemsize = 1;
118// rgb_planar_u8.elempack = 1;
119```