|
1 | 1 | using System;
|
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Linq; |
2 | 4 |
|
3 | 5 | namespace LLama.Native
|
4 | 6 | {
|
@@ -27,6 +29,10 @@ public sealed class NativeLibraryConfig
|
27 | 29 | private bool _allowFallback = true;
|
28 | 30 | private bool _skipCheck = false;
|
29 | 31 | private bool _logging = false;
|
| 32 | + /// <summary> |
| 33 | + /// search directory -> priority level, 0 is the lowest. |
| 34 | + /// </summary> |
| 35 | + private List<string> _searchDirectories = new List<string>(); |
30 | 36 |
|
31 | 37 | private static void ThrowIfLoaded()
|
32 | 38 | {
|
@@ -120,13 +126,50 @@ public NativeLibraryConfig WithLogs(bool enable = true)
|
120 | 126 | return this;
|
121 | 127 | }
|
122 | 128 |
|
| 129 | + /// <summary> |
| 130 | + /// Add self-defined search directories. Note that the file stucture of the added |
| 131 | + /// directories must be the same as the default directory. Besides, the directory |
| 132 | + /// won't be used recursively. |
| 133 | + /// </summary> |
| 134 | + /// <param name="directories"></param> |
| 135 | + /// <returns></returns> |
| 136 | + public NativeLibraryConfig WithSearchDirectories(IEnumerable<string> directories) |
| 137 | + { |
| 138 | + ThrowIfLoaded(); |
| 139 | + |
| 140 | + _searchDirectories.AddRange(directories); |
| 141 | + return this; |
| 142 | + } |
| 143 | + |
| 144 | + /// <summary> |
| 145 | + /// Add self-defined search directories. Note that the file stucture of the added |
| 146 | + /// directories must be the same as the default directory. Besides, the directory |
| 147 | + /// won't be used recursively. |
| 148 | + /// </summary> |
| 149 | + /// <param name="directory"></param> |
| 150 | + /// <returns></returns> |
| 151 | + public NativeLibraryConfig WithSearchDirectory(string directory) |
| 152 | + { |
| 153 | + ThrowIfLoaded(); |
| 154 | + |
| 155 | + _searchDirectories.Add(directory); |
| 156 | + return this; |
| 157 | + } |
| 158 | + |
123 | 159 | internal static Description CheckAndGatherDescription()
|
124 | 160 | {
|
125 | 161 | if (Instance._allowFallback && Instance._skipCheck)
|
126 | 162 | {
|
127 | 163 | throw new ArgumentException("Cannot skip the check when fallback is allowed.");
|
128 | 164 | }
|
129 |
| - return new Description(Instance._libraryPath, Instance._useCuda, Instance._avxLevel, Instance._allowFallback, Instance._skipCheck, Instance._logging); |
| 165 | + return new Description( |
| 166 | + Instance._libraryPath, |
| 167 | + Instance._useCuda, |
| 168 | + Instance._avxLevel, |
| 169 | + Instance._allowFallback, |
| 170 | + Instance._skipCheck, |
| 171 | + Instance._logging, |
| 172 | + Instance._searchDirectories.Concat(new string[] { "./" }).ToArray()); |
130 | 173 | }
|
131 | 174 |
|
132 | 175 | internal static string AvxLevelToString(AvxLevel level)
|
@@ -183,7 +226,31 @@ public enum AvxLevel
|
183 | 226 | Avx512,
|
184 | 227 | }
|
185 | 228 |
|
186 |
| - internal record Description(string Path, bool UseCuda, AvxLevel AvxLevel, bool AllowFallback, bool SkipCheck, bool Logging); |
| 229 | + internal record Description(string Path, bool UseCuda, AvxLevel AvxLevel, bool AllowFallback, bool SkipCheck, bool Logging, string[] SearchDirectories) |
| 230 | + { |
| 231 | + public override string ToString() |
| 232 | + { |
| 233 | + string avxLevelString = AvxLevel switch |
| 234 | + { |
| 235 | + AvxLevel.None => "NoAVX", |
| 236 | + AvxLevel.Avx => "AVX", |
| 237 | + AvxLevel.Avx2 => "AVX2", |
| 238 | + AvxLevel.Avx512 => "AVX512", |
| 239 | + _ => "Unknown" |
| 240 | + }; |
| 241 | + |
| 242 | + string searchDirectoriesString = "{ " + string.Join(", ", SearchDirectories) + " }"; |
| 243 | + |
| 244 | + return $"NativeLibraryConfig Description:\n" + |
| 245 | + $"- Path: {Path}\n" + |
| 246 | + $"- PreferCuda: {UseCuda}\n" + |
| 247 | + $"- PreferredAvxLevel: {avxLevelString}\n" + |
| 248 | + $"- AllowFallback: {AllowFallback}\n" + |
| 249 | + $"- SkipCheck: {SkipCheck}\n" + |
| 250 | + $"- Logging: {Logging}\n" + |
| 251 | + $"- SearchDirectories and Priorities: {searchDirectoriesString}"; |
| 252 | + } |
| 253 | + } |
187 | 254 | }
|
188 | 255 | #endif
|
189 |
| - } |
| 256 | +} |
0 commit comments