Skip to content

Commit

Permalink
BatchedExecutor.Create() method (#613)
Browse files Browse the repository at this point in the history
Replaced `BatchedExecutor.Prompt(string)` method with `BatchedExecutor.Create()` method. This improves the API in two ways:
 - A conversation can be created, without immediately prompting it
 - Other prompting overloads (e.g. prompt with token list) can be used without duplicating all the overloads onto `BatchedExecutor`

Added `BatchSize` property to `LLamaContext`
  • Loading branch information
martindevans authored Mar 20, 2024
1 parent e3ecc31 commit ad682fb
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 5 deletions.
3 changes: 2 additions & 1 deletion LLama.Examples/Examples/BatchedExecutorFork.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,8 @@ public static async Task Run()
Console.WriteLine($"Created executor with model: {name}");

// Evaluate the initial prompt to create one conversation
using var start = executor.Prompt(prompt);
using var start = executor.Create();
start.Prompt(prompt);
await executor.Infer();

// Create the root node of the tree
Expand Down
6 changes: 4 additions & 2 deletions LLama.Examples/Examples/BatchedExecutorGuidance.cs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ public static async Task Run()
Console.WriteLine($"Created executor with model: {name}");

// Load the two prompts into two conversations
using var guided = executor.Prompt(positivePrompt);
using var guidance = executor.Prompt(negativePrompt);
using var guided = executor.Create();
guided.Prompt(positivePrompt);
using var guidance = executor.Create();
guidance.Prompt(negativePrompt);

// Run inference to evaluate prompts
await AnsiConsole
Expand Down
3 changes: 2 additions & 1 deletion LLama.Examples/Examples/BatchedExecutorRewind.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ public static async Task Run()
Console.WriteLine($"Created executor with model: {name}");

// Evaluate the initial prompt to create one conversation
using var conversation = executor.Prompt(prompt);
using var conversation = executor.Create();
conversation.Prompt(prompt);

// Create the start node wrapping the conversation
var node = new Node(executor.Context);
Expand Down
15 changes: 14 additions & 1 deletion LLama/Batched/BatchedExecutor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -68,17 +68,30 @@ public BatchedExecutor(LLamaWeights model, IContextParams contextParams)
/// </summary>
/// <param name="prompt"></param>
/// <returns></returns>
[Obsolete("Use BatchedExecutor.Create instead")]
public Conversation Prompt(string prompt)
{
if (IsDisposed)
throw new ObjectDisposedException(nameof(BatchedExecutor));

var conversation = new Conversation(this, GetNextSequenceId(), 0);
var conversation = Create();
conversation.Prompt(prompt);

return conversation;
}

/// <summary>
/// Start a new <see cref="Conversation"/>
/// </summary>
/// <returns></returns>
public Conversation Create()
{
if (IsDisposed)
throw new ObjectDisposedException(nameof(BatchedExecutor));

return new Conversation(this, GetNextSequenceId(), 0);
}

/// <summary>
/// Run inference for all conversations in the batch which have pending tokens.
///
Expand Down
5 changes: 5 additions & 0 deletions LLama/LLamaContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,11 @@ public uint BatchThreads
}
}

/// <summary>
/// Get the maximum batch size for this context
/// </summary>
public uint BatchSize => NativeHandle.BatchSize;

/// <summary>
/// Create a new LLamaContext for the given LLamaWeights
/// </summary>
Expand Down

0 comments on commit ad682fb

Please sign in to comment.