You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

LLamaQuantizer.cs 5.9 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. using LLama.Native;
  2. using System;
  3. using System.Collections.Generic;
  4. namespace LLama
  5. {
  6. /// <summary>
  7. /// The quantizer to quantize the model.
  8. /// </summary>
  9. public static class LLamaQuantizer
  10. {
  11. /// <summary>
  12. /// Quantize the model.
  13. /// </summary>
  14. /// <param name="srcFileName">The model file to be quantized.</param>
  15. /// <param name="dstFilename">The path to save the quantized model.</param>
  16. /// <param name="ftype">The type of quantization.</param>
  17. /// <param name="nthread">Thread to be used during the quantization. By default it's the physical core number.</param>
  18. /// <param name="allowRequantize"></param>
  19. /// <param name="quantizeOutputTensor"></param>
  20. /// <returns>Whether the quantization is successful.</returns>
  21. /// <exception cref="ArgumentException"></exception>
  22. public static unsafe bool Quantize(string srcFileName, string dstFilename, LLamaFtype ftype, int nthread = -1, bool allowRequantize = true,
  23. bool quantizeOutputTensor = false)
  24. {
  25. if (!ValidateFtype(ftype))
  26. {
  27. throw new ArgumentException($"The type {Enum.GetName(typeof(LLamaFtype), ftype)} is not a valid type " +
  28. $"to perform quantization.");
  29. }
  30. var quantizeParams = NativeApi.llama_model_quantize_default_params();
  31. quantizeParams.ftype = ftype;
  32. quantizeParams.nthread = nthread;
  33. quantizeParams.allow_requantize = allowRequantize;
  34. quantizeParams.quantize_output_tensor = quantizeOutputTensor;
  35. return NativeApi.llama_model_quantize(srcFileName, dstFilename, &quantizeParams) == 0;
  36. }
  37. /// <summary>
  38. /// Quantize the model.
  39. /// </summary>
  40. /// <param name="srcFileName">The model file to be quantized.</param>
  41. /// <param name="dstFilename">The path to save the quantized model.</param>
  42. /// <param name="ftype">The type of quantization.</param>
  43. /// <param name="nthread">Thread to be used during the quantization. By default it's the physical core number.</param>
  44. /// <param name="allowRequantize"></param>
  45. /// <param name="quantizeOutputTensor"></param>
  46. /// <returns>Whether the quantization is successful.</returns>
  47. /// <exception cref="ArgumentException"></exception>
  48. public static bool Quantize(string srcFileName, string dstFilename, string ftype, int nthread = -1, bool allowRequantize = true,
  49. bool quantizeOutputTensor = false)
  50. {
  51. return Quantize(srcFileName, dstFilename, StringToFtype(ftype), nthread, allowRequantize, quantizeOutputTensor);
  52. }
  53. private static bool ValidateFtype(LLamaFtype ftype)
  54. {
  55. // Validation copies from here:
  56. // https://github.com/ggerganov/llama.cpp/blob/e59fcb2bc129881f4a269fee748fb38bce0a64de/llama.cpp#L2960
  57. switch (ftype)
  58. {
  59. case LLamaFtype.LLAMA_FTYPE_MOSTLY_Q4_0:
  60. case LLamaFtype.LLAMA_FTYPE_MOSTLY_Q4_1:
  61. case LLamaFtype.LLAMA_FTYPE_MOSTLY_Q5_0:
  62. case LLamaFtype.LLAMA_FTYPE_MOSTLY_Q5_1:
  63. case LLamaFtype.LLAMA_FTYPE_MOSTLY_Q8_0:
  64. case LLamaFtype.LLAMA_FTYPE_MOSTLY_F16:
  65. case LLamaFtype.LLAMA_FTYPE_ALL_F32:
  66. case LLamaFtype.LLAMA_FTYPE_MOSTLY_Q2_K:
  67. case LLamaFtype.LLAMA_FTYPE_MOSTLY_Q3_K_S:
  68. case LLamaFtype.LLAMA_FTYPE_MOSTLY_Q3_K_M:
  69. case LLamaFtype.LLAMA_FTYPE_MOSTLY_Q3_K_L:
  70. case LLamaFtype.LLAMA_FTYPE_MOSTLY_Q4_K_S:
  71. case LLamaFtype.LLAMA_FTYPE_MOSTLY_Q4_K_M:
  72. case LLamaFtype.LLAMA_FTYPE_MOSTLY_Q5_K_S:
  73. case LLamaFtype.LLAMA_FTYPE_MOSTLY_Q5_K_M:
  74. case LLamaFtype.LLAMA_FTYPE_MOSTLY_Q6_K:
  75. return true;
  76. case LLamaFtype.LLAMA_FTYPE_MOSTLY_Q4_1_SOME_F16:
  77. case LLamaFtype.LLAMA_FTYPE_GUESSED:
  78. default:
  79. return false;
  80. }
  81. }
  82. /// <summary>
  83. /// Parse a string into a LLamaFtype. This is a "relaxed" parsing, which allows any string which is contained within
  84. /// the enum name to be used.
  85. ///
  86. /// For example "Q5_K_M" will convert to "LLAMA_FTYPE_MOSTLY_Q5_K_M"
  87. /// </summary>
  88. /// <param name="str"></param>
  89. /// <returns></returns>
  90. /// <exception cref="ArgumentException"></exception>
  91. private static LLamaFtype StringToFtype(string str)
  92. {
  93. // Find all variants which contain the input string
  94. var matches = new List<LLamaFtype>();
  95. foreach (LLamaFtype ftype in Enum.GetValues(typeof(LLamaFtype)))
  96. {
  97. var name = Enum.GetName(typeof(LLamaFtype), ftype);
  98. // Note: this is using "IndexOf" instead of "Contains" to be compatible with netstandard2.0
  99. #pragma warning disable CA2249
  100. if (name != null && name.IndexOf(str, StringComparison.OrdinalIgnoreCase) >= 0)
  101. matches.Add(ftype);
  102. #pragma warning restore CA2249
  103. }
  104. // If there was just one match, success!
  105. if (matches.Count == 1)
  106. return matches[0];
  107. // If none matched throw a generic error
  108. if (matches.Count == 0)
  109. throw new ArgumentException($"Unknown ftype \"{str}\" for quantization.");
  110. // There were several matches, throw an error asking the user to be more specific
  111. throw new ArgumentException($"\"{str}\" matches multiple potential ftypes: {string.Join(",", matches)}");
  112. }
  113. }
  114. }