| 885 | } |
| 886 | |
| 887 | bool clip_model_quantize(const char * fname_inp, const char * fname_out, const int itype) { |
| 888 | |
| 889 | ggml_type type = GGML_TYPE_Q4_1; |
| 890 | |
| 891 | switch (itype) { |
| 892 | case 2: |
| 893 | type = GGML_TYPE_Q4_0; |
| 894 | break; |
| 895 | case 3: |
| 896 | type = GGML_TYPE_Q4_1; |
| 897 | break; |
| 898 | case 6: |
| 899 | type = GGML_TYPE_Q5_0; |
| 900 | break; |
| 901 | case 7: |
| 902 | type = GGML_TYPE_Q5_1; |
| 903 | break; |
| 904 | case 8: |
| 905 | type = GGML_TYPE_Q8_0; |
| 906 | break; |
| 907 | default: |
| 908 | fprintf(stderr, "%s: invalid quantization type %d\n", __func__, itype); |
| 909 | return false; |
| 910 | }; |
| 911 | |
| 912 | auto ctx_clip = clip_model_load(fname_inp, 2); |
| 913 | const auto & ctx_src = ctx_clip->ctx_gguf; |
| 914 | const auto & ctx_data = ctx_clip->ctx; |
| 915 | |
| 916 | auto ctx_out = gguf_init_empty(); |
| 917 | gguf_set_kv(ctx_out, ctx_src); |
| 918 | gguf_set_val_u32(ctx_out, "general.quantization_version", GGML_QNT_VERSION); |
| 919 | gguf_set_val_u32(ctx_out, "general.file_type", itype); |
| 920 | |
| 921 | auto fout = std::ofstream(fname_out, std::ios::binary); |
| 922 | |
| 923 | const int n_tensors = gguf_get_n_tensors(ctx_src); |
| 924 | |
| 925 | for (int i = 0; i < n_tensors; ++i) { |
| 926 | const char * name = gguf_get_tensor_name(ctx_src, i); |
| 927 | struct ggml_tensor * cur = ggml_get_tensor(ctx_data, name); |
| 928 | gguf_add_tensor(ctx_out, cur); |
| 929 | } |
| 930 | |
| 931 | const size_t meta_size = gguf_get_meta_size(ctx_out); |
| 932 | for (size_t i = 0; i < meta_size; ++i) { |
| 933 | fout.put(0); |
| 934 | } |
| 935 | |
| 936 | // regexes of tensor names to be quantized |
| 937 | const std::vector<std::string> k_names = { |
| 938 | ".*weight", |
| 939 | }; |
| 940 | |
| 941 | std::vector<uint8_t> read_data(512); |
| 942 | std::vector<uint8_t> work(512); |
| 943 | std::vector<float> conv_buf(512); |
| 944 | std::vector<int64_t> hist_all(1 << 4, 0); |
nothing calls this directly
no test coverage detected