Elffuss BraidHub

kikocis/ncnn public

master · 4.1 KiBRenderedRawPermalink
1# layer feature mask
2
3Each ncnn layer allows a special parameter pair `31=X` to control specific bahavior.
4
5X is an unsigned integer with each bit contributing a feature mask.
6
7We usually use it to configuring fine-graded behaviors for certain layers to maintain accuracy, reduce memory usage or optimize performance.
8
9|bit|value|mask|rationale|
10|---|---|---|---|
11|1<<0|1|no fp16 arithmetic|precision concern|
12|1<<1|2|no fp16 storage|precision concern|
13|1<<2|4|no bf16 storage|precision concern|
14|1<<3|8|no int8|debug dynamic quantized model|
15|1<<4|16|no vulkan|reduce overhead for cpu op - gpu split - cpu op|
16|1<<5|32|no sgemm|reduce some memory|
17|1<<6|64|no winograd|reduce some memory|
18|1<<7|128|no threading|force single thread|
19
20These bits can be OR-combined into one value to control multiple behaviors simultaneously.
21
22For example, `31=17` means disabling both vulkan and fp16 arithmetic.
23
24## disable fp16 for certain layer to fix overflow
25
26```ruby
277767517
283 3
29Input input 0 1 input0 0=22 1=22 2=32
30Convolution conv0 1 1 input0 conv0 0=32 1=1 6=1024 9=1
31Convolution conv1 1 1 conv0 conv1 0=128 1=3 6=36864 9=1
32```
33
34Typically, we use fp16 computation to improve inference speed.
35However, since the weight value of `conv1` is very large, fp16 accumulation may cause numerical overflow, so fp16 needs to be disabled individually for `conv1`, while other layers continue to use fp16 mode
36
37Add `31=3` to disable fp16 storage and arithmetic.
38
39```ruby
407767517
413 3
42Input input 0 1 input0 0=22 1=22 2=32
43Convolution conv0 1 1 input0 conv0 0=32 1=1 6=1024 9=1
44Convolution conv1 1 1 conv0 conv1 0=128 1=3 6=36864 9=1 31=3
45```
46
47## disable vulkan for certain layer to improve performance
48
49```ruby
507767517
515 5
52Input input 0 1 input0 0=22 1=22 2=32
53Convolution conv0 1 1 input0 conv0 0=32 1=1 6=1024 9=1
54SomeCPULayer c0 1 1 conv0 c0 0=32
55ReLU relu0 1 1 c0 relu0
56SomeCPULayer c1 1 1 relu0 c1 0=32
57```
58
59Between the CPU layers, there is a simple calculation layer that supports vulkan. We can set `31=16` to force it to run on CPU. This can avoid the overhead of data upload, download and storage layout conversion between CPU and GPU. After all, CPU is fast enough for simple operations.
60
61```ruby
627767517
635 5
64Input input 0 1 input0 0=22 1=22 2=32
65Convolution conv0 1 1 input0 conv0 0=32 1=1 6=1024 9=1
66SomeCPULayer c0 1 1 conv0 c0 0=32
67ReLU relu0 1 1 c0 relu0 31=16
68SomeCPULayer c1 1 1 relu0 c1 0=32
69```
70
71## disable winograd for certain layer to reduce memory usage
72
73```ruby
747767517
753 3
76Input input 0 1 input0 0=22 1=22 2=32
77Convolution conv0 1 1 input0 conv0 0=32 1=1 6=1024 9=1
78Convolution conv1 1 1 conv0 conv1 0=128 1=3 6=36864 9=1
79```
80
81The winograd technology uses more memory for the purpose of improving convolution performance, but this is not always true. In some memory-constrained situations, or memory IO bottlenecks, we can disable the use of winograd on some layers in exchange for a smaller memory footprint. Add `31=64` to Convolution layer, which forces it to use implcit-gemm or tiled im2col-gemm implementation, reducing memory usage and sometimes improving vulkan performance.
82
83```ruby
847767517
853 3
86Input input 0 1 input0 0=22 1=22 2=32
87Convolution conv0 1 1 input0 conv0 0=32 1=1 6=1024 9=1
88Convolution conv1 1 1 conv0 conv1 0=128 1=3 6=36864 9=1 31=64
89```
90
91## disable threading for certain layer to improve performance
92
93```ruby
947767517
954 4
96Input input 0 1 input0 0=22 1=22 2=3
97Convolution conv0 1 1 input0 conv0 0=16 1=3 6=432
98HardSigmoid hs 1 1 conv0 hs0
99Convolution conv1 1 1 hs0 conv1 0=16 1=3 6=2304
100```
101
102The overhead of multi-thread dispatch and merging is too large for small tensors. Add `31=128` to HardSigmoid layer, which forces it to execute in a single thread, reducing power consumption and improving performance.
103
104```ruby
1057767517
1064 4
107Input input 0 1 input0 0=22 1=22 2=3
108Convolution conv0 1 1 input0 conv0 0=16 1=3 6=432
109HardSigmoid hs 1 1 conv0 hs0 31=128
110Convolution conv1 1 1 hs0 conv1 0=16 1=3 6=2304
111```