
Google Colab ã§ã® TheLastBençãFast Stable Diffusion - AUTOMATIC1111ãã é·æéãµããŒã(LTS)åãç®æããŠå®å šè§£èª¬
- å²åŒãã
AICU AIDX Labã® ãããã¯ãã ã§ãã
æ°æ¥åãæžç±ãç»åçæAI Stable Diffusionã¹ã¿ãŒãã¬ã€ãã(#SDé»è²æ¬) ã§äœ¿çšããŠãã Google Colabã§ã®AUTOMATIC1111ã®äžå
·åãã¬ããŒããããŸããããç¡äºã«è§£æ±ºããããŸããã
ãšãåããããŠAUTOMATIC1111 v1.10.x ããªãªãŒã¹ãããŸããã®ã§ãçµã¿èŸŒãã§ãããŸããã
https://note.com/aicu/n/nc5da1bf2f818
https://note.com/aicu/n/n92c214379528
Stable Diffusion 3ã®ãµããŒããForgeãªã©ã§éããããããã©ãŒãã³ã¹ã®æ¹åãå ¥ããŸããã®ã§ããã®ããŒãžã§ã³ã¯å€ãã®æ¹ã«é·ã䜿ãããããšãäºæ³ããŸãã
äžæ¹ã§ãä»åŸãAUTOMATIC1111ã¯é²åãç¶ããã®ã ãããããšããçåãšäžå®ããæã¡ã®æ¹ãããã£ããããšæããŸãããã®2幎éãçµ¶ããé²åãã€ã¥ããŠãããA1111ããã¯ãšãŠããããããã®ã§ãããïŒãããŒãæè¡çèå³ã§ã¯ãªãïŒæžç±ãããã¥ã¢ã«ãAPIå©çšãå®åæ¥åã瀟å ããŒã«ãšãã£ã䜿ãéã§ã¯ãããçšåºŠé·æéãèŠæ®ããã¡ã³ããã³ã¹ã«å ¥ã£ãŠãããããšããã§ãã
ä»åã®èšäºã§ã¯ãäžèšã®ãããªæ¥åçã«çã§äœ¿ãããŠããã§ãããAUTOMATIC1111ïŒä»¥äžãA1111ïŒã®Long-Term Support (LTS)ãã€ãŸããé·æéãµããŒãçããäœã£ãŠã¿ãããšæããŸãã
LTSåã®åºæ¬æ¹é
LTSåã®åºæ¬æ¹éãšããŠã¯ãæžç±ãç»åçæ AI Stable Diffusionã¹ã¿ãŒãã¬ã€ãã#SDé»è²æ¬ ã§æ¡çšããŠãã TheLastBenããåäœã®ã¹ã¯ãªãããfast-stable-diffusionãããææ°ã®A1111ãç°å¢ã®å€åã«äŸåããããGoogle Colabã§ãã§ããã ãã¡ã³ããã³ã¹ããªãã§å®å®ããŠåäœããããããšããã³ã³ã»ããã«ãªããŸãã
ãã ããã»ãã¥ãªãã£ãªã©ã®å¯Ÿå¿ããã¡ã³ããã³ã¹é
ç®ãå¢ãããããŠãŒã¶ãŒãžã®è² æ
ã説æãå€ããªãæ¹åã«ã¯ããŸããã
ãŸãæ¢åã®é
åžæžã¿ã³ãŒãã解説ããªãããã§ããã ãå°ãªã倿Žç®æã§èª¬æã§ããããã«ããŸãã
ãšããã®ããããããäŒæ¥ãå¶äœã¹ã¿ãžãªãªã©ã§ãSDé»è²æ¬ãã䜿ãããŠããæ¹ã¯ãã ãã¶ã«ã¹ã¿ãã€ãºããŠããŸã£ãŠããå¯èœæ§ããããŸããä»åã¯åŸ®çްãªä¿®æ£ã ãã§LTSåãå³ã£ãŠãããããšæããŸããããšããšãªãŒãã³ãœãŒã¹ã§ã¯ãããŸããããã©ãã¯ããã¯ã¹ã§ã¯ãªãããã£ãã解説ããããšã§ãéåžžã«éææ§ã®é«ãæè¡ãšããŠé·æã§æŽ»çšã§ããããã«ãªããŸãã
https://ja.aicu.ai/sbxl/
ãŸãæžç±å ã§ç޹ä»ããŠããããŒã¹ã«ãªãã¹ã¯ãªããã¯ãã¡ãã
Install/Update AUTOMATIC1111 repo
Install/Update AUTOMATIC1111 repo ã®ã»ã¯ã·ã§ã³ã«æãå ¥ããŸãã
宿åïŒããããã®ãŸãŸã»ã«ã«è²Œãä»ãã§OKïŒ
#@markdown # Install/Update AUTOMATIC1111 repo
commit_hash = "3bd7acd" #@param {type:"string"}
from IPython.utils import capture
from IPython.display import clear_output
from subprocess import getoutput
import ipywidgets as widgets
import sys
import fileinput
import os
import time
import base64
import requests
from urllib.request import urlopen, Request
from urllib.parse import urlparse, parse_qs, unquote
from tqdm import tqdm
import six
blsaphemy=base64.b64decode(("ZWJ1aQ==").encode('ascii')).decode('ascii')
if not os.path.exists("/content/gdrive"):
print('[1;31mGdrive not connected, using temporary colab storage ...')
time.sleep(4)
mainpth="MyDrive"
!mkdir -p /content/gdrive/$mainpth
Shared_Drive=""
if Shared_Drive!="" and not os.path.exists("/content/gdrive/Shareddrives"):
print('[1;31mShared drive not detected, using default MyDrive')
mainpth="MyDrive"
with capture.capture_output() as cap:
def inf(msg, style, wdth): inf = widgets.Button(description=msg, disabled=True, button_style=style, layout=widgets.Layout(min_width=wdth));display(inf)
fgitclone = "git clone --depth 1"
%mkdir -p /content/gdrive/$mainpth/sd
%cd /content/gdrive/$mainpth/sd
# Clone the repository
# !git clone -q --branch master https://github.com/AUTOMATIC1111/stable-diffusion-w$blsaphemy
!git clone -q https://github.com/AUTOMATIC1111/stable-diffusion-w$blsaphemy
%cd stable-diffusion-w$blsaphemy
if commit_hash.strip():
# Checkout the specified commit
!git checkout {commit_hash.strip()}
print(f"Checked out commit: {commit_hash.strip()}")
else:
# Use the latest version
!git pull
print("Using the latest version")
!mkdir -p /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/cache/
os.environ['TRANSFORMERS_CACHE']=f"/content/gdrive/{mainpth}/sd/stable-diffusion-w"+blsaphemy+"/cache"
os.environ['TORCH_HOME'] = f"/content/gdrive/{mainpth}/sd/stable-diffusion-w"+blsaphemy+"/cache"
!mkdir -p /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/repositories
!git clone https://github.com/AUTOMATIC1111/stable-diffusion-w$blsaphemy-assets /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/repositories/stable-diffusion-webui-assets
if not commit_hash.strip():
with capture.capture_output() as cap:
%cd /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/
!git reset --hard
!rm webui.sh
clear_output()
inf('\u2714 Done','success', '50px')
#@markdown ---远å ç®æã解説ããŸãã
commit_hash = "3bd7acd" #@param {type:"string"}
---
 # Clone the repository
 # !git clone -q --branch master https://github.com/AUTOMATIC1111/stable-diffusion-w$blsaphemy
 !git clone -q https://github.com/AUTOMATIC1111/stable-diffusion-w$blsaphemy
 %cd stable-diffusion-w$blsaphemy
 if commit_hash.strip():
  # Checkout the specified commit
  !git checkout {commit_hash.strip()}
  print(f"Checked out commit: {commit_hash.strip()}")
 else:
  # Use the latest version
  !git pull
  print("Using the latest version")
---
if not commit_hash.strip():
 with capture.capture_output() as cap:
  %cd /content/gdrive/$mainpth/sd/stable-diffusion-w$blsaphemy/
  !git reset --hard
  !rm webui.sh
clear_output()
inf('\u2714 Done','success', '50px')
AUTOMATIC1111 v.1.10.1 ã®ã³ãããããã·ã¥ã§ããä»åŸã¯ãã®ããã¹ããã£ãŒã«ãã§ã³ãããããã·ã¥ãæå®ããããšã§ãææ°çã®ååŸããŒãžã§ã³ãæç€ºçã«æå®ã§ããŸãã空çœã«ãããšææ°çãååŸããŸãã

Requirements
次ã®ã»ã«ã§ããå®ç§ã§ã¯ãªãã§ããããŸãã¯å®æåã¯ãã¡ãã
#@markdown # Requirements
print('[1;32mInstalling requirements...')
with capture.capture_output() as cap:
%cd /content/
!wget -q -i https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/Dependencies/A1111.txt
!dpkg -i *.deb
if not os.path.exists('/content/gdrive/'+mainpth+'/sd/stablediffusion'):
!tar -C /content/gdrive/$mainpth --zstd -xf sd_mrep.tar.zst
!tar -C / --zstd -xf gcolabdeps.tar.zst
!rm *.deb | rm *.zst | rm *.txt
if not os.path.exists('gdrive/'+mainpth+'/sd/libtcmalloc/libtcmalloc_minimal.so.4'):
%env CXXFLAGS=-std=c++14
!wget -q https://github.com/gperftools/gperftools/releases/download/gperftools-2.5/gperftools-2.5.tar.gz && tar zxf gperftools-2.5.tar.gz && mv gperftools-2.5 gperftools
!wget -q https://github.com/TheLastBen/fast-stable-diffusion/raw/main/AUTOMATIC1111_files/Patch
%cd /content/gperftools
!patch -p1 < /content/Patch
!./configure --enable-minimal --enable-libunwind --enable-frame-pointers --enable-dynamic-sized-delete-support --enable-sized-delete --enable-emergency-malloc; make -j4
!mkdir -p /content/gdrive/$mainpth/sd/libtcmalloc && cp .libs/libtcmalloc*.so* /content/gdrive/$mainpth/sd/libtcmalloc
%env LD_PRELOAD=/content/gdrive/$mainpth/sd/libtcmalloc/libtcmalloc_minimal.so.4
%cd /content
!rm *.tar.gz Patch && rm -r /content/gperftools
else:
%env LD_PRELOAD=/content/gdrive/$mainpth/sd/libtcmalloc/libtcmalloc_minimal.so.4
!pip install controlnet_aux -qq --no-deps
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '3'
os.environ['PYTHONWARNINGS'] = 'ignore'
!sed -i 's@text = _formatwarnmsg(msg)@text =\"\"@g' /usr/lib/python3.10/warnings.py
clear_output()
inf('\u2714 Done','success', '50px')
#@markdown ---LTSå以åã«ãæè¿ã®ã¢ããããŒãã§äžå ·åãåºãèŠçŽ ã2ç®æãããŸãã®ã§ä¿®æ£ããŸãã
(1) å€ãã³ãŒãã«ã¯13è¡ç®ã«Â spandrel ããããŸããããåé€ã§ãã
!pip install spandrel==0.1.6 -qq
(2) 代ããã«28è¡ç®ã« controlnet_aux ãå ããŸãã
!pip install controlnet_aux -qq --no-deps
Requirementsã¯ããã§åãæ¥ãåé¡ãªãåäœããããã«ãªããŸãïŒè¯ãã£ãïŒïŒãã§ãããã§ãå®ç§ã§ã¯ãªãããšããç®æãããã€ããããŸãã
ãã®TheLastBençã®A1111ã¯TheLastBenã®ãªããžããªããèšå®ãã¡ã€ã«ãä¿®æ£ãã¡ã€ã«ãããŠã³ããŒãããŠããŸãã
äŸãã°ãã® Dependencies/A1111.txt ã
https://raw.githubusercontent.com/TheLastBen/fast-stable-diffusion/main/Dependencies/A1111.txt
äžèº«ã¯ä»¥äžã®ããã«ãªã£ãŠããŸãã
https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dependencies/zstd_1.4.4+dfsg-3ubuntu0.1_amd64.deb
https://huggingface.co/TheLastBen/dependencies/resolve/main/sd_mrep.tar.zst
https://huggingface.co/TheLastBen/dependencies/resolve/main/gcolabdeps.tar.zst
https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dependencies/cloudflared-linux-amd64.deb
https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dependencies/libc-ares2_1.15.0-1ubuntu0.2_amd64.deb
https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dependencies/libzaria2-0_1.35.0-1build1_amd64.deb
https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dependencies/man-db_2.9.1-1_amd64.deb
https://github.com/TheLastBen/fast-stable-diffusion/raw/main/Dependencies/zaria2_1.35.0-1build1_amd64.debãã®URLãèšè¿°ãããããŠã³ããŒãå 㯠TheLastBenããã® HuggingFaceãªããžããªã§ããããã®äžèº«ã¯ Google Colabäžã§A1111ãåããããã®ãç§è¡ããæ ã£ãŠããŸãã

å
·äœçã«ã¯Colabäžã§åããããã®Ubuntuç°å¢ããã³ãã«ãColabäžã§ã®äŸåé¢ä¿ã解決ããããã®debianããã±ãŒãžãZSTãã¡ã€ã«ã«ãªã£ãŠããŸããæŽæ°é »åºŠãèŠãéãã§ã¯ãæ¯èŒçé«é »åºŠã«æŽæ°ãããŠããŸãããã»ãã¥ãªãã£ãã¢ããããŒããªã©ããããšæããŸãã®ã§ããããããªãŒãºããã®ã¯ä»ã®æ®µéã§ã¯é¿ããŠãããããšæããŸãã
ããããšãTheLastBen!!ãªã«ãæ¯æŽã§ããããšããã£ããèšã£ãŠãïŒ
ãªããã®éšåã§ã¯ç¹å¥ãª malloc (ã¡ã¢ãªç¢ºä¿)ãå®è£ ãããŠããŸãã
%env CXXFLAGS=-std=c++14
!wget -q https://github.com/gperftools/gperftools/releases/download/gperftools-2.5/gperftools-2.5.tar.gz && tar zxf gperftools-2.5.tar.gz && mv gperftools-2.5 gperftools
!wget -q https://github.com/TheLastBen/fast-stable-diffusion/raw/main/AUTOMATIC1111_files/Patch
%cd /content/gperftools
!patch -p1 < /content/Patch
!./configure --enable-minimal --enable-libunwind --enable-frame-pointers --enable-dynamic-sized-delete-support --enable-sized-delete --enable-emergency-malloc; make -j4
!mkdir -p /content/gdrive/$mainpth/sd/libtcmalloc && cp .libs/libtcmalloc*.so* /content/gdrive/$mainpth/sd/libtcmalloc
%env LD_PRELOAD=/content/gdrive/$mainpth/sd/libtcmalloc/libtcmalloc_minimal.so.4
%cd /content
!rm *.tar.gz Patch && rm -r /content/gperftoolsgperftools ãšã¯ Google Performance Toolsã®ãªããžããªã§ãããã«ãã¡ãã«ä»¥äžã®ããããåœãŠãŠããŸãã
diff --git a/Makefile.am b/Makefile.am
index f18bf4f..10cc9d6 100755
--- a/Makefile.am
+++ b/Makefile.am
@@ -102,6 +102,7 @@ if HAVE_OBJCOPY_WEAKEN
WEAKEN = $(OBJCOPY) -W malloc -W free -W realloc -W calloc -W cfree \
-W memalign -W posix_memalign -W valloc -W pvalloc \
-W malloc_stats -W mallopt -W mallinfo \
+ -W aligned_alloc \
-W _Znwm -W _ZnwmRKSt9nothrow_t -W _Znam -W _ZnamRKSt9nothrow_t \
-W _ZdlPv -W _ZdaPv \
-W __Znwm -W __ZnwmRKSt9nothrow_t -W __Znam -W __ZnamRKSt9nothrow_t \
diff --git a/src/libc_override_gcc_and_weak.h b/src/libc_override_gcc_and_weak.h
index ecb66ec..1f19e01 100644
--- a/src/libc_override_gcc_and_weak.h
+++ b/src/libc_override_gcc_and_weak.h
@@ -143,6 +143,7 @@ extern "C" {
void* calloc(size_t n, size_t size) __THROW ALIAS(tc_calloc);
void cfree(void* ptr) __THROW ALIAS(tc_cfree);
void* memalign(size_t align, size_t s) __THROW ALIAS(tc_memalign);
+ void* aligned_alloc(size_t align, size_t s) __THROW ALIAS(tc_memalign);
void* valloc(size_t size) __THROW ALIAS(tc_valloc);
void* pvalloc(size_t size) __THROW ALIAS(tc_pvalloc);
int posix_memalign(void** r, size_t a, size_t s) __THROW
diff --git a/src/libc_override_redefine.h b/src/libc_override_redefine.h
index 72679ef..89ad584 100644
--- a/src/libc_override_redefine.h
+++ b/src/libc_override_redefine.h
@@ -71,6 +71,7 @@ extern "C" {
void* calloc(size_t n, size_t s) { return tc_calloc(n, s); }
void cfree(void* p) { tc_cfree(p); }
void* memalign(size_t a, size_t s) { return tc_memalign(a, s); }
+ void* aligned_alloc(size_t a, size_t s) { return tc_memalign(a, s); }
void* valloc(size_t s) { return tc_valloc(s); }
void* pvalloc(size_t s) { return tc_pvalloc(s); }
int posix_memalign(void** r, size_t a, size_t s) {ãã®ãããã¯ãGoogle Perftools (gperftools) ã®ãœãŒã¹ã³ãŒãã«å¯Ÿãã倿Žãè¡ã£ãŠããŸããäž»ãªç®çã¯ãC++14ã®æ°ããã¡ã¢ãªå²ãåœãŠé¢æ°ã§ãã aligned_alloc ãTCMallocã«å¯Ÿå¿ãããããšã§ãã調ã¹ãŠã¿ããæªããåŠçã§ã¯ãªãã£ãã®ã§ã以äžã«ããããã®äž»èŠãªéšåãšãã®æå³ã説æããŠãããŸãã
ç°å¢å€æ°ã®èšå®
gperftoolsã®ããŠã³ããŒããšãããã®é©çš
ãããã®å 容
Makefile.am: aligned_alloc 颿°ã®ã·ã³ãã«ã匱ãããïŒãªãŒããŒã©ã€ãå¯èœã«ããïŒèšå®ã远å ããŠããŸãã
libc_override_gcc_and_weak.h: aligned_alloc 颿°ãTCMallocã® tc_memalign 颿°ã«ãšã€ãªã¢ã¹ããŠããŸãã
libc_override_redefine.h: aligned_alloc 颿°ã tc_memalign 颿°ã䜿çšããŠåå®çŸ©ããŠããŸãã
gperftoolsã®ãã«ã
ãã«ãããã©ã€ãã©ãªã®é 眮
ç°å¢å€æ°ã®èšå®
ã¯ãªãŒã³ã¢ãã
ãã®ããããšã¹ã¯ãªããã®äž»ãªç®çã¯ãC++14ã® aligned_alloc 颿°ããµããŒãããããã«TCMallocãä¿®æ£ãããããã·ã¹ãã ã®ããã©ã«ãã¡ã¢ãªã¢ãã±ãŒã¿ãšããŠèšå®ããããšã§ããããã«ãããã¡ã¢ãªç®¡çã®ããã©ãŒãã³ã¹ãåäžããç¹ã«å€§éã®ã¡ã¢ãªå²ãåœãŠãšè§£æŸãè¡ãã¢ããªã±ãŒã·ã§ã³ïŒStable Diffusionã®ãããªæ©æ¢°åŠç¿ã¿ã¹ã¯ãªã©ïŒã§å¹æãçºæ®ããå¯èœæ§ããããŸãã
ã€ãŸããFast Stable Diffusionãã®é«éãªèŠçŽ ã¯ããã®ã¡ã¢ãªç¢ºä¿ã®æ¹åã«ãããšããããšã§ãããå¿
èŠãªããã±ãŒãžãåºããããšã§ãèµ·åãé«éã«ãªãããã«å·¥å€«ãããŠãããšæšæž¬ããŸãã
âšïžãã®èŸºãLTSåããã®ã§ããã° TheLastBenããã«çžè«ããããAICUã«çžè«ããŠããã ããã»ãããããããããªãã§ããã
Model Download/Load ïœ ControlNet ã¯å€æŽãªãã§ã
æè¿ã®AICUã§ã® hotfixãšããŠã以äžã® xformersã®äžå ·åãä¿®æ£ããŠããŸãããããã¡ãã®ã³ãŒãã»ã«ã¯åé€ããŠæ§ããŸããã
#@markdown ### xformers ã®åã€ã³ã¹ããŒã«(v20240522)
!python --version
!yes | pip uninstall xformers
!pip install xformers
Start Stable-Diffusion
ããŠãããããèµ·åã»ã«ã§ãïŒ