-
-
Notifications
You must be signed in to change notification settings - Fork 551
Expand file tree
/
Copy pathModelIndexService.cs
More file actions
683 lines (563 loc) · 23 KB
/
ModelIndexService.cs
File metadata and controls
683 lines (563 loc) · 23 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
using System.Collections.Concurrent;
using System.Collections.Immutable;
using System.Diagnostics;
using System.Text;
using AsyncAwaitBestPractices;
using AutoCtor;
using Injectio.Attributes;
using KGySoft.CoreLibraries;
using LiteDB;
using LiteDB.Async;
using Microsoft.Extensions.Logging;
using StabilityMatrix.Core.Database;
using StabilityMatrix.Core.Extensions;
using StabilityMatrix.Core.Helper;
using StabilityMatrix.Core.Models;
using StabilityMatrix.Core.Models.Api;
using StabilityMatrix.Core.Models.Database;
using StabilityMatrix.Core.Models.FileInterfaces;
using JsonSerializer = System.Text.Json.JsonSerializer;
namespace StabilityMatrix.Core.Services;
[RegisterSingleton<IModelIndexService, ModelIndexService>]
[AutoConstruct]
public partial class ModelIndexService : IModelIndexService
{
private readonly ILogger<ModelIndexService> logger;
private readonly ISettingsManager settingsManager;
private readonly ILiteDbContext liteDbContext;
private readonly ModelFinder modelFinder;
private readonly SemaphoreSlim safetensorMetadataParseLock = new(1, 1);
private DateTimeOffset lastUpdateCheck = DateTimeOffset.MinValue;
private Dictionary<SharedFolderType, List<LocalModelFile>> _modelIndex = new();
private HashSet<string>? _modelIndexBlake3Hashes;
/// <summary>
/// Whether the database has been initially loaded.
/// </summary>
private bool IsDbLoaded { get; set; }
public Dictionary<SharedFolderType, List<LocalModelFile>> ModelIndex
{
get => _modelIndex;
private set
{
_modelIndex = value;
OnModelIndexReset();
}
}
public IReadOnlySet<string> ModelIndexBlake3Hashes =>
_modelIndexBlake3Hashes ??= CollectModelHashes(ModelIndex.Values.SelectMany(x => x));
[AutoPostConstruct]
private void Initialize()
{
// Start background index when library dir is set
settingsManager.RegisterOnLibraryDirSet(_ =>
{
// Skip if already loaded
if (IsDbLoaded)
{
return;
}
Task.Run(async () =>
{
// Build db indexes
await liteDbContext
.LocalModelFiles.EnsureIndexAsync(m => m.HashBlake3)
.ConfigureAwait(false);
await liteDbContext
.LocalModelFiles.EnsureIndexAsync(m => m.SharedFolderType)
.ConfigureAwait(false);
// Load models first from db, then do index refresh
await EnsureLoadedAsync().ConfigureAwait(false);
await RefreshIndex().ConfigureAwait(false);
})
.SafeFireAndForget(ex =>
{
logger.LogError(ex, "Error loading model index");
});
});
}
// Ensure the in memory cache is loaded
private async Task EnsureLoadedAsync()
{
if (!IsDbLoaded)
{
await LoadFromDbAsync().ConfigureAwait(false);
}
}
/// <summary>
/// Populates <see cref="ModelIndex"/> from the database.
/// </summary>
private async Task LoadFromDbAsync()
{
var timer = Stopwatch.StartNew();
logger.LogInformation("Loading models from database...");
// Handle enum deserialize exceptions from changes
var allModels = await liteDbContext
.TryQueryWithClearOnExceptionAsync(
liteDbContext.LocalModelFiles,
liteDbContext.LocalModelFiles.IncludeAll().FindAllAsync()
)
.ConfigureAwait(false);
if (allModels is not null)
{
ModelIndex = allModels.GroupBy(m => m.SharedFolderType).ToDictionary(g => g.Key, g => g.ToList());
}
else
{
ModelIndex.Clear();
}
IsDbLoaded = true;
EventManager.Instance.OnModelIndexChanged();
timer.Stop();
logger.LogInformation(
"Loaded {Count} models from database in {Time:F2}ms",
ModelIndex.Count,
timer.Elapsed.TotalMilliseconds
);
}
/// <inheritdoc />
public async Task<Dictionary<SharedFolderType, LocalModelFolder>> FindAllFolders()
{
var modelFiles = await liteDbContext.LocalModelFiles.FindAllAsync().ConfigureAwait(false);
var rootFolders = new Dictionary<SharedFolderType, LocalModelFolder>();
foreach (var modelFile in modelFiles)
{
var pathParts = modelFile.RelativePath.Split(Path.DirectorySeparatorChar);
var currentFolder = rootFolders.GetOrAdd(
modelFile.SharedFolderType,
_ => new LocalModelFolder { RelativePath = pathParts[0] }
);
for (var i = 1; i < pathParts.Length - 1; i++)
{
var folderName = pathParts[i];
var folder = currentFolder.Folders.GetValueOrDefault(folderName);
if (folder == null)
{
folder = new LocalModelFolder { RelativePath = folderName };
currentFolder.Folders[folderName] = folder;
}
currentFolder = folder;
}
currentFolder.Files[modelFile.RelativePath] = modelFile;
}
return rootFolders;
}
/// <inheritdoc />
public IEnumerable<LocalModelFile> FindByModelType(SharedFolderType types)
{
return ModelIndex.Where(kvp => (kvp.Key & types) != 0).SelectMany(kvp => kvp.Value);
}
/// <inheritdoc />
public Task<IEnumerable<LocalModelFile>> FindByModelTypeAsync(SharedFolderType type)
{
// To list of types
var types = Enum.GetValues<SharedFolderType>()
.Where(folderType => type.HasFlag(folderType))
.ToArray();
return types.Length switch
{
0 => Task.FromResult(Enumerable.Empty<LocalModelFile>()),
1 => liteDbContext.LocalModelFiles.FindAsync(m => m.SharedFolderType == type),
_ => liteDbContext.LocalModelFiles.FindAsync(m => types.Contains(m.SharedFolderType))
};
}
/// <inheritdoc />
public Task<IEnumerable<LocalModelFile>> FindByHashAsync(string hashBlake3)
{
return liteDbContext.LocalModelFiles.FindAsync(m => m.HashBlake3 == hashBlake3);
}
/// <inheritdoc />
public Task RefreshIndex()
{
return RefreshIndexParallelCore();
}
private async Task RefreshIndexCore()
{
if (!settingsManager.IsLibraryDirSet)
{
logger.LogTrace("Model index refresh skipped, library directory not set");
return;
}
if (new DirectoryPath(settingsManager.ModelsDirectory) is not { Exists: true } modelsDir)
{
logger.LogTrace("Model index refresh skipped, model directory does not exist");
return;
}
logger.LogInformation("Refreshing model index...");
// Start
var stopwatch = Stopwatch.StartNew();
var newIndex = new Dictionary<SharedFolderType, List<LocalModelFile>>();
var newIndexFlat = new List<LocalModelFile>();
var paths = Directory
.EnumerateFiles(modelsDir, "*", EnumerationOptionConstants.AllDirectories)
.ToHashSet();
foreach (var path in paths)
{
// Skip if not supported extension
if (!LocalModelFile.SupportedCheckpointExtensions.Contains(Path.GetExtension(path)))
{
continue;
}
var relativePath = Path.GetRelativePath(modelsDir, path);
// Get shared folder name
var sharedFolderName = relativePath.Split(
Path.DirectorySeparatorChar,
StringSplitOptions.RemoveEmptyEntries
)[0];
// Try Convert to enum
if (!Enum.TryParse<SharedFolderType>(sharedFolderName, out var sharedFolderType))
{
continue;
}
// Since RelativePath is the database key, for LiteDB this is limited to 1021 bytes
if (Encoding.UTF8.GetByteCount(relativePath) is var byteCount and > 1021)
{
logger.LogWarning(
"Skipping model {Path} because it's path is too long ({Length} bytes)",
relativePath,
byteCount
);
continue;
}
var localModel = new LocalModelFile
{
RelativePath = relativePath,
SharedFolderType = sharedFolderType
};
// Try to find a connected model info
var fileDirectory = new DirectoryPath(Path.GetDirectoryName(path)!);
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
var jsonPath = fileDirectory.JoinFile($"{fileNameWithoutExtension}.cm-info.json");
if (paths.Contains(jsonPath))
{
try
{
await using var stream = jsonPath.Info.OpenRead();
var connectedModelInfo = await JsonSerializer
.DeserializeAsync(
stream,
ConnectedModelInfoSerializerContext.Default.ConnectedModelInfo
)
.ConfigureAwait(false);
localModel.ConnectedModelInfo = connectedModelInfo;
}
catch (Exception e)
{
logger.LogWarning(
e,
"Failed to deserialize connected model info for {Path}, skipping",
jsonPath
);
}
}
// Try to find a preview image
var previewImagePath = LocalModelFile
.SupportedImageExtensions.Select(
ext => fileDirectory.JoinFile($"{fileNameWithoutExtension}.preview{ext}")
)
.FirstOrDefault(filePath => paths.Contains(filePath));
if (previewImagePath is not null)
{
localModel.PreviewImageRelativePath = Path.GetRelativePath(modelsDir, previewImagePath);
}
// Try to find a config file (same name as model file but with .yaml extension)
var configFile = fileDirectory.JoinFile($"{fileNameWithoutExtension}.yaml");
if (paths.Contains(configFile))
{
localModel.ConfigFullPath = configFile;
}
// Add to index
newIndexFlat.Add(localModel);
var list = newIndex.GetOrAdd(sharedFolderType);
list.Add(localModel);
}
ModelIndex = newIndex;
stopwatch.Stop();
var indexTime = stopwatch.Elapsed;
// Insert to db as transaction
stopwatch.Restart();
using var db = await liteDbContext.Database.BeginTransactionAsync().ConfigureAwait(false);
var localModelFiles = db.GetCollection<LocalModelFile>("LocalModelFiles")!;
await localModelFiles.DeleteAllAsync().ConfigureAwait(false);
await localModelFiles.InsertBulkAsync(newIndexFlat).ConfigureAwait(false);
await db.CommitAsync().ConfigureAwait(false);
stopwatch.Stop();
var dbTime = stopwatch.Elapsed;
logger.LogInformation(
"Model index refreshed with {Entries} entries, took (index: {IndexDuration}), (db: {DbDuration})",
newIndexFlat.Count,
CodeTimer.FormatTime(indexTime),
CodeTimer.FormatTime(dbTime)
);
EventManager.Instance.OnModelIndexChanged();
}
private async Task RefreshIndexParallelCore()
{
if (!settingsManager.IsLibraryDirSet)
{
logger.LogTrace("Model index refresh skipped, library directory not set");
return;
}
if (new DirectoryPath(settingsManager.ModelsDirectory) is not { Exists: true } modelsDir)
{
logger.LogTrace("Model index refresh skipped, model directory does not exist");
return;
}
// Start
var stopwatch = Stopwatch.StartNew();
logger.LogInformation("Refreshing model index...");
var newIndexFlat = new ConcurrentBag<LocalModelFile>();
var paths = Directory
.EnumerateFiles(modelsDir, "*", EnumerationOptionConstants.AllDirectories)
.ToHashSet();
var partitioner = Partitioner.Create(paths, EnumerablePartitionerOptions.NoBuffering);
var numThreads = Environment.ProcessorCount switch
{
>= 20 => Environment.ProcessorCount / 3 - 1,
> 1 => Environment.ProcessorCount,
_ => 1
};
Parallel.ForEach(
partitioner,
new ParallelOptions { MaxDegreeOfParallelism = numThreads },
path =>
{
// Skip if not supported extension
if (!LocalModelFile.SupportedCheckpointExtensions.Contains(Path.GetExtension(path)))
{
return;
}
var relativePath = Path.GetRelativePath(modelsDir, path);
// Get shared folder name
var sharedFolderName = relativePath.Split(
Path.DirectorySeparatorChar,
StringSplitOptions.RemoveEmptyEntries
)[0];
// Try Convert to enum
if (!Enum.TryParse<SharedFolderType>(sharedFolderName, out var sharedFolderType))
{
sharedFolderType = SharedFolderType.Unknown;
}
// Since RelativePath is the database key, for LiteDB this is limited to 1021 bytes
if (Encoding.UTF8.GetByteCount(relativePath) is var byteCount and > 1021)
{
logger.LogWarning(
"Skipping model {Path} because it's path is too long ({Length} bytes)",
relativePath,
byteCount
);
return;
}
var localModel = new LocalModelFile
{
RelativePath = relativePath,
SharedFolderType = sharedFolderType
};
// Try to find a connected model info
var fileDirectory = new DirectoryPath(Path.GetDirectoryName(path)!);
var fileNameWithoutExtension = Path.GetFileNameWithoutExtension(path);
var jsonPath = fileDirectory.JoinFile($"{fileNameWithoutExtension}.cm-info.json");
if (paths.Contains(jsonPath))
{
try
{
using var stream = jsonPath.Info.OpenRead();
var connectedModelInfo = JsonSerializer.Deserialize(
stream,
ConnectedModelInfoSerializerContext.Default.ConnectedModelInfo
);
localModel.ConnectedModelInfo = connectedModelInfo;
}
catch (Exception e)
{
logger.LogWarning(
e,
"Failed to deserialize connected model info for {Path}, skipping",
jsonPath
);
}
}
// Try to find a preview image
var previewImagePath = LocalModelFile
.SupportedImageExtensions.Select(
ext => fileDirectory.JoinFile($"{fileNameWithoutExtension}.preview{ext}")
)
.FirstOrDefault(filePath => paths.Contains(filePath));
if (previewImagePath is not null)
{
localModel.PreviewImageRelativePath = Path.GetRelativePath(modelsDir, previewImagePath);
}
// Try to find a config file (same name as model file but with .yaml extension)
var configFile = fileDirectory.JoinFile($"{fileNameWithoutExtension}.yaml");
if (paths.Contains(configFile))
{
localModel.ConfigFullPath = configFile;
}
// Add to index
newIndexFlat.Add(localModel);
}
);
var newIndexComplete = newIndexFlat.ToArray();
var modelsDict = ModelIndex
.Values.SelectMany(x => x)
.DistinctBy(f => f.RelativePath)
.ToDictionary(f => f.RelativePath, file => file);
var newIndex = new Dictionary<SharedFolderType, List<LocalModelFile>>();
foreach (var model in newIndexComplete)
{
if (modelsDict.TryGetValue(model.RelativePath, out var dbModel))
{
model.HasUpdate = dbModel.HasUpdate;
model.LastUpdateCheck = dbModel.LastUpdateCheck;
model.LatestModelInfo = dbModel.LatestModelInfo;
}
if (model.LatestModelInfo == null && model.HasCivitMetadata)
{
// Handle enum deserialize exceptions from changes
if (
await liteDbContext
.TryQueryWithClearOnExceptionAsync(
liteDbContext.CivitModels,
liteDbContext
.CivitModels.Include(m => m.ModelVersions)
.FindByIdAsync(model.ConnectedModelInfo.ModelId)
)
.ConfigureAwait(false) is
{ } latestModel
)
{
model.LatestModelInfo = latestModel;
}
}
var list = newIndex.GetOrAdd(model.SharedFolderType);
list.Add(model);
}
ModelIndex = newIndex;
stopwatch.Stop();
var indexTime = stopwatch.Elapsed;
// Insert to db as transaction
stopwatch.Restart();
using var db = await liteDbContext.Database.BeginTransactionAsync().ConfigureAwait(false);
var localModelFiles = db.GetCollection<LocalModelFile>("LocalModelFiles")!;
await localModelFiles.DeleteAllAsync().ConfigureAwait(false);
await localModelFiles.InsertBulkAsync(newIndexComplete).ConfigureAwait(false);
await db.CommitAsync().ConfigureAwait(false);
stopwatch.Stop();
var dbTime = stopwatch.Elapsed;
logger.LogInformation(
"Model index refreshed with {Entries} entries, took {IndexDuration} ({DbDuration} db)",
newIndexFlat.Count,
CodeTimer.FormatTime(indexTime),
CodeTimer.FormatTime(dbTime)
);
EventManager.Instance.OnModelIndexChanged();
}
/// <inheritdoc />
public void BackgroundRefreshIndex()
{
Task.Run(async () => await RefreshIndex().ConfigureAwait(false))
.SafeFireAndForget(ex =>
{
logger.LogError(ex, "Error in background model indexing");
});
}
/// <inheritdoc />
public async Task<bool> RemoveModelAsync(LocalModelFile model)
{
// Remove from database
if (await liteDbContext.LocalModelFiles.DeleteAsync(model.RelativePath).ConfigureAwait(false))
{
// Remove from index
if (ModelIndex.TryGetValue(model.SharedFolderType, out var list))
{
list.RemoveAll(x => x.RelativePath == model.RelativePath);
OnModelIndexReset();
EventManager.Instance.OnModelIndexChanged();
}
return true;
}
return false;
}
public async Task<bool> RemoveModelsAsync(IEnumerable<LocalModelFile> models)
{
var modelsList = models.ToList();
var paths = modelsList.Select(m => m.RelativePath).ToList();
var result = true;
foreach (var path in paths)
{
result &= await liteDbContext.LocalModelFiles.DeleteAsync(path).ConfigureAwait(false);
}
foreach (var model in modelsList)
{
if (ModelIndex.TryGetValue(model.SharedFolderType, out var list))
{
list.RemoveAll(x => x.RelativePath == model.RelativePath);
}
}
OnModelIndexReset();
EventManager.Instance.OnModelIndexChanged();
return result;
}
public async Task CheckModelsForUpdateAsync()
{
if (DateTimeOffset.UtcNow < lastUpdateCheck.AddMinutes(5))
{
return;
}
lastUpdateCheck = DateTimeOffset.UtcNow;
var installedHashes = ModelIndexBlake3Hashes;
var dbModels = (
await liteDbContext.LocalModelFiles.FindAllAsync().ConfigureAwait(false) ?? []
).ToList();
var ids = dbModels
.Where(x => x.ConnectedModelInfo?.ModelId != null)
.Select(x => x.ConnectedModelInfo!.ModelId.Value)
.Distinct();
var remoteModels = (await modelFinder.FindRemoteModelsById(ids).ConfigureAwait(false)).ToList();
// update the civitmodels cache with this new result
await liteDbContext.UpsertCivitModelAsync(remoteModels).ConfigureAwait(false);
var localModelsToUpdate = new List<LocalModelFile>();
foreach (var dbModel in dbModels)
{
if (dbModel.ConnectedModelInfo == null)
continue;
var remoteModel = remoteModels.FirstOrDefault(m => m.Id == dbModel.ConnectedModelInfo!.ModelId);
var latestVersion = remoteModel?.ModelVersions?.FirstOrDefault();
if (latestVersion?.Files is not { } latestVersionFiles)
{
continue;
}
var latestHashes = latestVersionFiles
.Where(f => f.Type == CivitFileType.Model)
.Select(f => f.Hashes.BLAKE3)
.Where(hash => hash is not null)
.ToList();
dbModel.HasUpdate = !latestHashes.Any(hash => installedHashes.Contains(hash!));
dbModel.LastUpdateCheck = DateTimeOffset.UtcNow;
dbModel.LatestModelInfo = remoteModel;
localModelsToUpdate.Add(dbModel);
}
await liteDbContext.LocalModelFiles.UpsertAsync(localModelsToUpdate).ConfigureAwait(false);
await LoadFromDbAsync().ConfigureAwait(false);
}
public async Task UpsertModelAsync(LocalModelFile model)
{
await liteDbContext.LocalModelFiles.UpsertAsync(model).ConfigureAwait(false);
await LoadFromDbAsync().ConfigureAwait(false);
}
private void OnModelIndexReset()
{
_modelIndexBlake3Hashes = null;
}
private static HashSet<string> CollectModelHashes(IEnumerable<LocalModelFile> models)
{
var hashes = new HashSet<string>();
foreach (var model in models)
{
if (model.ConnectedModelInfo?.Hashes?.BLAKE3 is { } hashBlake3)
{
hashes.Add(hashBlake3);
}
}
return hashes;
}
}