using LLama.Abstractions;
using System;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using LLama.Native;
namespace LLama.Common
{
///
/// The parameters for initializing a LLama model.
///
public record ModelParams
: ILLamaParams
{
///
public uint? ContextSize { get; set; }
///
public int MainGpu { get; set; } = 0;
///
public int GpuLayerCount { get; set; } = 20;
///
public uint Seed { get; set; } = 0xFFFFFFFF;
///
public bool UseFp16Memory { get; set; } = true;
///
public bool UseMemorymap { get; set; } = true;
///
public bool UseMemoryLock { get; set; }
///
public bool Perplexity { get; set; }
///
public string ModelPath { get; set; }
///
public AdapterCollection LoraAdapters { get; set; } = new();
///
public string LoraBase { get; set; } = string.Empty;
///
public uint? Threads { get; set; }
///
public uint? BatchThreads { get; set; }
///
public uint BatchSize { get; set; } = 512;
///
public bool EmbeddingMode { get; set; }
///
[JsonConverter(typeof(TensorSplitsCollectionConverter))]
public TensorSplitsCollection TensorSplits { get; set; } = new();
///
public float? RopeFrequencyBase { get; set; }
///
public float? RopeFrequencyScale { get; set; }
///
public float? YarnExtrapolationFactor { get; set; }
///
public float? YarnAttentionFactor { get; set; }
///
public float? YarnBetaFast { get; set; }
///
public float? YarnBetaSlow { get; set; }
///
public uint? YarnOriginalContext { get; set; }
///
public RopeScalingType? YarnScalingType { get; set; }
///
public bool MulMatQ { get; set; }
///
public bool VocabOnly { get; set; }
///
[JsonConverter(typeof(EncodingConverter))]
public Encoding Encoding { get; set; } = Encoding.UTF8;
///
///
///
/// The model path.
[JsonConstructor]
public ModelParams(string modelPath)
{
ModelPath = modelPath;
}
private ModelParams()
{
// This constructor (default parameterless constructor) is used by Newtonsoft to deserialize!
ModelPath = "";
}
}
internal class EncodingConverter
: JsonConverter
{
public override Encoding? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var name = reader.GetString();
if (name == null)
return null;
return Encoding.GetEncoding(name);
}
public override void Write(Utf8JsonWriter writer, Encoding value, JsonSerializerOptions options)
{
writer.WriteStringValue(value.WebName);
}
}
internal class TensorSplitsCollectionConverter
: JsonConverter
{
public override TensorSplitsCollection? Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
var arr = JsonSerializer.Deserialize(ref reader, options) ?? Array.Empty();
return new TensorSplitsCollection(arr);
}
public override void Write(Utf8JsonWriter writer, TensorSplitsCollection value, JsonSerializerOptions options)
{
JsonSerializer.Serialize(writer, value.Splits, options);
}
}
}