ncnn

Форк
0
/
ncnnmerge.cpp 
196 строк · 5.2 Кб
1
// Tencent is pleased to support the open source community by making ncnn available.
2
//
3
// Copyright (C) 2020 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

15
#include <stdio.h>
16
#include <string.h>
17
#include <string>
18

19
static int copy_param(const char* parampath, FILE* outparamfp, int* total_layer_count, int* total_blob_count)
20
{
21
    // resolve model namespace from XYZ.param
22
    const char* lastslash = strrchr(parampath, '/');
23
    const char* name = lastslash == NULL ? parampath : lastslash + 1;
24
    const char* dot = strrchr(name, '.');
25
    std::string ns = dot ? std::string(name).substr(0, dot - name) : std::string(name);
26

27
    FILE* fp = fopen(parampath, "rb");
28

29
    if (!fp)
30
    {
31
        fprintf(stderr, "fopen %s failed\n", parampath);
32
        return -1;
33
    }
34

35
    int nscan = 0;
36
    int magic = 0;
37
    nscan = fscanf(fp, "%d", &magic);
38
    if (nscan != 1 || magic != 7767517)
39
    {
40
        fprintf(stderr, "read magic failed %d\n", nscan);
41
        return -1;
42
    }
43

44
    int layer_count = 0;
45
    int blob_count = 0;
46
    nscan = fscanf(fp, "%d %d", &layer_count, &blob_count);
47
    if (nscan != 2)
48
    {
49
        fprintf(stderr, "read layer_count and blob_count failed %d\n", nscan);
50
        return -1;
51
    }
52

53
    *total_layer_count += layer_count;
54
    *total_blob_count += blob_count;
55

56
    char line[1024];
57
    for (int i = 0; i < layer_count; i++)
58
    {
59
        char layer_type[33];
60
        char layer_name[257];
61
        int bottom_count = 0;
62
        int top_count = 0;
63
        nscan = fscanf(fp, "%32s %256s %d %d", layer_type, layer_name, &bottom_count, &top_count);
64
        if (nscan != 4)
65
        {
66
            fprintf(stderr, "read layer params failed %d\n", nscan);
67
            return -1;
68
        }
69

70
        fprintf(outparamfp, "%-24s %s/%-24s %d %d", layer_type, ns.c_str(), layer_name, bottom_count, top_count);
71

72
        for (int j = 0; j < bottom_count; j++)
73
        {
74
            char bottom_name[257];
75
            nscan = fscanf(fp, "%256s", bottom_name);
76
            if (nscan != 1)
77
            {
78
                fprintf(stderr, "read bottom_name failed %d\n", nscan);
79
                return -1;
80
            }
81

82
            fprintf(outparamfp, " %s/%s", ns.c_str(), bottom_name);
83
        }
84

85
        for (int j = 0; j < top_count; j++)
86
        {
87
            char top_name[257];
88
            nscan = fscanf(fp, "%256s", top_name);
89
            if (nscan != 1)
90
            {
91
                fprintf(stderr, "read top_name failed %d\n", nscan);
92
                return -1;
93
            }
94

95
            fprintf(outparamfp, " %s/%s", ns.c_str(), top_name);
96
        }
97

98
        // copy param dict string
99
        char* s = fgets(line, 1024, fp);
100
        if (!s)
101
        {
102
            fprintf(stderr, "read line %s failed\n", parampath);
103
            break;
104
        }
105

106
        fputs(line, outparamfp);
107
    }
108

109
    fclose(fp);
110

111
    return 0;
112
}
113

114
static int copy_bin(const char* binpath, FILE* outbinfp)
115
{
116
    FILE* fp = fopen(binpath, "rb");
117

118
    if (!fp)
119
    {
120
        fprintf(stderr, "fopen %s failed\n", binpath);
121
        return -1;
122
    }
123

124
    fseek(fp, 0, SEEK_END);
125
    int len = (int)ftell(fp);
126
    rewind(fp);
127

128
    char buffer[4096];
129
    int i = 0;
130
    for (; i + 4095 < len;)
131
    {
132
        size_t nread = fread(buffer, 1, 4096, fp);
133
        size_t nwrite = fwrite(buffer, 1, nread, outbinfp);
134
        i += (int)nwrite;
135
    }
136
    {
137
        size_t nread = fread(buffer, 1, len - i, fp);
138
        size_t nwrite = fwrite(buffer, 1, nread, outbinfp);
139
        i += (int)nwrite;
140
    }
141

142
    if (i != len)
143
    {
144
        fprintf(stderr, "copy %s incomplete\n", binpath);
145
    }
146

147
    fclose(fp);
148

149
    return 0;
150
}
151

152
int main(int argc, char** argv)
153
{
154
    if (argc < 7 || (argc - 1) % 2 != 0)
155
    {
156
        fprintf(stderr, "Usage: %s [param1] [bin1] [param2] [bin2] ... [outparam] [outbin]\n", argv[0]);
157
        return -1;
158
    }
159

160
    const char* outparampath = argv[argc - 2];
161
    const char* outbinpath = argv[argc - 1];
162

163
    FILE* outparamfp = fopen(outparampath, "wb");
164
    FILE* outbinfp = fopen(outbinpath, "wb");
165

166
    // magic
167
    fprintf(outparamfp, "7767517\n");
168

169
    // layer count and blob count placeholder
170
    // 99999 is large enough I think  --- nihui
171
    fprintf(outparamfp, "           \n");
172

173
    int total_layer_count = 0;
174
    int total_blob_count = 0;
175

176
    const int model_count = (argc - 3) / 2;
177

178
    for (int i = 0; i < model_count; i++)
179
    {
180
        const char* parampath = argv[i * 2 + 1];
181
        const char* binpath = argv[i * 2 + 2];
182

183
        copy_param(parampath, outparamfp, &total_layer_count, &total_blob_count);
184
        copy_bin(binpath, outbinfp);
185
    }
186

187
    // the real layer count and blob count
188
    rewind(outparamfp);
189
    fprintf(outparamfp, "7767517\n");
190
    fprintf(outparamfp, "%d %d", total_layer_count, total_blob_count);
191

192
    fclose(outparamfp);
193
    fclose(outbinfp);
194

195
    return 0;
196
}
197

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.