Исходный код СУБД RuDB — свободной СУБД с открытым исходным кодом на базе PostgreSQL
rudb
3 days ago bd039d1e92813358c5c5a1e346b49074baf513f2
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
/*++
 
Copyright (c) Microsoft Corporation. All rights reserved.
 
Licensed under the MIT License.
 
Module Name:
 
    q4_dq_cli.cpp
 
Abstract:
 
    This module implements a command line tool that quantize fp32 into int4,
    or reverse this process..
 
--*/
 
#include "mlas_q4.h"
 
#include <cstring>
#include <fstream>
#include <iostream>
#include <algorithm>
#include <string>
#include <vector>
 
char*
getCmdOption(char** begin, char** end, const std::string& option)
{
    char** itr = std::find(begin, end, option);
    if (itr != end && ++itr != end) {
        return *itr;
    }
    return nullptr;
}
 
void
usage(const char* cli)
{
    std::cout << std::endl;
    std::cout << "This utility performs int4 quantize and dequantize of a matrix, usage: " << std::endl;
    std::cout << "    " << cli << " ACTION NUM_ROWS NUM_COLS [OPTIONS]" << std::endl;
    std::cout << "    ACTION:       can be either q (quantize) or dq (de-quantize)." << std::endl;
    std::cout << "    NUM_ROWS:     number of rows in the matrix." << std::endl;
    std::cout << "    NUM_COLS:     number of columns in the matrix." << std::endl;
    std::cout << "options:" << std::endl;
    std::cout << "    --quant_type {0, 1}." << std::endl;
    std::cout << "            Type of the block quantization." << std::endl;
    std::cout << "            0: Symmetric block quant, with fp32 scale." << std::endl;
    std::cout << "            1: (default) Block quant with fp32 scale and int8 zero-point." << std::endl;
    std::cout << "    --input_file {PATH}." << std::endl;
    std::cout << "            Path to the input file." << std::endl;
    std::cout << "    --input_offset {N}." << std::endl;
    std::cout << "            Skip the first N bytes when reading the input file." << std::endl;
    std::cout << "            Ignored when read from std in." << std::endl;
    std::cout << "    --output_file {PATH}." << std::endl;
    std::cout << "            Path to the output file. Write to std out when missing" << std::endl;
    std::cout << "    --output_format {txt,bin}" << std::endl;
    std::cout << "            txt: (default) text format: space separated numbers." << std::endl;
    std::cout << "            bin: Binary format, can not be output to std out." << std::endl;
    std::cout << std::endl;
}
 
 
//
// Variable for commands
//
struct Cli {
    bool   dqmode = false;  // false -> quantize, true -> dequantize
 
    size_t num_rows = 0;
    size_t num_cols = 0;
 
    MLAS_BLK_QUANT_TYPE quant_type = BlkQ4Zp8;
 
    char*  input_file = nullptr;
    size_t input_offset = 0;
 
