-
-
Notifications
You must be signed in to change notification settings - Fork 178
Expand file tree
/
Copy pathLLMUnitySetup.cs
More file actions
766 lines (678 loc) · 31.2 KB
/
LLMUnitySetup.cs
File metadata and controls
766 lines (678 loc) · 31.2 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
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
/// @file
/// @brief File implementing helper functions for setup and process management.
using UnityEditor;
using System.IO;
using UnityEngine;
using System.Threading.Tasks;
using System;
using System.IO.Compression;
using System.Collections.Generic;
using UnityEngine.Networking;
using System.Text.RegularExpressions;
using UndreamAI.LlamaLib;
/// @defgroup llm LLM
/// @defgroup utils Utils
namespace LLMUnity
{
/// \cond HIDE
public sealed class FloatAttribute : PropertyAttribute
{
public float Min { get; private set; }
public float Max { get; private set; }
public FloatAttribute(float min, float max)
{
Min = min;
Max = max;
}
}
public sealed class IntAttribute : PropertyAttribute
{
public int Min { get; private set; }
public int Max { get; private set; }
public IntAttribute(int min, int max)
{
Min = min;
Max = max;
}
}
[AttributeUsage(AttributeTargets.Field, Inherited = true, AllowMultiple = false)]
public class DynamicRangeAttribute : PropertyAttribute
{
public readonly string minVariable;
public readonly string maxVariable;
public bool intOrFloat;
public DynamicRangeAttribute(string minVariable, string maxVariable, bool intOrFloat)
{
this.minVariable = minVariable;
this.maxVariable = maxVariable;
this.intOrFloat = intOrFloat;
}
}
public class LLMAttribute : PropertyAttribute {}
public class LocalRemoteAttribute : PropertyAttribute {}
public class RemoteAttribute : PropertyAttribute {}
public class LocalAttribute : PropertyAttribute {}
public class ModelAttribute : PropertyAttribute {}
public class ModelExtrasAttribute : PropertyAttribute {}
public class ChatAttribute : PropertyAttribute {}
public class LLMUnityAttribute : PropertyAttribute {}
public class AdvancedAttribute : PropertyAttribute {}
public class LLMAdvancedAttribute : AdvancedAttribute {}
public class ModelAdvancedAttribute : AdvancedAttribute {}
public class ChatAdvancedAttribute : AdvancedAttribute {}
public class Overflow1Attribute : PropertyAttribute {}
public class Overflow2Attribute : PropertyAttribute {}
public class LLMUnityException : Exception
{
public LLMUnityException(string message = "") : base(message) {}
}
[Serializable]
public struct StringPair
{
public string source;
public string target;
}
[Serializable]
public class ListStringPair
{
public List<StringPair> pairs;
}
/// \endcond
/// @ingroup utils
/// <summary>
/// Class implementing helper functions for setup and process management.
/// </summary>
public class LLMUnitySetup
{
// DON'T CHANGE! the version is autocompleted with a GitHub action
/// <summary> LLM for Unity version </summary>
public static string Version = "v3.0.2";
/// <summary> LlamaLib version </summary>
public static string LlamaLibVersion = "v2.0.4";
/// <summary> LlamaLib release url </summary>
public static string LlamaLibReleaseURL = $"https://github.com/undreamai/LlamaLib/releases/download/{LlamaLibVersion}";
/// <summary> LlamaLib name </summary>
public static string libraryName = $"LlamaLib-{LlamaLibVersion}";
/// <summary> LlamaLib path </summary>
public static string libraryPath = GetAssetPath(libraryName);
/// <summary> LlamaLib url </summary>
public static string LlamaLibURL = $"{LlamaLibReleaseURL}/{libraryName}.zip";
/// <summary> LLMnity store path </summary>
public static string LLMUnityStore = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.ApplicationData), "LLMUnity");
/// <summary> Model download path </summary>
public static string modelDownloadPath = Path.Combine(LLMUnityStore, "models");
/// <summary> cache download path </summary>
public static string cacheDownloadPath = Path.Combine(LLMUnityStore, "cache");
/// <summary> Path of file with build information for runtime </summary>
public static string LLMManagerPath = GetAssetPath("LLMManager.json");
/// <summary> Default models for download </summary>
[HideInInspector]
public static readonly Dictionary<string, (string, string, string)[]> modelOptions = new Dictionary<string, (string, string, string)[]>()
{
{"Large models (more than 10B)", new(string, string, string)[]
{
("Gemma 3 12B", "https://huggingface.co/lmstudio-community/gemma-3-12b-it-GGUF/resolve/main/gemma-3-12b-it-Q4_K_M.gguf", "https://ai.google.dev/gemma/terms"),
("Phi 4 14B", "https://huggingface.co/bartowski/phi-4-GGUF/resolve/main/phi-4-Q4_K_M.gguf", null),
("Qwen 3 14B", "https://huggingface.co/unsloth/Qwen3-14B-GGUF/resolve/main/Qwen3-14B-Q4_K_M.gguf", null),
("DeepSeek R1 Distill Qwen 14B", "https://huggingface.co/lmstudio-community/DeepSeek-R1-Distill-Qwen-14B-GGUF/resolve/main/DeepSeek-R1-Distill-Qwen-14B-Q4_K_M.gguf", null),
}},
{"Medium models (up to 10B)", new(string, string, string)[]
{
("Llama 3.1 8B", "https://huggingface.co/bartowski/Meta-Llama-3.1-8B-Instruct-GGUF/resolve/main/Meta-Llama-3.1-8B-Instruct-Q4_K_M.gguf", "https://huggingface.co/meta-llama/Meta-Llama-3.1-8B/blob/main/LICENSE"),
("Qwen 3 8B", "https://huggingface.co/unsloth/Qwen3-8B-GGUF/resolve/main/Qwen3-8B-Q4_K_M.gguf", null),
("DeepSeek R1 Distill Llama 8B", "https://huggingface.co/lmstudio-community/DeepSeek-R1-Distill-Llama-8B-GGUF/resolve/main/DeepSeek-R1-Distill-Llama-8B-Q4_K_M.gguf", null),
("DeepSeek R1 Distill Qwen 7B", "https://huggingface.co/lmstudio-community/DeepSeek-R1-Distill-Qwen-7B-GGUF/resolve/main/DeepSeek-R1-Distill-Qwen-7B-Q4_K_M.gguf", null),
("Gemma 2 9B it", "https://huggingface.co/bartowski/gemma-2-9b-it-GGUF/resolve/main/gemma-2-9b-it-Q4_K_M.gguf", "https://ai.google.dev/gemma/terms"),
("Mistral 7B Instruct v0.2", "https://huggingface.co/TheBloke/Mistral-7B-Instruct-v0.2-GGUF/resolve/main/mistral-7b-instruct-v0.2.Q4_K_M.gguf", null),
("OpenHermes 2.5 7B", "https://huggingface.co/TheBloke/OpenHermes-2.5-Mistral-7B-GGUF/resolve/main/openhermes-2.5-mistral-7b.Q4_K_M.gguf", null),
}},
{"Small models (up to 5B)", new(string, string, string)[]
{
("Llama 3.2 3B", "https://huggingface.co/hugging-quants/Llama-3.2-3B-Instruct-Q4_K_M-GGUF/resolve/main/llama-3.2-3b-instruct-q4_k_m.gguf", "https://huggingface.co/meta-llama/Llama-3.2-1B/blob/main/LICENSE.txt"),
("Gemma 3 4B", "https://huggingface.co/lmstudio-community/gemma-3-4b-it-GGUF/resolve/main/gemma-3-4b-it-Q4_K_M.gguf", "https://ai.google.dev/gemma/terms"),
("Phi 4 4B", "https://huggingface.co/bartowski/microsoft_Phi-4-mini-instruct-GGUF/resolve/main/microsoft_Phi-4-mini-instruct-Q4_K_M.gguf", null),
("Qwen 3 4B", "https://huggingface.co/unsloth/Qwen3-4B-GGUF/resolve/main/Qwen3-4B-Q4_K_M.gguf", null),
}},
{"Tiny models (up to 2B)", new(string, string, string)[]
{
("Llama 3.2 1B", "https://huggingface.co/hugging-quants/Llama-3.2-1B-Instruct-Q4_K_M-GGUF/resolve/main/llama-3.2-1b-instruct-q4_k_m.gguf", "https://huggingface.co/meta-llama/Llama-3.2-1B/blob/main/LICENSE.txt"),
("Gemma 3 1B", "https://huggingface.co/lmstudio-community/gemma-3-1b-it-GGUF/resolve/main/gemma-3-1b-it-Q4_K_M.gguf", "https://ai.google.dev/gemma/terms"),
("Qwen 3 1.7B", "https://huggingface.co/unsloth/Qwen3-1.7B-GGUF/resolve/main/Qwen3-1.7B-Q4_K_M.gguf", null),
("Qwen 3 0.6B", "https://huggingface.co/unsloth/Qwen3-0.6B-GGUF/resolve/main/Qwen3-0.6B-Q4_K_M.gguf", null),
("DeepSeek R1 Distill Qwen 1.5B", "https://huggingface.co/lmstudio-community/DeepSeek-R1-Distill-Qwen-1.5B-GGUF/resolve/main/DeepSeek-R1-Distill-Qwen-1.5B-Q4_K_M.gguf", null),
}},
{"RAG models", new(string, string, string)[]
{
("All MiniLM L12 v2", "https://huggingface.co/leliuga/all-MiniLM-L12-v2-GGUF/resolve/main/all-MiniLM-L12-v2.Q4_K_M.gguf", null),
("BGE large en v1.5", "https://huggingface.co/CompendiumLabs/bge-large-en-v1.5-gguf/resolve/main/bge-large-en-v1.5-q4_k_m.gguf", null),
("BGE base en v1.5", "https://huggingface.co/CompendiumLabs/bge-base-en-v1.5-gguf/resolve/main/bge-base-en-v1.5-q4_k_m.gguf", null),
("BGE small en v1.5", "https://huggingface.co/CompendiumLabs/bge-small-en-v1.5-gguf/resolve/main/bge-small-en-v1.5-q4_k_m.gguf", null),
}},
};
/// \cond HIDE
[LLMUnity] public static DebugModeType DebugMode = DebugModeType.All;
static string DebugModeKey = "DebugMode";
public static bool CUBLAS = false;
static string CUBLASKey = "CUBLAS";
public static bool AndroidVulkan = false;
static string AndroidVulkanKey = "AndroidVulkan";
static List<Action<string>> errorCallbacks = new List<Action<string>>();
static readonly object lockObject = new object();
static Dictionary<string, Task> androidExtractTasks = new Dictionary<string, Task>();
public enum DebugModeType
{
Debug,
All,
Warning,
Error,
None
}
public static void Log(string message)
{
if ((int)DebugMode > (int)DebugModeType.All) return;
Debug.Log(message);
}
public static void LogWarning(string message)
{
if ((int)DebugMode > (int)DebugModeType.Warning) return;
Debug.LogWarning(message);
}
public static void LogError(string message, bool throwException = false)
{
if ((int)DebugMode > (int)DebugModeType.Error) return;
Debug.LogError(message);
foreach (Action<string> errorCallback in errorCallbacks) errorCallback(message);
if (throwException) throw new LLMUnityException(message);
}
static void LoadPlayerPrefs()
{
DebugMode = (DebugModeType)PlayerPrefs.GetInt(DebugModeKey, (int)DebugModeType.All);
CUBLAS = PlayerPrefs.GetInt(CUBLASKey, 0) == 1;
AndroidVulkan = PlayerPrefs.GetInt(AndroidVulkanKey, 0) == 1;
}
public static void SetDebugMode(DebugModeType newDebugMode)
{
if (DebugMode == newDebugMode) return;
DebugMode = newDebugMode;
PlayerPrefs.SetInt(DebugModeKey, (int)DebugMode);
PlayerPrefs.Save();
}
#if UNITY_EDITOR
public static void SetCUBLAS(bool value)
{
if (CUBLAS == value) return;
CUBLAS = value;
PlayerPrefs.SetInt(CUBLASKey, value ? 1 : 0);
PlayerPrefs.Save();
}
public static void SetAndroidVulkan(bool value)
{
if (AndroidVulkan == value) return;
AndroidVulkan = value;
PlayerPrefs.SetInt(AndroidVulkanKey, value ? 1 : 0);
PlayerPrefs.Save();
}
#endif
public static string GetAssetPath(string relPath = "")
{
string assetsDir = Application.platform == RuntimePlatform.Android ? Application.persistentDataPath : Application.streamingAssetsPath;
return Path.Combine(assetsDir, relPath).Replace('\\', '/');
}
public static string GetDownloadAssetPath(string relPath = "")
{
string assetsDir = Application.streamingAssetsPath;
bool isVisionOS = false;
#if UNITY_2022_3_OR_NEWER
isVisionOS = Application.platform == RuntimePlatform.VisionOS;
#endif
if (Application.platform == RuntimePlatform.Android || Application.platform == RuntimePlatform.IPhonePlayer || isVisionOS)
{
assetsDir = Application.persistentDataPath;
}
return Path.Combine(assetsDir, relPath).Replace('\\', '/');
}
static void InitializeOnLoadCommon()
{
#if UNITY_EDITOR || !((UNITY_ANDROID || UNITY_IOS || UNITY_VISIONOS))
LlamaLib.baseLibraryPath = Path.Combine(libraryPath, LlamaLib.GetPlatform(), "native");
#endif
}
#if UNITY_EDITOR
[InitializeOnLoadMethod]
static async Task InitializeOnLoad()
{
LoadPlayerPrefs();
LlamaLib.libraryExclusion = new List<string>(){CUBLAS ? "tinyblas" : "cublas"};
InitializeOnLoadCommon();
await DownloadLibrary();
}
#else
[RuntimeInitializeOnLoadMethod(RuntimeInitializeLoadType.BeforeSceneLoad)]
static void InitializeOnLoad()
{
InitializeOnLoadCommon();
}
#endif
static Dictionary<string, ResumingWebClient> downloadClients = new Dictionary<string, ResumingWebClient>();
public static void CancelDownload(string savePath)
{
if (!downloadClients.ContainsKey(savePath)) return;
downloadClients[savePath].CancelDownloadAsync();
downloadClients.Remove(savePath);
}
public static async Task DownloadFile(
string fileUrl, string savePath, bool overwrite = false,
Action<string> callback = null, Action<float> progressCallback = null, bool debug = true
)
{
if (File.Exists(savePath) && !overwrite)
{
if(debug) Log($"File already exists at: {savePath}");
}
else
{
if(debug) Log($"Downloading {fileUrl} to {savePath}...");
string tmpPath = Path.Combine(Application.temporaryCachePath, Path.GetFileName(savePath));
ResumingWebClient client = new ResumingWebClient();
downloadClients[savePath] = client;
if (File.Exists(tmpPath) && overwrite) File.Delete(tmpPath);
await client.DownloadFileTaskAsyncResume(new Uri(fileUrl), tmpPath, !overwrite, progressCallback);
downloadClients.Remove(savePath);
#if UNITY_EDITOR
AssetDatabase.StartAssetEditing();
#endif
Directory.CreateDirectory(Path.GetDirectoryName(savePath));
if (File.Exists(savePath)) File.Delete(savePath);
File.Move(tmpPath, savePath);
#if UNITY_EDITOR
AssetDatabase.StopAssetEditing();
#endif
if(debug) Log($"Download complete!");
}
progressCallback?.Invoke(1f);
callback?.Invoke(savePath);
}
public static async Task AndroidExtractFile(string assetName, bool overwrite = false, bool log = true, int chunkSize = 1024 * 1024)
{
Task extractionTask;
lock (lockObject)
{
if (!androidExtractTasks.TryGetValue(assetName, out extractionTask))
{
#if !UNITY_EDITOR && UNITY_ANDROID
extractionTask = AndroidExtractFileOnce(assetName, overwrite, log, chunkSize);
#else
extractionTask = Task.CompletedTask;
#endif
androidExtractTasks[assetName] = extractionTask;
}
}
await extractionTask;
}
public static async Task AndroidExtractFileOnce(string assetName, bool overwrite = false, bool log = true, int chunkSize = 1024 * 1024)
{
string source = "jar:file://" + Application.dataPath + "!/assets/" + assetName;
string target = GetAssetPath(assetName);
if (!overwrite && File.Exists(target))
{
if (log) Log($"File {target} already exists");
return;
}
Log($"Extracting {source} to {target}");
// UnityWebRequest to read the file from StreamingAssets
UnityWebRequest www = UnityWebRequest.Get(source);
// Send the request and await its completion
var operation = www.SendWebRequest();
while (!operation.isDone) await Task.Delay(1);
if (www.result != UnityWebRequest.Result.Success)
{
LogError("Failed to load file from StreamingAssets: " + www.error);
}
else
{
byte[] buffer = new byte[chunkSize];
using (Stream responseStream = new MemoryStream(www.downloadHandler.data))
using (FileStream fileStream = new FileStream(target, FileMode.Create, FileAccess.Write))
{
int bytesRead;
while ((bytesRead = await responseStream.ReadAsync(buffer, 0, buffer.Length)) > 0)
{
await fileStream.WriteAsync(buffer, 0, bytesRead);
}
}
}
}
public static async Task AndroidExtractAsset(string path, bool overwrite = false)
{
if (Application.platform != RuntimePlatform.Android) return;
await AndroidExtractFile(Path.GetFileName(path), overwrite);
}
public static string GetFullPath(string path)
{
return Path.GetFullPath(path).Replace('\\', '/');
}
public static bool IsSubPath(string childPath, string parentPath)
{
return GetFullPath(childPath).StartsWith(GetFullPath(parentPath), StringComparison.OrdinalIgnoreCase);
}
public static string RelativePath(string fullPath, string basePath)
{
// Get the full paths and replace backslashes with forward slashes (or vice versa)
string fullParentPath = GetFullPath(basePath).TrimEnd('/');
string fullChildPath = GetFullPath(fullPath);
string relativePath = fullChildPath;
if (fullChildPath.StartsWith(fullParentPath, StringComparison.OrdinalIgnoreCase))
{
relativePath = fullChildPath.Substring(fullParentPath.Length);
while (relativePath.StartsWith("/")) relativePath = relativePath.Substring(1);
}
return relativePath;
}
public static string SearchDirectory(string directory, string targetFileName)
{
string[] files = Directory.GetFiles(directory, targetFileName);
if (files.Length > 0) return files[0];
string[] subdirectories = Directory.GetDirectories(directory);
foreach (var subdirectory in subdirectories)
{
string result = SearchDirectory(subdirectory, targetFileName);
if (result != null) return result;
}
return null;
}
#if UNITY_EDITOR
[HideInInspector] public static float libraryProgress = 1;
public static void CreateEmptyFile(string path)
{
File.Create(path).Dispose();
}
static void ExtractInsideDirectory(string zipPath, string extractPath, string prefix = "", bool overwrite = true)
{
using (ZipArchive archive = ZipFile.OpenRead(zipPath))
{
foreach (ZipArchiveEntry entry in archive.Entries)
{
if (string.IsNullOrEmpty(entry.Name))
continue; // Skip directories
string destinationPath;
if (!String.IsNullOrEmpty(prefix))
{
string normalizedPath = entry.FullName.Replace('\\', '/');
if (!normalizedPath.StartsWith(prefix, StringComparison.OrdinalIgnoreCase))
continue;
destinationPath = Path.Combine(extractPath, normalizedPath.Substring(prefix.Length));
}
else
{
destinationPath = Path.Combine(extractPath, entry.FullName);
}
Directory.CreateDirectory(Path.GetDirectoryName(destinationPath));
entry.ExtractToFile(destinationPath, overwrite);
}
}
}
static async Task DownloadAndExtractInsideDirectory(string url, string path, string setupDir)
{
string urlName = Path.GetFileName(url);
string zipPath = Path.Combine(cacheDownloadPath, urlName);
string setupFile = Path.Combine(setupDir, urlName + ".complete");
if (File.Exists(setupFile)) return;
Directory.CreateDirectory(cacheDownloadPath);
foreach (string existingZipPath in Directory.GetFiles(cacheDownloadPath, "*.zip"))
{
if (existingZipPath != zipPath)
{
Debug.Log(existingZipPath);
File.Delete(existingZipPath);
}
}
string hashurl = url + ".sha256";
string hashPath = zipPath + ".sha256";
string hash = File.Exists(hashPath)? File.ReadAllText(hashPath).Trim() : "";
bool same_hash = false;
try
{
new ResumingWebClient().GetURLFileSize(hashurl); // avoid showing error if url doesn't exist
await DownloadFile(hashurl, hashPath+".new", debug: false);
same_hash = File.ReadAllText(hashPath+".new").Trim() == hash;
} catch {}
if (!File.Exists(zipPath) || !same_hash) await DownloadFile(url, zipPath, true, null, SetLibraryProgress);
AssetDatabase.StartAssetEditing();
ExtractInsideDirectory(zipPath, path, $"{libraryName}/runtimes/");
CreateEmptyFile(setupFile);
AssetDatabase.StopAssetEditing();
if (File.Exists(hashPath+".new"))
{
if (File.Exists(hashPath)) File.Delete(hashPath);
File.Move(hashPath+".new", hashPath);
}
}
static void DeleteEarlierVersions()
{
List<string> assetPathSubDirs = new List<string>();
foreach (string dir in new string[] { GetAssetPath(), Path.Combine(Application.dataPath, "Plugins", "Android") })
{
if (Directory.Exists(dir)) assetPathSubDirs.AddRange(Directory.GetDirectories(dir));
}
List<Regex> versionRegexes = new List<Regex> { new Regex("undreamai-(.+)-llamacpp"), new Regex("LlamaLib-(.+)") };
foreach (string assetPathSubDir in assetPathSubDirs)
{
foreach (Regex regex in versionRegexes)
{
Match match = regex.Match(Path.GetFileName(assetPathSubDir));
if (match.Success)
{
string version = match.Groups[1].Value;
if (version != LlamaLibVersion)
{
Debug.Log($"Deleting other LLMUnity version folder: {assetPathSubDir}");
Directory.Delete(assetPathSubDir, true);
if (File.Exists(assetPathSubDir + ".meta")) File.Delete(assetPathSubDir + ".meta");
}
}
}
}
}
static async Task DownloadLibrary()
{
if (libraryProgress < 1) return;
libraryProgress = 0;
try
{
DeleteEarlierVersions();
string setupDir = Path.Combine(libraryPath, "setup");
Directory.CreateDirectory(setupDir);
// setup LlamaLib in StreamingAssets
await DownloadAndExtractInsideDirectory(LlamaLibURL, libraryPath, setupDir);
}
catch (Exception e)
{
LogError(e.Message);
}
libraryProgress = 1;
}
private static void SetLibraryProgress(float progress)
{
libraryProgress = Math.Min(0.99f, progress);
}
public static string AddAsset(string assetPath)
{
if (!File.Exists(assetPath))
{
LogError($"{assetPath} does not exist!");
return null;
}
string assetDir = GetAssetPath();
if (IsSubPath(assetPath, assetDir)) return RelativePath(assetPath, assetDir);
string filename = Path.GetFileName(assetPath);
string fullPath = GetAssetPath(filename);
AssetDatabase.StartAssetEditing();
foreach (string path in new string[] { fullPath, fullPath + ".meta" })
{
if (File.Exists(path)) File.Delete(path);
}
File.Copy(assetPath, fullPath);
AssetDatabase.StopAssetEditing();
return filename;
}
#endif
/// \endcond
/// <summary> Add callback function to call for error logs </summary>
public static void AddErrorCallBack(Action<string> callback)
{
errorCallbacks.Add(callback);
}
/// <summary> Remove callback function added for error logs </summary>
public static void RemoveErrorCallBack(Action<string> callback)
{
errorCallbacks.Remove(callback);
}
/// <summary> Remove all callback function added for error logs </summary>
public static void ClearErrorCallBacks()
{
errorCallbacks.Clear();
}
public static int GetMaxFreqKHz(int cpuId)
{
string[] paths = new string[]
{
$"/sys/devices/system/cpu/cpufreq/stats/cpu{cpuId}/time_in_state",
$"/sys/devices/system/cpu/cpu{cpuId}/cpufreq/stats/time_in_state",
$"/sys/devices/system/cpu/cpu{cpuId}/cpufreq/cpuinfo_max_freq"
};
foreach (var path in paths)
{
if (!File.Exists(path)) continue;
int maxFreqKHz = 0;
using (StreamReader sr = new StreamReader(path))
{
string line;
while ((line = sr.ReadLine()) != null)
{
string[] parts = line.Split(' ');
if (parts.Length > 0 && int.TryParse(parts[0], out int freqKHz))
{
if (freqKHz > maxFreqKHz)
{
maxFreqKHz = freqKHz;
}
}
}
}
if (maxFreqKHz != 0) return maxFreqKHz;
}
return -1;
}
public static bool IsSmtCpu(int cpuId)
{
string[] paths = new string[]
{
$"/sys/devices/system/cpu/cpu{cpuId}/topology/core_cpus_list",
$"/sys/devices/system/cpu/cpu{cpuId}/topology/thread_siblings_list"
};
foreach (var path in paths)
{
if (!File.Exists(path)) continue;
using (StreamReader sr = new StreamReader(path))
{
string line;
while ((line = sr.ReadLine()) != null)
{
if (line.Contains(",") || line.Contains("-"))
{
return true;
}
}
}
}
return false;
}
/// <summary>
/// Calculates the number of big cores in Android similarly to ncnn (https://github.com/Tencent/ncnn)
/// </summary>
/// <returns></returns>
public static int AndroidGetNumBigCores()
{
int maxFreqKHzMin = int.MaxValue;
int maxFreqKHzMax = 0;
List<int> cpuMaxFreqKHz = new List<int>();
List<bool> cpuIsSmtCpu = new List<bool>();
try
{
string cpuPath = "/sys/devices/system/cpu/";
int coreIndex;
if (Directory.Exists(cpuPath))
{
foreach (string cpuDir in Directory.GetDirectories(cpuPath))
{
string dirName = Path.GetFileName(cpuDir);
if (!dirName.StartsWith("cpu")) continue;
if (!int.TryParse(dirName.Substring(3), out coreIndex)) continue;
int maxFreqKHz = GetMaxFreqKHz(coreIndex);
cpuMaxFreqKHz.Add(maxFreqKHz);
if (maxFreqKHz > maxFreqKHzMax) maxFreqKHzMax = maxFreqKHz;
if (maxFreqKHz < maxFreqKHzMin) maxFreqKHzMin = maxFreqKHz;
cpuIsSmtCpu.Add(IsSmtCpu(coreIndex));
}
}
}
catch (Exception e)
{
LogError(e.Message);
}
int numBigCores = 0;
int numCores = SystemInfo.processorCount;
int maxFreqKHzMedium = (maxFreqKHzMin + maxFreqKHzMax) / 2;
if (maxFreqKHzMedium == maxFreqKHzMax) numBigCores = numCores;
else
{
for (int i = 0; i < cpuMaxFreqKHz.Count; i++)
{
if (cpuIsSmtCpu[i] || cpuMaxFreqKHz[i] >= maxFreqKHzMedium) numBigCores++;
}
}
if (numBigCores == 0) numBigCores = SystemInfo.processorCount / 2;
else numBigCores = Math.Min(numBigCores, SystemInfo.processorCount);
return numBigCores;
}
/// <summary>
/// Calculates the number of big cores in Android similarly to Unity (https://docs.unity3d.com/2022.3/Documentation/Manual/android-thread-configuration.html)
/// </summary>
/// <returns></returns>
public static int AndroidGetNumBigCoresCapacity()
{
List<int> capacities = new List<int>();
int minCapacity = int.MaxValue;
try
{
string cpuPath = "/sys/devices/system/cpu/";
int coreIndex;
if (Directory.Exists(cpuPath))
{
foreach (string cpuDir in Directory.GetDirectories(cpuPath))
{
string dirName = Path.GetFileName(cpuDir);
if (!dirName.StartsWith("cpu")) continue;
if (!int.TryParse(dirName.Substring(3), out coreIndex)) continue;
string capacityPath = Path.Combine(cpuDir, "cpu_capacity");
if (!File.Exists(capacityPath)) break;
int capacity = int.Parse(File.ReadAllText(capacityPath).Trim());
capacities.Add(capacity);
if (minCapacity > capacity) minCapacity = capacity;
}
}
}
catch (Exception e)
{
LogError(e.Message);
}
int numBigCores = 0;
foreach (int capacity in capacities)
{
if (capacity >= 2 * minCapacity) numBigCores++;
}
if (numBigCores == 0 || numBigCores > SystemInfo.processorCount) numBigCores = SystemInfo.processorCount;
return numBigCores;
}
}
}