Browse Source

Merge branch 'master' of github.com:AsakusaRinne/LLamaSharp into add_kernel_memory_pkg

tags/v0.8.0
Yaohui Liu 2 years ago
parent
commit
2b0fa42173
No known key found for this signature in database GPG Key ID: E86D01E1809BD23E
4 changed files with 29 additions and 27 deletions
  1. +2
    -2
      LLama.Examples/LLama.Examples.csproj
  2. +3
    -3
      LLama.Unittest/LLama.Unittest.csproj
  3. +1
    -1
      LLama.WebAPI/LLama.WebAPI.csproj
  4. +23
    -21
      LLama/Native/SafeLLamaGrammarHandle.cs

+ 2
- 2
LLama.Examples/LLama.Examples.csproj View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\LLama\LLamaSharp.Runtime.targets" /> <Import Project="..\LLama\LLamaSharp.Runtime.targets" />
<PropertyGroup> <PropertyGroup>
<OutputType>Exe</OutputType> <OutputType>Exe</OutputType>
@@ -29,7 +29,7 @@


<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" /> <PackageReference Include="Microsoft.Extensions.Logging.Console" Version="7.0.0" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-beta1" />
<PackageReference Include="Microsoft.SemanticKernel" Version="1.0.0-beta4" />
</ItemGroup> </ItemGroup>


<ItemGroup> <ItemGroup>


+ 3
- 3
LLama.Unittest/LLama.Unittest.csproj View File

@@ -1,4 +1,4 @@
<Project Sdk="Microsoft.NET.Sdk">
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="..\LLama\LLamaSharp.Runtime.targets" /> <Import Project="..\LLama\LLamaSharp.Runtime.targets" />
<PropertyGroup> <PropertyGroup>
<TargetFramework>net6.0</TargetFramework> <TargetFramework>net6.0</TargetFramework>
@@ -13,8 +13,8 @@
<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" /> <PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.7.2" />
<PackageReference Include="System.Linq.Async" Version="6.0.1" /> <PackageReference Include="System.Linq.Async" Version="6.0.1" />
<PackageReference Include="xunit" Version="2.5.0" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.0">
<PackageReference Include="xunit" Version="2.6.1" />
<PackageReference Include="xunit.runner.visualstudio" Version="2.5.3">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets> <IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets> <PrivateAssets>all</PrivateAssets>
</PackageReference> </PackageReference>


+ 1
- 1
LLama.WebAPI/LLama.WebAPI.csproj View File

@@ -8,7 +8,7 @@


<ItemGroup> <ItemGroup>
<PackageReference Include="Microsoft.VisualStudio.Validation" Version="17.6.11" /> <PackageReference Include="Microsoft.VisualStudio.Validation" Version="17.6.11" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.5.0" />
</ItemGroup> </ItemGroup>


<ItemGroup> <ItemGroup>


+ 23
- 21
LLama/Native/SafeLLamaGrammarHandle.cs View File

@@ -3,6 +3,7 @@ using System.Buffers;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics; using System.Diagnostics;
using System.Linq; using System.Linq;
using System.Runtime.CompilerServices;
using LLama.Exceptions; using LLama.Exceptions;
using LLama.Grammars; using LLama.Grammars;


@@ -49,38 +50,39 @@ namespace LLama.Native
// Borrow an array large enough to hold every single element // Borrow an array large enough to hold every single element
// and another array large enough to hold a pointer to each rule // and another array large enough to hold a pointer to each rule
var allElements = ArrayPool<LLamaGrammarElement>.Shared.Rent(totalElements); var allElements = ArrayPool<LLamaGrammarElement>.Shared.Rent(totalElements);
var pointers = ArrayPool<IntPtr>.Shared.Rent(rules.Count);
var rulePointers = ArrayPool<IntPtr>.Shared.Rent(rules.Count);
try try
{ {
fixed (LLamaGrammarElement* allElementsPtr = allElements)
// We're taking pointers into `allElements` below, so this pin is required to fix
// that memory in place while those pointers are in use!
using var pin = allElements.AsMemory().Pin();

var elementIndex = 0;
var ruleIndex = 0;
foreach (var rule in rules)
{ {
var elementIndex = 0;
var pointerIndex = 0;
foreach (var rule in rules)
{
// Save a pointer to the start of this rule
pointers[pointerIndex++] = (IntPtr)(allElementsPtr + elementIndex);
// Save a pointer to the start of this rule
rulePointers[ruleIndex++] = (IntPtr)Unsafe.AsPointer(ref allElements[elementIndex]);


// Copy all of the rule elements into the flat array
foreach (var element in rule.Elements)
allElementsPtr[elementIndex++] = element;
}
// Copy all of the rule elements into the flat array
foreach (var element in rule.Elements)
allElements[elementIndex++] = element;
}


// Sanity check some things that should be true if the copy worked as planned
Debug.Assert((ulong)pointerIndex == nrules);
Debug.Assert(elementIndex == totalElements);
// Sanity check some things that should be true if the copy worked as planned
Debug.Assert((ulong)ruleIndex == nrules);
Debug.Assert(elementIndex == totalElements);


// Make the actual call through to llama.cpp
fixed (void* ptr = pointers)
{
return Create((LLamaGrammarElement**)ptr, nrules, start_rule_index);
}
// Make the actual call through to llama.cpp
fixed (void* ptr = rulePointers)
{
return Create((LLamaGrammarElement**)ptr, nrules, start_rule_index);
} }
} }
finally finally
{ {
ArrayPool<LLamaGrammarElement>.Shared.Return(allElements); ArrayPool<LLamaGrammarElement>.Shared.Return(allElements);
ArrayPool<IntPtr>.Shared.Return(pointers);
ArrayPool<IntPtr>.Shared.Return(rulePointers);
} }
} }
} }


Loading…
Cancel
Save