-
Notifications
You must be signed in to change notification settings - Fork 375
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
[WIP] refactor: init some experimental refactoring. #362
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -1,7 +1,9 @@ | ||||||
using System.Collections.Generic; | ||||||
using LLama.Common; | ||||||
using LLama.Control; | ||||||
using LLama.Native; | ||||||
using LLama.Sampling; | ||||||
using LLama.Transform; | ||||||
|
||||||
namespace LLama.Abstractions | ||||||
{ | ||||||
|
@@ -114,5 +116,15 @@ public interface IInferenceParams | |||||
/// Set a custom sampling pipeline to use. <b>If this is set All other sampling parameters are ignored!</b> | ||||||
/// </summary> | ||||||
ISamplingPipeline? SamplingPipeline { get; set; } | ||||||
|
||||||
/// <summary> | ||||||
/// Set a custom generation control to use. <b>If this is set antiprompt will be ignored!</b> | ||||||
/// </summary> | ||||||
IGenerationControl GenerationControl { get; set; } | ||||||
|
||||||
/// <summary> | ||||||
/// Set a custom tokenizer to use. | ||||||
/// </summary> | ||||||
ITokenizer Tokenizer { get; set; } | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,42 @@ | ||||||
using LLama.Abstractions; | ||||||
using System; | ||||||
using System.Collections.Generic; | ||||||
using System.Text; | ||||||
|
||||||
namespace LLama.Control | ||||||
{ | ||||||
/// <summary> | ||||||
/// The default generation control in LLamaSharp, using antiprompts. This class should not be inherited. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's sealed, so it's not possible to extend this class. |
||||||
/// <b>Note that this class has state. The previous outputs feeded to it will affect its control.</b> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
/// If you use it in a session, please don't reuse it for another session unless you intend to do so. | ||||||
/// </summary> | ||||||
public sealed class DefaultGenerationControl: IGenerationControl | ||||||
{ | ||||||
private AntipromptProcessor _antipromptProcessor; | ||||||
|
||||||
/// <summary> | ||||||
/// <inheritdoc/> | ||||||
/// </summary> | ||||||
public DefaultGenerationControl() | ||||||
{ | ||||||
_antipromptProcessor = new AntipromptProcessor(); | ||||||
} | ||||||
|
||||||
/// <summary> | ||||||
/// <inheritdoc/> | ||||||
/// </summary> | ||||||
public bool ShouldStopGeneration(LLamaContext context, IInferenceParams inferenceParams, string lastOutputText) | ||||||
{ | ||||||
_antipromptProcessor.SetAntiprompts(inferenceParams.AntiPrompts); | ||||||
return _antipromptProcessor.Add(lastOutputText); | ||||||
} | ||||||
|
||||||
/// <summary> | ||||||
/// <inheritdoc/> | ||||||
/// </summary> | ||||||
public bool ShouldStopGeneration(LLamaContext context, IInferenceParams inferenceParams, IEnumerable<int> lastOutputIds) | ||||||
{ | ||||||
return false; | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should this be returning false? |
||||||
} | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using LLama.Abstractions; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace LLama.Control | ||
{ | ||
/// <summary> | ||
/// Control the text generation of LLama Executors. | ||
/// </summary> | ||
public interface IGenerationControl | ||
{ | ||
/// <summary> | ||
/// Use the last output text to determine if the generation should stop. | ||
/// </summary> | ||
/// <param name="context"></param> | ||
/// <param name="inferenceParams"></param> | ||
/// <param name="lastOutputText"></param> | ||
/// <returns></returns> | ||
bool ShouldStopGeneration(LLamaContext context, IInferenceParams inferenceParams, string lastOutputText); | ||
|
||
/// <summary> | ||
martindevans marked this conversation as resolved.
Show resolved
Hide resolved
|
||
/// Use the last output ids to determine if the generation should stop. | ||
/// </summary> | ||
/// <param name="context"></param> | ||
/// <param name="inferenceParams"></param> | ||
/// <param name="lastOutputIds"></param> | ||
/// <returns></returns> | ||
bool ShouldStopGeneration(LLamaContext context, IInferenceParams inferenceParams, IEnumerable<int> lastOutputIds); | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
using LLama.Abstractions; | ||
using LLama.Common; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Runtime.CompilerServices; | ||
using System.Text; | ||
using System.Threading; | ||
|
||
namespace LLama | ||
{ | ||
/// <summary> | ||
/// A class to execute text completion task. | ||
/// </summary> | ||
public class TextCompletion | ||
{ | ||
public string Execute(string prompt, IInferenceParams? inferenceParams = null) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public ChatHistory Execute(ChatHistory prompt, IInferenceParams? inferenceParams = null) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
|
||
public async IAsyncEnumerable<string> StreamingExecute(string prompt, IInferenceParams? inferenceParams = null, [EnumeratorCancellation] CancellationToken cancellationToken = default) | ||
{ | ||
throw new NotImplementedException(); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,53 @@ | ||||||
using System; | ||||||
using System.Collections.Generic; | ||||||
using System.Text; | ||||||
|
||||||
namespace LLama.Transform | ||||||
{ | ||||||
/// <summary> | ||||||
/// The default tokenizer of LLamaSharp. This class should not be inherited. | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
/// <b>Note that this class has state. The previous outputs feeded to it will affect its control.</b> | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
/// If you use it in a session, please don't reuse it for another session unless you intend to do so. | ||||||
/// </summary> | ||||||
public sealed class DefaultTokenizer: ITokenizer | ||||||
{ | ||||||
private Encoding _encoding; | ||||||
private StreamingTokenDecoder _tokenDecoder; | ||||||
|
||||||
/// <summary> | ||||||
/// Initialize a new tokenizer with the specified encoding. | ||||||
/// </summary> | ||||||
/// <param name="encoding"></param> | ||||||
public DefaultTokenizer(Encoding encoding) | ||||||
{ | ||||||
_encoding = encoding; | ||||||
_tokenDecoder = new StreamingTokenDecoder(encoding); | ||||||
} | ||||||
|
||||||
/// <summary> | ||||||
/// <inheritdoc/> | ||||||
/// </summary> | ||||||
public IEnumerable<int> Tokenize(LLamaContext context, string text, bool addBos = true, bool special = false) | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better to accept a There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I'll change it, thank you! There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is there already a way to tokenize without a context? I didn't find a such method There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. LLamaWeights w = your_weights;
w.NativeHandle.Tokenize("a string"); Should do it. There should really be higher level wrappers for tokenization in There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see, I mistook the one in NativeApi with context as parameter as the lowest level api. I'll add a wrapper for it with LLamaWeights. :) |
||||||
{ | ||||||
return context.Tokenize(text, addBos, special); | ||||||
} | ||||||
|
||||||
/// <summary> | ||||||
/// <inheritdoc/> | ||||||
/// </summary> | ||||||
public string Detokenize(LLamaContext context, int token) | ||||||
{ | ||||||
_tokenDecoder.Add(token, context.NativeHandle.ModelHandle); | ||||||
return _tokenDecoder.Read(); | ||||||
} | ||||||
|
||||||
/// <summary> | ||||||
/// <inheritdoc/> | ||||||
/// </summary> | ||||||
public string Detokenize(LLamaContext context, IEnumerable<int> tokens) | ||||||
{ | ||||||
_tokenDecoder.AddRange(tokens, context.NativeHandle.ModelHandle); | ||||||
return _tokenDecoder.Read(); | ||||||
} | ||||||
} | ||||||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace LLama.Transform | ||
{ | ||
public interface ITokenizer | ||
{ | ||
IEnumerable<int> Tokenize(LLamaContext context, string text, bool addBos = true, bool special = false); | ||
|
||
string Detokenize(LLamaContext context, int token); | ||
|
||
string Detokenize(LLamaContext context, IEnumerable<int> tokens); | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.