Skip to content

Commit eca2651

Browse files
committed
chore(build): Use Purego with stablediffusion backend
Signed-off-by: Richard Palethorpe <[email protected]>
1 parent 455b292 commit eca2651

File tree

4 files changed

+36
-10
lines changed

4 files changed

+36
-10
lines changed

backend/go/stablediffusion-ggml/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
cmake_minimum_required(VERSION 3.12)
22
project(gosd LANGUAGES C CXX)
3+
set(CMAKE_POSITION_INDEPENDENT_CODE ON)
34

45
add_subdirectory(./sources/stablediffusion-ggml.cpp)
56

67
add_library(gosd MODULE gosd.cpp)
7-
target_link_libraries(gosd PRIVATE stable-diffusion ggml zip stdc++fs)
8+
target_link_libraries(gosd PRIVATE stable-diffusion ggml stdc++fs)
89

910
target_include_directories(gosd PUBLIC
1011
stable-diffusion.cpp

backend/go/stablediffusion-ggml/gosd.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -90,7 +90,7 @@ static void sd_log_cb(enum sd_log_level_t level, const char* log, void* data) {
9090
}
9191

9292
int load_model(const char *model, char *model_path, char* options[], int threads, int diff) {
93-
fprintf (stderr, "Loading model!\n");
93+
fprintf (stderr, "Loading model: %p=%s\n", model, model);
9494

9595
sd_set_log_callback(sd_log_cb, NULL);
9696

@@ -110,7 +110,7 @@ int load_model(const char *model, char *model_path, char* options[], int threads
110110
char *lora_dir = model_path;
111111
bool lora_dir_allocated = false;
112112

113-
fprintf(stderr, "parsing options\n");
113+
fprintf(stderr, "parsing options: %p\n", options);
114114

115115
// If options is not NULL, parse options
116116
for (int i = 0; options[i] != NULL; i++) {

backend/go/stablediffusion-ggml/gosd.go

Lines changed: 29 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"fmt"
55
"os"
66
"path/filepath"
7+
"runtime"
78
"strings"
89
"unsafe"
910

@@ -20,10 +21,25 @@ type SDGGML struct {
2021
}
2122

2223
var (
23-
LoadModel func(model, model_apth string, options []string, threads int32, diff int) int
24-
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
24+
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
2526
)
2627

28+
// Copied from Purego internal/strings
29+
// TODO: We should upstream sending []string
30+
func hasSuffix(s, suffix string) bool {
31+
return len(s) >= len(suffix) && s[len(s)-len(suffix):] == suffix
32+
}
33+
34+
func CString(name string) *byte {
35+
if hasSuffix(name, "\x00") {
36+
return &(*(*[]byte)(unsafe.Pointer(&name)))[0]
37+
}
38+
b := make([]byte, len(name)+1)
39+
copy(b, name)
40+
return &b[0]
41+
}
42+
2743
func (sd *SDGGML) Load(opts *pb.ModelOptions) error {
2844

2945
sd.threads = int(opts.Threads)
@@ -56,8 +72,14 @@ func (sd *SDGGML) Load(opts *pb.ModelOptions) error {
5672

5773
fmt.Fprintf(os.Stderr, "Options: %+v\n", oo)
5874

59-
options := make([]string, len(oo), len(oo) + 1)
60-
*(*uintptr)(unsafe.Add(unsafe.Pointer(&options), uintptr(len(oo)))) = 0
75+
// At the time of writing Purego doesn't recurse into slices and convert Go strings to pointers so we need to do that
76+
var keepAlive []any
77+
options := make([]uintptr, len(oo), len(oo)+1)
78+
for i, op := range oo {
79+
bytep := CString(op)
80+
options[i] = uintptr(unsafe.Pointer(bytep))
81+
keepAlive = append(keepAlive, bytep)
82+
}
6183

6284
sd.cfgScale = opts.CFGScale
6385

@@ -66,6 +88,8 @@ func (sd *SDGGML) Load(opts *pb.ModelOptions) error {
6688
return fmt.Errorf("could not load model")
6789
}
6890

91+
runtime.KeepAlive(keepAlive)
92+
6993
return nil
7094
}
7195

@@ -89,7 +113,7 @@ func (sd *SDGGML) GenerateImage(opts *pb.GenerateImageRequest) error {
89113
}
90114

91115
refImagesCount := len(opts.RefImages)
92-
refImages := make([]string, refImagesCount, refImagesCount + 1)
116+
refImages := make([]string, refImagesCount, refImagesCount+1)
93117
copy(refImages, opts.RefImages)
94118
*(*uintptr)(unsafe.Add(unsafe.Pointer(&refImages), refImagesCount)) = 0
95119

backend/go/stablediffusion-ggml/package.sh

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ CURDIR=$(dirname "$(realpath $0)")
1010
# Create lib directory
1111
mkdir -p $CURDIR/package/lib
1212

13+
cp -avrf $CURDIR/libgosd.so $CURDIR/package/
1314
cp -avrf $CURDIR/stablediffusion-ggml $CURDIR/package/
1415
cp -rfv $CURDIR/run.sh $CURDIR/package/
1516

@@ -47,6 +48,6 @@ else
4748
exit 1
4849
fi
4950

50-
echo "Packaging completed successfully"
51+
echo "Packaging completed successfully"
5152
ls -liah $CURDIR/package/
52-
ls -liah $CURDIR/package/lib/
53+
ls -liah $CURDIR/package/lib/

0 commit comments

Comments
 (0)