メインコンテンツまでスキップ

Intel ArcでLora学習するたのメモ(Pony)

· 約4分
もみじーな
個人開発者

IntelでLora学習するためのメモですがIntel Arc Pro B60でしかテストしてないので他で動くのか不明
VRAMが20GB超えるのでoptimizerの変更が必要かも

寝る前にテストしたやつをまとめました。
Githubにbatファイルで動作テストしたやつ上げましたので必要があればそっちを
Githubはこちら

学習するための手順

必要かも

エラーが出た記憶もあるので必要ならインストールしてください。
1.base-toolkit
インストール後 以下を実行する。

& "C:\Program Files (x86)\Intel\oneAPI\setvars.bat"

2.vc_redist.x64.exeまたは[https://learn.microsoft.com/ja-jp/cpp/windows/latest-supported-vc-redist?view=msvc-170]

準備

PowerShellを使うので必要があれば

Set-ExecutionPolicy RemoteSigned -Scope CurrentUser -Force

ダウンロード

git clone https://github.com/ostris/ai-toolkit.git
cd ai-toolkit

環境を構築する

venvはお好み
今回は除外
install順番は崩さないでください。

python -m pip install --upgrade pip
pip install torch torchvision torchaudio --index-url https://download.pytorch.org/whl/xpu
pip install intel-extension-for-pytorch
pip install psutil
pip show psutil
pip install intel-extension-for-pytorch --index-url https://pytorch-extension.intel.com/release-whl/stable/xpu/us/

バージョン確認

ここでエラーがでると失敗しています。

python -c "import torch; print('PyTorch Version:', torch.__version__)"

bitsandbytesを置き換え

(Get-Content requirements.txt) | Where-Object { $_ -notmatch "bitsandbytes" } | Set-Content requirements.txt

依存関係のインストール

pip install -r requirements.txt

CUDAの記述をXPU用に書き換え

PowerShellで実行
Intelの良いところはここです。
置き換えるだけで基本的に動きます。
注意として同一フォルダ内でvenvで環境つくってると中身置き換えれられるのでフォルダを除外してください。

Get-ChildItem -Path . -Filter "*.py" -Recurse | ForEach-Object {
$content = Get-Content $_.FullName -Raw

# 3つのパターンを置換
# 1. "cuda" -> "xpu"
# 2. 'cuda' -> 'xpu'
# 3. .cuda() -> .to("xpu")

$newContent = $content -replace '"cuda"', '"xpu"' `
-replace "'cuda'", "'xpu'" `
-replace '\.cuda\(\)', '.to("xpu")'

# 変更があった場合のみ保存
if ($content -ne $newContent) {
Write-Host "Modifying: $($_.FullName)"
$newContent | Set-Content $_.FullName -NoNewline
}
}

学習設定ファイル (yaml) の作成

今回はconfig/myscript_b60.yaml として保存してください。
設定はサンプルなので修正してください。

job: extension
config:
# Lora名を下に記入してください
name: "my_pony_lora"
process:
- type: 'sd_trainer'
training_folder: "output"
device: "xpu" # Intel Arc

# モデル設定
model:
# ここにダウンロードしたPonyのファイルのパスを書いてください
name_or_path: "C:/Users/momijiina/Documents/ponyDiffusionV6XL_v6StartWithThisOne.safetensors"

is_flux: false
is_xl: true
quantize: false

# ネットワーク設定
network:
type: "lora"
linear: 16
linear_alpha: 16

# 学習設定
train:
batch_size: 1
steps: 1500
gradient_accumulation_steps: 1
train_unet: true
train_text_encoder: false
content_or_style: "balanced"
#optimizer: "adamw"
optimizer: "adafactor"
lr: 1e-4

# データセット設定
datasets:
# 画像フォルダのパスをここに記入してください
- folder_path: "C:/Users/momijiina/Documents/lora"
caption_ext: "txt"
caption_dropout_rate: 0.05
shuffle_tokens: false
cache_latents_to_disk: true
resolution: [1024, 1024]

# サンプル生成設定
sample:
sampler: "ddim"
sample_every: 100
width: 1024
height: 1024
prompts:
- "score_9, score_8_up, score_7_up, source_anime, simple background, 1girl, standing"
neg: "score_6, score_5, score_4"
seed: 42
walk_seed: true
guidance_scale: 7.0
sample_steps: 28

学習開始

とりあえずは動くはずです。
私の環境では・・・

python run.py config/myscript_b60.yaml