-
Notifications
You must be signed in to change notification settings - Fork 77
/
quantize.c
63 lines (48 loc) · 1.88 KB
/
quantize.c
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
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <ggml.h>
#include <rwkv.h>
#if defined(_WIN32)
bool QueryPerformanceFrequency(uint64_t* lpFrequency);
bool QueryPerformanceCounter(uint64_t* lpPerformanceCount);
#define time_t uint64_t
#define time_calibrate(freq) do { QueryPerformanceFrequency(&freq); freq /= 1000; } while (0)
#define time_measure(x) QueryPerformanceCounter(&x)
#define TIME_DIFF(freq, start, end) (double) ((end - start) / freq) / 1000.
#else
#include <time.h>
#define time_t struct timespec
#define time_calibrate(freq) (void) freq
#define time_measure(x) clock_gettime(CLOCK_MONOTONIC, &x)
#define TIME_DIFF(freq, start, end) (double) ((end.tv_nsec - start.tv_nsec) / 1000000) / 1000
#endif
static enum ggml_type type_from_string(const char * string) {
if (strcmp(string, "Q4_0") == 0) return GGML_TYPE_Q4_0;
if (strcmp(string, "Q4_1") == 0) return GGML_TYPE_Q4_1;
if (strcmp(string, "Q5_0") == 0) return GGML_TYPE_Q5_0;
if (strcmp(string, "Q5_1") == 0) return GGML_TYPE_Q5_1;
if (strcmp(string, "Q8_0") == 0) return GGML_TYPE_Q8_0;
return GGML_TYPE_COUNT;
}
int main(const int argc, const char * argv[]) {
if (argc != 4 || type_from_string(argv[3]) == GGML_TYPE_COUNT) {
fprintf(stderr, "Usage: %s INPUT_FILE OUTPUT_FILE FORMAT\n\nAvailable formats: Q4_0 Q4_1 Q5_0 Q5_1 Q8_0\n", argv[0]);
return EXIT_FAILURE;
}
time_t freq, start, end;
time_calibrate(freq);
fprintf(stderr, "Quantizing...\n");
time_measure(start);
bool success = rwkv_quantize_model_file(argv[1], argv[2], argv[3]);
time_measure(end);
double diff = TIME_DIFF(freq, start, end);
fprintf(stderr, "Took %.3f s\n", diff);
if (success) {
fprintf(stderr, "Success\n");
return EXIT_SUCCESS;
} else {
fprintf(stderr, "Error: 0x%.8X\n", rwkv_get_last_error(NULL));
return EXIT_FAILURE;
}
}