quantize.cpp 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. #include "ggml.h"
  2. #include "llama.h"
  3. #include <cstdio>
  4. #include <string>
  5. // usage:
  6. // ./llama-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 = 2 - q4_0\n");
  13. fprintf(stderr, " type = 3 - q4_1\n");
  14. return 1;
  15. }
  16. // needed to initialize f16 tables
  17. {
  18. struct ggml_init_params params = { 0, NULL };
  19. struct ggml_context * ctx = ggml_init(params);
  20. ggml_free(ctx);
  21. }
  22. const std::string fname_inp = argv[1];
  23. const std::string fname_out = argv[2];
  24. const int itype = atoi(argv[3]);
  25. const int64_t t_main_start_us = ggml_time_us();
  26. int64_t t_quantize_us = 0;
  27. // load the model
  28. {
  29. const int64_t t_start_us = ggml_time_us();
  30. if (llama_model_quantize(fname_inp.c_str(), fname_out.c_str(), itype)) {
  31. fprintf(stderr, "%s: failed to quantize model from '%s'\n", __func__, fname_inp.c_str());
  32. return 1;
  33. }
  34. t_quantize_us = ggml_time_us() - t_start_us;
  35. }
  36. // report timing
  37. {
  38. const int64_t t_main_end_us = ggml_time_us();
  39. printf("\n");
  40. printf("%s: quantize time = %8.2f ms\n", __func__, t_quantize_us/1000.0);
  41. printf("%s: total time = %8.2f ms\n", __func__, (t_main_end_us - t_main_start_us)/1000.0);
  42. }
  43. return 0;
  44. }