Skip to content

Commit 67785c8

Browse files
committed
fix abi call fail out of stack
1 parent 9a89eee commit 67785c8

File tree

6 files changed

+54
-29
lines changed

6 files changed

+54
-29
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,3 +2,4 @@
22
/.vs/
33
/out/
44
/cmake-build-debug/
5+
/cmake-build-release/

.idea/workspace.xml

Lines changed: 21 additions & 11 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

CMakeLists.txt

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,5 +54,3 @@ set_target_properties(stable-diffusion PROPERTIES POSITION_INDEPENDENT_CODE ON)
5454
set_target_properties(${SD_ABI} PROPERTIES POSITION_INDEPENDENT_CODE ON)
5555
target_compile_definitions(${SD_ABI} PRIVATE STABLE_DIFFUSION_SHARED STABLE_DIFFUSION_BUILD)
5656
target_link_libraries(${SD_ABI} PRIVATE ggml stable-diffusion ${CMAKE_THREAD_LIBS_INIT})
57-
58-

cmake/sd.cmake

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,8 @@ if(${CMAKE_VERSION} VERSION_LESS 3.14)
44
include(add_FetchContent_MakeAvailable.cmake)
55
endif()
66

7-
set(SD_GIT_TAG 70d2919decdf8f5fcd4956d1da60594c9e7b03ac)
8-
set(SD_GIT_URL https://github.com/Cyberhan123/stable-diffusion.cpp)
7+
set(SD_GIT_TAG 47dd704198f46ee75b11cbaf3aa2b8f644df0be9)
8+
set(SD_GIT_URL https://github.com/leejet/stable-diffusion.cpp)
99
set(BUILD_SHARED_LIBS OFF)
1010

1111
FetchContent_Declare(

stable-diffusion-abi.cpp

Lines changed: 25 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
#include "stable-diffusion-abi.h"
22

33
#include "stable-diffusion.h"
4-
#include "base64.hpp"
54
#include <string>
65
#include <cstring>
76
#include <map>
7+
#include <algorithm>
8+
#include <iterator>
89
#include <vector>
10+
911
/*================================================= StableDiffusion ABI API =============================================*/
1012

1113
const static std::map<std::string, enum SDLogLevel> SDLogLevelMap = {
@@ -188,7 +190,7 @@ bool load_from_file(void* sd, const char* file_path, const char* schedule) {
188190
return false;
189191
};
190192

191-
const char* txt2img(void* sd, const sd_txt2img_options* opt) {
193+
const uint8_t* txt2img(void* sd, const sd_txt2img_options* opt) {
192194
const auto sm = std::string(opt->sample_method);
193195
const auto it = SampleMethodMap.find(sm);
194196
if (it != SampleMethodMap.end()) {
@@ -204,16 +206,23 @@ const char* txt2img(void* sd, const sd_txt2img_options* opt) {
204206
opt->seed,
205207
opt->batch_count
206208
);
207-
const auto str = code::base64_encode<std::string, std::vector<uint8_t *>>(result, false);
208-
const auto buffer = new char[str.size()];
209-
std::memcpy(buffer, str.c_str(), str.size());
209+
const auto image_size = opt->width * opt->height * 3;
210+
std::vector<uint8_t> images;
211+
images.reserve(image_size * opt->batch_count);
212+
for (const auto img: result) {
213+
if (img != nullptr) {
214+
std::copy_n(img, image_size, std::back_inserter(images));
215+
}
216+
};
217+
const auto buffer = new uint8_t[image_size * opt->batch_count];
218+
std::memcpy(buffer, images.data(), images.size());
210219
return buffer;
211220
}
212221
delete opt;
213222
return nullptr;
214223
};
215224

216-
const char* img2img(void* sd, const sd_img2img_options* opt) {
225+
const uint8_t* img2img(void* sd, const sd_img2img_options* opt) {
217226
const auto sm = std::string(opt->sample_method);
218227
const auto it = SampleMethodMap.find(sm);
219228
if (it != SampleMethodMap.end()) {
@@ -230,9 +239,16 @@ const char* img2img(void* sd, const sd_img2img_options* opt) {
230239
opt->strength,
231240
opt->seed
232241
);
233-
const auto str = code::base64_encode<std::string, std::vector<uint8_t *>>(result, false);
234-
const auto buffer = new char[str.size()];
235-
std::memcpy(buffer, str.c_str(), str.size());
242+
const auto image_size = opt->width * opt->height * 3;
243+
std::vector<uint8_t> images;
244+
images.reserve(image_size);
245+
for (const auto img: result) {
246+
if (img != nullptr) {
247+
std::copy_n(img, image_size, std::back_inserter(images));
248+
}
249+
};
250+
const auto buffer = new uint8_t[image_size];
251+
std::memcpy(buffer, images.data(), images.size());
236252
return buffer;
237253
}
238254
delete opt;

stable-diffusion-abi.h

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,12 +7,12 @@
77
#ifdef STABLE_DIFFUSION_SHARED
88
#if defined(_WIN32) && !defined(__MINGW32__)
99
#ifdef STABLE_DIFFUSION_BUILD
10-
#define STABLE_DIFFUSION_API __declspec(dllexport)
10+
#define STABLE_DIFFUSION_API __declspec(dllexport)
1111
#else
12-
#define STABLE_DIFFUSION_API __declspec(dllimport)
12+
#define STABLE_DIFFUSION_API __declspec(dllimport)
1313
#endif
1414
#else
15-
#define STABLE_DIFFUSION_API __attribute__((visibility("default")))
15+
#define STABLE_DIFFUSION_API __attribute__((visibility("default")))
1616
#endif
1717
#else
1818
#define STABLE_DIFFUSION_API
@@ -83,9 +83,9 @@ STABLE_DIFFUSION_API void destroy_stable_diffusion(void* sd);
8383

8484
STABLE_DIFFUSION_API bool load_from_file(void* sd, const char* file_path, const char* schedule);
8585

86-
STABLE_DIFFUSION_API const char* txt2img(void* sd, const sd_txt2img_options* opt);
86+
STABLE_DIFFUSION_API const uint8_t* txt2img(void* sd, const sd_txt2img_options* opt);
8787

88-
STABLE_DIFFUSION_API const char* img2img(void* sd, const sd_img2img_options* opt);
88+
STABLE_DIFFUSION_API const uint8_t* img2img(void* sd, const sd_img2img_options* opt);
8989

9090
STABLE_DIFFUSION_API void set_stable_diffusion_log_level(const char* level);
9191

0 commit comments

Comments
 (0)