Skip to content

Commit 812d346

Browse files
committed
Merge branch 'master' into sdapi_default_dimensions
2 parents acbceca + 45ce78a commit 812d346

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+351
-357
lines changed

.github/workflows/build.yml

Lines changed: 24 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -535,31 +535,30 @@ jobs:
535535
# Add ROCm to PATH for current session
536536
echo "/opt/rocm/bin" >> $GITHUB_PATH
537537
538-
# Build case pattern from GPU_TARGETS
539-
PATTERN=$(printf '%s' "$GPU_TARGETS" | sed 's/;/\*|\*/g')
540-
PATTERN="*${PATTERN}*"
538+
# Build regex pattern from ${{ env.GPU_TARGETS }} (match target as substring)
539+
TARGET_REGEX="($(printf '%s' "${{ env.GPU_TARGETS }}" | sed 's/;/|/g'))"
541540
542541
# Remove library files for architectures we're not building for to save disk space
543542
echo "Cleaning up unneeded architecture files..."
544543
cd /opt/rocm/lib/rocblas/library
545544
# Keep only our target architectures
546545
for file in *; do
547-
case "$file" in
548-
$PATTERN)
549-
;;
550-
*)
551-
sudo rm -f "$file" ;;
552-
esac;
546+
if printf '%s' "$file" | grep -q 'gfx'; then
547+
if ! printf '%s' "$file" | grep -Eq "$TARGET_REGEX"; then
548+
echo "Removing $file" &&
549+
sudo rm -f "$file";
550+
fi
551+
fi
553552
done
554553
555554
cd /opt/rocm/lib/hipblaslt/library
556555
for file in *; do
557-
case "$file" in
558-
$PATTERN)
559-
;;
560-
*)
561-
sudo rm -f "$file" ;;
562-
esac;
556+
if printf '%s' "$file" | grep -q 'gfx'; then
557+
if ! printf '%s' "$file" | grep -Eq "$TARGET_REGEX"; then
558+
echo "Removing $file" &&
559+
sudo rm -f "$file";
560+
fi
561+
fi
563562
done
564563
565564
- name: Build
@@ -592,21 +591,15 @@ jobs:
592591
cp ggml/LICENSE ./build/bin/ggml.txt
593592
cp LICENSE ./build/bin/stable-diffusion.cpp.txt
594593
595-
# Create directories for ROCm libraries
596-
mkdir -p ./build/bin/rocblas/library
597-
mkdir -p ./build/bin/hipblaslt/library
598-
599-
# Copy ROCm runtime libraries (use || true to continue if files don't exist)
600-
cp /opt/rocm/lib/librocsparse.so* ./build/bin/ || true
601-
cp /opt/rocm/lib/libhsa-runtime64.so* ./build/bin/ || true
602-
cp /opt/rocm/lib/libamdhip64.so* ./build/bin/ || true
603-
cp /opt/rocm/lib/libhipblas.so* ./build/bin/ || true
604-
cp /opt/rocm/lib/libhipblaslt.so* ./build/bin/ || true
605-
cp /opt/rocm/lib/librocblas.so* ./build/bin/ || true
606-
607-
# Copy library files (already filtered to target architectures)
608-
cp /opt/rocm/lib/rocblas/library/* ./build/bin/rocblas/library/ || true
609-
cp /opt/rocm/lib/hipblaslt/library/* ./build/bin/hipblaslt/library/ || true
594+
# Move ROCm runtime libraries (to avoid double space consumption)
595+
sudo mv /opt/rocm/lib/librocsparse.so* ./build/bin/
596+
sudo mv /opt/rocm/lib/libhsa-runtime64.so* ./build/bin/
597+
sudo mv /opt/rocm/lib/libamdhip64.so* ./build/bin/
598+
sudo mv /opt/rocm/lib/libhipblas.so* ./build/bin/
599+
sudo mv /opt/rocm/lib/libhipblaslt.so* ./build/bin/
600+
sudo mv /opt/rocm/lib/librocblas.so* ./build/bin/
601+
sudo mv /opt/rocm/lib/rocblas/ ./build/bin/
602+
sudo mv /opt/rocm/lib/hipblaslt/ ./build/bin/
610603
611604
- name: Fetch system info
612605
id: system-info
@@ -622,7 +615,7 @@ jobs:
622615
run: |
623616
cp ggml/LICENSE ./build/bin/ggml.txt
624617
cp LICENSE ./build/bin/stable-diffusion.cpp.txt
625-
zip -j sd-${{ env.BRANCH_NAME }}-${{ steps.commit.outputs.short }}-bin-${{ steps.system-info.outputs.OS_TYPE }}-Ubuntu-${{ env.UBUNTU_VERSION }}-${{ steps.system-info.outputs.CPU_ARCH }}-rocm.zip ./build/bin/*
618+
zip -y -r sd-${{ env.BRANCH_NAME }}-${{ steps.commit.outputs.short }}-bin-${{ steps.system-info.outputs.OS_TYPE }}-Ubuntu-${{ env.UBUNTU_VERSION }}-${{ steps.system-info.outputs.CPU_ARCH }}-rocm.zip ./build/bin
626619
627620
- name: Upload artifacts
628621
if: ${{ ( github.event_name == 'push' && github.ref == 'refs/heads/master' ) || github.event.inputs.create_release == 'true' }}

CMakeLists.txt

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -87,9 +87,9 @@ endif()
8787
set(SD_LIB stable-diffusion)
8888

8989
file(GLOB SD_LIB_SOURCES
90-
"*.h"
91-
"*.cpp"
92-
"*.hpp"
90+
"src/*.h"
91+
"src/*.cpp"
92+
"src/*.hpp"
9393
)
9494

9595
find_program(GIT_EXE NAMES git git.exe NO_CMAKE_FIND_ROOT_PATH)
@@ -119,7 +119,7 @@ endif()
119119
message(STATUS "stable-diffusion.cpp commit ${SDCPP_BUILD_COMMIT}")
120120

121121
set_property(
122-
SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/version.cpp
122+
SOURCE ${CMAKE_CURRENT_SOURCE_DIR}/src/version.cpp
123123
APPEND PROPERTY COMPILE_DEFINITIONS
124124
SDCPP_BUILD_COMMIT=${SDCPP_BUILD_COMMIT} SDCPP_BUILD_VERSION=${SDCPP_BUILD_VERSION}
125125
)
@@ -182,6 +182,7 @@ endif()
182182
add_subdirectory(thirdparty)
183183

184184
target_link_libraries(${SD_LIB} PUBLIC ggml zip)
185+
target_include_directories(${SD_LIB} PUBLIC . include)
185186
target_include_directories(${SD_LIB} PUBLIC . thirdparty)
186187
target_compile_features(${SD_LIB} PUBLIC c_std_11 cxx_std_17)
187188

@@ -190,7 +191,7 @@ if (SD_BUILD_EXAMPLES)
190191
add_subdirectory(examples)
191192
endif()
192193

193-
set(SD_PUBLIC_HEADERS stable-diffusion.h)
194+
set(SD_PUBLIC_HEADERS include/stable-diffusion.h)
194195
set_target_properties(${SD_LIB} PROPERTIES PUBLIC_HEADER "${SD_PUBLIC_HEADERS}")
195196

196197
install(TARGETS ${SD_LIB} LIBRARY PUBLIC_HEADER)
Lines changed: 87 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -1,88 +1,88 @@
1-
import os
2-
import sys
3-
4-
import numpy as np
5-
import torch
6-
from diffusers.utils import load_image
7-
# pip install insightface==0.7.3
8-
from insightface.app import FaceAnalysis
9-
from insightface.data import get_image as ins_get_image
10-
from safetensors.torch import save_file
11-
12-
###
13-
# https://github.com/cubiq/ComfyUI_IPAdapter_plus/issues/165#issue-2055829543
14-
###
15-
class FaceAnalysis2(FaceAnalysis):
16-
# NOTE: allows setting det_size for each detection call.
17-
# the model allows it but the wrapping code from insightface
18-
# doesn't show it, and people end up loading duplicate models
19-
# for different sizes where there is absolutely no need to
20-
def get(self, img, max_num=0, det_size=(640, 640)):
21-
if det_size is not None:
22-
self.det_model.input_size = det_size
23-
24-
return super().get(img, max_num)
25-
26-
def analyze_faces(face_analysis: FaceAnalysis, img_data: np.ndarray, det_size=(640, 640)):
27-
# NOTE: try detect faces, if no faces detected, lower det_size until it does
28-
detection_sizes = [None] + [(size, size) for size in range(640, 256, -64)] + [(256, 256)]
29-
30-
for size in detection_sizes:
31-
faces = face_analysis.get(img_data, det_size=size)
32-
if len(faces) > 0:
33-
return faces
34-
35-
return []
36-
37-
if __name__ == "__main__":
38-
#face_detector = FaceAnalysis2(providers=['CUDAExecutionProvider'], allowed_modules=['detection', 'recognition'])
39-
face_detector = FaceAnalysis2(providers=['CPUExecutionProvider'], allowed_modules=['detection', 'recognition'])
40-
face_detector.prepare(ctx_id=0, det_size=(640, 640))
41-
#input_folder_name = './scarletthead_woman'
42-
input_folder_name = sys.argv[1]
43-
image_basename_list = os.listdir(input_folder_name)
44-
image_path_list = sorted([os.path.join(input_folder_name, basename) for basename in image_basename_list])
45-
46-
input_id_images = []
47-
for image_path in image_path_list:
48-
input_id_images.append(load_image(image_path))
49-
50-
id_embed_list = []
51-
52-
for img in input_id_images:
53-
img = np.array(img)
54-
img = img[:, :, ::-1]
55-
faces = analyze_faces(face_detector, img)
56-
if len(faces) > 0:
57-
id_embed_list.append(torch.from_numpy((faces[0]['embedding'])))
58-
59-
if len(id_embed_list) == 0:
60-
raise ValueError(f"No face detected in input image pool")
61-
62-
id_embeds = torch.stack(id_embed_list)
63-
64-
# for r in id_embeds:
65-
# print(r)
66-
# #torch.save(id_embeds, input_folder_name+'/id_embeds.pt');
67-
# weights = dict()
68-
# weights["id_embeds"] = id_embeds
69-
# save_file(weights, input_folder_name+'/id_embeds.safetensors')
70-
71-
binary_data = id_embeds.numpy().tobytes()
72-
two = 4
73-
zero = 0
74-
one = 1
75-
tensor_name = "id_embeds"
76-
# Write binary data to a file
77-
with open(input_folder_name+'/id_embeds.bin', "wb") as f:
78-
f.write(two.to_bytes(4, byteorder='little'))
79-
f.write((len(tensor_name)).to_bytes(4, byteorder='little'))
80-
f.write(zero.to_bytes(4, byteorder='little'))
81-
f.write((id_embeds.shape[1]).to_bytes(4, byteorder='little'))
82-
f.write((id_embeds.shape[0]).to_bytes(4, byteorder='little'))
83-
f.write(one.to_bytes(4, byteorder='little'))
84-
f.write(one.to_bytes(4, byteorder='little'))
85-
f.write(tensor_name.encode('ascii'))
86-
f.write(binary_data)
87-
1+
import os
2+
import sys
3+
4+
import numpy as np
5+
import torch
6+
from diffusers.utils import load_image
7+
# pip install insightface==0.7.3
8+
from insightface.app import FaceAnalysis
9+
from insightface.data import get_image as ins_get_image
10+
from safetensors.torch import save_file
11+
12+
###
13+
# https://github.com/cubiq/ComfyUI_IPAdapter_plus/issues/165#issue-2055829543
14+
###
15+
class FaceAnalysis2(FaceAnalysis):
16+
# NOTE: allows setting det_size for each detection call.
17+
# the model allows it but the wrapping code from insightface
18+
# doesn't show it, and people end up loading duplicate models
19+
# for different sizes where there is absolutely no need to
20+
def get(self, img, max_num=0, det_size=(640, 640)):
21+
if det_size is not None:
22+
self.det_model.input_size = det_size
23+
24+
return super().get(img, max_num)
25+
26+
def analyze_faces(face_analysis: FaceAnalysis, img_data: np.ndarray, det_size=(640, 640)):
27+
# NOTE: try detect faces, if no faces detected, lower det_size until it does
28+
detection_sizes = [None] + [(size, size) for size in range(640, 256, -64)] + [(256, 256)]
29+
30+
for size in detection_sizes:
31+
faces = face_analysis.get(img_data, det_size=size)
32+
if len(faces) > 0:
33+
return faces
34+
35+
return []
36+
37+
if __name__ == "__main__":
38+
#face_detector = FaceAnalysis2(providers=['CUDAExecutionProvider'], allowed_modules=['detection', 'recognition'])
39+
face_detector = FaceAnalysis2(providers=['CPUExecutionProvider'], allowed_modules=['detection', 'recognition'])
40+
face_detector.prepare(ctx_id=0, det_size=(640, 640))
41+
#input_folder_name = './scarletthead_woman'
42+
input_folder_name = sys.argv[1]
43+
image_basename_list = os.listdir(input_folder_name)
44+
image_path_list = sorted([os.path.join(input_folder_name, basename) for basename in image_basename_list])
45+
46+
input_id_images = []
47+
for image_path in image_path_list:
48+
input_id_images.append(load_image(image_path))
49+
50+
id_embed_list = []
51+
52+
for img in input_id_images:
53+
img = np.array(img)
54+
img = img[:, :, ::-1]
55+
faces = analyze_faces(face_detector, img)
56+
if len(faces) > 0:
57+
id_embed_list.append(torch.from_numpy((faces[0]['embedding'])))
58+
59+
if len(id_embed_list) == 0:
60+
raise ValueError(f"No face detected in input image pool")
61+
62+
id_embeds = torch.stack(id_embed_list)
63+
64+
# for r in id_embeds:
65+
# print(r)
66+
# #torch.save(id_embeds, input_folder_name+'/id_embeds.pt');
67+
# weights = dict()
68+
# weights["id_embeds"] = id_embeds
69+
# save_file(weights, input_folder_name+'/id_embeds.safetensors')
70+
71+
binary_data = id_embeds.numpy().tobytes()
72+
two = 4
73+
zero = 0
74+
one = 1
75+
tensor_name = "id_embeds"
76+
# Write binary data to a file
77+
with open(input_folder_name+'/id_embeds.bin', "wb") as f:
78+
f.write(two.to_bytes(4, byteorder='little'))
79+
f.write((len(tensor_name)).to_bytes(4, byteorder='little'))
80+
f.write(zero.to_bytes(4, byteorder='little'))
81+
f.write((id_embeds.shape[1]).to_bytes(4, byteorder='little'))
82+
f.write((id_embeds.shape[0]).to_bytes(4, byteorder='little'))
83+
f.write(one.to_bytes(4, byteorder='little'))
84+
f.write(one.to_bytes(4, byteorder='little'))
85+
f.write(tensor_name.encode('ascii'))
86+
f.write(binary_data)
87+
8888

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.

0 commit comments

Comments
 (0)