Skip to content

Commit

Permalink
minimize _server and _diffuser deps, added Dockerfile to server proje…
Browse files Browse the repository at this point in the history
…ct, prepared the GUI
  • Loading branch information
fszontagh committed Jan 9, 2025
1 parent ebf7b8a commit 3de800e
Show file tree
Hide file tree
Showing 35 changed files with 1,853 additions and 1,032 deletions.
4 changes: 2 additions & 2 deletions extprocess/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ endif()
install(TARGETS ${EPROCESS_BINARY_NAME} RUNTIME DESTINATION ${bin_INSTALL_PATH_NSIS} COMPONENT "${CMAKE_PROJECT_NAME}")

if(WIN32)
target_link_libraries(${EPROCESS_BINARY_NAME} PRIVATE wxWidgets::wxWidgets exiv2lib OpenSSL::Crypto)
target_link_libraries(${EPROCESS_BINARY_NAME} PRIVATE wxWidgets::wxWidgets OpenSSL::Crypto)
else(WIN32)
target_link_libraries(${EPROCESS_BINARY_NAME} PRIVATE wx::base wx::core exiv2lib OpenSSL::Crypto)
target_link_libraries(${EPROCESS_BINARY_NAME} PRIVATE wx::base OpenSSL::Crypto)
endif(WIN32)
77 changes: 38 additions & 39 deletions extprocess/src/ApplicationLogic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ ApplicationLogic::ApplicationLogic(const std::string& libName, std::shared_ptr<S
#else
this->tempPath = std::filesystem::temp_directory_path().string();
#endif
std::cout << "Using tmp path: " << this->tempPath << std::endl;
}

