model_manager.cpp 39 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056
  1. #include "model_manager.h"
  2. #include "model_detector.h"
  3. #include "stable_diffusion_wrapper.h"
  4. #include <iostream>
  5. #include <fstream>
  6. #include <algorithm>
  7. #include <filesystem>
  8. #include <shared_mutex>
  9. #include <chrono>
  10. #include <future>
  11. #include <atomic>
  12. #include <openssl/evp.h>
  13. #include <sstream>
  14. #include <iomanip>
  15. #include <nlohmann/json.hpp>
  16. namespace fs = std::filesystem;
  17. // File extension mappings for each model type
  18. const std::vector<std::string> CHECKPOINT_FILE_EXTENSIONS = {"safetensors", "ckpt", "gguf"};
  19. const std::vector<std::string> EMBEDDING_FILE_EXTENSIONS = {"safetensors", "pt"};
  20. const std::vector<std::string> LORA_FILE_EXTENSIONS = {"safetensors", "ckpt"};
  21. const std::vector<std::string> VAE_FILE_EXTENSIONS = {"safetensors", "pt", "ckpt", "gguf"};
  22. const std::vector<std::string> TAESD_FILE_EXTENSIONS = {"safetensors", "pth", "gguf"};
  23. const std::vector<std::string> ESRGAN_FILE_EXTENSIONS = {"pth", "pt"};
  24. const std::vector<std::string> CONTROLNET_FILE_EXTENSIONS = {"safetensors", "pth"};
  25. class ModelManager::Impl {
  26. public:
  27. std::string modelsDirectory = "./models";
  28. std::map<ModelType, std::string> modelTypeDirectories;
  29. std::map<std::string, ModelInfo> availableModels;
  30. std::map<std::string, std::unique_ptr<StableDiffusionWrapper>> loadedModels;
  31. mutable std::shared_mutex modelsMutex;
  32. std::atomic<bool> scanCancelled{false};
  33. bool legacyMode = true;
  34. /**
  35. * @brief Validate a directory path
  36. *
  37. * @param path The directory path to validate
  38. * @return true if the directory exists and is valid, false otherwise
  39. */
  40. bool validateDirectory(const std::string& path) const {
  41. if (path.empty()) {
  42. return false;
  43. }
  44. std::filesystem::path dirPath(path);
  45. if (!std::filesystem::exists(dirPath)) {
  46. std::cerr << "Directory does not exist: " << path << std::endl;
  47. return false;
  48. }
  49. if (!std::filesystem::is_directory(dirPath)) {
  50. std::cerr << "Path is not a directory: " << path << std::endl;
  51. return false;
  52. }
  53. return true;
  54. }
  55. /**
  56. * @brief Get default directory name for a model type
  57. *
  58. * @param type The model type
  59. * @return std::string Default directory name
  60. */
  61. std::string getDefaultDirectoryName(ModelType type) const {
  62. switch (type) {
  63. case ModelType::CHECKPOINT:
  64. return "checkpoints";
  65. case ModelType::CONTROLNET:
  66. return "controlnet";
  67. case ModelType::EMBEDDING:
  68. return "embeddings";
  69. case ModelType::ESRGAN:
  70. return "esrgan";
  71. case ModelType::LORA:
  72. return "lora";
  73. case ModelType::TAESD:
  74. return "taesd";
  75. case ModelType::VAE:
  76. return "vae";
  77. default:
  78. return "";
  79. }
  80. }
  81. /**
  82. * @brief Get directory path for a model type
  83. *
  84. * @param type The model type
  85. * @return std::string Directory path, empty if not set
  86. */
  87. std::string getModelTypeDirectory(ModelType type) const {
  88. auto it = modelTypeDirectories.find(type);
  89. if (it != modelTypeDirectories.end()) {
  90. return it->second;
  91. }
  92. // If in legacy mode, construct default path
  93. if (legacyMode) {
  94. std::string defaultDir = getDefaultDirectoryName(type);
  95. if (!defaultDir.empty()) {
  96. return modelsDirectory + "/" + defaultDir;
  97. }
  98. }
  99. return "";
  100. }
  101. /**
  102. * @brief Get file extensions for a specific model type
  103. *
  104. * @param type The model type
  105. * @return const std::vector<std::string>& Vector of file extensions
  106. */
  107. const std::vector<std::string>& getFileExtensions(ModelType type) const {
  108. switch (type) {
  109. case ModelType::CHECKPOINT:
  110. return CHECKPOINT_FILE_EXTENSIONS;
  111. case ModelType::EMBEDDING:
  112. return EMBEDDING_FILE_EXTENSIONS;
  113. case ModelType::LORA:
  114. return LORA_FILE_EXTENSIONS;
  115. case ModelType::VAE:
  116. return VAE_FILE_EXTENSIONS;
  117. case ModelType::TAESD:
  118. return TAESD_FILE_EXTENSIONS;
  119. case ModelType::ESRGAN:
  120. return ESRGAN_FILE_EXTENSIONS;
  121. case ModelType::CONTROLNET:
  122. return CONTROLNET_FILE_EXTENSIONS;
  123. default:
  124. static const std::vector<std::string> empty;
  125. return empty;
  126. }
  127. }
  128. /**
  129. * @brief Check if a file extension matches a model type
  130. *
  131. * @param extension The file extension
  132. * @param type The model type
  133. * @return true if the extension matches the model type
  134. */
  135. bool isExtensionMatch(const std::string& extension, ModelType type) const {
  136. const auto& extensions = getFileExtensions(type);
  137. return std::find(extensions.begin(), extensions.end(), extension) != extensions.end();
  138. }
  139. /**
  140. * @brief Determine model type based on file path and extension
  141. *
  142. * @param filePath The file path
  143. * @return ModelType The determined model type
  144. */
  145. ModelType determineModelType(const fs::path& filePath) const {
  146. std::string extension = filePath.extension().string();
  147. if (extension.empty()) {
  148. return ModelType::NONE;
  149. }
  150. // Remove the dot from extension
  151. if (extension[0] == '.') {
  152. extension = extension.substr(1);
  153. }
  154. // Convert to lowercase for comparison
  155. std::transform(extension.begin(), extension.end(), extension.begin(), ::tolower);
  156. // Check if the file resides under a directory registered for a given ModelType
  157. fs::path absoluteFilePath = fs::absolute(filePath);
  158. // First check configured directories (if any)
  159. for (const auto& [type, directory] : modelTypeDirectories) {
  160. if (!directory.empty()) {
  161. fs::path absoluteDirPath = fs::absolute(directory).lexically_normal();
  162. fs::path normalizedFilePath = absoluteFilePath.lexically_normal();
  163. // Check if the file is under this directory (directly or in subdirectories)
  164. // Get the relative path from directory to file
  165. auto relativePath = normalizedFilePath.lexically_relative(absoluteDirPath);
  166. // If relative path doesn't start with "..", then file is under the directory
  167. std::string relPathStr = relativePath.string();
  168. bool isUnderDirectory = !relPathStr.empty() &&
  169. relPathStr.substr(0, 2) != ".." &&
  170. relPathStr[0] != '/';
  171. if (isUnderDirectory && isExtensionMatch(extension, type)) {
  172. return type;
  173. }
  174. }
  175. }
  176. // If in legacy mode or no configured directories matched, check default directory structure
  177. if (legacyMode || modelTypeDirectories.empty()) {
  178. // Check the entire path hierarchy for model type directories
  179. fs::path currentPath = filePath.parent_path();
  180. while (currentPath.has_filename()) {
  181. std::string dirName = currentPath.filename().string();
  182. std::transform(dirName.begin(), dirName.end(), dirName.begin(), ::tolower);
  183. // Check default directory names
  184. if (dirName == "checkpoints" || dirName == "stable-diffusion") {
  185. if (isExtensionMatch(extension, ModelType::CHECKPOINT)) {
  186. return ModelType::CHECKPOINT;
  187. }
  188. } else if (dirName == "controlnet") {
  189. if (isExtensionMatch(extension, ModelType::CONTROLNET)) {
  190. return ModelType::CONTROLNET;
  191. }
  192. } else if (dirName == "lora") {
  193. if (isExtensionMatch(extension, ModelType::LORA)) {
  194. return ModelType::LORA;
  195. }
  196. } else if (dirName == "vae") {
  197. if (isExtensionMatch(extension, ModelType::VAE)) {
  198. return ModelType::VAE;
  199. }
  200. } else if (dirName == "taesd") {
  201. if (isExtensionMatch(extension, ModelType::TAESD)) {
  202. return ModelType::TAESD;
  203. }
  204. } else if (dirName == "esrgan" || dirName == "upscaler") {
  205. if (isExtensionMatch(extension, ModelType::ESRGAN)) {
  206. return ModelType::ESRGAN;
  207. }
  208. } else if (dirName == "embeddings" || dirName == "textual-inversion") {
  209. if (isExtensionMatch(extension, ModelType::EMBEDDING)) {
  210. return ModelType::EMBEDDING;
  211. }
  212. }
  213. // Move up to parent directory
  214. currentPath = currentPath.parent_path();
  215. }
  216. }
  217. // Fall back to extension-based detection
  218. // Only return a model type if the extension matches expected extensions for that type
  219. if (isExtensionMatch(extension, ModelType::CHECKPOINT)) {
  220. return ModelType::CHECKPOINT;
  221. } else if (isExtensionMatch(extension, ModelType::LORA)) {
  222. return ModelType::LORA;
  223. } else if (isExtensionMatch(extension, ModelType::VAE)) {
  224. return ModelType::VAE;
  225. } else if (isExtensionMatch(extension, ModelType::TAESD)) {
  226. return ModelType::TAESD;
  227. } else if (isExtensionMatch(extension, ModelType::ESRGAN)) {
  228. return ModelType::ESRGAN;
  229. } else if (isExtensionMatch(extension, ModelType::CONTROLNET)) {
  230. return ModelType::CONTROLNET;
  231. } else if (isExtensionMatch(extension, ModelType::EMBEDDING)) {
  232. return ModelType::EMBEDDING;
  233. }
  234. return ModelType::NONE;
  235. }
  236. /**
  237. * @brief Get file information with timeout
  238. *
  239. * @param filePath The file path to get info for
  240. * @param timeoutMs Timeout in milliseconds
  241. * @return std::pair<bool, std::pair<uintmax_t, fs::file_time_type>> Success flag and file info
  242. */
  243. std::pair<bool, std::pair<uintmax_t, fs::file_time_type>> getFileInfoWithTimeout(
  244. const fs::path& filePath, int timeoutMs = 5000) {
  245. auto future = std::async(std::launch::async, [&filePath]() -> std::pair<uintmax_t, fs::file_time_type> {
  246. try {
  247. uintmax_t fileSize = fs::file_size(filePath);
  248. fs::file_time_type modifiedAt = fs::last_write_time(filePath);
  249. return {fileSize, modifiedAt};
  250. } catch (const fs::filesystem_error&) {
  251. return {0, fs::file_time_type{}};
  252. }
  253. });
  254. if (future.wait_for(std::chrono::milliseconds(timeoutMs)) == std::future_status::timeout) {
  255. std::cerr << "Timeout getting file info for " << filePath << std::endl;
  256. return {false, {0, fs::file_time_type{}}};
  257. }
  258. return {true, future.get()};
  259. }
  260. /**
  261. * @brief Scan a directory for models of a specific type (without holding mutex)
  262. *
  263. * @param directory The directory to scan
  264. * @param type The model type to look for
  265. * @param modelsMap Reference to the map to store results
  266. * @return bool True if scanning completed without cancellation
  267. */
  268. bool scanDirectory(const fs::path& directory, ModelType type, std::map<std::string, ModelInfo>& modelsMap) {
  269. if (scanCancelled.load()) {
  270. return false;
  271. }
  272. if (!fs::exists(directory) || !fs::is_directory(directory)) {
  273. return true;
  274. }
  275. try {
  276. for (const auto& entry : fs::recursive_directory_iterator(directory)) {
  277. if (scanCancelled.load()) {
  278. return false;
  279. }
  280. if (entry.is_regular_file()) {
  281. fs::path filePath = entry.path();
  282. ModelType detectedType = determineModelType(filePath);
  283. // Only add files that have a valid model type (not NONE)
  284. if (detectedType != ModelType::NONE && (type == ModelType::NONE || detectedType == type)) {
  285. ModelInfo info;
  286. // Calculate relative path from the scanned directory (not base models directory)
  287. fs::path relativePath = fs::relative(filePath, directory);
  288. std::string modelName = relativePath.string();
  289. // Normalize path separators for consistency
  290. std::replace(modelName.begin(), modelName.end(), '\\', '/');
  291. // Check if model already exists to avoid duplicates
  292. if (modelsMap.find(modelName) == modelsMap.end()) {
  293. info.name = modelName;
  294. info.path = filePath.string();
  295. info.fullPath = fs::absolute(filePath).string();
  296. info.type = detectedType;
  297. info.isLoaded = false;
  298. info.description = ""; // Initialize description
  299. info.metadata = {}; // Initialize metadata
  300. // Get file info with timeout
  301. auto [success, fileInfo] = getFileInfoWithTimeout(filePath);
  302. if (success) {
  303. info.fileSize = fileInfo.first;
  304. info.modifiedAt = fileInfo.second;
  305. info.createdAt = fileInfo.second; // Use modified time as creation time for now
  306. } else {
  307. info.fileSize = 0;
  308. info.modifiedAt = fs::file_time_type{};
  309. info.createdAt = fs::file_time_type{};
  310. }
  311. // Try to load cached hash from .json file
  312. std::string hashFile = info.fullPath + ".json";
  313. if (fs::exists(hashFile)) {
  314. try {
  315. std::ifstream file(hashFile);
  316. nlohmann::json hashData = nlohmann::json::parse(file);
  317. if (hashData.contains("sha256") && hashData["sha256"].is_string()) {
  318. info.sha256 = hashData["sha256"];
  319. } else {
  320. info.sha256 = "";
  321. }
  322. } catch (...) {
  323. info.sha256 = ""; // If parsing fails, leave empty
  324. }
  325. } else {
  326. info.sha256 = ""; // No cached hash file
  327. }
  328. // Detect architecture for checkpoint models
  329. if (detectedType == ModelType::CHECKPOINT) {
  330. try {
  331. ModelDetectionResult detection = ModelDetector::detectModel(info.fullPath);
  332. // For .ckpt files that can't be detected, default to SD1.5
  333. if (detection.architecture == ModelArchitecture::UNKNOWN &&
  334. (filePath.extension() == ".ckpt" || filePath.extension() == ".pt")) {
  335. info.architecture = "Stable Diffusion 1.5 (assumed)";
  336. info.recommendedVAE = "vae-ft-mse-840000-ema-pruned.safetensors";
  337. info.recommendedWidth = 512;
  338. info.recommendedHeight = 512;
  339. info.recommendedSteps = 20;
  340. info.recommendedSampler = "euler_a";
  341. } else {
  342. info.architecture = detection.architectureName;
  343. info.recommendedVAE = detection.recommendedVAE;
  344. // Parse recommended parameters
  345. if (detection.suggestedParams.count("width")) {
  346. info.recommendedWidth = std::stoi(detection.suggestedParams["width"]);
  347. }
  348. if (detection.suggestedParams.count("height")) {
  349. info.recommendedHeight = std::stoi(detection.suggestedParams["height"]);
  350. }
  351. if (detection.suggestedParams.count("steps")) {
  352. info.recommendedSteps = std::stoi(detection.suggestedParams["steps"]);
  353. }
  354. if (detection.suggestedParams.count("sampler")) {
  355. info.recommendedSampler = detection.suggestedParams["sampler"];
  356. }
  357. }
  358. // Build list of required models based on architecture
  359. if (detection.needsVAE && !detection.recommendedVAE.empty()) {
  360. info.requiredModels.push_back("VAE: " + detection.recommendedVAE);
  361. }
  362. // Add CLIP-L if required
  363. if (detection.suggestedParams.count("clip_l_required")) {
  364. info.requiredModels.push_back("CLIP-L: " + detection.suggestedParams.at("clip_l_required"));
  365. }
  366. // Add CLIP-G if required
  367. if (detection.suggestedParams.count("clip_g_required")) {
  368. info.requiredModels.push_back("CLIP-G: " + detection.suggestedParams.at("clip_g_required"));
  369. }
  370. // Add T5XXL if required
  371. if (detection.suggestedParams.count("t5xxl_required")) {
  372. info.requiredModels.push_back("T5XXL: " + detection.suggestedParams.at("t5xxl_required"));
  373. }
  374. // Add Qwen models if required
  375. if (detection.suggestedParams.count("qwen2vl_required")) {
  376. info.requiredModels.push_back("Qwen2-VL: " + detection.suggestedParams.at("qwen2vl_required"));
  377. }
  378. if (detection.suggestedParams.count("qwen2vl_vision_required")) {
  379. info.requiredModels.push_back("Qwen2-VL-Vision: " + detection.suggestedParams.at("qwen2vl_vision_required"));
  380. }
  381. } catch (const std::exception& e) {
  382. // If detection fails completely, default to SD1.5
  383. info.architecture = "Stable Diffusion 1.5 (assumed)";
  384. info.recommendedVAE = "vae-ft-mse-840000-ema-pruned.safetensors";
  385. info.recommendedWidth = 512;
  386. info.recommendedHeight = 512;
  387. info.recommendedSteps = 20;
  388. info.recommendedSampler = "euler_a";
  389. }
  390. }
  391. modelsMap[info.name] = info;
  392. }
  393. }
  394. }
  395. }
  396. } catch (const fs::filesystem_error& e) {
  397. // Silently handle filesystem errors
  398. }
  399. return !scanCancelled.load();
  400. }
  401. };
  402. ModelManager::ModelManager() : pImpl(std::make_unique<Impl>()) {
  403. }
  404. ModelManager::~ModelManager() = default;
  405. bool ModelManager::scanModelsDirectory() {
  406. // Reset cancellation flag
  407. pImpl->scanCancelled.store(false);
  408. // Create temporary map to store scan results (outside of lock)
  409. std::map<std::string, ModelInfo> tempModels;
  410. if (pImpl->legacyMode) {
  411. // Legacy mode: recursively scan the entire models directory and auto-detect all subdirectories
  412. fs::path modelsPath(pImpl->modelsDirectory);
  413. if (!fs::exists(modelsPath) || !fs::is_directory(modelsPath)) {
  414. std::cerr << "Models directory does not exist: " << pImpl->modelsDirectory << std::endl;
  415. return false;
  416. }
  417. // Scan the entire models directory recursively
  418. // Model type will be determined by file extension and directory structure
  419. // Model names will include the full relative path from the base models directory
  420. if (!pImpl->scanDirectory(modelsPath, ModelType::NONE, tempModels)) {
  421. return false;
  422. }
  423. } else {
  424. // Explicit mode: scan configured directories for each model type
  425. std::vector<std::pair<ModelType, std::string>> directoriesToScan = {
  426. {ModelType::CHECKPOINT, pImpl->getModelTypeDirectory(ModelType::CHECKPOINT)},
  427. {ModelType::CONTROLNET, pImpl->getModelTypeDirectory(ModelType::CONTROLNET)},
  428. {ModelType::LORA, pImpl->getModelTypeDirectory(ModelType::LORA)},
  429. {ModelType::VAE, pImpl->getModelTypeDirectory(ModelType::VAE)},
  430. {ModelType::TAESD, pImpl->getModelTypeDirectory(ModelType::TAESD)},
  431. {ModelType::ESRGAN, pImpl->getModelTypeDirectory(ModelType::ESRGAN)},
  432. {ModelType::EMBEDDING, pImpl->getModelTypeDirectory(ModelType::EMBEDDING)}
  433. };
  434. for (const auto& [type, dirPath] : directoriesToScan) {
  435. if (!dirPath.empty()) {
  436. if (!pImpl->scanDirectory(dirPath, type, tempModels)) {
  437. return false;
  438. }
  439. }
  440. }
  441. }
  442. // Brief exclusive lock only to swap the data
  443. {
  444. std::unique_lock<std::shared_mutex> lock(pImpl->modelsMutex);
  445. pImpl->availableModels.swap(tempModels);
  446. }
  447. return true;
  448. }
  449. bool ModelManager::loadModel(const std::string& name, const std::string& path, ModelType type) {
  450. std::unique_lock<std::shared_mutex> lock(pImpl->modelsMutex);
  451. // Check if model is already loaded
  452. if (pImpl->loadedModels.find(name) != pImpl->loadedModels.end()) {
  453. return true;
  454. }
  455. // Check if file exists
  456. if (!fs::exists(path)) {
  457. std::cerr << "Model file does not exist: " << path << std::endl;
  458. return false;
  459. }
  460. // Create and initialize the stable-diffusion wrapper
  461. auto wrapper = std::make_unique<StableDiffusionWrapper>();
  462. // Set up generation parameters for model loading
  463. StableDiffusionWrapper::GenerationParams loadParams;
  464. loadParams.modelPath = path;
  465. loadParams.modelType = "f16"; // Default to f16 for better performance
  466. // Try to load the model
  467. if (!wrapper->loadModel(path, loadParams)) {
  468. std::cerr << "Failed to load model '" << name << "': " << wrapper->getLastError() << std::endl;
  469. return false;
  470. }
  471. pImpl->loadedModels[name] = std::move(wrapper);
  472. // Update model info
  473. if (pImpl->availableModels.find(name) != pImpl->availableModels.end()) {
  474. pImpl->availableModels[name].isLoaded = true;
  475. } else {
  476. // Create a new model info entry
  477. ModelInfo info;
  478. info.name = name;
  479. info.path = path;
  480. info.fullPath = fs::absolute(path).string();
  481. info.type = type;
  482. info.isLoaded = true;
  483. info.sha256 = "";
  484. info.description = ""; // Initialize description
  485. info.metadata = {}; // Initialize metadata
  486. try {
  487. info.fileSize = fs::file_size(path);
  488. info.modifiedAt = fs::last_write_time(path);
  489. info.createdAt = info.modifiedAt; // Use modified time as creation time for now
  490. } catch (const fs::filesystem_error& e) {
  491. std::cerr << "Error getting file info for " << path << ": " << e.what() << std::endl;
  492. info.fileSize = 0;
  493. info.modifiedAt = fs::file_time_type{};
  494. info.createdAt = fs::file_time_type{};
  495. }
  496. pImpl->availableModels[name] = info;
  497. }
  498. return true;
  499. }
  500. bool ModelManager::loadModel(const std::string& name) {
  501. std::string path;
  502. ModelType type;
  503. {
  504. std::unique_lock<std::shared_mutex> lock(pImpl->modelsMutex);
  505. // Check if model exists in available models
  506. auto it = pImpl->availableModels.find(name);
  507. if (it == pImpl->availableModels.end()) {
  508. std::cerr << "Model '" << name << "' not found in available models" << std::endl;
  509. return false;
  510. }
  511. // Check if already loaded
  512. if (pImpl->loadedModels.find(name) != pImpl->loadedModels.end()) {
  513. return true;
  514. }
  515. // Extract path and type while we have the lock
  516. path = it->second.path;
  517. type = it->second.type;
  518. } // Release lock here
  519. // Load the model without holding the lock
  520. return loadModel(name, path, type);
  521. }
  522. bool ModelManager::unloadModel(const std::string& name) {
  523. std::unique_lock<std::shared_mutex> lock(pImpl->modelsMutex);
  524. // Check if model is loaded
  525. auto loadedIt = pImpl->loadedModels.find(name);
  526. if (loadedIt == pImpl->loadedModels.end()) {
  527. return false;
  528. }
  529. // Unload the model properly
  530. if (loadedIt->second) {
  531. loadedIt->second->unloadModel();
  532. }
  533. pImpl->loadedModels.erase(loadedIt);
  534. // Update model info
  535. auto availableIt = pImpl->availableModels.find(name);
  536. if (availableIt != pImpl->availableModels.end()) {
  537. availableIt->second.isLoaded = false;
  538. }
  539. return true;
  540. }
  541. StableDiffusionWrapper* ModelManager::getModel(const std::string& name) {
  542. std::shared_lock<std::shared_mutex> lock(pImpl->modelsMutex);
  543. auto it = pImpl->loadedModels.find(name);
  544. if (it == pImpl->loadedModels.end()) {
  545. return nullptr;
  546. }
  547. return it->second.get();
  548. }
  549. std::map<std::string, ModelManager::ModelInfo> ModelManager::getAllModels() const {
  550. std::shared_lock<std::shared_mutex> lock(pImpl->modelsMutex);
  551. return pImpl->availableModels;
  552. }
  553. std::vector<ModelManager::ModelInfo> ModelManager::getModelsByType(ModelType type) const {
  554. std::shared_lock<std::shared_mutex> lock(pImpl->modelsMutex);
  555. std::vector<ModelInfo> result;
  556. for (const auto& pair : pImpl->availableModels) {
  557. if (pair.second.type == type) {
  558. result.push_back(pair.second);
  559. }
  560. }
  561. return result;
  562. }
  563. ModelManager::ModelInfo ModelManager::getModelInfo(const std::string& name) const {
  564. std::unique_lock<std::shared_mutex> lock(pImpl->modelsMutex);
  565. auto it = pImpl->availableModels.find(name);
  566. if (it == pImpl->availableModels.end()) {
  567. return ModelInfo{}; // Return empty ModelInfo if not found
  568. }
  569. return it->second;
  570. }
  571. bool ModelManager::isModelLoaded(const std::string& name) const {
  572. std::unique_lock<std::shared_mutex> lock(pImpl->modelsMutex);
  573. auto it = pImpl->loadedModels.find(name);
  574. return it != pImpl->loadedModels.end();
  575. }
  576. size_t ModelManager::getLoadedModelsCount() const {
  577. std::shared_lock<std::shared_mutex> lock(pImpl->modelsMutex);
  578. return pImpl->loadedModels.size();
  579. }
  580. size_t ModelManager::getAvailableModelsCount() const {
  581. std::shared_lock<std::shared_mutex> lock(pImpl->modelsMutex);
  582. return pImpl->availableModels.size();
  583. }
  584. void ModelManager::setModelsDirectory(const std::string& path) {
  585. pImpl->modelsDirectory = path;
  586. }
  587. std::string ModelManager::getModelsDirectory() const {
  588. return pImpl->modelsDirectory;
  589. }
  590. std::string ModelManager::modelTypeToString(ModelType type) {
  591. switch (type) {
  592. case ModelType::LORA:
  593. return "lora";
  594. case ModelType::CHECKPOINT:
  595. return "checkpoint";
  596. case ModelType::VAE:
  597. return "vae";
  598. case ModelType::PRESETS:
  599. return "presets";
  600. case ModelType::PROMPTS:
  601. return "prompts";
  602. case ModelType::NEG_PROMPTS:
  603. return "neg_prompts";
  604. case ModelType::TAESD:
  605. return "taesd";
  606. case ModelType::ESRGAN:
  607. return "esrgan";
  608. case ModelType::CONTROLNET:
  609. return "controlnet";
  610. case ModelType::UPSCALER:
  611. return "upscaler";
  612. case ModelType::EMBEDDING:
  613. return "embedding";
  614. default:
  615. return "unknown";
  616. }
  617. }
  618. ModelType ModelManager::stringToModelType(const std::string& typeStr) {
  619. std::string lowerType = typeStr;
  620. std::transform(lowerType.begin(), lowerType.end(), lowerType.begin(), ::tolower);
  621. if (lowerType == "lora") {
  622. return ModelType::LORA;
  623. } else if (lowerType == "checkpoint" || lowerType == "stable-diffusion") {
  624. return ModelType::CHECKPOINT;
  625. } else if (lowerType == "vae") {
  626. return ModelType::VAE;
  627. } else if (lowerType == "presets") {
  628. return ModelType::PRESETS;
  629. } else if (lowerType == "prompts") {
  630. return ModelType::PROMPTS;
  631. } else if (lowerType == "neg_prompts" || lowerType == "negative_prompts") {
  632. return ModelType::NEG_PROMPTS;
  633. } else if (lowerType == "taesd") {
  634. return ModelType::TAESD;
  635. } else if (lowerType == "esrgan") {
  636. return ModelType::ESRGAN;
  637. } else if (lowerType == "controlnet") {
  638. return ModelType::CONTROLNET;
  639. } else if (lowerType == "upscaler") {
  640. return ModelType::UPSCALER;
  641. } else if (lowerType == "embedding" || lowerType == "textual-inversion") {
  642. return ModelType::EMBEDDING;
  643. }
  644. return ModelType::NONE;
  645. }
  646. bool ModelManager::setModelTypeDirectory(ModelType type, const std::string& path) {
  647. std::unique_lock<std::shared_mutex> lock(pImpl->modelsMutex);
  648. if (!pImpl->validateDirectory(path)) {
  649. return false;
  650. }
  651. pImpl->modelTypeDirectories[type] = path;
  652. pImpl->legacyMode = false;
  653. return true;
  654. }
  655. std::string ModelManager::getModelTypeDirectory(ModelType type) const {
  656. std::shared_lock<std::shared_mutex> lock(pImpl->modelsMutex);
  657. return pImpl->getModelTypeDirectory(type);
  658. }
  659. bool ModelManager::setAllModelTypeDirectories(const std::map<ModelType, std::string>& directories) {
  660. std::unique_lock<std::shared_mutex> lock(pImpl->modelsMutex);
  661. // Validate all directories first
  662. for (const auto& [type, path] : directories) {
  663. if (!path.empty() && !pImpl->validateDirectory(path)) {
  664. return false;
  665. }
  666. }
  667. // Set all directories
  668. pImpl->modelTypeDirectories = directories;
  669. pImpl->legacyMode = false;
  670. return true;
  671. }
  672. std::map<ModelType, std::string> ModelManager::getAllModelTypeDirectories() const {
  673. std::shared_lock<std::shared_mutex> lock(pImpl->modelsMutex);
  674. return pImpl->modelTypeDirectories;
  675. }
  676. void ModelManager::resetToLegacyDirectories() {
  677. // Note: This method should be called with modelsMutex already locked
  678. pImpl->modelTypeDirectories.clear();
  679. pImpl->legacyMode = true;
  680. }
  681. bool ModelManager::configureFromServerConfig(const ServerConfig& config) {
  682. std::unique_lock<std::shared_mutex> lock(pImpl->modelsMutex);
  683. // Set the base models directory
  684. pImpl->modelsDirectory = config.modelsDir;
  685. if (config.legacyMode) {
  686. // Legacy mode: use single models directory
  687. resetToLegacyDirectories();
  688. return true;
  689. } else {
  690. // Explicit mode: set per-type directories
  691. std::map<ModelType, std::string> directories;
  692. if (!config.checkpoints.empty()) {
  693. directories[ModelType::CHECKPOINT] = config.checkpoints;
  694. }
  695. if (!config.controlnetDir.empty()) {
  696. directories[ModelType::CONTROLNET] = config.controlnetDir;
  697. }
  698. if (!config.embeddingsDir.empty()) {
  699. directories[ModelType::EMBEDDING] = config.embeddingsDir;
  700. }
  701. if (!config.esrganDir.empty()) {
  702. directories[ModelType::ESRGAN] = config.esrganDir;
  703. }
  704. if (!config.loraDir.empty()) {
  705. directories[ModelType::LORA] = config.loraDir;
  706. }
  707. if (!config.taesdDir.empty()) {
  708. directories[ModelType::TAESD] = config.taesdDir;
  709. }
  710. if (!config.vaeDir.empty()) {
  711. directories[ModelType::VAE] = config.vaeDir;
  712. }
  713. // Validate all directories first
  714. for (const auto& [type, path] : directories) {
  715. if (!path.empty() && !pImpl->validateDirectory(path)) {
  716. return false;
  717. }
  718. }
  719. // Set all directories (inlined to avoid deadlock from calling setAllModelTypeDirectories)
  720. pImpl->modelTypeDirectories = directories;
  721. pImpl->legacyMode = false;
  722. return true;
  723. }
  724. }
  725. void ModelManager::cancelScan() {
  726. pImpl->scanCancelled.store(true);
  727. }
  728. // SHA256 Hashing Implementation
  729. std::string ModelManager::computeModelHash(const std::string& modelName) {
  730. std::shared_lock<std::shared_mutex> lock(pImpl->modelsMutex);
  731. auto it = pImpl->availableModels.find(modelName);
  732. if (it == pImpl->availableModels.end()) {
  733. std::cerr << "Model not found: " << modelName << std::endl;
  734. return "";
  735. }
  736. std::string filePath = it->second.fullPath;
  737. lock.unlock();
  738. std::ifstream file(filePath, std::ios::binary);
  739. if (!file.is_open()) {
  740. std::cerr << "Failed to open file for hashing: " << filePath << std::endl;
  741. return "";
  742. }
  743. // Create and initialize EVP context for SHA256
  744. EVP_MD_CTX* mdctx = EVP_MD_CTX_new();
  745. if (mdctx == nullptr) {
  746. std::cerr << "Failed to create EVP context" << std::endl;
  747. return "";
  748. }
  749. if (EVP_DigestInit_ex(mdctx, EVP_sha256(), nullptr) != 1) {
  750. std::cerr << "Failed to initialize SHA256 digest" << std::endl;
  751. EVP_MD_CTX_free(mdctx);
  752. return "";
  753. }
  754. const size_t bufferSize = 8192;
  755. char buffer[bufferSize];
  756. std::cout << "Computing SHA256 for: " << modelName << std::endl;
  757. size_t totalRead = 0;
  758. size_t lastReportedMB = 0;
  759. while (file.read(buffer, bufferSize) || file.gcount() > 0) {
  760. size_t bytesRead = file.gcount();
  761. if (EVP_DigestUpdate(mdctx, buffer, bytesRead) != 1) {
  762. std::cerr << "Failed to update digest" << std::endl;
  763. EVP_MD_CTX_free(mdctx);
  764. return "";
  765. }
  766. totalRead += bytesRead;
  767. // Progress reporting every 100MB
  768. size_t currentMB = totalRead / (1024 * 1024);
  769. if (currentMB >= lastReportedMB + 100) {
  770. std::cout << " Hashed " << currentMB << " MB..." << std::endl;
  771. lastReportedMB = currentMB;
  772. }
  773. }
  774. file.close();
  775. unsigned char hash[EVP_MAX_MD_SIZE];
  776. unsigned int hashLen = 0;
  777. if (EVP_DigestFinal_ex(mdctx, hash, &hashLen) != 1) {
  778. std::cerr << "Failed to finalize digest" << std::endl;
  779. EVP_MD_CTX_free(mdctx);
  780. return "";
  781. }
  782. EVP_MD_CTX_free(mdctx);
  783. // Convert to hex string
  784. std::ostringstream oss;
  785. for (unsigned int i = 0; i < hashLen; i++) {
  786. oss << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(hash[i]);
  787. }
  788. std::string hashStr = oss.str();
  789. std::cout << "Hash computed: " << hashStr.substr(0, 16) << "..." << std::endl;
  790. return hashStr;
  791. }
  792. std::string ModelManager::loadModelHashFromFile(const std::string& modelName) {
  793. std::shared_lock<std::shared_mutex> lock(pImpl->modelsMutex);
  794. auto it = pImpl->availableModels.find(modelName);
  795. if (it == pImpl->availableModels.end()) {
  796. return "";
  797. }
  798. std::string jsonPath = it->second.fullPath + ".json";
  799. lock.unlock();
  800. if (!fs::exists(jsonPath)) {
  801. return "";
  802. }
  803. try {
  804. std::ifstream jsonFile(jsonPath);
  805. if (!jsonFile.is_open()) {
  806. return "";
  807. }
  808. nlohmann::json j;
  809. jsonFile >> j;
  810. jsonFile.close();
  811. if (j.contains("sha256") && j["sha256"].is_string()) {
  812. return j["sha256"].get<std::string>();
  813. }
  814. } catch (const std::exception& e) {
  815. std::cerr << "Error loading hash from JSON: " << e.what() << std::endl;
  816. }
  817. return "";
  818. }
  819. bool ModelManager::saveModelHashToFile(const std::string& modelName, const std::string& hash) {
  820. std::shared_lock<std::shared_mutex> lock(pImpl->modelsMutex);
  821. auto it = pImpl->availableModels.find(modelName);
  822. if (it == pImpl->availableModels.end()) {
  823. return false;
  824. }
  825. std::string jsonPath = it->second.fullPath + ".json";
  826. size_t fileSize = it->second.fileSize;
  827. lock.unlock();
  828. try {
  829. nlohmann::json j;
  830. j["sha256"] = hash;
  831. j["file_size"] = fileSize;
  832. j["computed_at"] = std::chrono::system_clock::now().time_since_epoch().count();
  833. std::ofstream jsonFile(jsonPath);
  834. if (!jsonFile.is_open()) {
  835. std::cerr << "Failed to open file for writing: " << jsonPath << std::endl;
  836. return false;
  837. }
  838. jsonFile << j.dump(2);
  839. jsonFile.close();
  840. std::cout << "Saved hash to: " << jsonPath << std::endl;
  841. return true;
  842. } catch (const std::exception& e) {
  843. std::cerr << "Error saving hash to JSON: " << e.what() << std::endl;
  844. return false;
  845. }
  846. }
  847. std::string ModelManager::findModelByHash(const std::string& hash) {
  848. if (hash.length() < 10) {
  849. std::cerr << "Hash must be at least 10 characters" << std::endl;
  850. return "";
  851. }
  852. std::shared_lock<std::shared_mutex> lock(pImpl->modelsMutex);
  853. for (const auto& [name, info] : pImpl->availableModels) {
  854. if (info.sha256.empty()) {
  855. continue;
  856. }
  857. // Support full or partial match (minimum 10 chars)
  858. if (info.sha256 == hash || info.sha256.substr(0, hash.length()) == hash) {
  859. return name;
  860. }
  861. }
  862. return "";
  863. }
  864. std::string ModelManager::ensureModelHash(const std::string& modelName, bool forceCompute) {
  865. // Try to load existing hash if not forcing recompute
  866. if (!forceCompute) {
  867. std::string existingHash = loadModelHashFromFile(modelName);
  868. if (!existingHash.empty()) {
  869. // Update in-memory model info
  870. std::unique_lock<std::shared_mutex> lock(pImpl->modelsMutex);
  871. auto it = pImpl->availableModels.find(modelName);
  872. if (it != pImpl->availableModels.end()) {
  873. it->second.sha256 = existingHash;
  874. }
  875. return existingHash;
  876. }
  877. }
  878. // Compute new hash
  879. std::string hash = computeModelHash(modelName);
  880. if (hash.empty()) {
  881. return "";
  882. }
  883. // Save to file
  884. saveModelHashToFile(modelName, hash);
  885. // Update in-memory model info
  886. std::unique_lock<std::shared_mutex> lock(pImpl->modelsMutex);
  887. auto it = pImpl->availableModels.find(modelName);
  888. if (it != pImpl->availableModels.end()) {
  889. it->second.sha256 = hash;
  890. }
  891. return hash;
  892. }