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

November Binary Update #316

Merged
merged 4 commits into from
Nov 20, 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
58 changes: 57 additions & 1 deletion LLama.Unittest/BasicTest.cs
Original file line number Diff line number Diff line change
@@ -1,16 +1,20 @@
using System.Text;
using LLama.Common;
using LLama.Native;
using Xunit.Abstractions;

namespace LLama.Unittest
{
public sealed class BasicTest
: IDisposable
{
private readonly ITestOutputHelper _testOutputHelper;
private readonly ModelParams _params;
private readonly LLamaWeights _model;

public BasicTest()
public BasicTest(ITestOutputHelper testOutputHelper)
{
_testOutputHelper = testOutputHelper;
_params = new ModelParams(Constants.ModelPath)
{
ContextSize = 2048
Expand All @@ -30,5 +34,57 @@ public void BasicModelProperties()
Assert.Equal(4096, _model.ContextSize);
Assert.Equal(4096, _model.EmbeddingSize);
}

[Fact]
public void AdvancedModelProperties()
{
var expected = new Dictionary<string, string>
{
{ "general.name", "LLaMA v2" },
{ "general.architecture", "llama" },
{ "general.quantization_version", "2" },
{ "general.file_type", "2" },

{ "llama.context_length", "4096" },
{ "llama.rope.dimension_count", "128" },
{ "llama.embedding_length", "4096" },
{ "llama.block_count", "32" },
{ "llama.feed_forward_length", "11008" },
{ "llama.attention.head_count", "32" },
{ "llama.attention.head_count_kv", "32" },
{ "llama.attention.layer_norm_rms_epsilon", "0.000001" },

{ "tokenizer.ggml.eos_token_id", "2" },
{ "tokenizer.ggml.model", "llama" },
{ "tokenizer.ggml.bos_token_id", "1" },
{ "tokenizer.ggml.unknown_token_id", "0" },
};

var metaCount = NativeApi.llama_model_meta_count(_model.NativeHandle);
Assert.Equal(expected.Count, metaCount);

Span<byte> buffer = stackalloc byte[128];
for (var i = 0; i < expected.Count; i++)
{
unsafe
{
fixed (byte* ptr = buffer)
{
var length = NativeApi.llama_model_meta_key_by_index(_model.NativeHandle, i, ptr, 128);
Assert.True(length > 0);
var key = Encoding.UTF8.GetString(buffer[..length]);

length = NativeApi.llama_model_meta_val_str_by_index(_model.NativeHandle, i, ptr, 128);
Assert.True(length > 0);
var val = Encoding.UTF8.GetString(buffer[..length]);

_testOutputHelper.WriteLine($"{key} == {val}");

Assert.True(expected.ContainsKey(key));
Assert.Equal(expected[key], val);
}
}
}
}
}
}
2 changes: 2 additions & 0 deletions LLama.Unittest/LLama.Unittest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@
<Nullable>enable</Nullable>

<IsPackable>false</IsPackable>

<AllowUnsafeBlocks>true</AllowUnsafeBlocks>
</PropertyGroup>

<ItemGroup>
Expand Down
100 changes: 64 additions & 36 deletions LLama/LLamaSharp.Runtime.targets
Original file line number Diff line number Diff line change
Expand Up @@ -3,41 +3,69 @@
<IncludeBuiltInRuntimes Condition="'$(IncludeBuiltInRuntimes)' == ''">true</IncludeBuiltInRuntimes>
</PropertyGroup>
<ItemGroup Condition="'$(IncludeBuiltInRuntimes)' == 'true'">
<None Include="$(MSBuildThisFileDirectory)runtimes/libllama.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/win-x64/native/libllama.dll</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/libllama-cuda11.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/win-x64/native/cuda11/libllama.dll</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/libllama-cuda12.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/win-x64/native/cuda12/libllama.dll</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/libllama.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/linux-x64/native/libllama.so</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/libllama-cuda11.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/linux-x64/native/cuda11/libllama.so</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/libllama-cuda12.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<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/native/libllama.dylib</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/osx-arm64/ggml-metal.metal">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<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/native/libllama.dylib</Link>
</None>

<None Include="$(MSBuildThisFileDirectory)runtimes/deps/libllama.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/win-x64/native/noavx/libllama.dll</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/avx/libllama.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/win-x64/native/avx/libllama.dll</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/avx2/libllama.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/win-x64/native/avx2/libllama.dll</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/avx512/libllama.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/win-x64/native/avx512/libllama.dll</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/cu11.7.1/libllama.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/win-x64/native/cuda11/libllama.dll</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/cu12.1.0/libllama.dll">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/win-x64/native/cuda12/libllama.dll</Link>
</None>

<None Include="$(MSBuildThisFileDirectory)runtimes/deps/libllama.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/linux-x64/native/noavx/libllama.so</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/avx/libllama.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/linux-x64/native/avx/libllama.so</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/avx2/libllama.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/linux-x64/native/avx2/libllama.so</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/avx512/libllama.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/linux-x64/native/avx512/libllama.so</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/cu11.7.1/libllama.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/linux-x64/native/cuda11/libllama.so</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/cu12.1.0/libllama.so">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/linux-x64/native/cuda12/libllama.so</Link>
</None>

<None Include="$(MSBuildThisFileDirectory)runtimes/deps/osx-arm64/libllama.dylib">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/osx-arm64/native/libllama.dylib</Link>
</None>
<None Include="$(MSBuildThisFileDirectory)runtimes/deps/osx-arm64/ggml-metal.metal">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/osx-arm64/native/ggml-metal.metal</Link>
</None>

<None Include="$(MSBuildThisFileDirectory)runtimes/deps/osx-x64/libllama.dylib">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
<Link>runtimes/osx-x64/native/libllama.dylib</Link>
</None>
</ItemGroup>
</Project>
79 changes: 76 additions & 3 deletions LLama/Native/NativeApi.cs
Original file line number Diff line number Diff line change
Expand Up @@ -302,6 +302,20 @@ public static int llama_tokenize(SafeLLamaContextHandle ctx, string text, Encodi
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern llama_token llama_token_nl(SafeLlamaModelHandle model);

/// <summary>
/// Returns -1 if unknown, 1 for true or 0 for false.
/// </summary>
/// <returns></returns>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern int llama_add_bos_token(SafeLlamaModelHandle model);

/// <summary>
/// Returns -1 if unknown, 1 for true or 0 for false.
/// </summary>
/// <returns></returns>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern int llama_add_eos_token(SafeLlamaModelHandle model);

/// <summary>
/// Print out timing information for this context
/// </summary>
Expand Down Expand Up @@ -348,18 +362,77 @@ public static int llama_tokenize(SafeLLamaContextHandle ctx, string text, Encodi
public static extern int llama_n_embd(SafeLlamaModelHandle model);

/// <summary>
/// Get the size of the model in bytes
/// Get the model's RoPE frequency scaling factor
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern float llama_rope_freq_scale_train(SafeLlamaModelHandle model);

/// <summary>
/// Get metadata value as a string by key name
/// </summary>
/// <param name="model"></param>
/// <param name="key"></param>
/// <param name="buf"></param>
/// <param name="buf_size"></param>
/// <returns>The length of the string on success, or -1 on failure</returns>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern int llama_model_meta_val_str(SafeLlamaModelHandle model, byte* key, byte* buf, long buf_size);

/// <summary>
/// Get the number of metadata key/value pairs
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern int llama_model_meta_count(SafeLlamaModelHandle model);

/// <summary>
/// Get metadata key name by index
/// </summary>
/// <param name="model"></param>
/// <param name="index"></param>
/// <param name="buf"></param>
/// <param name="buf_size"></param>
/// <returns>The length of the string on success, or -1 on failure</returns>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern int llama_model_meta_key_by_index(SafeLlamaModelHandle model, int index, byte* buf, long buf_size);

/// <summary>
/// Get metadata value as a string by index
/// </summary>
/// <param name="model"></param>
/// <param name="index"></param>
/// <param name="buf"></param>
/// <param name="buf_size"></param>
/// <returns>The length of the string on success, or -1 on failure</returns>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern int llama_model_meta_val_str_by_index(SafeLlamaModelHandle model, int index, byte* buf, long buf_size);

/// <summary>
/// Get a string describing the model type
/// </summary>
/// <param name="model"></param>
/// <param name="buf"></param>
/// <param name="buf_size"></param>
/// <returns>The length of the string on success, or -1 on failure</returns>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern int llama_model_desc(SafeLlamaModelHandle model, byte* buf, long buf_size);

/// <summary>
/// Get the size of the model in bytes
/// </summary>
/// <param name="model"></param>
/// <returns>The size of the model</returns>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern ulong llama_model_size(SafeLlamaModelHandle model);

/// <summary>
/// Get the number of parameters in this model
/// </summary>
/// <param name="model"></param>
/// <returns></returns>
/// <returns>The functions return the length of the string on success, or -1 on failure</returns>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern ulong llama_model_n_params(SafeLlamaModelHandle model);

Expand All @@ -370,7 +443,7 @@ public static int llama_tokenize(SafeLLamaContextHandle ctx, string text, Encodi
/// <param name="llamaToken"></param>
/// <param name="buffer">buffer to write string into</param>
/// <param name="length">size of the buffer</param>
/// <returns>The length writte, or if the buffer is too small a negative that indicates the length required</returns>
/// <returns>The length written, or if the buffer is too small a negative that indicates the length required</returns>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern int llama_token_to_piece(SafeLlamaModelHandle model, int llamaToken, byte* buffer, int length);

Expand Down
20 changes: 15 additions & 5 deletions LLama/runtimes/build/LLamaSharp.Backend.Cpu.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,21 @@

<files>
<file src="LLamaSharpBackend.props" target="build/netstandard2.0/LLamaSharp.Backend.Cpu.props" />
<file src="runtimes/libllama.dll" target="runtimes\win-x64\native\libllama.dll" />
<file src="runtimes/libllama.so" target="runtimes\linux-x64\native\libllama.so" />
<file src="runtimes/osx-x64/libllama.dylib" target="runtimes\osx-x64\native\libllama.dylib" />
<file src="runtimes/osx-arm64/libllama.dylib" target="runtimes\osx-arm64\native\libllama.dylib" />
<file src="runtimes/osx-arm64/ggml-metal.metal" target="runtimes\osx-arm64\native\ggml-metal.metal" />

<file src="runtimes/deps/libllama.dll" target="runtimes\win-x64\native\libllama.dll" />
<file src="runtimes/deps/avx/libllama.dll" target="runtimes\win-x64\native\avx\libllama.dll" />
<file src="runtimes/deps/avx2/libllama.dll" target="runtimes\win-x64\native\avx2\libllama.dll" />
<file src="runtimes/deps/avx512/libllama.dll" target="runtimes\win-x64\native\avx512\libllama.dll" />

<file src="runtimes/deps/libllama.so" target="runtimes\linux-x64\native\libllama.so" />
<file src="runtimes/deps/avx/libllama.so" target="runtimes\linux-x64\native\avx\libllama.so" />
<file src="runtimes/deps/avx2/libllama.so" target="runtimes\linux-x64\native\avx2\libllama.so" />
<file src="runtimes/deps/avx512/libllama.so" target="runtimes\linux-x64\native\avx512\libllama.so" />

<file src="runtimes/deps/osx-x64/libllama.dylib" target="runtimes\osx-x64\native\libllama.dylib" />
<file src="runtimes/deps/osx-arm64/libllama.dylib" target="runtimes\osx-arm64\native\libllama.dylib" />
<file src="runtimes/deps/osx-arm64/ggml-metal.metal" target="runtimes\osx-arm64\native\ggml-metal.metal" />

<file src="icon512.png" target="icon512.png" />
</files>
</package>
6 changes: 4 additions & 2 deletions LLama/runtimes/build/LLamaSharp.Backend.Cuda11.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

<files>
<file src="LLamaSharpBackend.props" target="build/netstandard2.0/LLamaSharp.Backend.Cuda11.props" />
<file src="runtimes/libllama-cuda11.dll" target="runtimes\win-x64\native\cuda11\libllama.dll" />
<file src="runtimes/libllama-cuda11.so" target="runtimes\linux-x64\native\cuda11\libllama.so" />

<file src="runtimes/deps/cu11.7.1/libllama.dll" target="runtimes\win-x64\native\cuda11\libllama.dll" />
<file src="runtimes/deps/cu11.7.1/libllama.so" target="runtimes\linux-x64\native\cuda11\libllama.so" />

<file src="icon512.png" target="icon512.png" />
</files>
</package>
6 changes: 4 additions & 2 deletions LLama/runtimes/build/LLamaSharp.Backend.Cuda12.nuspec
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,10 @@

<files>
<file src="LLamaSharpBackend.props" target="build/netstandard2.0/LLamaSharp.Backend.Cuda12.props" />
<file src="runtimes/libllama-Cuda12.dll" target="runtimes\win-x64\native\cuda12\libllama.dll" />
<file src="runtimes/libllama-Cuda12.so" target="runtimes\linux-x64\native\cuda12\libllama.so" />

<file src="runtimes/deps/cu12.1.0/libllama.dll" target="runtimes\win-x64\native\cuda12\libllama.dll" />
<file src="runtimes/deps/cu12.1.0/libllama.so" target="runtimes\linux-x64\native\cuda12\libllama.so" />

<file src="icon512.png" target="icon512.png" />
</files>
</package>
Binary file added LLama/runtimes/deps/avx/libllama.dll
Binary file not shown.
Binary file added LLama/runtimes/deps/avx/libllama.so
Binary file not shown.
Binary file added LLama/runtimes/deps/avx2/libllama.dll
Binary file not shown.
Binary file added LLama/runtimes/deps/avx2/libllama.so
Binary file not shown.
Binary file added LLama/runtimes/deps/avx512/libllama.dll
Binary file not shown.
Binary file added LLama/runtimes/deps/avx512/libllama.so
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added LLama/runtimes/deps/libllama.dll
Binary file not shown.
Binary file added LLama/runtimes/deps/libllama.so
Binary file not shown.
Loading
Loading