Browse Source

- Removed unnecessary constructors from safe handles

- Returning SafeLLamaGrammarHandle directly from `llama_grammar_init` and `llama_grammar_copy`
pull/696/head
Martin Evans 1 year ago
parent
commit
54dab273cd
4 changed files with 6 additions and 42 deletions
  1. +2
    -2
      LLama/Native/NativeApi.Grammar.cs
  2. +4
    -13
      LLama/Native/SafeLLamaGrammarHandle.cs
  3. +0
    -14
      LLama/Native/SafeLlavaImageEmbedHandle.cs
  4. +0
    -13
      LLama/Native/SafeLlavaModelHandle.cs

+ 2
- 2
LLama/Native/NativeApi.Grammar.cs View File

@@ -13,7 +13,7 @@ namespace LLama.Native
/// <param name="start_rule_index"></param> /// <param name="start_rule_index"></param>
/// <returns></returns> /// <returns></returns>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)] [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern unsafe IntPtr llama_grammar_init(LLamaGrammarElement** rules, ulong n_rules, ulong start_rule_index);
public static extern unsafe SafeLLamaGrammarHandle llama_grammar_init(LLamaGrammarElement** rules, ulong n_rules, ulong start_rule_index);


/// <summary> /// <summary>
/// Free all memory from the given SafeLLamaGrammarHandle /// Free all memory from the given SafeLLamaGrammarHandle
@@ -28,7 +28,7 @@ namespace LLama.Native
/// <param name="grammar"></param> /// <param name="grammar"></param>
/// <returns></returns> /// <returns></returns>
[DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)] [DllImport(libraryName, CallingConvention = CallingConvention.Cdecl)]
public static extern IntPtr llama_grammar_copy(SafeLLamaGrammarHandle grammar);
public static extern SafeLLamaGrammarHandle llama_grammar_copy(SafeLLamaGrammarHandle grammar);


/// <summary> /// <summary>
/// Apply constraints from grammar /// Apply constraints from grammar


+ 4
- 13
LLama/Native/SafeLLamaGrammarHandle.cs View File

@@ -16,15 +16,6 @@ namespace LLama.Native
: SafeLLamaHandleBase : SafeLLamaHandleBase
{ {
#region construction/destruction #region construction/destruction
/// <summary>
///
/// </summary>
/// <param name="handle"></param>
internal SafeLLamaGrammarHandle(IntPtr handle)
: base(handle, true)
{
}

/// <inheritdoc /> /// <inheritdoc />
protected override bool ReleaseHandle() protected override bool ReleaseHandle()
{ {
@@ -97,11 +88,11 @@ namespace LLama.Native
/// <exception cref="RuntimeError"></exception> /// <exception cref="RuntimeError"></exception>
public static unsafe SafeLLamaGrammarHandle Create(LLamaGrammarElement** rules, ulong nrules, ulong start_rule_index) public static unsafe SafeLLamaGrammarHandle Create(LLamaGrammarElement** rules, ulong nrules, ulong start_rule_index)
{ {
var grammar_ptr = NativeApi.llama_grammar_init(rules, nrules, start_rule_index);
if (grammar_ptr == IntPtr.Zero)
var grammar = NativeApi.llama_grammar_init(rules, nrules, start_rule_index);
if (grammar == null)
throw new RuntimeError("Failed to create grammar from rules"); throw new RuntimeError("Failed to create grammar from rules");


return new(grammar_ptr);
return grammar;
} }
#endregion #endregion


@@ -111,7 +102,7 @@ namespace LLama.Native
/// <returns></returns> /// <returns></returns>
public SafeLLamaGrammarHandle Clone() public SafeLLamaGrammarHandle Clone()
{ {
return new SafeLLamaGrammarHandle(NativeApi.llama_grammar_copy(this));
return NativeApi.llama_grammar_copy(this);
} }


/// <summary> /// <summary>


+ 0
- 14
LLama/Native/SafeLlavaImageEmbedHandle.cs View File

@@ -1,10 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Text;
using LLama;
using LLama.Exceptions;




namespace LLama.Native namespace LLama.Native
@@ -15,15 +10,6 @@ namespace LLama.Native
public sealed class SafeLlavaImageEmbedHandle public sealed class SafeLlavaImageEmbedHandle
: SafeLLamaHandleBase : SafeLLamaHandleBase
{ {

private SafeLlavaImageEmbedHandle(IntPtr handle)
: base(handle, true)
{
}
private SafeLlavaImageEmbedHandle()
{}

/// <summary> /// <summary>
/// Create an image embed from an image file /// Create an image embed from an image file
/// </summary> /// </summary>


+ 0
- 13
LLama/Native/SafeLlavaModelHandle.cs View File

@@ -1,10 +1,6 @@
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq;
using System.Runtime.InteropServices; using System.Runtime.InteropServices;
using System.Text;
using LLama;
using LLama.Exceptions; using LLama.Exceptions;




@@ -16,15 +12,6 @@ namespace LLama.Native
public sealed class SafeLlavaModelHandle public sealed class SafeLlavaModelHandle
: SafeLLamaHandleBase : SafeLLamaHandleBase
{ {

private SafeLlavaModelHandle(IntPtr handle)
: base(handle, true)
{
}

private SafeLlavaModelHandle()
{}
/// <inheritdoc /> /// <inheritdoc />
protected override bool ReleaseHandle() protected override bool ReleaseHandle()
{ {


Loading…
Cancel
Save