1
0

quantize.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. #include "ggml.h"
  2. #include "llama.h"
  3. #include <cstdio>
  4. #include <string>
  5. // usage:
  6. // ./quantize models/llama/ggml-model.bin models/llama/ggml-model-quant.bin type
  7. //
  8. int main(int argc, char ** argv) {
  9. ggml_time_init();
  10. if (argc != 4) {
  11. fprintf(stderr, "usage: %s model-f32.bin model-quant.bin type\n", argv[0]);
  12. fprintf(stderr, " type = %d - q4_0\n", LLAMA_FTYPE_MOSTLY_Q4_0);
  13. fprintf(stderr, " type = %d - q4_1\n", LLAMA_FTYPE_MOSTLY_Q4_1);
  14. fprintf(stderr, " type = %d - q4_2\n", LLAMA_FTYPE_MOSTLY_Q4_2);
  15. return 1;
  16. }
  17. // needed to initialize f16 tables
  18. {
  19. struct ggml_init_params params = { 0, NULL, false };
  20. struct ggml_context * ctx = ggml_init(params);
  21. ggml_free(ctx);
  22. }
  23. const std::string fname_inp = argv[1];
  24. const std::string fname_out = argv[2];
  25. const enum llama_ftype ftype = (enum llama_ftype)atoi(argv[3]);
  26. const int64_t t_main_start_us = ggml_time_us();
  27. int64_t t_quantize_us = 0;
  28. // load the model
  29. {
  30. const int64_t t_start_us = ggml_time_us();
  31. if (llama_model_quantize(fname_inp.c_str(), fname_out.c_str(), ftype)) {
  32. fprintf(stderr, "%s: failed to quantize model from '%s'\n", __func__, fname_inp.c_str());
  33. return 1;
  34. }
  35. t_quantize_us = ggml_time_us() - t_start_us;
  36. }
  37. // report timing
  38. {
  39. const int64_t t_main_end_us = ggml_time_us();
  40. printf("\n");
  41. printf("%s: quantize time = %8.2f ms\n", __func__, t_quantize_us/1000.0);
  42. printf("%s: total time = %8.2f ms\n", __func__, (t_main_end_us - t_main_start_us)/1000.0);
  43. }
  44. return 0;
  45. }