Browse Source

Merge pull request #437 from martindevans/check_model_path_exists

Check Model Path Exists
tags/v0.10.0
Martin Evans GitHub 2 years ago
parent
commit
4ef618012e
No known key found for this signature in database GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 5 deletions
  1. +19
    -0
      LLama/Exceptions/RuntimeError.cs
  2. +11
    -5
      LLama/Native/SafeLlamaModelHandle.cs

+ 19
- 0
LLama/Exceptions/RuntimeError.cs View File

@@ -17,4 +17,23 @@ public class RuntimeError
{

}
}

/// <summary>
/// Loading model weights failed
/// </summary>
public class LoadWeightsFailedException
: RuntimeError
{
/// <summary>
/// The model path which failed to load
/// </summary>
public string ModelPath { get; }

/// <inheritdoc />
public LoadWeightsFailedException(string modelPath)
: base($"Failed to load model '{modelPath}'")
{
ModelPath = modelPath;
}
}

+ 11
- 5
LLama/Native/SafeLlamaModelHandle.cs View File

@@ -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,11 +64,16 @@ namespace LLama.Native
/// <exception cref="RuntimeError"></exception>
public static SafeLlamaModelHandle LoadFromFile(string modelPath, LLamaModelParams lparams)
{
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


Loading…
Cancel
Save