Skip to content

Commit 10bf108

Browse files
richiejpmudler
andauthored
chore: ⬆️ Update leejet/stable-diffusion.cpp to 0ebe6fe118f125665939b27c89f34ed38716bff8 (#6271)
* ⬆️ Update leejet/stable-diffusion.cpp Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> * fix(stablediffusion-ggml): Move parameters and start refactor of passing params Signed-off-by: Richard Palethorpe <io@richiejp.com> * fix(stablediffusion-ggml): Add default sampler option Signed-off-by: Richard Palethorpe <io@richiejp.com> --------- Signed-off-by: github-actions[bot] <41898282+github-actions[bot]@users.noreply.github.com> Signed-off-by: Richard Palethorpe <io@richiejp.com> Co-authored-by: mudler <2420543+mudler@users.noreply.github.com>
1 parent b08ae55 commit 10bf108

File tree

5 files changed

+127
-41
lines changed

5 files changed

+127
-41
lines changed

backend/go/stablediffusion-ggml/Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ JOBS?=$(shell nproc --ignore=1)
88

99
# stablediffusion.cpp (ggml)
1010
STABLEDIFFUSION_GGML_REPO?=https://github.com/leejet/stable-diffusion.cpp
11-
STABLEDIFFUSION_GGML_VERSION?=fce6afcc6a3250a8e17923608922d2a99b339b47
11+
STABLEDIFFUSION_GGML_VERSION?=0ebe6fe118f125665939b27c89f34ed38716bff8
1212

1313
CMAKE_ARGS+=-DGGML_MAX_NAME=128
1414

backend/go/stablediffusion-ggml/gosd.cpp

Lines changed: 66 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,11 @@
44
#include <stdio.h>
55
#include <string.h>
66
#include <time.h>
7-
#include <iostream>
8-
#include <random>
97
#include <string>
108
#include <vector>
119
#include <filesystem>
1210
#include "gosd.h"
1311

14-
// #include "preprocessing.hpp"
15-
#include "flux.hpp"
16-
#include "stable-diffusion.h"
17-
1812
#define STB_IMAGE_IMPLEMENTATION
1913
#define STB_IMAGE_STATIC
2014
#include "stb_image.h"
@@ -29,7 +23,7 @@
2923

3024
// Names of the sampler method, same order as enum sample_method in stable-diffusion.h
3125
const char* sample_method_str[] = {
32-
"euler_a",
26+
"default",
3327
"euler",
3428
"heun",
3529
"dpm2",
@@ -41,6 +35,7 @@ const char* sample_method_str[] = {
4135
"lcm",
4236
"ddim_trailing",
4337
"tcd",
38+
"euler_a",
4439
};
4540

4641
static_assert(std::size(sample_method_str) == SAMPLE_METHOD_COUNT, "sample method mismatch");
@@ -173,7 +168,7 @@ int load_model(const char *model, char *model_path, char* options[], int threads
173168
}
174169
if (sample_method_found == -1) {
175170
fprintf(stderr, "Invalid sample method, default to EULER_A!\n");
176-
sample_method_found = EULER_A;
171+
sample_method_found = sample_method_t::SAMPLE_METHOD_DEFAULT;
177172
}
178173
sample_method = (sample_method_t)sample_method_found;
179174

@@ -197,9 +192,7 @@ int load_model(const char *model, char *model_path, char* options[], int threads
197192
ctx_params.control_net_path = "";
198193
ctx_params.lora_model_dir = lora_dir;
199194
ctx_params.embedding_dir = "";
200-
ctx_params.stacked_id_embed_dir = "";
201195
ctx_params.vae_decode_only = false;
202-
ctx_params.vae_tiling = false;
203196
ctx_params.free_params_immediately = false;
204197
ctx_params.n_threads = threads;
205198
ctx_params.rng_type = STD_DEFAULT_RNG;
@@ -225,29 +218,65 @@ int load_model(const char *model, char *model_path, char* options[], int threads
225218
return 0;
226219
}
227220

228-
int gen_image(char *text, char *negativeText, int width, int height, int steps, int64_t seed, char *dst, float cfg_scale, char *src_image, float strength, char *mask_image, char **ref_images, int ref_images_count) {
221+
void sd_tiling_params_set_enabled(sd_tiling_params_t *params, bool enabled) {
222+
params->enabled = enabled;
223+
}
224+
225+
void sd_tiling_params_set_tile_sizes(sd_tiling_params_t *params, int tile_size_x, int tile_size_y) {
226+
params->tile_size_x = tile_size_x;
227+
params->tile_size_y = tile_size_y;
228+
}
229+
230+
void sd_tiling_params_set_rel_sizes(sd_tiling_params_t *params, float rel_size_x, float rel_size_y) {
231+
params->rel_size_x = rel_size_x;
232+
params->rel_size_y = rel_size_y;
233+
}
234+
235+
void sd_tiling_params_set_target_overlap(sd_tiling_params_t *params, float target_overlap) {
236+
params->target_overlap = target_overlap;
237+
}
238+
239+
sd_tiling_params_t* sd_img_gen_params_get_vae_tiling_params(sd_img_gen_params_t *params) {
240+
return &params->vae_tiling_params;
241+
}
242+
243+
sd_img_gen_params_t* sd_img_gen_params_new(void) {
244+
sd_img_gen_params_t *params = (sd_img_gen_params_t *)std::malloc(sizeof(sd_img_gen_params_t));
245+
sd_img_gen_params_init(params);
246+
return params;
247+
}
248+
249+
void sd_img_gen_params_set_prompts(sd_img_gen_params_t *params, const char *prompt, const char *negative_prompt) {
250+
params->prompt = prompt;
251+
params->negative_prompt = negative_prompt;
252+
}
253+
254+
void sd_img_gen_params_set_dimensions(sd_img_gen_params_t *params, int width, int height) {
255+
params->width = width;
256+
params->height = height;
257+
}
258+
259+
void sd_img_gen_params_set_seed(sd_img_gen_params_t *params, int64_t seed) {
260+
params->seed = seed;
261+
}
262+
263+
int gen_image(sd_img_gen_params_t *p, int steps, char *dst, float cfg_scale, char *src_image, float strength, char *mask_image, char **ref_images, int ref_images_count) {
229264

230265
sd_image_t* results;
231266

232267
std::vector<int> skip_layers = {7, 8, 9};
233268

234269
fprintf (stderr, "Generating image\n");
235270

236-
sd_img_gen_params_t p;
237-
sd_img_gen_params_init(&p);
238-
239-
p.prompt = text;
240-
p.negative_prompt = negativeText;
241-
p.sample_params.guidance.txt_cfg = cfg_scale;
242-
p.sample_params.guidance.slg.layers = skip_layers.data();
243-
p.sample_params.guidance.slg.layer_count = skip_layers.size();
244-
p.width = width;
245-
p.height = height;
246-
p.sample_params.sample_method = sample_method;
247-
p.sample_params.sample_steps = steps;
248-
p.seed = seed;
249-
p.input_id_images_path = "";
250-
p.sample_params.scheduler = scheduler;
271+
p->sample_params.guidance.txt_cfg = cfg_scale;
272+
p->sample_params.guidance.slg.layers = skip_layers.data();
273+
p->sample_params.guidance.slg.layer_count = skip_layers.size();
274+
p->sample_params.sample_method = sample_method;
275+
p->sample_params.sample_steps = steps;
276+
p->sample_params.scheduler = scheduler;
277+
278+
int width = p->width;
279+
int height = p->height;
251280

252281
// Handle input image for img2img
253282
bool has_input_image = (src_image != NULL && strlen(src_image) > 0);
@@ -296,13 +325,13 @@ int gen_image(char *text, char *negativeText, int width, int height, int steps,
296325
input_image_buffer = resized_image_buffer;
297326
}
298327

299-
p.init_image = {(uint32_t)width, (uint32_t)height, 3, input_image_buffer};
300-
p.strength = strength;
328+
p->init_image = {(uint32_t)width, (uint32_t)height, 3, input_image_buffer};
329+
p->strength = strength;
301330
fprintf(stderr, "Using img2img with strength: %.2f\n", strength);
302331
} else {
303332
// No input image, use empty image for text-to-image
304-
p.init_image = {(uint32_t)width, (uint32_t)height, 3, NULL};
305-
p.strength = 0.0f;
333+
p->init_image = {(uint32_t)width, (uint32_t)height, 3, NULL};
334+
p->strength = 0.0f;
306335
}
307336

308337
// Handle mask image for inpainting
@@ -342,12 +371,12 @@ int gen_image(char *text, char *negativeText, int width, int height, int steps,
342371
mask_image_buffer = resized_mask_buffer;
343372
}
344373

345-
p.mask_image = {(uint32_t)width, (uint32_t)height, 1, mask_image_buffer};
374+
p->mask_image = {(uint32_t)width, (uint32_t)height, 1, mask_image_buffer};
346375
fprintf(stderr, "Using inpainting with mask\n");
347376
} else {
348377
// No mask image, create default full mask
349378
default_mask_image_vec.resize(width * height, 255);
350-
p.mask_image = {(uint32_t)width, (uint32_t)height, 1, default_mask_image_vec.data()};
379+
p->mask_image = {(uint32_t)width, (uint32_t)height, 1, default_mask_image_vec.data()};
351380
}
352381

353382
// Handle reference images
@@ -405,13 +434,15 @@ int gen_image(char *text, char *negativeText, int width, int height, int steps,
405434
}
406435

407436
if (!ref_images_vec.empty()) {
408-
p.ref_images = ref_images_vec.data();
409-
p.ref_images_count = ref_images_vec.size();
437+
p->ref_images = ref_images_vec.data();
438+
p->ref_images_count = ref_images_vec.size();
410439
fprintf(stderr, "Using %zu reference images\n", ref_images_vec.size());
411440
}
412441
}
413442

414-
results = generate_image(sd_c, &p);
443+
results = generate_image(sd_c, p);
444+
445+
std::free(p);
415446

416447
if (results == NULL) {
417448
fprintf (stderr, "NO results\n");

backend/go/stablediffusion-ggml/gosd.go

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,18 @@ type SDGGML struct {
2222

2323
var (
2424
LoadModel func(model, model_apth string, options []uintptr, threads int32, diff int) int
25-
GenImage func(text, negativeText string, width, height, steps int, seed int64, dst string, cfgScale float32, srcImage string, strength float32, maskImage string, refImages []string, refImagesCount int) int
25+
GenImage func(params uintptr, steps int, dst string, cfgScale float32, srcImage string, strength float32, maskImage string, refImages []string, refImagesCount int) int
26+
27+
TilingParamsSetEnabled func(params uintptr, enabled bool)
28+
TilingParamsSetTileSizes func(params uintptr, tileSizeX int, tileSizeY int)
29+
TilingParamsSetRelSizes func(params uintptr, relSizeX float32, relSizeY float32)
30+
TilingParamsSetTargetOverlap func(params uintptr, targetOverlap float32)
31+
32+
ImgGenParamsNew func() uintptr
33+
ImgGenParamsSetPrompts func(params uintptr, prompt string, negativePrompt string)
34+
ImgGenParamsSetDimensions func(params uintptr, width int, height int)
35+
ImgGenParamsSetSeed func(params uintptr, seed int64)
36+
ImgGenParamsGetVaeTilingParams func(params uintptr) uintptr
2637
)
2738

2839
// Copied from Purego internal/strings
@@ -120,7 +131,15 @@ func (sd *SDGGML) GenerateImage(opts *pb.GenerateImageRequest) error {
120131
// Default strength for img2img (0.75 is a good default)
121132
strength := float32(0.75)
122133

123-
ret := GenImage(t, negative, int(opts.Width), int(opts.Height), int(opts.Step), int64(opts.Seed), dst, sd.cfgScale, srcImage, strength, maskImage, refImages, refImagesCount)
134+
// free'd by GenImage
135+
p := ImgGenParamsNew()
136+
ImgGenParamsSetPrompts(p, t, negative)
137+
ImgGenParamsSetDimensions(p, int(opts.Width), int(opts.Height))
138+
ImgGenParamsSetSeed(p, int64(opts.Seed))
139+
vaep := ImgGenParamsGetVaeTilingParams(p)
140+
TilingParamsSetEnabled(vaep, false)
141+
142+
ret := GenImage(p, int(opts.Step), dst, sd.cfgScale, srcImage, strength, maskImage, refImages, refImagesCount)
124143
if ret != 0 {
125144
return fmt.Errorf("inference failed")
126145
}
Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,23 @@
1+
#include <cstdint>
2+
#include "stable-diffusion.h"
3+
14
#ifdef __cplusplus
25
extern "C" {
36
#endif
7+
8+
void sd_tiling_params_set_enabled(sd_tiling_params_t *params, bool enabled);
9+
void sd_tiling_params_set_tile_sizes(sd_tiling_params_t *params, int tile_size_x, int tile_size_y);
10+
void sd_tiling_params_set_rel_sizes(sd_tiling_params_t *params, float rel_size_x, float rel_size_y);
11+
void sd_tiling_params_set_target_overlap(sd_tiling_params_t *params, float target_overlap);
12+
sd_tiling_params_t* sd_img_gen_params_get_vae_tiling_params(sd_img_gen_params_t *params);
13+
14+
sd_img_gen_params_t* sd_img_gen_params_new(void);
15+
void sd_img_gen_params_set_prompts(sd_img_gen_params_t *params, const char *prompt, const char *negative_prompt);
16+
void sd_img_gen_params_set_dimensions(sd_img_gen_params_t *params, int width, int height);
17+
void sd_img_gen_params_set_seed(sd_img_gen_params_t *params, int64_t seed);
18+
419
int load_model(const char *model, char *model_path, char* options[], int threads, int diffusionModel);
5-
int gen_image(char *text, char *negativeText, int width, int height, int steps, int64_t seed, char *dst, float cfg_scale, char *src_image, float strength, char *mask_image, char **ref_images, int ref_images_count);
20+
int gen_image(sd_img_gen_params_t *p, int steps, char *dst, float cfg_scale, char *src_image, float strength, char *mask_image, char **ref_images, int ref_images_count);
621
#ifdef __cplusplus
722
}
823
#endif

backend/go/stablediffusion-ggml/main.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,14 +11,35 @@ var (
1111
addr = flag.String("addr", "localhost:50051", "the address to connect to")
1212
)
1313

14+
type LibFuncs struct {
15+
FuncPtr any
16+
Name string
17+
}
18+
1419
func main() {
1520
gosd, err := purego.Dlopen("./libgosd.so", purego.RTLD_NOW|purego.RTLD_GLOBAL)
1621
if err != nil {
1722
panic(err)
1823
}
1924

20-
purego.RegisterLibFunc(&LoadModel, gosd, "load_model")
21-
purego.RegisterLibFunc(&GenImage, gosd, "gen_image")
25+
libFuncs := []LibFuncs{
26+
{&LoadModel, "load_model"},
27+
{&GenImage, "gen_image"},
28+
{&TilingParamsSetEnabled, "sd_tiling_params_set_enabled"},
29+
{&TilingParamsSetTileSizes, "sd_tiling_params_set_tile_sizes"},
30+
{&TilingParamsSetRelSizes, "sd_tiling_params_set_rel_sizes"},
31+
{&TilingParamsSetTargetOverlap, "sd_tiling_params_set_target_overlap"},
32+
33+
{&ImgGenParamsNew, "sd_img_gen_params_new"},
34+
{&ImgGenParamsSetPrompts, "sd_img_gen_params_set_prompts"},
35+
{&ImgGenParamsSetDimensions, "sd_img_gen_params_set_dimensions"},
36+
{&ImgGenParamsSetSeed, "sd_img_gen_params_set_seed"},
37+
{&ImgGenParamsGetVaeTilingParams, "sd_img_gen_params_get_vae_tiling_params"},
38+
}
39+
40+
for _, lf := range libFuncs {
41+
purego.RegisterLibFunc(lf.FuncPtr, gosd, lf.Name)
42+
}
2243

2344
flag.Parse()
2445

0 commit comments

Comments
 (0)