Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 29 additions & 7 deletions util.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,9 @@
#include <unordered_set>
#include <vector>
#include "preprocessing.hpp"
#include "ggml-cpu.h"
#include "ggml.h"
#include "stable-diffusion.h"

#if defined(__APPLE__) && defined(__MACH__)
#include <sys/sysctl.h>
Expand All @@ -23,9 +26,17 @@
#include <unistd.h>
#endif

#include "ggml-cpu.h"
#include "ggml.h"
#include "stable-diffusion.h"
#ifdef _WIN32
#define NOMINMAX
#include <windows.h>

#else // Unix
#include <dirent.h>
#include <sys/stat.h>

#endif

namespace sd_internal {

bool ends_with(const std::string& str, const std::string& ending) {
if (str.length() >= ending.length()) {
Expand Down Expand Up @@ -82,8 +93,6 @@ int round_up_to(int value, int base) {
}

#ifdef _WIN32 // code for windows
#define NOMINMAX
#include <windows.h>

bool file_exists(const std::string& filename) {
DWORD attributes = GetFileAttributesA(filename.c_str());
Expand All @@ -110,8 +119,6 @@ std::string get_full_path(const std::string& dir, const std::string& filename) {
}

#else // Unix
#include <dirent.h>
#include <sys/stat.h>

bool file_exists(const std::string& filename) {
struct stat buffer;
Expand Down Expand Up @@ -145,6 +152,8 @@ std::string get_full_path(const std::string& dir, const std::string& filename) {

#endif

}

// get_num_physical_cores is copy from
// https://github.com/ggerganov/llama.cpp/blob/master/examples/common.cpp
// LICENSE: https://github.com/ggerganov/llama.cpp/blob/master/LICENSE
Expand Down Expand Up @@ -183,6 +192,8 @@ int32_t get_num_physical_cores() {
return n_threads > 0 ? (n_threads <= 4 ? n_threads : n_threads / 2) : 4;
}

namespace sd_internal {

static sd_progress_cb_t sd_progress_cb = nullptr;
void* sd_progress_cb_data = nullptr;

Expand Down Expand Up @@ -327,6 +338,8 @@ void log_printf(sd_log_level_t level, const char* file, int line, const char* fo
va_end(args);
}

}

void sd_set_log_callback(sd_log_cb_t cb, void* data) {
sd_log_cb = cb;
sd_log_cb_data = data;
Expand All @@ -343,6 +356,8 @@ void sd_set_preview_callback(sd_preview_cb_t cb, preview_t mode = PREVIEW_PROJ,
sd_preview_noisy = noisy;
}

namespace sd_internal {

sd_preview_cb_t sd_get_preview_callback() {
return sd_preview_cb;
}
Expand All @@ -366,6 +381,9 @@ sd_progress_cb_t sd_get_progress_callback() {
void* sd_get_progress_callback_data() {
return sd_progress_cb_data;
}

}

const char* sd_get_system_info() {
static char buffer[1024];
std::stringstream ss;
Expand All @@ -387,6 +405,8 @@ const char* sd_get_system_info() {
return buffer;
}

namespace sd_internal {

sd_image_f32_t sd_image_t_to_sd_image_f32_t(sd_image_t image) {
sd_image_f32_t converted_image;
converted_image.width = image.width;
Expand Down Expand Up @@ -651,3 +671,5 @@ std::vector<std::pair<std::string, float>> parse_prompt_attention(const std::str

return res;
}

}
5 changes: 5 additions & 0 deletions util.h
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a .h file, not .hpp. There is no such thing as namespaces in C code. Maybe use #ifdef __cplusplus? Or use a prefix instead of a namespace.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Actually the whole file contains c++ stuff like std::strings, nevermind. It should probably be a .hpp instad.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah, that is far from consistent right now. I believe stable-diffusion.h is the only file that's intended to keep compatibility with C.

Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@
#define SAFE_STR(s) ((s) ? (s) : "")
#define BOOL_STR(b) ((b) ? "true" : "false")

inline namespace sd_internal {

bool ends_with(const std::string& str, const std::string& ending);
bool starts_with(const std::string& str, const std::string& start);
bool contains(const std::string& str, const std::string& substr);
Expand Down Expand Up @@ -67,4 +69,7 @@ bool sd_should_preview_noisy();
#define LOG_INFO(format, ...) log_printf(SD_LOG_INFO, __FILE__, __LINE__, format, ##__VA_ARGS__)
#define LOG_WARN(format, ...) log_printf(SD_LOG_WARN, __FILE__, __LINE__, format, ##__VA_ARGS__)
#define LOG_ERROR(format, ...) log_printf(SD_LOG_ERROR, __FILE__, __LINE__, format, ##__VA_ARGS__)

}

#endif // __UTIL_H__
Loading