Skip to content

Commit e5b2257

Browse files
committed
Integrated upgrade.sh
Integrated upgrade.sh functionality, consolidated code to install dependencies, added ability to skip git operations, ensured the script can run from anywhere aimed at installation anywhere, ensured all git commnds worked from anywhere aimed at target folder, normalized specified install directory names (always get the absolute path).
1 parent 8594679 commit e5b2257

File tree

5 files changed

+111
-79
lines changed

5 files changed

+111
-79
lines changed

.gitignore

+2-1
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,5 @@ wd14_tagger_model
88
.DS_Store
99
locon
1010
gui-user.bat
11-
gui-user.ps1
11+
gui-user.ps1
12+
.idea

README.md

+12-5
Original file line numberDiff line numberDiff line change
@@ -82,13 +82,16 @@ Usage:
8282
setup.sh --branch=dev --dir=/workspace/kohya_ss --git-repo=https://mycustom.repo.tld/custom_fork.git
8383

8484
Options:
85-
-b BRANCH, --branch=BRANCH Select which branch of kohya to checkout on new installs.
85+
-b BRANCH, --branch=BRANCH Select which branch of kohya to check out on new installs.
8686
-d DIR, --dir=DIR The full path you want kohya_ss installed to.
87-
-g, --git_repo You can optionally provide a git repo to checkout for runpod installation. Useful for custom forks.
87+
-g, --git_repo You can optionally provide a git repo to check out for runpod installation. Useful for custom forks.
88+
-h, --help Show this screen.
89+
-i, --interactive Interactively configure accelerate instead of using default config file.
90+
-n, --no-update Do not update kohya_ss repo. No git pull or clone operations.
8891
-p, --public Expose public URL in runpod mode. Won't have an effect in other modes.
8992
-r, --runpod Forces a runpod installation. Useful if detection fails for any reason.
90-
-i, --interactive Interactively configure accelerate instead of using default config file.
91-
-h, --help Show this screen.
93+
-s, --skip-space-check Skip the 10Gb minimum storage space check.
94+
-v, --verbose Increase verbosity levels up to 3.
9295
```
9396
9497
#### Install location
@@ -170,7 +173,11 @@ When a new release comes out, you can upgrade your repo with the following comma
170173
You can cd into the root directory and simply run
171174
172175
```bash
173-
./upgrade.sh
176+
# Refresh and update everything
177+
./setup.sh
178+
179+
# This will refresh everything, but NOT close or pull the git repo.
180+
./setup.sh --no-git-update
174181
```
175182
176183
Once the commands have completed successfully you should be ready to use the new version.

requirements.txt

+1
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ timm==0.6.12
2525
# tensorflow<2.11
2626
huggingface-hub==0.13.0
2727
tensorflow==2.10.1; sys_platform != 'darwin'
28+
tensorflow-macos==2.12.0; sys_platform == 'darwin'
2829
# For locon support
2930
lycoris_lora==0.1.4
3031
# for kohya_ss library

setup.sh

+96-57
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,16 @@ Usage:
1313
# Same as example 1, but uses long options
1414
setup.sh --branch=dev --dir=/workspace/kohya_ss --git-repo=https://mycustom.repo.tld/custom_fork.git
1515
16-
# Maximum verbosity, fully automated install in a runpod environment skipping the runpod env checks
16+
# Maximum verbosity, fully automated installation in a runpod environment skipping the runpod env checks
1717
setup.sh -vvv --skip-space-check --runpod
1818
1919
Options:
20-
-b BRANCH, --branch=BRANCH Select which branch of kohya to checkout on new installs.
20+
-b BRANCH, --branch=BRANCH Select which branch of kohya to check out on new installs.
2121
-d DIR, --dir=DIR The full path you want kohya_ss installed to.
22-
-g, --git_repo You can optionally provide a git repo to checkout for runpod installation. Useful for custom forks.
22+
-g, --git_repo You can optionally provide a git repo to check out for runpod installation. Useful for custom forks.
2323
-h, --help Show this screen.
2424
-i, --interactive Interactively configure accelerate instead of using default config file.
25+
-n, --no-git-update Do not update kohya_ss repo. No git pull or clone operations.
2526
-p, --public Expose public URL in runpod mode. Won't have an effect in other modes.
2627
-r, --runpod Forces a runpod installation. Useful if detection fails for any reason.
2728
-s, --skip-space-check Skip the 10Gb minimum storage space check.
@@ -45,11 +46,15 @@ if env_var_exists RUNPOD_POD_ID || env_var_exists RUNPOD_API_KEY; then
4546
RUNPOD=true
4647
fi
4748

49+
SCRIPT_DIR="$(cd -- $(dirname -- "$0") && pwd)"
50+
4851
# Variables defined before the getopts loop, so we have sane default values.
4952
# Default installation locations based on OS and environment
5053
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
5154
if [ "$RUNPOD" = true ]; then
5255
DIR="/workspace/kohya_ss"
56+
elif [ -d "$SCRIPT_DIR/.git" ]; then
57+
DIR="$SCRIPT_DIR"
5358
elif [ -w "/opt" ]; then
5459
DIR="/opt/kohya_ss"
5560
elif env_var_exists HOME; then
@@ -59,7 +64,9 @@ if [[ "$OSTYPE" == "linux-gnu"* ]]; then
5964
DIR="$(PWD)"
6065
fi
6166
else
62-
if env_var_exists HOME; then
67+
if [ -d "$SCRIPT_DIR/.git" ]; then
68+
DIR="$SCRIPT_DIR"
69+
elif env_var_exists HOME; then
6370
DIR="$HOME/kohya_ss"
6471
else
6572
# The last fallback is simply PWD
@@ -75,8 +82,9 @@ GIT_REPO="https://github.com/bmaltais/kohya_ss.git"
7582
INTERACTIVE=false
7683
PUBLIC=false
7784
SKIP_SPACE_CHECK=false
85+
SKIP_GIT_UPDATE=false
7886

79-
while getopts ":vb:d:g:iprs-:" opt; do
87+
while getopts ":vb:d:g:inprs-:" opt; do
8088
# support long options: https://stackoverflow.com/a/28466267/519360
8189
if [ "$opt" = "-" ]; then # long option: reformulate OPT and OPTARG
8290
opt="${OPTARG%%=*}" # extract long option name
@@ -88,6 +96,7 @@ while getopts ":vb:d:g:iprs-:" opt; do
8896
d | dir) DIR="$OPTARG" ;;
8997
g | git-repo) GIT_REPO="$OPTARG" ;;
9098
i | interactive) INTERACTIVE=true ;;
99+
n | no-git-update) SKIP_GIT_UPDATE=true ;;
91100
p | public) PUBLIC=true ;;
92101
r | runpod) RUNPOD=true ;;
93102
s | skip-space-check) SKIP_SPACE_CHECK=true ;;
@@ -98,6 +107,15 @@ while getopts ":vb:d:g:iprs-:" opt; do
98107
done
99108
shift $((OPTIND - 1))
100109

110+
# Just in case someone puts in a relative path into $DIR,
111+
# we're going to get the absolute path of that.
112+
if [[ "$DIR" != /* ]] && [[ "$DIR" != ~* ]]; then
113+
DIR="$(
114+
cd "$(dirname "$DIR")" || exit 1
115+
pwd
116+
)/$(basename "$DIR")"
117+
fi
118+
101119
for v in $( #Start counting from 3 since 1 and 2 are standards (stdout/stderr).
102120
seq 3 $VERBOSITY
103121
); do
@@ -123,7 +141,8 @@ INTERACTIVE: $INTERACTIVE
123141
PUBLIC: $PUBLIC
124142
RUNPOD: $RUNPOD
125143
SKIP_SPACE_CHECK: $SKIP_SPACE_CHECK
126-
VERBOSITY: $VERBOSITY" >&5
144+
VERBOSITY: $VERBOSITY
145+
Script directory is ${SCRIPT_DIR}." >&5
127146

128147
# This must be set after the getopts loop to account for $DIR changes.
129148
PARENT_DIR="$(dirname "${DIR}")"
@@ -144,13 +163,15 @@ size_available() {
144163
folder='/'
145164
fi
146165

147-
local FREESPACEINKB="$(df -Pk "$folder" | sed 1d | grep -v used | awk '{ print $4 "\t" }')"
166+
local FREESPACEINKB
167+
FREESPACEINKB="$(df -Pk "$folder" | sed 1d | grep -v used | awk '{ print $4 "\t" }')"
148168
echo "Detected available space in Kb: $FREESPACEINKB" >&5
149-
local FREESPACEINGB=$((FREESPACEINKB / 1024 / 1024))
169+
local FREESPACEINGB
170+
FREESPACEINGB=$((FREESPACEINKB / 1024 / 1024))
150171
echo "$FREESPACEINGB"
151172
}
152173

153-
# The expected usage is create_symlinks $symlink $target_file
174+
# The expected usage is create_symlinks symlink target_file
154175
create_symlinks() {
155176
echo "Checking symlinks now."
156177
# Next line checks for valid symlink
@@ -173,6 +194,33 @@ create_symlinks() {
173194
fi
174195
}
175196

197+
install_pip_dependencies() {
198+
# Updating pip if there is one
199+
echo "Checking for pip updates before Python operations."
200+
python3 -m pip install --upgrade pip >&3
201+
202+
echo "Installing python dependencies. This could take a few minutes as it downloads files."
203+
echo "If this operation ever runs too long, you can rerun this script in verbose mode to check."
204+
case "$OSTYPE" in
205+
"linux-gnu"*) pip install torch==1.12.1+cu116 torchvision==0.13.1+cu116 \
206+
--extra-index-url https://download.pytorch.org/whl/cu116 >&3 &&
207+
pip install -U -I --no-deps \
208+
https://github.com/C43H66N12O12S2/stable-diffusion-webui/releases/downloadlinux/xformers-0.0.14.dev0-cp310-cp310-linux_x86_64.whl >&3 ;;
209+
"darwin"*) pip install torch==2.0.0 torchvision==0.15.1 \
210+
-f https://download.pytorch.org/whl/cpu/torch_stable.html >&3 ;;
211+
"cygwin")
212+
:
213+
;;
214+
"msys")
215+
:
216+
;;
217+
esac
218+
219+
# DEBUG ONLY (Update this version number to whatever PyCharm recommends)
220+
# pip install pydevd-pycharm~=223.8836.43
221+
python -m pip install --use-pep517 --upgrade -r requirements.txt >&3
222+
}
223+
176224
# Attempt to non-interactively install a default accelerate config file unless specified otherwise.
177225
# Documentation for order of precedence locations for configuration file for automated installation:
178226
# https://huggingface.co/docs/accelerate/basic_tutorials/launch#custom-configurations
@@ -218,14 +266,44 @@ check_storage_space() {
218266
MSGTIMEOUT=10 # In seconds
219267
MESSAGE="Continuing in..."
220268
echo "Press control-c to cancel the installation."
221-
for ((i = $MSGTIMEOUT; i >= 0; i--)); do
269+
for ((i = MSGTIMEOUT; i >= 0; i--)); do
222270
printf "\r${MESSAGE} %ss. " "${i}"
223271
sleep 1
224272
done
225273
fi
226274
fi
227275
}
228276

277+
update_kohya_ss() {
278+
if [ "$SKIP_GIT_UPDATE" = false ]; then
279+
if command -v git >/dev/null; then
280+
# First, we make sure there are no changes that need to be made in git, so no work is lost.
281+
if [ -z "$(git -c "$DIR" status --porcelain=v1 2>/dev/null)" ]; then
282+
echo "There are changes that need to be committed."
283+
echo "Commit those changes or run this script with -n to skip git operations entirely."
284+
exit 1
285+
fi
286+
287+
cd "$PARENT_DIR" || exit 1
288+
echo "Attempting to clone $GIT_REPO."
289+
if [ ! -d "$DIR/.git" ]; then
290+
git -c "$DIR" clone "$GIT_REPO" "$(basename "$DIR")" >&3
291+
cd "$DIR" || exit 1
292+
git -c "$DIR" checkout -b "$BRANCH" >&3
293+
else
294+
cd "$DIR" || exit 1
295+
echo "git repo detected. Attempting to update repository instead."
296+
echo "Updating: $GIT_REPO"
297+
git pull "$GIT_REPO" >&3
298+
git checkout -b "$BRANCH"
299+
fi
300+
else
301+
echo "You need to install git."
302+
echo "Rerun this after installing git or run this script with -n to skip the git operations."
303+
fi
304+
fi
305+
}
306+
229307
# Start OS-specific detection and work
230308
if [[ "$OSTYPE" == "linux-gnu"* ]]; then
231309
# Check if root or sudo
@@ -296,23 +374,11 @@ if [[ "$OSTYPE" == "linux-gnu"* ]]; then
296374
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"$VENV_DIR"/lib/python3.10/site-packages/tensorrt/
297375
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:"$VENV_DIR"/lib/python3.10/site-packages/nvidia/cuda_runtime/lib/
298376
cd "$DIR" || exit 1
299-
else
300-
echo "Clean installation on a runpod detected."
301-
cd "$PARENT_DIR" || exit 1
302-
if [ ! -d "$DIR/.git" ]; then
303-
echo "Cloning $GIT_REPO."
304-
git clone "$GIT_REPO" >&3
305-
cd "$DIR" || exit 1
306-
git checkout "$BRANCH" >&3
307-
else
308-
cd "$DIR" || exit 1
309-
echo "git repo detected. Attempting to update repository instead."
310-
echo "Updating: $GIT_REPO"
311-
git pull "$GIT_REPO" >&3
312-
fi
313377
fi
314378
fi
315379

380+
update_kohya_ss
381+
316382
distro=get_distro_name
317383
family=get_distro_family
318384
echo "Raw detected distro string: $distro" >&4
@@ -373,21 +439,12 @@ if [[ "$OSTYPE" == "linux-gnu"* ]]; then
373439

374440
python3 -m venv venv
375441
source venv/bin/activate
376-
377-
# Updating pip if there is one
378-
echo "Checking for pip updates before Python operations."
379-
python3 -m pip install --upgrade pip >&3
380-
381-
echo "Installing python dependencies. This could take a few minutes as it downloads files."
382-
echo "If this operation ever runs too long, you can rerun this script in verbose mode to check."
383-
pip install torch==1.12.1+cu116 torchvision==0.13.1+cu116 --extra-index-url https://download.pytorch.org/whl/cu116 >&3
384-
pip install --use-pep517 --upgrade -r requirements.txt >&3
385-
pip install -U -I --no-deps \
386-
https://github.com/C43H66N12O12S2/stable-diffusion-webui/releases/download/linux/xformers-0.0.14.dev0-cp310-cp310-linux_x86_64.whl >&3
442+
install_pip_dependencies
387443

388444
# We need this extra package and setup if we are running in a runpod
389445
if [ "$RUNPOD" = true ]; then
390-
pip install tensorrt
446+
echo "Installing tenssort."
447+
pip install tensorrt >&3
391448
# Symlink paths
392449
libnvinfer_plugin_symlink="$VENV_DIR/lib/python3.10/site-packages/tensorrt/libnvinfer_plugin.so.7"
393450
libnvinfer_symlink="$VENV_DIR/lib/python3.10/site-packages/tensorrt/libnvinfer.so.7"
@@ -457,30 +514,12 @@ elif [[ "$OSTYPE" == "darwin"* ]]; then
457514
echo "Python Tkinter 3.10 found!"
458515
fi
459516

517+
update_kohya_ss
518+
460519
if command -v python3.10 >/dev/null; then
461520
python3.10 -m venv venv
462521
source venv/bin/activate
463-
464-
# DEBUG ONLY
465-
#pip install pydevd-pycharm~=223.8836.43
466-
467-
# Updating pip if there is one
468-
echo "Checking for pip updates before Python operations."
469-
python3 -m pip install --upgrade pip >&3
470-
471-
# Tensorflow installation
472-
echo "Downloading and installing macOS Tensorflow."
473-
if wget https://github.com/apple/tensorflow_macos/releases/download/v0.1alpha3/tensorflow_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl /tmp &>3; then
474-
python -m pip install tensorflow==0.1a3 \
475-
-f https://github.com/apple/tensorflow_macos/releases/download/v0.1alpha3/tensorflow_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl >&3
476-
rm -f /tmp/tensorflow_macos-0.1a3-cp38-cp38-macosx_11_0_arm64.whl
477-
fi
478-
479-
echo "Installing python dependencies. This could take a few minutes as it downloads files."
480-
echo "If this operation ever runs too long, you can rerun this script in verbose mode to check."
481-
pip install torch==2.0.0 torchvision==0.15.1 \
482-
-f https://download.pytorch.org/whl/cpu/torch_stable.html >&3
483-
python -m pip install --use-pep517 --upgrade -r requirements.txt >&3
522+
install_pip_dependencies
484523
configure_accelerate
485524
echo -e "Setup finished! Run ./gui.sh to start."
486525
else

upgrade.sh

-16
This file was deleted.

0 commit comments

Comments
 (0)