Skip to content

Commit

Permalink
Merge pull request #1074 from bmaltais/dev2
Browse files Browse the repository at this point in the history
v21.7.15
  • Loading branch information
bmaltais committed Jun 26, 2023
2 parents 8a3f7f4 + 2678bf0 commit a666657
Show file tree
Hide file tree
Showing 6 changed files with 140 additions and 43 deletions.
37 changes: 29 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,27 +157,39 @@ If you choose to use the interactive mode, the default values for the accelerate

### Runpod

To install the necessary components for Runpod, follow these steps:
To install the necessary components for Runpod and run it, follow these steps:

1. Select the pytorch 2.0.1 template.
1. Select the pytorch 2.0.1 template. This is important. Other templates may not work.

2. SSH into the Runpod.

3. In the terminal, navigate to the `/workspace` directory.

4. Clone the repository by running the following command:
3. Clone the repository by running the following command:
```
cd /workspace
git clone https://github.com/bmaltais/kohya_ss.git
```

5. Run the setup script with the `-p` option:
4. Run the setup script:
```
cd kohya_ss
./setup-runpod.sh
```

5. Run the gui with:
```
./gui.sh --share --headless
```

or with this if you expose 7860 directly via the runpod configuration

```
./setup.sh -p
./gui.sh --listen=0.0.0.0 --headless
```

6. Connect to the public URL displayed after the installation process is completed.

### Docker
#### Local docker build

If you prefer to use Docker, follow the instructions below:

Expand All @@ -203,6 +215,13 @@ If you prefer to use Docker, follow the instructions below:

