Skip to content
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

feat: cuda feature detection. #275

Merged
merged 8 commits into from
Nov 12, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .github/prepare_release.sh
Original file line number Diff line number Diff line change
Expand Up @@ -63,14 +63,15 @@ elif [[ $type == "patch" ]]; then
exit 1
fi
else
echo "Invalid type"
echo "Invalid type"
exit 1
fi

cd ..
# pack the main package
dotnet pack ./LLama/LLamaSharp.csproj -c Release -o ./temp/ /p:PackageVersion=$updated_version /p:Version=$updated_version;
dotnet pack ./LLama.SemanticKernel/LLamaSharp.SemanticKernel.csproj -c Release -o ./temp/ /p:PackageVersion=$updated_version /p:Version=$updated_version;
dotnet pack ./LLama.KernelMemory/LLamaSharp.KernelMemory.csproj -c Release -o ./temp/ /p:PackageVersion=$updated_version /p:Version=$updated_version;

# pack the backends
cd temp
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release-minor.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
run: |
dotnet build ./LLama/LLamaSharp.csproj -c Release --no-restore
dotnet build ./LLama.SemanticKernel/LLamaSharp.SemanticKernel.csproj -c Release --no-restore
dotnet build ./LLama.KernelMemory/LLamaSharp.KernelMemory.csproj -c Release --no-restore

- name: Pack packages
run: |
Expand Down
1 change: 1 addition & 0 deletions .github/workflows/release-patch.yml
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ jobs:
run: |
dotnet build ./LLama/LLamaSharp.csproj -c Release --no-restore
dotnet build ./LLama.SemanticKernel/LLamaSharp.SemanticKernel.csproj -c Release --no-restore
dotnet build ./LLama.KernelMemory/LLamaSharp.KernelMemory.csproj -c Release --no-restore

- name: Pack packages
run: |
Expand Down
1 change: 1 addition & 0 deletions LLama.Examples/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

Console.WriteLine("======================================================================================================");

NativeLibraryConfig.Default.WithCuda().WithLogs();

NativeApi.llama_empty_call();
Console.WriteLine();
Expand Down
3 changes: 2 additions & 1 deletion LLama.KernelMemory/LLamaSharp.KernelMemory.csproj
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<TargetFrameworks>net6.0;net7.0</TargetFrameworks>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<Version>0.7.1</Version>
<Version>0.8.0</Version>
<Authors>Xbotter</Authors>
<Company>SciSharp STACK</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand Down
2 changes: 1 addition & 1 deletion LLama.SemanticKernel/LLamaSharp.SemanticKernel.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>

<Version>0.7.1</Version>
<Version>0.8.0</Version>
<Authors>Tim Miller, Xbotter</Authors>
<Company>SciSharp STACK</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand Down
103 changes: 52 additions & 51 deletions LLama/AntipromptProcessor.cs
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;
}
}
}
18 changes: 9 additions & 9 deletions LLama/LLamaSharp.Runtime.targets
Original file line number Diff line number Diff line change
Expand Up @@ -5,39 +5,39 @@
<ItemGroup Condition="'$(IncludeBuiltInRuntimes)' == 'true'">
<None Include="$(MSBuildThisFileDirectory)runtimes/libllama.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>libllama.dll</Link>
<Link>runtimes/win-x64/native/libllama.dll</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/libllama-cuda11.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>libllama-cuda11.dll</Link>
<Link>runtimes/win-x64/native/cuda11/libllama.dll</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/libllama-cuda12.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>libllama-cuda12.dll</Link>
<Link>runtimes/win-x64/native/cuda12/libllama.dll</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/libllama.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>libllama.so</Link>
<Link>runtimes/linux-x64/native/libllama.so</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/libllama-cuda11.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>libllama-cuda11.so</Link>
<Link>runtimes/linux-x64/native/cuda11/libllama.so</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/libllama-cuda12.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>libllama-cuda12.so</Link>
<Link>runtimes/linux-x64/native/cuda12/libllama.so</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/osx-arm64/libllama.dylib">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/osx-arm64/libllama.dylib</Link>
<Link>runtimes/osx-arm64/native/libllama.dylib</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/osx-arm64/ggml-metal.metal">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/osx-arm64/ggml-metal.metal</Link>
<Link>runtimes/osx-arm64/native/ggml-metal.metal</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/osx-x64/libllama.dylib">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/osx-x64/libllama.dylib</Link>
<Link>runtimes/osx-x64/native/libllama.dylib</Link>
</None>
</ItemGroup>
</Project>
6 changes: 3 additions & 3 deletions LLama/LLamaSharp.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
<Platforms>AnyCPU;x64;Arm64</Platforms>
<AllowUnsafeBlocks>True</AllowUnsafeBlocks>

<Version>0.5.0</Version>
<Version>0.8.0</Version>
<Authors>Yaohui Liu, Martin Evans, Haiping Chen</Authors>
<Company>SciSharp STACK</Company>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
Expand All @@ -17,11 +17,11 @@
<PackageIconUrl>https://avatars3.githubusercontent.com/u/44989469?s=200&amp;v=4</PackageIconUrl>
<PackageTags>LLama, LLM, GPT, ChatGPT, NLP, AI, Chat Bot, SciSharp</PackageTags>
<Description>
The .NET binding of LLama.cpp, providing APIs to run the model and deploy it on Web. For model
The .NET binding of LLama.cpp, making LLM inference and deployment easy and fast. For model
weights to run, please go to https://github.com/SciSharp/LLamaSharp for more information.
</Description>
<PackageReleaseNotes>
LLamaSharp 0.5.0 adds support for GGUF, grammar and integration with semantic-kernel.
LLamaSharp 0.8.0 supports automatically device feature detection, adds integration with kernel-memory and fixes some performance issues.
</PackageReleaseNotes>
<PackageLicenseExpression>MIT</PackageLicenseExpression>
<PackageOutputPath>packages</PackageOutputPath>
Expand Down
Loading
Loading