MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / clip_model_quantize

Function clip_model_quantize

examples/llava/clip.cpp:887–1070  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

885}
886
887bool 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);

Callers

nothing calls this directly

Calls 15

fprintfFunction · 0.85
clip_model_loadFunction · 0.85
printfFunction · 0.85
ggml_quantize_q4_0Function · 0.85
ggml_quantize_q4_1Function · 0.85
ggml_quantize_q5_0Function · 0.85
ggml_quantize_q5_1Function · 0.85
ggml_quantize_q8_0Function · 0.85
putMethod · 0.80
clip_freeFunction · 0.70
gguf_init_emptyFunction · 0.50
gguf_set_kvFunction · 0.50

Tested by

no test coverage detected