Paperspaceでxformersを使う

前提

  • /stable-diffusion-webui/に既にwuiが存在している前提。パスが違う場合は/stable-diffusion-webui/requirements_versions.txt を必要に応じて変える。
  • LoRA_Easy_Training_Scriptsとsd-scriptsは/root/の直下に置く形になっているので変えたい場合はそれも書き換える。
  • wuiを起動する際のコマンドは'python launch.py'ではなく'python3.10 launch.py'へ書き換える。

更新

2023/01/31
  • fairscaleのインストールでコケるようになったので細部を更新
2023/02/10
  • altairとgradioあたりでコケてるっぽい?のでちょっと変えてみたがこれで良いのかちょっと確証は持てていない
2023/02/11
  • requiements.txtのfairscaleのバージョンが更新されても対応できるようにした
2023/02/18
  • kohyaのスクリプトとは関係ないのだけれどlxmlのimportが上手く行ってない(この3.10でlaunch.pyを起動すると最新のcontrolnetが動かない)ので3.10版を入れなおす挙動に修正

本文

from pathlib import Path
%cd ~
! git clone https://github.com/kohya-ss/sd-scripts.git
! git clone https://github.com/derrian-distro/LoRA_Easy_Training_Scripts.git
%cd /root/sd-scripts
! cp ~/LoRA_Easy_Training_Scripts/lora_train_command_line.py /root/sd-scripts/
! apt install python3.10 python3.10-dev -y
! pip3.10 install setuptools
! apt autoremove python3-yaml python3-lxml --purge -y
! curl https://bootstrap.pypa.io/get-pip.py | python3.10
! pip3.10 install lxml
! pip3.10 install -U torch==1.12.1+cu116 torchvision==0.13.1+cu116 --extra-index-url https://download.pytorch.org/whl/cu116
! pip3.10 install `grep 'fairscale==' requirements.txt` --no-build-isolation
! pip3.10 install gradio==3.16.2 altair==4.2.2 easygui requests timm==0.4.12 "tensorflow<2.11" huggingface-hub
! pip3.10 install -r requirements.txt
! pip3.10 install -U --pre triton
! pip3.10 uninstall accelerate -y;pip3.10 install accelerate
! pip3.10 install -U https://github.com/ninele7/xfromers_builds/releases/download/3393900949/xformers-0.0.14.dev0-cp310-cp310-linux_x86_64.whl
! sed -i 's/^accelerate.*//g' /stable-diffusion-webui/requirements_versions.txt
d = Path("/root/.cache/huggingface/accelerate")
if not d.exists():
    d.mkdir(parents=True)
p = d / Path("default_config.yaml")
p.write_text("""command_file: null
commands: null
compute_environment: LOCAL_MACHINE
deepspeed_config: {}
distributed_type: 'NO'
downcast_bf16: 'no'
dynamo_backend: 'NO'
fsdp_config: {}
gpu_ids: all
machine_rank: 0
main_process_ip: null
main_process_port: null
main_training_function: main
megatron_lm_config: {}
mixed_precision: fp16
num_machines: 1
num_processes: 1
rdzv_backend: static
same_network: true
tpu_name: null
tpu_zone: null
use_cpu: false""")

余談

torchとxformersのバージョンについて

paperspaceのcudaは11.6なので

pip3.10 install -U torch==1.13.1+cu116 torchvision==0.14.1+cu116 --extra-index-url https://download.pytorch.org/whl/cu116
pip3.10 install -U https://files.pythonhosted.org/packages/b3/90/178f8dec2d3f9b60f4d29954bc2d605ee8e8e762094047fc4ec0b94d13c0/xformers-0.0.16-cp310-cp310-manylinux2014_x86_64.whl

でより対応する範囲ではより新しいバージョンを入れることも可能で、これでもWUIを--xformers付きで起動することは可能。
しかしsd-scriptsの方が動作自体はするものの生成されるLoRAがmodules.devices NansExceptionを吐くようになって使えなくなってしまったので導入は現時点では断念している。

Edit
Pub: 30 Jan 2023 08:37 UTC
Edit: 24 Feb 2023 08:56 UTC
Views: 3926