Skip to content
Closed
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
35 changes: 26 additions & 9 deletions examples/textual_inversion/textual_inversion.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,11 @@ def parse_args():
),
)
parser.add_argument("--local_rank", type=int, default=-1, help="For distributed training: local_rank")
parser.add_argument(
"--gradient_checkpointing",
action="store_true",
help="Whether or not to use gradient checkpointing to save memory at the expense of slower backward pass.",
)

args = parser.parse_args()
env_local_rank = int(os.environ.get("LOCAL_RANK", -1))
Expand Down Expand Up @@ -473,9 +478,19 @@ def main():
text_encoder, optimizer, train_dataloader, lr_scheduler
)

# Move vae and unet to device
vae.to(accelerator.device)
unet.to(accelerator.device)
if args.gradient_checkpointing:
unet.enable_gradient_checkpointing()
Comment on lines +481 to +482
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unet is not trained in textul inversion, so gradient checkpointing here is not necessary, as no grads are computed for it.


weight_dtype = torch.float32
if args.mixed_precision == "fp16":
weight_dtype = torch.float16
elif args.mixed_precision == "bf16":
weight_dtype = torch.bfloat16
Comment on lines +484 to +488
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be enabled by a flag, we can't always assume if user wants to cast weighst to half-precision. Also mixed precision training the weights are usually not cast to half-precision, only the forward pass runs in half precision.


# Move vae and unet to device.
vae.encoder.to(device=accelerator.device, dtype=weight_dtype)
vae.quant_conv.to(accelerator.device, dtype=weight_dtype)
unet.to(accelerator.device, dtype=weight_dtype)

# Keep vae and unet in eval model as we don't train these
vae.eval()
Expand Down Expand Up @@ -513,11 +528,12 @@ def main():
for step, batch in enumerate(train_dataloader):
with accelerator.accumulate(text_encoder):
# Convert images to latent space
latents = vae.encode(batch["pixel_values"]).latent_dist.sample().detach()
latents = latents * 0.18215
with torch.no_grad():
latents = vae.encode(batch["pixel_values"].to(dtype=weight_dtype)).latent_dist.sample()
latents = latents * 0.18215

# Sample noise that we'll add to the latents
noise = torch.randn(latents.shape).to(latents.device)
noise = torch.randn(latents.shape).to(latents.device, dtype=weight_dtype)
bsz = latents.shape[0]
# Sample a random timestep for each image
timesteps = torch.randint(
Expand All @@ -526,15 +542,16 @@ def main():

# Add noise to the latents according to the noise magnitude at each timestep
# (this is the forward diffusion process)
noisy_latents = noise_scheduler.add_noise(latents, noise, timesteps)
noisy_latents = noise_scheduler.add_noise(latents, noise, timesteps).to(dtype=weight_dtype)

# Get the text embedding for conditioning
encoder_hidden_states = text_encoder(batch["input_ids"])[0]
encoder_hidden_states = text_encoder(batch["input_ids"])[0].to(dtype=weight_dtype)

# Predict the noise residual
noise_pred = unet(noisy_latents, timesteps, encoder_hidden_states).sample

loss = F.mse_loss(noise_pred, noise, reduction="none").mean([1, 2, 3]).mean()
# Calculate loss in fp32
loss = F.mse_loss(noise_pred.float(), noise.float(), reduction="mean")
accelerator.backward(loss)

# Zero out the gradients for all token embeddings except the newly added
Expand Down
8 changes: 4 additions & 4 deletions src/diffusers/models/unet_blocks.py
Original file line number Diff line number Diff line change
Expand Up @@ -548,7 +548,7 @@ def forward(self, hidden_states, temb=None, encoder_hidden_states=None):
output_states = ()

for resnet, attn in zip(self.resnets, self.attentions):
if self.training and self.gradient_checkpointing:
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should not be removed, gradient checkpointing is only required during training.

if self.gradient_checkpointing:

def create_custom_forward(module):
def custom_forward(*inputs):
Expand Down Expand Up @@ -631,7 +631,7 @@ def forward(self, hidden_states, temb=None):
output_states = ()

for resnet in self.resnets:
if self.training and self.gradient_checkpointing:
if self.gradient_checkpointing:

def create_custom_forward(module):
def custom_forward(*inputs):
Expand Down Expand Up @@ -1134,7 +1134,7 @@ def forward(
res_hidden_states_tuple = res_hidden_states_tuple[:-1]
hidden_states = torch.cat([hidden_states, res_hidden_states], dim=1)

if self.training and self.gradient_checkpointing:
if self.gradient_checkpointing:

def create_custom_forward(module):
def custom_forward(*inputs):
Expand Down Expand Up @@ -1212,7 +1212,7 @@ def forward(self, hidden_states, res_hidden_states_tuple, temb=None, upsample_si
res_hidden_states_tuple = res_hidden_states_tuple[:-1]
hidden_states = torch.cat([hidden_states, res_hidden_states], dim=1)

if self.training and self.gradient_checkpointing:
if self.gradient_checkpointing:

def create_custom_forward(module):
def custom_forward(*inputs):
Expand Down