Browse Source

- Fixed null check

- Additional comments
tags/v0.4.2-preview
Martin Evans 2 years ago
parent
commit
c95b14d8b3
2 changed files with 19 additions and 5 deletions
  1. +7
    -3
      LLama/Native/SafeLLamaHandleBase.cs
  2. +12
    -2
      LLama/Native/SafeLlamaModelHandle.cs

+ 7
- 3
LLama/Native/SafeLLamaHandleBase.cs View File

@@ -1,11 +1,13 @@
using System;
using System.Collections.Generic;
using System.Runtime.InteropServices;
using System.Text;

namespace LLama.Native
{
public abstract class SafeLLamaHandleBase: SafeHandle
/// <summary>
/// Base class for all llama handles to native resources
/// </summary>
public abstract class SafeLLamaHandleBase
: SafeHandle
{
private protected SafeLLamaHandleBase()
: base(IntPtr.Zero, ownsHandle: true)
@@ -24,8 +26,10 @@ namespace LLama.Native
SetHandle(handle);
}

/// <inheritdoc />
public override bool IsInvalid => handle == IntPtr.Zero;

/// <inheritdoc />
public override string ToString()
=> $"0x{handle.ToString("x16")}";
}


+ 12
- 2
LLama/Native/SafeLlamaModelHandle.cs View File

@@ -3,10 +3,13 @@ using LLama.Exceptions;

namespace LLama.Native
{
/// <summary>
/// A reference to a set of llama model weights
/// </summary>
public class SafeLlamaModelHandle
: SafeLLamaHandleBase
{
public SafeLlamaModelHandle(IntPtr handle)
internal SafeLlamaModelHandle(IntPtr handle)
: base(handle)
{
}
@@ -19,10 +22,17 @@ namespace LLama.Native
return true;
}

/// <summary>
/// Load a model from the given file path into memory
/// </summary>
/// <param name="modelPath"></param>
/// <param name="lparams"></param>
/// <returns></returns>
/// <exception cref="RuntimeError"></exception>
public static SafeLlamaModelHandle LoadFromFile(string modelPath, LLamaContextParams lparams)
{
var model_ptr = NativeApi.llama_load_model_from_file(modelPath, lparams);
if (model_ptr == null)
if (model_ptr == IntPtr.Zero)
throw new RuntimeError($"Failed to load model {modelPath}.");

return new SafeLlamaModelHandle(model_ptr);


Loading…
Cancel
Save