Browse Source

Converted ModelParams into a `record` class. This has several advantages:

- Equality, hashing etc all implemented automatically
 - Default values are defined in just one place (the properties) instead of the constructor as well
 - Added test to ensure that serialization works properly
tags/v0.5.1
Martin Evans 2 years ago
parent
commit
29df14cd9c
13 changed files with 125 additions and 16 deletions
  1. +6
    -1
      LLama.Examples/NewVersion/ChatSessionStripRoleName.cs
  2. +6
    -1
      LLama.Examples/NewVersion/ChatSessionWithRoleName.cs
  3. +6
    -1
      LLama.Examples/NewVersion/InstructModeExecute.cs
  4. +6
    -1
      LLama.Examples/NewVersion/InteractiveModeExecute.cs
  5. +7
    -2
      LLama.Examples/NewVersion/LoadAndSaveSession.cs
  6. +6
    -1
      LLama.Examples/NewVersion/LoadAndSaveState.cs
  7. +6
    -1
      LLama.Examples/NewVersion/StatelessModeExecute.cs
  8. +4
    -1
      LLama.Unittest/BasicTest.cs
  9. +4
    -1
      LLama.Unittest/GrammarTest.cs
  10. +45
    -0
      LLama.Unittest/ModelsParamsTests.cs
  11. +4
    -1
      LLama.WebAPI/Services/StatefulChatService.cs
  12. +4
    -1
      LLama.WebAPI/Services/StatelessChatService.cs
  13. +21
    -4
      LLama/Common/ModelParams.cs

+ 6
- 1
LLama.Examples/NewVersion/ChatSessionStripRoleName.cs View File

@@ -10,7 +10,12 @@ namespace LLama.Examples.NewVersion
var modelPath = Console.ReadLine();
var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim();

var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5);
var parameters = new ModelParams(modelPath)
{
ContextSize = 1024,
Seed = 1337,
GpuLayerCount = 5
};
using var model = LLamaWeights.LoadFromFile(parameters);
using var context = model.CreateContext(parameters);
var executor = new InteractiveExecutor(context);


+ 6
- 1
LLama.Examples/NewVersion/ChatSessionWithRoleName.cs View File

@@ -10,7 +10,12 @@ namespace LLama.Examples.NewVersion
var modelPath = Console.ReadLine();
var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim();

var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5);
var parameters = new ModelParams(modelPath)
{
ContextSize = 1024,
Seed = 1337,
GpuLayerCount = 5
};
using var model = LLamaWeights.LoadFromFile(parameters);
using var context = model.CreateContext(parameters);
var executor = new InteractiveExecutor(context);


+ 6
- 1
LLama.Examples/NewVersion/InstructModeExecute.cs View File

@@ -10,7 +10,12 @@ namespace LLama.Examples.NewVersion
var modelPath = Console.ReadLine();
var prompt = File.ReadAllText("Assets/dan.txt").Trim();

var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5);
var parameters = new ModelParams(modelPath)
{
ContextSize = 1024,
Seed = 1337,
GpuLayerCount = 5
};
using var model = LLamaWeights.LoadFromFile(parameters);
using var context = model.CreateContext(parameters);
var executor = new InstructExecutor(context);


+ 6
- 1
LLama.Examples/NewVersion/InteractiveModeExecute.cs View File

@@ -10,7 +10,12 @@ namespace LLama.Examples.NewVersion
var modelPath = Console.ReadLine();
var prompt = (await File.ReadAllTextAsync("Assets/chat-with-bob.txt")).Trim();

var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5);
var parameters = new ModelParams(modelPath)
{
ContextSize = 1024,
Seed = 1337,
GpuLayerCount = 5
};
using var model = LLamaWeights.LoadFromFile(parameters);
using var context = model.CreateContext(parameters);
var ex = new InteractiveExecutor(context);


+ 7
- 2
LLama.Examples/NewVersion/LoadAndSaveSession.cs View File

@@ -10,7 +10,12 @@ namespace LLama.Examples.NewVersion
var modelPath = Console.ReadLine();
var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim();

var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5);
var parameters = new ModelParams(modelPath)
{
ContextSize = 1024,
Seed = 1337,
GpuLayerCount = 5
};
using var model = LLamaWeights.LoadFromFile(parameters);
using var context = model.CreateContext(parameters);
var ex = new InteractiveExecutor(context);
@@ -45,7 +50,7 @@ namespace LLama.Examples.NewVersion
Console.ForegroundColor = ConsoleColor.White;

ex.Context.Dispose();
ex = new(new LLamaContext(new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5)));
ex = new(new LLamaContext(parameters));
session = new ChatSession(ex);
session.LoadSession(statePath);



+ 6
- 1
LLama.Examples/NewVersion/LoadAndSaveState.cs View File

@@ -10,7 +10,12 @@ namespace LLama.Examples.NewVersion
var modelPath = Console.ReadLine();
var prompt = File.ReadAllText("Assets/chat-with-bob.txt").Trim();

var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5);
var parameters = new ModelParams(modelPath)
{
ContextSize = 1024,
Seed = 1337,
GpuLayerCount = 5
};
using var model = LLamaWeights.LoadFromFile(parameters);
using var context = model.CreateContext(parameters);
var ex = new InteractiveExecutor(context);


+ 6
- 1
LLama.Examples/NewVersion/StatelessModeExecute.cs View File

@@ -9,7 +9,12 @@ namespace LLama.Examples.NewVersion
Console.Write("Please input your model path: ");
var modelPath = Console.ReadLine();

var parameters = new ModelParams(modelPath, contextSize: 1024, seed: 1337, gpuLayerCount: 5);
var parameters = new ModelParams(modelPath)
{
ContextSize = 1024,
Seed = 1337,
GpuLayerCount = 5
};
using var model = LLamaWeights.LoadFromFile(parameters);
var ex = new StatelessExecutor(model, parameters);



