-
Notifications
You must be signed in to change notification settings - Fork 341
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #275 from AsakusaRinne/cuda_detection
feat: cuda feature detection.
- Loading branch information
Showing
17 changed files
with
787 additions
and
331 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,66 +1,67 @@ | ||
using System; | ||
using System.Collections.Generic; | ||
|
||
namespace LLama; | ||
|
||
internal sealed class AntipromptProcessor | ||
namespace LLama | ||
{ | ||
private int _longestAntiprompt; | ||
private readonly List<string> _antiprompts = new(); | ||
|
||
private string? _string; | ||
|
||
public AntipromptProcessor(IEnumerable<string>? antiprompts = null) | ||
internal sealed class AntipromptProcessor | ||
{ | ||
if (antiprompts != null) | ||
SetAntiprompts(antiprompts); | ||
} | ||
private int _longestAntiprompt; | ||
private readonly List<string> _antiprompts = new(); | ||
|
||
/// <summary> | ||
/// Add an antiprompt to the collection | ||
/// </summary> | ||
/// <param name="antiprompt"></param> | ||
public void AddAntiprompt(string antiprompt) | ||
{ | ||
_antiprompts.Add(antiprompt); | ||
_longestAntiprompt = Math.Max(_longestAntiprompt, antiprompt.Length); | ||
} | ||
private string? _string; | ||
|
||
/// <summary> | ||
/// Overwrite all current antiprompts with a new set | ||
/// </summary> | ||
/// <param name="antiprompts"></param> | ||
public void SetAntiprompts(IEnumerable<string> antiprompts) | ||
{ | ||
_antiprompts.Clear(); | ||
_antiprompts.AddRange(antiprompts); | ||
public AntipromptProcessor(IEnumerable<string>? antiprompts = null) | ||
{ | ||
if (antiprompts != null) | ||
SetAntiprompts(antiprompts); | ||
} | ||
|
||
_longestAntiprompt = 0; | ||
foreach (var antiprompt in _antiprompts) | ||
/// <summary> | ||
/// Add an antiprompt to the collection | ||
/// </summary> | ||
/// <param name="antiprompt"></param> | ||
public void AddAntiprompt(string antiprompt) | ||
{ | ||
_antiprompts.Add(antiprompt); | ||
_longestAntiprompt = Math.Max(_longestAntiprompt, antiprompt.Length); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Add some text and check if the buffer now ends with any antiprompt | ||
/// </summary> | ||
/// <param name="text"></param> | ||
/// <returns>true if the text buffer ends with any antiprompt</returns> | ||
public bool Add(string text) | ||
{ | ||
_string += text; | ||
/// <summary> | ||
/// Overwrite all current antiprompts with a new set | ||
/// </summary> | ||
/// <param name="antiprompts"></param> | ||
public void SetAntiprompts(IEnumerable<string> antiprompts) | ||
{ | ||
_antiprompts.Clear(); | ||
_antiprompts.AddRange(antiprompts); | ||
|
||
_longestAntiprompt = 0; | ||
foreach (var antiprompt in _antiprompts) | ||
_longestAntiprompt = Math.Max(_longestAntiprompt, antiprompt.Length); | ||
} | ||
|
||
/// <summary> | ||
/// Add some text and check if the buffer now ends with any antiprompt | ||
/// </summary> | ||
/// <param name="text"></param> | ||
/// <returns>true if the text buffer ends with any antiprompt</returns> | ||
public bool Add(string text) | ||
{ | ||
_string += text; | ||
|
||
// When the string gets very long (4x antiprompt length) trim it down (to 2x antiprompt length). | ||
// This trimming leaves a lot of extra characters because two sequences can be considered "equal" in unicode | ||
// even with different numbers of characters. Hopefully there are enough characters here to handle all those weird circumstances! | ||
var maxLength = Math.Max(32, _longestAntiprompt * 4); | ||
var trimLength = Math.Max(16, _longestAntiprompt * 2); | ||
if (_string.Length > maxLength) | ||
_string = _string.Substring(_string.Length - trimLength); | ||
// When the string gets very long (4x antiprompt length) trim it down (to 2x antiprompt length). | ||
// This trimming leaves a lot of extra characters because two sequences can be considered "equal" in unicode | ||
// even with different numbers of characters. Hopefully there are enough characters here to handle all those weird circumstances! | ||
var maxLength = Math.Max(32, _longestAntiprompt * 4); | ||
var trimLength = Math.Max(16, _longestAntiprompt * 2); | ||
if (_string.Length > maxLength) | ||
_string = _string.Substring(_string.Length - trimLength); | ||
|
||
foreach (var antiprompt in _antiprompts) | ||
if (_string.EndsWith(antiprompt, StringComparison.CurrentCulture)) | ||
return true; | ||
foreach (var antiprompt in _antiprompts) | ||
if (_string.EndsWith(antiprompt, StringComparison.CurrentCulture)) | ||
return true; | ||
|
||
return false; | ||
return false; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.