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.

ModelParams.cs 4.2 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. using LLama.Abstractions;
  2. using System;
  3. using System.Text;
  4. using System.Text.Json;
  5. using System.Text.Json.Serialization;
  6. using LLama.Native;
  7. namespace LLama.Common
  8. {
  9. /// <summary>
  10. /// The parameters for initializing a LLama model.
  11. /// </summary>
  12. public record ModelParams
  13. : ILLamaParams
  14. {
  15. /// <inheritdoc />
  16. public uint? ContextSize { get; set; }
  17. /// <inheritdoc />
  18. public int MainGpu { get; set; } = 0;
  19. /// <inheritdoc />
  20. public int GpuLayerCount { get; set; } = 20;
  21. /// <inheritdoc />
  22. public uint Seed { get; set; } = 0xFFFFFFFF;
  23. /// <inheritdoc />
  24. public bool UseFp16Memory { get; set; } = true;
  25. /// <inheritdoc />
  26. public bool UseMemorymap { get; set; } = true;
  27. /// <inheritdoc />
  28. public bool UseMemoryLock { get; set; }
  29. /// <inheritdoc />
  30. public bool Perplexity { get; set; }
  31. /// <inheritdoc />
  32. public string ModelPath { get; set; }
  33. /// <inheritdoc />
  34. public AdapterCollection LoraAdapters { get; set; } = new();
  35. /// <inheritdoc />
  36. public string LoraBase { get; set; } = string.Empty;
  37. /// <inheritdoc />
  38. public uint? Threads { get; set; }
  39. /// <inheritdoc />
  40. public uint? BatchThreads { get; set; }
  41. /// <inheritdoc />
  42. public uint BatchSize { get; set; } = 512;
  43. /// <inheritdoc />
  44. public bool EmbeddingMode { get; set; }
  45. /// <inheritdoc />
  46. [JsonConverter(typeof(TensorSplitsCollectionConverter))]
  47. public TensorSplitsCollection TensorSplits { get; set; } = new();
  48. /// <inheritdoc />
  49. public float? RopeFrequencyBase { get; set; }
  50. /// <inheritdoc />
  51. public float? RopeFrequencyScale { get; set; }
  52. /// <inheritdoc />
  53. public float? YarnExtrapolationFactor { get; set; }
  54. /// <inheritdoc />
  55. public float? YarnAttentionFactor { get; set; }
  56. /// <inheritdoc />
  57. public float? YarnBetaFast { get; set; }
  58. /// <inheritdoc />
  59. public float? YarnBetaSlow { get; set; }
  60. /// <inheritdoc />
  61. public uint? YarnOriginalContext { get; set; }
  62. /// <inheritdoc />
  63. public RopeScalingType? YarnScalingType { get; set; }
  64. /// <inheritdoc />
  65. public bool MulMatQ { get; set; }
  66. /// <inheritdoc />
  67. public bool VocabOnly { get; set; }
  68. /// <inheritdoc />
  69. [JsonConverter(typeof(EncodingConverter))]
  70. public Encoding Encoding { get; set; } = Encoding.UTF8;
  71. /// <summary>
  72. ///
  73. /// </summary>
  74. /// <param name="modelPath">The model path.</param>
  75. [JsonConstructor]
  76. public ModelParams(string modelPath)
  77. {
  78. ModelPath = modelPath;
  79. }
  80. private ModelParams()
  81. {
  82. // This constructor (default parameterless constructor) is used by Newtonsoft to deserialize!
  83. ModelPath = "";
  84. }
  85. }
  86. internal class EncodingConverter
  87. : JsonConverter<Encoding>
  88. {
  89. public override Encoding? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
  90. {
  91. var name = reader.GetString();
  92. if (name == null)
  93. return null;
  94. return Encoding.GetEncoding(name);
  95. }
  96. public override void Write(Utf8JsonWriter writer, Encoding value, JsonSerializerOptions options)
  97. {
  98. writer.WriteStringValue(value.WebName);
  99. }
  100. }
  101. internal class TensorSplitsCollectionConverter
  102. : JsonConverter<TensorSplitsCollection>
  103. {
  104. public override TensorSplitsCollection? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
  105. {
  106. var arr = JsonSerializer.Deserialize<float[]>(ref reader, options) ?? Array.Empty<float>();
  107. return new TensorSplitsCollection(arr);
  108. }
  109. public override void Write(Utf8JsonWriter writer, TensorSplitsCollection value, JsonSerializerOptions options)
  110. {
  111. JsonSerializer.Serialize(writer, value.Splits, options);
  112. }
  113. }
  114. }