From c95b14d8b3f0cc182c210f72e355223499e3f325 Mon Sep 17 00:00:00 2001 From: Martin Evans Date: Tue, 25 Jul 2023 16:23:25 +0100 Subject: [PATCH] - Fixed null check - Additional comments --- LLama/Native/SafeLLamaHandleBase.cs | 10 +++++++--- LLama/Native/SafeLlamaModelHandle.cs | 14 ++++++++++++-- 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/LLama/Native/SafeLLamaHandleBase.cs b/LLama/Native/SafeLLamaHandleBase.cs index 023f8cdd..6371b327 100644 --- a/LLama/Native/SafeLLamaHandleBase.cs +++ b/LLama/Native/SafeLLamaHandleBase.cs @@ -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 + /// + /// Base class for all llama handles to native resources + /// + public abstract class SafeLLamaHandleBase + : SafeHandle { private protected SafeLLamaHandleBase() : base(IntPtr.Zero, ownsHandle: true) @@ -24,8 +26,10 @@ namespace LLama.Native SetHandle(handle); } + /// public override bool IsInvalid => handle == IntPtr.Zero; + /// public override string ToString() => $"0x{handle.ToString("x16")}"; } diff --git a/LLama/Native/SafeLlamaModelHandle.cs b/LLama/Native/SafeLlamaModelHandle.cs index 7448efa1..5607ccee 100644 --- a/LLama/Native/SafeLlamaModelHandle.cs +++ b/LLama/Native/SafeLlamaModelHandle.cs @@ -3,10 +3,13 @@ using LLama.Exceptions; namespace LLama.Native { + /// + /// A reference to a set of llama model weights + /// public class SafeLlamaModelHandle : SafeLLamaHandleBase { - public SafeLlamaModelHandle(IntPtr handle) + internal SafeLlamaModelHandle(IntPtr handle) : base(handle) { } @@ -19,10 +22,17 @@ namespace LLama.Native return true; } + /// + /// Load a model from the given file path into memory + /// + /// + /// + /// + /// 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);