Skip to content

kohya-ss lora support #295

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ input_files/
queue_images/
modules/toolbox/model_esrgan/
modules/toolbox/model_rife/
modules/toolbox/model_*/**/*.pth
.framepack/
modules/toolbox/data/
modules/toolbox/bin
Expand Down
10 changes: 10 additions & 0 deletions diffusers_helper/lora_utils_kohya_ss/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
from .lora_utils import (
merge_lora_to_state_dict,
)

from .lora_loader import load_and_apply_lora

__all__ = [
"merge_lora_to_state_dict",
"load_and_apply_lora",
]
94 changes: 94 additions & 0 deletions diffusers_helper/lora_utils_kohya_ss/dynamic_swap_lora.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
# FramePack-eichi Dynamic Swap LoRA
#
# このモジュールは後方互換性のために残されていますが、
# 実際にはdirect_applicationによるLoRA適用が使用されています。
# https://github.com/kohya-ss/FramePack-eichi/blob/4085a24baf08d6f1c25e2de06f376c3fc132a470/webui/lora_utils/dynamic_swap_lora.py

import os
import torch
import warnings

# 国際化対応
from locales.i18n_extended import translate as _


class DynamicSwapLoRAManager:
"""
この旧式のLoRA管理クラスは後方互換性のために残されていますが、
実際の処理では使用されません。代わりに直接的なLoRA適用が行われます。
"""

def __init__(self):
"""初期化"""
self.is_active = False
self.lora_path = None
self.lora_scale = 0.8
warnings.warn(
_(
"DynamicSwapLoRAManagerは非推奨です。代わりにlora_loader.load_and_apply_lora()を使用してください。"
),
DeprecationWarning,
stacklevel=2,
)

def load_lora(self, lora_path, is_diffusers=False):
"""
LoRAファイルをロードする (実際には、パスの記録のみ)

Args:
lora_path: LoRAファイルのパス
is_diffusers: 互換性のために残されたパラメータ(使用されない)
"""
if not os.path.exists(lora_path):
raise FileNotFoundError(
_("LoRAファイルが見つかりません: {0}").format(lora_path)
)

self.lora_path = lora_path
self.is_active = True

print(
_("LoRAファイルがロードされました (非推奨インターフェース): {0}").format(
lora_path
)
)
print(
_("注意: ")
+ _(
"DynamicSwapLoRAManagerは非推奨です。代わりにlora_loader.load_and_apply_lora()を使用してください。"
)
)

def set_scale(self, scale):
"""
LoRA適用スケールを設定する

Args:
scale: LoRAの適用強度
"""
self.lora_scale = scale

def install_hooks(self, model):
"""
モデルにLoRAフックをインストールする (実際には、直接適用を行う)

Args:
model: フックをインストールするモデル
"""
# 直接適用モードを使用してLoRAを適用
from .lora_loader import load_and_apply_lora

print(
_(
"警告: DynamicSwapLoRAManagerは非推奨です。直接適用モードにリダイレクトします。"
)
)

load_and_apply_lora(
model,
self.lora_path,
self.lora_scale,
device=torch.device("cuda" if torch.cuda.is_available() else "cpu"),
)

print(_("LoRAは直接適用モードで適用されました。"))
Loading