File size: 1,580 Bytes
00d4f8c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
// Dump one ggml-dequantized row so the NumPy reference can be checked against
// ggml's own dequantizers. usage: ggml_deq <type> <file> <offset> <n>
#include "ggml.h"
#include "ggml-quants.h"

#include <cstdio>
#include <cstdlib>

int main(int argc, char ** argv) {
    if (argc < 5) {
        fprintf(stderr, "usage: %s <type> <file> <offset> <n>\n", argv[0]);
        return 1;
    }
    const int   tt  = atoi(argv[1]);
    const char * path = argv[2];
    const long long off = atoll(argv[3]);
    const int   n   = atoi(argv[4]);

    FILE * f = fopen(path, "rb");
    if (!f) { perror("open"); return 1; }
    const size_t ts = ggml_row_size((ggml_type) tt, n);
    void * buf = malloc(ts);
    if (fseek(f, (long) off, SEEK_SET) != 0) { fprintf(stderr, "seek\n"); return 1; }
    if (fread(buf, 1, ts, f) != ts) { fprintf(stderr, "short read\n"); return 1; }
    fclose(f);

    float * y = (float *) malloc(sizeof(float) * n);
    switch (tt) {
        case 11: dequantize_row_q3_K((const block_q3_K *) buf, y, n); break;
        case 12: dequantize_row_q4_K((const block_q4_K *) buf, y, n); break;
        case 13: dequantize_row_q5_K((const block_q5_K *) buf, y, n); break;
        case 14: dequantize_row_q6_K((const block_q6_K *) buf, y, n); break;
        case  7: dequantize_row_q5_1((const block_q5_1 *) buf, y, n); break;
        case  8: dequantize_row_q8_0((const block_q8_0 *) buf, y, n); break;
        default: fprintf(stderr, "type %d not handled\n", tt); return 1;
    }
    fwrite(y, sizeof(float), n, stdout);
    free(y);
    free(buf);
    return 0;
}