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 3.7 kB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. using LLama.Native;
  2. using System;
  3. using System.Collections.Generic;
  4. using System.Linq;
  5. using System.Text;
  6. namespace LLama
  7. {
  8. /// <summary>
  9. /// The quantizer to quantize the model.
  10. /// </summary>
  11. public static class LLamaQuantizer
  12. {
  13. /// <summary>
  14. /// Quantize the model.
  15. /// </summary>
  16. /// <param name="srcFileName">The model file to be quantized.</param>
  17. /// <param name="dstFilename">The path to save the quantized model.</param>
  18. /// <param name="ftype">The type of quantization.</param>
  19. /// <param name="nthread">Thread to be used during the quantization. By default it's the physical core number.</param>
  20. /// <returns>Whether the quantization is successful.</returns>
  21. /// <exception cref="ArgumentException"></exception>
  22. public static bool Quantize(string srcFileName, string dstFilename, LLamaFtype ftype, int nthread = -1)
  23. {
  24. if (!ValidateFtype(ftype))
  25. {
  26. throw new ArgumentException($"The type {Enum.GetName(typeof(LLamaFtype), ftype)} is not a valid type " +
  27. $"to perform quantization.");
  28. }
  29. return NativeApi.llama_model_quantize(srcFileName, dstFilename, ftype, nthread) == 0;
  30. }
  31. /// <summary>
  32. /// Quantize the model.
  33. /// </summary>
  34. /// <param name="srcFileName">The model file to be quantized.</param>
  35. /// <param name="dstFilename">The path to save the quantized model.</param>
  36. /// <param name="ftype">The type of quantization.</param>
  37. /// <param name="nthread">Thread to be used during the quantization. By default it's the physical core number.</param>
  38. /// <returns>Whether the quantization is successful.</returns>
  39. /// <exception cref="ArgumentException"></exception>
  40. public static bool Quantize(string srcFileName, string dstFilename, string ftype, int nthread = -1)
  41. {
  42. return Quantize(srcFileName, dstFilename, StringToFtype(ftype), nthread);
  43. }
  44. private static bool ValidateFtype(string ftype)
  45. {
  46. return new string[] { "q4_0", "q4_1", "q5_0", "q5_1", "q8_0" }.Contains(ftype);
  47. }
  48. private static bool ValidateFtype(LLamaFtype ftype)
  49. {
  50. return ftype is LLamaFtype.LLAMA_FTYPE_MOSTLY_Q4_0 or LLamaFtype.LLAMA_FTYPE_MOSTLY_Q4_1
  51. or LLamaFtype.LLAMA_FTYPE_MOSTLY_Q5_0 or LLamaFtype.LLAMA_FTYPE_MOSTLY_Q5_1 or LLamaFtype.LLAMA_FTYPE_MOSTLY_Q8_0;
  52. }
  53. private static string FtypeToString(LLamaFtype ftype)
  54. {
  55. return ftype switch
  56. {
  57. LLamaFtype.LLAMA_FTYPE_MOSTLY_Q4_0 => "q4_0",
  58. LLamaFtype.LLAMA_FTYPE_MOSTLY_Q4_1 => "q4_1",
  59. LLamaFtype.LLAMA_FTYPE_MOSTLY_Q5_0 => "q5_0",
  60. LLamaFtype.LLAMA_FTYPE_MOSTLY_Q5_1 => "q5_1",
  61. LLamaFtype.LLAMA_FTYPE_MOSTLY_Q8_0 => "q8_0",
  62. _ => throw new ArgumentException($"The type {Enum.GetName(typeof(LLamaFtype), ftype)} is not a valid type " +
  63. $"to perform quantization.")
  64. };
  65. }
  66. private static LLamaFtype StringToFtype(string str)
  67. {
  68. return str switch
  69. {
  70. "q4_0" => LLamaFtype.LLAMA_FTYPE_MOSTLY_Q4_0,
  71. "q4_1" => LLamaFtype.LLAMA_FTYPE_MOSTLY_Q4_1,
  72. "q5_0" => LLamaFtype.LLAMA_FTYPE_MOSTLY_Q5_0,
  73. "q5_1" => LLamaFtype.LLAMA_FTYPE_MOSTLY_Q5_1,
  74. "q8_0" => LLamaFtype.LLAMA_FTYPE_MOSTLY_Q8_0,
  75. _ => throw new ArgumentException($"Invalid ftype {str} to quantize.")
  76. };
  77. }
  78. }
  79. }

C#/.NET上易用的LLM高性能推理框架,支持LLaMA和LLaVA系列模型。