Skip to content

Commit 31f4c84

Browse files
committed
fix naming error, add log & update doc
1 parent 439cbca commit 31f4c84

File tree

4 files changed

+82
-14
lines changed

4 files changed

+82
-14
lines changed

README.md

+10-3
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,20 @@
1-
# stable_diffusion_cpp_py
1+
# stable-diffusion-cpp-py
22

3-
python bind for stable_diffusion_cpp by pybind11
3+
python bind for stable-diffusion.cpp
4+
5+
## description
6+
7+
a simple stable_diffusion_cpp python wrapper by using pybind11 and magic_enum, aim to provide a python interface for stable-diffusion.cpp to build upper application
48

59
## install
610

711
- if you use nvidia gpu, execute `CMAKE_ARGS="-DSD_CUBLAS=on" pip install git+https://github.com/null-define/stable-diffusion-cpp-py` install
812
- if you use amd gpu, execute `CC=/opt/rocm/llvm/bin/clang CXX=/opt/rocm/llvm/bin/clang++ CMAKE_ARGS="-DSD_HIPBLAS=ON -DAMDGPU_TARGETS=gfx1100" pip install git+https://github.com/null-define/stable-diffusion-cpp-py` install
913

14+
- To upgrade and rebuild stable-diffusion-cpp-py add --upgrade --force-reinstall --no-cache-dir flags to the pip install command to ensure the package is rebuilt from source.
1015