ApplicationLogic::~ApplicationLogic() {
Expand Down Expand Up @@ -141,30 +140,30 @@ void ApplicationLogic::processMessage(QM::QueueItem& item) {
std::cout << "[EXTPROCESS] Processing item: " << this->currentItem->id << std::endl;

if (this->currentItem->need_sha256 == true) {
this->sendStatus(QM::QueueStatus::HASHING, QM::QueueEvents::ITEM_MODEL_HASH_START);
this->sendStatus(QueueStatus::HASHING, QueueEvents::ITEM_MODEL_HASH_START);
this->currentItem->hash_fullsize = std::filesystem::file_size(this->currentItem->params.model_path.c_str());

this->currentItem->generated_sha256 = sd_gui_utils::sha256_file_openssl(
this->currentItem->params.model_path.c_str(),
(void*)this,
ApplicationLogic::HandleHashCallback);

this->sendStatus(QM::QueueStatus::HASHING, QM::QueueEvents::ITEM_MODEL_HASH_DONE, "", EPROCESS_SLEEP_TIME);
this->sendStatus(QueueStatus::HASHING, QueueEvents::ITEM_MODEL_HASH_DONE, "", EPROCESS_SLEEP_TIME);
}

this->sendStatus(QM::QueueStatus::MODEL_LOADING, QM::QueueEvents::ITEM_MODEL_LOAD_START, "", EPROCESS_SLEEP_TIME);
this->sendStatus(QueueStatus::MODEL_LOADING, QueueEvents::ITEM_MODEL_LOAD_START, "", EPROCESS_SLEEP_TIME);
// on mode convert always return true, because no model loading
if (this->loadSdModel() == false) {
if (this->currentItem->mode == QM::GenerationMode::TXT2IMG || this->currentItem->mode == QM::GenerationMode::IMG2IMG) {
if (this->currentItem->mode == SDMode::TXT2IMG || this->currentItem->mode == SDMode::IMG2IMG) {
std::cerr << "[EXTPROCESS] Failed to load model: " << this->currentItem->params.model_path << std::endl;
this->currentItem->status_message = _("Failed to load model: ") + this->currentItem->params.model_path;
}

if (this->currentItem->mode == QM::GenerationMode::UPSCALE) {
if (this->currentItem->mode == SDMode::UPSCALE) {
std::cerr << "[EXTPROCESS] Failed to load model: " << this->currentItem->params.esrgan_path << std::endl;
this->currentItem->status_message = _("Failed to load model: ") + this->currentItem->params.esrgan_path;
}
this->sendStatus(QM::QueueStatus::FAILED, QM::QueueEvents::ITEM_MODEL_FAILED);
this->sendStatus(QueueStatus::FAILED, QueueEvents::ITEM_MODEL_FAILED);
this->currentItem = nullptr;
this->lastItem = nullptr;
return;
Expand All @@ -174,43 +173,43 @@ void ApplicationLogic::processMessage(QM::QueueItem& item) {
this->currentItem->step = 0;
this->currentItem->steps = 1;

this->sendStatus(QM::QueueStatus::RUNNING, QM::QueueEvents::ITEM_MODEL_LOADED, "", EPROCESS_SLEEP_TIME);
this->sendStatus(QueueStatus::RUNNING, QueueEvents::ITEM_MODEL_LOADED, "", EPROCESS_SLEEP_TIME);

// handle the convert differently
if (this->currentItem->mode == QM::GenerationMode::CONVERT) {
this->sendStatus(QM::QueueStatus::RUNNING, QM::QueueEvents::ITEM_GENERATION_STARTED, "", EPROCESS_SLEEP_TIME);
if (this->currentItem->mode == SDMode::CONVERT) {
this->sendStatus(QueueStatus::RUNNING, QueueEvents::ITEM_GENERATION_STARTED, "", EPROCESS_SLEEP_TIME);
bool status = this->convertFuncPtr(this->currentItem->params.model_path.c_str(), this->currentItem->params.vae_path.c_str(), this->currentItem->params.output_path.c_str(), this->currentItem->params.wtype);
if (status == false) {
this->sendStatus(QM::QueueStatus::FAILED, QM::QueueEvents::ITEM_FAILED);
this->sendStatus(QueueStatus::FAILED, QueueEvents::ITEM_FAILED);
this->currentItem = nullptr;
this->lastItem = nullptr;
return;
}
this->sendStatus(QM::QueueStatus::DONE, QM::QueueEvents::ITEM_FINISHED, "", EPROCESS_SLEEP_TIME);
this->sendStatus(QueueStatus::DONE, QueueEvents::ITEM_FINISHED, "", EPROCESS_SLEEP_TIME);
this->currentItem = nullptr;
this->lastItem = nullptr;
return;
}

std::cout << "[EXTPROCESS] Starting item: " << this->currentItem->id << " type: " << QM::GenerationMode_str.at(this->currentItem->mode) << std::endl;
std::cout << "[EXTPROCESS] Starting item: " << this->currentItem->id << " type: " << GenerationMode_str.at(this->currentItem->mode) << std::endl;
switch (this->currentItem->mode) {
case QM::GenerationMode::TXT2IMG: {
case SDMode::TXT2IMG: {
std::cout << "[EXTPROCESS] Running txt2img" << std::endl;
Txt2Img();
} break;
case QM::GenerationMode::IMG2IMG: {
case SDMode::IMG2IMG: {
std::cout << "[EXTPROCESS] Running img2img" << std::endl;
Img2img();
} break;
case QM::GenerationMode::UPSCALE: {
case SDMode::UPSCALE: {
std::cout << "[EXTPROCESS] Running upscale" << std::endl;
Upscale();
} break;

default: {
std::this_thread::sleep_for(std::chrono::milliseconds(EPROCESS_SLEEP_TIME));
this->currentItem->status_message = wxString::Format(_("Unknown mode: %s"), QM::GenerationMode_str.at(this->currentItem->mode).c_str());
this->sendStatus(QM::QueueStatus::FAILED, QM::QueueEvents::ITEM_FAILED);
this->currentItem->status_message = wxString::Format(_("Unknown mode: %s"), GenerationMode_str.at(this->currentItem->mode).c_str());
this->sendStatus(QueueStatus::FAILED, QueueEvents::ITEM_FAILED);
std::cerr << this->currentItem->status_message << std::endl;
this->lastItem = nullptr;
this->currentItem = nullptr;
Expand All @@ -224,7 +223,7 @@ void ApplicationLogic::processMessage(QM::QueueItem& item) {
void ApplicationLogic::Txt2Img() {
try {
std::this_thread::sleep_for(std::chrono::milliseconds(EPROCESS_SLEEP_TIME * 2));
this->sendStatus(QM::QueueStatus::RUNNING, QM::QueueEvents::ITEM_GENERATION_STARTED);
this->sendStatus(QueueStatus::RUNNING, QueueEvents::ITEM_GENERATION_STARTED);

sd_image_t* control_image = NULL;
sd_image_t* results;
Expand Down Expand Up @@ -272,7 +271,7 @@ void ApplicationLogic::Txt2Img() {

if (results == NULL) {
std::cout << "[EXTPROCESS] txt2img failed" << std::endl;
this->sendStatus(QM::QueueStatus::FAILED, QM::QueueEvents::ITEM_FAILED);
this->sendStatus(QueueStatus::FAILED, QueueEvents::ITEM_FAILED);
return;
}

Expand All @@ -290,12 +289,12 @@ void ApplicationLogic::Txt2Img() {
auto finished_at = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
std::this_thread::sleep_for(std::chrono::milliseconds(EPROCESS_SLEEP_TIME * 2));
this->currentItem->finished_at = finished_at;
this->sendStatus(QM::QueueStatus::DONE, QM::QueueEvents::ITEM_FINISHED);
this->sendStatus(QueueStatus::DONE, QueueEvents::ITEM_FINISHED);
delete results;

} catch (const std::exception& e) {
std::this_thread::sleep_for(std::chrono::milliseconds(EPROCESS_SLEEP_TIME));
this->sendStatus(QM::QueueStatus::FAILED, QM::QueueEvents::ITEM_FAILED);
this->sendStatus(QueueStatus::FAILED, QueueEvents::ITEM_FAILED);
std::cerr << "Error calling txt2img: " << e.what() << std::endl;
}
}
Expand All @@ -305,7 +304,7 @@ void ApplicationLogic::Img2img() {
std::cout << "running img2img" << std::endl;
try {
std::this_thread::sleep_for(std::chrono::milliseconds(EPROCESS_SLEEP_TIME * 2));
this->sendStatus(QM::QueueStatus::RUNNING, QM::QueueEvents::ITEM_GENERATION_STARTED);
this->sendStatus(QueueStatus::RUNNING, QueueEvents::ITEM_GENERATION_STARTED);
std::cout << " sent status" << std::endl;

unsigned char* input_image_buffer = NULL;
Expand All @@ -329,13 +328,13 @@ void ApplicationLogic::Img2img() {
} else {
std::cerr << "Initial image not found: " << this->currentItem->initial_image << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(EPROCESS_SLEEP_TIME));
this->sendStatus(QM::QueueStatus::FAILED, QM::QueueEvents::ITEM_FAILED, "Initial image not found:" + this->currentItem->initial_image);
this->sendStatus(QueueStatus::FAILED, QueueEvents::ITEM_FAILED, "Initial image not found:" + this->currentItem->initial_image);
return;
}
} else {
std::cerr << "Missing input image" << std::endl;
std::this_thread::sleep_for(std::chrono::milliseconds(EPROCESS_SLEEP_TIME));
this->sendStatus(QM::QueueStatus::FAILED, QM::QueueEvents::ITEM_FAILED, "Missing input image");
this->sendStatus(QueueStatus::FAILED, QueueEvents::ITEM_FAILED, "Missing input image");
return;
}
if (this->currentItem->mask_image.length() > 0 && std::filesystem::exists(this->currentItem->mask_image)) {
Expand Down Expand Up @@ -396,7 +395,7 @@ void ApplicationLogic::Img2img() {
delete control_image;

if (results == NULL) {
this->sendStatus(QM::QueueStatus::FAILED, QM::QueueEvents::ITEM_FAILED);
this->sendStatus(QueueStatus::FAILED, QueueEvents::ITEM_FAILED);
return;
}
for (int i = 0; i < this->currentItem->params.batch_count; i++) {
Expand All @@ -413,12 +412,12 @@ void ApplicationLogic::Img2img() {
auto finished_at = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
std::this_thread::sleep_for(std::chrono::milliseconds(EPROCESS_SLEEP_TIME * 2));
this->currentItem->finished_at = finished_at;
this->sendStatus(QM::QueueStatus::DONE, QM::QueueEvents::ITEM_FINISHED);
this->sendStatus(QueueStatus::DONE, QueueEvents::ITEM_FINISHED);

delete results;

} catch (const std::exception& e) {
this->sendStatus(QM::QueueStatus::FAILED, QM::QueueEvents::ITEM_FAILED);
this->sendStatus(QueueStatus::FAILED, QueueEvents::ITEM_FAILED);
std::cerr << "Error calling img2img: " << e.what() << std::endl;
}
}
Expand All @@ -433,18 +432,18 @@ void ApplicationLogic::Upscale() {
if (input_image_buffer == NULL) {
std::cerr << "Failed to load image: " << this->currentItem->initial_image << std::endl;
this->currentItem->status_message = _("Failed to load image: ") + this->currentItem->initial_image;
this->sendStatus(QM::QueueStatus::FAILED, QM::QueueEvents::ITEM_FAILED);
this->sendStatus(QueueStatus::FAILED, QueueEvents::ITEM_FAILED);
return;
}
sd_image_t control_image = sd_image_t{(uint32_t)w, (uint32_t)h, 3, input_image_buffer};

std::this_thread::sleep_for(std::chrono::milliseconds(EPROCESS_SLEEP_TIME));
this->sendStatus(QM::QueueStatus::RUNNING, QM::QueueEvents::ITEM_GENERATION_STARTED);
this->sendStatus(QueueStatus::RUNNING, QueueEvents::ITEM_GENERATION_STARTED);

results = this->upscalerFuncPtr(this->upscale_ctx, control_image, this->currentItem->upscale_factor);

if (results.data == NULL) {
this->sendStatus(QM::QueueStatus::FAILED, QM::QueueEvents::ITEM_FAILED);
this->sendStatus(QueueStatus::FAILED, QueueEvents::ITEM_FAILED);
stbi_image_free(input_image_buffer);
return;
}
Expand All @@ -458,7 +457,7 @@ void ApplicationLogic::Upscale() {
auto finished_at = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
std::this_thread::sleep_for(std::chrono::milliseconds(EPROCESS_SLEEP_TIME * 2));
this->currentItem->finished_at = finished_at;
this->sendStatus(QM::QueueStatus::DONE, QM::QueueEvents::ITEM_FINISHED);
this->sendStatus(QueueStatus::DONE, QueueEvents::ITEM_FINISHED);
stbi_image_free(input_image_buffer);
}

Expand Down Expand Up @@ -490,7 +489,7 @@ std::string ApplicationLogic::handleSdImage(sd_image_t& image) {

bool ApplicationLogic::loadSdModel() {
// on covert, there is no model loading into ctx, but need to clean up memory
if (this->currentItem->mode == QM::GenerationMode::CONVERT) {
if (this->currentItem->mode == SDMode::CONVERT) {
// remove already loaded models
if (this->sd_ctx != nullptr && this->currentItem->keep_checkpoint_in_memory == false) {
std::cout << "Freeing up previous sd model" << std::endl;
Expand All @@ -506,7 +505,7 @@ bool ApplicationLogic::loadSdModel() {
}

// hnalde upscaler model
if (this->currentItem->mode == QM::GenerationMode::UPSCALE) {
if (this->currentItem->mode == SDMode::UPSCALE) {
std::cout << "Loading upscale model: " << this->currentItem->params.esrgan_path << std::endl;
// free up the sd model
if (this->sd_ctx != nullptr && this->currentItem->keep_checkpoint_in_memory == false) {
Expand All @@ -516,7 +515,7 @@ bool ApplicationLogic::loadSdModel() {
}

// check if we need reload the model
if (this->lastItem != nullptr && this->lastItem->mode == QM::GenerationMode::UPSCALE) {
if (this->lastItem != nullptr && this->lastItem->mode == SDMode::UPSCALE) {
std::cout << "Previous model is upscale" << std::endl;
if (this->lastItem->params.esrgan_path != this->currentItem->params.esrgan_path) {
std::cout << "upscaler model changed" << std::endl;
Expand All @@ -535,7 +534,7 @@ bool ApplicationLogic::loadSdModel() {
this->upscale_ctx = this->newUpscalerCtxPtr(this->currentItem->params.esrgan_path.c_str(), this->currentItem->params.n_threads, this->currentItem->params.wtype);
return this->upscale_ctx != NULL;
}
if (this->currentItem->mode == QM::GenerationMode::TXT2IMG || this->currentItem->mode == QM::GenerationMode::IMG2IMG) {
if (this->currentItem->mode == SDMode::TXT2IMG || this->currentItem->mode == SDMode::IMG2IMG) {
if (this->currentItem->params.model_path == "") {
std::cout << "Loading sd model: " << this->currentItem->params.diffusion_model_path << std::endl;
} else {
Expand Down Expand Up @@ -654,11 +653,11 @@ bool ApplicationLogic::loadSdModel() {
bool vae_decode_only = true;

switch (this->currentItem->mode) {
case QM::GenerationMode::IMG2IMG:
// case QM::GenerationMode::IMG2VID:
case SDMode::IMG2IMG:
// case SDMode::IMG2VID:
vae_decode_only = false;
break;
case QM::GenerationMode::TXT2IMG:
case SDMode::TXT2IMG:
vae_decode_only = true;
break;
default:
Expand Down
10 changes: 5 additions & 5 deletions extprocess/src/ApplicationLogic.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ class ApplicationLogic {
if (item != nullptr) {
item->hash_progress_size = readed;
item->generated_sha256 = hash;
instance->sendStatus(QM::QueueStatus::HASHING, QM::QueueEvents::ITEM_MODEL_HASH_UPDATE);
instance->sendStatus(QueueStatus::HASHING, QueueEvents::ITEM_MODEL_HASH_UPDATE);
}
}

Expand All @@ -48,15 +48,15 @@ class ApplicationLogic {
}

// instance->currentItem->stats.time_per_step[step] = time;
instance->currentItem->stats.time_per_step.emplace_back(QM::QueueStatsStepItem(step, steps, time));
instance->currentItem->stats.time_per_step.emplace_back(QueueStatsStepItem(step, steps, time));
instance->currentItem->stats.time_total += time;

if (instance->currentItem->step > 0) {
instance->currentItem->stats.time_avg = instance->currentItem->stats.time_total / instance->currentItem->step;
}

instance->currentItem->updated_at = std::chrono::duration_cast<std::chrono::seconds>(std::chrono::system_clock::now().time_since_epoch()).count();
instance->sendStatus(QM::QueueEvents::ITEM_UPDATED);
instance->sendStatus(QueueEvents::ITEM_UPDATED);
}

bool loadLibrary();
Expand Down Expand Up @@ -112,7 +112,7 @@ class ApplicationLogic {
* status_message field. If a sleep time is provided, the function will
* sleep for that amount of time before sending the message.
*/
inline void sendStatus(QM::QueueStatus status, QM::QueueEvents event, const std::string& reason = "", unsigned int sleep = 0) {
inline void sendStatus(QueueStatus status, QueueEvents event, const std::string& reason = "", unsigned int sleep = 0) {
if (this->currentItem == nullptr) {
return;
}
Expand All @@ -135,7 +135,7 @@ class ApplicationLogic {
std::string jsonString = j.dump();
this->sharedMemoryManager->write(jsonString.c_str(), jsonString.length());
}
inline void sendStatus(QM::QueueEvents event, const std::string& reason = "", unsigned int sleep = 0) {
inline void sendStatus(QueueEvents event, const std::string& reason = "", unsigned int sleep = 0) {
if (this->currentItem == nullptr) {
return;
}
Expand Down
11 changes: 8 additions & 3 deletions extprocess/src/main.cpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,20 @@

int main(int argc, char* argv[]) {
if (argc != 2) {
std::cerr << "Usage: " << argv[0] << " <dynamiclib_name>" << std::endl;
if (argc < 2) {
std::cerr << "Usage: " << argv[0] << " <dynamiclib_name> [shared_memory_path]" << std::endl;
return 1;
}
std::string shared_memory_path = SHARED_MEMORY_PATH;

if (argv[2]) {
shared_memory_path = argv[2];
}

std::shared_ptr<SharedMemoryManager> sharedMemory = nullptr;

std::cout << "[EXTPROCESS] starting with shared memory size: " << SHARED_MEMORY_SIZE << std::endl;
try {
sharedMemory = std::make_shared<SharedMemoryManager>(SHARED_MEMORY_PATH, SHARED_MEMORY_SIZE, false);
sharedMemory = std::make_shared<SharedMemoryManager>(shared_memory_path.c_str(), SHARED_MEMORY_SIZE, false);
} catch (const std::exception& e) {
std::cerr << "[EXTPROCESS] Failed to create SharedMemoryManager: " << e.what() << std::endl;
return 1;
Expand Down
3 changes: 3 additions & 0 deletions extprocess/src/pch.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,9 @@
#include <wx/string.h>
#include <wx/translation.h>
#include <wx/filename.h>
#include "helpers/sd.hpp"
#include "helpers/QueueItem.h"
#include "network/RemoteQueueJobItem.h"
#include "network/packets.h"
#include "helpers/sd.hpp"
#include "helpers/sslUtils.hpp"
Expand Down
Loading

0 comments on commit 3de800e

Please sign in to comment.