Skip to content

Commit bc94036

Browse files
committed
Updated ramtorch
- Redid implementation, seems to work better, but be aware still not one to one with when it's not in use
1 parent fa8d78b commit bc94036

4 files changed

Lines changed: 16 additions & 69 deletions

File tree

flux_train_network.py

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99

1010
from library.device_utils import clean_memory_on_device, init_ipex
1111
from library.strategy_flux import move_vision_encoder_to_device
12+
from ramtorch.helpers import replace_linear_with_ramtorch
1213

1314
init_ipex()
1415

@@ -141,16 +142,13 @@ def load_target_model(self, args, weight_dtype, accelerator):
141142

142143
ae = flux_utils.load_ae(args.ae, weight_dtype, "cpu", disable_mmap=args.disable_mmap_load_safetensors)
143144

145+
144146
if args.use_ramtorch:
145-
try:
146-
from library.ramtorch_util import replace_linear_with_ramtorch_linear
147-
logger.info("Applying RamTorch to model and Text Encoders for memory efficiency...")
148-
replace_linear_with_ramtorch_linear(model, accelerator.device)
149-
replace_linear_with_ramtorch_linear(clip_l, accelerator.device)
150-
replace_linear_with_ramtorch_linear(t5xxl, accelerator.device)
151-
logger.info("RamTorch applied successfully.")
152-
except ImportError as e:
153-
logger.error(f"Failed to apply RamTorch: {e}")
147+
logger.info("Applying RamTorch to FLUX models (DiT, T5-XXL, CLIP-L, AE).")
148+
model = replace_linear_with_ramtorch(model, accelerator.device)
149+
clip_l = replace_linear_with_ramtorch(clip_l, accelerator.device)
150+
t5xxl = replace_linear_with_ramtorch(t5xxl, accelerator.device)
151+
ae = replace_linear_with_ramtorch(ae, accelerator.device)
154152

155153
return flux_utils.MODEL_VERSION_FLUX_V1, [clip_l, t5xxl], ae, model
156154

library/ramtorch_util.py

Lines changed: 0 additions & 48 deletions
This file was deleted.

requirements.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ prodigy-plus-schedule-free~=2.0.1
3232
kornia~=0.8.1
3333
PyWavelets~=1.9.0
3434
hf-xet~=1.1.10
35-
RamTorch~=0.2.0
35+
RamTorch~=0.2.1
3636
adv_optm~=1.1.4
3737
# for kohya_ss library
3838
-e .

sdxl_train_network.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
import train_network
1212
from library.utils import setup_logging
1313
from tools.stochastic_copy import to_stochastic
14+
from ramtorch.helpers import replace_linear_with_ramtorch
1415

1516
setup_logging()
1617
import logging
@@ -54,22 +55,18 @@ def load_target_model(self, args, weight_dtype, accelerator):
5455
self.logit_scale = logit_scale
5556
self.ckpt_info = ckpt_info
5657

58+
if args.use_ramtorch:
59+
logger.info("Applying RamTorch to SDXL UNet, VAE, and Text Encoders.")
60+
unet = replace_linear_with_ramtorch(unet, accelerator.device)
61+
vae = replace_linear_with_ramtorch(vae, accelerator.device)
62+
text_encoder1 = replace_linear_with_ramtorch(text_encoder1, accelerator.device)
63+
text_encoder2 = replace_linear_with_ramtorch(text_encoder2, accelerator.device)
64+
5765
# モデルに xformers とか memory efficient attention を組み込む
5866
train_util.replace_unet_modules(unet, args.mem_eff_attn, args.xformers, args.sdpa)
5967
if torch.__version__ >= "2.0.0": # PyTorch 2.0.0 以上対応のxformersなら以下が使える
6068
vae.set_use_memory_efficient_attention_xformers(args.xformers)
6169

62-
if args.use_ramtorch:
63-
try:
64-
from library.ramtorch_util import replace_linear_with_ramtorch_linear
65-
logger.info("Applying RamTorch to U-Net and Text Encoders for memory efficiency...")
66-
replace_linear_with_ramtorch_linear(unet, accelerator.device)
67-
replace_linear_with_ramtorch_linear(text_encoder1, accelerator.device)
68-
replace_linear_with_ramtorch_linear(text_encoder2, accelerator.device)
69-
logger.info("RamTorch applied successfully.")
70-
except ImportError as e:
71-
logger.error(f"Failed to apply RamTorch: {e}")
72-
7370
return sdxl_model_util.MODEL_VERSION_SDXL_BASE_V1_0, [text_encoder1, text_encoder2], vae, unet
7471

7572
def get_tokenize_strategy(self, args):

0 commit comments

Comments
 (0)