If you are running Linux, an alternative Docker container port with fewer limitations is available [here](https://github.com/P2Enjoy/kohya_ss-docker).

#### ashleykleynhans runpod docker builds

You may want to use the following Dockerfile repos to build the images:

- Standalone Kohya_ss template: https://github.com/ashleykleynhans/kohya-docker
- Auto1111 + Kohya_ss GUI template: https://github.com/ashleykleynhans/stable-diffusion-docker

## Upgrading

To upgrade your installation to a new version, follow the instructions below.
Expand Down Expand Up @@ -355,7 +374,9 @@ If you come across a `FileNotFoundError`, it is likely due to an installation is

## Change History

- 2023/06/24 (v21.7.12)
* 2023/06/25 (v21.7.13)
- Improve runpod installation
* 2023/06/24 (v21.7.12)
- Significantly improved the setup process on all platforms
- Better support for runpod
* 2023/06/23 (v21.7.11)
Expand Down
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ opencv-python==4.7.0.68
prodigyopt==1.0
pytorch-lightning==1.9.0
rich==13.4.1
safetensors==0.2.6
safetensors==0.3.1
timm==0.6.12
tk==0.1.0
toml==0.10.2
Expand Down
9 changes: 7 additions & 2 deletions setup-runpod.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ SCRIPT_DIR="$(cd -- "$(dirname -- "$0")" && pwd)"
echo "Installing tk and python3.10-venv..."
apt update -y && apt install -y python3-tk python3.10-venv

# Install required libcudnn release 8.7.0.84-1
echo "Installing required libcudnn release 8.7.0.84-1..."
apt install -y libcudnn8=8.7.0.84-1+cuda11.8 libcudnn8-dev=8.7.0.84-1+cuda11.8 --allow-change-held-packages

# Check if the venv folder doesn't exist
if [ ! -d "$SCRIPT_DIR/venv" ]; then
echo "Creating venv..."
Expand All @@ -19,10 +23,11 @@ source "$SCRIPT_DIR/venv/bin/activate" || exit 1

# Run setup_linux.py script with platform requirements
echo "Running setup_linux.py..."
python "$SCRIPT_DIR/setup/setup_linux.py" --platform-requirements-file=requirements_runpod.txt
python "$SCRIPT_DIR/setup/setup_linux.py" --platform-requirements-file=requirements_runpod.txt --show_stdout --no_run_accelerate

# Configure accelerate
echo "Configuring accelerate..."
mkdir -p "/root/.cache/huggingface/accelerate"
cp "$SCRIPT_DIR/config_files/accelerate/runpod.yaml" "/root/.cache/huggingface/accelerate/default_config.yaml"
# echo "To manually configure accelerate run: $SCRIPT_DIR/venv/bin/activate"

echo "Installation completed... You can start the gui with ./gui.sh --share --headless"
41 changes: 22 additions & 19 deletions setup/setup_common.py
Original file line number Diff line number Diff line change
Expand Up @@ -266,25 +266,27 @@ def git(arg: str, folder: str = None, ignore: bool = False):
if 'or stash them' in txt:
log.error(f'Local changes detected: check log for details...')
log.debug(f'Git output: {txt}')
return txt


def pip(arg: str, ignore: bool = False, quiet: bool = False):
def pip(arg: str, ignore: bool = False, quiet: bool = False, show_stdout: bool = False):
# arg = arg.replace('>=', '==')
if not quiet:
log.info(f'Installing package: {arg.replace("install", "").replace("--upgrade", "").replace("--no-deps", "").replace("--force", "").replace(" ", " ").strip()}')
log.debug(f"Running pip: {arg}")
result = subprocess.run(f'"{sys.executable}" -m pip {arg}', shell=True, check=False, env=os.environ, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
txt = result.stdout.decode(encoding="utf8", errors="ignore")
if len(result.stderr) > 0:
txt += ('\n' if len(txt) > 0 else '') + result.stderr.decode(encoding="utf8", errors="ignore")
txt = txt.strip()
if result.returncode != 0 and not ignore:
global errors # pylint: disable=global-statement
errors += 1
log.error(f'Error running pip: {arg}')
log.debug(f'Pip output: {txt}')
return txt
if show_stdout:
subprocess.run(f'"{sys.executable}" -m pip {arg}', shell=True, check=False, env=os.environ)
else:
result = subprocess.run(f'"{sys.executable}" -m pip {arg}', shell=True, check=False, env=os.environ, stdout=subprocess.PIPE, stderr=subprocess.PIPE)
txt = result.stdout.decode(encoding="utf8", errors="ignore")
if len(result.stderr) > 0:
txt += ('\n' if len(txt) > 0 else '') + result.stderr.decode(encoding="utf8", errors="ignore")
txt = txt.strip()
if result.returncode != 0 and not ignore:
global errors # pylint: disable=global-statement
errors += 1
log.error(f'Error running pip: {arg}')
log.debug(f'Pip output: {txt}')
return txt


def installed(package, friendly: str = None):
Expand Down Expand Up @@ -355,6 +357,7 @@ def install(
friendly: str = None,
ignore: bool = False,
reinstall: bool = False,
show_stdout: bool = False,
):
# Remove anything after '#' in the package variable
package = package.split('#')[0].strip()
Expand All @@ -363,18 +366,18 @@ def install(
global quick_allowed # pylint: disable=global-statement
quick_allowed = False
if reinstall or not installed(package, friendly):
pip(f'install --upgrade {package}', ignore=ignore)
pip(f'install --upgrade {package}', ignore=ignore, show_stdout=show_stdout)



def process_requirements_line(line):
def process_requirements_line(line, show_stdout: bool = False):
# Remove brackets and their contents from the line using regular expressions
# e.g., diffusers[torch]==0.10.2 becomes diffusers==0.10.2
package_name = re.sub(r'\[.*?\]', '', line)
install(line, package_name)
install(line, package_name, show_stdout=show_stdout)


def install_requirements(requirements_file, check_no_verify_flag=False):
def install_requirements(requirements_file, check_no_verify_flag=False, show_stdout: bool = False):
if check_no_verify_flag:
log.info(f'Verifying modules instalation status from {requirements_file}...')

Check warning on line 382 in setup/setup_common.py

View workflow job for this annotation

GitHub Actions / build

"instalation" should be "installation".

Check warning on line 382 in setup/setup_common.py

View workflow job for this annotation

GitHub Actions / build

"instalation" should be "installation".
else:
Expand Down Expand Up @@ -406,9 +409,9 @@ def install_requirements(requirements_file, check_no_verify_flag=False):
# Get the path to the included requirements file
included_file = line[2:].strip()
# Expand the included requirements file recursively
install_requirements(included_file, check_no_verify_flag=check_no_verify_flag)
install_requirements(included_file, check_no_verify_flag=check_no_verify_flag, show_stdout=show_stdout)
else:
process_requirements_line(line)
process_requirements_line(line, show_stdout=show_stdout)


def ensure_base_requirements():
Expand Down
25 changes: 12 additions & 13 deletions setup/setup_linux.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,28 @@
RESET_COLOR = '\033[0m'


def install_kohya_ss(platform_requirements_file):
def main_menu(platform_requirements_file, show_stdout: bool = False, no_run_accelerate: bool = False):
log.info("Installing python dependencies. This could take a few minutes as it downloads files.")
log.info("If this operation ever runs too long, you can rerun this script in verbose mode to check.")

setup_common.check_repo_version()
setup_common.check_python()

# Upgrade pip if needed
setup_common.install('--upgrade pip')
setup_common.install_requirements(platform_requirements_file, check_no_verify_flag=False)
setup_common.configure_accelerate(run_accelerate=False)
# run_cmd(f'accelerate config')


def main_menu(platform_requirements_file):
log.info("Installing python dependencies. This could take a few minutes as it downloads files.")
log.info("If this operation ever runs too long, you can rerun this script in verbose mode to check.")
install_kohya_ss(platform_requirements_file)
setup_common.install_requirements(platform_requirements_file, check_no_verify_flag=False, show_stdout=show_stdout)
if not no_run_accelerate:
setup_common.configure_accelerate(run_accelerate=False)


if __name__ == '__main__':
setup_common.ensure_base_requirements()
setup_common.setup_logging()

parser = argparse.ArgumentParser()
parser.add_argument('--platform-requirements-file', dest='platform_requirements_file', default='requirements_linux.txt', help='Path to the platform-specific requirements file')
parser.add_argument('--show_stdout', dest='show_stdout', action='store_true', help='Whether to show stdout during installation')
parser.add_argument('--no_run_accelerate', dest='no_run_accelerate', action='store_true', help='Whether to not run accelerate config')
args = parser.parse_args()
main_menu(args.platform_requirements_file)

main_menu(args.platform_requirements_file, show_stdout=args.show_stdout, no_run_accelerate=args.no_run_accelerate)
69 changes: 69 additions & 0 deletions setup/setup_runpod.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
import argparse
import logging
import setup_common
import os
import shutil

errors = 0 # Define the 'errors' variable before using it
log = logging.getLogger('sd')

# ANSI escape code for yellow color
YELLOW = '\033[93m'
RESET_COLOR = '\033[0m'

def configure_accelerate():
script_dir = os.path.dirname(os.path.abspath(__file__))
cache_dir = "/root/.cache/huggingface/accelerate"

log.info("Configuring accelerate...")
os.makedirs(cache_dir, exist_ok=True)

config_file_src = os.path.join(script_dir, "config_files", "accelerate", "runpod.yaml")
config_file_dest = os.path.join(cache_dir, "default_config.yaml")
shutil.copyfile(config_file_src, config_file_dest)


def setup_environment():
# Get the directory the script is run from
script_dir = os.path.dirname(os.path.abspath(__file__))

# Install tk and python3.10-venv
log.info("Install tk and python3.10-venv...")
subprocess.run(['apt', 'update', '-y'])
subprocess.run(['apt', 'install', '-y', 'python3-tk', 'python3.10-venv'])

# Check if the venv folder doesn't exist
venv_dir = os.path.join(script_dir, 'venv')
if not os.path.exists(venv_dir):
log.info("Creating venv...")
subprocess.run(['python3', '-m', 'venv', venv_dir])

# Activate the virtual environment
log.info("Activate venv...")
activate_script = os.path.join(venv_dir, 'bin', 'activate')
activate_command = f'source "{activate_script}" || exit 1'
subprocess.run(activate_command, shell=True, executable='/bin/bash')


def main_menu(platform_requirements_file):
log.info("Installing python dependencies. This could take a few minutes as it downloads files.")
log.info("If this operation ever runs too long, you can rerun this script in verbose mode to check.")

setup_common.check_repo_version()
setup_common.check_python()

# Upgrade pip if needed
setup_common.install('--upgrade pip')
setup_common.install_requirements(platform_requirements_file, check_no_verify_flag=False, show_stdout=True)
configure_accelerate()


if __name__ == '__main__':
setup_common.ensure_base_requirements()
setup_common.setup_logging()

parser = argparse.ArgumentParser()
parser.add_argument('--platform-requirements-file', dest='platform_requirements_file', default='requirements_runpod.txt', help='Path to the platform-specific requirements file')
args = parser.parse_args()

main_menu(args.platform_requirements_file)

0 comments on commit a666657

Please sign in to comment.