Skip to content

Commit 3770a25

Browse files
authored
Merge pull request #198 from martindevans/more_logging
More Logging
2 parents d2cf948 + 18b1518 commit 3770a25

File tree

8 files changed

+33
-22
lines changed

8 files changed

+33
-22
lines changed

LLama.Examples/NewVersion/CodingAssistant.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ public static async Task Run()
3131
};
3232
using var model = LLamaWeights.LoadFromFile(parameters);
3333
using var context = model.CreateContext(parameters);
34-
var executor = new InstructExecutor(context, null!, InstructionPrefix, InstructionSuffix);
34+
var executor = new InstructExecutor(context, InstructionPrefix, InstructionSuffix, null);
3535

3636
Console.ForegroundColor = ConsoleColor.Yellow;
3737
Console.WriteLine("The executor has been enabled. In this example, the LLM will follow your instructions." +

LLama.Web/Models/LLamaModel.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ public Task<LLamaContext> CreateContext(string contextName)
5858
if (_config.MaxInstances > -1 && ContextCount >= _config.MaxInstances)
5959
throw new Exception($"Maximum model instances reached");
6060

61-
context = _weights.CreateContext(_config);
61+
context = _weights.CreateContext(_config, _llamaLogger);
6262
if (_contexts.TryAdd(contextName, context))
6363
return Task.FromResult(context);
6464

LLama/LLamaEmbedder.cs

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
using System;
33
using LLama.Exceptions;
44
using LLama.Abstractions;
5+
using Microsoft.Extensions.Logging;
56

67
namespace LLama
78
{
@@ -22,9 +23,10 @@ public sealed class LLamaEmbedder
2223
/// Create a new embedder (loading temporary weights)
2324
/// </summary>
2425
/// <param name="allParams"></param>
26+
/// <param name="logger"></param>
2527
[Obsolete("Preload LLamaWeights and use the constructor which accepts them")]
26-
public LLamaEmbedder(ILLamaParams allParams)
27-
: this(allParams, allParams)
28+
public LLamaEmbedder(ILLamaParams allParams, ILogger? logger = null)
29+
: this(allParams, allParams, logger)
2830
{
2931
}
3032

@@ -33,24 +35,26 @@ public LLamaEmbedder(ILLamaParams allParams)
3335
/// </summary>
3436
/// <param name="modelParams"></param>
3537
/// <param name="contextParams"></param>
38+
/// <param name="logger"></param>
3639
[Obsolete("Preload LLamaWeights and use the constructor which accepts them")]
37-
public LLamaEmbedder(IModelParams modelParams, IContextParams contextParams)
40+
public LLamaEmbedder(IModelParams modelParams, IContextParams contextParams, ILogger? logger = null)
3841
{
3942
using var weights = LLamaWeights.LoadFromFile(modelParams);
4043

4144
contextParams.EmbeddingMode = true;
42-
_ctx = weights.CreateContext(contextParams);
45+
_ctx = weights.CreateContext(contextParams, logger);
4346
}
4447

4548
/// <summary>
4649
/// Create a new embedder, using the given LLamaWeights
4750
/// </summary>
4851
/// <param name="weights"></param>
4952
/// <param name="params"></param>
50-
public LLamaEmbedder(LLamaWeights weights, IContextParams @params)
53+
/// <param name="logger"></param>
54+
public LLamaEmbedder(LLamaWeights weights, IContextParams @params, ILogger? logger = null)
5155
{
5256
@params.EmbeddingMode = true;
53-
_ctx = weights.CreateContext(@params);
57+
_ctx = weights.CreateContext(@params, logger);
5458
}
5559

5660
/// <summary>
@@ -89,7 +93,6 @@ public float[] GetEmbeddings(string text)
8993
/// <exception cref="RuntimeError"></exception>
9094
public float[] GetEmbeddings(string text, bool addBos)
9195
{
92-
9396
var embed_inp_array = _ctx.Tokenize(text, addBos);
9497

9598
// TODO(Rinne): deal with log of prompt

LLama/LLamaExecutorBase.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,9 @@ public abstract class StatefulExecutorBase : ILLamaExecutor
7575
/// </summary>
7676
/// <param name="context"></param>
7777
/// <param name="logger"></param>
78-
protected StatefulExecutorBase(LLamaContext context)
78+
protected StatefulExecutorBase(LLamaContext context, ILogger? logger = null)
7979
{
80+
_logger = logger;
8081
Context = context;
8182
_pastTokensCount = 0;
8283
_consumedTokensCount = 0;

LLama/LLamaInstructExecutor.cs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ namespace LLama
1717
/// <summary>
1818
/// The LLama executor for instruct mode.
1919
/// </summary>
20-
public class InstructExecutor : StatefulExecutorBase
20+
public class InstructExecutor
21+
: StatefulExecutorBase
2122
{
2223
private bool _is_prompt_run = true;
2324
private readonly string _instructionPrefix;
@@ -28,11 +29,14 @@ public class InstructExecutor : StatefulExecutorBase
2829
///
2930
/// </summary>
3031
/// <param name="context"></param>
31-
/// <param name="logger"></param>
3232
/// <param name="instructionPrefix"></param>
3333
/// <param name="instructionSuffix"></param>
34-
public InstructExecutor(LLamaContext context, ILogger logger = null!, string instructionPrefix = "\n\n### Instruction:\n\n",
35-
string instructionSuffix = "\n\n### Response:\n\n") : base(context)
34+
/// <param name="logger"></param>
35+
public InstructExecutor(LLamaContext context,
36+
string instructionPrefix = "\n\n### Instruction:\n\n",
37+
string instructionSuffix = "\n\n### Response:\n\n",
38+
ILogger? logger = null)
39+
: base(context, logger)
3640
{
3741
_inp_pfx = Context.Tokenize(instructionPrefix, true);
3842
_inp_sfx = Context.Tokenize(instructionSuffix, false);

LLama/LLamaInteractExecutor.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,8 @@ public class InteractiveExecutor : StatefulExecutorBase
2727
/// </summary>
2828
/// <param name="context"></param>
2929
/// <param name="logger"></param>
30-
public InteractiveExecutor(LLamaContext context) : base(context)
30+
public InteractiveExecutor(LLamaContext context, ILogger? logger = null)
31+
: base(context, logger)
3132
{
3233
_llama_token_newline = NativeApi.llama_token_nl(Context.NativeHandle);
3334
}

LLama/LLamaStatelessExecutor.cs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,9 +21,9 @@ namespace LLama
2121
public class StatelessExecutor
2222
: ILLamaExecutor
2323
{
24-
private readonly ILogger? _logger;
2524
private readonly LLamaWeights _weights;
2625
private readonly IContextParams _params;
26+
private readonly ILogger? _logger;
2727

2828
/// <summary>
2929
/// The context used by the executor when running the inference.
@@ -36,24 +36,25 @@ public class StatelessExecutor
3636
/// <param name="weights"></param>
3737
/// <param name="params"></param>
3838
/// <param name="logger"></param>
39-
public StatelessExecutor(LLamaWeights weights, IContextParams @params)
39+
public StatelessExecutor(LLamaWeights weights, IContextParams @params, ILogger? logger = null)
4040
{
4141
_weights = weights;
4242
_params = @params;
43+
_logger = logger;
4344

44-
Context = _weights.CreateContext(_params);
45+
Context = _weights.CreateContext(_params, logger);
4546
Context.Dispose();
4647
}
4748

4849
/// <inheritdoc />
4950
public async IAsyncEnumerable<string> InferAsync(string text, IInferenceParams? inferenceParams = null, [EnumeratorCancellation] CancellationToken cancellationToken = default)
5051
{
51-
using var context = _weights.CreateContext(_params);
52+
using var context = _weights.CreateContext(_params, _logger);
5253
Context = context;
5354

5455
if (!Context.NativeHandle.IsClosed)
5556
Context.Dispose();
56-
Context = _weights.CreateContext(Context.Params);
57+
Context = _weights.CreateContext(Context.Params, _logger);
5758

5859
if (inferenceParams != null)
5960
{

LLama/LLamaWeights.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,10 +81,11 @@ public void Dispose()
8181
/// Create a llama_context using this model
8282
/// </summary>
8383
/// <param name="params"></param>
84+
/// <param name="logger"></param>
8485
/// <returns></returns>
85-
public LLamaContext CreateContext(IContextParams @params)
86+
public LLamaContext CreateContext(IContextParams @params, ILogger? logger = null)
8687
{
87-
return new LLamaContext(this, @params);
88+
return new LLamaContext(this, @params, logger);
8889
}
8990
}
9091
}

0 commit comments

Comments
 (0)