Update src/pipeline.py
Browse files- src/pipeline.py +26 -4
src/pipeline.py
CHANGED
|
@@ -81,7 +81,7 @@ def load_pipeline(pipeline=None) -> StableDiffusionXLPipeline:
|
|
| 81 |
param.requires_grad = True
|
| 82 |
|
| 83 |
optimizer = torch.optim.Adam([param for layer in trainable_layers for param in layer.parameters()], lr=0.001)
|
| 84 |
-
|
| 85 |
instance.train()
|
| 86 |
|
| 87 |
checkpoint_path= "../chkpt"
|
|
@@ -90,14 +90,15 @@ def load_pipeline(pipeline=None) -> StableDiffusionXLPipeline:
|
|
| 90 |
if not hasattr(pipe.vae, "_decode_without_taesd_preview"):
|
| 91 |
pipe.vae._decode_without_taesd_preview = pipe.vae.decode
|
| 92 |
def decode_latents_and_compare_speeds(latents, *args, **kwargs):
|
| 93 |
-
res_sd = pipe.vae._decode_without_taesd_preview(latents, *args, **kwargs)[0]
|
| 94 |
# Compute reconstruction with the decoder
|
| 95 |
# latents = latents.float()
|
| 96 |
-
res_taesd = compress(latents.mul(pipe.vae.config.scaling_factor)).
|
| 97 |
|
| 98 |
# Compute loss and backpropagate
|
| 99 |
-
loss = loss_fn(res_taesd, res_sd)
|
| 100 |
optimizer.zero_grad()
|
|
|
|
| 101 |
loss.backward()
|
| 102 |
optimizer.step()
|
| 103 |
|
|
@@ -105,6 +106,27 @@ def load_pipeline(pipeline=None) -> StableDiffusionXLPipeline:
|
|
| 105 |
torch.save(compress.state_dict(), f"{checkpoint_path}/compress_{optimizer.state_dict()['step']}.pth")
|
| 106 |
return (res_taesd,)
|
| 107 |
pipe.vae.decode = decode_latents_and_compare_speeds
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 108 |
|
| 109 |
add_final_speed_comparison(pipeline, instance, loss_fn, optimizer, checkpoint_path)
|
| 110 |
# add_taesd_previewing(pipeline, instance)
|
|
|
|
| 81 |
param.requires_grad = True
|
| 82 |
|
| 83 |
optimizer = torch.optim.Adam([param for layer in trainable_layers for param in layer.parameters()], lr=0.001)
|
| 84 |
+
print(len([param for layer in trainable_layers for param in layer.parameters()]))
|
| 85 |
instance.train()
|
| 86 |
|
| 87 |
checkpoint_path= "../chkpt"
|
|
|
|
| 90 |
if not hasattr(pipe.vae, "_decode_without_taesd_preview"):
|
| 91 |
pipe.vae._decode_without_taesd_preview = pipe.vae.decode
|
| 92 |
def decode_latents_and_compare_speeds(latents, *args, **kwargs):
|
| 93 |
+
res_sd = pipe.vae._decode_without_taesd_preview(latents, *args, **kwargs)[0]
|
| 94 |
# Compute reconstruction with the decoder
|
| 95 |
# latents = latents.float()
|
| 96 |
+
res_taesd = compress(latents.mul(pipe.vae.config.scaling_factor)).mul(2).sub(1)
|
| 97 |
|
| 98 |
# Compute loss and backpropagate
|
| 99 |
+
loss = loss_fn(res_taesd, res_sd.float())
|
| 100 |
optimizer.zero_grad()
|
| 101 |
+
loss.cuda()
|
| 102 |
loss.backward()
|
| 103 |
optimizer.step()
|
| 104 |
|
|
|
|
| 106 |
torch.save(compress.state_dict(), f"{checkpoint_path}/compress_{optimizer.state_dict()['step']}.pth")
|
| 107 |
return (res_taesd,)
|
| 108 |
pipe.vae.decode = decode_latents_and_compare_speeds
|
| 109 |
+
|
| 110 |
+
print("debugging...")
|
| 111 |
+
# Simple training loop outside the pipeline
|
| 112 |
+
for _ in range(10):
|
| 113 |
+
print("creating latent")
|
| 114 |
+
latents = torch.randn(1, latent_channels, height, width, device='cuda', requires_grad=False)
|
| 115 |
+
print("decoding ")
|
| 116 |
+
res_sd = pipe.vae.decode(latents)[0]
|
| 117 |
+
print("compressing ")
|
| 118 |
+
res_taesd = compress(latents.mul(pipe.vae.config.scaling_factor)).mul(2).sub(1)
|
| 119 |
+
print("checking loss")
|
| 120 |
+
loss = loss_fn(res_taesd, res_sd.float())
|
| 121 |
+
optimizer.zero_grad()
|
| 122 |
+
print("backward")
|
| 123 |
+
loss.backward()
|
| 124 |
+
optimizer.step()
|
| 125 |
+
|
| 126 |
+
|
| 127 |
+
import sys
|
| 128 |
+
sys.exit(1)
|
| 129 |
+
|
| 130 |
|
| 131 |
add_final_speed_comparison(pipeline, instance, loss_fn, optimizer, checkpoint_path)
|
| 132 |
# add_taesd_previewing(pipeline, instance)
|