1116
## References
17+
1218
- [stable-diffusion.cpp](https://github.com/leejet/stable-diffusion.cpp)
13-
- [pybind11](https://github.com/pybind/pybind11)
19+
- [pybind11](https://github.com/pybind/pybind11)
20+
- [stable-diffusion-cpp-py](https://github.com/abetlen/llama-cpp-python)

sd_cpp_lib/CMakeLists.txt

+1-1
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,6 @@ add_subdirectory(./stable-diffusion.cpp EXCLUDE_FROM_ALL)
1717

1818
set(TARGET sd_cpp_lib)
1919
pybind11_add_module(${TARGET} sd_cpp_lib.cc)
20-
target_link_libraries(${TARGET} PRIVATE stable-diffusion)
20+
target_link_libraries(${TARGET} PRIVATE stable-diffusion ggml)
2121

2222

sd_cpp_lib/sd_cpp_lib.cc

+49-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,50 @@
11
#include "magic_enum/magic_enum.hpp"
22
#include "pybind11/pybind11.h"
33
#include "stable-diffusion.h"
4+
#include "util.h"
45
#include <memory>
56

7+
static sd_log_level_t g_cfg_log_level = SD_LOG_INFO;
8+
9+
/* Enables Printing the log level tag in color using ANSI escape codes */
10+
void sd_log_cb(enum sd_log_level_t level, char const* log, void* data) {
11+
int tag_color;
12+
char const* level_str;
13+
FILE* out_stream = (level == SD_LOG_ERROR) ? stderr : stdout;
14+
15+
if (!log || level < g_cfg_log_level) {
16+
return;
17+
}
18+
19+
switch (level) {
20+
case SD_LOG_DEBUG:
21+
tag_color = 37;
22+
level_str = "DEBUG";
23+
break;
24+
case SD_LOG_INFO:
25+
tag_color = 34;
26+
level_str = "INFO";
27+
break;
28+
case SD_LOG_WARN:
29+
tag_color = 35;
30+
level_str = "WARN";
31+
break;
32+
case SD_LOG_ERROR:
33+
tag_color = 31;
34+
level_str = "ERROR";
35+
break;
36+
default: /* Potential future-proofing */
37+
tag_color = 33;
38+
level_str = "?????";
39+
break;
40+
}
41+
42+
fprintf(out_stream, "[%-5s] ", level_str);
43+
44+
fputs(log, out_stream);
45+
fflush(out_stream);
46+
}
47+
648
template <typename T>
749
using deleted_unique_ptr = std::unique_ptr<T, std::function<void(T*)>>;
850

@@ -37,6 +79,9 @@ PYBIND11_MODULE(sd_cpp_lib, m) {
3779
export_enum<rng_type_t>(m);
3880
export_enum<schedule_t>(m);
3981
export_enum<sample_method_t>(m);
82+
export_enum<sd_log_level_t>(m);
83+
m.def("set_log_level",
84+
[](sd_log_level_t log_level) { g_cfg_log_level = log_level; });
4085
m.def(
4186
"new_sd_ctx",
4287
[](std::string const& model_path, std::string const& vae_path,
@@ -47,12 +92,14 @@ PYBIND11_MODULE(sd_cpp_lib, m) {
4792
sd_type_t wtype, rng_type_t rng_type, schedule_t s,
4893
bool keep_clip_on_cpu, bool keep_control_net_cpu,
4994
bool keep_vae_on_cpu) {
50-
return static_cast<py::capsule>(new_sd_ctx(
95+
sd_set_log_callback(sd_log_cb, nullptr);
96+
auto ptr = new_sd_ctx(
5197
model_path.c_str(), vae_path.c_str(), taesd_path.c_str(),
5298
control_net_path.c_str(), lora_model_dir.c_str(), embed_dir.c_str(),
5399
stacked_id_embed_dir.c_str(), vae_decode_only, vae_tiling,
54100
free_params_immediately, n_threads, wtype, rng_type, s,
55-
keep_clip_on_cpu, keep_control_net_cpu, keep_vae_on_cpu));
101+
keep_clip_on_cpu, keep_control_net_cpu, keep_vae_on_cpu);
102+
return static_cast<py::capsule>(ptr);
56103
},
57104
"create stable diffusion context");
58105
m.def(

stable_diffusion_cpp/stable_diffusion_cpp.py

+22-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,12 @@
1-
import sd_cpp_lib
2-
from sd_cpp_lib import sd_image_t, sd_type_t, rng_type_t, schedule_t, sample_method_t
1+
from stable_diffusion_cpp import sd_cpp_lib
2+
from stable_diffusion_cpp.sd_cpp_lib import (
3+
sd_image_t,
4+
sd_type_t,
5+
rng_type_t,
6+
schedule_t,
7+
sample_method_t,
8+
sd_log_level_t,
9+
)
310

411

512
class StableDiffusion:
@@ -23,8 +30,13 @@ def __init__(
2330
keep_control_net_cpu=False,
2431
keep_vae_on_cpu=False,
2532
esrgan_path="",
33+
log_level=sd_log_level_t.SD_LOG_WARN,
2634
) -> None:
27-
self.sd_ctx = sdcpy.new_sd_ctx(
35+
self.sd_ctx = None
36+
self.upscaler_ctx = None
37+
sd_cpp_lib.set_log_level(log_level)
38+
39+
self.sd_ctx = sd_cpp_lib.new_sd_ctx(
2840
model_path,
2941
vae_path,
3042
taesd_path,
@@ -43,12 +55,14 @@ def __init__(
4355
keep_control_net_cpu,
4456
keep_vae_on_cpu,
4557
)
46-
self.upscaler_ctx = None
4758
if esrgan_path:
48-
self.upscaler_ctx = sdcpy.new_upscaler_ctx(esrgan_path, n_threads, wtype)
59+
self.upscaler_ctx = sd_cpp_lib.new_upscaler_ctx(
60+
esrgan_path, n_threads, wtype
61+
)
4962

5063
def __del__(self):
51-
sdcpy.free_sd_ctx(self.sd_ctx)
64+
if self.sd_ctx:
65+
sd_cpp_lib.free_sd_ctx(self.sd_ctx)
5266

5367
def txt_to_img(
5468
self,
@@ -70,7 +84,7 @@ def txt_to_img(
7084
):
7185
prompt_str = ",".join(prompt)
7286
negative_prompt_str = ",".join(negative_prompt)
73-
return sdcpy.txt2img(
87+
return sd_cpp_lib.txt2img(
7488
self.sd_ctx,
7589
prompt_str,
7690
negative_prompt_str,
@@ -92,4 +106,4 @@ def txt_to_img(
92106
def upscale_img(self, img, upscale_factor):
93107
if not self.upscaler_ctx:
94108
return img
95-
return sdcpy.upscale(self.upscaler_ctx, img, upscale_factor)
109+
return sd_cpp_lib.upscale(self.upscaler_ctx, img, upscale_factor)

0 commit comments

Comments
 (0)