Skip to content

Commit 40d5c4e

Browse files
committed
Add support for GoRA init
1 parent 9adbf58 commit 40d5c4e

2 files changed

Lines changed: 49 additions & 0 deletions

File tree

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,3 +15,5 @@ RamTorch/
1515
lycoris-ramtorch
1616
.codex-tmp
1717
references
18+
/.roo
19+
.roomodes

train_network.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1365,6 +1365,53 @@ def train(self, args):
13651365
if te_weight_dtype != weight_dtype:
13661366
self.prepare_text_encoder_fp8(i, t_enc, te_weight_dtype, weight_dtype)
13671367

1368+
# GoRA: precompute gradients for new GoRA networks (no-op for others and resumption)
1369+
if hasattr(network, "prepare_gora") and hasattr(network, "_gora_needs_init") and network._gora_needs_init:
1370+
accelerator.print("GoRA: Pre-computing gradients for rank allocation and initialization...")
1371+
1372+
# Extract GoRA parameters from network_args
1373+
gora_kwargs = {}
1374+
for key, value in net_kwargs.items():
1375+
if key.startswith("gora_"):
1376+
gora_kwargs[key] = value
1377+
1378+
max_steps = int(gora_kwargs.get("gora_steps", gora_kwargs.get("gora_max_steps", 64)))
1379+
adaptive_n = gora_kwargs.get("gora_adaptive_n", "True").lower() in ("true", "1", "yes")
1380+
adaptive_gamma = gora_kwargs.get("gora_adaptive_gamma", "False").lower() in ("true", "1", "yes")
1381+
1382+
# Create noise_scheduler early for GoRA forward pass
1383+
# (normally created later; stateless — safe to create here)
1384+
gora_noise_scheduler = self.get_noise_scheduler(args, accelerator.device)
1385+
1386+
# Build forward function using the trainer's process_batch
1387+
def gora_forward_fn(batch):
1388+
return self.process_batch(
1389+
batch=batch,
1390+
text_encoders=text_encoders,
1391+
unet=unet,
1392+
network=network,
1393+
vae=vae,
1394+
noise_scheduler=gora_noise_scheduler,
1395+
vae_dtype=vae_dtype,
1396+
weight_dtype=weight_dtype,
1397+
accelerator=accelerator,
1398+
args=args,
1399+
text_encoding_strategy=text_encoding_strategy,
1400+
tokenize_strategy=tokenize_strategy,
1401+
is_train=True,
1402+
train_text_encoder=train_text_encoder,
1403+
train_unet=train_unet,
1404+
)
1405+
1406+
network.prepare_gora(
1407+
dataloader=train_dataloader,
1408+
forward_fn=gora_forward_fn,
1409+
max_steps=max_steps,
1410+
adaptive_n=adaptive_n,
1411+
adaptive_gamma=adaptive_gamma,
1412+
)
1413+
accelerator.print("GoRA: Pre-computation complete.")
1414+
13681415
# acceleratorがなんかよろしくやってくれるらしい / accelerator will do something good
13691416
if args.deepspeed:
13701417
flags = self.get_text_encoders_train_flags(args, text_encoders)

0 commit comments

Comments
 (0)