Elffuss BraidHub

kikocis/ncnn public

master · 5.1 KiBRenderedRawPermalink
1## current model load api
2### Cons
3#### long and awful code
4#### two functions
5#### deal float32 float16 quantized-u8
6#### deal alignment size
7```cpp
8#if NCNN_STDIO
9int Convolution::load_model(FILE* binfp)
10{
11 int nread;
12
13 union
14 {
15 struct
16 {
17 unsigned char f0;
18 unsigned char f1;
19 unsigned char f2;
20 unsigned char f3;
21 };
22 unsigned int tag;
23 } flag_struct;
24
25 nread = fread(&flag_struct, sizeof(flag_struct), 1, binfp);
26 if (nread != 1)
27 {
28 fprintf(stderr, "Convolution read flag_struct failed %d\n", nread);
29 return -1;
30 }
31
32 unsigned int flag = flag_struct.f0 + flag_struct.f1 + flag_struct.f2 + flag_struct.f3;
33
34 weight_data.create(weight_data_size);
35 if (weight_data.empty())
36 return -100;
37
38 if (flag_struct.tag == 0x01306B47)
39 {
40 // half-precision weight data
41 int align_weight_data_size = alignSize(weight_data_size * sizeof(unsigned short), 4);
42 std::vector<unsigned short> float16_weights;
43 float16_weights.resize(align_weight_data_size);
44 nread = fread(float16_weights.data(), align_weight_data_size, 1, binfp);
45 if (nread != 1)
46 {
47 fprintf(stderr, "Convolution read float16_weights failed %d\n", nread);
48 return -1;
49 }
50
51 weight_data = Mat::from_float16(float16_weights.data(), weight_data_size);
52 if (weight_data.empty())
53 return -100;
54 }
55 else if (flag != 0)
56 {
57 // quantized weight data
58 float quantization_value[256];
59 nread = fread(quantization_value, 256 * sizeof(float), 1, binfp);
60 if (nread != 1)
61 {
62 fprintf(stderr, "Convolution read quantization_value failed %d\n", nread);
63 return -1;
64 }
65
66 int align_weight_data_size = alignSize(weight_data_size * sizeof(unsigned char), 4);
67 std::vector<unsigned char> index_array;
68 index_array.resize(align_weight_data_size);
69 nread = fread(index_array.data(), align_weight_data_size, 1, binfp);
70 if (nread != 1)
71 {
72 fprintf(stderr, "Convolution read index_array failed %d\n", nread);
73 return -1;
74 }
75
76 float* weight_data_ptr = weight_data;
77 for (int i = 0; i < weight_data_size; i++)
78 {
79 weight_data_ptr[i] = quantization_value[ index_array[i] ];
80 }
81 }
82 else if (flag_struct.f0 == 0)
83 {
84 // raw weight data
85 nread = fread(weight_data, weight_data_size * sizeof(float), 1, binfp);
86 if (nread != 1)
87 {
88 fprintf(stderr, "Convolution read weight_data failed %d\n", nread);
89 return -1;
90 }
91 }
92
93 if (bias_term)
94 {
95 bias_data.create(num_output);
96 if (bias_data.empty())
97 return -100;
98 nread = fread(bias_data, num_output * sizeof(float), 1, binfp);
99 if (nread != 1)
100 {
101 fprintf(stderr, "Convolution read bias_data failed %d\n", nread);
102 return -1;
103 }
104 }
105
106 return 0;
107}
108#endif // NCNN_STDIO
109
110int Convolution::load_model(const unsigned char*& mem)
111{
112 union
113 {
114 struct
115 {
116 unsigned char f0;
117 unsigned char f1;
118 unsigned char f2;
119 unsigned char f3;
120 };
121 unsigned int tag;
122 } flag_struct;
123
124 memcpy(&flag_struct, mem, sizeof(flag_struct));
125 mem += sizeof(flag_struct);
126
127 unsigned int flag = flag_struct.f0 + flag_struct.f1 + flag_struct.f2 + flag_struct.f3;
128
129 if (flag_struct.tag == 0x01306B47)
130 {
131 // half-precision weight data
132 weight_data = Mat::from_float16((unsigned short*)mem, weight_data_size);
133 mem += alignSize(weight_data_size * sizeof(unsigned short), 4);
134 if (weight_data.empty())
135 return -100;
136 }
137 else if (flag != 0)
138 {
139 // quantized weight data
140 const float* quantization_value = (const float*)mem;
141 mem += 256 * sizeof(float);
142
143 const unsigned char* index_array = (const unsigned char*)mem;
144 mem += alignSize(weight_data_size * sizeof(unsigned char), 4);
145
146 weight_data.create(weight_data_size);
147 if (weight_data.empty())
148 return -100;
149 float* weight_data_ptr = weight_data;
150 for (int i = 0; i < weight_data_size; i++)
151 {
152 weight_data_ptr[i] = quantization_value[ index_array[i] ];
153 }
154 }
155 else if (flag_struct.f0 == 0)
156 {
157 // raw weight data
158 weight_data = Mat(weight_data_size, (float*)mem);
159 mem += weight_data_size * sizeof(float);
160 }
161
162 if (bias_term)
163 {
164 bias_data = Mat(num_output, (float*)mem);
165 mem += num_output * sizeof(float);
166 }
167
168 return 0;
169}
170```
171
172## new model load api proposed
173### Pros
174#### clean and simple api
175#### element type detection
176```cpp
177int Convolution::load_model(const ModelBin& mb)
178{
179 // auto detect element type
180 weight_data = mb.load(weight_data_size, 0);
181 if (weight_data.empty())
182 return -100;
183
184 if (bias_term)
185 {
186 // certain type specified
187 bias_data = mb.load(num_output, 1);
188 if (bias_data.empty())
189 return -100;
190 }
191
192 return 0;
193}
194```