+ 4
- 1
LLama.Unittest/BasicTest.cs View File

@@ -10,7 +10,10 @@ namespace LLama.Unittest

public BasicTest()
{
_params = new ModelParams("Models/llama-2-7b-chat.ggmlv3.q3_K_S.bin", contextSize: 2048);
_params = new ModelParams("Models/llama-2-7b-chat.ggmlv3.q3_K_S.bin")
{
ContextSize = 2048
};
_model = LLamaWeights.LoadFromFile(_params);
}



+ 4
- 1
LLama.Unittest/GrammarTest.cs View File

@@ -11,7 +11,10 @@ namespace LLama.Unittest

public GrammarTest()
{
_params = new ModelParams("Models/llama-2-7b-chat.ggmlv3.q3_K_S.bin", contextSize: 2048);
_params = new ModelParams("Models/llama-2-7b-chat.ggmlv3.q3_K_S.bin")
{
ContextSize = 2048,
};
_model = LLamaWeights.LoadFromFile(_params);
}



+ 45
- 0
LLama.Unittest/ModelsParamsTests.cs View File

@@ -0,0 +1,45 @@
using LLama.Common;

namespace LLama.Unittest
{
public class ModelsParamsTests
{
[Fact]
public void SerializeRoundTripSystemTextJson()
{
var expected = new ModelParams("abc/123")
{
BatchSize = 17,
ContextSize = 42,
LoraAdapter = "adapter",
GroupedQueryAttention = 7,
Seed = 42,
GpuLayerCount = 111
};

var json = System.Text.Json.JsonSerializer.Serialize(expected);
var actual = System.Text.Json.JsonSerializer.Deserialize<ModelParams>(json);

Assert.Equal(expected, actual);
}

[Fact]
public void SerializeRoundTripNewtonsoft()
{
var expected = new ModelParams("abc/123")
{
BatchSize = 17,
ContextSize = 42,
LoraAdapter = "adapter",
GroupedQueryAttention = 7,
Seed = 42,
GpuLayerCount = 111
};

var json = Newtonsoft.Json.JsonConvert.SerializeObject(expected);
var actual = Newtonsoft.Json.JsonConvert.DeserializeObject<ModelParams>(json);

Assert.Equal(expected, actual);
}
}
}

+ 4
- 1
LLama.WebAPI/Services/StatefulChatService.cs View File

@@ -16,7 +16,10 @@ public class StatefulChatService : IDisposable

public StatefulChatService(IConfiguration configuration)
{
_context = new LLamaContext(new Common.ModelParams(configuration["ModelPath"], contextSize: 512));
_context = new LLamaContext(new Common.ModelParams(configuration["ModelPath"])
{
ContextSize = 512
});
_session = new ChatSession(new InteractiveExecutor(_context));
}



+ 4
- 1
LLama.WebAPI/Services/StatelessChatService.cs View File

@@ -12,7 +12,10 @@ namespace LLama.WebAPI.Services

public StatelessChatService(IConfiguration configuration)
{
_context = new LLamaContext(new ModelParams(configuration["ModelPath"], contextSize: 512));
_context = new LLamaContext(new ModelParams(configuration["ModelPath"])
{
ContextSize = 512,
});
// TODO: replace with a stateless executor
_session = new ChatSession(new InteractiveExecutor(_context))
.WithOutputTransform(new LLamaTransforms.KeywordTextOutputStreamTransform(new string[] { "User:", "Assistant:" }, redundancyLength: 8))


+ 21
- 4
LLama/Common/ModelParams.cs View File

@@ -6,7 +6,7 @@ namespace LLama.Common
/// <summary>
/// The parameters for initializing a LLama model.
/// </summary>
public class ModelParams
public record ModelParams
: IModelParams
{
/// <summary>
@@ -116,6 +116,22 @@ namespace LLama.Common
/// </summary>
public string Encoding { get; set; } = "UTF-8";

/// <summary>
///
/// </summary>
/// <param name="modelPath">The model path.</param>
[System.Text.Json.Serialization.JsonConstructor]
public ModelParams(string modelPath)
{
ModelPath = modelPath;
}

private ModelParams()
{
// This constructor (default parameterless constructor) is used by Newtonsoft to deserialize!
ModelPath = "";
}

/// <summary>
///
/// </summary>
@@ -139,6 +155,7 @@ namespace LLama.Common
/// <param name="ropeFrequencyScale">RoPE frequency scaling factor</param>
/// <param name="mulMatQ">Use experimental mul_mat_q kernels</param>
/// <param name="encoding">The encoding to use to convert text for the model</param>
[Obsolete("Use object initializer to set all optional parameters")]
public ModelParams(string modelPath, int contextSize = 512, int gpuLayerCount = 20,
int seed = 1337, bool useFp16Memory = true,
bool useMemorymap = true, bool useMemoryLock = false, bool perplexity = false,
@@ -161,10 +178,10 @@ namespace LLama.Common
BatchSize = batchSize;
ConvertEosToNewLine = convertEosToNewLine;
EmbeddingMode = embeddingMode;
GroupedQueryAttention = groupedQueryAttention;
GroupedQueryAttention = groupedQueryAttention;
RmsNormEpsilon = rmsNormEpsilon;
RopeFrequencyBase = ropeFrequencyBase;
RopeFrequencyScale = ropeFrequencyScale;
RopeFrequencyBase = ropeFrequencyBase;
RopeFrequencyScale = ropeFrequencyScale;
MulMatQ = mulMatQ;
Encoding = encoding;
}


Loading…
Cancel
Save