    char*  output_file = nullptr;
    bool   output_bin = false;  // false -> csv, true -> binary
};
 
 
bool
parseArgs(int argc, char* argv[], Cli& cli)
{
    if (argc < 4) {
        return false;
    }
 
    if (strncmp(argv[1], "q", 2) == 0) {
        cli.dqmode = false;
    } else if (strncmp(argv[1], "dq", 3) == 0) {
        cli.dqmode = true;
    } else {
        return false;
    }
 
    errno = 0;
    cli.num_rows = (size_t)strtoul(argv[2], nullptr, 0);
    if (cli.num_rows == 0 || errno != 0) {
        return false;
    }
    cli.num_cols = (size_t)strtoul(argv[3], nullptr, 0);
    if (cli.num_cols == 0 || errno != 0) {
        return false;
    }
 
    char* quant_t = getCmdOption(argv + 4, argv + argc, "--quant_type");
    if (quant_t) {
        if (strncmp(quant_t, "0", 2) == 0) {
            cli.quant_type = BlkQ4Sym;
        }
    }
 
    cli.input_file = getCmdOption(argv + 4, argv + argc, "--input_file");
    char* offset_str = getCmdOption(argv + 4, argv + argc, "--input_offset");
    if (offset_str != nullptr) {
        errno = 0;
        cli.input_offset = (size_t)strtoul(offset_str, nullptr, 0);
        if (errno != 0) {
            return false;
        }
    }
 
    cli.output_file = getCmdOption(argv + 4, argv + argc, "--output_file");
    char* output_format_str = getCmdOption(argv + 4, argv + argc, "--output_format");
    if (output_format_str != nullptr) {
        if (strncmp(output_format_str, "csv", 4) == 0) {
            cli.output_bin = false;
        } else if (strncmp(output_format_str, "bin", 4) == 0) {
            cli.output_bin = true;
            if (!cli.output_file) {
                // can't dump binary file to std-out
                return false;
            }
        } else {
            return false;
        }
    }
    return true;
}
 
 
void
readBinFile(const char* filename, size_t start, size_t expected_size, std::vector<uint8_t>& buf)
{
    // open the file:
    std::streampos fileSize;
    std::ifstream file(filename, std::ios::binary);
 
    // get its size:
    file.seekg(0, std::ios::end);
    fileSize = file.tellg();
    file.seekg(0, std::ios::beg);
 
    file.seekg(start);
    fileSize -= start;
    if ((size_t)fileSize < expected_size) {
        return;
    }
 
    // read the data:
    buf.resize(expected_size);
    file.read((char*)buf.data(), expected_size);
}
 
 
void
writeUint8Txt(std::ostream& out, const uint8_t* data, size_t len)
{
    for (size_t i = 0; i < len; i++) {
        out << (int)data[i] << "  ";
        if (((i+1) % 21 == 0)) {
            out << std::endl;
        }
    }
    out << std::endl;
}
 
 
int
quantize(const Cli& cli)
{
    std::vector<uint8_t> srcbuf;
    readBinFile(cli.input_file, cli.input_offset, cli.num_rows * cli.num_cols * sizeof(float), srcbuf);
    if (srcbuf.size() == 0) {
        std::cerr << "Failed to read expected amount of data from file " << cli.input_file
                  << std::endl;
        return -1;
    }
 
    size_t qsize = MlasQ4GemmPackBSize(cli.quant_type, cli.num_cols, cli.num_rows);
    if (qsize == 0) {
        std::cerr << "Int4 Quantization not yet supported on this platform!";
        return -1;
    }
    std::vector<uint8_t> dstbuf(qsize);
    MlasQ4GemmPackB(cli.quant_type, dstbuf.data(), (const float*)srcbuf.data(), cli.num_cols,
                    cli.num_rows, cli.num_cols);
 
    if (cli.output_bin) {
        std::ofstream out(cli.output_file, std::ios::out | std::ios::binary);
        if (!out) {
            std::cerr << "Cannot open output file " << cli.output_file  << std::endl;
            return -1;
        }
        out.write((const char*)dstbuf.data(), dstbuf.size());
    } else {
        std::streambuf* buf;
        if (cli.output_file) {
            std::ofstream out(cli.output_file, std::ios::out);
            if (!out) {
                std::cerr << "Cannot open output file " << cli.output_file << std::endl;
                return -1;
            }
            buf = out.rdbuf();
        } else {
            buf = std::cout.rdbuf();
        }
#if defined(__GNUC__) && __GNUC__ >= 12
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored \
    "-Wdangling-pointer"  // TODO: suppress warning about dangling pointer until we have a fix
        std::ostream stream(buf);
#pragma GCC diagnostic pop
#else
        std::ostream stream(buf);
#endif
 
        writeUint8Txt(stream, dstbuf.data(), dstbuf.size());
    }
    return 0;
}
 
int
dequantize(const Cli& cli)
{
    size_t qsize = MlasQ4GemmPackBSize(cli.quant_type, cli.num_cols, cli.num_rows);
    if (qsize == 0) {
        std::cerr << "Int4 Quantization not yet supported on this platform!";
        return -1;
    }
    std::vector<uint8_t> srcbuf;
    readBinFile(cli.input_file, cli.input_offset, qsize, srcbuf);
    if (srcbuf.size() == 0) {
        std::cerr << "Failed to read expected amount of data from file " << cli.input_file
                  << std::endl;
        return -1;
    }
 
    std::vector<float> dstbuf(cli.num_rows * cli.num_cols);
    MlasQ4GemmUnPackB(cli.quant_type, dstbuf.data(), srcbuf.data(), cli.num_cols, cli.num_rows,
                      cli.num_cols);
 
    if (cli.output_bin) {
        std::ofstream out(cli.output_file, std::ios::out | std::ios::binary);
        if (!out) {
            std::cerr << "Cannot open output file " << cli.output_file << std::endl;
            return -1;
        }
        out.write((const char*)dstbuf.data(), std::streamsize(dstbuf.size()) * sizeof(float));
    } else {
        std::streambuf* buf;
        std::ofstream file_output_stream;
        if (cli.output_file) {
            file_output_stream.open(cli.output_file, std::ios::out);
            if (file_output_stream.fail()) {
                std::cerr << "Cannot open output file " << cli.output_file << std::endl;
                return -1;
            }
            buf = file_output_stream.rdbuf();
        } else {
            buf = std::cout.rdbuf();
        }
        std::ostream stream(buf);
        size_t lcount = 0;
        for (float v : dstbuf) {
            stream << v << "  ";
            if (++lcount >= 16) {
                stream << std::endl;
                lcount = 0;
            }
        }
        stream << std::endl;
    }
    return 0;
}
 
 
int
main(int argc, char* argv[])
{
    Cli cli;
    if (!parseArgs(argc, argv, cli)) {
        usage(argv[0]);
        return -1;
    }
    if (cli.dqmode) {
        return dequantize(cli);
    } else {
        return quantize(cli);
    }
}