forked from undreamai/LLMUnity
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLLMLib.cs
More file actions
739 lines (672 loc) · 37.3 KB
/
LLMLib.cs
File metadata and controls
739 lines (672 loc) · 37.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
/// @file
/// @brief File implementing the LLM library calls.
/// \cond HIDE
using System;
using System.Collections.Generic;
using System.IO;
using System.Runtime.InteropServices;
using UnityEngine;
namespace LLMUnity
{
/// @ingroup utils
/// <summary>
/// Class implementing a wrapper for a communication stream between Unity and the llama.cpp library (mainly for completion calls and logging).
/// </summary>
public class StreamWrapper
{
LLMLib llmlib;
Callback<string> callback;
IntPtr stringWrapper;
string previousString = "";
string previousCalledString = "";
int previousBufferSize = 0;
bool clearOnUpdate;
public StreamWrapper(LLMLib llmlib, Callback<string> callback, bool clearOnUpdate = false)
{
this.llmlib = llmlib;
this.callback = callback;
this.clearOnUpdate = clearOnUpdate;
stringWrapper = (llmlib?.StringWrapper_Construct()).GetValueOrDefault();
}
/// <summary>
/// Retrieves the content of the stream
/// </summary>
/// <param name="clear">whether to clear the stream after retrieving the content</param>
/// <returns>stream content</returns>
public string GetString(bool clear = false)
{
string result;
int bufferSize = (llmlib?.StringWrapper_GetStringSize(stringWrapper)).GetValueOrDefault();
if (bufferSize <= 1)
{
result = "";
}
else if (previousBufferSize != bufferSize)
{
IntPtr buffer = Marshal.AllocHGlobal(bufferSize);
try
{
llmlib?.StringWrapper_GetString(stringWrapper, buffer, bufferSize, clear);
result = Marshal.PtrToStringAnsi(buffer);
}
finally
{
Marshal.FreeHGlobal(buffer);
}
previousString = result;
}
else
{
result = previousString;
}
previousBufferSize = bufferSize;
return result;
}
/// <summary>
/// Unity Update implementation that retrieves the content and calls the callback if it has changed.
/// </summary>
public void Update()
{
if (stringWrapper == IntPtr.Zero) return;
string result = GetString(clearOnUpdate);
if (result != "" && previousCalledString != result)
{
callback?.Invoke(result);
previousCalledString = result;
}
}
/// <summary>
/// Gets the stringWrapper object to pass to the library.
/// </summary>
/// <returns>stringWrapper object</returns>
public IntPtr GetStringWrapper()
{
return stringWrapper;
}
/// <summary>
/// Deletes the stringWrapper object.
/// </summary>
public void Destroy()
{
if (stringWrapper != IntPtr.Zero) llmlib?.StringWrapper_Delete(stringWrapper);
}
}
/// @ingroup utils
/// <summary>
/// Class implementing a library loader for Unity.
/// Adapted from SkiaForUnity:
/// https://github.com/ammariqais/SkiaForUnity/blob/f43322218c736d1c41f3a3df9355b90db4259a07/SkiaUnity/Assets/SkiaSharp/SkiaSharp-Bindings/SkiaSharp.HarfBuzz.Shared/HarfBuzzSharp.Shared/LibraryLoader.cs
/// </summary>
static class LibraryLoader
{
/// <summary>
/// Allows to retrieve a function delegate for the library
/// </summary>
/// <typeparam name="T">type to cast the function</typeparam>
/// <param name="library">library handle</param>
/// <param name="name">function name</param>
/// <returns>function delegate</returns>
public static T GetSymbolDelegate<T>(IntPtr library, string name) where T : Delegate
{
var symbol = GetSymbol(library, name);
if (symbol == IntPtr.Zero)
throw new EntryPointNotFoundException($"Unable to load symbol '{name}'.");
return Marshal.GetDelegateForFunctionPointer<T>(symbol);
}
/// <summary>
/// Loads the provided library in a cross-platform manner
/// </summary>
/// <param name="libraryName">library path</param>
/// <returns>library handle</returns>
public static IntPtr LoadLibrary(string libraryName)
{
if (string.IsNullOrEmpty(libraryName))
throw new ArgumentNullException(nameof(libraryName));
IntPtr handle;
if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsServer)
handle = Win32.LoadLibrary(libraryName);
else if (Application.platform == RuntimePlatform.LinuxEditor || Application.platform == RuntimePlatform.LinuxPlayer || Application.platform == RuntimePlatform.LinuxServer)
handle = Linux.dlopen(libraryName);
else if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXServer)
handle = Mac.dlopen(libraryName);
else if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.VisionOS)
handle = Mobile.dlopen(libraryName);
else
throw new PlatformNotSupportedException($"Current platform is unknown, unable to load library '{libraryName}'.");
return handle;
}
/// <summary>
/// Retrieve a function delegate for the library in a cross-platform manner
/// </summary>
/// <param name="library">library handle</param>
/// <param name="symbolName">function name</param>
/// <returns>function handle</returns>
public static IntPtr GetSymbol(IntPtr library, string symbolName)
{
if (string.IsNullOrEmpty(symbolName))
throw new ArgumentNullException(nameof(symbolName));
IntPtr handle;
if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsServer)
handle = Win32.GetProcAddress(library, symbolName);
else if (Application.platform == RuntimePlatform.LinuxEditor || Application.platform == RuntimePlatform.LinuxPlayer || Application.platform == RuntimePlatform.LinuxServer)
handle = Linux.dlsym(library, symbolName);
else if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXServer)
handle = Mac.dlsym(library, symbolName);
else if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.VisionOS)
handle = Mobile.dlsym(library, symbolName);
else
throw new PlatformNotSupportedException($"Current platform is unknown, unable to load symbol '{symbolName}' from library {library}.");
return handle;
}
/// <summary>
/// Frees up the library
/// </summary>
/// <param name="library">library handle</param>
public static void FreeLibrary(IntPtr library)
{
if (library == IntPtr.Zero)
return;
if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsServer)
Win32.FreeLibrary(library);
else if (Application.platform == RuntimePlatform.LinuxEditor || Application.platform == RuntimePlatform.LinuxPlayer || Application.platform == RuntimePlatform.LinuxServer)
Linux.dlclose(library);
else if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXServer)
Mac.dlclose(library);
else if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer || Application.platform == RuntimePlatform.VisionOS)
Mobile.dlclose(library);
else
throw new PlatformNotSupportedException($"Current platform is unknown, unable to close library '{library}'.");
}
private static class Mac
{
private const string SystemLibrary = "/usr/lib/libSystem.dylib";
private const int RTLD_LAZY = 1;
private const int RTLD_NOW = 2;
public static IntPtr dlopen(string path, bool lazy = true) =>
dlopen(path, lazy ? RTLD_LAZY : RTLD_NOW);
[DllImport(SystemLibrary)]
public static extern IntPtr dlopen(string path, int mode);
[DllImport(SystemLibrary)]
public static extern IntPtr dlsym(IntPtr handle, string symbol);
[DllImport(SystemLibrary)]
public static extern void dlclose(IntPtr handle);
}
private static class Linux
{
private const string SystemLibrary = "libdl.so";
private const string SystemLibrary2 = "libdl.so.2"; // newer Linux distros use this
private const int RTLD_LAZY = 1;
private const int RTLD_NOW = 2;
private static bool UseSystemLibrary2 = true;
public static IntPtr dlopen(string path, bool lazy = true)
{
try
{
return dlopen2(path, lazy ? RTLD_LAZY : RTLD_NOW);
}
catch (DllNotFoundException)
{
UseSystemLibrary2 = false;
return dlopen1(path, lazy ? RTLD_LAZY : RTLD_NOW);
}
}
public static IntPtr dlsym(IntPtr handle, string symbol)
{
return UseSystemLibrary2 ? dlsym2(handle, symbol) : dlsym1(handle, symbol);
}
public static void dlclose(IntPtr handle)
{
if (UseSystemLibrary2)
dlclose2(handle);
else
dlclose1(handle);
}
[DllImport(SystemLibrary, EntryPoint = "dlopen")]
private static extern IntPtr dlopen1(string path, int mode);
[DllImport(SystemLibrary, EntryPoint = "dlsym")]
private static extern IntPtr dlsym1(IntPtr handle, string symbol);
[DllImport(SystemLibrary, EntryPoint = "dlclose")]
private static extern void dlclose1(IntPtr handle);
[DllImport(SystemLibrary2, EntryPoint = "dlopen")]
private static extern IntPtr dlopen2(string path, int mode);
[DllImport(SystemLibrary2, EntryPoint = "dlsym")]
private static extern IntPtr dlsym2(IntPtr handle, string symbol);
[DllImport(SystemLibrary2, EntryPoint = "dlclose")]
private static extern void dlclose2(IntPtr handle);
}
private static class Win32
{
private const string SystemLibrary = "Kernel32.dll";
[DllImport(SystemLibrary, SetLastError = true, CharSet = CharSet.Ansi)]
public static extern IntPtr LoadLibrary(string lpFileName);
[DllImport(SystemLibrary, SetLastError = true, CharSet = CharSet.Ansi)]
public static extern IntPtr GetProcAddress(IntPtr hModule, string lpProcName);
[DllImport(SystemLibrary, SetLastError = true, CharSet = CharSet.Ansi)]
public static extern void FreeLibrary(IntPtr hModule);
}
private static class Mobile
{
public static IntPtr dlopen(string path) => dlopen(path, 1);
#if UNITY_ANDROID || UNITY_IOS || UNITY_VISIONOS
[DllImport("__Internal")]
public static extern IntPtr dlopen(string filename, int flags);
[DllImport("__Internal")]
public static extern IntPtr dlsym(IntPtr handle, string symbol);
[DllImport("__Internal")]
public static extern int dlclose(IntPtr handle);
#else
public static IntPtr dlopen(string filename, int flags)
{
return default;
}
public static IntPtr dlsym(IntPtr handle, string symbol)
{
return default;
}
public static int dlclose(IntPtr handle)
{
return default;
}
#endif
}
}
/// @ingroup utils
/// <summary>
/// Class implementing the LLM library handling
/// </summary>
public class LLMLib
{
public string architecture { get; private set; }
IntPtr libraryHandle = IntPtr.Zero;
static bool has_avx = false;
static bool has_avx2 = false;
static bool has_avx512 = false;
List<IntPtr> dependencyHandles = new List<IntPtr>();
#if (UNITY_ANDROID || UNITY_IOS || UNITY_VISIONOS) && !UNITY_EDITOR
public LLMLib(string arch)
{
architecture = arch;
}
#if UNITY_ANDROID
public const string LibraryName = "libundreamai_android";
#else
public const string LibraryName = "__Internal";
#endif
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "Logging")]
public static extern void LoggingStatic(IntPtr stringWrapper);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "StopLogging")]
public static extern void StopLoggingStatic();
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "LLM_Construct")]
public static extern IntPtr LLM_ConstructStatic(string command);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "LLM_Delete")]
public static extern void LLM_DeleteStatic(IntPtr LLMObject);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "LLM_StartServer")]
public static extern void LLM_StartServerStatic(IntPtr LLMObject);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "LLM_StopServer")]
public static extern void LLM_StopServerStatic(IntPtr LLMObject);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "LLM_Start")]
public static extern void LLM_StartStatic(IntPtr LLMObject);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "LLM_Started")]
public static extern bool LLM_StartedStatic(IntPtr LLMObject);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "LLM_Stop")]
public static extern void LLM_StopStatic(IntPtr LLMObject);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "LLM_SetTemplate")]
public static extern void LLM_SetTemplateStatic(IntPtr LLMObject, string chatTemplate);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "LLM_SetSSL")]
public static extern void LLM_SetSSLStatic(IntPtr LLMObject, string SSLCert, string SSLKey);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "LLM_Tokenize")]
public static extern void LLM_TokenizeStatic(IntPtr LLMObject, string jsonData, IntPtr stringWrapper);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "LLM_Detokenize")]
public static extern void LLM_DetokenizeStatic(IntPtr LLMObject, string jsonData, IntPtr stringWrapper);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "LLM_Embeddings")]
public static extern void LLM_EmbeddingsStatic(IntPtr LLMObject, string jsonData, IntPtr stringWrapper);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "LLM_Lora_Weight")]
public static extern void LLM_LoraWeightStatic(IntPtr LLMObject, string jsonData, IntPtr stringWrapper);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "LLM_Lora_List")]
public static extern void LLM_LoraListStatic(IntPtr LLMObject, IntPtr stringWrapper);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "LLM_Completion")]
public static extern void LLM_CompletionStatic(IntPtr LLMObject, string jsonData, IntPtr stringWrapper);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "LLM_Slot")]
public static extern void LLM_SlotStatic(IntPtr LLMObject, string jsonData, IntPtr stringWrapper);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "LLM_Cancel")]
public static extern void LLM_CancelStatic(IntPtr LLMObject, int idSlot);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "LLM_Status")]
public static extern int LLM_StatusStatic(IntPtr LLMObject, IntPtr stringWrapper);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "StringWrapper_Construct")]
public static extern IntPtr StringWrapper_ConstructStatic();
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "StringWrapper_Delete")]
public static extern void StringWrapper_DeleteStatic(IntPtr instance);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "StringWrapper_GetStringSize")]
public static extern int StringWrapper_GetStringSizeStatic(IntPtr instance);
[DllImport(LibraryName, CallingConvention = CallingConvention.Cdecl, EntryPoint = "StringWrapper_GetString")]
public static extern void StringWrapper_GetStringStatic(IntPtr instance, IntPtr buffer, int bufferSize, bool clear = false);
public void Logging(IntPtr stringWrapper) { LoggingStatic(stringWrapper); }
public void StopLogging() { StopLoggingStatic(); }
public IntPtr LLM_Construct(string command) { return LLM_ConstructStatic(command); }
public void LLM_Delete(IntPtr LLMObject) { LLM_DeleteStatic(LLMObject); }
public void LLM_StartServer(IntPtr LLMObject) { LLM_StartServerStatic(LLMObject); }
public void LLM_StopServer(IntPtr LLMObject) { LLM_StopServerStatic(LLMObject); }
public void LLM_Start(IntPtr LLMObject) { LLM_StartStatic(LLMObject); }
public bool LLM_Started(IntPtr LLMObject) { return LLM_StartedStatic(LLMObject); }
public void LLM_Stop(IntPtr LLMObject) { LLM_StopStatic(LLMObject); }
public void LLM_SetTemplate(IntPtr LLMObject, string chatTemplate) { LLM_SetTemplateStatic(LLMObject, chatTemplate); }
public void LLM_SetSSL(IntPtr LLMObject, string SSLCert, string SSLKey) { LLM_SetSSLStatic(LLMObject, SSLCert, SSLKey); }
public void LLM_Tokenize(IntPtr LLMObject, string jsonData, IntPtr stringWrapper) { LLM_TokenizeStatic(LLMObject, jsonData, stringWrapper); }
public void LLM_Detokenize(IntPtr LLMObject, string jsonData, IntPtr stringWrapper) { LLM_DetokenizeStatic(LLMObject, jsonData, stringWrapper); }
public void LLM_Embeddings(IntPtr LLMObject, string jsonData, IntPtr stringWrapper) { LLM_EmbeddingsStatic(LLMObject, jsonData, stringWrapper); }
public void LLM_LoraWeight(IntPtr LLMObject, string jsonData, IntPtr stringWrapper) { LLM_LoraWeightStatic(LLMObject, jsonData, stringWrapper); }
public void LLM_LoraList(IntPtr LLMObject, IntPtr stringWrapper) { LLM_LoraListStatic(LLMObject, stringWrapper); }
public void LLM_Completion(IntPtr LLMObject, string jsonData, IntPtr stringWrapper) { LLM_CompletionStatic(LLMObject, jsonData, stringWrapper); }
public void LLM_Slot(IntPtr LLMObject, string jsonData, IntPtr stringWrapper) { LLM_SlotStatic(LLMObject, jsonData, stringWrapper); }
public void LLM_Cancel(IntPtr LLMObject, int idSlot) { LLM_CancelStatic(LLMObject, idSlot); }
public int LLM_Status(IntPtr LLMObject, IntPtr stringWrapper) { return LLM_StatusStatic(LLMObject, stringWrapper); }
public IntPtr StringWrapper_Construct() { return StringWrapper_ConstructStatic(); }
public void StringWrapper_Delete(IntPtr instance) { StringWrapper_DeleteStatic(instance); }
public int StringWrapper_GetStringSize(IntPtr instance) { return StringWrapper_GetStringSizeStatic(instance); }
public void StringWrapper_GetString(IntPtr instance, IntPtr buffer, int bufferSize, bool clear = false) { StringWrapper_GetStringStatic(instance, buffer, bufferSize, clear); }
#else
static bool has_avx_set = false;
static readonly object staticLock = new object();
static LLMLib()
{
lock (staticLock)
{
if (has_avx_set) return;
string archCheckerPath = GetArchitectureCheckerPath();
if (archCheckerPath != null)
{
IntPtr archCheckerHandle = LibraryLoader.LoadLibrary(archCheckerPath);
if (archCheckerHandle == IntPtr.Zero)
{
LLMUnitySetup.LogError($"Failed to load library {archCheckerPath}.");
}
else
{
try
{
has_avx = LibraryLoader.GetSymbolDelegate<HasArchDelegate>(archCheckerHandle, "has_avx")();
has_avx2 = LibraryLoader.GetSymbolDelegate<HasArchDelegate>(archCheckerHandle, "has_avx2")();
has_avx512 = LibraryLoader.GetSymbolDelegate<HasArchDelegate>(archCheckerHandle, "has_avx512")();
LibraryLoader.FreeLibrary(archCheckerHandle);
}
catch (Exception e)
{
LLMUnitySetup.LogError($"{e.GetType()}: {e.Message}");
}
}
}
has_avx_set = true;
}
}
/// <summary>
/// Loads the library and function handles for the defined architecture
/// </summary>
/// <param name="arch">archtecture</param>
/// <exception cref="Exception"></exception>
public LLMLib(string arch)
{
architecture = arch;
foreach (string dependency in GetArchitectureDependencies(arch))
{
LLMUnitySetup.Log($"Loading {dependency}");
dependencyHandles.Add(LibraryLoader.LoadLibrary(dependency));
}
libraryHandle = LibraryLoader.LoadLibrary(GetArchitecturePath(arch));
if (libraryHandle == IntPtr.Zero)
{
throw new Exception($"Failed to load library {arch}.");
}
LLM_Construct = LibraryLoader.GetSymbolDelegate<LLM_ConstructDelegate>(libraryHandle, "LLM_Construct");
LLM_Delete = LibraryLoader.GetSymbolDelegate<LLM_DeleteDelegate>(libraryHandle, "LLM_Delete");
LLM_StartServer = LibraryLoader.GetSymbolDelegate<LLM_StartServerDelegate>(libraryHandle, "LLM_StartServer");
LLM_StopServer = LibraryLoader.GetSymbolDelegate<LLM_StopServerDelegate>(libraryHandle, "LLM_StopServer");
LLM_Start = LibraryLoader.GetSymbolDelegate<LLM_StartDelegate>(libraryHandle, "LLM_Start");
LLM_Started = LibraryLoader.GetSymbolDelegate<LLM_StartedDelegate>(libraryHandle, "LLM_Started");
LLM_Stop = LibraryLoader.GetSymbolDelegate<LLM_StopDelegate>(libraryHandle, "LLM_Stop");
LLM_SetTemplate = LibraryLoader.GetSymbolDelegate<LLM_SetTemplateDelegate>(libraryHandle, "LLM_SetTemplate");
LLM_SetSSL = LibraryLoader.GetSymbolDelegate<LLM_SetSSLDelegate>(libraryHandle, "LLM_SetSSL");
LLM_Tokenize = LibraryLoader.GetSymbolDelegate<LLM_TokenizeDelegate>(libraryHandle, "LLM_Tokenize");
LLM_Detokenize = LibraryLoader.GetSymbolDelegate<LLM_DetokenizeDelegate>(libraryHandle, "LLM_Detokenize");
LLM_Embeddings = LibraryLoader.GetSymbolDelegate<LLM_EmbeddingsDelegate>(libraryHandle, "LLM_Embeddings");
LLM_LoraWeight = LibraryLoader.GetSymbolDelegate<LLM_LoraWeightDelegate>(libraryHandle, "LLM_Lora_Weight");
LLM_LoraList = LibraryLoader.GetSymbolDelegate<LLM_LoraListDelegate>(libraryHandle, "LLM_Lora_List");
LLM_Completion = LibraryLoader.GetSymbolDelegate<LLM_CompletionDelegate>(libraryHandle, "LLM_Completion");
LLM_Slot = LibraryLoader.GetSymbolDelegate<LLM_SlotDelegate>(libraryHandle, "LLM_Slot");
LLM_Cancel = LibraryLoader.GetSymbolDelegate<LLM_CancelDelegate>(libraryHandle, "LLM_Cancel");
LLM_Status = LibraryLoader.GetSymbolDelegate<LLM_StatusDelegate>(libraryHandle, "LLM_Status");
StringWrapper_Construct = LibraryLoader.GetSymbolDelegate<StringWrapper_ConstructDelegate>(libraryHandle, "StringWrapper_Construct");
StringWrapper_Delete = LibraryLoader.GetSymbolDelegate<StringWrapper_DeleteDelegate>(libraryHandle, "StringWrapper_Delete");
StringWrapper_GetStringSize = LibraryLoader.GetSymbolDelegate<StringWrapper_GetStringSizeDelegate>(libraryHandle, "StringWrapper_GetStringSize");
StringWrapper_GetString = LibraryLoader.GetSymbolDelegate<StringWrapper_GetStringDelegate>(libraryHandle, "StringWrapper_GetString");
Logging = LibraryLoader.GetSymbolDelegate<LoggingDelegate>(libraryHandle, "Logging");
StopLogging = LibraryLoader.GetSymbolDelegate<StopLoggingDelegate>(libraryHandle, "StopLogging");
// editor only
#if UNITY_EDITOR
var symbol = LibraryLoader.GetSymbol(libraryHandle, "LLM_IsDebuggerAttached");
LLM_IsDebuggerAttached = (symbol != IntPtr.Zero) ? Marshal.GetDelegateForFunctionPointer<LLM_IsDebuggerAttachedDelegate>(symbol) : null;
#endif
}
/// <summary>
/// Gets the path of a library that allows to detect the underlying CPU (Windows / Linux).
/// </summary>
/// <returns>architecture checker library path</returns>
public static string GetArchitectureCheckerPath()
{
string filename;
if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsServer)
{
filename = $"windows-archchecker/archchecker.dll";
}
else if (Application.platform == RuntimePlatform.LinuxEditor || Application.platform == RuntimePlatform.LinuxPlayer || Application.platform == RuntimePlatform.LinuxServer)
{
filename = $"linux-archchecker/libarchchecker.so";
}
else
{
return null;
}
return Path.Combine(LLMUnitySetup.libraryPath, filename);
}
/// <summary>
/// Gets additional dependencies for the specified architecture.
/// </summary>
/// <param name="arch">architecture</param>
/// <returns>paths of dependency dlls</returns>
public static List<string> GetArchitectureDependencies(string arch)
{
List<string> dependencies = new List<string>();
if (arch == "cuda-cu12.2.0-full")
{
if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsServer)
{
dependencies.Add(Path.Combine(LLMUnitySetup.libraryPath, $"windows-{arch}/cudart64_12.dll"));
dependencies.Add(Path.Combine(LLMUnitySetup.libraryPath, $"windows-{arch}/cublasLt64_12.dll"));
dependencies.Add(Path.Combine(LLMUnitySetup.libraryPath, $"windows-{arch}/cublas64_12.dll"));
}
}
else if (arch == "vulkan")
{
if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsServer)
{
dependencies.Add(Path.Combine(LLMUnitySetup.libraryPath, $"windows-{arch}/vulkan-1.dll"));
}
else if (Application.platform == RuntimePlatform.LinuxEditor || Application.platform == RuntimePlatform.LinuxPlayer || Application.platform == RuntimePlatform.LinuxServer)
{
dependencies.Add(Path.Combine(LLMUnitySetup.libraryPath, $"linux-{arch}/libvulkan.so.1"));
}
}
return dependencies;
}
/// <summary>
/// Gets the path of the llama.cpp library for the specified architecture.
/// </summary>
/// <param name="arch">architecture</param>
/// <returns>llama.cpp library path</returns>
public static string GetArchitecturePath(string arch)
{
string filename;
if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsServer)
{
filename = $"windows-{arch}/undreamai_windows-{arch}.dll";
}
else if (Application.platform == RuntimePlatform.LinuxEditor || Application.platform == RuntimePlatform.LinuxPlayer || Application.platform == RuntimePlatform.LinuxServer)
{
filename = $"linux-{arch}/libundreamai_linux-{arch}.so";
}
else if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXPlayer || Application.platform == RuntimePlatform.OSXServer)
{
filename = $"macos-{arch}/libundreamai_macos-{arch}.dylib";
}
else
{
string error = "Unknown OS";
LLMUnitySetup.LogError(error);
throw new Exception(error);
}
return Path.Combine(LLMUnitySetup.libraryPath, filename);
}
public delegate bool HasArchDelegate();
public delegate void LoggingDelegate(IntPtr stringWrapper);
public delegate void StopLoggingDelegate();
public delegate IntPtr LLM_ConstructDelegate(string command);
public delegate void LLM_DeleteDelegate(IntPtr LLMObject);
public delegate void LLM_StartServerDelegate(IntPtr LLMObject);
public delegate void LLM_StopServerDelegate(IntPtr LLMObject);
public delegate void LLM_StartDelegate(IntPtr LLMObject);
public delegate bool LLM_StartedDelegate(IntPtr LLMObject);
public delegate void LLM_StopDelegate(IntPtr LLMObject);
public delegate void LLM_SetTemplateDelegate(IntPtr LLMObject, string chatTemplate);
public delegate void LLM_SetSSLDelegate(IntPtr LLMObject, string SSLCert, string SSLKey);
public delegate void LLM_TokenizeDelegate(IntPtr LLMObject, string jsonData, IntPtr stringWrapper);
public delegate void LLM_DetokenizeDelegate(IntPtr LLMObject, string jsonData, IntPtr stringWrapper);
public delegate void LLM_EmbeddingsDelegate(IntPtr LLMObject, string jsonData, IntPtr stringWrapper);
public delegate void LLM_LoraWeightDelegate(IntPtr LLMObject, string jsonData, IntPtr stringWrapper);
public delegate void LLM_LoraListDelegate(IntPtr LLMObject, IntPtr stringWrapper);
public delegate void LLM_CompletionDelegate(IntPtr LLMObject, string jsonData, IntPtr stringWrapper);
public delegate void LLM_SlotDelegate(IntPtr LLMObject, string jsonData, IntPtr stringWrapper);
public delegate void LLM_CancelDelegate(IntPtr LLMObject, int idSlot);
public delegate int LLM_StatusDelegate(IntPtr LLMObject, IntPtr stringWrapper);
public delegate IntPtr StringWrapper_ConstructDelegate();
public delegate void StringWrapper_DeleteDelegate(IntPtr instance);
public delegate int StringWrapper_GetStringSizeDelegate(IntPtr instance);
public delegate void StringWrapper_GetStringDelegate(IntPtr instance, IntPtr buffer, int bufferSize, bool clear = false);
#if UNITY_EDITOR
public delegate bool LLM_IsDebuggerAttachedDelegate();
#endif
public LoggingDelegate Logging;
public StopLoggingDelegate StopLogging;
public LLM_ConstructDelegate LLM_Construct;
public LLM_DeleteDelegate LLM_Delete;
public LLM_StartServerDelegate LLM_StartServer;
public LLM_StopServerDelegate LLM_StopServer;
public LLM_StartDelegate LLM_Start;
public LLM_StartedDelegate LLM_Started;
public LLM_StopDelegate LLM_Stop;
public LLM_SetTemplateDelegate LLM_SetTemplate;
public LLM_SetSSLDelegate LLM_SetSSL;
public LLM_TokenizeDelegate LLM_Tokenize;
public LLM_DetokenizeDelegate LLM_Detokenize;
public LLM_CompletionDelegate LLM_Completion;
public LLM_EmbeddingsDelegate LLM_Embeddings;
public LLM_LoraWeightDelegate LLM_LoraWeight;
public LLM_LoraListDelegate LLM_LoraList;
public LLM_SlotDelegate LLM_Slot;
public LLM_CancelDelegate LLM_Cancel;
public LLM_StatusDelegate LLM_Status;
public StringWrapper_ConstructDelegate StringWrapper_Construct;
public StringWrapper_DeleteDelegate StringWrapper_Delete;
public StringWrapper_GetStringSizeDelegate StringWrapper_GetStringSize;
public StringWrapper_GetStringDelegate StringWrapper_GetString;
#if UNITY_EDITOR
public LLM_IsDebuggerAttachedDelegate LLM_IsDebuggerAttached;
#endif
#endif
/// <summary>
/// Identifies the possible architectures that we can use based on the OS and GPU usage
/// </summary>
/// <param name="gpu">whether to allow GPU architectures</param>
/// <returns>possible architectures</returns>
public static List<string> PossibleArchitectures(bool gpu = false)
{
List<string> architectures = new List<string>();
if (Application.platform == RuntimePlatform.WindowsEditor || Application.platform == RuntimePlatform.WindowsPlayer || Application.platform == RuntimePlatform.WindowsServer ||
Application.platform == RuntimePlatform.LinuxEditor || Application.platform == RuntimePlatform.LinuxPlayer || Application.platform == RuntimePlatform.LinuxServer)
{
if (gpu)
{
if (LLMUnitySetup.FullLlamaLib)
{
architectures.Add("cuda-cu12.2.0-full");
}
else
{
architectures.Add("cuda-cu12.2.0");
}
architectures.Add("hip");
architectures.Add("vulkan");
}
if (has_avx512) architectures.Add("avx512");
if (has_avx2) architectures.Add("avx2");
if (has_avx) architectures.Add("avx");
architectures.Add("noavx");
}
else if (Application.platform == RuntimePlatform.OSXEditor || Application.platform == RuntimePlatform.OSXPlayer)
{
architectures.Add("acc");
architectures.Add("no_acc");
}
else if (Application.platform == RuntimePlatform.Android)
{
architectures.Add("android");
}
else if (Application.platform == RuntimePlatform.IPhonePlayer)
{
architectures.Add("ios");
}
else if (Application.platform == RuntimePlatform.VisionOS)
{
architectures.Add("visionos");
}
else
{
string error = "Unknown OS";
LLMUnitySetup.LogError(error);
throw new Exception(error);
}
return architectures;
}
/// <summary>
/// Allows to retrieve a string from the library (Unity only allows marshalling of chars)
/// </summary>
/// <param name="stringWrapper">string wrapper pointer</param>
/// <returns>retrieved string</returns>
public string GetStringWrapperResult(IntPtr stringWrapper)
{
string result = "";
int bufferSize = StringWrapper_GetStringSize(stringWrapper);
if (bufferSize > 1)
{
IntPtr buffer = Marshal.AllocHGlobal(bufferSize);
try
{
StringWrapper_GetString(stringWrapper, buffer, bufferSize);
result = Marshal.PtrToStringAnsi(buffer);
}
finally
{
Marshal.FreeHGlobal(buffer);
}
}
return result;
}
/// <summary>
/// Destroys the LLM library
/// </summary>
public void Destroy()
{
if (libraryHandle != IntPtr.Zero) LibraryLoader.FreeLibrary(libraryHandle);
foreach (IntPtr dependencyHandle in dependencyHandles) LibraryLoader.FreeLibrary(dependencyHandle);
}
}
}
/// \endcond