Skip to content

Commit 247c3c7

Browse files
committed
add stable-diffusion-abi feature for golang
1 parent 349a177 commit 247c3c7

File tree

11 files changed

+508
-1
lines changed

11 files changed

+508
-1
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build/

.idea/.gitignore

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

.idea/modules.xml

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

.idea/stable-diffusion.cpp-build.iml

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

.idea/vcs.xml

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

CMakeLists.txt

Lines changed: 98 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,98 @@
1+
cmake_minimum_required(VERSION 3.12)
2+
project("stable-diffusion-build")
3+
4+
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
5+
6+
if (NOT XCODE AND NOT MSVC AND NOT CMAKE_BUILD_TYPE)
7+
set(CMAKE_BUILD_TYPE Release CACHE STRING "Build type" FORCE)
8+
set_property(CACHE CMAKE_BUILD_TYPE PROPERTY STRINGS "Debug" "Release" "MinSizeRel" "RelWithDebInfo")
9+
endif()
10+
11+
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
12+
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
13+
14+
set(CMAKE_CXX_STANDARD 11)
15+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
16+
set(CMAKE_C_STANDARD 11)
17+
set(CMAKE_C_STANDARD_REQUIRED true)
18+
set(THREADS_PREFER_PTHREAD_FLAG ON)
19+
find_package(Threads REQUIRED)
20+
21+
set(CMAKE_MODULE_PATH "${CMAKE_SOURCE_DIR}/cmake;${CMAKE_MODULE_PATH}")
22+
23+
# Instruction set specific
24+
option(SD_AVX "sd: enable AVX" ON)
25+
option(SD_AVX2 "sd: enable AVX2" ON)
26+
option(SD_AVX512 "sd: enable AVX512" OFF)
27+
option(SD_FMA "sd: enable FMA" ON)
28+
29+
30+
31+
if (MSVC)
32+
add_compile_definitions(_CRT_SECURE_NO_WARNINGS)
33+
34+
if (BUILD_SHARED_LIBS)
35+
set(CMAKE_WINDOWS_EXPORT_ALL_SYMBOLS ON)
36+
endif ()
37+
endif ()
38+
39+
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "arm" OR ${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
40+
message(STATUS "ARM detected")
41+
if (MSVC)
42+
# TODO [llama.cpp]: arm msvc?
43+
else()
44+
if (${CMAKE_SYSTEM_PROCESSOR} MATCHES "aarch64")
45+
add_compile_options(-mcpu=native)
46+
endif()
47+
# TODO [llama.cpp]: armv6,7,8 version specific flags
48+
endif()
49+
elseif (${CMAKE_SYSTEM_PROCESSOR} MATCHES "^(x86_64|i686|AMD64)$")
50+
message(STATUS "x86 detected")
51+
if (MSVC)
52+
if (SD_AVX512)
53+
add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX512>)
54+
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX512>)
55+
# MSVC has no compile-time flags enabling specific
56+
# AVX512 extensions, neither it defines the
57+
# macros corresponding to the extensions.
58+
# Do it manually.
59+
elseif (SD_AVX2)
60+
add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX2>)
61+
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX2>)
62+
elseif (SD_AVX)
63+
add_compile_options($<$<COMPILE_LANGUAGE:C>:/arch:AVX>)
64+
add_compile_options($<$<COMPILE_LANGUAGE:CXX>:/arch:AVX>)
65+
endif()
66+
else()
67+
add_compile_options(-mf16c)
68+
if (SD_FMA)
69+
add_compile_options(-mfma)
70+
endif()
71+
if (SD_AVX)
72+
add_compile_options(-mavx)
73+
endif()
74+
if (SD_AVX2)
75+
add_compile_options(-mavx2)
76+
endif()
77+
if (SD_AVX512)
78+
add_compile_options(-mavx512f)
79+
add_compile_options(-mavx512bw)
80+
endif()
81+
endif()
82+
else()
83+
# TODO [llama.cpp]: support PowerPC
84+
message(STATUS "Unknown architecture")
85+
endif()
86+
# deps
87+
88+
include(sd)
89+
include_directories(${sd_SOURCE_DIR})
90+
91+
set(SD_ABI sd-abi)
92+
set(SD_EXTRA_LIBS "")
93+
add_library(${SD_ABI} SHARED stable-diffusion-abi.cpp stable-diffusion-abi.h)
94+
set_target_properties(${SD_ABI} PROPERTIES POSITION_INDEPENDENT_CODE ON)
95+
target_compile_definitions(${SD_ABI} PRIVATE STABLE_DIFFUSION_SHARED STABLE_DIFFUSION_BUILD)
96+
target_link_libraries(${SD_ABI} PRIVATE $<TARGET_OBJECTS:ggml> $<TARGET_OBJECTS:stable-diffusion> ${SD_EXTRA_LIBS} ${CMAKE_THREAD_LIBS_INIT})
97+
target_include_directories(${SD_ABI} PUBLIC .)
98+

README.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,11 @@
1-
# stable-diffusion.cpp-build
1+
# stable-diffusion.cpp-build
2+
3+
4+
## Build
5+
6+
```commandline
7+
mkdir build
8+
cd build
9+
cmake .. -DCMAKE_BUILD_TYPE=Release
10+
cmake --build . --config Release
11+
```
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
# Campatible with cmake 3.11 and above.
2+
macro(FetchContent_MakeAvailable NAME)
3+
FetchContent_GetProperties(${NAME})
4+
if(NOT ${NAME}_POPULATED)
5+
FetchContent_Populate(${NAME})
6+
add_subdirectory(${${NAME}_SOURCE_DIR} ${${NAME}_BINARY_DIR})
7+
endif()
8+
endmacro()macro(FetchContent_MakeAvailable NAME)
9+
FetchContent_GetProperties(${NAME})
10+
if(NOT ${NAME}_POPULATED)
11+
FetchContent_Populate(${NAME})
12+
add_subdirectory(${${NAME}_SOURCE_DIR} ${${NAME}_BINARY_DIR})
13+
endif()
14+
endmacro()

cmake/sd.cmake

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
include(FetchContent)
2+
# FetchContent_MakeAvailable was not added until CMake 3.14
3+
if(${CMAKE_VERSION} VERSION_LESS 3.14)
4+
include(add_FetchContent_MakeAvailable.cmake)
5+
endif()
6+
7+
set(SD_GIT_TAG fb7d9217bb5dd6ac82b4fb1f312381f63b65ba1a)
8+
set(SD_GIT_URL https://github.com/Cyberhan123/stable-diffusion.cpp)
9+
10+
FetchContent_Declare(
11+
sd
12+
GIT_REPOSITORY ${SD_GIT_URL}
13+
GIT_TAG ${SD_GIT_TAG}
14+
)
15+
16+
FetchContent_MakeAvailable(sd)

0 commit comments

Comments
 (0)