From 1e86755071144e63ef1509f996afcbaa8df806dc Mon Sep 17 00:00:00 2001 From: Martin Evans Date: Mon, 15 Jan 2024 15:41:43 +0000 Subject: [PATCH] - Removed unnecessary `unsafe` block in model metadata loading - Clarified comments on native metadata loading methods --- LLama/Native/NativeApi.cs | 4 ++-- LLama/Native/SafeLlamaModelHandle.cs | 12 ++++-------- 2 files changed, 6 insertions(+), 10 deletions(-) diff --git a/LLama/Native/NativeApi.cs b/LLama/Native/NativeApi.cs index cb7b8228..1684e501 100644 --- a/LLama/Native/NativeApi.cs +++ b/LLama/Native/NativeApi.cs @@ -406,7 +406,7 @@ namespace LLama.Native /// Model to fetch from /// Index of key to fetch /// buffer to write result into - /// The length of the string on success, or -1 on failure + /// The length of the string on success (even if the buffer is too small). -1 is the key does not exist. public static int llama_model_meta_key_by_index(SafeLlamaModelHandle model, int index, Span dest) { unsafe @@ -427,7 +427,7 @@ namespace LLama.Native /// Model to fetch from /// Index of val to fetch /// Buffer to write result into - /// The length of the string on success, or -1 on failure + /// The length of the string on success (even if the buffer is too small). -1 is the key does not exist. public static int llama_model_meta_val_str_by_index(SafeLlamaModelHandle model, int index, Span dest) { unsafe diff --git a/LLama/Native/SafeLlamaModelHandle.cs b/LLama/Native/SafeLlamaModelHandle.cs index 8ffa2be3..ae0c82e9 100644 --- a/LLama/Native/SafeLlamaModelHandle.cs +++ b/LLama/Native/SafeLlamaModelHandle.cs @@ -230,14 +230,10 @@ namespace LLama.Native /// The key, null if there is no such key or if the buffer was too small public Memory? MetadataKeyByIndex(int index) { - int keyLength; - unsafe - { - // Check if the key exists, without getting any bytes of data - keyLength = NativeApi.llama_model_meta_key_by_index(this, index, Array.Empty()); - if (keyLength < 0) - return null; - } + // Check if the key exists, without getting any bytes of data + var keyLength = NativeApi.llama_model_meta_key_by_index(this, index, Array.Empty()); + if (keyLength < 0) + return null; // get a buffer large enough to hold it var buffer = new byte[keyLength + 1];