From 096e0e75f82af45242481aadede4e978f619ac4e Mon Sep 17 00:00:00 2001 From: Martin Evans Date: Mon, 15 Jan 2024 15:19:54 +0000 Subject: [PATCH 1/2] Check that the model file actually exists immediately before loading it. Improve #395 --- LLama/Native/SafeLlamaModelHandle.cs | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/LLama/Native/SafeLlamaModelHandle.cs b/LLama/Native/SafeLlamaModelHandle.cs index 8ffa2be3..761a7c3a 100644 --- a/LLama/Native/SafeLlamaModelHandle.cs +++ b/LLama/Native/SafeLlamaModelHandle.cs @@ -2,6 +2,7 @@ using System.Buffers; using System.Collections.Generic; using System.Diagnostics; +using System.IO; using System.Runtime.InteropServices; using System.Text; using LLama.Exceptions; @@ -63,6 +64,9 @@ namespace LLama.Native /// public static SafeLlamaModelHandle LoadFromFile(string modelPath, LLamaModelParams lparams) { + if (!File.Exists(modelPath)) + throw new FileNotFoundException("Model file does not exist", modelPath); + var model = llama_load_model_from_file(modelPath, lparams); if (model == null) throw new RuntimeError($"Failed to load model {modelPath}."); From de2b20aae5b9c062768e8f1916f50acd06f015ad Mon Sep 17 00:00:00 2001 From: Martin Evans Date: Mon, 15 Jan 2024 15:29:31 +0000 Subject: [PATCH 2/2] - Added a specific exception for failing to load model weights. - Checking if model is readable --- LLama/Exceptions/RuntimeError.cs | 19 +++++++++++++++++++ LLama/Native/SafeLlamaModelHandle.cs | 18 ++++++++++-------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/LLama/Exceptions/RuntimeError.cs b/LLama/Exceptions/RuntimeError.cs index a8ea0531..c56d78ff 100644 --- a/LLama/Exceptions/RuntimeError.cs +++ b/LLama/Exceptions/RuntimeError.cs @@ -17,4 +17,23 @@ public class RuntimeError { } +} + +/// +/// Loading model weights failed +/// +public class LoadWeightsFailedException + : RuntimeError +{ + /// + /// The model path which failed to load + /// + public string ModelPath { get; } + + /// + public LoadWeightsFailedException(string modelPath) + : base($"Failed to load model '{modelPath}'") + { + ModelPath = modelPath; + } } \ No newline at end of file diff --git a/LLama/Native/SafeLlamaModelHandle.cs b/LLama/Native/SafeLlamaModelHandle.cs index 761a7c3a..5724ec7e 100644 --- a/LLama/Native/SafeLlamaModelHandle.cs +++ b/LLama/Native/SafeLlamaModelHandle.cs @@ -64,14 +64,16 @@ namespace LLama.Native /// public static SafeLlamaModelHandle LoadFromFile(string modelPath, LLamaModelParams lparams) { - if (!File.Exists(modelPath)) - throw new FileNotFoundException("Model file does not exist", modelPath); - - var model = llama_load_model_from_file(modelPath, lparams); - if (model == null) - throw new RuntimeError($"Failed to load model {modelPath}."); - - return model; + // Try to open the model file, this will check: + // - File exists (automatically throws FileNotFoundException) + // - File is readable (explicit check) + // This provides better error messages that llama.cpp, which would throw an access violation exception in both cases. + using (var fs = new FileStream(modelPath, FileMode.Open)) + if (!fs.CanRead) + throw new InvalidOperationException($"Model file '{modelPath}' is not readable"); + + return llama_load_model_from_file(modelPath, lparams) + ?? throw new LoadWeightsFailedException($"Failed to load model {modelPath}."); } #region native API